1 // Copyright (c) 2011-2016 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.
7 #include "data/base58_encode_decode.json.h"
8 #include "data/base58_keys_invalid.json.h"
9 #include "data/base58_keys_valid.json.h"
12 #include "script/script.h"
13 #include "test/test_bitcoin.h"
16 #include "utilstrencodings.h"
20 #include <boost/test/unit_test.hpp>
23 extern UniValue
read_json(const std::string
& jsondata
);
25 BOOST_FIXTURE_TEST_SUITE(base58_tests
, BasicTestingSetup
)
27 // Goal: test low-level base58 encoding functionality
28 BOOST_AUTO_TEST_CASE(base58_EncodeBase58
)
30 UniValue tests
= read_json(std::string(json_tests::base58_encode_decode
, json_tests::base58_encode_decode
+ sizeof(json_tests::base58_encode_decode
)));
31 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
32 UniValue test
= tests
[idx
];
33 std::string strTest
= test
.write();
34 if (test
.size() < 2) // Allow for extra stuff (useful for comments)
36 BOOST_ERROR("Bad test: " << strTest
);
39 std::vector
<unsigned char> sourcedata
= ParseHex(test
[0].get_str());
40 std::string base58string
= test
[1].get_str();
42 EncodeBase58(sourcedata
.data(), sourcedata
.data() + sourcedata
.size()) == base58string
,
47 // Goal: test low-level base58 decoding functionality
48 BOOST_AUTO_TEST_CASE(base58_DecodeBase58
)
50 UniValue tests
= read_json(std::string(json_tests::base58_encode_decode
, json_tests::base58_encode_decode
+ sizeof(json_tests::base58_encode_decode
)));
51 std::vector
<unsigned char> result
;
53 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
54 UniValue test
= tests
[idx
];
55 std::string strTest
= test
.write();
56 if (test
.size() < 2) // Allow for extra stuff (useful for comments)
58 BOOST_ERROR("Bad test: " << strTest
);
61 std::vector
<unsigned char> expected
= ParseHex(test
[0].get_str());
62 std::string base58string
= test
[1].get_str();
63 BOOST_CHECK_MESSAGE(DecodeBase58(base58string
, result
), strTest
);
64 BOOST_CHECK_MESSAGE(result
.size() == expected
.size() && std::equal(result
.begin(), result
.end(), expected
.begin()), strTest
);
67 BOOST_CHECK(!DecodeBase58("invalid", result
));
69 // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
70 BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result
));
71 BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result
));
72 std::vector
<unsigned char> expected
= ParseHex("971a55");
73 BOOST_CHECK_EQUAL_COLLECTIONS(result
.begin(), result
.end(), expected
.begin(), expected
.end());
76 // Goal: check that parsed keys match test payload
77 BOOST_AUTO_TEST_CASE(base58_keys_valid_parse
)
79 UniValue tests
= read_json(std::string(json_tests::base58_keys_valid
, json_tests::base58_keys_valid
+ sizeof(json_tests::base58_keys_valid
)));
80 CBitcoinSecret secret
;
81 CTxDestination destination
;
82 SelectParams(CBaseChainParams::MAIN
);
84 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
85 UniValue test
= tests
[idx
];
86 std::string strTest
= test
.write();
87 if (test
.size() < 3) { // Allow for extra stuff (useful for comments)
88 BOOST_ERROR("Bad test: " << strTest
);
91 std::string exp_base58string
= test
[0].get_str();
92 std::vector
<unsigned char> exp_payload
= ParseHex(test
[1].get_str());
93 const UniValue
&metadata
= test
[2].get_obj();
94 bool isPrivkey
= find_value(metadata
, "isPrivkey").get_bool();
95 SelectParams(find_value(metadata
, "chain").get_str());
96 bool try_case_flip
= find_value(metadata
, "tryCaseFlip").isNull() ? false : find_value(metadata
, "tryCaseFlip").get_bool();
98 bool isCompressed
= find_value(metadata
, "isCompressed").get_bool();
99 // Must be valid private key
100 BOOST_CHECK_MESSAGE(secret
.SetString(exp_base58string
), "!SetString:"+ strTest
);
101 BOOST_CHECK_MESSAGE(secret
.IsValid(), "!IsValid:" + strTest
);
102 CKey privkey
= secret
.GetKey();
103 BOOST_CHECK_MESSAGE(privkey
.IsCompressed() == isCompressed
, "compressed mismatch:" + strTest
);
104 BOOST_CHECK_MESSAGE(privkey
.size() == exp_payload
.size() && std::equal(privkey
.begin(), privkey
.end(), exp_payload
.begin()), "key mismatch:" + strTest
);
106 // Private key must be invalid public key
107 destination
= DecodeDestination(exp_base58string
);
108 BOOST_CHECK_MESSAGE(!IsValidDestination(destination
), "IsValid privkey as pubkey:" + strTest
);
110 // Must be valid public key
111 destination
= DecodeDestination(exp_base58string
);
112 CScript script
= GetScriptForDestination(destination
);
113 BOOST_CHECK_MESSAGE(IsValidDestination(destination
), "!IsValid:" + strTest
);
114 BOOST_CHECK_EQUAL(HexStr(script
), HexStr(exp_payload
));
116 // Try flipped case version
117 for (char& c
: exp_base58string
) {
118 if (c
>= 'a' && c
<= 'z') {
120 } else if (c
>= 'A' && c
<= 'Z') {
124 destination
= DecodeDestination(exp_base58string
);
125 BOOST_CHECK_MESSAGE(IsValidDestination(destination
) == try_case_flip
, "!IsValid case flipped:" + strTest
);
126 if (IsValidDestination(destination
)) {
127 script
= GetScriptForDestination(destination
);
128 BOOST_CHECK_EQUAL(HexStr(script
), HexStr(exp_payload
));
131 // Public key must be invalid private key
132 secret
.SetString(exp_base58string
);
133 BOOST_CHECK_MESSAGE(!secret
.IsValid(), "IsValid pubkey as privkey:" + strTest
);
138 // Goal: check that generated keys match test vectors
139 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen
)
141 UniValue tests
= read_json(std::string(json_tests::base58_keys_valid
, json_tests::base58_keys_valid
+ sizeof(json_tests::base58_keys_valid
)));
143 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
144 UniValue test
= tests
[idx
];
145 std::string strTest
= test
.write();
146 if (test
.size() < 3) // Allow for extra stuff (useful for comments)
148 BOOST_ERROR("Bad test: " << strTest
);
151 std::string exp_base58string
= test
[0].get_str();
152 std::vector
<unsigned char> exp_payload
= ParseHex(test
[1].get_str());
153 const UniValue
&metadata
= test
[2].get_obj();
154 bool isPrivkey
= find_value(metadata
, "isPrivkey").get_bool();
155 SelectParams(find_value(metadata
, "chain").get_str());
157 bool isCompressed
= find_value(metadata
, "isCompressed").get_bool();
159 key
.Set(exp_payload
.begin(), exp_payload
.end(), isCompressed
);
160 assert(key
.IsValid());
161 CBitcoinSecret secret
;
163 BOOST_CHECK_MESSAGE(secret
.ToString() == exp_base58string
, "result mismatch: " + strTest
);
166 CScript
exp_script(exp_payload
.begin(), exp_payload
.end());
167 ExtractDestination(exp_script
, dest
);
168 std::string address
= EncodeDestination(dest
);
170 BOOST_CHECK_EQUAL(address
, exp_base58string
);
174 SelectParams(CBaseChainParams::MAIN
);
178 // Goal: check that base58 parsing code is robust against a variety of corrupted data
179 BOOST_AUTO_TEST_CASE(base58_keys_invalid
)
181 UniValue tests
= read_json(std::string(json_tests::base58_keys_invalid
, json_tests::base58_keys_invalid
+ sizeof(json_tests::base58_keys_invalid
))); // Negative testcases
182 CBitcoinSecret secret
;
183 CTxDestination destination
;
185 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
186 UniValue test
= tests
[idx
];
187 std::string strTest
= test
.write();
188 if (test
.size() < 1) // Allow for extra stuff (useful for comments)
190 BOOST_ERROR("Bad test: " << strTest
);
193 std::string exp_base58string
= test
[0].get_str();
195 // must be invalid as public and as private key
196 for (auto chain
: { CBaseChainParams::MAIN
, CBaseChainParams::TESTNET
, CBaseChainParams::REGTEST
}) {
198 destination
= DecodeDestination(exp_base58string
);
199 BOOST_CHECK_MESSAGE(!IsValidDestination(destination
), "IsValid pubkey in mainnet:" + strTest
);
200 secret
.SetString(exp_base58string
);
201 BOOST_CHECK_MESSAGE(!secret
.IsValid(), "IsValid privkey in mainnet:" + strTest
);
207 BOOST_AUTO_TEST_SUITE_END()