LCOV - code coverage report
Current view: top level - src/test - denialofservice_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.8 % 243 240
Test Date: 2025-10-04 04:34:28 Functions: 100.0 % 12 12
Branches: 51.4 % 1008 518

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

Generated by: LCOV version 2.0-1