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