Bitcoin Core 31.99.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
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>
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 <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
28{
29 static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
30}
31
33{
34 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
36 FakeSteadyClock steady_clock;
38 node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
39 if (const auto service_opt =
40 ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
41 {
42 node.SetAddrLocal(*service_opt);
43 }
47 [&] {
48 node.CloseSocketDisconnect();
49 },
50 [&] {
51 CNodeStats stats;
52 node.CopyStats(stats);
53 },
54 [&] {
55 const CNode* add_ref_node = node.AddRef();
56 assert(add_ref_node == &node);
57 },
58 [&] {
59 if (node.GetRefCount() > 0) {
60 node.Release();
61 }
62 },
63 [&] {
64 const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
65 bool complete;
66 node.ReceiveMsgBytes(b, complete);
67 });
68 }
69
70 (void)node.GetAddrLocal();
71 (void)node.GetId();
72 (void)node.GetLocalNonce();
73 const int ref_count = node.GetRefCount();
74 assert(ref_count >= 0);
75 (void)node.GetCommonVersion();
76
78 (void)node.HasPermission(net_permission_flags);
79 (void)node.ConnectedThroughNetwork();
80}
81
83{
84 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
86 FakeSteadyClock steady_clock;
89 {
91 mapLocalHost.clear();
92 }
96 [&] {
98 },
99 [&] {
100 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
101 if (!added) return;
102 assert(service.IsRoutable());
103 assert(IsLocal(service));
104 assert(SeenLocal(service));
105 },
106 [&] {
107 (void)RemoveLocal(service);
108 },
109 [&] {
110 (void)SeenLocal(service);
111 },
112 [&] {
113 (void)IsLocal(service);
114 },
115 [&] {
116 (void)GetLocalAddress(node);
117 });
118 }
119}
Information about a peer.
Definition: net.h:681
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:54
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition: time.h:29
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: basic.cpp:8
Definition: messages.h:21
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:335
void RemoveLocal(const CService &addr)
Definition: net.cpp:313
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:278
CService GetLocalAddress(const CNode &peer)
Definition: net.cpp:221
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:119
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:324
@ LOCAL_MAX
Definition: net.h:160
NetPermissionFlags
#define LOCK(cs)
Definition: sync.h:268
FUZZ_TARGET(net,.init=initialize_net)
Definition: net.cpp:32
void initialize_net()
Definition: net.cpp:27
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:250
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, FakeSteadyClock &clock, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:270
NodeSeconds 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:147
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:37
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:63
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:131
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39