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