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());
37 node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
38 if (const auto service_opt =
39 ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
40 {
41 node.SetAddrLocal(*service_opt);
42 }
46 [&] {
47 node.CloseSocketDisconnect();
48 },
49 [&] {
50 CNodeStats stats;
51 node.CopyStats(stats);
52 },
53 [&] {
54 const CNode* add_ref_node = node.AddRef();
55 assert(add_ref_node == &node);
56 },
57 [&] {
58 if (node.GetRefCount() > 0) {
59 node.Release();
60 }
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
77 (void)node.HasPermission(net_permission_flags);
78 (void)node.ConnectedThroughNetwork();
79}
80
82{
83 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
87 {
89 mapLocalHost.clear();
90 }
94 [&] {
96 },
97 [&] {
98 const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
99 if (!added) return;
100 assert(service.IsRoutable());
101 assert(IsLocal(service));
102 assert(SeenLocal(service));
103 },
104 [&] {
105 (void)RemoveLocal(service);
106 },
107 [&] {
108 (void)SeenLocal(service);
109 },
110 [&] {
111 (void)IsLocal(service);
112 },
113 [&] {
114 (void)GetLocalAddress(node);
115 });
116 }
117}
Information about a peer.
Definition: net.h:679
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
T ConsumeIntegralInRange(T min, T max)
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
#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:334
void RemoveLocal(const CService &addr)
Definition: net.cpp:312
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:277
CService GetLocalAddress(const CNode &peer)
Definition: net.cpp:220
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:118
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:323
@ 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, 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:128
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:131
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39