Changeset View
Changeset View
Standalone View
Standalone View
src/test/cashaddrenc_tests.cpp
Show All 17 Lines | |||||
namespace { | namespace { | ||||
std::vector<std::string> GetNetworks() { | std::vector<std::string> GetNetworks() { | ||||
return {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, | return {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, | ||||
CBaseChainParams::REGTEST}; | CBaseChainParams::REGTEST}; | ||||
} | } | ||||
uint160 insecure_GetRandUInt160(FastRandomContext &rand) { | |||||
uint160 n; | |||||
for (uint8_t *c = n.begin(); c != n.end(); ++c) { | |||||
*c = static_cast<uint8_t>(rand.rand32()); | |||||
} | |||||
return n; | |||||
} | |||||
std::vector<uint8_t> insecure_GetRandomByteArray(FastRandomContext &rand, | std::vector<uint8_t> insecure_GetRandomByteArray(FastRandomContext &rand, | ||||
size_t n) { | size_t n) { | ||||
std::vector<uint8_t> out; | std::vector<uint8_t> out; | ||||
out.reserve(n); | out.reserve(n); | ||||
for (size_t i = 0; i < n; i++) { | for (size_t i = 0; i < n; i++) { | ||||
out.push_back(uint8_t(rand.randbits(8))); | out.push_back(uint8_t(rand.randbits(8))); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | for (auto net : GetNetworks()) { | ||||
CTxDestination decoded = DecodeCashAddr(encoded, *otherNetParams); | CTxDestination decoded = DecodeCashAddr(encoded, *otherNetParams); | ||||
BOOST_CHECK(decoded != dst); | BOOST_CHECK(decoded != dst); | ||||
BOOST_CHECK(decoded == invalidDst); | BOOST_CHECK(decoded == invalidDst); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(random_dst) { | BOOST_AUTO_TEST_CASE(random_dst) { | ||||
FastRandomContext rand(true); | |||||
const size_t NUM_TESTS = 5000; | const size_t NUM_TESTS = 5000; | ||||
const auto params = CreateChainParams(CBaseChainParams::MAIN); | const auto params = CreateChainParams(CBaseChainParams::MAIN); | ||||
for (size_t i = 0; i < NUM_TESTS; ++i) { | for (size_t i = 0; i < NUM_TESTS; ++i) { | ||||
uint160 hash = insecure_GetRandUInt160(rand); | uint160 hash = InsecureRand160(); | ||||
const CTxDestination dst_key = PKHash(hash); | const CTxDestination dst_key = PKHash(hash); | ||||
const CTxDestination dst_scr = ScriptHash(hash); | const CTxDestination dst_scr = ScriptHash(hash); | ||||
const std::string encoded_key = EncodeCashAddr(dst_key, *params); | const std::string encoded_key = EncodeCashAddr(dst_key, *params); | ||||
const CTxDestination decoded_key = DecodeCashAddr(encoded_key, *params); | const CTxDestination decoded_key = DecodeCashAddr(encoded_key, *params); | ||||
const std::string encoded_scr = EncodeCashAddr(dst_scr, *params); | const std::string encoded_scr = EncodeCashAddr(dst_scr, *params); | ||||
const CTxDestination decoded_scr = DecodeCashAddr(encoded_scr, *params); | const CTxDestination decoded_scr = DecodeCashAddr(encoded_scr, *params); | ||||
▲ Show 20 Lines • Show All 302 Lines • Show Last 20 Lines |