Changeset View
Changeset View
Standalone View
Standalone View
src/test/base58_tests.cpp
Show All 10 Lines | |||||
#include <utilstrencodings.h> | #include <utilstrencodings.h> | ||||
#include <test/data/base58_encode_decode.json.h> | #include <test/data/base58_encode_decode.json.h> | ||||
#include <test/data/base58_keys_invalid.json.h> | #include <test/data/base58_keys_invalid.json.h> | ||||
#include <test/data/base58_keys_valid.json.h> | #include <test/data/base58_keys_valid.json.h> | ||||
#include <test/jsonutil.h> | #include <test/jsonutil.h> | ||||
#include <test/test_bitcoin.h> | #include <test/test_bitcoin.h> | ||||
#include <boost/test/unit_test.hpp> | |||||
#include <univalue.h> | #include <univalue.h> | ||||
#include <boost/test/unit_test.hpp> | |||||
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup) | ||||
// Goal: test low-level base58 encoding functionality | // Goal: test low-level base58 encoding functionality | ||||
BOOST_AUTO_TEST_CASE(base58_EncodeBase58) { | BOOST_AUTO_TEST_CASE(base58_EncodeBase58) { | ||||
UniValue tests = | UniValue tests = | ||||
read_json(std::string(json_tests::base58_encode_decode, | read_json(std::string(json_tests::base58_encode_decode, | ||||
json_tests::base58_encode_decode + | json_tests::base58_encode_decode + | ||||
sizeof(json_tests::base58_encode_decode))); | sizeof(json_tests::base58_encode_decode))); | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(base58_DecodeBase58) { | ||||
// non-whitespace at the end. | // non-whitespace at the end. | ||||
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result)); | BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result)); | ||||
BOOST_CHECK(DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result)); | BOOST_CHECK(DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result)); | ||||
std::vector<uint8_t> expected = ParseHex("971a55"); | std::vector<uint8_t> expected = ParseHex("971a55"); | ||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), | BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), | ||||
expected.begin(), expected.end()); | expected.begin(), expected.end()); | ||||
} | } | ||||
// Visitor to check address type | |||||
class TestAddrTypeVisitor : public boost::static_visitor<bool> { | |||||
private: | |||||
std::string exp_addrType; | |||||
public: | |||||
explicit TestAddrTypeVisitor(const std::string &_exp_addrType) | |||||
: exp_addrType(_exp_addrType) {} | |||||
bool operator()(const CKeyID &id) const { | |||||
return (exp_addrType == "pubkey"); | |||||
} | |||||
bool operator()(const CScriptID &id) const { | |||||
return (exp_addrType == "script"); | |||||
} | |||||
bool operator()(const CNoDestination &no) const { | |||||
return (exp_addrType == "none"); | |||||
} | |||||
}; | |||||
// Visitor to check address payload | |||||
class TestPayloadVisitor : public boost::static_visitor<bool> { | |||||
private: | |||||
std::vector<uint8_t> exp_payload; | |||||
public: | |||||
explicit TestPayloadVisitor(std::vector<uint8_t> &_exp_payload) | |||||
: exp_payload(_exp_payload) {} | |||||
bool operator()(const CKeyID &id) const { | |||||
uint160 exp_key(exp_payload); | |||||
return exp_key == id; | |||||
} | |||||
bool operator()(const CScriptID &id) const { | |||||
uint160 exp_key(exp_payload); | |||||
return exp_key == id; | |||||
} | |||||
bool operator()(const CNoDestination &no) const { | |||||
return exp_payload.size() == 0; | |||||
} | |||||
}; | |||||
// 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; | ||||
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(); | ||||
bool isTestnet = find_value(metadata, "isTestnet").get_bool(); | SelectParams(find_value(metadata, "chain").get_str()); | ||||
if (isTestnet) | |||||
SelectParams(CBaseChainParams::TESTNET); | |||||
else | |||||
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 | ||||
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 | ||||
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 { | ||||
// "script" or "pubkey" | |||||
std::string exp_addrType = | |||||
find_value(metadata, "addrType").get_str(); | |||||
// Must be valid public key | // Must be valid public key | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
CScript script = GetScriptForDestination(destination); | |||||
BOOST_CHECK_MESSAGE(IsValidDestination(destination), | BOOST_CHECK_MESSAGE(IsValidDestination(destination), | ||||
"!IsValid:" + strTest); | "!IsValid:" + strTest); | ||||
BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) != | BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | ||||
nullptr) == (exp_addrType == "script"), | |||||
"isScript mismatch" + strTest); | |||||
BOOST_CHECK_MESSAGE( | |||||
boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), | |||||
destination), | |||||
"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 11 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
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(); | SelectParams(find_value(metadata, "chain").get_str()); | ||||
if (isTestnet) | |||||
SelectParams(CBaseChainParams::TESTNET); | |||||
else | |||||
SelectParams(CBaseChainParams::MAIN); | |||||
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; | CBitcoinSecret secret; | ||||
secret.SetKey(key); | secret.SetKey(key); | ||||
BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, | BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, | ||||
"result mismatch: " + strTest); | "result mismatch: " + strTest); | ||||
} else { | } else { | ||||
std::string exp_addrType = | |||||
find_value(metadata, "addrType").get_str(); | |||||
CTxDestination dest; | CTxDestination dest; | ||||
if (exp_addrType == "pubkey") { | CScript exp_script(exp_payload.begin(), exp_payload.end()); | ||||
dest = CKeyID(uint160(exp_payload)); | ExtractDestination(exp_script, dest); | ||||
} else if (exp_addrType == "script") { | |||||
dest = CScriptID(uint160(exp_payload)); | |||||
} else if (exp_addrType == "none") { | |||||
dest = CNoDestination(); | |||||
} else { | |||||
BOOST_ERROR("Bad addrtype: " << strTest); | |||||
continue; | |||||
} | |||||
std::string address = EncodeLegacyAddr(dest, Params()); | std::string address = EncodeLegacyAddr(dest, Params()); | ||||
BOOST_CHECK_MESSAGE(address == exp_base58string, | BOOST_CHECK_EQUAL(address, exp_base58string); | ||||
"mismatch: " + strTest); | |||||
} | } | ||||
} | } | ||||
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 | ||||
Show All 12 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
// 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, | |||||
CBaseChainParams::REGTEST}) { | |||||
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); | 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() |