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.7 % 236 233
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 12 12
Branches: 51.4 % 970 499

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

Generated by: LCOV version 2.0-1