2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2019-2022 The Bitcoin Core developers
|
2019-10-23 12:37:59 +00:00
|
|
|
// 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>
|
2020-03-11 12:05:52 +00:00
|
|
|
#include <psbt.h>
|
2025-02-20 16:48:28 +01:00
|
|
|
#include <span.h>
|
2024-08-29 19:03:40 +02:00
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
2019-10-23 12:37:59 +00:00
|
|
|
#include <util/strencodings.h>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <util/string.h>
|
2019-10-23 12:37:59 +00:00
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2024-08-29 19:03:40 +02:00
|
|
|
#include <ranges>
|
2019-10-23 12:37:59 +00:00
|
|
|
|
2023-12-06 15:13:39 -05:00
|
|
|
using util::TrimStringView;
|
|
|
|
|
|
2024-08-29 19:03:40 +02:00
|
|
|
FUZZ_TARGET(base58_encode_decode)
|
2019-10-23 12:37:59 +00:00
|
|
|
{
|
2025-02-20 16:48:28 +01:00
|
|
|
FuzzedDataProvider provider{buffer.data(), buffer.size()};
|
|
|
|
|
const auto random_string{provider.ConsumeRandomLengthString(100)};
|
|
|
|
|
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
|
2019-10-23 12:37:59 +00:00
|
|
|
|
2024-08-29 19:03:40 +02:00
|
|
|
// Decode/Encode roundtrip
|
2025-02-20 16:48:28 +01:00
|
|
|
if (std::vector<unsigned char> decoded; DecodeBase58(random_string, decoded, max_ret_len)) {
|
2024-08-29 19:03:40 +02:00
|
|
|
const auto encoded_string{EncodeBase58(decoded)};
|
|
|
|
|
assert(encoded_string == TrimStringView(random_string));
|
2025-02-20 16:48:28 +01:00
|
|
|
assert(decoded.empty() || !DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
|
2019-10-23 12:37:59 +00:00
|
|
|
}
|
2024-08-29 19:03:40 +02:00
|
|
|
// Encode/Decode roundtrip
|
2025-02-20 16:48:28 +01:00
|
|
|
const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
|
2024-08-29 19:03:40 +02:00
|
|
|
std::vector<unsigned char> roundtrip_decoded;
|
2025-02-20 16:48:28 +01:00
|
|
|
assert(DecodeBase58(encoded, roundtrip_decoded, random_string.size())
|
|
|
|
|
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
|
2024-08-29 19:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FUZZ_TARGET(base58check_encode_decode)
|
|
|
|
|
{
|
2025-02-20 16:48:28 +01:00
|
|
|
FuzzedDataProvider provider{buffer.data(), buffer.size()};
|
|
|
|
|
const auto random_string{provider.ConsumeRandomLengthString(100)};
|
|
|
|
|
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
|
2019-10-23 12:37:59 +00:00
|
|
|
|
2024-08-29 19:03:40 +02:00
|
|
|
// Decode/Encode roundtrip
|
2025-02-20 16:48:28 +01:00
|
|
|
if (std::vector<unsigned char> decoded; DecodeBase58Check(random_string, decoded, max_ret_len)) {
|
2024-08-29 19:03:40 +02:00
|
|
|
const auto encoded_string{EncodeBase58Check(decoded)};
|
|
|
|
|
assert(encoded_string == TrimStringView(random_string));
|
2025-02-20 16:48:28 +01:00
|
|
|
assert(decoded.empty() || !DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
|
2019-10-23 12:37:59 +00:00
|
|
|
}
|
2024-08-29 19:03:40 +02:00
|
|
|
// Encode/Decode roundtrip
|
2025-02-20 16:48:28 +01:00
|
|
|
const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
|
2024-08-29 19:03:40 +02:00
|
|
|
std::vector<unsigned char> roundtrip_decoded;
|
2025-02-20 16:48:28 +01:00
|
|
|
assert(DecodeBase58Check(encoded, roundtrip_decoded, random_string.size())
|
|
|
|
|
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
|
2024-08-29 19:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FUZZ_TARGET(base32_encode_decode)
|
|
|
|
|
{
|
|
|
|
|
const std::string random_string{buffer.begin(), buffer.end()};
|
2019-10-23 12:37:59 +00:00
|
|
|
|
2024-08-29 19:03:40 +02:00
|
|
|
// Decode/Encode roundtrip
|
|
|
|
|
if (auto result{DecodeBase32(random_string)}) {
|
|
|
|
|
const auto encoded_string{EncodeBase32(*result)};
|
|
|
|
|
assert(encoded_string == ToLower(TrimStringView(random_string)));
|
2019-10-23 12:37:59 +00:00
|
|
|
}
|
2024-08-29 19:03:40 +02:00
|
|
|
// 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()};
|
2019-10-23 12:37:59 +00:00
|
|
|
|
2024-08-29 19:03:40 +02:00
|
|
|
// Decode/Encode roundtrip
|
|
|
|
|
if (auto result{DecodeBase64(random_string)}) {
|
|
|
|
|
const auto encoded_string{EncodeBase64(*result)};
|
|
|
|
|
assert(encoded_string == TrimStringView(random_string));
|
2019-10-23 12:37:59 +00:00
|
|
|
}
|
2024-08-29 19:03:40 +02:00
|
|
|
// 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()};
|
2020-03-11 12:05:52 +00:00
|
|
|
|
|
|
|
|
PartiallySignedTransaction psbt;
|
|
|
|
|
std::string error;
|
2024-08-29 19:03:40 +02:00
|
|
|
assert(DecodeBase64PSBT(psbt, random_string, error) == error.empty());
|
2019-10-23 12:37:59 +00:00
|
|
|
}
|