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.0 % 50 48
Test Date: 2026-03-31 04:13:53 Functions: 100.0 % 3 3
Branches: 58.3 % 60 35

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

Generated by: LCOV version 2.0-1