Bitcoin Core  22.99.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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/util/net.h>
16 #include <test/util/setup_common.h>
17 #include <util/asmap.h>
18 
19 #include <cstdint>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
25 {
26  static const auto testing_setup = MakeNoLogFileContext<>(CBaseChainParams::MAIN);
27 }
28 
30 {
31  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
32  SetMockTime(ConsumeTime(fuzzed_data_provider));
33  CNode node{ConsumeNode(fuzzed_data_provider)};
34  node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
35  while (fuzzed_data_provider.ConsumeBool()) {
36  CallOneOf(
37  fuzzed_data_provider,
38  [&] {
39  node.CloseSocketDisconnect();
40  },
41  [&] {
42  CNodeStats stats;
43  node.CopyStats(stats);
44  },
45  [&] {
46  const CNode* add_ref_node = node.AddRef();
47  assert(add_ref_node == &node);
48  },
49  [&] {
50  if (node.GetRefCount() > 0) {
51  node.Release();
52  }
53  },
54  [&] {
55  const std::optional<CInv> inv_opt = ConsumeDeserializable<CInv>(fuzzed_data_provider);
56  if (!inv_opt) {
57  return;
58  }
59  node.AddKnownTx(inv_opt->hash);
60  },
61  [&] {
62  node.PushTxInventory(ConsumeUInt256(fuzzed_data_provider));
63  },
64  [&] {
65  const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider);
66  if (!service_opt) {
67  return;
68  }
69  node.SetAddrLocal(*service_opt);
70  },
71  [&] {
72  const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
73  bool complete;
74  node.ReceiveMsgBytes(b, complete);
75  });
76  }
77 
78  (void)node.GetAddrLocal();
79  (void)node.GetId();
80  (void)node.GetLocalNonce();
81  (void)node.GetLocalServices();
82  const int ref_count = node.GetRefCount();
83  assert(ref_count >= 0);
84  (void)node.GetCommonVersion();
85 
86  const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
87  (void)node.HasPermission(net_permission_flags);
88  (void)node.ConnectedThroughNetwork();
89 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
assert
assert(!tx.IsCoinBase())
asmap.h
setup_common.h
chainparamsbase.h
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(net, initialize_net)
Definition: net.cpp:29
CNode
Information about a peer.
Definition: net.h:393
util.h
ALL_NET_PERMISSION_FLAGS
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:50
chainparams.h
NetPermissionFlags
NetPermissionFlags
Definition: net_permissions.h:18
ConsumeTime
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:226
random.h
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
net.h
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
netaddress.h
initialize_net
void initialize_net()
Definition: net.cpp:24
FuzzedDataProvider.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
ConsumeNode
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: util.h:258
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
node
Definition: interfaces.cpp:68
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
ConsumeWeakEnum
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:115
CNodeStats
Definition: net.h:238
net_permissions.h