Skip to content

Commit 4d6e246

Browse files
vasildMarcoFalke
authored andcommitted
test: use pointers in denialofservice_tests/peer_discouragement
This is a non-functional change that replaces the `CNode` on-stack variables with `CNode` pointers. The reason for this is that it would allow us to add those `CNode`s to `CConnman::vNodes[]` which in turn would allow us to check that they are disconnected properly - a `CNode` object must be in `CConnman::vNodes[]` in order for its `fDisconnect` flag to be set. If we store pointers to the on-stack variables in `CConnman` then it would crash at the end, trying to `delete` them.
1 parent e08f319 commit 4d6e246

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

src/test/denialofservice_tests.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <test/util/setup_common.h>
2424

25+
#include <array>
2526
#include <stdint.h>
2627

2728
#include <boost/test/unit_test.hpp>
@@ -213,42 +214,54 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
213214
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
214215
*m_node.scheduler, *m_node.chainman, *m_node.mempool, false);
215216

217+
const std::array<CAddress, 2> addr{CAddress{ip(0xa0b0c001), NODE_NONE},
218+
CAddress{ip(0xa0b0c002), NODE_NONE}};
219+
220+
const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[].
221+
222+
std::array<CNode*, 2> nodes;
223+
216224
banman->ClearBanned();
217-
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
218-
CNode dummyNode1(id++, NODE_NETWORK, INVALID_SOCKET, addr1, /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false);
219-
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
220-
peerLogic->InitializeNode(&dummyNode1);
221-
dummyNode1.fSuccessfullyConnected = true;
222-
peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged
225+
nodes[0] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[0], /* nKeyedNetGroupIn */ 0,
226+
/* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "",
227+
ConnectionType::INBOUND, /* inbound_onion */ false};
228+
nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
229+
peerLogic->InitializeNode(nodes[0]);
230+
nodes[0]->fSuccessfullyConnected = true;
231+
peerLogic->Misbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged
223232
{
224-
LOCK(dummyNode1.cs_sendProcessing);
225-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode1));
233+
LOCK(nodes[0]->cs_sendProcessing);
234+
BOOST_CHECK(peerLogic->SendMessages(nodes[0]));
226235
}
227-
BOOST_CHECK(banman->IsDiscouraged(addr1));
228-
BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001|0x0000ff00))); // Different IP, not discouraged
229-
230-
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
231-
CNode dummyNode2(id++, NODE_NETWORK, INVALID_SOCKET, addr2, /* nKeyedNetGroupIn */ 1, /* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false);
232-
dummyNode2.SetCommonVersion(PROTOCOL_VERSION);
233-
peerLogic->InitializeNode(&dummyNode2);
234-
dummyNode2.fSuccessfullyConnected = true;
235-
peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ "");
236+
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
237+
BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
238+
239+
nodes[1] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[1], /* nKeyedNetGroupIn */ 1,
240+
/* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "",
241+
ConnectionType::INBOUND, /* inbound_onion */ false};
242+
nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
243+
peerLogic->InitializeNode(nodes[1]);
244+
nodes[1]->fSuccessfullyConnected = true;
245+
peerLogic->Misbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ "");
236246
{
237-
LOCK(dummyNode2.cs_sendProcessing);
238-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode2));
247+
LOCK(nodes[1]->cs_sendProcessing);
248+
BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
239249
}
240-
BOOST_CHECK(!banman->IsDiscouraged(addr2)); // 2 not discouraged yet...
241-
BOOST_CHECK(banman->IsDiscouraged(addr1)); // ... but 1 still should be
242-
peerLogic->Misbehaving(dummyNode2.GetId(), 1, /* message */ ""); // 2 reaches discouragement threshold
250+
BOOST_CHECK(!banman->IsDiscouraged(addr[1])); // [1] not discouraged yet...
251+
BOOST_CHECK(banman->IsDiscouraged(addr[0])); // ... but [0] still should be
252+
peerLogic->Misbehaving(nodes[1]->GetId(), 1, /* message */ ""); // [1] reaches discouragement threshold
243253
{
244-
LOCK(dummyNode2.cs_sendProcessing);
245-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode2));
254+
LOCK(nodes[1]->cs_sendProcessing);
255+
BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
246256
}
247-
BOOST_CHECK(banman->IsDiscouraged(addr1)); // Expect both 1 and 2
248-
BOOST_CHECK(banman->IsDiscouraged(addr2)); // to be discouraged now
257+
// Expect both [0] and [1] to be discouraged now.
258+
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
259+
BOOST_CHECK(banman->IsDiscouraged(addr[1]));
249260

250-
peerLogic->FinalizeNode(dummyNode1);
251-
peerLogic->FinalizeNode(dummyNode2);
261+
for (CNode* node : nodes) {
262+
peerLogic->FinalizeNode(*node);
263+
delete node;
264+
}
252265
}
253266

254267
BOOST_AUTO_TEST_CASE(DoS_bantime)

0 commit comments

Comments
 (0)