Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / base58_tests.cpp
blob4829590c54be74970dbc50cf04d70c783632b252
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 CTxDestination destination;
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 BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest);
149 BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest);
150 CKey privkey = secret.GetKey();
151 BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
152 BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
154 // Private key must be invalid public key
155 destination = DecodeDestination(exp_base58string);
156 BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest);
158 else
160 std::string exp_addrType = find_value(metadata, "addrType").get_str(); // "script" or "pubkey"
161 // Must be valid public key
162 destination = DecodeDestination(exp_base58string);
163 BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest);
164 BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) != nullptr) == (exp_addrType == "script"), "isScript mismatch" + strTest);
165 BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), destination), "addrType mismatch" + strTest);
167 // Public key must be invalid private key
168 secret.SetString(exp_base58string);
169 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
174 // Goal: check that generated keys match test vectors
175 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
177 UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid)));
179 for (unsigned int idx = 0; idx < tests.size(); idx++) {
180 UniValue test = tests[idx];
181 std::string strTest = test.write();
182 if (test.size() < 3) // Allow for extra stuff (useful for comments)
184 BOOST_ERROR("Bad test: " << strTest);
185 continue;
187 std::string exp_base58string = test[0].get_str();
188 std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
189 const UniValue &metadata = test[2].get_obj();
190 bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
191 bool isTestnet = find_value(metadata, "isTestnet").get_bool();
192 if (isTestnet)
193 SelectParams(CBaseChainParams::TESTNET);
194 else
195 SelectParams(CBaseChainParams::MAIN);
196 if(isPrivkey)
198 bool isCompressed = find_value(metadata, "isCompressed").get_bool();
199 CKey key;
200 key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
201 assert(key.IsValid());
202 CBitcoinSecret secret;
203 secret.SetKey(key);
204 BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest);
206 else
208 std::string exp_addrType = find_value(metadata, "addrType").get_str();
209 CTxDestination dest;
210 if(exp_addrType == "pubkey")
212 dest = CKeyID(uint160(exp_payload));
214 else if(exp_addrType == "script")
216 dest = CScriptID(uint160(exp_payload));
218 else if(exp_addrType == "none")
220 dest = CNoDestination();
222 else
224 BOOST_ERROR("Bad addrtype: " << strTest);
225 continue;
227 std::string address = EncodeDestination(dest);
228 BOOST_CHECK_MESSAGE(address == exp_base58string, "mismatch: " + strTest);
232 SelectParams(CBaseChainParams::MAIN);
235 // Goal: check that base58 parsing code is robust against a variety of corrupted data
236 BOOST_AUTO_TEST_CASE(base58_keys_invalid)
238 UniValue tests = read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid + sizeof(json_tests::base58_keys_invalid))); // Negative testcases
239 CBitcoinSecret secret;
240 CTxDestination destination;
242 for (unsigned int idx = 0; idx < tests.size(); idx++) {
243 UniValue test = tests[idx];
244 std::string strTest = test.write();
245 if (test.size() < 1) // Allow for extra stuff (useful for comments)
247 BOOST_ERROR("Bad test: " << strTest);
248 continue;
250 std::string exp_base58string = test[0].get_str();
252 // must be invalid as public and as private key
253 destination = DecodeDestination(exp_base58string);
254 BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey:" + strTest);
255 secret.SetString(exp_base58string);
256 BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest);
261 BOOST_AUTO_TEST_SUITE_END()