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 : 4821 : int32_t GetCheckRatio()
25 : : {
26 [ + - + - ]: 9642 : 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 [ + - ]: 3863 : FUZZ_TARGET(connman, .init = initialize_connman)
38 : : {
39 : 3407 : SeedRandomStateForTest(SeedRand::ZEROS);
40 : 3407 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
41 : 3407 : SetMockTime(ConsumeTime(fuzzed_data_provider));
42 : 3407 : auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
43 [ + - + - ]: 3407 : auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
44 [ + + ]: 3407 : if (fuzzed_data_provider.ConsumeBool()) {
45 : 2235 : const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
46 [ - + + - ]: 2235 : DataStream ds{serialized_data};
47 : 2235 : try {
48 [ + + ]: 4470 : ds >> *addr_man_ptr;
49 [ - + ]: 1414 : } catch (const std::ios_base::failure&) {
50 [ + - + - ]: 2828 : addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
51 : 1414 : }
52 : 2235 : }
53 : 3407 : AddrManDeterministic& addr_man{*addr_man_ptr};
54 : 3407 : ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
55 : : fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
56 : : addr_man,
57 : : netgroupman,
58 : : Params(),
59 [ + - + - ]: 3407 : fuzzed_data_provider.ConsumeBool()};
60 : :
61 : 3407 : const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
62 : 3407 : CConnman::Options options;
63 : 3407 : options.nMaxOutboundLimit = max_outbound_limit;
64 [ + - ]: 3407 : connman.Init(options);
65 : :
66 [ + - ]: 3407 : CNetAddr random_netaddr;
67 : 3407 : CNode random_node = ConsumeNode(fuzzed_data_provider);
68 [ + - ]: 3407 : CSubNet random_subnet;
69 : 3407 : std::string random_string;
70 : :
71 [ + + + + ]: 31209 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
72 : 27802 : CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
73 [ + - ]: 27802 : connman.AddTestNode(p2p_node);
74 : : }
75 : :
76 [ + + + + ]: 1041974 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
77 [ + - ]: 1038567 : CallOneOf(
78 : : fuzzed_data_provider,
79 : 4536 : [&] {
80 : 4536 : random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
81 : 4536 : },
82 : 124023 : [&] {
83 : 124023 : random_subnet = ConsumeSubNet(fuzzed_data_provider);
84 : 124023 : },
85 : 40575 : [&] {
86 : 40575 : random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
87 : 40575 : },
88 : 156750 : [&] {
89 : 627000 : connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
90 [ - + + - ]: 470250 : },
91 : 2143 : [&] {
92 : 2143 : connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
93 : 2143 : },
94 : 68316 : [&] {
95 : 68316 : connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
96 : 68316 : },
97 : 50758 : [&] {
98 : 50758 : connman.DisconnectNode(random_netaddr);
99 : 50758 : },
100 : 46142 : [&] {
101 : 46142 : connman.DisconnectNode(random_string);
102 : 46142 : },
103 : 9683 : [&] {
104 : 9683 : connman.DisconnectNode(random_subnet);
105 : 9683 : },
106 : 24312 : [&] {
107 [ + - ]: 24312 : connman.ForEachNode([](auto) {});
108 : 24312 : },
109 : 3654 : [&] {
110 [ + - ]: 3654 : (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
111 : 3654 : },
112 : 150728 : [&] {
113 : 150728 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
114 : 150728 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
115 : 150728 : auto filtered = fuzzed_data_provider.ConsumeBool();
116 : 150728 : (void)connman.GetAddressesUnsafe(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
117 : 150728 : },
118 : 10936 : [&] {
119 : 10936 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
120 : 10936 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
121 : 10936 : (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
122 : 10936 : },
123 : 2530 : [&] {
124 : 2530 : (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
125 : 2530 : },
126 : 4810 : [&] {
127 : 4810 : (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
128 : 4810 : },
129 : 76192 : [&] {
130 : 76192 : (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
131 : 76192 : },
132 : 116162 : [&] {
133 [ + - ]: 116162 : CSerializedNetMsg serialized_net_msg;
134 [ + - ]: 116162 : serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
135 : 116162 : serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
136 [ + - ]: 116162 : connman.PushMessage(&random_node, std::move(serialized_net_msg));
137 : 116162 : },
138 : 109284 : [&] {
139 : 109284 : connman.RemoveAddedNode(random_string);
140 : 109284 : },
141 : 5069 : [&] {
142 : 5069 : connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
143 : 5069 : },
144 : 31964 : [&] {
145 : 31964 : connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
146 : 31964 : });
147 : : }
148 [ + - ]: 3407 : (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
149 [ + - ]: 3407 : (void)connman.GetExtraFullOutboundCount();
150 [ + - ]: 3407 : (void)connman.GetLocalServices();
151 [ + - - + ]: 3407 : assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
152 [ + - ]: 3407 : (void)connman.GetMaxOutboundTimeframe();
153 [ + - ]: 3407 : (void)connman.GetMaxOutboundTimeLeftInCycle();
154 [ + - ]: 3407 : (void)connman.GetNetworkActive();
155 : 3407 : std::vector<CNodeStats> stats;
156 [ + - ]: 3407 : connman.GetNodeStats(stats);
157 [ + - ]: 3407 : (void)connman.GetOutboundTargetBytesLeft();
158 [ + - ]: 3407 : (void)connman.GetTotalBytesRecv();
159 [ + - ]: 3407 : (void)connman.GetTotalBytesSent();
160 [ + - ]: 3407 : (void)connman.GetTryNewOutboundPeer();
161 : 3407 : (void)connman.GetUseAddrmanOutgoing();
162 [ + - ]: 3407 : (void)connman.ASMapHealthCheck();
163 : :
164 [ + - ]: 3407 : connman.ClearTestNodes();
165 : 3407 : }
|