Bitcoin Core  26.99.0
P2P Digital Currency
connman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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 <common/args.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/fuzz/util/net.h>
15 #include <test/util/setup_common.h>
16 #include <util/translation.h>
17 
18 #include <cstdint>
19 #include <vector>
20 
21 namespace {
22 const TestingSetup* g_setup;
23 } // namespace
24 
26 {
27  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
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  ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
36  fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
37  *g_setup->m_node.addrman,
38  *g_setup->m_node.netgroupman,
39  Params(),
40  fuzzed_data_provider.ConsumeBool()};
41 
42  const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
43  CConnman::Options options;
44  options.nMaxOutboundLimit = max_outbound_limit;
45  connman.Init(options);
46 
47  CNetAddr random_netaddr;
48  CNode random_node = ConsumeNode(fuzzed_data_provider);
49  CSubNet random_subnet;
50  std::string random_string;
51 
52  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
53  CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
54  connman.AddTestNode(p2p_node);
55  }
56 
57  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
58  CallOneOf(
59  fuzzed_data_provider,
60  [&] {
61  random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
62  },
63  [&] {
64  random_subnet = ConsumeSubNet(fuzzed_data_provider);
65  },
66  [&] {
67  random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
68  },
69  [&] {
70  connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
71  },
72  [&] {
73  connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
74  },
75  [&] {
76  connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
77  },
78  [&] {
79  connman.DisconnectNode(random_netaddr);
80  },
81  [&] {
82  connman.DisconnectNode(random_string);
83  },
84  [&] {
85  connman.DisconnectNode(random_subnet);
86  },
87  [&] {
88  connman.ForEachNode([](auto) {});
89  },
90  [&] {
91  (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
92  },
93  [&] {
94  (void)connman.GetAddresses(
95  /*max_addresses=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
96  /*max_pct=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
97  /*network=*/std::nullopt,
98  /*filtered=*/fuzzed_data_provider.ConsumeBool());
99  },
100  [&] {
101  (void)connman.GetAddresses(
102  /*requestor=*/random_node,
103  /*max_addresses=*/fuzzed_data_provider.ConsumeIntegral<size_t>(),
104  /*max_pct=*/fuzzed_data_provider.ConsumeIntegral<size_t>());
105  },
106  [&] {
107  (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
108  },
109  [&] {
110  (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
111  },
112  [&] {
113  (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
114  },
115  [&] {
116  CSerializedNetMsg serialized_net_msg;
117  serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::COMMAND_SIZE);
118  serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
119  connman.PushMessage(&random_node, std::move(serialized_net_msg));
120  },
121  [&] {
122  connman.RemoveAddedNode(random_string);
123  },
124  [&] {
125  connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
126  },
127  [&] {
128  connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
129  });
130  }
131  (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
132  (void)connman.GetExtraFullOutboundCount();
133  (void)connman.GetLocalServices();
134  assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
135  (void)connman.GetMaxOutboundTimeframe();
136  (void)connman.GetMaxOutboundTimeLeftInCycle();
137  (void)connman.GetNetworkActive();
138  std::vector<CNodeStats> stats;
139  connman.GetNodeStats(stats);
140  (void)connman.GetOutboundTargetBytesLeft();
141  (void)connman.GetTotalBytesRecv();
142  (void)connman.GetTotalBytesSent();
143  (void)connman.GetTryNewOutboundPeer();
144  (void)connman.GetUseAddrmanOutgoing();
145 
146  connman.ClearTestNodes();
147 }
const CChainParams & Params()
Return the currently selected parameters.
AddrMan & addrman
Definition: net.h:1409
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:31
Network address.
Definition: netaddress.h:112
Information about a peer.
Definition: net.h:677
void initialize_connman()
Definition: connman.cpp:25
FUZZ_TARGET(connman,.init=initialize_connman)
Definition: connman.cpp:31
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:23
int64_t NodeId
Definition: net.h:102
uint64_t nMaxOutboundLimit
Definition: net.h:1053
std::string m_type
Definition: net.h:136
std::vector< unsigned char > data
Definition: net.h:135
Testing setup that configures a complete environment.
Definition: setup_common.h:78
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:28
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:96
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:145
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:109
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
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
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:81
assert(!tx.IsCoinBase())