Changeset View
Changeset View
Standalone View
Standalone View
src/test/cashaddrenc_tests.cpp
- This file was added.
// Copyright (c) 2017 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#include "cashaddrenc.h" | |||||
#include "chainparams.h" | |||||
#include "random.h" | |||||
#include "test/test_bitcoin.h" | |||||
#include "uint256.h" | |||||
#include <boost/test/unit_test.hpp> | |||||
static std::vector<std::string> GetNetworks() { | |||||
return {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, | |||||
CBaseChainParams::REGTEST}; | |||||
} | |||||
static 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; | |||||
} | |||||
BOOST_FIXTURE_TEST_SUITE(cashaddrenc_tests, BasicTestingSetup) | |||||
BOOST_AUTO_TEST_CASE(encode_decode) { | |||||
std::vector<CTxDestination> toTest = {CNoDestination{}, | |||||
CKeyID(uint160S("badf00d")), | |||||
CScriptID(uint160S("f00dbad"))}; | |||||
for (auto dst : toTest) { | |||||
for (auto net : GetNetworks()) { | |||||
std::string encoded = EncodeCashAddr(dst, Params(net)); | |||||
CTxDestination decoded = DecodeCashAddr(encoded, Params(net)); | |||||
BOOST_CHECK(dst == decoded); | |||||
} | |||||
} | |||||
} | |||||
// Check that an encoded cash address is not valid on another network. | |||||
BOOST_AUTO_TEST_CASE(invalid_on_wrong_network) { | |||||
const CTxDestination dst = CKeyID(uint160S("c0ffee")); | |||||
const CTxDestination invalidDst = CNoDestination{}; | |||||
for (auto net : GetNetworks()) { | |||||
for (auto otherNet : GetNetworks()) { | |||||
if (net == otherNet) continue; | |||||
std::string encoded = EncodeCashAddr(dst, Params(net)); | |||||
CTxDestination decoded = DecodeCashAddr(encoded, Params(otherNet)); | |||||
BOOST_CHECK(decoded != dst); | |||||
BOOST_CHECK(decoded == invalidDst); | |||||
} | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_CASE(random_dst) { | |||||
FastRandomContext rand(true); | |||||
const size_t NUM_TESTS = 5000; | |||||
const CChainParams ¶ms = Params(CBaseChainParams::MAIN); | |||||
for (size_t i = 0; i < NUM_TESTS; ++i) { | |||||
uint160 hash = insecure_GetRandUInt160(rand); | |||||
const CTxDestination dst = CKeyID(hash); | |||||
const std::string encoded = EncodeCashAddr(dst, params); | |||||
const CTxDestination decoded = DecodeCashAddr(encoded, params); | |||||
BOOST_CHECK_MESSAGE(dst == decoded, "cashaddr failed for hash " | |||||
<< hash.ToString()); | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() |