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