2018-07-26 18:36:45 -04:00
|
|
|
// Copyright (c) 2011-2018 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 06:11:00 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-08-15 16:22:09 -03:00
|
|
|
#include <test/data/base58_encode_decode.json.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
|
2017-09-20 20:57:44 -03:00
|
|
|
#include <base58.h>
|
2019-04-11 09:44:10 -04:00
|
|
|
#include <test/setup_common.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/strencodings.h>
|
2017-09-19 19:30:11 -03:00
|
|
|
|
|
|
|
#include <univalue.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-05-18 09:02:18 -03:00
|
|
|
|
2015-05-13 16:29:19 -03:00
|
|
|
extern UniValue read_json(const std::string& jsondata);
|
2011-09-30 21:47:47 -03:00
|
|
|
|
2015-03-12 05:34:42 -03:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
|
2011-09-30 21:47:47 -03:00
|
|
|
|
2012-09-29 06:13:32 -03:00
|
|
|
// Goal: test low-level base58 encoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
|
|
|
|
{
|
2015-05-13 16:29:19 -03:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
|
2014-08-20 15:15:16 -04:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 16:29:19 -03:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 15:15:16 -04:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 06:13:32 -03:00
|
|
|
if (test.size() < 2) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
|
|
|
|
std::string base58string = test[1].get_str();
|
|
|
|
BOOST_CHECK_MESSAGE(
|
2016-12-09 00:01:37 -03:00
|
|
|
EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
|
2012-09-29 06:13:32 -03:00
|
|
|
strTest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goal: test low-level base58 decoding functionality
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|
|
|
{
|
2015-05-13 16:29:19 -03:00
|
|
|
UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
|
2012-09-29 06:13:32 -03:00
|
|
|
std::vector<unsigned char> result;
|
|
|
|
|
2014-08-20 15:15:16 -04:00
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) {
|
2015-05-13 16:29:19 -03:00
|
|
|
UniValue test = tests[idx];
|
2014-08-20 15:15:16 -04:00
|
|
|
std::string strTest = test.write();
|
2012-09-29 06:13:32 -03:00
|
|
|
if (test.size() < 2) // Allow for extra stuff (useful for comments)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Bad test: " << strTest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<unsigned char> expected = ParseHex(test[0].get_str());
|
|
|
|
std::string base58string = test[1].get_str();
|
|
|
|
BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result), strTest);
|
|
|
|
BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(!DecodeBase58("invalid", result));
|
2012-09-19 04:31:15 -03:00
|
|
|
|
|
|
|
// check that DecodeBase58 skips whitespace, but still fails with unexpected 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 ", result));
|
|
|
|
std::vector<unsigned char> expected = ParseHex("971a55");
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
2012-09-29 06:13:32 -03:00
|
|
|
}
|
|
|
|
|
2011-09-30 21:47:47 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|