LCOV - code coverage report
Current view: top level - src/test/fuzz - p2p_handshake.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.3 % 54 52
Test Date: 2026-06-01 07:10:42 Functions: 100.0 % 3 3
Branches: 59.1 % 66 39

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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                 :             : #include <banman.h>
       6                 :             : #include <net.h>
       7                 :             : #include <net_processing.h>
       8                 :             : #include <protocol.h>
       9                 :             : #include <sync.h>
      10                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      11                 :             : #include <test/fuzz/fuzz.h>
      12                 :             : #include <test/fuzz/util.h>
      13                 :             : #include <test/fuzz/util/net.h>
      14                 :             : #include <test/util/net.h>
      15                 :             : #include <test/util/setup_common.h>
      16                 :             : #include <test/util/time.h>
      17                 :             : #include <test/util/validation.h>
      18                 :             : #include <util/time.h>
      19                 :             : #include <validationinterface.h>
      20                 :             : 
      21                 :             : #include <ios>
      22                 :             : #include <utility>
      23                 :             : #include <vector>
      24                 :             : 
      25                 :             : namespace {
      26                 :             : TestingSetup* g_setup;
      27                 :             : 
      28                 :           1 : void initialize()
      29                 :             : {
      30                 :           1 :     static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
      31   [ +  -  +  -  :           1 :         /*chain_type=*/ChainType::REGTEST);
                   +  - ]
      32                 :           1 :     g_setup = testing_setup.get();
      33                 :           1 : }
      34                 :             : } // namespace
      35                 :             : 
      36         [ +  - ]:        1526 : FUZZ_TARGET(p2p_handshake, .init = ::initialize)
      37                 :             : {
      38                 :        1062 :     SeedRandomStateForTest(SeedRand::ZEROS);
      39                 :        1062 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      40                 :             : 
      41                 :        1062 :     auto& node{g_setup->m_node};
      42                 :        1062 :     auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
      43                 :        1062 :     auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
      44                 :        1062 :     NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
      45         [ +  - ]:        1062 :     chainman.ResetIbd();
      46                 :             : 
      47         [ +  + ]:        1062 :     node.banman.reset();
      48         [ +  - ]:        1062 :     node.addrman.reset();
      49         [ +  - ]:        1062 :     node.peerman.reset();
      50                 :        2124 :     node.addrman = std::make_unique<AddrMan>(
      51         [ +  - ]:        1062 :         *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
      52                 :        2124 :     node.peerman = PeerManager::make(connman, *node.addrman,
      53                 :             :                                      /*banman=*/nullptr, chainman,
      54         [ +  - ]:        1062 :                                      *node.mempool, *node.warnings,
      55                 :             :                                      PeerManager::Options{
      56                 :             :                                          .reconcile_txs = true,
      57                 :             :                                          .deterministic_rng = true,
      58                 :        1062 :                                      });
      59         [ +  - ]:        1062 :     connman.SetMsgProc(node.peerman.get());
      60         [ +  - ]:        1062 :     connman.SetAddrman(*node.addrman);
      61                 :             : 
      62         [ +  - ]:        1062 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      63                 :             : 
      64                 :        1062 :     std::vector<CNode*> peers;
      65                 :        1062 :     const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
      66         [ +  + ]:        3209 :     for (int i = 0; i < num_peers_to_add; ++i) {
      67         [ +  - ]:        2147 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
      68         [ +  - ]:        2147 :         connman.AddTestNode(*peers.back());
      69                 :        2147 :         node.peerman->InitializeNode(
      70         [ +  - ]:        2147 :             *peers.back(),
      71                 :        2147 :             static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
      72                 :             :     }
      73                 :             : 
      74   [ +  +  +  + ]:       16950 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
      75                 :             :     {
      76                 :       15888 :         CNode& connection = *PickValue(fuzzed_data_provider, peers);
      77   [ +  +  +  + ]:       15888 :         if (connection.fDisconnect || connection.fSuccessfullyConnected) {
      78                 :             :             // Skip if the connection was disconnected or if the version
      79                 :             :             // handshake was already completed.
      80                 :        2132 :             continue;
      81                 :             :         }
      82                 :             : 
      83                 :       27512 :         clock_ctx += std::chrono::seconds{
      84         [ +  - ]:       13756 :                     fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(
      85                 :             :                         -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
      86                 :             :                         std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
      87         [ +  - ]:       13756 :         };
      88                 :             : 
      89                 :       13756 :         CSerializedNetMsg net_msg;
      90         [ +  - ]:       13756 :         net_msg.m_type = PickValue(fuzzed_data_provider, ALL_NET_MESSAGE_TYPES);
      91                 :       13756 :         net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
      92                 :             : 
      93         [ +  - ]:       13756 :         connman.FlushSendBuffer(connection);
      94         [ +  - ]:       13756 :         (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
      95                 :             : 
      96                 :             :         bool more_work{true};
      97         [ +  + ]:       28349 :         while (more_work) {
      98         [ +  - ]:       14593 :             connection.fPauseSend = false;
      99                 :             : 
     100                 :       14593 :             try {
     101         [ +  - ]:       14593 :                 more_work = connman.ProcessMessagesOnce(connection);
     102         [ -  - ]:           0 :             } catch (const std::ios_base::failure&) {
     103                 :           0 :             }
     104         [ +  - ]:       14593 :             node.peerman->SendMessages(connection);
     105                 :             :         }
     106                 :       13756 :     }
     107                 :             : 
     108         [ +  - ]:        1062 :     node.connman->StopNodes();
     109         [ +  - ]:        2124 : }
        

Generated by: LCOV version 2.0-1