mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
test: Fuzz Base32/Base58/Base64 roundtrip conversions
This commit introduces symmetric encode-decode roundtrips for all bases. Minor refactors were also included: • Split each base into a separate fuzz target. • Added symmetric encode-decode roundtrip tests for all bases. • Removed trim testing for encoded_string, as Base58 does not use whitespace padding. • Made comparisons stricter by removing unnecessary lowercase conversions for bases that have mixed-case alphabets. Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
This commit is contained in:
parent
5dd3a0d8a8
commit
635bc58f46
2 changed files with 65 additions and 40 deletions
|
@ -82,20 +82,4 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|||
BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh\0" "0IOl"s, result, 100));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
|
||||
{
|
||||
for (int n = 0; n < 1000; ++n) {
|
||||
unsigned int len = 1 + m_rng.randbits(8);
|
||||
unsigned int zeroes = m_rng.randbool() ? m_rng.randrange(len + 1) : 0;
|
||||
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), m_rng.randbytes(len - zeroes));
|
||||
auto encoded = EncodeBase58Check(data);
|
||||
std::vector<unsigned char> decoded;
|
||||
auto ok_too_small = DecodeBase58Check(encoded, decoded, m_rng.randrange(len));
|
||||
BOOST_CHECK(!ok_too_small);
|
||||
auto ok = DecodeBase58Check(encoded, decoded, len + m_rng.randrange(257 - len));
|
||||
BOOST_CHECK(ok);
|
||||
BOOST_CHECK(data == decoded);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -6,49 +6,90 @@
|
|||
|
||||
#include <base58.h>
|
||||
#include <psbt.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ranges>
|
||||
|
||||
using util::TrimString;
|
||||
using util::TrimStringView;
|
||||
|
||||
FUZZ_TARGET(base_encode_decode)
|
||||
FUZZ_TARGET(base58_encode_decode)
|
||||
{
|
||||
const std::string random_encoded_string(buffer.begin(), buffer.end());
|
||||
FuzzedDataProvider provider(buffer.data(), buffer.size());
|
||||
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
|
||||
|
||||
// Decode/Encode roundtrip
|
||||
std::vector<unsigned char> decoded;
|
||||
if (DecodeBase58(random_encoded_string, decoded, 100)) {
|
||||
const std::string encoded_string = EncodeBase58(decoded);
|
||||
assert(encoded_string == TrimStringView(encoded_string));
|
||||
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
|
||||
if (DecodeBase58(random_string, decoded, 100)) {
|
||||
const auto encoded_string{EncodeBase58(decoded)};
|
||||
assert(encoded_string == TrimStringView(random_string));
|
||||
assert(encoded_string.empty() || !DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
|
||||
}
|
||||
// Encode/Decode roundtrip
|
||||
const auto encoded{EncodeBase58(buffer)};
|
||||
std::vector<unsigned char> roundtrip_decoded;
|
||||
assert(DecodeBase58(encoded, roundtrip_decoded, buffer.size())
|
||||
&& std::ranges::equal(roundtrip_decoded, buffer));
|
||||
}
|
||||
|
||||
if (DecodeBase58Check(random_encoded_string, decoded, 100)) {
|
||||
const std::string encoded_string = EncodeBase58Check(decoded);
|
||||
assert(encoded_string == TrimString(encoded_string));
|
||||
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
|
||||
}
|
||||
FUZZ_TARGET(base58check_encode_decode)
|
||||
{
|
||||
FuzzedDataProvider provider(buffer.data(), buffer.size());
|
||||
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
|
||||
|
||||
auto result = DecodeBase32(random_encoded_string);
|
||||
if (result) {
|
||||
const std::string encoded_string = EncodeBase32(*result);
|
||||
assert(encoded_string == TrimStringView(encoded_string));
|
||||
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
|
||||
// Decode/Encode roundtrip
|
||||
std::vector<unsigned char> decoded;
|
||||
if (DecodeBase58Check(random_string, decoded, 100)) {
|
||||
const auto encoded_string{EncodeBase58Check(decoded)};
|
||||
assert(encoded_string == TrimStringView(random_string));
|
||||
assert(encoded_string.empty() || !DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
|
||||
}
|
||||
// Encode/Decode roundtrip
|
||||
const auto encoded{EncodeBase58Check(buffer)};
|
||||
std::vector<unsigned char> roundtrip_decoded;
|
||||
assert(DecodeBase58Check(encoded, roundtrip_decoded, buffer.size())
|
||||
&& std::ranges::equal(roundtrip_decoded, buffer));
|
||||
}
|
||||
|
||||
result = DecodeBase64(random_encoded_string);
|
||||
if (result) {
|
||||
const std::string encoded_string = EncodeBase64(*result);
|
||||
assert(encoded_string == TrimString(encoded_string));
|
||||
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
|
||||
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;
|
||||
(void)DecodeBase64PSBT(psbt, random_encoded_string, error);
|
||||
assert(DecodeBase64PSBT(psbt, random_string, error) == error.empty());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue