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) 2012-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(base32_tests)16BOOST_AUTO_TEST_CASE(base32_testvectors)17{18static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};19static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};20static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"};21for (unsigned int i=0; i<std::size(vstrIn); i++)22{23std::string strEnc = EncodeBase32(vstrIn[i]);24BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);25strEnc = EncodeBase32(vstrIn[i], false);26BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);27auto dec = DecodeBase32(vstrOut[i]);28BOOST_REQUIRE(dec);29BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);30}32BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid")); // invalid size33BOOST_CHECK( DecodeBase32("AWSX3VPP")); // valid35// Decoding strings with embedded NUL characters should fail36BOOST_CHECK(!DecodeBase32("invalid\0"sv)); // correct size, invalid due to \037BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"sv)); // correct size, invalid due to \038}40BOOST_AUTO_TEST_CASE(base32_padding)41{42// Is valid without padding43BOOST_CHECK_EQUAL(EncodeBase32("fooba"), "mzxw6ytb");45// Valid size46BOOST_CHECK(!DecodeBase32("========"));47BOOST_CHECK(!DecodeBase32("a======="));48BOOST_CHECK( DecodeBase32("aa======"));49BOOST_CHECK(!DecodeBase32("aaa====="));50BOOST_CHECK( DecodeBase32("aaaa===="));51BOOST_CHECK( DecodeBase32("aaaaa==="));52BOOST_CHECK(!DecodeBase32("aaaaaa=="));53BOOST_CHECK( DecodeBase32("aaaaaaa="));54}56BOOST_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.