diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2019-11-18 15:16:50 -0800 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2019-11-19 15:38:27 -0800 |
commit | 2bcf1fc444d5c4b8efa879e54e7b6134b7e6b986 (patch) | |
tree | 333cb13784c3cd48d5ade305246212ddbd590386 /src/test/base58_tests.cpp | |
parent | b4a1da9ef8e4b673c290d5b882527e627ae1b43a (diff) |
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.
Diffstat (limited to 'src/test/base58_tests.cpp')
-rw-r--r-- | src/test/base58_tests.cpp | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp index 52301f799a..5d53088cc6 100644 --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -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() |