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.5 % 57 55
Test Date: 2026-08-29 05:53:03 Functions: 100.0 % 3 3
Branches: 61.8 % 76 47

             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         [ +  - ]:        2899 : FUZZ_TARGET(p2p_handshake, .init = ::initialize)
      37                 :             : {
      38                 :        2423 :     SeedRandomStateForTest(SeedRand::ZEROS);
      39                 :        2423 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      40                 :             : 
      41                 :        2423 :     auto& node{g_setup->m_node};
      42                 :        2423 :     auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
      43                 :        2423 :     auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
      44                 :        2423 :     FakeNodeClock clock{1610000000s}; // 2021-01-07, arbitrary
      45         [ +  - ]:        2423 :     FakeSteadyClock steady_clock;
      46         [ +  - ]:        2423 :     chainman.ResetIbd();
      47                 :             : 
      48         [ +  + ]:        2423 :     node.banman.reset();
      49         [ +  - ]:        2423 :     node.addrman.reset();
      50         [ +  - ]:        2423 :     node.peerman.reset();
      51                 :        4846 :     node.addrman = std::make_unique<AddrMan>(
      52         [ +  - ]:        2423 :         *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
      53                 :        4846 :     node.peerman = PeerManager::make(connman, *node.addrman,
      54                 :             :                                      /*banman=*/nullptr, chainman,
      55         [ +  - ]:        2423 :                                      *node.mempool, *node.warnings,
      56                 :             :                                      PeerManager::Options{
      57                 :             :                                          .reconcile_txs = true,
      58                 :             :                                          .deterministic_rng = true,
      59                 :        2423 :                                      });
      60         [ +  - ]:        2423 :     connman.SetMsgProc(node.peerman.get());
      61         [ +  - ]:        2423 :     connman.SetAddrman(*node.addrman);
      62                 :             : 
      63         [ +  - ]:        2423 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      64                 :             : 
      65                 :        2423 :     std::vector<CNode*> peers;
      66                 :        2423 :     const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
      67         [ +  + ]:        7319 :     for (int i = 0; i < num_peers_to_add; ++i) {
      68         [ +  - ]:        4896 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
      69         [ +  - ]:        4896 :         connman.AddTestNode(*peers.back());
      70                 :        4896 :         node.peerman->InitializeNode(
      71         [ +  - ]:        4896 :             *peers.back(),
      72                 :        4896 :             static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
      73                 :             :     }
      74                 :             : 
      75                 :             :     // Toggle IBD from within the loop, so that some messages may be processed
      76                 :             :     // under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard
      77                 :             :     // it to call at most once.
      78                 :             :     bool jump_out_of_ibd{false};
      79   [ +  +  +  + ]:       30608 :     LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {
      80                 :       28185 :         CNode& connection = *PickValue(fuzzed_data_provider, peers);
      81   [ +  +  +  + ]:       28185 :         if (connection.fDisconnect || connection.fSuccessfullyConnected) {
      82                 :             :             // Skip if the connection was disconnected or if the version
      83                 :             :             // handshake was already completed.
      84                 :        1136 :             continue;
      85                 :             :         }
      86                 :             : 
      87         [ +  + ]:       27049 :         if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
      88   [ +  +  +  +  :       27049 :         if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();
                   +  - ]
      89                 :             : 
      90                 :       54098 :         clock += std::chrono::seconds{
      91         [ +  - ]:       27049 :                     fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(
      92                 :             :                         -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
      93                 :             :                         std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
      94         [ +  - ]:       27049 :         };
      95                 :             : 
      96                 :       27049 :         CSerializedNetMsg net_msg;
      97         [ +  - ]:       27049 :         net_msg.m_type = PickValue(fuzzed_data_provider, ALL_NET_MESSAGE_TYPES);
      98                 :       27049 :         net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
      99                 :             : 
     100         [ +  - ]:       27049 :         connman.FlushSendBuffer(connection);
     101         [ +  - ]:       27049 :         (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
     102                 :             : 
     103                 :             :         bool more_work{true};
     104         [ +  + ]:       55771 :         while (more_work) {
     105         [ +  - ]:       28722 :             connection.fPauseSend = false;
     106                 :             : 
     107                 :       28722 :             try {
     108         [ +  - ]:       28722 :                 more_work = connman.ProcessMessagesOnce(connection);
     109         [ -  - ]:           0 :             } catch (const std::ios_base::failure&) {
     110                 :           0 :             }
     111         [ +  - ]:       28722 :             node.peerman->SendMessages(connection);
     112                 :             :         }
     113                 :       27049 :     }
     114                 :             : 
     115         [ +  - ]:        2423 :     node.connman->StopNodes();
     116         [ +  - ]:        4846 : }
        

Generated by: LCOV version 2.5.0-full