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 : 6160 : int32_t GetCheckRatio()
25 : : {
26 [ + - + - ]: 12320 : 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 [ + - + - ]: 2 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
34 : 1 : g_setup = testing_setup.get();
35 [ + - ]: 2 : }
36 : :
37 [ + - ]: 4528 : FUZZ_TARGET(connman, .init = initialize_connman)
38 : : {
39 : 4096 : SeedRandomStateForTest(SeedRand::ZEROS);
40 : 4096 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
41 : 4096 : SetMockTime(ConsumeTime(fuzzed_data_provider));
42 : 4096 : auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
43 [ + - + - ]: 4096 : auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
44 [ + + ]: 4096 : if (fuzzed_data_provider.ConsumeBool()) {
45 : 2614 : const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
46 [ + - ]: 2614 : DataStream ds{serialized_data};
47 : 2614 : try {
48 [ + + ]: 5228 : ds >> *addr_man_ptr;
49 [ - + ]: 2064 : } catch (const std::ios_base::failure&) {
50 [ + - + - ]: 4128 : addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
51 : 2064 : }
52 : 2614 : }
53 : 4096 : AddrManDeterministic& addr_man{*addr_man_ptr};
54 : 4096 : ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
55 : : fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
56 : : addr_man,
57 : : netgroupman,
58 : : Params(),
59 [ + - + - ]: 4096 : fuzzed_data_provider.ConsumeBool()};
60 : :
61 : 4096 : const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
62 : 4096 : CConnman::Options options;
63 : 4096 : options.nMaxOutboundLimit = max_outbound_limit;
64 [ + - ]: 4096 : connman.Init(options);
65 : :
66 [ + - ]: 4096 : CNetAddr random_netaddr;
67 : 4096 : CNode random_node = ConsumeNode(fuzzed_data_provider);
68 [ + - ]: 4096 : CSubNet random_subnet;
69 : 4096 : std::string random_string;
70 : :
71 [ + + + + ]: 38959 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
72 : 34863 : CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
73 [ + - ]: 34863 : connman.AddTestNode(p2p_node);
74 : : }
75 : :
76 [ + + + + ]: 1234323 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
77 [ + - ]: 1230227 : CallOneOf(
78 : : fuzzed_data_provider,
79 : 4045 : [&] {
80 : 4045 : random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
81 : 4045 : },
82 : 155897 : [&] {
83 : 155897 : random_subnet = ConsumeSubNet(fuzzed_data_provider);
84 : 155897 : },
85 : 48663 : [&] {
86 : 48663 : random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
87 : 48663 : },
88 : 324865 : [&] {
89 : 324865 : connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
90 [ + - ]: 974595 : },
91 : 1827 : [&] {
92 : 1827 : connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
93 : 1827 : },
94 : 94731 : [&] {
95 : 94731 : connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
96 : 94731 : },
97 : 51518 : [&] {
98 : 51518 : connman.DisconnectNode(random_netaddr);
99 : 51518 : },
100 : 33728 : [&] {
101 : 33728 : connman.DisconnectNode(random_string);
102 : 33728 : },
103 : 13263 : [&] {
104 : 13263 : connman.DisconnectNode(random_subnet);
105 : 13263 : },
106 : 31047 : [&] {
107 [ + - ]: 31047 : connman.ForEachNode([](auto) {});
108 : 31047 : },
109 : 3882 : [&] {
110 [ + - ]: 3882 : (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
111 : 3882 : },
112 : 81431 : [&] {
113 : 81431 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
114 : 81431 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
115 : 81431 : auto filtered = fuzzed_data_provider.ConsumeBool();
116 : 81431 : (void)connman.GetAddresses(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
117 : 81431 : },
118 : 9673 : [&] {
119 : 9673 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
120 : 9673 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
121 : 9673 : (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
122 : 9673 : },
123 : 4791 : [&] {
124 : 4791 : (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
125 : 4791 : },
126 : 6066 : [&] {
127 : 6066 : (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
128 : 6066 : },
129 : 107619 : [&] {
130 : 107619 : (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
131 : 107619 : },
132 : 86246 : [&] {
133 [ + - ]: 86246 : CSerializedNetMsg serialized_net_msg;
134 [ + - ]: 86246 : serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
135 : 86246 : serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
136 [ + - ]: 86246 : connman.PushMessage(&random_node, std::move(serialized_net_msg));
137 : 86246 : },
138 : 131458 : [&] {
139 : 131458 : connman.RemoveAddedNode(random_string);
140 : 131458 : },
141 : 8110 : [&] {
142 : 8110 : connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
143 : 8110 : },
144 : 31367 : [&] {
145 : 31367 : connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
146 : 31367 : });
147 : : }
148 [ + - ]: 4096 : (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
149 [ + - ]: 4096 : (void)connman.GetExtraFullOutboundCount();
150 [ + - ]: 4096 : (void)connman.GetLocalServices();
151 [ + - - + ]: 4096 : assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
152 [ + - ]: 4096 : (void)connman.GetMaxOutboundTimeframe();
153 [ + - ]: 4096 : (void)connman.GetMaxOutboundTimeLeftInCycle();
154 [ + - ]: 4096 : (void)connman.GetNetworkActive();
155 : 4096 : std::vector<CNodeStats> stats;
156 [ + - ]: 4096 : connman.GetNodeStats(stats);
157 [ + - ]: 4096 : (void)connman.GetOutboundTargetBytesLeft();
158 [ + - ]: 4096 : (void)connman.GetTotalBytesRecv();
159 [ + - ]: 4096 : (void)connman.GetTotalBytesSent();
160 [ + - ]: 4096 : (void)connman.GetTryNewOutboundPeer();
161 : 4096 : (void)connman.GetUseAddrmanOutgoing();
162 [ + - ]: 4096 : (void)connman.ASMapHealthCheck();
163 : :
164 [ + - ]: 4096 : connman.ClearTestNodes();
165 : 4096 : }
|