1 // Copyright (c) 2011-2019 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/setup_common.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 
16 
17 extern UniValue read_json(const std::string& jsondata);
18 
BOOST_FIXTURE_TEST_SUITE(base58_tests,BasicTestingSetup)19 BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
20 
21 // Goal: test low-level base58 encoding functionality
22 BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
23 {
24     UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
25     for (unsigned int idx = 0; idx < tests.size(); idx++) {
26         UniValue test = tests[idx];
27         std::string strTest = test.write();
28         if (test.size() < 2) // Allow for extra stuff (useful for comments)
29         {
30             BOOST_ERROR("Bad test: " << strTest);
31             continue;
32         }
33         std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
34         std::string base58string = test[1].get_str();
35         BOOST_CHECK_MESSAGE(
36                     EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
37                     strTest);
38     }
39 }
40 
41 // Goal: test low-level base58 decoding functionality
BOOST_AUTO_TEST_CASE(base58_DecodeBase58)42 BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
43 {
44     UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
45     std::vector<unsigned char> result;
46 
47     for (unsigned int idx = 0; idx < tests.size(); idx++) {
48         UniValue test = tests[idx];
49         std::string strTest = test.write();
50         if (test.size() < 2) // Allow for extra stuff (useful for comments)
51         {
52             BOOST_ERROR("Bad test: " << strTest);
53             continue;
54         }
55         std::vector<unsigned char> expected = ParseHex(test[0].get_str());
56         std::string base58string = test[1].get_str();
57         BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
58         BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
59     }
60 
61     BOOST_CHECK(!DecodeBase58("invalid", result, 100));
62     BOOST_CHECK(!DecodeBase58(std::string("invalid"), result, 100));
63     BOOST_CHECK(!DecodeBase58(std::string("\0invalid", 8), result, 100));
64 
65     BOOST_CHECK(DecodeBase58(std::string("good", 4), result, 100));
66     BOOST_CHECK(!DecodeBase58(std::string("bad0IOl", 7), result, 100));
67     BOOST_CHECK(!DecodeBase58(std::string("goodbad0IOl", 11), result, 100));
68     BOOST_CHECK(!DecodeBase58(std::string("good\0bad0IOl", 12), result, 100));
69 
70     // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
71     BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
72     BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
73     std::vector<unsigned char> expected = ParseHex("971a55");
74     BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
75 
76     BOOST_CHECK(DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh", 21), result, 100));
77     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oi", 21), result, 100));
78     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh0IOl", 25), result, 100));
79     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh\00IOl", 26), result, 100));
80 }
81 
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)82 BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
83 {
84     for (int n = 0; n < 1000; ++n) {
85         unsigned int len = 1 + InsecureRandBits(8);
86         unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
87         auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
88         auto encoded = EncodeBase58Check(data);
89         std::vector<unsigned char> decoded;
90         auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
91         BOOST_CHECK(!ok_too_small);
92         auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
93         BOOST_CHECK(ok);
94         BOOST_CHECK(data == decoded);
95     }
96 }
97 
98 BOOST_AUTO_TEST_SUITE_END()
99