LCOV - code coverage report
Current view: top level - src/test/fuzz - p2p_handshake.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 87.0 % 46 40
Test Date: 2024-09-01 05:20:30 Functions: 66.7 % 3 2
Branches: 51.9 % 77 40

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

Generated by: LCOV version 2.0-1