forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_encode_decode.cpp
96 lines (82 loc) · 3.5 KB
/
base_encode_decode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2019-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/fuzz/fuzz.h>
#include <base58.h>
#include <psbt.h>
#include <span.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <cassert>
#include <string>
#include <vector>
#include <ranges>
using util::TrimStringView;
FUZZ_TARGET(base58_encode_decode)
{
FuzzedDataProvider provider{buffer.data(), buffer.size()};
const auto random_string{provider.ConsumeRandomLengthString(100)};
const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
if (std::vector<unsigned char> decoded; DecodeBase58(decode_input, decoded, max_ret_len)) {
const auto encoded_string{EncodeBase58(decoded)};
assert(encoded_string == TrimStringView(decode_input));
if (decoded.size() > 0) {
assert(max_ret_len > 0);
assert(decoded.size() <= static_cast<size_t>(max_ret_len));
assert(!DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
}
}
}
FUZZ_TARGET(base58check_encode_decode)
{
FuzzedDataProvider provider{buffer.data(), buffer.size()};
const auto random_string{provider.ConsumeRandomLengthString(100)};
const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
if (std::vector<unsigned char> decoded; DecodeBase58Check(decode_input, decoded, max_ret_len)) {
const auto encoded_string{EncodeBase58Check(decoded)};
assert(encoded_string == TrimStringView(decode_input));
if (decoded.size() > 0) {
assert(max_ret_len > 0);
assert(decoded.size() <= static_cast<size_t>(max_ret_len));
assert(!DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
}
}
}
FUZZ_TARGET(base32_encode_decode)
{
const std::string random_string{buffer.begin(), buffer.end()};
// Decode/Encode roundtrip
if (auto result{DecodeBase32(random_string)}) {
const auto encoded_string{EncodeBase32(*result)};
assert(encoded_string == ToLower(TrimStringView(random_string)));
}
// Encode/Decode roundtrip
const auto encoded{EncodeBase32(buffer)};
const auto decoded{DecodeBase32(encoded)};
assert(decoded && std::ranges::equal(*decoded, buffer));
}
FUZZ_TARGET(base64_encode_decode)
{
const std::string random_string{buffer.begin(), buffer.end()};
// Decode/Encode roundtrip
if (auto result{DecodeBase64(random_string)}) {
const auto encoded_string{EncodeBase64(*result)};
assert(encoded_string == TrimStringView(random_string));
}
// Encode/Decode roundtrip
const auto encoded{EncodeBase64(buffer)};
const auto decoded{DecodeBase64(encoded)};
assert(decoded && std::ranges::equal(*decoded, buffer));
}
FUZZ_TARGET(psbt_base64_decode)
{
const std::string random_string{buffer.begin(), buffer.end()};
PartiallySignedTransaction psbt;
std::string error;
assert(DecodeBase64PSBT(psbt, random_string, error) == error.empty());
}