Bitcoin Core 28.99.0
P2P Digital Currency
base58_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <test/data/base58_encode_decode.json.h>
6
7#include <base58.h>
8#include <test/util/json.h>
9#include <test/util/random.h>
11#include <util/strencodings.h>
12#include <util/vector.h>
13
14#include <univalue.h>
15
16#include <boost/test/unit_test.hpp>
17#include <string>
18
19using namespace std::literals;
20using namespace util::hex_literals;
21
23
24// Goal: test low-level base58 encoding functionality
25BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
26{
27 UniValue tests = read_json(json_tests::base58_encode_decode);
28 for (unsigned int idx = 0; idx < tests.size(); idx++) {
29 const UniValue& test = tests[idx];
30 std::string strTest = test.write();
31 if (test.size() < 2) // Allow for extra stuff (useful for comments)
32 {
33 BOOST_ERROR("Bad test: " << strTest);
34 continue;
35 }
36 std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
37 std::string base58string = test[1].get_str();
38 BOOST_CHECK_MESSAGE(
39 EncodeBase58(sourcedata) == base58string,
40 strTest);
41 }
42}
43
44// Goal: test low-level base58 decoding functionality
45BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
46{
47 UniValue tests = read_json(json_tests::base58_encode_decode);
48 std::vector<unsigned char> result;
49
50 for (unsigned int idx = 0; idx < tests.size(); idx++) {
51 const UniValue& test = tests[idx];
52 std::string strTest = test.write();
53 if (test.size() < 2) // Allow for extra stuff (useful for comments)
54 {
55 BOOST_ERROR("Bad test: " << strTest);
56 continue;
57 }
58 std::vector<unsigned char> expected = ParseHex(test[0].get_str());
59 std::string base58string = test[1].get_str();
60 BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
61 BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
62 }
63
64 BOOST_CHECK(!DecodeBase58("invalid"s, result, 100));
65 BOOST_CHECK(!DecodeBase58("invalid\0"s, result, 100));
66 BOOST_CHECK(!DecodeBase58("\0invalid"s, result, 100));
67
68 BOOST_CHECK(DecodeBase58("good"s, result, 100));
69 BOOST_CHECK(!DecodeBase58("bad0IOl"s, result, 100));
70 BOOST_CHECK(!DecodeBase58("goodbad0IOl"s, result, 100));
71 BOOST_CHECK(!DecodeBase58("good\0bad0IOl"s, result, 100));
72
73 // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
74 BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
75 BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
76 constexpr auto expected{"971a55"_hex_u8};
77 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
78
79 BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
80 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oi"s, result, 100));
81 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh0IOl"s, result, 100));
82 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh\0" "0IOl"s, result, 100));
83}
84
85BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
86{
87 for (int n = 0; n < 1000; ++n) {
88 unsigned int len = 1 + m_rng.randbits(8);
89 unsigned int zeroes = m_rng.randbool() ? m_rng.randrange(len + 1) : 0;
90 auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), m_rng.randbytes(len - zeroes));
91 auto encoded = EncodeBase58Check(data);
92 std::vector<unsigned char> decoded;
93 auto ok_too_small = DecodeBase58Check(encoded, decoded, m_rng.randrange(len));
94 BOOST_CHECK(!ok_too_small);
95 auto ok = DecodeBase58Check(encoded, decoded, len + m_rng.randrange(257 - len));
96 BOOST_CHECK(ok);
97 BOOST_CHECK(data == decoded);
98 }
99}
100
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:89
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:137
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:146
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:40
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
size_t size() const
Definition: univalue.h:71
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
UniValue read_json(std::string_view jsondata)
Definition: json.cpp:12
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:427
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
Basic testing setup.
Definition: setup_common.h:63
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34