20
20
21
21
#include < boost/test/unit_test.hpp>
22
22
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
+
23
40
// Tests these internal-to-net_processing.cpp methods:
24
41
extern bool AddOrphanTx (const CTransactionRef& tx, NodeId peer);
25
42
extern void EraseOrphansFor (NodeId peer);
@@ -57,6 +74,8 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
57
74
// work.
58
75
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
59
76
{
77
+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
78
+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
60
79
61
80
// Mock an outbound peer
62
81
CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
@@ -109,7 +128,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
109
128
peerLogic->FinalizeNode (dummyNode1.GetId (), dummy);
110
129
}
111
130
112
- static void AddRandomOutboundPeer (std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
131
+ static void AddRandomOutboundPeer (std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman )
113
132
{
114
133
CAddress addr (ip (g_insecure_rand_ctx.randbits (32 )), NODE_NONE);
115
134
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
120
139
node.nVersion = 1 ;
121
140
node.fSuccessfullyConnected = true ;
122
141
123
- CConnmanTest:: AddNode (node);
142
+ connman-> AddNode (node);
124
143
}
125
144
126
145
BOOST_AUTO_TEST_CASE (stale_tip_peer_management)
127
146
{
147
+ auto connman = MakeUnique<CConnmanTest>(0x1337 , 0x1337 );
148
+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
149
+
128
150
const Consensus::Params& consensusParams = Params ().GetConsensus ();
129
151
constexpr int nMaxOutbound = 8 ;
130
152
CConnman::Options options;
@@ -137,7 +159,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
137
159
138
160
// Mock some outbound peers
139
161
for (int i=0 ; i<nMaxOutbound; ++i) {
140
- AddRandomOutboundPeer (vNodes, *peerLogic);
162
+ AddRandomOutboundPeer (vNodes, *peerLogic, connman. get () );
141
163
}
142
164
143
165
peerLogic->CheckForStaleTipAndEvictPeers (consensusParams);
@@ -162,7 +184,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
162
184
// If we add one more peer, something should get marked for eviction
163
185
// on the next check (since we're mocking the time to be in the future, the
164
186
// required time connected check should be satisfied).
165
- AddRandomOutboundPeer (vNodes, *peerLogic);
187
+ AddRandomOutboundPeer (vNodes, *peerLogic, connman. get () );
166
188
167
189
peerLogic->CheckForStaleTipAndEvictPeers (consensusParams);
168
190
for (int i=0 ; i<nMaxOutbound; ++i) {
@@ -189,11 +211,13 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
189
211
peerLogic->FinalizeNode (node->GetId (), dummy);
190
212
}
191
213
192
- CConnmanTest:: ClearNodes ();
214
+ connman-> ClearNodes ();
193
215
}
194
216
195
217
BOOST_AUTO_TEST_CASE (DoS_banning)
196
218
{
219
+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
220
+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
197
221
198
222
connman->ClearBanned ();
199
223
CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
@@ -246,6 +270,8 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
246
270
247
271
BOOST_AUTO_TEST_CASE (DoS_banscore)
248
272
{
273
+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
274
+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
249
275
250
276
connman->ClearBanned ();
251
277
gArgs .ForceSetArg (" -banscore" , " 111" ); // because 11 is my favorite number
@@ -290,6 +316,8 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
290
316
291
317
BOOST_AUTO_TEST_CASE (DoS_bantime)
292
318
{
319
+ auto connman = MakeUnique<CConnman>(0x1337 , 0x1337 );
320
+ auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get (), scheduler, false );
293
321
294
322
connman->ClearBanned ();
295
323
int64_t nStartTime = GetTime ();
0 commit comments