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-03-31 04:13:53 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         [ +  - ]:        1335 : FUZZ_TARGET(net, .init = initialize_net)
      33                 :             : {
      34                 :         877 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      35                 :         877 :     NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
      36                 :         877 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      37         [ +  - ]:         877 :     node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
      38         [ +  + ]:         877 :     if (const auto service_opt =
      39         [ +  + ]:         877 :             ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
      40                 :             :     {
      41         [ +  - ]:         268 :         node.SetAddrLocal(*service_opt);
      42                 :           0 :     }
      43   [ +  +  +  + ]:      205343 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      44         [ +  - ]:      204466 :         CallOneOf(
      45                 :             :             fuzzed_data_provider,
      46                 :       11385 :             [&] {
      47                 :       11385 :                 node.CloseSocketDisconnect();
      48                 :       11385 :             },
      49                 :       94116 :             [&] {
      50                 :       94116 :                 CNodeStats stats;
      51         [ +  - ]:       94116 :                 node.CopyStats(stats);
      52                 :       94116 :             },
      53                 :        6828 :             [&] {
      54         [ -  + ]:        6828 :                 const CNode* add_ref_node = node.AddRef();
      55         [ -  + ]:        6828 :                 assert(add_ref_node == &node);
      56                 :        6828 :             },
      57                 :       40360 :             [&] {
      58         [ +  + ]:       40360 :                 if (node.GetRefCount() > 0) {
      59                 :        1836 :                     node.Release();
      60                 :             :                 }
      61                 :       40360 :             },
      62                 :       51777 :             [&] {
      63                 :       51777 :                 const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
      64                 :       51777 :                 bool complete;
      65   [ -  +  +  - ]:       51777 :                 node.ReceiveMsgBytes(b, complete);
      66                 :       51777 :             });
      67                 :             :     }
      68                 :             : 
      69         [ +  - ]:         877 :     (void)node.GetAddrLocal();
      70                 :         877 :     (void)node.GetId();
      71                 :         877 :     (void)node.GetLocalNonce();
      72                 :         877 :     const int ref_count = node.GetRefCount();
      73         [ -  + ]:         877 :     assert(ref_count >= 0);
      74                 :         877 :     (void)node.GetCommonVersion();
      75                 :             : 
      76                 :         877 :     const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
      77         [ +  - ]:         877 :     (void)node.HasPermission(net_permission_flags);
      78         [ +  - ]:         877 :     (void)node.ConnectedThroughNetwork();
      79                 :         877 : }
      80                 :             : 
      81         [ +  - ]:        1203 : FUZZ_TARGET(local_address, .init = initialize_net)
      82                 :             : {
      83                 :         745 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      84                 :         745 :     NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
      85                 :         745 :     CService service{ConsumeService(fuzzed_data_provider)};
      86                 :         745 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      87                 :         745 :     {
      88         [ +  - ]:         745 :         LOCK(g_maplocalhost_mutex);
      89         [ +  - ]:         745 :         mapLocalHost.clear();
      90                 :         745 :     }
      91   [ +  +  +  + ]:      272949 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      92         [ +  - ]:      272204 :         CallOneOf(
      93                 :             :             fuzzed_data_provider,
      94                 :       46239 :             [&] {
      95                 :       46239 :                 service = ConsumeService(fuzzed_data_provider);
      96                 :       46239 :             },
      97                 :      101872 :             [&] {
      98                 :      101872 :                 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
      99         [ +  + ]:      101872 :                 if (!added) return;
     100         [ -  + ]:       82407 :                 assert(service.IsRoutable());
     101         [ -  + ]:       82407 :                 assert(IsLocal(service));
     102         [ -  + ]:       82407 :                 assert(SeenLocal(service));
     103                 :             :             },
     104                 :       31891 :             [&] {
     105                 :       31891 :                 (void)RemoveLocal(service);
     106                 :       31891 :             },
     107                 :       26243 :             [&] {
     108                 :       26243 :                 (void)SeenLocal(service);
     109                 :       26243 :             },
     110                 :        7172 :             [&] {
     111                 :        7172 :                 (void)IsLocal(service);
     112                 :        7172 :             },
     113                 :       58787 :             [&] {
     114                 :       58787 :                 (void)GetLocalAddress(node);
     115                 :       58787 :             });
     116                 :             :     }
     117                 :         745 : }
        

Generated by: LCOV version 2.0-1