Changeset View
Changeset View
Standalone View
Standalone View
src/test/base58_tests.cpp
Show First 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), | ||||
expected.begin(), expected.end()); | expected.begin(), expected.end()); | ||||
} | } | ||||
// Goal: check that parsed keys match test payload | // Goal: check that parsed keys match test payload | ||||
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) { | BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) { | ||||
UniValue tests = read_json(std::string( | UniValue tests = read_json(std::string( | ||||
json_tests::base58_keys_valid, | json_tests::base58_keys_valid, | ||||
json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid))); | json_tests::base58_keys_valid + sizeof(json_tests::base58_keys_valid))); | ||||
CBitcoinSecret secret; | CKey privkey; | ||||
CTxDestination destination; | CTxDestination destination; | ||||
SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
for (unsigned int idx = 0; idx < tests.size(); idx++) { | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
UniValue test = tests[idx]; | UniValue test = tests[idx]; | ||||
std::string strTest = test.write(); | std::string strTest = test.write(); | ||||
// Allow for extra stuff (useful for comments) | // Allow for extra stuff (useful for comments) | ||||
if (test.size() < 3) { | if (test.size() < 3) { | ||||
BOOST_ERROR("Bad test: " << strTest); | BOOST_ERROR("Bad test: " << strTest); | ||||
continue; | continue; | ||||
} | } | ||||
std::string exp_base58string = test[0].get_str(); | std::string exp_base58string = test[0].get_str(); | ||||
std::vector<uint8_t> exp_payload = ParseHex(test[1].get_str()); | std::vector<uint8_t> exp_payload = ParseHex(test[1].get_str()); | ||||
const UniValue &metadata = test[2].get_obj(); | const UniValue &metadata = test[2].get_obj(); | ||||
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | ||||
SelectParams(find_value(metadata, "chain").get_str()); | SelectParams(find_value(metadata, "chain").get_str()); | ||||
if (isPrivkey) { | if (isPrivkey) { | ||||
bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | ||||
// Must be valid private key | // Must be valid private key | ||||
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), | privkey = DecodeSecret(exp_base58string); | ||||
"!SetString:" + strTest); | BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); | ||||
BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest); | |||||
CKey privkey = secret.GetKey(); | |||||
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, | BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, | ||||
"compressed mismatch:" + strTest); | "compressed mismatch:" + strTest); | ||||
BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && | BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && | ||||
std::equal(privkey.begin(), privkey.end(), | std::equal(privkey.begin(), privkey.end(), | ||||
exp_payload.begin()), | exp_payload.begin()), | ||||
"key mismatch:" + strTest); | "key mismatch:" + strTest); | ||||
// Private key must be invalid public key | // Private key must be invalid public key | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | ||||
"IsValid privkey as pubkey:" + strTest); | "IsValid privkey as pubkey:" + strTest); | ||||
} else { | } else { | ||||
// Must be valid public key | // Must be valid public key | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
CScript script = GetScriptForDestination(destination); | CScript script = GetScriptForDestination(destination); | ||||
BOOST_CHECK_MESSAGE(IsValidDestination(destination), | BOOST_CHECK_MESSAGE(IsValidDestination(destination), | ||||
"!IsValid:" + strTest); | "!IsValid:" + strTest); | ||||
BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | ||||
// Public key must be invalid private key | // Public key must be invalid private key | ||||
secret.SetString(exp_base58string); | privkey = DecodeSecret(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!secret.IsValid(), | BOOST_CHECK_MESSAGE(!privkey.IsValid(), | ||||
"IsValid pubkey as privkey:" + strTest); | "IsValid pubkey as privkey:" + strTest); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Goal: check that generated keys match test vectors | // Goal: check that generated keys match test vectors | ||||
BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) { | BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) { | ||||
UniValue tests = read_json(std::string( | UniValue tests = read_json(std::string( | ||||
Show All 13 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
const UniValue &metadata = test[2].get_obj(); | const UniValue &metadata = test[2].get_obj(); | ||||
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | ||||
SelectParams(find_value(metadata, "chain").get_str()); | SelectParams(find_value(metadata, "chain").get_str()); | ||||
if (isPrivkey) { | if (isPrivkey) { | ||||
bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | ||||
CKey key; | CKey key; | ||||
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed); | key.Set(exp_payload.begin(), exp_payload.end(), isCompressed); | ||||
assert(key.IsValid()); | assert(key.IsValid()); | ||||
CBitcoinSecret secret; | BOOST_CHECK_MESSAGE(EncodeSecret(key) == exp_base58string, | ||||
secret.SetKey(key); | |||||
BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, | |||||
"result mismatch: " + strTest); | "result mismatch: " + strTest); | ||||
} else { | } else { | ||||
CTxDestination dest; | CTxDestination dest; | ||||
CScript exp_script(exp_payload.begin(), exp_payload.end()); | CScript exp_script(exp_payload.begin(), exp_payload.end()); | ||||
ExtractDestination(exp_script, dest); | ExtractDestination(exp_script, dest); | ||||
std::string address = EncodeLegacyAddr(dest, Params()); | std::string address = EncodeLegacyAddr(dest, Params()); | ||||
BOOST_CHECK_EQUAL(address, exp_base58string); | BOOST_CHECK_EQUAL(address, exp_base58string); | ||||
} | } | ||||
} | } | ||||
SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
} | } | ||||
// Goal: check that base58 parsing code is robust against a variety of corrupted | // Goal: check that base58 parsing code is robust against a variety of corrupted | ||||
// data | // data | ||||
BOOST_AUTO_TEST_CASE(base58_keys_invalid) { | BOOST_AUTO_TEST_CASE(base58_keys_invalid) { | ||||
// Negative testcases | // Negative testcases | ||||
UniValue tests = | UniValue tests = | ||||
read_json(std::string(json_tests::base58_keys_invalid, | read_json(std::string(json_tests::base58_keys_invalid, | ||||
json_tests::base58_keys_invalid + | json_tests::base58_keys_invalid + | ||||
sizeof(json_tests::base58_keys_invalid))); | sizeof(json_tests::base58_keys_invalid))); | ||||
CBitcoinSecret secret; | CKey privkey; | ||||
CTxDestination destination; | CTxDestination destination; | ||||
for (unsigned int idx = 0; idx < tests.size(); idx++) { | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
UniValue test = tests[idx]; | UniValue test = tests[idx]; | ||||
std::string strTest = test.write(); | std::string strTest = test.write(); | ||||
// Allow for extra stuff (useful for comments) | // Allow for extra stuff (useful for comments) | ||||
if (test.size() < 1) { | if (test.size() < 1) { | ||||
BOOST_ERROR("Bad test: " << strTest); | BOOST_ERROR("Bad test: " << strTest); | ||||
continue; | continue; | ||||
} | } | ||||
std::string exp_base58string = test[0].get_str(); | std::string exp_base58string = test[0].get_str(); | ||||
// must be invalid as public and as private key | // must be invalid as public and as private key | ||||
for (auto chain : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, | for (auto chain : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, | ||||
CBaseChainParams::REGTEST}) { | CBaseChainParams::REGTEST}) { | ||||
SelectParams(chain); | SelectParams(chain); | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | ||||
"IsValid pubkey:" + strTest); | "IsValid pubkey:" + strTest); | ||||
secret.SetString(exp_base58string); | privkey = DecodeSecret(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!secret.IsValid(), | BOOST_CHECK_MESSAGE(!privkey.IsValid(), | ||||
"IsValid privkey:" + strTest); | "IsValid privkey:" + strTest); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |