2020
2121#include < boost/test/unit_test.hpp>
2222
23+ struct CConnmanTest : public CConnman {
24+ using CConnman::CConnman;
25+ void AddNode (CNode& node)
26+ {
27+ LOCK (cs_vNodes);
28+ vNodes.push_back (&node);
29+ }
30+ void ClearNodes ()
31+ {
32+ LOCK (cs_vNodes);
33+ for (CNode* node : vNodes) {
34+ delete node;
35+ }
36+ vNodes.clear ();
37+ }
38+ };
39+
2340// Tests these internal-to-net_processing.cpp methods:
2441extern bool AddOrphanTx (const CTransactionRef& tx, NodeId peer);
2542extern void EraseOrphansFor (NodeId peer);
@@ -57,6 +74,8 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
5774// work.
5875BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
5976{
77+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
78+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
6079
6180 // Mock an outbound peer
6281 CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
@@ -109,7 +128,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
109128 peerLogic->FinalizeNode (dummyNode1.GetId (), dummy);
110129}
111130
112- static void AddRandomOutboundPeer (std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
131+ static void AddRandomOutboundPeer (std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman )
113132{
114133 CAddress addr (ip (g_insecure_rand_ctx.randbits (32 )), NODE_NONE);
115134 vNodes.emplace_back (new CNode (id++, ServiceFlags (NODE_NETWORK|NODE_WITNESS), 0 , INVALID_SOCKET, addr, 0 , 0 , CAddress (), " " , /* fInboundIn=*/ false ));
@@ -120,11 +139,14 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
120139 node.nVersion = 1 ;
121140 node.fSuccessfullyConnected = true ;
122141
123- CConnmanTest:: AddNode (node);
142+ connman-> AddNode (node);
124143}
125144
126145BOOST_AUTO_TEST_CASE (stale_tip_peer_management)
127146{
147+ auto connman = MakeUnique<CConnmanTest>(0x1337 , 0x1337 );
148+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
149+
128150 const Consensus::Params& consensusParams = Params ().GetConsensus ();
129151 constexpr int nMaxOutbound = 8 ;
130152 CConnman::Options options;
@@ -137,7 +159,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
137159
138160 // Mock some outbound peers
139161 for (int i=0 ; i<nMaxOutbound; ++i) {
140- AddRandomOutboundPeer (vNodes, *peerLogic);
162+ AddRandomOutboundPeer (vNodes, *peerLogic, connman. get () );
141163 }
142164
143165 peerLogic->CheckForStaleTipAndEvictPeers (consensusParams);
@@ -162,7 +184,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
162184 // If we add one more peer, something should get marked for eviction
163185 // on the next check (since we're mocking the time to be in the future, the
164186 // required time connected check should be satisfied).
165- AddRandomOutboundPeer (vNodes, *peerLogic);
187+ AddRandomOutboundPeer (vNodes, *peerLogic, connman. get () );
166188
167189 peerLogic->CheckForStaleTipAndEvictPeers (consensusParams);
168190 for (int i=0 ; i<nMaxOutbound; ++i) {
@@ -189,11 +211,13 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
189211 peerLogic->FinalizeNode (node->GetId (), dummy);
190212 }
191213
192- CConnmanTest:: ClearNodes ();
214+ connman-> ClearNodes ();
193215}
194216
195217BOOST_AUTO_TEST_CASE (DoS_banning)
196218{
219+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
220+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
197221
198222 connman->ClearBanned ();
199223 CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
@@ -246,6 +270,8 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
246270
247271BOOST_AUTO_TEST_CASE (DoS_banscore)
248272{
273+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
274+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
249275
250276 connman->ClearBanned ();
251277 gArgs .ForceSetArg (" -banscore" , " 111" ); // because 11 is my favorite number
@@ -290,6 +316,8 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
290316
291317BOOST_AUTO_TEST_CASE (DoS_bantime)
292318{
319+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
320+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
293321
294322 connman->ClearBanned ();
295323 int64_t nStartTime = GetTime ();
0 commit comments