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