|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
5 |
| -#include <base58.h> |
6 |
| - |
7 | 5 | #include <test/data/base58_encode_decode.json.h>
|
8 |
| -#include <test/data/base58_keys_invalid.json.h> |
9 |
| -#include <test/data/base58_keys_valid.json.h> |
10 | 6 |
|
11 |
| -#include <key.h> |
12 |
| -#include <key_io.h> |
13 |
| -#include <script/script.h> |
| 7 | +#include <base58.h> |
14 | 8 | #include <test/test_bitcoin.h>
|
15 |
| -#include <uint256.h> |
16 |
| -#include <util.h> |
17 | 9 | #include <utilstrencodings.h>
|
18 | 10 |
|
19 | 11 | #include <univalue.h>
|
@@ -74,132 +66,4 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
74 | 66 | BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
75 | 67 | }
|
76 | 68 |
|
77 |
| -// Goal: check that parsed keys match test payload |
78 |
| -BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
79 |
| -{ |
80 |
| - UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid))); |
81 |
| - CKey privkey; |
82 |
| - CTxDestination destination; |
83 |
| - SelectParams(CBaseChainParams::MAIN); |
84 |
| - |
85 |
| - for (unsigned int idx = 0; idx < tests.size(); idx++) { |
86 |
| - UniValue test = tests[idx]; |
87 |
| - std::string strTest = test.write(); |
88 |
| - if (test.size() < 3) { // Allow for extra stuff (useful for comments) |
89 |
| - BOOST_ERROR("Bad test: " << strTest); |
90 |
| - continue; |
91 |
| - } |
92 |
| - std::string exp_base58string = test[0].get_str(); |
93 |
| - std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str()); |
94 |
| - const UniValue &metadata = test[2].get_obj(); |
95 |
| - bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); |
96 |
| - SelectParams(find_value(metadata, "chain").get_str()); |
97 |
| - bool try_case_flip = find_value(metadata, "tryCaseFlip").isNull() ? false : find_value(metadata, "tryCaseFlip").get_bool(); |
98 |
| - if (isPrivkey) { |
99 |
| - bool isCompressed = find_value(metadata, "isCompressed").get_bool(); |
100 |
| - // Must be valid private key |
101 |
| - privkey = DecodeSecret(exp_base58string); |
102 |
| - BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); |
103 |
| - BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest); |
104 |
| - BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest); |
105 |
| - |
106 |
| - // Private key must be invalid public key |
107 |
| - destination = DecodeDestination(exp_base58string); |
108 |
| - BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest); |
109 |
| - } else { |
110 |
| - // Must be valid public key |
111 |
| - destination = DecodeDestination(exp_base58string); |
112 |
| - CScript script = GetScriptForDestination(destination); |
113 |
| - BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest); |
114 |
| - BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); |
115 |
| - |
116 |
| - // Try flipped case version |
117 |
| - for (char& c : exp_base58string) { |
118 |
| - if (c >= 'a' && c <= 'z') { |
119 |
| - c = (c - 'a') + 'A'; |
120 |
| - } else if (c >= 'A' && c <= 'Z') { |
121 |
| - c = (c - 'A') + 'a'; |
122 |
| - } |
123 |
| - } |
124 |
| - destination = DecodeDestination(exp_base58string); |
125 |
| - BOOST_CHECK_MESSAGE(IsValidDestination(destination) == try_case_flip, "!IsValid case flipped:" + strTest); |
126 |
| - if (IsValidDestination(destination)) { |
127 |
| - script = GetScriptForDestination(destination); |
128 |
| - BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); |
129 |
| - } |
130 |
| - |
131 |
| - // Public key must be invalid private key |
132 |
| - privkey = DecodeSecret(exp_base58string); |
133 |
| - BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid pubkey as privkey:" + strTest); |
134 |
| - } |
135 |
| - } |
136 |
| -} |
137 |
| - |
138 |
| -// Goal: check that generated keys match test vectors |
139 |
| -BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) |
140 |
| -{ |
141 |
| - UniValue tests = read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid))); |
142 |
| - |
143 |
| - for (unsigned int idx = 0; idx < tests.size(); idx++) { |
144 |
| - UniValue test = tests[idx]; |
145 |
| - std::string strTest = test.write(); |
146 |
| - if (test.size() < 3) // Allow for extra stuff (useful for comments) |
147 |
| - { |
148 |
| - BOOST_ERROR("Bad test: " << strTest); |
149 |
| - continue; |
150 |
| - } |
151 |
| - std::string exp_base58string = test[0].get_str(); |
152 |
| - std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str()); |
153 |
| - const UniValue &metadata = test[2].get_obj(); |
154 |
| - bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); |
155 |
| - SelectParams(find_value(metadata, "chain").get_str()); |
156 |
| - if (isPrivkey) { |
157 |
| - bool isCompressed = find_value(metadata, "isCompressed").get_bool(); |
158 |
| - CKey key; |
159 |
| - key.Set(exp_payload.begin(), exp_payload.end(), isCompressed); |
160 |
| - assert(key.IsValid()); |
161 |
| - BOOST_CHECK_MESSAGE(EncodeSecret(key) == exp_base58string, "result mismatch: " + strTest); |
162 |
| - } else { |
163 |
| - CTxDestination dest; |
164 |
| - CScript exp_script(exp_payload.begin(), exp_payload.end()); |
165 |
| - ExtractDestination(exp_script, dest); |
166 |
| - std::string address = EncodeDestination(dest); |
167 |
| - |
168 |
| - BOOST_CHECK_EQUAL(address, exp_base58string); |
169 |
| - } |
170 |
| - } |
171 |
| - |
172 |
| - SelectParams(CBaseChainParams::MAIN); |
173 |
| -} |
174 |
| - |
175 |
| - |
176 |
| -// Goal: check that base58 parsing code is robust against a variety of corrupted data |
177 |
| -BOOST_AUTO_TEST_CASE(base58_keys_invalid) |
178 |
| -{ |
179 |
| - UniValue tests = read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid + sizeof(json_tests::base58_keys_invalid))); // Negative testcases |
180 |
| - CKey privkey; |
181 |
| - CTxDestination destination; |
182 |
| - |
183 |
| - for (unsigned int idx = 0; idx < tests.size(); idx++) { |
184 |
| - UniValue test = tests[idx]; |
185 |
| - std::string strTest = test.write(); |
186 |
| - if (test.size() < 1) // Allow for extra stuff (useful for comments) |
187 |
| - { |
188 |
| - BOOST_ERROR("Bad test: " << strTest); |
189 |
| - continue; |
190 |
| - } |
191 |
| - std::string exp_base58string = test[0].get_str(); |
192 |
| - |
193 |
| - // must be invalid as public and as private key |
194 |
| - for (auto chain : { CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::REGTEST }) { |
195 |
| - SelectParams(chain); |
196 |
| - destination = DecodeDestination(exp_base58string); |
197 |
| - BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey in mainnet:" + strTest); |
198 |
| - privkey = DecodeSecret(exp_base58string); |
199 |
| - BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid privkey in mainnet:" + strTest); |
200 |
| - } |
201 |
| - } |
202 |
| -} |
203 |
| - |
204 |
| - |
205 | 69 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments