Skip to content

Commit

Permalink
Allow unit tests to access additional CConnman members
Browse files Browse the repository at this point in the history
that are otherwise private:
- CConnman::m_nodes
- CConnman::ConnectNodes()
- CConnman::AlreadyConnectedToAddress()

and update the #include headers per iwyu.
  • Loading branch information
jonatack authored and sr-gi committed Oct 30, 2023
1 parent 34b9ef4 commit 4b834f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/test/util/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

#include <test/util/net.h>

#include <chainparams.h>
#include <node/eviction.h>
#include <net.h>
#include <net_processing.h>
#include <netaddress.h>
#include <netmessagemaker.h>
#include <node/connection_types.h>
#include <node/eviction.h>
#include <protocol.h>
#include <random.h>
#include <serialize.h>
#include <span.h>

#include <vector>
Expand Down Expand Up @@ -98,6 +102,17 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) co
return complete;
}

CNode* ConnmanTestMsg::ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
{
CNode* node = ConnectNode(CAddress{}, pszDest, /*fCountFailure=*/false, conn_type, /*use_v2transport=*/true);
if (!node) return nullptr;
node->SetCommonVersion(PROTOCOL_VERSION);
peerman.InitializeNode(*node, ServiceFlags(NODE_NETWORK | NODE_WITNESS));
node->fSuccessfullyConnected = true;
AddTestNode(*node);
return node;
}

std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
{
std::vector<NodeEvictionCandidate> candidates;
Expand Down
29 changes: 27 additions & 2 deletions src/test/util/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@
#define BITCOIN_TEST_UTIL_NET_H

#include <compat/compat.h>
#include <node/eviction.h>
#include <netaddress.h>
#include <net.h>
#include <net_permissions.h>
#include <net_processing.h>
#include <netaddress.h>
#include <node/connection_types.h>
#include <node/eviction.h>
#include <sync.h>
#include <util/sock.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

class FastRandomContext;

template <typename C>
class Span;

struct ConnmanTestMsg : public CConnman {
using CConnman::CConnman;
Expand All @@ -25,6 +39,12 @@ struct ConnmanTestMsg : public CConnman {
m_peer_connect_timeout = timeout;
}

std::vector<CNode*> TestNodes()
{
LOCK(m_nodes_mutex);
return m_nodes;
}

void AddTestNode(CNode& node)
{
LOCK(m_nodes_mutex);
Expand Down Expand Up @@ -56,6 +76,11 @@ struct ConnmanTestMsg : public CConnman {

bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const;
void FlushSendBuffer(CNode& node) const;

bool AlreadyConnectedPublic(const CAddress& addr) { return AlreadyConnectedToAddress(addr); };

CNode* ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
};

constexpr ServiceFlags ALL_SERVICE_FLAGS[]{
Expand Down

0 comments on commit 4b834f6

Please sign in to comment.