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