Changeset View
Changeset View
Standalone View
Standalone View
src/test/base58_tests.cpp
Show First 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
// 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); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Goal: check that generated keys match test vectors | static void testGeneratedKeys(bool cashaddr) { | ||||
BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) { | |||||
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))); | ||||
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) | ||||
Show All 28 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; | ||||
} | } | ||||
std::string address = EncodeDestination(dest); | std::string address = cashaddr ? EncodeDestination(dest) | ||||
: EncodeDestinationBase58(dest); | |||||
BOOST_CHECK_MESSAGE(dest == DecodeDestination(address), | |||||
"mismatch: " + strTest); | |||||
if (!cashaddr) { | |||||
// TODO: We only have test files for base58 encoding. | |||||
// We need to create a json_tests::cashaddr_keys_valid | |||||
BOOST_CHECK_MESSAGE(address == exp_base58string, | BOOST_CHECK_MESSAGE(address == exp_base58string, | ||||
"mismatch: " + strTest); | "mismatch: " + strTest); | ||||
} | } | ||||
} | } | ||||
} | |||||
SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
} | } | ||||
// Goal: check that generated keys match test vectors | |||||
BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) { | |||||
bool cashaddr = true; | |||||
testGeneratedKeys(cashaddr); | |||||
testGeneratedKeys(!cashaddr); | |||||
} | |||||
// 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))); | ||||
Show All 23 Lines |