Changeset View
Changeset View
Standalone View
Standalone View
src/test/denialofservice_tests.cpp
| Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) { | ||||
| ConnmanTestMsg &connman = static_cast<ConnmanTestMsg &>(*m_node.connman); | ConnmanTestMsg &connman = static_cast<ConnmanTestMsg &>(*m_node.connman); | ||||
| // Disable inactivity checks for this test to avoid interference | // Disable inactivity checks for this test to avoid interference | ||||
| connman.SetPeerConnectTimeout(99999s); | connman.SetPeerConnectTimeout(99999s); | ||||
| PeerManager &peerman = *m_node.peerman; | PeerManager &peerman = *m_node.peerman; | ||||
| // Mock an outbound peer | // Mock an outbound peer | ||||
| CAddress addr1(ip(0xa0b0c001), NODE_NONE); | CAddress addr1(ip(0xa0b0c001), NODE_NONE); | ||||
| CNode dummyNode1(id++, ServiceFlags(NODE_NETWORK), INVALID_SOCKET, addr1, | CNode dummyNode1(id++, INVALID_SOCKET, addr1, | ||||
| /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, | /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, | ||||
| /* nLocalExtraEntropyIn */ 0, CAddress(), /* pszDest */ "", | /* nLocalExtraEntropyIn */ 0, CAddress(), /* pszDest */ "", | ||||
| ConnectionType::OUTBOUND_FULL_RELAY, | ConnectionType::OUTBOUND_FULL_RELAY, | ||||
| /* inbound_onion */ false); | /* inbound_onion */ false); | ||||
| connman.Handshake( | connman.Handshake( | ||||
| /*node=*/dummyNode1, | /*node=*/dummyNode1, | ||||
| /*successfully_connected=*/true, | /*successfully_connected=*/true, | ||||
| /*remote_services=*/ServiceFlags(NODE_NETWORK), | /*remote_services=*/ServiceFlags(NODE_NETWORK), | ||||
| /*local_services=*/ServiceFlags(NODE_NETWORK), | |||||
| /*permission_flags=*/NetPermissionFlags::None, | /*permission_flags=*/NetPermissionFlags::None, | ||||
| /*version=*/PROTOCOL_VERSION, | /*version=*/PROTOCOL_VERSION, | ||||
| /*relay_txs=*/true); | /*relay_txs=*/true); | ||||
| TestOnlyResetTimeData(); | TestOnlyResetTimeData(); | ||||
| // This test requires that we have a chain with non-zero work. | // This test requires that we have a chain with non-zero work. | ||||
| { | { | ||||
| LOCK(cs_main); | LOCK(cs_main); | ||||
| Show All 38 Lines | BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) { | ||||
| peerman.FinalizeNode(config, dummyNode1); | peerman.FinalizeNode(config, dummyNode1); | ||||
| } | } | ||||
| static void AddRandomOutboundPeer(const Config &config, | static void AddRandomOutboundPeer(const Config &config, | ||||
| std::vector<CNode *> &vNodes, | std::vector<CNode *> &vNodes, | ||||
| PeerManager &peerLogic, | PeerManager &peerLogic, | ||||
| CConnmanTest *connman) { | CConnmanTest *connman) { | ||||
| CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE); | CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE); | ||||
| vNodes.emplace_back( | vNodes.emplace_back(new CNode(id++, INVALID_SOCKET, addr, | ||||
| new CNode(id++, ServiceFlags(NODE_NETWORK), INVALID_SOCKET, addr, | |||||
| /* nKeyedNetGroupIn */ 0, | /* nKeyedNetGroupIn */ 0, | ||||
| /* nLocalHostNonceIn */ 0, | /* nLocalHostNonceIn */ 0, | ||||
| /* nLocalExtraEntropyIn */ 0, CAddress(), | /* nLocalExtraEntropyIn */ 0, CAddress(), | ||||
| /* pszDest */ "", ConnectionType::OUTBOUND_FULL_RELAY, | /* pszDest */ "", | ||||
| ConnectionType::OUTBOUND_FULL_RELAY, | |||||
| /* inbound_onion */ false)); | /* inbound_onion */ false)); | ||||
| CNode &node = *vNodes.back(); | CNode &node = *vNodes.back(); | ||||
| node.SetCommonVersion(PROTOCOL_VERSION); | node.SetCommonVersion(PROTOCOL_VERSION); | ||||
| peerLogic.InitializeNode(config, node, node.GetLocalServices()); | peerLogic.InitializeNode(config, node, ServiceFlags(NODE_NETWORK)); | ||||
| node.fSuccessfullyConnected = true; | node.fSuccessfullyConnected = true; | ||||
| connman->AddNode(node); | connman->AddNode(node); | ||||
| } | } | ||||
| BOOST_AUTO_TEST_CASE(stale_tip_peer_management) { | BOOST_AUTO_TEST_CASE(stale_tip_peer_management) { | ||||
| const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
| ▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(peer_discouragement) { | ||||
| auto connman = | auto connman = | ||||
| std::make_unique<CConnman>(config, 0x1337, 0x1337, *m_node.addrman); | std::make_unique<CConnman>(config, 0x1337, 0x1337, *m_node.addrman); | ||||
| auto peerLogic = | auto peerLogic = | ||||
| PeerManager::make(*connman, *m_node.addrman, banman.get(), | PeerManager::make(*connman, *m_node.addrman, banman.get(), | ||||
| *m_node.chainman, *m_node.mempool, false); | *m_node.chainman, *m_node.mempool, false); | ||||
| banman->ClearBanned(); | banman->ClearBanned(); | ||||
| CAddress addr1(ip(0xa0b0c001), NODE_NONE); | CAddress addr1(ip(0xa0b0c001), NODE_NONE); | ||||
| CNode dummyNode1(id++, NODE_NETWORK, INVALID_SOCKET, addr1, | CNode dummyNode1(id++, INVALID_SOCKET, addr1, | ||||
| /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, | /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, | ||||
| /* nLocalExtraEntropyIn */ 0, CAddress(), /* pszDest */ "", | /* nLocalExtraEntropyIn */ 0, CAddress(), /* pszDest */ "", | ||||
| ConnectionType::INBOUND, /* inbound_onion */ false); | ConnectionType::INBOUND, /* inbound_onion */ false); | ||||
| dummyNode1.SetCommonVersion(PROTOCOL_VERSION); | dummyNode1.SetCommonVersion(PROTOCOL_VERSION); | ||||
| peerLogic->InitializeNode(config, dummyNode1, | peerLogic->InitializeNode(config, dummyNode1, NODE_NETWORK); | ||||
| dummyNode1.GetLocalServices()); | |||||
| dummyNode1.fSuccessfullyConnected = true; | dummyNode1.fSuccessfullyConnected = true; | ||||
| // Should be discouraged | // Should be discouraged | ||||
| peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, | peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, | ||||
| /* message */ ""); | /* message */ ""); | ||||
| { | { | ||||
| LOCK(dummyNode1.cs_sendProcessing); | LOCK(dummyNode1.cs_sendProcessing); | ||||
| BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode1)); | BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode1)); | ||||
| } | } | ||||
| BOOST_CHECK(banman->IsDiscouraged(addr1)); | BOOST_CHECK(banman->IsDiscouraged(addr1)); | ||||
| // Different IP, not discouraged | // Different IP, not discouraged | ||||
| BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001 | 0x0000ff00))); | BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001 | 0x0000ff00))); | ||||
| CAddress addr2(ip(0xa0b0c002), NODE_NONE); | CAddress addr2(ip(0xa0b0c002), NODE_NONE); | ||||
| CNode dummyNode2(id++, NODE_NETWORK, INVALID_SOCKET, addr2, | CNode dummyNode2(id++, INVALID_SOCKET, addr2, | ||||
| /* nKeyedNetGroupIn */ 1, /* nLocalHostNonceIn */ 1, | /* nKeyedNetGroupIn */ 1, /* nLocalHostNonceIn */ 1, | ||||
| /* nLocalExtraEntropyIn */ 1, CAddress(), | /* nLocalExtraEntropyIn */ 1, CAddress(), | ||||
| /* pszDest */ "", ConnectionType::INBOUND, | /* pszDest */ "", ConnectionType::INBOUND, | ||||
| /* inbound_onion */ false); | /* inbound_onion */ false); | ||||
| dummyNode2.SetCommonVersion(PROTOCOL_VERSION); | dummyNode2.SetCommonVersion(PROTOCOL_VERSION); | ||||
| peerLogic->InitializeNode(config, dummyNode2, | peerLogic->InitializeNode(config, dummyNode2, NODE_NETWORK); | ||||
| dummyNode2.GetLocalServices()); | |||||
| dummyNode2.fSuccessfullyConnected = true; | dummyNode2.fSuccessfullyConnected = true; | ||||
| peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, | peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, | ||||
| /* message */ ""); | /* message */ ""); | ||||
| { | { | ||||
| LOCK(dummyNode2.cs_sendProcessing); | LOCK(dummyNode2.cs_sendProcessing); | ||||
| BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode2)); | BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode2)); | ||||
| } | } | ||||
| // 2 not discouraged yet... | // 2 not discouraged yet... | ||||
| Show All 26 Lines | auto peerLogic = | ||||
| *m_node.chainman, *m_node.mempool, false); | *m_node.chainman, *m_node.mempool, false); | ||||
| banman->ClearBanned(); | banman->ClearBanned(); | ||||
| int64_t nStartTime = GetTime(); | int64_t nStartTime = GetTime(); | ||||
| // Overrides future calls to GetTime() | // Overrides future calls to GetTime() | ||||
| SetMockTime(nStartTime); | SetMockTime(nStartTime); | ||||
| CAddress addr(ip(0xa0b0c001), NODE_NONE); | CAddress addr(ip(0xa0b0c001), NODE_NONE); | ||||
| CNode dummyNode(id++, NODE_NETWORK, INVALID_SOCKET, addr, | CNode dummyNode(id++, INVALID_SOCKET, addr, | ||||
| /* nKeyedNetGroupIn */ 4, /* nLocalHostNonceIn */ 4, | /* nKeyedNetGroupIn */ 4, /* nLocalHostNonceIn */ 4, | ||||
| /* nLocalExtraEntropyIn */ 4, CAddress(), /* pszDest */ "", | /* nLocalExtraEntropyIn */ 4, CAddress(), /* pszDest */ "", | ||||
| ConnectionType::INBOUND, /* inbound_onion */ false); | ConnectionType::INBOUND, /* inbound_onion */ false); | ||||
| dummyNode.SetCommonVersion(PROTOCOL_VERSION); | dummyNode.SetCommonVersion(PROTOCOL_VERSION); | ||||
| peerLogic->InitializeNode(config, dummyNode, dummyNode.GetLocalServices()); | peerLogic->InitializeNode(config, dummyNode, NODE_NETWORK); | ||||
| dummyNode.fSuccessfullyConnected = true; | dummyNode.fSuccessfullyConnected = true; | ||||
| peerLogic->Misbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD, | peerLogic->Misbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD, | ||||
| /* message */ ""); | /* message */ ""); | ||||
| { | { | ||||
| LOCK(dummyNode.cs_sendProcessing); | LOCK(dummyNode.cs_sendProcessing); | ||||
| BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode)); | BOOST_CHECK(peerLogic->SendMessages(config, &dummyNode)); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 102 Lines • Show Last 20 Lines | |||||