Branch data Line data Source code
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>
11 : : #include <test/fuzz/FuzzedDataProvider.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 : :
24 : 2422 : int32_t GetCheckRatio()
25 : : {
26 [ + - + - ]: 4844 : return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
27 : : }
28 : :
29 : : } // namespace
30 : :
31 : 1 : void initialize_connman()
32 : : {
33 [ + - + - : 1 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
+ - ]
34 : 1 : g_setup = testing_setup.get();
35 : 1 : }
36 : :
37 [ + - ]: 2169 : FUZZ_TARGET(connman, .init = initialize_connman)
38 : : {
39 : 1713 : SeedRandomStateForTest(SeedRand::ZEROS);
40 : 1713 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
41 : 1713 : SetMockTime(ConsumeTime(fuzzed_data_provider));
42 : 1713 : auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
43 [ + - + - ]: 1713 : auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
44 [ + + ]: 1713 : if (fuzzed_data_provider.ConsumeBool()) {
45 : 1145 : const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
46 [ - + + - ]: 1145 : DataStream ds{serialized_data};
47 : 1145 : try {
48 [ + + ]: 2290 : ds >> *addr_man_ptr;
49 [ - + ]: 709 : } catch (const std::ios_base::failure&) {
50 [ + - + - ]: 1418 : addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
51 : 709 : }
52 : 1145 : }
53 : 1713 : AddrManDeterministic& addr_man{*addr_man_ptr};
54 : 1713 : ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
55 : : fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
56 : : addr_man,
57 : : netgroupman,
58 : : Params(),
59 [ + - + - ]: 1713 : fuzzed_data_provider.ConsumeBool()};
60 : :
61 : 1713 : const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
62 : 1713 : CConnman::Options options;
63 : 1713 : options.nMaxOutboundLimit = max_outbound_limit;
64 [ + - ]: 1713 : connman.Init(options);
65 : :
66 [ + - ]: 1713 : CNetAddr random_netaddr;
67 : 1713 : CNode random_node = ConsumeNode(fuzzed_data_provider);
68 [ + - ]: 1713 : CSubNet random_subnet;
69 : 1713 : std::string random_string;
70 : :
71 [ + + + + ]: 17556 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
72 : 15843 : CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
73 [ + - ]: 15843 : connman.AddTestNode(p2p_node);
74 : : }
75 : :
76 [ + + + + ]: 523281 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
77 [ + - ]: 521568 : CallOneOf(
78 : : fuzzed_data_provider,
79 : 2341 : [&] {
80 : 2341 : random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
81 : 2341 : },
82 : 59383 : [&] {
83 : 59383 : random_subnet = ConsumeSubNet(fuzzed_data_provider);
84 : 59383 : },
85 : 18761 : [&] {
86 : 18761 : random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
87 : 18761 : },
88 : 76027 : [&] {
89 : 304108 : connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
90 [ - + + - ]: 228081 : },
91 : 1302 : [&] {
92 : 1302 : connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
93 : 1302 : },
94 : 33748 : [&] {
95 : 33748 : connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
96 : 33748 : },
97 : 19746 : [&] {
98 : 19746 : connman.DisconnectNode(random_netaddr);
99 : 19746 : },
100 : 32647 : [&] {
101 : 32647 : connman.DisconnectNode(random_string);
102 : 32647 : },
103 : 5410 : [&] {
104 : 5410 : connman.DisconnectNode(random_subnet);
105 : 5410 : },
106 : 13544 : [&] {
107 [ + - ]: 13544 : connman.ForEachNode([](auto) {});
108 : 13544 : },
109 : 1747 : [&] {
110 [ + - ]: 1747 : (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
111 : 1747 : },
112 : 98693 : [&] {
113 : 98693 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
114 : 98693 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
115 : 98693 : auto filtered = fuzzed_data_provider.ConsumeBool();
116 : 98693 : (void)connman.GetAddressesUnsafe(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
117 : 98693 : },
118 : 5142 : [&] {
119 : 5142 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
120 : 5142 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
121 : 5142 : (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
122 : 5142 : },
123 : 1463 : [&] {
124 : 1463 : (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
125 : 1463 : },
126 : 2312 : [&] {
127 : 2312 : (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
128 : 2312 : },
129 : 37785 : [&] {
130 : 37785 : (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
131 : 37785 : },
132 : 45214 : [&] {
133 [ + - ]: 45214 : CSerializedNetMsg serialized_net_msg;
134 [ + - ]: 45214 : serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
135 : 45214 : serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
136 [ + - ]: 45214 : connman.PushMessage(&random_node, std::move(serialized_net_msg));
137 : 45214 : },
138 : 50248 : [&] {
139 : 50248 : connman.RemoveAddedNode(random_string);
140 : 50248 : },
141 : 2504 : [&] {
142 : 2504 : connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
143 : 2504 : },
144 : 13551 : [&] {
145 : 13551 : connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
146 : 13551 : });
147 : : }
148 [ + - ]: 1713 : (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
149 [ + - ]: 1713 : (void)connman.GetExtraFullOutboundCount();
150 [ + - ]: 1713 : (void)connman.GetLocalServices();
151 [ + - - + ]: 1713 : assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
152 [ + - ]: 1713 : (void)connman.GetMaxOutboundTimeframe();
153 [ + - ]: 1713 : (void)connman.GetMaxOutboundTimeLeftInCycle();
154 [ + - ]: 1713 : (void)connman.GetNetworkActive();
155 : 1713 : std::vector<CNodeStats> stats;
156 [ + - ]: 1713 : connman.GetNodeStats(stats);
157 [ + - ]: 1713 : (void)connman.GetOutboundTargetBytesLeft();
158 [ + - ]: 1713 : (void)connman.GetTotalBytesRecv();
159 [ + - ]: 1713 : (void)connman.GetTotalBytesSent();
160 [ + - ]: 1713 : (void)connman.GetTryNewOutboundPeer();
161 : 1713 : (void)connman.GetUseAddrmanOutgoing();
162 [ + - ]: 1713 : (void)connman.ASMapHealthCheck();
163 : :
164 [ + - ]: 1713 : connman.ClearTestNodes();
165 : 1713 : }
|