]> Git Repo - VerusCoin.git/blob - src/test/base58_tests.cpp
Merge pull request #4822
[VerusCoin.git] / src / test / base58_tests.cpp
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.
4
5 #include "base58.h"
6
7 #include "data/base58_encode_decode.json.h"
8 #include "data/base58_keys_invalid.json.h"
9 #include "data/base58_keys_valid.json.h"
10
11 #include "key.h"
12 #include "script/script.h"
13 #include "uint256.h"
14 #include "util.h"
15
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"
21
22 using namespace json_spirit;
23 extern Array read_json(const std::string& jsondata);
24
25 BOOST_AUTO_TEST_SUITE(base58_tests)
26
27 // Goal: test low-level base58 encoding functionality
28 BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
29 {
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)
32     {
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)
36         {
37             BOOST_ERROR("Bad test: " << strTest);
38             continue;
39         }
40         std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
41         std::string base58string = test[1].get_str();
42         BOOST_CHECK_MESSAGE(
43                     EncodeBase58(&sourcedata[0], &sourcedata[sourcedata.size()]) == base58string,
44                     strTest);
45     }
46 }
47
48 // Goal: test low-level base58 decoding functionality
49 BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
50 {
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;
53
54     BOOST_FOREACH(Value& tv, tests)
55     {
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)
59         {
60             BOOST_ERROR("Bad test: " << strTest);
61             continue;
62         }
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);
67     }
68
69     BOOST_CHECK(!DecodeBase58("invalid", result));
70
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());
76 }
77
78 // Visitor to check address type
79 class TestAddrTypeVisitor : public boost::static_visitor<bool>
80 {
81 private:
82     std::string exp_addrType;
83 public:
84     TestAddrTypeVisitor(const std::string &exp_addrType) : exp_addrType(exp_addrType) { }
85     bool operator()(const CKeyID &id) const
86     {
87         return (exp_addrType == "pubkey");
88     }
89     bool operator()(const CScriptID &id) const
90     {
91         return (exp_addrType == "script");
92     }
93     bool operator()(const CNoDestination &no) const
94     {
95         return (exp_addrType == "none");
96     }
97 };
98
99 // Visitor to check address payload
100 class TestPayloadVisitor : public boost::static_visitor<bool>
101 {
102 private:
103     std::vector<unsigned char> exp_payload;
104 public:
105     TestPayloadVisitor(std::vector<unsigned char> &exp_payload) : exp_payload(exp_payload) { }
106     bool operator()(const CKeyID &id) const
107     {
108         uint160 exp_key(exp_payload);
109         return exp_key == id;
110     }
111     bool operator()(const CScriptID &id) const
112     {
113         uint160 exp_key(exp_payload);
114         return exp_key == id;
115     }
116     bool operator()(const CNoDestination &no) const
117     {
118         return exp_payload.size() == 0;
119     }
120 };
121
122 // Goal: check that parsed keys match test payload
123 BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
124 {
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;
129
130     BOOST_FOREACH(Value& tv, tests)
131     {
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)
135         {
136             BOOST_ERROR("Bad test: " << strTest);
137             continue;
138         }
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();
144         if (isTestnet)
145             SelectParams(CBaseChainParams::TESTNET);
146         else
147             SelectParams(CBaseChainParams::MAIN);
148         if(isPrivkey)
149         {
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);
158
159             // Private key must be invalid public key
160             addr.SetString(exp_base58string);
161             BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid privkey as pubkey:" + strTest);
162         }
163         else
164         {
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);
172
173             // Public key must be invalid private key
174             secret.SetString(exp_base58string);
175             BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
176         }
177     }
178     SelectParams(CBaseChainParams::MAIN);
179 }
180
181 // Goal: check that generated keys match test vectors
182 BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
183 {
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)
187     {
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)
191         {
192             BOOST_ERROR("Bad test: " << strTest);
193             continue;
194         }
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();
200         if (isTestnet)
201             SelectParams(CBaseChainParams::TESTNET);
202         else
203             SelectParams(CBaseChainParams::MAIN);
204         if(isPrivkey)
205         {
206             bool isCompressed = find_value(metadata, "isCompressed").get_bool();
207             CKey key;
208             key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
209             assert(key.IsValid());
210             CBitcoinSecret secret;
211             secret.SetKey(key);
212             BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest);
213         }
214         else
215         {
216             std::string exp_addrType = find_value(metadata, "addrType").get_str();
217             CTxDestination dest;
218             if(exp_addrType == "pubkey")
219             {
220                 dest = CKeyID(uint160(exp_payload));
221             }
222             else if(exp_addrType == "script")
223             {
224                 dest = CScriptID(uint160(exp_payload));
225             }
226             else if(exp_addrType == "none")
227             {
228                 dest = CNoDestination();
229             }
230             else
231             {
232                 BOOST_ERROR("Bad addrtype: " << strTest);
233                 continue;
234             }
235             CBitcoinAddress addrOut;
236             BOOST_CHECK_MESSAGE(addrOut.Set(dest), "encode dest: " + strTest);
237             BOOST_CHECK_MESSAGE(addrOut.ToString() == exp_base58string, "mismatch: " + strTest);
238         }
239     }
240
241     // Visiting a CNoDestination must fail
242     CBitcoinAddress dummyAddr;
243     CTxDestination nodest = CNoDestination();
244     BOOST_CHECK(!dummyAddr.Set(nodest));
245
246     SelectParams(CBaseChainParams::MAIN);
247 }
248
249 // Goal: check that base58 parsing code is robust against a variety of corrupted data
250 BOOST_AUTO_TEST_CASE(base58_keys_invalid)
251 {
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;
256
257     BOOST_FOREACH(Value& tv, tests)
258     {
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)
262         {
263             BOOST_ERROR("Bad test: " << strTest);
264             continue;
265         }
266         std::string exp_base58string = test[0].get_str();
267
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);
273     }
274 }
275
276
277 BOOST_AUTO_TEST_SUITE_END()
278
This page took 0.042233 seconds and 4 git commands to generate.