Branch data Line data Source code
# 1 : : // Copyright (c) 2012-2020 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <test/util/setup_common.h>
# 6 : : #include <util/strencodings.h>
# 7 : :
# 8 : : #include <boost/test/unit_test.hpp>
# 9 : : #include <string>
# 10 : :
# 11 : : using namespace std::literals;
# 12 : :
# 13 : : BOOST_FIXTURE_TEST_SUITE(base32_tests, BasicTestingSetup)
# 14 : :
# 15 : : BOOST_AUTO_TEST_CASE(base32_testvectors)
# 16 : 2 : {
# 17 : 2 : static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
# 18 : 2 : static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};
# 19 : 2 : static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"};
# 20 [ + + ]: 16 : for (unsigned int i=0; i<std::size(vstrIn); i++)
# 21 : 14 : {
# 22 : 14 : std::string strEnc = EncodeBase32(vstrIn[i]);
# 23 : 14 : BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
# 24 : 14 : strEnc = EncodeBase32(vstrIn[i], false);
# 25 : 14 : BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
# 26 : 14 : std::string strDec = DecodeBase32(vstrOut[i]);
# 27 : 14 : BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
# 28 : 14 : }
# 29 : :
# 30 : : // Decoding strings with embedded NUL characters should fail
# 31 : 2 : bool failure;
# 32 : 2 : (void)DecodeBase32("invalid\0"s, &failure); // correct size, invalid due to \0
# 33 : 2 : BOOST_CHECK(failure);
# 34 : 2 : (void)DecodeBase32("AWSX3VPP"s, &failure); // valid
# 35 : 2 : BOOST_CHECK(!failure);
# 36 : 2 : (void)DecodeBase32("AWSX3VPP\0invalid"s, &failure); // correct size, invalid due to \0
# 37 : 2 : BOOST_CHECK(failure);
# 38 : 2 : (void)DecodeBase32("AWSX3VPPinvalid"s, &failure); // invalid size
# 39 : 2 : BOOST_CHECK(failure);
# 40 : 2 : }
# 41 : :
# 42 : : BOOST_AUTO_TEST_SUITE_END()
|