Coverage Report

Created: 2024-11-15 12:18

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