Changeset View
Changeset View
Standalone View
Standalone View
src/test/base58_tests.cpp
Show First 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
// 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; | CBitcoinSecret secret; | ||||
CBitcoinAddress addr; | 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(); | ||||
bool isTestnet = find_value(metadata, "isTestnet").get_bool(); | bool isTestnet = find_value(metadata, "isTestnet").get_bool(); | ||||
if (isTestnet) | if (isTestnet) | ||||
SelectParams(CBaseChainParams::TESTNET); | SelectParams(CBaseChainParams::TESTNET); | ||||
else | else | ||||
SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
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 | ||||
// Note: CBitcoinSecret::SetString tests isValid, whereas | |||||
// CBitcoinAddress does not! | |||||
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), | BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), | ||||
"!SetString:" + strTest); | "!SetString:" + strTest); | ||||
BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest); | BOOST_CHECK_MESSAGE(secret.IsValid(), "!IsValid:" + strTest); | ||||
CKey privkey = secret.GetKey(); | 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 | ||||
addr.SetString(exp_base58string); | destination = DecodeDestination(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!addr.IsValid(), | BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | ||||
"IsValid privkey as pubkey:" + strTest); | "IsValid privkey as pubkey:" + strTest); | ||||
} else { | } else { | ||||
std::string exp_addrType = find_value(metadata, "addrType") | std::string exp_addrType = find_value(metadata, "addrType") | ||||
.get_str(); // "script" or "pubkey" | .get_str(); // "script" or "pubkey" | ||||
// Must be valid public key | // Must be valid public key | ||||
BOOST_CHECK_MESSAGE(addr.SetString(exp_base58string), | destination = DecodeDestination(exp_base58string); | ||||
"SetString:" + strTest); | BOOST_CHECK_MESSAGE(IsValidDestination(destination), | ||||
BOOST_CHECK_MESSAGE(addr.IsValid(), "!IsValid:" + strTest); | "!IsValid:" + strTest); | ||||
BOOST_CHECK_MESSAGE(addr.IsScript() == (exp_addrType == "script"), | BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) != | ||||
nullptr) == (exp_addrType == "script"), | |||||
"isScript mismatch" + strTest); | "isScript mismatch" + strTest); | ||||
CTxDestination dest = addr.Get(); | |||||
BOOST_CHECK_MESSAGE( | BOOST_CHECK_MESSAGE( | ||||
boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), dest), | boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), | ||||
destination), | |||||
"addrType mismatch" + strTest); | "addrType mismatch" + strTest); | ||||
// Public key must be invalid private key | // Public key must be invalid private key | ||||
secret.SetString(exp_base58string); | secret.SetString(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!secret.IsValid(), | BOOST_CHECK_MESSAGE(!secret.IsValid(), | ||||
"IsValid pubkey as privkey:" + strTest); | "IsValid pubkey as privkey:" + strTest); | ||||
} | } | ||||
} | } | ||||
Show All 40 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
} else if (exp_addrType == "script") { | } else if (exp_addrType == "script") { | ||||
dest = CScriptID(uint160(exp_payload)); | dest = CScriptID(uint160(exp_payload)); | ||||
} else if (exp_addrType == "none") { | } else if (exp_addrType == "none") { | ||||
dest = CNoDestination(); | dest = CNoDestination(); | ||||
} else { | } else { | ||||
BOOST_ERROR("Bad addrtype: " << strTest); | BOOST_ERROR("Bad addrtype: " << strTest); | ||||
continue; | continue; | ||||
} | } | ||||
CBitcoinAddress addrOut; | std::string address = EncodeDestination(dest); | ||||
BOOST_CHECK_MESSAGE(addrOut.Set(dest), "encode dest: " + strTest); | BOOST_CHECK_MESSAGE(address == exp_base58string, | ||||
BOOST_CHECK_MESSAGE(addrOut.ToString() == exp_base58string, | |||||
"mismatch: " + strTest); | "mismatch: " + strTest); | ||||
} | } | ||||
} | } | ||||
// Visiting a CNoDestination must fail | |||||
CBitcoinAddress dummyAddr; | |||||
CTxDestination nodest = CNoDestination(); | |||||
BOOST_CHECK(!dummyAddr.Set(nodest)); | |||||
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; | CBitcoinSecret secret; | ||||
CBitcoinAddress addr; | 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 | ||||
addr.SetString(exp_base58string); | destination = DecodeDestination(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!addr.IsValid(), "IsValid pubkey:" + strTest); | BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | ||||
"IsValid pubkey:" + strTest); | |||||
secret.SetString(exp_base58string); | secret.SetString(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest); | BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |