Branch data Line data Source code
1 : : // Copyright (c) 2019-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 <test/fuzz/fuzz.h>
6 : :
7 : : #include <base58.h>
8 : : #include <psbt.h>
9 : : #include <span.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <util/strencodings.h>
12 : : #include <util/string.h>
13 : :
14 : : #include <cassert>
15 : : #include <string>
16 : : #include <vector>
17 : : #include <ranges>
18 : :
19 : : using util::TrimStringView;
20 : :
21 [ + - ]: 568 : FUZZ_TARGET(base58_encode_decode)
22 : : {
23 : 126 : FuzzedDataProvider provider{buffer.data(), buffer.size()};
24 : 126 : const auto random_string{provider.ConsumeRandomLengthString(100)};
25 : :
26 [ + - ]: 126 : const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
27 [ + + + - ]: 178 : const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
28 : 126 : const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
29 [ + - + + ]: 126 : if (std::vector<unsigned char> decoded; DecodeBase58(decode_input, decoded, max_ret_len)) {
30 [ + - ]: 88 : const auto encoded_string{EncodeBase58(decoded)};
31 [ + - - + ]: 88 : assert(encoded_string == TrimStringView(decode_input));
32 [ + + ]: 88 : if (decoded.size() > 0) {
33 [ - + ]: 69 : assert(max_ret_len > 0);
34 [ - + ]: 69 : assert(decoded.size() <= static_cast<size_t>(max_ret_len));
35 [ + - - + ]: 69 : assert(!DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
36 : : }
37 : 214 : }
38 : 126 : }
39 : :
40 [ + - ]: 572 : FUZZ_TARGET(base58check_encode_decode)
41 : : {
42 : 130 : FuzzedDataProvider provider{buffer.data(), buffer.size()};
43 : 130 : const auto random_string{provider.ConsumeRandomLengthString(100)};
44 : :
45 [ + - ]: 130 : const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
46 [ + + + - ]: 185 : const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
47 : 130 : const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
48 [ + - + + ]: 130 : if (std::vector<unsigned char> decoded; DecodeBase58Check(decode_input, decoded, max_ret_len)) {
49 [ + - ]: 41 : const auto encoded_string{EncodeBase58Check(decoded)};
50 [ + - - + ]: 41 : assert(encoded_string == TrimStringView(decode_input));
51 [ + + ]: 41 : if (decoded.size() > 0) {
52 [ - + ]: 40 : assert(max_ret_len > 0);
53 [ - + ]: 40 : assert(decoded.size() <= static_cast<size_t>(max_ret_len));
54 [ + - - + ]: 40 : assert(!DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
55 : : }
56 : 171 : }
57 : 130 : }
58 : :
59 [ + - ]: 489 : FUZZ_TARGET(base32_encode_decode)
60 : : {
61 [ + - ]: 47 : const std::string random_string{buffer.begin(), buffer.end()};
62 : :
63 : : // Decode/Encode roundtrip
64 [ + - + + ]: 47 : if (auto result{DecodeBase32(random_string)}) {
65 [ + - ]: 25 : const auto encoded_string{EncodeBase32(*result)};
66 [ + - + - : 50 : assert(encoded_string == ToLower(TrimStringView(random_string)));
- + ]
67 : 25 : }
68 : : // Encode/Decode roundtrip
69 [ + - ]: 47 : const auto encoded{EncodeBase32(buffer)};
70 [ + - ]: 47 : const auto decoded{DecodeBase32(encoded)};
71 [ + - - + ]: 47 : assert(decoded && std::ranges::equal(*decoded, buffer));
72 : 47 : }
73 : :
74 [ + - ]: 481 : FUZZ_TARGET(base64_encode_decode)
75 : : {
76 [ + - ]: 39 : const std::string random_string{buffer.begin(), buffer.end()};
77 : :
78 : : // Decode/Encode roundtrip
79 [ + - + + ]: 39 : if (auto result{DecodeBase64(random_string)}) {
80 [ + - ]: 18 : const auto encoded_string{EncodeBase64(*result)};
81 [ + - - + ]: 18 : assert(encoded_string == TrimStringView(random_string));
82 : 18 : }
83 : : // Encode/Decode roundtrip
84 [ + - ]: 39 : const auto encoded{EncodeBase64(buffer)};
85 [ + - ]: 39 : const auto decoded{DecodeBase64(encoded)};
86 [ + - - + ]: 39 : assert(decoded && std::ranges::equal(*decoded, buffer));
87 : 39 : }
88 : :
89 [ + - ]: 1878 : FUZZ_TARGET(psbt_base64_decode)
90 : : {
91 : 1436 : const std::string random_string{buffer.begin(), buffer.end()};
92 : :
93 : 1436 : PartiallySignedTransaction psbt;
94 [ + - ]: 1436 : std::string error;
95 [ + - ]: 1436 : const bool ok{DecodeBase64PSBT(psbt, random_string, error)};
96 [ - + ]: 1436 : assert(ok == error.empty());
97 : 1436 : }
|