Changeset View
Changeset View
Standalone View
Standalone View
src/test/cashaddrenc_tests.cpp
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | const std::array<std::pair<uint8_t, uint32_t>, 8> valid_sizes = { | ||||
{{0, 20}, {1, 24}, {2, 28}, {3, 32}, {4, 40}, {5, 48}, {6, 56}, {7, 64}}}; | {{0, 20}, {1, 24}, {2, 28}, {3, 32}, {4, 40}, {5, 48}, {6, 56}, {7, 64}}}; | ||||
} // namespace | } // namespace | ||||
BOOST_FIXTURE_TEST_SUITE(cashaddrenc_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(cashaddrenc_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(encode_decode_all_sizes) { | BOOST_AUTO_TEST_CASE(encode_decode_all_sizes) { | ||||
FastRandomContext rand(true); | FastRandomContext rand(true); | ||||
const std::string prefix = "bitcoincash"; | const std::string CASHADDR_PREFIX = "abc"; | ||||
for (auto ps : valid_sizes) { | for (auto ps : valid_sizes) { | ||||
std::vector<uint8_t> data = | std::vector<uint8_t> data = | ||||
insecure_GetRandomByteArray(rand, ps.second); | insecure_GetRandomByteArray(rand, ps.second); | ||||
CashAddrContent content = {PUBKEY_TYPE, data}; | CashAddrContent content = {PUBKEY_TYPE, data}; | ||||
std::vector<uint8_t> packed_data = PackCashAddrContent(content); | std::vector<uint8_t> packed_data = PackCashAddrContent(content); | ||||
// Check that the packed size is correct | // Check that the packed size is correct | ||||
BOOST_CHECK_EQUAL(packed_data[1] >> 2, ps.first); | BOOST_CHECK_EQUAL(packed_data[1] >> 2, ps.first); | ||||
std::string address = cashaddr::Encode(prefix, packed_data); | std::string address = cashaddr::Encode(CASHADDR_PREFIX, packed_data); | ||||
// Check that the address decodes properly | // Check that the address decodes properly | ||||
CashAddrContent decoded = DecodeCashAddrContent(address, prefix); | CashAddrContent decoded = | ||||
DecodeCashAddrContent(address, CASHADDR_PREFIX); | |||||
BOOST_CHECK_EQUAL_COLLECTIONS( | BOOST_CHECK_EQUAL_COLLECTIONS( | ||||
std::begin(content.hash), std::end(content.hash), | std::begin(content.hash), std::end(content.hash), | ||||
std::begin(decoded.hash), std::end(decoded.hash)); | std::begin(decoded.hash), std::end(decoded.hash)); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(check_packaddr_throws) { | BOOST_AUTO_TEST_CASE(check_packaddr_throws) { | ||||
FastRandomContext rand(true); | FastRandomContext rand(true); | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(check_type) { | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* We ensure size is extracted and checked properly. | * We ensure size is extracted and checked properly. | ||||
*/ | */ | ||||
BOOST_AUTO_TEST_CASE(check_size) { | BOOST_AUTO_TEST_CASE(check_size) { | ||||
const CTxDestination nodst = CNoDestination{}; | const CTxDestination nodst = CNoDestination{}; | ||||
const std::string prefix = "bitcoincash"; | const auto prefixes = {"bitcoincash", "abc"}; | ||||
std::vector<uint8_t> data; | std::vector<uint8_t> data; | ||||
for (const auto &prefix : prefixes) { | |||||
for (auto ps : valid_sizes) { | for (auto ps : valid_sizes) { | ||||
// Number of bytes required for a 5-bit packed version of a hash, with | // Number of bytes required for a 5-bit packed version of a hash, | ||||
// version byte. Add half a byte(4) so integer math provides the next | // with version byte. Add half a byte(4) so integer math provides | ||||
// multiple-of-5 that would fit all the data. | // the next multiple-of-5 that would fit all the data. | ||||
size_t expectedSize = (8 * (1 + ps.second) + 4) / 5; | size_t expectedSize = (8 * (1 + ps.second) + 4) / 5; | ||||
data.resize(expectedSize); | data.resize(expectedSize); | ||||
std::fill(begin(data), end(data), 0); | std::fill(begin(data), end(data), 0); | ||||
// After conversion from 8 bit packing to 5 bit packing, the size will | // After conversion from 8 bit packing to 5 bit packing, the size | ||||
// be in the second 5-bit group, shifted left twice. | // will be in the second 5-bit group, shifted left twice. | ||||
data[1] = ps.first << 2; | data[1] = ps.first << 2; | ||||
auto content = | auto content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | ||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), ps.second); | BOOST_CHECK_EQUAL(content.hash.size(), ps.second); | ||||
data.push_back(0); | data.push_back(0); | ||||
content = DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | |||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | ||||
data.pop_back(); | data.pop_back(); | ||||
data.pop_back(); | data.pop_back(); | ||||
content = DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | |||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | ||||
} | } | ||||
} | } | ||||
} | |||||
BOOST_AUTO_TEST_CASE(test_encode_address) { | BOOST_AUTO_TEST_CASE(test_encode_address) { | ||||
const auto params = CreateChainParams(CBaseChainParams::MAIN); | const auto params = CreateChainParams(CBaseChainParams::MAIN); | ||||
std::vector<std::vector<uint8_t>> hash{ | std::vector<std::vector<uint8_t>> hash{ | ||||
{118, 160, 64, 83, 189, 160, 168, 139, 218, 81, | {118, 160, 64, 83, 189, 160, 168, 139, 218, 81, | ||||
119, 184, 106, 21, 195, 178, 159, 85, 152, 115}, | 119, 184, 106, 21, 195, 178, 159, 85, 152, 115}, | ||||
{203, 72, 18, 50, 41, 156, 213, 116, 49, 81, | {203, 72, 18, 50, 41, 156, 213, 116, 49, 81, | ||||
172, 75, 45, 99, 174, 25, 142, 123, 176, 169}, | 172, 75, 45, 99, 174, 25, 142, 123, 176, 169}, | ||||
{1, 31, 40, 228, 115, 201, 95, 64, 19, 215, | {1, 31, 40, 228, 115, 201, 95, 64, 19, 215, | ||||
213, 62, 197, 251, 195, 180, 45, 248, 237, 16}}; | 213, 62, 197, 251, 195, 180, 45, 248, 237, 16}}; | ||||
std::vector<std::string> pubkey = { | std::vector<std::string> pubkey = { | ||||
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a", | "abc:qpm2qsznhks23z7629mms6s4cwef74vcwvjr9zzhs3", | ||||
"bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy", | "abc:qr95sy3j9xwd2ap32xkykttr4cvcu7as4yefrnvckg", | ||||
"bitcoincash:qqq3728yw0y47sqn6l2na30mcw6zm78dzqre909m2r"}; | "abc:qqq3728yw0y47sqn6l2na30mcw6zm78dzq4s2922q0"}; | ||||
std::vector<std::string> script = { | std::vector<std::string> script = { | ||||
"bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq", | "abc:ppm2qsznhks23z7629mms6s4cwef74vcwv9xcd95tv", | ||||
"bitcoincash:pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e", | "abc:pr95sy3j9xwd2ap32xkykttr4cvcu7as4ywv7utmd4", | ||||
"bitcoincash:pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"}; | "abc:pqq3728yw0y47sqn6l2na30mcw6zm78dzqz4h2dfmj"}; | ||||
for (size_t i = 0; i < hash.size(); ++i) { | for (size_t i = 0; i < hash.size(); ++i) { | ||||
const CTxDestination dstKey = PKHash(uint160(hash[i])); | const CTxDestination dstKey = PKHash(uint160(hash[i])); | ||||
BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr(dstKey, *params)); | BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr(dstKey, *params)); | ||||
CashAddrContent keyContent{PUBKEY_TYPE, hash[i]}; | CashAddrContent keyContent{PUBKEY_TYPE, hash[i]}; | ||||
BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr("bitcoincash", keyContent)); | BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr("abc", keyContent)); | ||||
const CTxDestination dstScript = ScriptHash(uint160(hash[i])); | const CTxDestination dstScript = ScriptHash(uint160(hash[i])); | ||||
BOOST_CHECK_EQUAL(script[i], EncodeCashAddr(dstScript, *params)); | BOOST_CHECK_EQUAL(script[i], EncodeCashAddr(dstScript, *params)); | ||||
CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | ||||
BOOST_CHECK_EQUAL(script[i], | BOOST_CHECK_EQUAL(script[i], EncodeCashAddr("abc", scriptContent)); | ||||
} | |||||
std::vector<std::string> backward_compat_pubkey = { | |||||
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a", | |||||
"bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy", | |||||
"bitcoincash:qqq3728yw0y47sqn6l2na30mcw6zm78dzqre909m2r"}; | |||||
std::vector<std::string> backward_compat_script = { | |||||
"bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq", | |||||
"bitcoincash:pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e", | |||||
"bitcoincash:pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"}; | |||||
for (size_t i = 0; i < hash.size(); ++i) { | |||||
CashAddrContent keyContent{PUBKEY_TYPE, hash[i]}; | |||||
BOOST_CHECK_EQUAL(backward_compat_pubkey[i], | |||||
EncodeCashAddr("bitcoincash", keyContent)); | |||||
CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | |||||
BOOST_CHECK_EQUAL(backward_compat_script[i], | |||||
EncodeCashAddr("bitcoincash", scriptContent)); | EncodeCashAddr("bitcoincash", scriptContent)); | ||||
} | } | ||||
} | } | ||||
struct CashAddrTestVector { | struct CashAddrTestVector { | ||||
std::string prefix; | std::string prefix; | ||||
CashAddrType type; | CashAddrType type; | ||||
std::vector<uint8_t> hash; | std::vector<uint8_t> hash; | ||||
▲ Show 20 Lines • Show All 152 Lines • Show Last 20 Lines |