Bitcoin Core  24.99.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
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 <chainparamsbase.h>
7 #include <net.h>
8 #include <net_permissions.h>
9 #include <netaddress.h>
10 #include <protocol.h>
11 #include <random.h>
13 #include <test/fuzz/fuzz.h>
14 #include <test/fuzz/util.h>
15 #include <test/fuzz/util/net.h>
16 #include <test/util/net.h>
17 #include <test/util/setup_common.h>
18 #include <util/asmap.h>
19 
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
26 {
27  static const auto testing_setup = MakeNoLogFileContext<>(CBaseChainParams::MAIN);
28 }
29 
31 {
32  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33  SetMockTime(ConsumeTime(fuzzed_data_provider));
34  CNode node{ConsumeNode(fuzzed_data_provider)};
35  node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
36  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
37  CallOneOf(
38  fuzzed_data_provider,
39  [&] {
40  node.CloseSocketDisconnect();
41  },
42  [&] {
43  CNodeStats stats;
44  node.CopyStats(stats);
45  },
46  [&] {
47  const CNode* add_ref_node = node.AddRef();
48  assert(add_ref_node == &node);
49  },
50  [&] {
51  if (node.GetRefCount() > 0) {
52  node.Release();
53  }
54  },
55  [&] {
56  const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider);
57  if (!service_opt) {
58  return;
59  }
60  node.SetAddrLocal(*service_opt);
61  },
62  [&] {
63  const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
64  bool complete;
65  node.ReceiveMsgBytes(b, complete);
66  });
67  }
68 
69  (void)node.GetAddrLocal();
70  (void)node.GetId();
71  (void)node.GetLocalNonce();
72  const int ref_count = node.GetRefCount();
73  assert(ref_count >= 0);
74  (void)node.GetCommonVersion();
75 
76  const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
77  (void)node.HasPermission(net_permission_flags);
78  (void)node.ConnectedThroughNetwork();
79 }
static const std::string MAIN
Chain name strings.
Information about a peer.
Definition: net.h:347
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
Definition: init.h:25
NetPermissionFlags
void initialize_net()
Definition: net.cpp:25
FUZZ_TARGET_INIT(net, initialize_net)
Definition: net.cpp:30
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:101
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:22
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:111
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:36
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:66
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:89
assert(!tx.IsCoinBase())