LCOV - code coverage report
Current view: top level - src/test/fuzz - connman.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 172 172
Test Date: 2025-12-19 04:17:13 Functions: 100.0 % 27 27
Branches: 58.3 % 144 84

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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                 :             : #include <addrman.h>
       6                 :             : #include <chainparams.h>
       7                 :             : #include <common/args.h>
       8                 :             : #include <net.h>
       9                 :             : #include <net_processing.h>
      10                 :             : #include <netaddress.h>
      11                 :             : #include <protocol.h>
      12                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      13                 :             : #include <test/fuzz/fuzz.h>
      14                 :             : #include <test/fuzz/util.h>
      15                 :             : #include <test/fuzz/util/net.h>
      16                 :             : #include <test/fuzz/util/threadinterrupt.h>
      17                 :             : #include <test/util/setup_common.h>
      18                 :             : #include <util/translation.h>
      19                 :             : 
      20                 :             : #include <cstdint>
      21                 :             : #include <vector>
      22                 :             : 
      23                 :             : namespace {
      24                 :             : const TestingSetup* g_setup;
      25                 :             : 
      26                 :        5609 : int32_t GetCheckRatio()
      27                 :             : {
      28   [ +  -  +  - ]:       11218 :     return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
      29                 :             : }
      30                 :             : 
      31                 :             : } // namespace
      32                 :             : 
      33                 :           1 : void initialize_connman()
      34                 :             : {
      35   [ +  -  +  -  :           1 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   +  - ]
      36                 :           1 :     g_setup = testing_setup.get();
      37                 :           1 : }
      38                 :             : 
      39         [ +  - ]:        4380 : FUZZ_TARGET(connman, .init = initialize_connman)
      40                 :             : {
      41                 :        3922 :     SeedRandomStateForTest(SeedRand::ZEROS);
      42                 :        3922 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      43                 :        3922 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      44                 :        3922 :     auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
      45   [ +  -  +  - ]:        3922 :     auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
      46         [ +  + ]:        3922 :     if (fuzzed_data_provider.ConsumeBool()) {
      47                 :        2581 :         const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
      48   [ -  +  +  - ]:        2581 :         DataStream ds{serialized_data};
      49                 :        2581 :         try {
      50         [ +  + ]:        5162 :             ds >> *addr_man_ptr;
      51         [ -  + ]:        1687 :         } catch (const std::ios_base::failure&) {
      52   [ +  -  +  - ]:        3374 :             addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
      53                 :        1687 :         }
      54                 :        2581 :     }
      55         [ +  - ]:        3922 :     AddrManDeterministic& addr_man{*addr_man_ptr};
      56         [ +  - ]:        3922 :     auto net_events{ConsumeNetEvents(fuzzed_data_provider)};
      57                 :             : 
      58                 :             :     // Mock CreateSock() to create FuzzedSock.
      59         [ +  - ]:        3922 :     auto CreateSockOrig = CreateSock;
      60                 :       51131 :     CreateSock = [&fuzzed_data_provider](int, int, int) {
      61                 :       47209 :         return std::make_unique<FuzzedSock>(fuzzed_data_provider);
      62                 :        3922 :     };
      63                 :             : 
      64                 :             :     // Mock g_dns_lookup() to return a fuzzed address.
      65         [ +  - ]:        3922 :     auto g_dns_lookup_orig = g_dns_lookup;
      66                 :       11148 :     g_dns_lookup = [&fuzzed_data_provider](const std::string&, bool) {
      67   [ +  -  +  +  :       21678 :         return std::vector<CNetAddr>{ConsumeNetAddr(fuzzed_data_provider)};
                   -  - ]
      68                 :       11148 :     };
      69                 :             : 
      70                 :        3922 :     ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
      71                 :             :                      fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
      72                 :             :                      addr_man,
      73                 :             :                      netgroupman,
      74                 :             :                      Params(),
      75                 :        3922 :                      fuzzed_data_provider.ConsumeBool(),
      76   [ +  -  +  -  :        7844 :                      ConsumeThreadInterrupt(fuzzed_data_provider)};
                   +  - ]
      77                 :             : 
      78                 :        3922 :     const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
      79                 :        3922 :     CConnman::Options options;
      80                 :        3922 :     options.m_msgproc = &net_events;
      81                 :        3922 :     options.nMaxOutboundLimit = max_outbound_limit;
      82         [ +  - ]:        3922 :     connman.Init(options);
      83                 :             : 
      84         [ +  - ]:        3922 :     CNetAddr random_netaddr;
      85         [ +  - ]:        3922 :     CAddress random_address;
      86                 :        3922 :     CNode random_node = ConsumeNode(fuzzed_data_provider);
      87         [ +  - ]:        3922 :     CSubNet random_subnet;
      88                 :        3922 :     std::string random_string;
      89                 :             : 
      90   [ +  +  +  + ]:       51380 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
      91                 :       47458 :         CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
      92         [ +  - ]:       47458 :         connman.AddTestNode(p2p_node);
      93                 :             :     }
      94                 :             : 
      95   [ +  +  +  + ]:      399182 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      96         [ +  - ]:      395260 :         CallOneOf(
      97                 :             :             fuzzed_data_provider,
      98                 :        7862 :             [&] {
      99                 :        7862 :                 random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
     100                 :        7862 :             },
     101                 :       48995 :             [&] {
     102                 :       48995 :                 random_address = ConsumeAddress(fuzzed_data_provider);
     103                 :       48995 :             },
     104                 :       12026 :             [&] {
     105                 :       12026 :                 random_subnet = ConsumeSubNet(fuzzed_data_provider);
     106                 :       12026 :             },
     107                 :        6023 :             [&] {
     108                 :        6023 :                 random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
     109                 :        6023 :             },
     110                 :       14875 :             [&] {
     111                 :       59500 :                 connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
     112   [ -  +  +  - ]:       44625 :             },
     113                 :       17758 :             [&] {
     114                 :       17758 :                 connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
     115                 :       17758 :             },
     116                 :        4855 :             [&] {
     117                 :        4855 :                 connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
     118                 :        4855 :             },
     119                 :        4143 :             [&] {
     120                 :        4143 :                 connman.DisconnectNode(random_netaddr);
     121                 :        4143 :             },
     122                 :        1006 :             [&] {
     123         [ -  + ]:        1006 :                 connman.DisconnectNode(random_string);
     124                 :        1006 :             },
     125                 :         647 :             [&] {
     126                 :         647 :                 connman.DisconnectNode(random_subnet);
     127                 :         647 :             },
     128                 :       29970 :             [&] {
     129         [ +  - ]:       29970 :                 connman.ForEachNode([](auto) {});
     130                 :       29970 :             },
     131                 :         980 :             [&] {
     132         [ +  - ]:         980 :                 (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
     133                 :         980 :             },
     134                 :       15637 :             [&] {
     135                 :       15637 :                 auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
     136                 :       15637 :                 auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
     137                 :       15637 :                 auto filtered = fuzzed_data_provider.ConsumeBool();
     138                 :       15637 :                 (void)connman.GetAddressesUnsafe(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
     139                 :       15637 :             },
     140                 :        2273 :             [&] {
     141                 :        2273 :                 auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
     142                 :        2273 :                 auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
     143                 :        2273 :                 (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
     144                 :        2273 :             },
     145                 :         654 :             [&] {
     146                 :         654 :                 (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
     147                 :         654 :             },
     148                 :        9734 :             [&] {
     149                 :        9734 :                 (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
     150                 :        9734 :             },
     151                 :       65353 :             [&] {
     152                 :       65353 :                 (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
     153                 :       65353 :             },
     154                 :        9144 :             [&] {
     155         [ +  - ]:        9144 :                 CSerializedNetMsg serialized_net_msg;
     156         [ +  - ]:        9144 :                 serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
     157                 :        9144 :                 serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
     158         [ +  - ]:        9144 :                 connman.PushMessage(&random_node, std::move(serialized_net_msg));
     159                 :        9144 :             },
     160                 :        4872 :             [&] {
     161         [ -  + ]:        4872 :                 connman.RemoveAddedNode(random_string);
     162                 :        4872 :             },
     163                 :       34555 :             [&] {
     164                 :       34555 :                 connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
     165                 :       34555 :             },
     166                 :         576 :             [&] {
     167                 :         576 :                 connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
     168                 :         576 :             },
     169                 :       65638 :             [&] {
     170                 :       65638 :                 ConnectionType conn_type{
     171                 :       65638 :                     fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES)};
     172         [ +  + ]:       65638 :                 if (conn_type == ConnectionType::INBOUND) { // INBOUND is not allowed
     173                 :         868 :                     conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
     174                 :             :                 }
     175                 :             : 
     176         [ -  + ]:      131276 :                 connman.OpenNetworkConnection(
     177                 :             :                     /*addrConnect=*/random_address,
     178         [ +  - ]:       65638 :                     /*fCountFailure=*/fuzzed_data_provider.ConsumeBool(),
     179                 :             :                     /*grant_outbound=*/{},
     180         [ +  + ]:       65638 :                     /*pszDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
     181                 :             :                     /*conn_type=*/conn_type,
     182                 :       65638 :                     /*use_v2transport=*/fuzzed_data_provider.ConsumeBool());
     183                 :       65638 :             },
     184                 :       26724 :             [&] {
     185                 :       26724 :                 connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
     186                 :       26724 :                 const auto peer = ConsumeAddress(fuzzed_data_provider);
     187                 :       26724 :                 connman.CreateNodeFromAcceptedSocketPublic(
     188         [ +  - ]:       53448 :                     /*sock=*/CreateSock(AF_INET, SOCK_STREAM, IPPROTO_TCP),
     189                 :             :                     /*permissions=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS),
     190                 :       53448 :                     /*addr_bind=*/ConsumeAddress(fuzzed_data_provider),
     191                 :             :                     /*addr_peer=*/peer);
     192                 :       26724 :             },
     193                 :        5418 :             [&] {
     194                 :        5418 :                 CConnman::Options options;
     195                 :             : 
     196                 :        5418 :                 options.vBinds = ConsumeServiceVector(fuzzed_data_provider);
     197                 :             : 
     198                 :        5418 :                 options.vWhiteBinds = std::vector<NetWhitebindPermissions>{
     199         [ +  - ]:       10836 :                     fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 5)};
     200         [ +  + ]:       12439 :                 for (auto& wb : options.vWhiteBinds) {
     201                 :        7021 :                     wb.m_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
     202                 :        7021 :                     wb.m_service = ConsumeService(fuzzed_data_provider);
     203                 :             :                 }
     204                 :             : 
     205                 :        5418 :                 options.onion_binds = ConsumeServiceVector(fuzzed_data_provider);
     206                 :             : 
     207   [ +  +  +  + ]:        5418 :                 options.bind_on_any = options.vBinds.empty() && options.vWhiteBinds.empty() &&
     208         [ +  + ]:        1223 :                                       options.onion_binds.empty();
     209                 :             : 
     210         [ +  - ]:        5418 :                 connman.InitBindsPublic(options);
     211                 :        5418 :             },
     212                 :        5542 :             [&] {
     213                 :        5542 :                 connman.SocketHandlerPublic();
     214                 :        5542 :             });
     215                 :             :     }
     216         [ +  - ]:        3922 :     (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
     217         [ +  - ]:        3922 :     (void)connman.GetExtraFullOutboundCount();
     218         [ +  - ]:        3922 :     (void)connman.GetLocalServices();
     219   [ +  -  -  + ]:        3922 :     assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
     220         [ +  - ]:        3922 :     (void)connman.GetMaxOutboundTimeframe();
     221         [ +  - ]:        3922 :     (void)connman.GetMaxOutboundTimeLeftInCycle();
     222         [ +  - ]:        3922 :     (void)connman.GetNetworkActive();
     223                 :        3922 :     std::vector<CNodeStats> stats;
     224         [ +  - ]:        3922 :     connman.GetNodeStats(stats);
     225         [ +  - ]:        3922 :     (void)connman.GetOutboundTargetBytesLeft();
     226         [ +  - ]:        3922 :     (void)connman.GetTotalBytesRecv();
     227         [ +  - ]:        3922 :     (void)connman.GetTotalBytesSent();
     228         [ +  - ]:        3922 :     (void)connman.GetTryNewOutboundPeer();
     229                 :        3922 :     (void)connman.GetUseAddrmanOutgoing();
     230         [ +  - ]:        3922 :     (void)connman.ASMapHealthCheck();
     231                 :             : 
     232         [ +  - ]:        3922 :     connman.ClearTestNodes();
     233         [ +  - ]:        3922 :     g_dns_lookup = g_dns_lookup_orig;
     234         [ +  - ]:        3922 :     CreateSock = CreateSockOrig;
     235                 :        3922 : }
        

Generated by: LCOV version 2.0-1