LCOV - code coverage report
Current view: top level - src/test - denialofservice_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.8 % 243 240
Test Date: 2026-04-27 06:58:15 Functions: 100.0 % 12 12
Branches: 51.4 % 1000 514

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : // Unit tests for denial-of-service detection/prevention code
       6                 :             : 
       7                 :             : #include <banman.h>
       8                 :             : #include <chainparams.h>
       9                 :             : #include <common/args.h>
      10                 :             : #include <net.h>
      11                 :             : #include <net_processing.h>
      12                 :             : #include <pubkey.h>
      13                 :             : #include <script/sign.h>
      14                 :             : #include <script/signingprovider.h>
      15                 :             : #include <serialize.h>
      16                 :             : #include <test/util/net.h>
      17                 :             : #include <test/util/random.h>
      18                 :             : #include <test/util/setup_common.h>
      19                 :             : #include <test/util/time.h>
      20                 :             : #include <util/string.h>
      21                 :             : #include <util/time.h>
      22                 :             : #include <validation.h>
      23                 :             : 
      24                 :             : #include <array>
      25                 :             : #include <cstdint>
      26                 :             : 
      27                 :             : #include <boost/test/unit_test.hpp>
      28                 :             : 
      29                 :          17 : static CService ip(uint32_t i)
      30                 :             : {
      31                 :          17 :     struct in_addr s;
      32                 :          17 :     s.s_addr = i;
      33         [ +  - ]:          34 :     return CService(CNetAddr(s), Params().GetDefaultPort());
      34                 :             : }
      35                 :             : 
      36                 :             : BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
      37                 :             : 
      38                 :             : // Test eviction of an outbound peer whose chain never advances
      39                 :             : // Mock a node connection, and use mocktime to simulate a peer
      40                 :             : // which never sends any headers messages.  PeerLogic should
      41                 :             : // decide to evict that outbound peer, after the appropriate timeouts.
      42                 :             : // Note that we protect 4 outbound nodes from being subject to
      43                 :             : // this logic; this test takes advantage of that protection only
      44                 :             : // being applied to nodes which send headers with sufficient
      45                 :             : // work.
      46   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      47                 :             : {
      48                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      49                 :             : 
      50         [ +  - ]:           1 :     ConnmanTestMsg& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
      51                 :             :     // Disable inactivity checks for this test to avoid interference
      52         [ +  - ]:           1 :     connman.SetPeerConnectTimeout(99999s);
      53                 :           1 :     PeerManager& peerman = *m_node.peerman;
      54                 :             : 
      55                 :             :     // Mock an outbound peer
      56         [ +  - ]:           2 :     CAddress addr1(ip(0xa0b0c001), NODE_NONE);
      57                 :           1 :     NodeId id{0};
      58                 :           1 :     CNode dummyNode1{id++,
      59                 :             :                      /*sock=*/nullptr,
      60                 :             :                      addr1,
      61                 :             :                      /*nKeyedNetGroupIn=*/0,
      62                 :             :                      /*nLocalHostNonceIn=*/0,
      63         [ -  + ]:           1 :                      CAddress(),
      64         [ +  - ]:           2 :                      /*addrNameIn=*/"",
      65                 :             :                      ConnectionType::OUTBOUND_FULL_RELAY,
      66                 :             :                      /*inbound_onion=*/false,
      67   [ +  -  +  - ]:           3 :                      /*network_key=*/0};
      68                 :             : 
      69         [ +  - ]:           1 :     connman.Handshake(
      70                 :             :         /*node=*/dummyNode1,
      71                 :             :         /*successfully_connected=*/true,
      72                 :             :         /*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
      73                 :             :         /*local_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
      74                 :             :         /*version=*/PROTOCOL_VERSION,
      75                 :             :         /*relay_txs=*/true);
      76                 :             : 
      77                 :             :     // This test requires that we have a chain with non-zero work.
      78                 :           1 :     {
      79         [ +  - ]:           1 :         LOCK(cs_main);
      80   [ +  -  +  -  :           3 :         BOOST_CHECK(m_node.chainman->ActiveChain().Tip() != nullptr);
          -  +  +  -  +  
                      - ]
      81   [ +  -  +  -  :           3 :         BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->nChainWork > 0);
          -  +  +  -  +  
                -  +  - ]
      82                 :           0 :     }
      83                 :             : 
      84                 :             :     // Test starts here
      85   [ +  -  +  -  :           2 :     BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in getheaders
             +  -  +  - ]
      86                 :             : 
      87                 :           1 :     {
      88         [ +  - ]:           1 :         LOCK(dummyNode1.cs_vSend);
      89         [ +  - ]:           1 :         const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false);
      90   [ +  -  +  -  :           2 :         BOOST_CHECK(!to_send.empty());
                   +  - ]
      91                 :           0 :     }
      92         [ +  - ]:           1 :     connman.FlushSendBuffer(dummyNode1);
      93                 :             : 
      94         [ +  - ]:           1 :     NodeClockContext clock_ctx{};
      95         [ +  - ]:           1 :     clock_ctx += 21min;
      96                 :             : 
      97   [ +  -  +  -  :           2 :     BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in getheaders
             +  -  +  - ]
      98                 :           1 :     {
      99         [ +  - ]:           1 :         LOCK(dummyNode1.cs_vSend);
     100         [ +  - ]:           1 :         const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false);
     101   [ +  -  +  -  :           2 :         BOOST_CHECK(!to_send.empty());
                   +  - ]
     102                 :           0 :     }
     103                 :             : 
     104         [ +  - ]:           1 :     clock_ctx += 3min;
     105   [ +  -  +  -  :           2 :     BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in disconnect
             +  -  +  - ]
     106   [ +  -  +  -  :           2 :     BOOST_CHECK(dummyNode1.fDisconnect == true);
                   +  - ]
     107                 :             : 
     108         [ +  - ]:           1 :     peerman.FinalizeNode(dummyNode1);
     109         [ +  - ]:           2 : }
     110                 :             : 
     111                 :           4 : struct OutboundTest : TestingSetup {
     112                 :          14 : void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType, bool onion_peer = false)
     113                 :             : {
     114                 :          14 :     CAddress addr;
     115                 :             : 
     116         [ +  + ]:          14 :     if (onion_peer) {
     117                 :           2 :         auto tor_addr{m_rng.randbytes(ADDR_TORV3_SIZE)};
     118   [ +  -  -  +  :           6 :         BOOST_REQUIRE(addr.SetSpecial(OnionToString(tor_addr)));
          +  -  +  -  +  
                      - ]
     119                 :           2 :     }
     120                 :             : 
     121   [ +  -  +  + ]:          26 :     while (!addr.IsRoutable()) {
     122         [ +  - ]:          24 :         addr = CAddress(ip(m_rng.randbits(32)), NODE_NONE);
     123                 :             :     }
     124                 :             : 
     125   [ +  -  -  - ]:          14 :     vNodes.emplace_back(new CNode{id++,
     126                 :             :                                   /*sock=*/nullptr,
     127                 :             :                                   addr,
     128                 :             :                                   /*nKeyedNetGroupIn=*/0,
     129                 :             :                                   /*nLocalHostNonceIn=*/0,
     130         [ -  + ]:          14 :                                   CAddress(),
     131         [ +  - ]:          28 :                                   /*addrNameIn=*/"",
     132                 :             :                                   connType,
     133                 :             :                                   /*inbound_onion=*/false,
     134   [ +  -  +  -  :          42 :                                   /*network_key=*/0});
                   +  - ]
     135                 :          14 :     CNode &node = *vNodes.back();
     136                 :          14 :     node.SetCommonVersion(PROTOCOL_VERSION);
     137                 :             : 
     138         [ +  - ]:          14 :     peerLogic.InitializeNode(node, ServiceFlags(NODE_NETWORK | NODE_WITNESS));
     139         [ +  - ]:          14 :     node.fSuccessfullyConnected = true;
     140                 :             : 
     141         [ +  - ]:          14 :     connman.AddTestNode(node);
     142                 :          14 : }
     143                 :             : }; // struct OutboundTest
     144                 :             : 
     145   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(stale_tip_peer_management, OutboundTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     146                 :             : {
     147                 :           1 :     NodeId id{0};
     148                 :           1 :     auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
     149         [ +  - ]:           1 :     auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
     150                 :             : 
     151                 :           1 :     constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
     152                 :           1 :     CConnman::Options options;
     153                 :           1 :     options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
     154                 :             : 
     155                 :           1 :     const auto time_init{Now<NodeSeconds>()};
     156         [ +  - ]:           1 :     NodeClockContext clock_ctx{time_init};
     157         [ +  - ]:           1 :     const auto delta{3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
     158         [ +  - ]:           1 :     connman->Init(options);
     159                 :           1 :     std::vector<CNode *> vNodes;
     160                 :             : 
     161                 :             :     // Mock some outbound peers
     162         [ +  + ]:           9 :     for (int i = 0; i < max_outbound_full_relay; ++i) {
     163         [ +  - ]:           8 :         AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
     164                 :             :     }
     165                 :             : 
     166         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     167                 :             : 
     168                 :             :     // No nodes should be marked for disconnection while we have no extra peers
     169         [ +  + ]:           9 :     for (const CNode *node : vNodes) {
     170   [ +  -  +  - ]:          16 :         BOOST_CHECK(node->fDisconnect == false);
     171                 :             :     }
     172                 :             : 
     173         [ +  - ]:           1 :     clock_ctx += delta;
     174                 :             : 
     175                 :             :     // Now tip should definitely be stale, and we should look for an extra
     176                 :             :     // outbound peer
     177         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     178   [ +  -  +  -  :           2 :     BOOST_CHECK(connman->GetTryNewOutboundPeer());
                   +  - ]
     179                 :             : 
     180                 :             :     // Still no peers should be marked for disconnection
     181         [ +  + ]:           9 :     for (const CNode *node : vNodes) {
     182   [ +  -  +  - ]:          16 :         BOOST_CHECK(node->fDisconnect == false);
     183                 :             :     }
     184                 :             : 
     185                 :             :     // If we add one more peer, something should get marked for eviction
     186                 :             :     // on the next check (since we're mocking the time to be in the future, the
     187                 :             :     // required time connected check should be satisfied).
     188         [ +  - ]:           1 :     clock_ctx.set(time_init);
     189         [ +  - ]:           1 :     AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
     190         [ +  - ]:           1 :     clock_ctx += delta;
     191                 :             : 
     192         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     193         [ +  + ]:           9 :     for (int i = 0; i < max_outbound_full_relay; ++i) {
     194   [ +  -  +  - ]:          16 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     195                 :             :     }
     196                 :             :     // Last added node should get marked for eviction
     197   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == true);
                   +  - ]
     198                 :             : 
     199         [ +  - ]:           1 :     vNodes.back()->fDisconnect = false;
     200                 :             : 
     201                 :             :     // Update the last announced block time for the last
     202                 :             :     // peer, and check that the next newest node gets evicted.
     203   [ +  -  +  - ]:           1 :     peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(), GetTime());
     204                 :             : 
     205         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     206         [ +  + ]:           8 :     for (int i = 0; i < max_outbound_full_relay - 1; ++i) {
     207   [ +  -  +  - ]:          14 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     208                 :             :     }
     209   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect == true);
                   +  - ]
     210   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == false);
                   +  - ]
     211                 :             : 
     212         [ +  - ]:           1 :     vNodes[max_outbound_full_relay - 1]->fDisconnect = false;
     213                 :             : 
     214                 :             :     // Add an onion peer, that will be protected because it is the only one for
     215                 :             :     // its network, so another peer gets disconnected instead.
     216         [ +  - ]:           1 :     clock_ctx.set(time_init);
     217         [ +  - ]:           1 :     AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY, /*onion_peer=*/true);
     218         [ +  - ]:           1 :     clock_ctx += delta;
     219         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     220                 :             : 
     221         [ +  + ]:           7 :     for (int i = 0; i < max_outbound_full_relay - 2; ++i) {
     222   [ +  -  +  - ]:          12 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     223                 :             :     }
     224   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes[max_outbound_full_relay - 2]->fDisconnect == false);
                   +  - ]
     225   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect == true);
                   +  - ]
     226   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes[max_outbound_full_relay]->fDisconnect == false);
                   +  - ]
     227                 :             : 
     228                 :             :     // Add a second onion peer which won't be protected
     229         [ +  - ]:           1 :     clock_ctx.set(time_init);
     230         [ +  - ]:           1 :     AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY, /*onion_peer=*/true);
     231         [ +  - ]:           1 :     clock_ctx += delta;
     232         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     233                 :             : 
     234   [ +  -  +  - ]:           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == true);
     235                 :             : 
     236         [ +  + ]:          12 :     for (const CNode *node : vNodes) {
     237         [ +  - ]:          11 :         peerLogic->FinalizeNode(*node);
     238                 :             :     }
     239                 :             : 
     240         [ +  - ]:           1 :     connman->ClearTestNodes();
     241                 :           1 : }
     242                 :             : 
     243   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(block_relay_only_eviction, OutboundTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     244                 :             : {
     245                 :           1 :     NodeId id{0};
     246                 :           1 :     auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
     247         [ +  - ]:           1 :     auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
     248                 :             : 
     249                 :           1 :     constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
     250                 :           1 :     constexpr auto MINIMUM_CONNECT_TIME{30s};
     251                 :           1 :     CConnman::Options options;
     252                 :           1 :     options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
     253                 :             : 
     254         [ +  - ]:           1 :     connman->Init(options);
     255                 :           1 :     std::vector<CNode*> vNodes;
     256                 :             : 
     257                 :             :     // Add block-relay-only peers up to the limit
     258         [ +  + ]:           3 :     for (int i = 0; i < max_outbound_block_relay; ++i) {
     259         [ +  - ]:           2 :         AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
     260                 :             :     }
     261         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     262                 :             : 
     263         [ +  + ]:           3 :     for (int i = 0; i < max_outbound_block_relay; ++i) {
     264   [ +  -  +  - ]:           4 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     265                 :             :     }
     266                 :             : 
     267                 :             :     // Add an extra block-relay-only peer breaking the limit (mocks logic in ThreadOpenConnections)
     268         [ +  - ]:           1 :     AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
     269         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     270                 :             : 
     271                 :             :     // The extra peer should only get marked for eviction after MINIMUM_CONNECT_TIME
     272         [ +  + ]:           3 :     for (int i = 0; i < max_outbound_block_relay; ++i) {
     273   [ +  -  +  - ]:           4 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     274                 :             :     }
     275   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == false);
                   +  - ]
     276                 :             : 
     277         [ +  - ]:           1 :     NodeClockContext clock_ctx{};
     278         [ +  - ]:           1 :     clock_ctx += MINIMUM_CONNECT_TIME;
     279         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     280         [ +  + ]:           3 :     for (int i = 0; i < max_outbound_block_relay; ++i) {
     281   [ +  -  +  - ]:           4 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     282                 :             :     }
     283   [ +  -  +  - ]:           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == true);
     284                 :             : 
     285                 :             :     // Update the last block time for the extra peer,
     286                 :             :     // and check that the next youngest peer gets evicted.
     287                 :           1 :     vNodes.back()->fDisconnect = false;
     288                 :           1 :     vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>();
     289                 :             : 
     290         [ +  - ]:           1 :     peerLogic->CheckForStaleTipAndEvictPeers();
     291         [ +  + ]:           2 :     for (int i = 0; i < max_outbound_block_relay - 1; ++i) {
     292   [ +  -  +  - ]:           2 :         BOOST_CHECK(vNodes[i]->fDisconnect == false);
     293                 :             :     }
     294   [ +  -  +  -  :           2 :     BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect == true);
                   +  - ]
     295   [ +  -  +  - ]:           2 :     BOOST_CHECK(vNodes.back()->fDisconnect == false);
     296                 :             : 
     297         [ +  + ]:           4 :     for (const CNode* node : vNodes) {
     298         [ +  - ]:           3 :         peerLogic->FinalizeNode(*node);
     299                 :             :     }
     300         [ +  - ]:           1 :     connman->ClearTestNodes();
     301                 :           1 : }
     302                 :             : 
     303   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(peer_discouragement)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     304                 :             : {
     305                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
     306                 :             : 
     307   [ +  -  +  - ]:           3 :     auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
     308   [ +  -  +  - ]:           1 :     auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
     309         [ +  - ]:           1 :     auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
     310                 :             : 
     311         [ +  - ]:           1 :     CNetAddr tor_netaddr;
     312   [ +  -  +  -  :           2 :     BOOST_REQUIRE(
             +  -  +  - ]
     313                 :             :         tor_netaddr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
     314   [ +  -  +  - ]:           1 :     const CService tor_service{tor_netaddr, Params().GetDefaultPort()};
     315                 :             : 
     316         [ +  - ]:           1 :     const std::array<CAddress, 3> addr{CAddress{ip(0xa0b0c001), NODE_NONE},
     317                 :           1 :                                        CAddress{ip(0xa0b0c002), NODE_NONE},
     318   [ +  -  -  - ]:           2 :                                        CAddress{tor_service, NODE_NONE}};
     319                 :             : 
     320         [ +  - ]:           1 :     const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[].
     321                 :             : 
     322                 :           1 :     std::array<CNode*, 3> nodes;
     323                 :             : 
     324         [ +  - ]:           1 :     banman->ClearBanned();
     325                 :           1 :     NodeId id{0};
     326                 :           1 :     nodes[0] = new CNode{id++,
     327                 :             :                          /*sock=*/nullptr,
     328         [ +  - ]:           1 :                          addr[0],
     329                 :             :                          /*nKeyedNetGroupIn=*/0,
     330                 :             :                          /*nLocalHostNonceIn=*/0,
     331         [ -  + ]:           1 :                          CAddress(),
     332         [ +  - ]:           2 :                          /*addrNameIn=*/"",
     333                 :             :                          ConnectionType::INBOUND,
     334                 :             :                          /*inbound_onion=*/false,
     335   [ +  -  +  -  :           3 :                          /*network_key=*/1};
                   +  - ]
     336                 :           1 :     nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
     337         [ +  - ]:           1 :     peerLogic->InitializeNode(*nodes[0], NODE_NETWORK);
     338         [ +  - ]:           1 :     nodes[0]->fSuccessfullyConnected = true;
     339         [ +  - ]:           1 :     connman->AddTestNode(*nodes[0]);
     340         [ +  - ]:           1 :     peerLogic->UnitTestMisbehaving(nodes[0]->GetId()); // Should be discouraged
     341   [ +  -  +  -  :           2 :     BOOST_CHECK(peerLogic->SendMessages(*nodes[0]));
             +  -  +  - ]
     342                 :             : 
     343   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[0]));
             +  -  +  - ]
     344   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[0]->fDisconnect);
                   +  - ]
     345   [ +  -  +  -  :           2 :     BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
             +  -  +  - ]
     346                 :             : 
     347                 :           1 :     nodes[1] = new CNode{id++,
     348                 :             :                          /*sock=*/nullptr,
     349         [ +  - ]:           1 :                          addr[1],
     350                 :             :                          /*nKeyedNetGroupIn=*/1,
     351                 :             :                          /*nLocalHostNonceIn=*/1,
     352         [ -  + ]:           1 :                          CAddress(),
     353         [ +  - ]:           2 :                          /*addrNameIn=*/"",
     354                 :             :                          ConnectionType::INBOUND,
     355                 :             :                          /*inbound_onion=*/false,
     356   [ +  -  +  -  :           3 :                          /*network_key=*/1};
                   +  - ]
     357                 :           1 :     nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
     358         [ +  - ]:           1 :     peerLogic->InitializeNode(*nodes[1], NODE_NETWORK);
     359         [ +  - ]:           1 :     nodes[1]->fSuccessfullyConnected = true;
     360         [ +  - ]:           1 :     connman->AddTestNode(*nodes[1]);
     361   [ +  -  +  -  :           2 :     BOOST_CHECK(peerLogic->SendMessages(*nodes[1]));
             +  -  +  - ]
     362                 :             :     // [0] is still discouraged/disconnected.
     363   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[0]));
             +  -  +  - ]
     364   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[0]->fDisconnect);
                   +  - ]
     365                 :             :     // [1] is not discouraged/disconnected yet.
     366   [ +  -  +  -  :           2 :     BOOST_CHECK(!banman->IsDiscouraged(addr[1]));
             +  -  +  - ]
     367   [ +  -  +  -  :           2 :     BOOST_CHECK(!nodes[1]->fDisconnect);
                   +  - ]
     368         [ +  - ]:           1 :     peerLogic->UnitTestMisbehaving(nodes[1]->GetId());
     369   [ +  -  +  -  :           2 :     BOOST_CHECK(peerLogic->SendMessages(*nodes[1]));
             +  -  +  - ]
     370                 :             :     // Expect both [0] and [1] to be discouraged/disconnected now.
     371   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[0]));
             +  -  +  - ]
     372   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[0]->fDisconnect);
                   +  - ]
     373   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[1]));
             +  -  +  - ]
     374   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[1]->fDisconnect);
                   +  - ]
     375                 :             : 
     376                 :             :     // Make sure non-IP peers are discouraged and disconnected properly.
     377                 :             : 
     378                 :           1 :     nodes[2] = new CNode{id++,
     379                 :             :                          /*sock=*/nullptr,
     380         [ +  - ]:           1 :                          addr[2],
     381                 :             :                          /*nKeyedNetGroupIn=*/1,
     382                 :             :                          /*nLocalHostNonceIn=*/1,
     383         [ -  + ]:           1 :                          CAddress(),
     384         [ +  - ]:           2 :                          /*addrNameIn=*/"",
     385                 :             :                          ConnectionType::OUTBOUND_FULL_RELAY,
     386                 :             :                          /*inbound_onion=*/false,
     387   [ +  -  +  -  :           3 :                          /*network_key=*/2};
                   +  - ]
     388                 :           1 :     nodes[2]->SetCommonVersion(PROTOCOL_VERSION);
     389         [ +  - ]:           1 :     peerLogic->InitializeNode(*nodes[2], NODE_NETWORK);
     390         [ +  - ]:           1 :     nodes[2]->fSuccessfullyConnected = true;
     391         [ +  - ]:           1 :     connman->AddTestNode(*nodes[2]);
     392         [ +  - ]:           1 :     peerLogic->UnitTestMisbehaving(nodes[2]->GetId());
     393   [ +  -  +  -  :           2 :     BOOST_CHECK(peerLogic->SendMessages(*nodes[2]));
             +  -  +  - ]
     394   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[0]));
             +  -  +  - ]
     395   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[1]));
             +  -  +  - ]
     396   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr[2]));
             +  -  +  - ]
     397   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[0]->fDisconnect);
                   +  - ]
     398   [ +  -  +  -  :           2 :     BOOST_CHECK(nodes[1]->fDisconnect);
                   +  - ]
     399   [ +  -  +  - ]:           2 :     BOOST_CHECK(nodes[2]->fDisconnect);
     400                 :             : 
     401         [ +  + ]:           4 :     for (CNode* node : nodes) {
     402         [ +  - ]:           3 :         peerLogic->FinalizeNode(*node);
     403                 :             :     }
     404         [ +  - ]:           1 :     connman->ClearTestNodes();
     405         [ +  - ]:           3 : }
     406                 :             : 
     407   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(DoS_bantime)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     408                 :             : {
     409                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
     410                 :             : 
     411   [ +  -  +  - ]:           3 :     auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
     412   [ +  -  +  - ]:           1 :     auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
     413         [ +  - ]:           1 :     auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
     414                 :             : 
     415         [ +  - ]:           1 :     banman->ClearBanned();
     416         [ +  - ]:           1 :     const NodeClockContext clock_ctx{}; // keep mocktime constant
     417                 :             : 
     418         [ +  - ]:           2 :     CAddress addr(ip(0xa0b0c001), NODE_NONE);
     419                 :           1 :     NodeId id{0};
     420                 :           1 :     CNode dummyNode{id++,
     421                 :             :                     /*sock=*/nullptr,
     422                 :             :                     addr,
     423                 :             :                     /*nKeyedNetGroupIn=*/4,
     424                 :             :                     /*nLocalHostNonceIn=*/4,
     425         [ -  + ]:           1 :                     CAddress(),
     426         [ +  - ]:           2 :                     /*addrNameIn=*/"",
     427                 :             :                     ConnectionType::INBOUND,
     428                 :             :                     /*inbound_onion=*/false,
     429   [ +  -  +  - ]:           3 :                     /*network_key=*/1};
     430                 :           1 :     dummyNode.SetCommonVersion(PROTOCOL_VERSION);
     431         [ +  - ]:           1 :     peerLogic->InitializeNode(dummyNode, NODE_NETWORK);
     432         [ +  - ]:           1 :     dummyNode.fSuccessfullyConnected = true;
     433                 :             : 
     434         [ +  - ]:           1 :     peerLogic->UnitTestMisbehaving(dummyNode.GetId());
     435   [ +  -  +  -  :           2 :     BOOST_CHECK(peerLogic->SendMessages(dummyNode));
             +  -  +  - ]
     436   [ +  -  +  -  :           2 :     BOOST_CHECK(banman->IsDiscouraged(addr));
             +  -  +  - ]
     437                 :             : 
     438         [ +  - ]:           1 :     peerLogic->FinalizeNode(dummyNode);
     439         [ +  - ]:           2 : }
     440                 :             : 
     441                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1