Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <util/strencodings.h>7#include <boost/test/unit_test.hpp>9#include <algorithm>10#include <string>12using namespace std::literals;14BOOST_AUTO_TEST_SUITE(base64_tests)16BOOST_AUTO_TEST_CASE(base64_testvectors)17{18static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};19static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};20for (unsigned int i=0; i<std::size(vstrIn); i++)21{22std::string strEnc = EncodeBase64(vstrIn[i]);23BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);24auto dec = DecodeBase64(strEnc);25BOOST_REQUIRE(dec);26BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);27}29{30const std::vector<uint8_t> in_u{0xff, 0x01, 0xff};31const std::vector<std::byte> in_b{std::byte{0xff}, std::byte{0x01}, std::byte{0xff}};32const std::string in_s{"\xff\x01\xff"};33const std::string out_exp{"/wH/"};34BOOST_CHECK_EQUAL(EncodeBase64(in_u), out_exp);35BOOST_CHECK_EQUAL(EncodeBase64(in_b), out_exp);36BOOST_CHECK_EQUAL(EncodeBase64(in_s), out_exp);37}39BOOST_CHECK(DecodeBase64("nQB/pZw=")); // valid41// Decoding strings with embedded NUL characters should fail42BOOST_CHECK(!DecodeBase64("invalid\0"sv)); // correct size, invalid due to \043BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"sv));44BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"sv)); // invalid, padding only allowed at the end45}47BOOST_AUTO_TEST_CASE(base64_padding)48{49// Is valid without padding50BOOST_CHECK_EQUAL(EncodeBase64("foobar"), "Zm9vYmFy");52// Valid size53BOOST_CHECK(!DecodeBase64("===="));54BOOST_CHECK(!DecodeBase64("a==="));55BOOST_CHECK( DecodeBase64("YQ=="));56BOOST_CHECK( DecodeBase64("YWE="));57}59BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.