mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-01 09:35:52 -05:00
Merge f919d919eb
into 85f96b01b7
This commit is contained in:
commit
a1e412fe1a
5 changed files with 115 additions and 51 deletions
|
@ -29,11 +29,28 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
|
|||
BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
|
||||
}
|
||||
|
||||
BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid")); // invalid size
|
||||
BOOST_CHECK( DecodeBase32("AWSX3VPP")); // valid
|
||||
|
||||
// Decoding strings with embedded NUL characters should fail
|
||||
BOOST_CHECK(!DecodeBase32("invalid\0"s)); // correct size, invalid due to \0
|
||||
BOOST_CHECK(DecodeBase32("AWSX3VPP"s)); // valid
|
||||
BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"s)); // correct size, invalid due to \0
|
||||
BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid"s)); // invalid size
|
||||
BOOST_CHECK(!DecodeBase32("invalid\0"sv)); // correct size, invalid due to \0
|
||||
BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"sv)); // correct size, invalid due to \0
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(base32_padding)
|
||||
{
|
||||
// Is valid without padding
|
||||
BOOST_CHECK_EQUAL(EncodeBase32("fooba"), "mzxw6ytb");
|
||||
|
||||
// Valid size
|
||||
BOOST_CHECK(!DecodeBase32("========"));
|
||||
BOOST_CHECK(!DecodeBase32("a======="));
|
||||
BOOST_CHECK( DecodeBase32("aa======"));
|
||||
BOOST_CHECK(!DecodeBase32("aaa====="));
|
||||
BOOST_CHECK( DecodeBase32("aaaa===="));
|
||||
BOOST_CHECK( DecodeBase32("aaaaa==="));
|
||||
BOOST_CHECK(!DecodeBase32("aaaaaa=="));
|
||||
BOOST_CHECK( DecodeBase32("aaaaaaa="));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|||
BOOST_CHECK(!DecodeBase58("invalid\0"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58("\0invalid"s, result, 100));
|
||||
|
||||
BOOST_CHECK(DecodeBase58("good"s, result, 100));
|
||||
BOOST_CHECK( DecodeBase58("good"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58("bad0IOl"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58("goodbad0IOl"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58("good\0bad0IOl"s, result, 100));
|
||||
|
@ -76,26 +76,10 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|||
constexpr auto expected{"971a55"_hex_u8};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
|
||||
BOOST_CHECK( DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oi"s, result, 100));
|
||||
BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh0IOl"s, result, 100));
|
||||
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()
|
||||
|
|
|
@ -36,11 +36,24 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
|
|||
BOOST_CHECK_EQUAL(EncodeBase64(in_s), out_exp);
|
||||
}
|
||||
|
||||
BOOST_CHECK(DecodeBase64("nQB/pZw=")); // valid
|
||||
|
||||
// Decoding strings with embedded NUL characters should fail
|
||||
BOOST_CHECK(!DecodeBase64("invalid\0"s));
|
||||
BOOST_CHECK(DecodeBase64("nQB/pZw="s));
|
||||
BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s));
|
||||
BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s));
|
||||
BOOST_CHECK(!DecodeBase64("invalid\0"sv)); // correct size, invalid due to \0
|
||||
BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"sv));
|
||||
BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"sv)); // invalid, padding only allowed at the end
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(base64_padding)
|
||||
{
|
||||
// Is valid without padding
|
||||
BOOST_CHECK_EQUAL(EncodeBase64("foobar"), "Zm9vYmFy");
|
||||
|
||||
// Valid size
|
||||
BOOST_CHECK(!DecodeBase64("===="));
|
||||
BOOST_CHECK(!DecodeBase64("a==="));
|
||||
BOOST_CHECK( DecodeBase64("YQ=="));
|
||||
BOOST_CHECK( DecodeBase64("YWE="));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -11,6 +11,13 @@
|
|||
["ecac89cad93923c02321", "EJDM8drfXA6uyA"],
|
||||
["10c8511e", "Rt5zm"],
|
||||
["00000000000000000000", "1111111111"],
|
||||
["00000000000000000000000000000000000000000000000000000000000000000000000000000000", "1111111111111111111111111111111111111111"],
|
||||
["00000000000000000000000000000000000000000000000000000000000000000000000000000001", "1111111111111111111111111111111111111112"],
|
||||
["0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ec39d04c37e71e5d591881f6", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111115TYzLYH1udmLdzCLM"],
|
||||
["000111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"],
|
||||
["000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1cWB5HCBdLjAuqGGReWE3R3CguuwSjw6RHn39s2yuDRTS5NsBgNiFpWgAnEx6VQi8csexkgYw3mdYrMHr8x9i7aEwP8kZ7vccXWqKDvGv3u1GxFKPuAkn8JCPPGDMf3vMMnbzm6Nh9zh1gcNsMvH3ZNLmP5fSG6DGbbi2tuwMWPthr4boWwCxf7ewSgNQeacyozhKDDQQ1qL5fQFUW52QKUZDZ5fw3KXNQJMcNTcaB723LchjeKun7MuGW5qyCBZYzA1KjofN1gYBV3NqyhQJ3Ns746GNuf9N2pQPmHz4xpnSrrfCvy6TVVz5d4PdrjeshsWQwpZsZGzvbdAdN8MKV5QsBDY"]
|
||||
["000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1cWB5HCBdLjAuqGGReWE3R3CguuwSjw6RHn39s2yuDRTS5NsBgNiFpWgAnEx6VQi8csexkgYw3mdYrMHr8x9i7aEwP8kZ7vccXWqKDvGv3u1GxFKPuAkn8JCPPGDMf3vMMnbzm6Nh9zh1gcNsMvH3ZNLmP5fSG6DGbbi2tuwMWPthr4boWwCxf7ewSgNQeacyozhKDDQQ1qL5fQFUW52QKUZDZ5fw3KXNQJMcNTcaB723LchjeKun7MuGW5qyCBZYzA1KjofN1gYBV3NqyhQJ3Ns746GNuf9N2pQPmHz4xpnSrrfCvy6TVVz5d4PdrjeshsWQwpZsZGzvbdAdN8MKV5QsBDY"],
|
||||
["271F359E", "zzzzy"],
|
||||
["271F359F", "zzzzz"],
|
||||
["271F35A0", "211111"],
|
||||
["271F35A1", "211112"]
|
||||
]
|
||||
|
|
|
@ -6,49 +6,92 @@
|
|||
|
||||
#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)};
|
||||
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 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, max_ret_len)) {
|
||||
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)};
|
||||
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 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, max_ret_len)) {
|
||||
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