Bitcoin Core  22.99.0
P2P Digital Currency
connman.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 <addrman.h>
6 #include <chainparams.h>
7 #include <chainparamsbase.h>
8 #include <net.h>
9 #include <netaddress.h>
10 #include <protocol.h>
12 #include <test/fuzz/fuzz.h>
13 #include <test/fuzz/util.h>
14 #include <test/util/setup_common.h>
15 #include <util/system.h>
16 #include <util/translation.h>
17 
18 #include <cstdint>
19 #include <vector>
20 
21 namespace {
22 const BasicTestingSetup* g_setup;
23 } // namespace
24 
26 {
27  static const auto testing_setup = MakeNoLogFileContext<>();
28  g_setup = testing_setup.get();
29 }
30 
32 {
33  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
34  SetMockTime(ConsumeTime(fuzzed_data_provider));
35  AddrMan addrman(/*asmap=*/std::vector<bool>(),
36  /*deterministic=*/false,
37  g_setup->m_node.args->GetIntArg("-checkaddrman", 0));
38  CConnman connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>(), addrman, fuzzed_data_provider.ConsumeBool()};
39  CNetAddr random_netaddr;
40  CNode random_node = ConsumeNode(fuzzed_data_provider);
41  CSubNet random_subnet;
42  std::string random_string;
43  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
44  CallOneOf(
45  fuzzed_data_provider,
46  [&] {
47  random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
48  },
49  [&] {
50  random_subnet = ConsumeSubNet(fuzzed_data_provider);
51  },
52  [&] {
53  random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
54  },
55  [&] {
56  connman.AddNode(random_string);
57  },
58  [&] {
59  connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
60  },
61  [&] {
62  connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
63  },
64  [&] {
65  connman.DisconnectNode(random_netaddr);
66  },
67  [&] {
68  connman.DisconnectNode(random_string);
69  },
70  [&] {
71  connman.DisconnectNode(random_subnet);
72  },
73  [&] {
74  connman.ForEachNode([](auto) {});
75  },
76  [&] {
77  (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
78  },
79  [&] {
80  (void)connman.GetAddresses(
81  /*max_addresses=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
82  /*max_pct=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
83  /*network=*/std::nullopt);
84  },
85  [&] {
86  (void)connman.GetAddresses(
87  /*requestor=*/random_node,
88  /*max_addresses=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
89  /*max_pct=*/fuzzed_data_provider.ConsumeIntegral<size_t>());
90  },
91  [&] {
92  (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
93  },
94  [&] {
95  (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
96  },
97  [&] {
98  (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
99  },
100  [&] {
101  // Limit now to int32_t to avoid signed integer overflow
102  (void)connman.PoissonNextSendInbound(
103  std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int32_t>()},
104  std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int>()});
105  },
106  [&] {
107  CSerializedNetMsg serialized_net_msg;
108  serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::COMMAND_SIZE);
109  serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
110  connman.PushMessage(&random_node, std::move(serialized_net_msg));
111  },
112  [&] {
113  connman.RemoveAddedNode(random_string);
114  },
115  [&] {
116  connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
117  },
118  [&] {
119  connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
120  });
121  }
122  (void)connman.GetAddedNodeInfo();
123  (void)connman.GetExtraFullOutboundCount();
124  (void)connman.GetLocalServices();
125  (void)connman.GetMaxOutboundTarget();
126  (void)connman.GetMaxOutboundTimeframe();
127  (void)connman.GetMaxOutboundTimeLeftInCycle();
128  (void)connman.GetNetworkActive();
129  std::vector<CNodeStats> stats;
130  connman.GetNodeStats(stats);
131  (void)connman.GetOutboundTargetBytesLeft();
132  (void)connman.GetReceiveFloodSize();
133  (void)connman.GetTotalBytesRecv();
134  (void)connman.GetTotalBytesSent();
135  (void)connman.GetTryNewOutboundPeer();
136  (void)connman.GetUseAddrmanOutgoing();
137 }
NodeId
int64_t NodeId
Definition: net.h:88
ConnectionDirection::Out
@ Out
CSerializedNetMsg::data
std::vector< unsigned char > data
Definition: net.h:110
setup_common.h
CNetAddr
Network address.
Definition: netaddress.h:118
chainparamsbase.h
CNode
Information about a peer.
Definition: net.h:394
util.h
ConnectionDirection::Both
@ Both
AddrMan
Stochastic address manager.
Definition: addrman.h:79
CSerializedNetMsg::m_type
std::string m_type
Definition: net.h:111
ConsumeNetAddr
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:481
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(connman, initialize_connman)
Definition: connman.cpp:31
chainparams.h
ConsumeTime
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:281
ConnectionDirection::In
@ In
ConsumeSubNet
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:233
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:42
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
netaddress.h
CSerializedNetMsg
Definition: net.h:101
FuzzedDataProvider.h
CMessageHeader::COMMAND_SIZE
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:34
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:63
CSubNet
Definition: netaddress.h:475
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
ConsumeNode
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: util.h:249
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
system.h
CConnman
Definition: net.h:741
translation.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
initialize_connman
void initialize_connman()
Definition: connman.cpp:25
addrman.h
ConnectionDirection::None
@ None