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"
15 #include "utilstrencodings.h"
16 #include "test/test_bitcoin.h"
18 #include <boost/foreach.hpp>
19 #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 // Visitor to check address type
77 class TestAddrTypeVisitor
: public boost::static_visitor
<bool>
80 std::string exp_addrType
;
82 TestAddrTypeVisitor(const std::string
&_exp_addrType
) : exp_addrType(_exp_addrType
) { }
83 bool operator()(const CKeyID
&id
) const
85 return (exp_addrType
== "pubkey");
87 bool operator()(const CScriptID
&id
) const
89 return (exp_addrType
== "script");
91 bool operator()(const CNoDestination
&no
) const
93 return (exp_addrType
== "none");
97 // Visitor to check address payload
98 class TestPayloadVisitor
: public boost::static_visitor
<bool>
101 std::vector
<unsigned char> exp_payload
;
103 TestPayloadVisitor(std::vector
<unsigned char> &_exp_payload
) : exp_payload(_exp_payload
) { }
104 bool operator()(const CKeyID
&id
) const
106 uint160
exp_key(exp_payload
);
107 return exp_key
== id
;
109 bool operator()(const CScriptID
&id
) const
111 uint160
exp_key(exp_payload
);
112 return exp_key
== id
;
114 bool operator()(const CNoDestination
&no
) const
116 return exp_payload
.size() == 0;
120 // Goal: check that parsed keys match test payload
121 BOOST_AUTO_TEST_CASE(base58_keys_valid_parse
)
123 UniValue tests
= read_json(std::string(json_tests::base58_keys_valid
, json_tests::base58_keys_valid
+ sizeof(json_tests::base58_keys_valid
)));
124 CBitcoinSecret secret
;
125 CBitcoinAddress addr
;
126 SelectParams(CBaseChainParams::MAIN
);
128 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
129 UniValue test
= tests
[idx
];
130 std::string strTest
= test
.write();
131 if (test
.size() < 3) // Allow for extra stuff (useful for comments)
133 BOOST_ERROR("Bad test: " << strTest
);
136 std::string exp_base58string
= test
[0].get_str();
137 std::vector
<unsigned char> exp_payload
= ParseHex(test
[1].get_str());
138 const UniValue
&metadata
= test
[2].get_obj();
139 bool isPrivkey
= find_value(metadata
, "isPrivkey").get_bool();
140 bool isTestnet
= find_value(metadata
, "isTestnet").get_bool();
142 SelectParams(CBaseChainParams::TESTNET
);
144 SelectParams(CBaseChainParams::MAIN
);
147 bool isCompressed
= find_value(metadata
, "isCompressed").get_bool();
148 // Must be valid private key
149 // Note: CBitcoinSecret::SetString tests isValid, whereas CBitcoinAddress does not!
150 BOOST_CHECK_MESSAGE(secret
.SetString(exp_base58string
), "!SetString:"+ strTest
);
151 BOOST_CHECK_MESSAGE(secret
.IsValid(), "!IsValid:" + strTest
);
152 CKey privkey
= secret
.GetKey();
153 BOOST_CHECK_MESSAGE(privkey
.IsCompressed() == isCompressed
, "compressed mismatch:" + strTest
);
154 BOOST_CHECK_MESSAGE(privkey
.size() == exp_payload
.size() && std::equal(privkey
.begin(), privkey
.end(), exp_payload
.begin()), "key mismatch:" + strTest
);
156 // Private key must be invalid public key
157 addr
.SetString(exp_base58string
);
158 BOOST_CHECK_MESSAGE(!addr
.IsValid(), "IsValid privkey as pubkey:" + strTest
);
162 std::string exp_addrType
= find_value(metadata
, "addrType").get_str(); // "script" or "pubkey"
163 // Must be valid public key
164 BOOST_CHECK_MESSAGE(addr
.SetString(exp_base58string
), "SetString:" + strTest
);
165 BOOST_CHECK_MESSAGE(addr
.IsValid(), "!IsValid:" + strTest
);
166 BOOST_CHECK_MESSAGE(addr
.IsScript() == (exp_addrType
== "script"), "isScript mismatch" + strTest
);
167 CTxDestination dest
= addr
.Get();
168 BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType
), dest
), "addrType mismatch" + strTest
);
170 // Public key must be invalid private key
171 secret
.SetString(exp_base58string
);
172 BOOST_CHECK_MESSAGE(!secret
.IsValid(), "IsValid pubkey as privkey:" + strTest
);
177 // Goal: check that generated keys match test vectors
178 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen
)
180 UniValue tests
= read_json(std::string(json_tests::base58_keys_valid
, json_tests::base58_keys_valid
+ sizeof(json_tests::base58_keys_valid
)));
182 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
183 UniValue test
= tests
[idx
];
184 std::string strTest
= test
.write();
185 if (test
.size() < 3) // Allow for extra stuff (useful for comments)
187 BOOST_ERROR("Bad test: " << strTest
);
190 std::string exp_base58string
= test
[0].get_str();
191 std::vector
<unsigned char> exp_payload
= ParseHex(test
[1].get_str());
192 const UniValue
&metadata
= test
[2].get_obj();
193 bool isPrivkey
= find_value(metadata
, "isPrivkey").get_bool();
194 bool isTestnet
= find_value(metadata
, "isTestnet").get_bool();
196 SelectParams(CBaseChainParams::TESTNET
);
198 SelectParams(CBaseChainParams::MAIN
);
201 bool isCompressed
= find_value(metadata
, "isCompressed").get_bool();
203 key
.Set(exp_payload
.begin(), exp_payload
.end(), isCompressed
);
204 assert(key
.IsValid());
205 CBitcoinSecret secret
;
207 BOOST_CHECK_MESSAGE(secret
.ToString() == exp_base58string
, "result mismatch: " + strTest
);
211 std::string exp_addrType
= find_value(metadata
, "addrType").get_str();
213 if(exp_addrType
== "pubkey")
215 dest
= CKeyID(uint160(exp_payload
));
217 else if(exp_addrType
== "script")
219 dest
= CScriptID(uint160(exp_payload
));
221 else if(exp_addrType
== "none")
223 dest
= CNoDestination();
227 BOOST_ERROR("Bad addrtype: " << strTest
);
230 CBitcoinAddress addrOut
;
231 BOOST_CHECK_MESSAGE(addrOut
.Set(dest
), "encode dest: " + strTest
);
232 BOOST_CHECK_MESSAGE(addrOut
.ToString() == exp_base58string
, "mismatch: " + strTest
);
236 // Visiting a CNoDestination must fail
237 CBitcoinAddress dummyAddr
;
238 CTxDestination nodest
= CNoDestination();
239 BOOST_CHECK(!dummyAddr
.Set(nodest
));
241 SelectParams(CBaseChainParams::MAIN
);
244 // Goal: check that base58 parsing code is robust against a variety of corrupted data
245 BOOST_AUTO_TEST_CASE(base58_keys_invalid
)
247 UniValue tests
= read_json(std::string(json_tests::base58_keys_invalid
, json_tests::base58_keys_invalid
+ sizeof(json_tests::base58_keys_invalid
))); // Negative testcases
248 CBitcoinSecret secret
;
249 CBitcoinAddress addr
;
251 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
252 UniValue test
= tests
[idx
];
253 std::string strTest
= test
.write();
254 if (test
.size() < 1) // Allow for extra stuff (useful for comments)
256 BOOST_ERROR("Bad test: " << strTest
);
259 std::string exp_base58string
= test
[0].get_str();
261 // must be invalid as public and as private key
262 addr
.SetString(exp_base58string
);
263 BOOST_CHECK_MESSAGE(!addr
.IsValid(), "IsValid pubkey:" + strTest
);
264 secret
.SetString(exp_base58string
);
265 BOOST_CHECK_MESSAGE(!secret
.IsValid(), "IsValid privkey:" + strTest
);
270 BOOST_AUTO_TEST_SUITE_END()