Coverage Report

Created: 2026-09-15 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/test/fuzz/minisketch.cpp
Line
Count
Source
1
// Copyright (c) 2021-present 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 <minisketch.h>
6
7
#include <test/fuzz/FuzzedDataProvider.h>
8
#include <test/fuzz/fuzz.h>
9
#include <test/fuzz/util.h>
10
#include <util/check.h>
11
12
#include <limits>
13
#include <map>
14
#include <numeric>
15
#include <utility>
16
17
namespace {
18
19
Minisketch MakeFuzzMinisketch32(size_t capacity, uint32_t impl)
20
0
{
21
0
    return Assert(Minisketch(32, impl, capacity));
22
0
}
23
24
} // namespace
25
26
FUZZ_TARGET(minisketch)
27
0
{
28
0
    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
29
30
0
    const auto capacity{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 200)};
31
0
    const uint32_t impl{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, Minisketch::MaxImplementation())};
32
0
    if (!Minisketch::ImplementationSupported(32, impl)) return;
  Branch (32:9): [True: 0, False: 0]
33
34
0
    Minisketch sketch_a{MakeFuzzMinisketch32(capacity, impl)};
35
0
    Minisketch sketch_b{MakeFuzzMinisketch32(capacity, impl)};
36
0
    sketch_a.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
37
0
    sketch_b.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
38
39
    // Fill two sets and keep the difference in a map
40
0
    std::map<uint32_t, bool> diff;
41
0
    LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
42
0
        const auto entry{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, std::numeric_limits<uint32_t>::max() - 1)};
43
0
        const auto KeepDiff{[&] {
44
0
            bool& mut{diff[entry]};
45
0
            mut = !mut;
46
0
        }};
47
0
        CallOneOf(
48
0
            fuzzed_data_provider,
49
0
            [&] {
50
0
                sketch_a.Add(entry);
51
0
                KeepDiff();
52
0
            },
53
0
            [&] {
54
0
                sketch_b.Add(entry);
55
0
                KeepDiff();
56
0
            },
57
0
            [&] {
58
0
                sketch_a.Add(entry);
59
0
                sketch_b.Add(entry);
60
0
            });
61
0
    }
62
0
    const auto num_diff{std::accumulate(diff.begin(), diff.end(), size_t{0}, [](auto n, const auto& e) { return n + e.second; })};
63
64
0
    Minisketch sketch_ar{MakeFuzzMinisketch32(capacity, impl)};
65
0
    Minisketch sketch_br{MakeFuzzMinisketch32(capacity, impl)};
66
0
    sketch_ar.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
67
0
    sketch_br.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
68
69
0
    sketch_ar.Deserialize(sketch_a.Serialize());
70
0
    sketch_br.Deserialize(sketch_b.Serialize());
71
72
0
    Minisketch sketch_diff{std::move(fuzzed_data_provider.ConsumeBool() ? sketch_a : sketch_ar)};
  Branch (72:38): [True: 0, False: 0]
73
0
    sketch_diff.Merge(fuzzed_data_provider.ConsumeBool() ? sketch_b : sketch_br);
  Branch (73:23): [True: 0, False: 0]
74
75
0
    if (capacity >= num_diff) {
  Branch (75:9): [True: 0, False: 0]
76
0
        const auto max_elements{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(num_diff, capacity)};
77
0
        const auto dec{*Assert(sketch_diff.Decode(max_elements))};
78
0
        Assert(dec.size() == num_diff);
79
0
        for (auto d : dec) {
  Branch (79:21): [True: 0, False: 0]
80
0
            Assert(diff.at(d));
81
0
        }
82
0
    }
83
0
}