Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
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>
11
#include <
test/fuzz/FuzzedDataProvider.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
>
16
#include <
test/util/setup_common.h
>
17
#include <
util/asmap.h
>
18
#include <
util/chaintype.h
>
19
#include <util/time.h>
20
21
#include <cstdint>
22
#include <optional>
23
#include <string>
24
#include <vector>
25
26
void
initialize_net
()
27
{
28
static
const
auto
testing_setup =
MakeNoLogFileContext<>
(
ChainType::MAIN
);
29
}
30
31
FUZZ_TARGET
(net, .
init
=
initialize_net
)
32
{
33
FuzzedDataProvider
fuzzed_data_provider(buffer.data(), buffer.size());
34
SetMockTime
(
ConsumeTime
(fuzzed_data_provider));
35
CNode
node
{
ConsumeNode
(fuzzed_data_provider)};
36
node
.SetCommonVersion(fuzzed_data_provider.
ConsumeIntegral
<
int
>());
37
if
(
const
auto
service_opt =
38
ConsumeDeserializable<CService>
(fuzzed_data_provider,
ConsumeDeserializationParams<CNetAddr::SerParams>
(fuzzed_data_provider)))
39
{
40
node
.SetAddrLocal(*service_opt);
41
}
42
LIMITED_WHILE
(fuzzed_data_provider.
ConsumeBool
(), 10000) {
43
CallOneOf
(
44
fuzzed_data_provider,
45
[&] {
46
node
.CloseSocketDisconnect();
47
},
48
[&] {
49
CNodeStats
stats;
50
node
.CopyStats(stats);
51
},
52
[&] {
53
const
CNode
* add_ref_node =
node
.AddRef();
54
assert
(add_ref_node == &
node
);
55
},
56
[&] {
57
if
(
node
.GetRefCount() > 0) {
58
node
.Release();
59
}
60
},
61
[&] {
62
const
std::vector<uint8_t> b =
ConsumeRandomLengthByteVector
(fuzzed_data_provider);
63
bool
complete;
64
node
.ReceiveMsgBytes(b, complete);
65
});
66
}
67
68
(void)
node
.GetAddrLocal();
69
(void)
node
.GetId();
70
(void)
node
.GetLocalNonce();
71
const
int
ref_count =
node
.GetRefCount();
72
assert
(ref_count >= 0);
73
(void)
node
.GetCommonVersion();
74
75
const
NetPermissionFlags
net_permission_flags =
ConsumeWeakEnum
(fuzzed_data_provider,
ALL_NET_PERMISSION_FLAGS
);
76
(void)
node
.HasPermission(net_permission_flags);
77
(void)
node
.ConnectedThroughNetwork();
78
}
79
80
FUZZ_TARGET
(local_address, .
init
=
initialize_net
)
81
{
82
FuzzedDataProvider
fuzzed_data_provider(buffer.data(), buffer.size());
83
SetMockTime
(
ConsumeTime
(fuzzed_data_provider));
84
CService
service{
ConsumeService
(fuzzed_data_provider)};
85
CNode
node
{
ConsumeNode
(fuzzed_data_provider)};
86
{
87
LOCK
(
g_maplocalhost_mutex
);
88
mapLocalHost.clear();
89
}
90
LIMITED_WHILE
(fuzzed_data_provider.
ConsumeBool
(), 10000) {
91
CallOneOf
(
92
fuzzed_data_provider,
93
[&] {
94
service =
ConsumeService
(fuzzed_data_provider);
95
},
96
[&] {
97
const
bool
added{
AddLocal
(service, fuzzed_data_provider.
ConsumeIntegralInRange
<
int
>(0,
LOCAL_MAX
- 1))};
98
if
(!added)
return
;
99
assert
(service.
IsRoutable
());
100
assert
(
IsLocal
(service));
101
assert
(
SeenLocal
(service));
102
},
103
[&] {
104
(void)
RemoveLocal
(service);
105
},
106
[&] {
107
(void)
SeenLocal
(service);
108
},
109
[&] {
110
(void)
IsLocal
(service);
111
},
112
[&] {
113
(void)
GetLocalAddress
(
node
);
114
});
115
}
116
}
FuzzedDataProvider.h
asmap.h
chaintype.h
ChainType::MAIN
@ MAIN
Definition
chaintype.h:12
CNetAddr::IsRoutable
bool IsRoutable() const
Definition
netaddress.cpp:462
CNode
Information about a peer.
Definition
net.h:681
CNodeStats
Definition
net.h:192
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition
netaddress.h:530
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition
FuzzedDataProvider.h:205
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition
FuzzedDataProvider.h:195
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition
fuzz.h:22
init
Definition
bitcoin-gui.cpp:17
node
Definition
messages.h:21
IsLocal
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition
net.cpp:329
RemoveLocal
void RemoveLocal(const CService &addr)
Definition
net.cpp:310
AddLocal
bool AddLocal(const CService &addr_, int nScore)
Definition
net.cpp:277
GetLocalAddress
CService GetLocalAddress(const CNode &peer)
Definition
net.cpp:220
g_maplocalhost_mutex
GlobalMutex g_maplocalhost_mutex
Definition
net.cpp:118
SeenLocal
bool SeenLocal(const CService &addr)
vote for a local address
Definition
net.cpp:318
LOCAL_MAX
@ LOCAL_MAX
Definition
net.h:160
net_permissions.h
NetPermissionFlags
NetPermissionFlags
Definition
net_permissions.h:24
netaddress.h
setup_common.h
MakeNoLogFileContext
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Definition
setup_common.h:250
LOCK
#define LOCK(cs)
Definition
sync.h:258
initialize_net
void initialize_net()
Definition
net.cpp:26
net.h
ConsumeService
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition
net.h:250
ConsumeNode
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition
net.h:270
ConsumeTime
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition
util.cpp:34
util.h
ConsumeWeakEnum
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition
util.h:128
ConsumeDeserializationParams
P ConsumeDeserializationParams(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition
net.cpp:94
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition
util.h:35
ConsumeRandomLengthByteVector
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:57
ConsumeDeserializable
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:100
net.h
ALL_NET_PERMISSION_FLAGS
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition
net.h:130
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition
time.cpp:44
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0