mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-10-09 19:13:28 +02:00
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.
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
@@ -213,42 +214,54 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
|
|||||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
|
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
|
||||||
*m_node.scheduler, *m_node.chainman, *m_node.mempool, false);
|
*m_node.scheduler, *m_node.chainman, *m_node.mempool, false);
|
||||||
|
|
||||||
|
const std::array<CAddress, 2> addr{CAddress{ip(0xa0b0c001), NODE_NONE},
|
||||||
|
CAddress{ip(0xa0b0c002), NODE_NONE}};
|
||||||
|
|
||||||
|
const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[].
|
||||||
|
|
||||||
|
std::array<CNode*, 2> nodes;
|
||||||
|
|
||||||
banman->ClearBanned();
|
banman->ClearBanned();
|
||||||
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
|
nodes[0] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[0], /* nKeyedNetGroupIn */ 0,
|
||||||
CNode dummyNode1(id++, NODE_NETWORK, INVALID_SOCKET, addr1, /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false);
|
/* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "",
|
||||||
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
|
ConnectionType::INBOUND, /* inbound_onion */ false};
|
||||||
peerLogic->InitializeNode(&dummyNode1);
|
nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
|
||||||
dummyNode1.fSuccessfullyConnected = true;
|
peerLogic->InitializeNode(nodes[0]);
|
||||||
peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged
|
nodes[0]->fSuccessfullyConnected = true;
|
||||||
|
peerLogic->Misbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged
|
||||||
{
|
{
|
||||||
LOCK(dummyNode1.cs_sendProcessing);
|
LOCK(nodes[0]->cs_sendProcessing);
|
||||||
BOOST_CHECK(peerLogic->SendMessages(&dummyNode1));
|
BOOST_CHECK(peerLogic->SendMessages(nodes[0]));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(banman->IsDiscouraged(addr1));
|
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
|
||||||
BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001|0x0000ff00))); // Different IP, not discouraged
|
BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
|
||||||
|
|
||||||
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
|
nodes[1] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[1], /* nKeyedNetGroupIn */ 1,
|
||||||
CNode dummyNode2(id++, NODE_NETWORK, INVALID_SOCKET, addr2, /* nKeyedNetGroupIn */ 1, /* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false);
|
/* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "",
|
||||||
dummyNode2.SetCommonVersion(PROTOCOL_VERSION);
|
ConnectionType::INBOUND, /* inbound_onion */ false};
|
||||||
peerLogic->InitializeNode(&dummyNode2);
|
nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
|
||||||
dummyNode2.fSuccessfullyConnected = true;
|
peerLogic->InitializeNode(nodes[1]);
|
||||||
peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ "");
|
nodes[1]->fSuccessfullyConnected = true;
|
||||||
|
peerLogic->Misbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ "");
|
||||||
{
|
{
|
||||||
LOCK(dummyNode2.cs_sendProcessing);
|
LOCK(nodes[1]->cs_sendProcessing);
|
||||||
BOOST_CHECK(peerLogic->SendMessages(&dummyNode2));
|
BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(!banman->IsDiscouraged(addr2)); // 2 not discouraged yet...
|
BOOST_CHECK(!banman->IsDiscouraged(addr[1])); // [1] not discouraged yet...
|
||||||
BOOST_CHECK(banman->IsDiscouraged(addr1)); // ... but 1 still should be
|
BOOST_CHECK(banman->IsDiscouraged(addr[0])); // ... but [0] still should be
|
||||||
peerLogic->Misbehaving(dummyNode2.GetId(), 1, /* message */ ""); // 2 reaches discouragement threshold
|
peerLogic->Misbehaving(nodes[1]->GetId(), 1, /* message */ ""); // [1] reaches discouragement threshold
|
||||||
{
|
{
|
||||||
LOCK(dummyNode2.cs_sendProcessing);
|
LOCK(nodes[1]->cs_sendProcessing);
|
||||||
BOOST_CHECK(peerLogic->SendMessages(&dummyNode2));
|
BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(banman->IsDiscouraged(addr1)); // Expect both 1 and 2
|
// Expect both [0] and [1] to be discouraged now.
|
||||||
BOOST_CHECK(banman->IsDiscouraged(addr2)); // to be discouraged now
|
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
|
||||||
|
BOOST_CHECK(banman->IsDiscouraged(addr[1]));
|
||||||
|
|
||||||
peerLogic->FinalizeNode(dummyNode1);
|
for (CNode* node : nodes) {
|
||||||
peerLogic->FinalizeNode(dummyNode2);
|
peerLogic->FinalizeNode(*node);
|
||||||
|
delete node;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(DoS_bantime)
|
BOOST_AUTO_TEST_CASE(DoS_bantime)
|
||||||
|
Reference in New Issue
Block a user