Coverage Report

Created: 2026-06-12 16:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/test/fuzz/bech32.cpp
Line
Count
Source
1
// Copyright (c) 2019-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 <bech32.h>
6
#include <test/fuzz/fuzz.h>
7
#include <test/fuzz/FuzzedDataProvider.h>
8
#include <util/strencodings.h>
9
10
#include <cassert>
11
#include <cstdint>
12
#include <string>
13
#include <vector>
14
15
FUZZ_TARGET(bech32_random_decode)
16
0
{
17
0
    auto limit = bech32::CharLimit::BECH32;
18
0
    FuzzedDataProvider fdp(buffer.data(), buffer.size());
19
0
    auto random_string = fdp.ConsumeRandomLengthString(limit + 1);
20
0
    auto decoded = bech32::Decode(random_string, limit);
21
22
0
    if (decoded.hrp.empty()) {
  Branch (22:9): [True: 0, False: 0]
23
0
        assert(decoded.encoding == bech32::Encoding::INVALID);
  Branch (23:9): [True: 0, False: 0]
24
0
        assert(decoded.data.empty());
  Branch (24:9): [True: 0, False: 0]
25
0
    } else {
26
0
        assert(decoded.encoding != bech32::Encoding::INVALID);
  Branch (26:9): [True: 0, False: 0]
27
0
        auto reencoded = bech32::Encode(decoded.encoding, decoded.hrp, decoded.data);
28
0
        assert(CaseInsensitiveEqual(random_string, reencoded));
  Branch (28:9): [True: 0, False: 0]
29
0
    }
30
0
}
31
32
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki and https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
33
std::string GenerateRandomHRP(FuzzedDataProvider& fdp)
34
0
{
35
0
    std::string hrp;
36
0
    size_t length = fdp.ConsumeIntegralInRange<size_t>(1, 83);
37
0
    for (size_t i = 0; i < length; ++i) {
  Branch (37:24): [True: 0, False: 0]
38
        // Generate lowercase ASCII characters in ([33-126] - ['A'-'Z']) range
39
0
        char c = fdp.ConsumeBool()
  Branch (39:18): [True: 0, False: 0]
40
0
                 ? fdp.ConsumeIntegralInRange<char>(33, 'A' - 1)
41
0
                 : fdp.ConsumeIntegralInRange<char>('Z' + 1, 126);
42
0
        hrp += c;
43
0
    }
44
0
    return hrp;
45
0
}
46
47
FUZZ_TARGET(bech32_roundtrip)
48
0
{
49
0
    FuzzedDataProvider fdp(buffer.data(), buffer.size());
50
0
    auto hrp = GenerateRandomHRP(fdp);
51
52
0
    auto input_chars = fdp.ConsumeBytes<unsigned char>(fdp.ConsumeIntegralInRange<size_t>(0, 82));
53
0
    std::vector<uint8_t> converted_input;
54
0
    ConvertBits<8, 5, true>([&](auto c) { converted_input.push_back(c); }, input_chars.begin(), input_chars.end());
55
56
0
    auto size = converted_input.size() + hrp.length() + std::string({bech32::SEPARATOR}).size() + bech32::CHECKSUM_SIZE;
57
0
    if (size <= bech32::CharLimit::BECH32) {
  Branch (57:9): [True: 0, False: 0]
58
0
        for (auto encoding: {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
  Branch (58:27): [True: 0, False: 0]
59
0
            auto encoded = bech32::Encode(encoding, hrp, converted_input);
60
0
            assert(!encoded.empty());
  Branch (60:13): [True: 0, False: 0]
61
62
0
            const auto decoded = bech32::Decode(encoded);
63
0
            assert(decoded.encoding == encoding);
  Branch (63:13): [True: 0, False: 0]
64
0
            assert(decoded.hrp == hrp);
  Branch (64:13): [True: 0, False: 0]
65
0
            assert(decoded.data == converted_input);
  Branch (65:13): [True: 0, False: 0]
66
0
        }
67
0
    }
68
0
}