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 : 10 89 : BOOST_FIXTURE_TEST_SUITE(base32_tests, BasicTestingSetup) 11 : 12 95 : BOOST_AUTO_TEST_CASE(base32_testvectors) 13 : { 14 8 : static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; 15 8 : static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"}; 16 8 : for (unsigned int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++) 17 : { 18 7 : std::string strEnc = EncodeBase32(vstrIn[i]); 19 7 : BOOST_CHECK_EQUAL(strEnc, vstrOut[i]); 20 7 : std::string strDec = DecodeBase32(vstrOut[i]); 21 7 : BOOST_CHECK_EQUAL(strDec, vstrIn[i]); 22 7 : } 23 : 24 : // Decoding strings with embedded NUL characters should fail 25 1 : bool failure; 26 1 : (void)DecodeBase32(std::string("invalid", 7), &failure); 27 1 : BOOST_CHECK_EQUAL(failure, true); 28 1 : (void)DecodeBase32(std::string("AWSX3VPP", 8), &failure); 29 1 : BOOST_CHECK_EQUAL(failure, false); 30 1 : (void)DecodeBase32(std::string("AWSX3VPP\0invalid", 16), &failure); 31 1 : BOOST_CHECK_EQUAL(failure, true); 32 1 : (void)DecodeBase32(std::string("AWSX3VPPinvalid", 15), &failure); 33 1 : BOOST_CHECK_EQUAL(failure, true); 34 1 : } 35 : 36 89 : BOOST_AUTO_TEST_SUITE_END()