Bitcoin Core 28.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 <net.h>
7#include <net_permissions.h>
8#include <netaddress.h>
9#include <protocol.h>
10#include <random.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>
17#include <util/asmap.h>
18#include <util/chaintype.h>
19
20#include <cstdint>
21#include <optional>
22#include <string>
23#include <vector>
24
26{
27 static const auto testing_setup = MakeNoLogFileContext<>(ChainType::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 if (const auto service_opt =
37 ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
38 {
39 node.SetAddrLocal(*service_opt);
40 }
41 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
43 fuzzed_data_provider,
44 [&] {
45 node.CloseSocketDisconnect();
46 },
47 [&] {
48 CNodeStats stats;
49 node.CopyStats(stats);
50 },
51 [&] {
52 const CNode* add_ref_node = node.AddRef();
53 assert(add_ref_node == &node);
54 },
55 [&] {
56 if (node.GetRefCount() > 0) {
57 node.Release();
58 }
59 },
60 [&] {
61 const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
62 bool complete;
63 node.ReceiveMsgBytes(b, complete);
64 });
65 }
66
67 (void)node.GetAddrLocal();
68 (void)node.GetId();
69 (void)node.GetLocalNonce();
70 const int ref_count = node.GetRefCount();
71 assert(ref_count >= 0);
72 (void)node.GetCommonVersion();
73
74 const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
75 (void)node.HasPermission(net_permission_flags);
76 (void)node.ConnectedThroughNetwork();
77}
78
80{
81 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
82 CService service{ConsumeService(fuzzed_data_provider)};
83 CNode node{ConsumeNode(fuzzed_data_provider)};
84 {
86 mapLocalHost.clear();
87 }
88 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
90 fuzzed_data_provider,
91 [&] {
92 service = ConsumeService(fuzzed_data_provider);
93 },
94 [&] {
95 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
96 if (!added) return;
97 assert(service.IsRoutable());
98 assert(IsLocal(service));
99 assert(SeenLocal(service));
100 },
101 [&] {
102 (void)RemoveLocal(service);
103 },
104 [&] {
105 (void)SeenLocal(service);
106 },
107 [&] {
108 (void)IsLocal(service);
109 },
110 [&] {
111 (void)GetLocalAddress(node);
112 });
113 }
114}
Information about a peer.
Definition: net.h:673
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
T ConsumeIntegralInRange(T min, T max)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
Definition: messages.h:20
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:325
void RemoveLocal(const CService &addr)
Definition: net.cpp:306
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:273
CService GetLocalAddress(const CNode &peer)
Definition: net.cpp:222
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:120
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:314
@ LOCAL_MAX
Definition: net.h:154
NetPermissionFlags
#define LOCK(cs)
Definition: sync.h:257
FUZZ_TARGET(net,.init=initialize_net)
Definition: net.cpp:30
void initialize_net()
Definition: net.cpp:25
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:213
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:221
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:133
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:104
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:35
assert(!tx.IsCoinBase())