LCOV - code coverage report
Current view: top level - src/test/fuzz - net.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.7 % 77 76
Test Date: 2026-06-17 08:15:17 Functions: 100.0 % 12 12
Branches: 62.9 % 62 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 <chainparams.h>
       6                 :             : #include <net.h>
       7                 :             : #include <net_permissions.h>
       8                 :             : #include <netaddress.h>
       9                 :             : #include <protocol.h>
      10                 :             : #include <random.h>
      11                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      12                 :             : #include <test/fuzz/fuzz.h>
      13                 :             : #include <test/fuzz/util.h>
      14                 :             : #include <test/fuzz/util/net.h>
      15                 :             : #include <test/util/net.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <test/util/time.h>
      18                 :             : #include <util/asmap.h>
      19                 :             : #include <util/chaintype.h>
      20                 :             : #include <util/time.h>
      21                 :             : 
      22                 :             : #include <cstdint>
      23                 :             : #include <optional>
      24                 :             : #include <string>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :           2 : void initialize_net()
      28                 :             : {
      29   [ +  -  +  -  :           2 :     static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
                   +  - ]
      30                 :           2 : }
      31                 :             : 
      32         [ +  - ]:        1963 : FUZZ_TARGET(net, .init = initialize_net)
      33                 :             : {
      34                 :        1497 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      35                 :        1497 :     FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
      36                 :        1497 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      37         [ +  - ]:        1497 :     node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
      38         [ +  + ]:        1497 :     if (const auto service_opt =
      39         [ +  + ]:        1497 :             ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
      40                 :             :     {
      41         [ +  - ]:         507 :         node.SetAddrLocal(*service_opt);
      42                 :           0 :     }
      43   [ +  +  +  + ]:      385083 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      44         [ +  - ]:      383586 :         CallOneOf(
      45                 :             :             fuzzed_data_provider,
      46                 :       23554 :             [&] {
      47                 :       23554 :                 node.CloseSocketDisconnect();
      48                 :       23554 :             },
      49                 :      152036 :             [&] {
      50                 :      152036 :                 CNodeStats stats;
      51         [ +  - ]:      152036 :                 node.CopyStats(stats);
      52                 :      152036 :             },
      53                 :       13716 :             [&] {
      54         [ -  + ]:       13716 :                 const CNode* add_ref_node = node.AddRef();
      55         [ -  + ]:       13716 :                 assert(add_ref_node == &node);
      56                 :       13716 :             },
      57                 :       89021 :             [&] {
      58         [ +  + ]:       89021 :                 if (node.GetRefCount() > 0) {
      59                 :        4338 :                     node.Release();
      60                 :             :                 }
      61                 :       89021 :             },
      62                 :      105259 :             [&] {
      63                 :      105259 :                 const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
      64                 :      105259 :                 bool complete;
      65   [ -  +  +  - ]:      105259 :                 node.ReceiveMsgBytes(b, complete);
      66                 :      105259 :             });
      67                 :             :     }
      68                 :             : 
      69         [ +  - ]:        1497 :     (void)node.GetAddrLocal();
      70                 :        1497 :     (void)node.GetId();
      71                 :        1497 :     (void)node.GetLocalNonce();
      72                 :        1497 :     const int ref_count = node.GetRefCount();
      73         [ -  + ]:        1497 :     assert(ref_count >= 0);
      74                 :        1497 :     (void)node.GetCommonVersion();
      75                 :             : 
      76                 :        1497 :     const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
      77         [ +  - ]:        1497 :     (void)node.HasPermission(net_permission_flags);
      78         [ +  - ]:        1497 :     (void)node.ConnectedThroughNetwork();
      79                 :        1497 : }
      80                 :             : 
      81         [ +  - ]:        1885 : FUZZ_TARGET(local_address, .init = initialize_net)
      82                 :             : {
      83                 :        1419 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      84                 :        1419 :     FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
      85                 :        1419 :     CService service{ConsumeService(fuzzed_data_provider)};
      86                 :        1419 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      87                 :        1419 :     {
      88         [ +  - ]:        1419 :         LOCK(g_maplocalhost_mutex);
      89         [ +  - ]:        1419 :         mapLocalHost.clear();
      90                 :        1419 :     }
      91   [ +  +  +  + ]:      586160 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      92         [ +  - ]:      584741 :         CallOneOf(
      93                 :             :             fuzzed_data_provider,
      94                 :      103810 :             [&] {
      95                 :      103810 :                 service = ConsumeService(fuzzed_data_provider);
      96                 :      103810 :             },
      97                 :      215840 :             [&] {
      98                 :      215840 :                 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
      99         [ +  + ]:      215840 :                 if (!added) return;
     100         [ -  + ]:      172243 :                 assert(service.IsRoutable());
     101         [ -  + ]:      172243 :                 assert(IsLocal(service));
     102         [ -  + ]:      172243 :                 assert(SeenLocal(service));
     103                 :             :             },
     104                 :       64182 :             [&] {
     105                 :       64182 :                 (void)RemoveLocal(service);
     106                 :       64182 :             },
     107                 :       64587 :             [&] {
     108                 :       64587 :                 (void)SeenLocal(service);
     109                 :       64587 :             },
     110                 :       16035 :             [&] {
     111                 :       16035 :                 (void)IsLocal(service);
     112                 :       16035 :             },
     113                 :      120287 :             [&] {
     114                 :      120287 :                 (void)GetLocalAddress(node);
     115                 :      120287 :             });
     116                 :             :     }
     117                 :        1419 : }
        

Generated by: LCOV version 2.0-1