Line data Source code
1 : // Copyright (c) 2011-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(base64_tests, BasicTestingSetup) 11 : 12 95 : BOOST_AUTO_TEST_CASE(base64_testvectors) 13 : { 14 8 : static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; 15 8 : static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"}; 16 8 : for (unsigned int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++) 17 : { 18 7 : std::string strEnc = EncodeBase64(vstrIn[i]); 19 7 : BOOST_CHECK_EQUAL(strEnc, vstrOut[i]); 20 7 : std::string strDec = DecodeBase64(strEnc); 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)DecodeBase64(std::string("invalid", 7), &failure); 27 1 : BOOST_CHECK_EQUAL(failure, true); 28 1 : (void)DecodeBase64(std::string("nQB/pZw=", 8), &failure); 29 1 : BOOST_CHECK_EQUAL(failure, false); 30 1 : (void)DecodeBase64(std::string("nQB/pZw=\0invalid", 16), &failure); 31 1 : BOOST_CHECK_EQUAL(failure, true); 32 1 : (void)DecodeBase64(std::string("nQB/pZw=invalid", 15), &failure); 33 1 : BOOST_CHECK_EQUAL(failure, true); 34 1 : } 35 : 36 89 : BOOST_AUTO_TEST_SUITE_END()