1 // Copyright (c) 2011-2014 The Bitcoin Core developers
2 // Distributed under the MIT/X11 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"
16 #include <boost/foreach.hpp>
17 #include <boost/test/unit_test.hpp>
18 #include "json/json_spirit_reader_template.h"
19 #include "json/json_spirit_utils.h"
20 #include "json/json_spirit_writer_template.h"
22 using namespace json_spirit;
23 extern Array read_json(const std::string& jsondata);
25 BOOST_AUTO_TEST_SUITE(base58_tests)
27 // Goal: test low-level base58 encoding functionality
28 BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
30 Array tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
31 BOOST_FOREACH(Value& tv, tests)
33 Array test = tv.get_array();
34 std::string strTest = write_string(tv, false);
35 if (test.size() < 2) // Allow for extra stuff (useful for comments)
37 BOOST_ERROR("Bad test: " << strTest);
40 std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
41 std::string base58string = test[1].get_str();
43 EncodeBase58(&sourcedata[0], &sourcedata[sourcedata.size()]) == base58string,
48 // Goal: test low-level base58 decoding functionality
49 BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
51 Array tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
52 std::vector<unsigned char> result;
54 BOOST_FOREACH(Value& tv, tests)
56 Array test = tv.get_array();
57 std::string strTest = write_string(tv, false);
58 if (test.size() < 2) // Allow for extra stuff (useful for comments)
60 BOOST_ERROR("Bad test: " << strTest);
63 std::vector<unsigned char> expected = ParseHex(test[0].get_str());
64 std::string base58string = test[1].get_str();
65 BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result), strTest);
66 BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
69 BOOST_CHECK(!DecodeBase58("invalid", result));
71 // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
72 BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result));
73 BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result));
74 std::vector<unsigned char> expected = ParseHex("971a55");
75 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
78 // Visitor to check address type
79 class TestAddrTypeVisitor : public boost::static_visitor<bool>
82 std::string exp_addrType;
84 TestAddrTypeVisitor(const std::string &exp_addrType) : exp_addrType(exp_addrType) { }
85 bool operator()(const CKeyID &id) const
87 return (exp_addrType == "pubkey");
89 bool operator()(const CScriptID &id) const
91 return (exp_addrType == "script");
93 bool operator()(const CNoDestination &no) const
95 return (exp_addrType == "none");
99 // Visitor to check address payload
100 class TestPayloadVisitor : public boost::static_visitor<bool>
103 std::vector<unsigned char> exp_payload;
105 TestPayloadVisitor(std::vector<unsigned char> &exp_payload) : exp_payload(exp_payload) { }
106 bool operator()(const CKeyID &id) const
108 uint160 exp_key(exp_payload);
109 return exp_key == id;
111 bool operator()(const CScriptID &id) const
113 uint160 exp_key(exp_payload);
114 return exp_key == id;
116 bool operator()(const CNoDestination &no) const
118 return exp_payload.size() == 0;
122 // Goal: check that parsed keys match test payload
123 BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
125 Array tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
126 std::vector<unsigned char> result;
127 CBitcoinSecret secret;
128 CBitcoinAddress addr;
130 BOOST_FOREACH(Value& tv, tests)
132 Array test = tv.get_array();
133 std::string strTest = write_string(tv, false);
134 if (test.size() < 3) // Allow for extra stuff (useful for comments)
136 BOOST_ERROR("Bad test: " << strTest);
139 std::string exp_base58string = test[0].get_str();
140 std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
141 const Object &metadata = test[2].get_obj();
142 bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
143 bool isTestnet = find_value(metadata, "isTestnet").get_bool();
145 SelectParams(CBaseChainParams::TESTNET);
147 SelectParams(CBaseChainParams::MAIN);
150 bool isCompressed = find_value(metadata, "isCompressed").get_bool();
151 // Must be valid private key
152 // Note: CBitcoinSecret::SetString tests isValid, whereas CBitcoinAddress does not!
153 BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest);
154 BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest);
155 CKey privkey = secret.GetKey();
156 BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
157 BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
159 // Private key must be invalid public key
160 addr.SetString(exp_base58string);
161 BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid privkey as pubkey:" + strTest);
165 std::string exp_addrType = find_value(metadata, "addrType").get_str(); // "script" or "pubkey"
166 // Must be valid public key
167 BOOST_CHECK_MESSAGE(addr.SetString(exp_base58string), "SetString:" + strTest);
168 BOOST_CHECK_MESSAGE(addr.IsValid(), "!IsValid:" + strTest);
169 BOOST_CHECK_MESSAGE(addr.IsScript() == (exp_addrType == "script"), "isScript mismatch" + strTest);
170 CTxDestination dest = addr.Get();
171 BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), dest), "addrType mismatch" + strTest);
173 // Public key must be invalid private key
174 secret.SetString(exp_base58string);
175 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
178 SelectParams(CBaseChainParams::MAIN);
181 // Goal: check that generated keys match test vectors
182 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
184 Array tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
185 std::vector<unsigned char> result;
186 BOOST_FOREACH(Value& tv, tests)
188 Array test = tv.get_array();
189 std::string strTest = write_string(tv, false);
190 if (test.size() < 3) // Allow for extra stuff (useful for comments)
192 BOOST_ERROR("Bad test: " << strTest);
195 std::string exp_base58string = test[0].get_str();
196 std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
197 const Object &metadata = test[2].get_obj();
198 bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
199 bool isTestnet = find_value(metadata, "isTestnet").get_bool();
201 SelectParams(CBaseChainParams::TESTNET);
203 SelectParams(CBaseChainParams::MAIN);
206 bool isCompressed = find_value(metadata, "isCompressed").get_bool();
208 key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
209 assert(key.IsValid());
210 CBitcoinSecret secret;
212 BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest);
216 std::string exp_addrType = find_value(metadata, "addrType").get_str();
218 if(exp_addrType == "pubkey")
220 dest = CKeyID(uint160(exp_payload));
222 else if(exp_addrType == "script")
224 dest = CScriptID(uint160(exp_payload));
226 else if(exp_addrType == "none")
228 dest = CNoDestination();
232 BOOST_ERROR("Bad addrtype: " << strTest);
235 CBitcoinAddress addrOut;
236 BOOST_CHECK_MESSAGE(addrOut.Set(dest), "encode dest: " + strTest);
237 BOOST_CHECK_MESSAGE(addrOut.ToString() == exp_base58string, "mismatch: " + strTest);
241 // Visiting a CNoDestination must fail
242 CBitcoinAddress dummyAddr;
243 CTxDestination nodest = CNoDestination();
244 BOOST_CHECK(!dummyAddr.Set(nodest));
246 SelectParams(CBaseChainParams::MAIN);
249 // Goal: check that base58 parsing code is robust against a variety of corrupted data
250 BOOST_AUTO_TEST_CASE(base58_keys_invalid)
252 Array tests = read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid + sizeof(json_tests::base58_keys_invalid))); // Negative testcases
253 std::vector<unsigned char> result;
254 CBitcoinSecret secret;
255 CBitcoinAddress addr;
257 BOOST_FOREACH(Value& tv, tests)
259 Array test = tv.get_array();
260 std::string strTest = write_string(tv, false);
261 if (test.size() < 1) // Allow for extra stuff (useful for comments)
263 BOOST_ERROR("Bad test: " << strTest);
266 std::string exp_base58string = test[0].get_str();
268 // must be invalid as public and as private key
269 addr.SetString(exp_base58string);
270 BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid pubkey:" + strTest);
271 secret.SetString(exp_base58string);
272 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest);
277 BOOST_AUTO_TEST_SUITE_END()