Bitcoin Core 22.99.0
P2P Digital Currency
base58_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 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
6
7#include <base58.h>
9#include <util/strencodings.h>
10#include <util/vector.h>
11
12#include <univalue.h>
13
14#include <boost/test/unit_test.hpp>
15#include <string>
16
17using namespace std::literals;
18
19UniValue read_json(const std::string& jsondata);
20
22
23// Goal: test low-level base58 encoding functionality
24BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
25{
27 for (unsigned int idx = 0; idx < tests.size(); idx++) {
28 UniValue test = tests[idx];
29 std::string strTest = test.write();
30 if (test.size() < 2) // Allow for extra stuff (useful for comments)
31 {
32 BOOST_ERROR("Bad test: " << strTest);
33 continue;
34 }
35 std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
36 std::string base58string = test[1].get_str();
37 BOOST_CHECK_MESSAGE(
38 EncodeBase58(sourcedata) == base58string,
39 strTest);
40 }
41}
42
43// Goal: test low-level base58 decoding functionality
44BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
45{
47 std::vector<unsigned char> result;
48
49 for (unsigned int idx = 0; idx < tests.size(); idx++) {
50 UniValue test = tests[idx];
51 std::string strTest = test.write();
52 if (test.size() < 2) // Allow for extra stuff (useful for comments)
53 {
54 BOOST_ERROR("Bad test: " << strTest);
55 continue;
56 }
57 std::vector<unsigned char> expected = ParseHex(test[0].get_str());
58 std::string base58string = test[1].get_str();
59 BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
60 BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
61 }
62
63 BOOST_CHECK(!DecodeBase58("invalid"s, result, 100));
64 BOOST_CHECK(!DecodeBase58("invalid\0"s, result, 100));
65 BOOST_CHECK(!DecodeBase58("\0invalid"s, result, 100));
66
67 BOOST_CHECK(DecodeBase58("good"s, result, 100));
68 BOOST_CHECK(!DecodeBase58("bad0IOl"s, result, 100));
69 BOOST_CHECK(!DecodeBase58("goodbad0IOl"s, result, 100));
70 BOOST_CHECK(!DecodeBase58("good\0bad0IOl"s, result, 100));
71
72 // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
73 BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
74 BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
75 std::vector<unsigned char> expected = ParseHex("971a55");
76 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
77
78 BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
79 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oi"s, result, 100));
80 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh0IOl"s, result, 100));
81 BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh\0" "0IOl"s, result, 100));
82}
83
84BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
85{
86 for (int n = 0; n < 1000; ++n) {
87 unsigned int len = 1 + InsecureRandBits(8);
88 unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
89 auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
90 auto encoded = EncodeBase58Check(data);
91 std::vector<unsigned char> decoded;
92 auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
93 BOOST_CHECK(!ok_too_small);
94 auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
95 BOOST_CHECK(ok);
96 BOOST_CHECK(data == decoded);
97 }
98}
99
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:87
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:135
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:144
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:38
UniValue read_json(const std::string &jsondata)
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:626
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:66
BOOST_AUTO_TEST_SUITE_END()
static unsigned const char base58_encode_decode[]
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:68
static uint64_t InsecureRandBits(int bits)
Definition: setup_common.h:67
static bool InsecureRandBool()
Definition: setup_common.h:69
std::vector< unsigned char > ParseHex(const char *psz)
Basic testing setup.
Definition: setup_common.h:76
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:31