Use the variable name _ for unused return values
[bitcoinplatinum.git] / src / test / base58_tests.cpp
blobee633249e9aa7a518301b954b4facfaede6362f2
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.
5 #include "base58.h"
7 #include "data/base58_encode_decode.json.h"
8 #include "data/base58_keys_invalid.json.h"
9 #include "data/base58_keys_valid.json.h"
11 #include "key.h"
12 #include "script/script.h"
13 #include "uint256.h"
14 #include "util.h"
15 #include "utilstrencodings.h"
16 #include "test/test_bitcoin.h"
18 #include <boost/test/unit_test.hpp>
20 #include <univalue.h>
22 extern UniValue read_json(const std::string& jsondata);
24 BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
26 // Goal: test low-level base58 encoding functionality
27 BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
29 UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
30 for (unsigned int idx = 0; idx < tests.size(); idx++) {
31 UniValue test = tests[idx];
32 std::string strTest = test.write();
33 if (test.size() < 2) // Allow for extra stuff (useful for comments)
35 BOOST_ERROR("Bad test: " << strTest);
36 continue;
38 std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
39 std::string base58string = test[1].get_str();
40 BOOST_CHECK_MESSAGE(
41 EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
42 strTest);
46 // Goal: test low-level base58 decoding functionality
47 BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
49 UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
50 std::vector<unsigned char> result;
52 for (unsigned int idx = 0; idx < tests.size(); idx++) {
53 UniValue test = tests[idx];
54 std::string strTest = test.write();
55 if (test.size() < 2) // Allow for extra stuff (useful for comments)
57 BOOST_ERROR("Bad test: " << strTest);
58 continue;
60 std::vector<unsigned char> expected = ParseHex(test[0].get_str());
61 std::string base58string = test[1].get_str();
62 BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result), strTest);
63 BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
66 BOOST_CHECK(!DecodeBase58("invalid", result));
68 // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
69 BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result));
70 BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result));
71 std::vector<unsigned char> expected = ParseHex("971a55");
72 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
75 // Visitor to check address type
76 class TestAddrTypeVisitor : public boost::static_visitor<bool>
78 private:
79 std::string exp_addrType;
80 public:
81 explicit TestAddrTypeVisitor(const std::string &_exp_addrType) : exp_addrType(_exp_addrType) { }
82 bool operator()(const CKeyID &id) const
84 return (exp_addrType == "pubkey");
86 bool operator()(const CScriptID &id) const
88 return (exp_addrType == "script");
90 bool operator()(const CNoDestination &no) const
92 return (exp_addrType == "none");
96 // Visitor to check address payload
97 class TestPayloadVisitor : public boost::static_visitor<bool>
99 private:
100 std::vector<unsigned char> exp_payload;
101 public:
102 explicit TestPayloadVisitor(std::vector<unsigned char> &_exp_payload) : exp_payload(_exp_payload) { }
103 bool operator()(const CKeyID &id) const
105 uint160 exp_key(exp_payload);
106 return exp_key == id;
108 bool operator()(const CScriptID &id) const
110 uint160 exp_key(exp_payload);
111 return exp_key == id;
113 bool operator()(const CNoDestination &no) const
115 return exp_payload.size() == 0;
119 // Goal: check that parsed keys match test payload
120 BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
122 UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
123 CBitcoinSecret secret;
124 CBitcoinAddress addr;
125 SelectParams(CBaseChainParams::MAIN);
127 for (unsigned int idx = 0; idx < tests.size(); idx++) {
128 UniValue test = tests[idx];
129 std::string strTest = test.write();
130 if (test.size() < 3) // Allow for extra stuff (useful for comments)
132 BOOST_ERROR("Bad test: " << strTest);
133 continue;
135 std::string exp_base58string = test[0].get_str();
136 std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
137 const UniValue &metadata = test[2].get_obj();
138 bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
139 bool isTestnet = find_value(metadata, "isTestnet").get_bool();
140 if (isTestnet)
141 SelectParams(CBaseChainParams::TESTNET);
142 else
143 SelectParams(CBaseChainParams::MAIN);
144 if(isPrivkey)
146 bool isCompressed = find_value(metadata, "isCompressed").get_bool();
147 // Must be valid private key
148 // Note: CBitcoinSecret::SetString tests isValid, whereas CBitcoinAddress does not!
149 BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest);
150 BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest);
151 CKey privkey = secret.GetKey();
152 BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
153 BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
155 // Private key must be invalid public key
156 addr.SetString(exp_base58string);
157 BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid privkey as pubkey:" + strTest);
159 else
161 std::string exp_addrType = find_value(metadata, "addrType").get_str(); // "script" or "pubkey"
162 // Must be valid public key
163 BOOST_CHECK_MESSAGE(addr.SetString(exp_base58string), "SetString:" + strTest);
164 BOOST_CHECK_MESSAGE(addr.IsValid(), "!IsValid:" + strTest);
165 BOOST_CHECK_MESSAGE(addr.IsScript() == (exp_addrType == "script"), "isScript mismatch" + strTest);
166 CTxDestination dest = addr.Get();
167 BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), dest), "addrType mismatch" + strTest);
169 // Public key must be invalid private key
170 secret.SetString(exp_base58string);
171 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
176 // Goal: check that generated keys match test vectors
177 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
179 UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
181 for (unsigned int idx = 0; idx < tests.size(); idx++) {
182 UniValue test = tests[idx];
183 std::string strTest = test.write();
184 if (test.size() < 3) // Allow for extra stuff (useful for comments)
186 BOOST_ERROR("Bad test: " << strTest);
187 continue;
189 std::string exp_base58string = test[0].get_str();
190 std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
191 const UniValue &metadata = test[2].get_obj();
192 bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
193 bool isTestnet = find_value(metadata, "isTestnet").get_bool();
194 if (isTestnet)
195 SelectParams(CBaseChainParams::TESTNET);
196 else
197 SelectParams(CBaseChainParams::MAIN);
198 if(isPrivkey)
200 bool isCompressed = find_value(metadata, "isCompressed").get_bool();
201 CKey key;
202 key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
203 assert(key.IsValid());
204 CBitcoinSecret secret;
205 secret.SetKey(key);
206 BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest);
208 else
210 std::string exp_addrType = find_value(metadata, "addrType").get_str();
211 CTxDestination dest;
212 if(exp_addrType == "pubkey")
214 dest = CKeyID(uint160(exp_payload));
216 else if(exp_addrType == "script")
218 dest = CScriptID(uint160(exp_payload));
220 else if(exp_addrType == "none")
222 dest = CNoDestination();
224 else
226 BOOST_ERROR("Bad addrtype: " << strTest);
227 continue;
229 CBitcoinAddress addrOut;
230 BOOST_CHECK_MESSAGE(addrOut.Set(dest), "encode dest: " + strTest);
231 BOOST_CHECK_MESSAGE(addrOut.ToString() == exp_base58string, "mismatch: " + strTest);
235 // Visiting a CNoDestination must fail
236 CBitcoinAddress dummyAddr;
237 CTxDestination nodest = CNoDestination();
238 BOOST_CHECK(!dummyAddr.Set(nodest));
240 SelectParams(CBaseChainParams::MAIN);
243 // Goal: check that base58 parsing code is robust against a variety of corrupted data
244 BOOST_AUTO_TEST_CASE(base58_keys_invalid)
246 UniValue tests = read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid + sizeof(json_tests::base58_keys_invalid))); // Negative testcases
247 CBitcoinSecret secret;
248 CBitcoinAddress addr;
250 for (unsigned int idx = 0; idx < tests.size(); idx++) {
251 UniValue test = tests[idx];
252 std::string strTest = test.write();
253 if (test.size() < 1) // Allow for extra stuff (useful for comments)
255 BOOST_ERROR("Bad test: " << strTest);
256 continue;
258 std::string exp_base58string = test[0].get_str();
260 // must be invalid as public and as private key
261 addr.SetString(exp_base58string);
262 BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid pubkey:" + strTest);
263 secret.SetString(exp_base58string);
264 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest);
269 BOOST_AUTO_TEST_SUITE_END()