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