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