LCOV - code coverage report
Current view: top level - src/test/fuzz - net.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 0.0 % 78 0
Test Date: 2024-07-07 05:05:19 Functions: 0.0 % 13 0
Branches: 0.0 % 56 0

             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 <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 <util/asmap.h>
      18                 :             : #include <util/chaintype.h>
      19                 :             : 
      20                 :             : #include <cstdint>
      21                 :             : #include <optional>
      22                 :             : #include <string>
      23                 :             : #include <vector>
      24                 :             : 
      25                 :           0 : void initialize_net()
      26                 :             : {
      27   [ #  #  #  #  :           0 :     static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
                   #  # ]
      28                 :           0 : }
      29                 :             : 
      30         [ #  # ]:           0 : FUZZ_TARGET(net, .init = initialize_net)
      31                 :             : {
      32                 :           0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      33                 :           0 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      34                 :           0 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      35                 :           0 :     node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
      36   [ #  #  #  # ]:           0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      37         [ #  # ]:           0 :         CallOneOf(
      38                 :             :             fuzzed_data_provider,
      39                 :           0 :             [&] {
      40                 :           0 :                 node.CloseSocketDisconnect();
      41                 :           0 :             },
      42                 :           0 :             [&] {
      43                 :           0 :                 CNodeStats stats;
      44         [ #  # ]:           0 :                 node.CopyStats(stats);
      45                 :           0 :             },
      46                 :           0 :             [&] {
      47         [ #  # ]:           0 :                 const CNode* add_ref_node = node.AddRef();
      48         [ #  # ]:           0 :                 assert(add_ref_node == &node);
      49                 :           0 :             },
      50                 :           0 :             [&] {
      51         [ #  # ]:           0 :                 if (node.GetRefCount() > 0) {
      52                 :           0 :                     node.Release();
      53                 :             :                 }
      54                 :           0 :             },
      55                 :           0 :             [&] {
      56                 :           0 :                 const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider));
      57         [ #  # ]:           0 :                 if (!service_opt) {
      58                 :           0 :                     return;
      59                 :             :                 }
      60         [ #  # ]:           0 :                 node.SetAddrLocal(*service_opt);
      61                 :           0 :             },
      62                 :           0 :             [&] {
      63                 :           0 :                 const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
      64                 :           0 :                 bool complete;
      65         [ #  # ]:           0 :                 node.ReceiveMsgBytes(b, complete);
      66                 :           0 :             });
      67                 :             :     }
      68                 :             : 
      69         [ #  # ]:           0 :     (void)node.GetAddrLocal();
      70                 :           0 :     (void)node.GetId();
      71                 :           0 :     (void)node.GetLocalNonce();
      72                 :           0 :     const int ref_count = node.GetRefCount();
      73         [ #  # ]:           0 :     assert(ref_count >= 0);
      74                 :           0 :     (void)node.GetCommonVersion();
      75                 :             : 
      76                 :           0 :     const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
      77         [ #  # ]:           0 :     (void)node.HasPermission(net_permission_flags);
      78         [ #  # ]:           0 :     (void)node.ConnectedThroughNetwork();
      79                 :           0 : }
      80                 :             : 
      81         [ #  # ]:           0 : FUZZ_TARGET(local_address, .init = initialize_net)
      82                 :             : {
      83                 :           0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      84                 :           0 :     CService service{ConsumeService(fuzzed_data_provider)};
      85                 :           0 :     CNode node{ConsumeNode(fuzzed_data_provider)};
      86                 :           0 :     {
      87         [ #  # ]:           0 :         LOCK(g_maplocalhost_mutex);
      88         [ #  # ]:           0 :         mapLocalHost.clear();
      89                 :           0 :     }
      90   [ #  #  #  # ]:           0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      91         [ #  # ]:           0 :         CallOneOf(
      92                 :             :             fuzzed_data_provider,
      93                 :           0 :             [&] {
      94                 :           0 :                 service = ConsumeService(fuzzed_data_provider);
      95                 :           0 :             },
      96                 :           0 :             [&] {
      97                 :           0 :                 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
      98         [ #  # ]:           0 :                 if (!added) return;
      99         [ #  # ]:           0 :                 assert(service.IsRoutable());
     100         [ #  # ]:           0 :                 assert(IsLocal(service));
     101         [ #  # ]:           0 :                 assert(SeenLocal(service));
     102                 :             :             },
     103                 :           0 :             [&] {
     104                 :           0 :                 (void)RemoveLocal(service);
     105                 :           0 :             },
     106                 :           0 :             [&] {
     107                 :           0 :                 (void)SeenLocal(service);
     108                 :           0 :             },
     109                 :           0 :             [&] {
     110                 :           0 :                 (void)IsLocal(service);
     111                 :           0 :             },
     112                 :           0 :             [&] {
     113                 :           0 :                 (void)GetLocalAddress(node);
     114                 :           0 :             });
     115                 :             :     }
     116                 :           0 : }
        

Generated by: LCOV version 2.0-1