Coverage Report

Created: 2024-10-29 12:10

/root/bitcoin/src/test/fuzz/net.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 <chainparams.h>
6
#include <net.h>
7
#include <net_permissions.h>
8
#include <netaddress.h>
9
#include <protocol.h>
10
#include <random.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/net.h>
16
#include <test/util/setup_common.h>
17
#include <util/asmap.h>
18
#include <util/chaintype.h>
19
20
#include <cstdint>
21
#include <optional>
22
#include <string>
23
#include <vector>
24
25
void initialize_net()
26
0
{
27
0
    static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
28
0
}
29
30
FUZZ_TARGET(net, .init = initialize_net)
31
0
{
32
0
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33
0
    SetMockTime(ConsumeTime(fuzzed_data_provider));
34
0
    CNode node{ConsumeNode(fuzzed_data_provider)};
35
0
    node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
36
0
    if (const auto service_opt =
37
0
            ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
38
0
    {
39
0
        node.SetAddrLocal(*service_opt);
40
0
    }
41
0
    LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
42
0
        CallOneOf(
43
0
            fuzzed_data_provider,
44
0
            [&] {
45
0
                node.CloseSocketDisconnect();
46
0
            },
47
0
            [&] {
48
0
                CNodeStats stats;
49
0
                node.CopyStats(stats);
50
0
            },
51
0
            [&] {
52
0
                const CNode* add_ref_node = node.AddRef();
53
0
                assert(add_ref_node == &node);
54
0
            },
55
0
            [&] {
56
0
                if (node.GetRefCount() > 0) {
57
0
                    node.Release();
58
0
                }
59
0
            },
60
0
            [&] {
61
0
                const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
62
0
                bool complete;
63
0
                node.ReceiveMsgBytes(b, complete);
64
0
            });
65
0
    }
66
67
0
    (void)node.GetAddrLocal();
68
0
    (void)node.GetId();
69
0
    (void)node.GetLocalNonce();
70
0
    const int ref_count = node.GetRefCount();
71
0
    assert(ref_count >= 0);
72
0
    (void)node.GetCommonVersion();
73
74
0
    const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
75
0
    (void)node.HasPermission(net_permission_flags);
76
0
    (void)node.ConnectedThroughNetwork();
77
0
}
78
79
FUZZ_TARGET(local_address, .init = initialize_net)
80
0
{
81
0
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
82
0
    CService service{ConsumeService(fuzzed_data_provider)};
83
0
    CNode node{ConsumeNode(fuzzed_data_provider)};
84
0
    {
85
0
        LOCK(g_maplocalhost_mutex);
86
0
        mapLocalHost.clear();
87
0
    }
88
0
    LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
89
0
        CallOneOf(
90
0
            fuzzed_data_provider,
91
0
            [&] {
92
0
                service = ConsumeService(fuzzed_data_provider);
93
0
            },
94
0
            [&] {
95
0
                const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
96
0
                if (!added) return;
97
0
                assert(service.IsRoutable());
98
0
                assert(IsLocal(service));
99
0
                assert(SeenLocal(service));
100
0
            },
101
0
            [&] {
102
0
                (void)RemoveLocal(service);
103
0
            },
104
0
            [&] {
105
0
                (void)SeenLocal(service);
106
0
            },
107
0
            [&] {
108
0
                (void)IsLocal(service);
109
0
            },
110
0
            [&] {
111
0
                (void)GetLocalAddress(node);
112
0
            });
113
0
    }
114
0
}