mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Pass a maximum output length to DecodeBase58 and DecodeBase58Check
Also remove a needless loop in DecodeBase58 to prune zeroes in the base256 output of the conversion. The number of zeroes is implied by keeping track explicitly of the length during the loop.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <base58.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/vector.h>
|
||||
|
||||
#include <univalue.h>
|
||||
|
||||
@@ -66,4 +67,20 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
|
||||
{
|
||||
for (int n = 0; n < 1000; ++n) {
|
||||
unsigned int len = 1 + InsecureRandBits(8);
|
||||
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
|
||||
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
|
||||
auto encoded = EncodeBase58Check(data);
|
||||
std::vector<unsigned char> decoded;
|
||||
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
|
||||
BOOST_CHECK(!ok_too_small);
|
||||
auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
|
||||
BOOST_CHECK(ok);
|
||||
BOOST_CHECK(data == decoded);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user