Branch data Line data Source code
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 [ + - ]: 536 : FUZZ_TARGET(bech32_random_decode)
16 : : {
17 : 78 : auto limit = bech32::CharLimit::BECH32;
18 : 78 : FuzzedDataProvider fdp(buffer.data(), buffer.size());
19 : 78 : auto random_string = fdp.ConsumeRandomLengthString(limit + 1);
20 [ + - ]: 78 : auto decoded = bech32::Decode(random_string, limit);
21 : :
22 [ + + ]: 78 : if (decoded.hrp.empty()) {
23 [ - + ]: 53 : assert(decoded.encoding == bech32::Encoding::INVALID);
24 [ - + ]: 53 : assert(decoded.data.empty());
25 : : } else {
26 [ - + ]: 25 : assert(decoded.encoding != bech32::Encoding::INVALID);
27 [ + - ]: 25 : auto reencoded = bech32::Encode(decoded.encoding, decoded.hrp, decoded.data);
28 [ - + - + : 25 : assert(CaseInsensitiveEqual(random_string, reencoded));
+ - - + ]
29 : 25 : }
30 : 78 : }
31 : :
32 : : // https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki and https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
33 : 46 : std::string GenerateRandomHRP(FuzzedDataProvider& fdp)
34 : : {
35 : 46 : std::string hrp;
36 : 46 : size_t length = fdp.ConsumeIntegralInRange<size_t>(1, 83);
37 [ + + ]: 1330 : for (size_t i = 0; i < length; ++i) {
38 : : // Generate lowercase ASCII characters in ([33-126] - ['A'-'Z']) range
39 : 1284 : char c = fdp.ConsumeBool()
40 [ + + ]: 1284 : ? fdp.ConsumeIntegralInRange<char>(33, 'A' - 1)
41 : 997 : : fdp.ConsumeIntegralInRange<char>('Z' + 1, 126);
42 [ + - ]: 2568 : hrp += c;
43 : : }
44 : 46 : return hrp;
45 : 0 : }
46 : :
47 [ + - ]: 504 : FUZZ_TARGET(bech32_roundtrip)
48 : : {
49 : 46 : FuzzedDataProvider fdp(buffer.data(), buffer.size());
50 : 46 : auto hrp = GenerateRandomHRP(fdp);
51 : :
52 [ + - ]: 46 : auto input_chars = fdp.ConsumeBytes<unsigned char>(fdp.ConsumeIntegralInRange<size_t>(0, 82));
53 : 46 : std::vector<uint8_t> converted_input;
54 [ + - ]: 624 : ConvertBits<8, 5, true>([&](auto c) { converted_input.push_back(c); }, input_chars.begin(), input_chars.end());
55 : :
56 [ - + - + : 46 : auto size = converted_input.size() + hrp.length() + std::string({bech32::SEPARATOR}).size() + bech32::CHECKSUM_SIZE;
+ - - + ]
57 [ + + ]: 46 : if (size <= bech32::CharLimit::BECH32) {
58 [ + + ]: 126 : for (auto encoding: {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
59 [ + - ]: 84 : auto encoded = bech32::Encode(encoding, hrp, converted_input);
60 [ - + ]: 84 : assert(!encoded.empty());
61 : :
62 [ + - ]: 84 : const auto decoded = bech32::Decode(encoded);
63 [ - + ]: 84 : assert(decoded.encoding == encoding);
64 [ - + ]: 84 : assert(decoded.hrp == hrp);
65 [ - + ]: 84 : assert(decoded.data == converted_input);
66 : 84 : }
67 : : }
68 : 46 : }
|