Skip to content

Commit fc5eb52

Browse files
jnewberydergoegge
authored andcommitted
[tests] Connect peer in outbound_slow_chain_eviction by sending p2p messages
Prior to this commit, the peer was connected, and then the services and connectivity fields in the CNode object were manually set. Instead, send p2p `version` and `verack` messages, and have net_processing's internal logic set the state of the node. This ensures that the node's internal state is consistent with how it would be set in the live code. Prior to this commit, `dummyNode1.nServices` was set to `NODE_NONE` which was not a problem since `CNode::fClient` and `CNode::m_limited_node` are default initialised to false. Now that we are doing the actual version handshake, the values of `fClient` and `m_limited_node` are set during the handshake and cause the test to fail if we do not set `dummyNode1.nServices` to a reasonable value (NODE_NETWORK | NODE_WITNESS).
1 parent 1f52c47 commit fc5eb52

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/test/denialofservice_tests.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <serialize.h>
1616
#include <test/util/net.h>
1717
#include <test/util/setup_common.h>
18+
#include <timedata.h>
1819
#include <util/string.h>
1920
#include <util/system.h>
2021
#include <util/time.h>
@@ -44,11 +45,10 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
4445
// work.
4546
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
4647
{
47-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
48+
ConnmanTestMsg& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
4849
// Disable inactivity checks for this test to avoid interference
49-
static_cast<ConnmanTestMsg*>(connman.get())->SetPeerConnectTimeout(99999s);
50-
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr,
51-
*m_node.chainman, *m_node.mempool, false);
50+
connman.SetPeerConnectTimeout(99999s);
51+
PeerManager& peerman = *m_node.peerman;
5252

5353
// Mock an outbound peer
5454
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@@ -63,10 +63,15 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
6363
/*addrNameIn=*/"",
6464
ConnectionType::OUTBOUND_FULL_RELAY,
6565
/*inbound_onion=*/false};
66-
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
6766

68-
peerLogic->InitializeNode(dummyNode1, dummyNode1.GetLocalServices());
69-
dummyNode1.fSuccessfullyConnected = true;
67+
connman.Handshake(
68+
/*node=*/dummyNode1,
69+
/*successfully_connected=*/true,
70+
/*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
71+
/*permission_flags=*/NetPermissionFlags::None,
72+
/*version=*/PROTOCOL_VERSION,
73+
/*relay_txs=*/true);
74+
TestOnlyResetTimeData();
7075

7176
// This test requires that we have a chain with non-zero work.
7277
{
@@ -78,7 +83,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
7883
// Test starts here
7984
{
8085
LOCK(dummyNode1.cs_sendProcessing);
81-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode1)); // should result in getheaders
86+
BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders
8287
}
8388
{
8489
LOCK(dummyNode1.cs_vSend);
@@ -91,7 +96,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
9196
SetMockTime(nStartTime+21*60);
9297
{
9398
LOCK(dummyNode1.cs_sendProcessing);
94-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode1)); // should result in getheaders
99+
BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders
95100
}
96101
{
97102
LOCK(dummyNode1.cs_vSend);
@@ -101,11 +106,11 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
101106
SetMockTime(nStartTime+24*60);
102107
{
103108
LOCK(dummyNode1.cs_sendProcessing);
104-
BOOST_CHECK(peerLogic->SendMessages(&dummyNode1)); // should result in disconnect
109+
BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in disconnect
105110
}
106111
BOOST_CHECK(dummyNode1.fDisconnect == true);
107112

108-
peerLogic->FinalizeNode(dummyNode1);
113+
peerman.FinalizeNode(dummyNode1);
109114
}
110115

111116
static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType)

0 commit comments

Comments
 (0)