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 <clientversion.h>6#include <common/signmessage.h>7#include <compat/compat.h>8#include <hash.h>9#include <key.h>10#include <script/parsing.h>11#include <span.h>12#include <sync.h>13#include <test/util/common.h>14#include <test/util/random.h>15#include <test/util/setup_common.h>16#include <test/util/time.h>17#include <uint256.h>18#include <univalue.h>19#include <util/bitdeque.h>20#include <util/byte_units.h>21#include <util/fs.h>22#include <util/fs_helpers.h>23#include <util/moneystr.h>24#include <util/overflow.h>25#include <util/readwritefile.h>26#include <util/strencodings.h>27#include <util/string.h>28#include <util/time.h>29#include <util/tokenbucket.h>30#include <util/vector.h>32#include <array>33#include <cmath>34#include <cstdint>35#include <cstring>36#include <fstream>37#include <limits>38#include <map>39#include <optional>40#include <string>41#include <thread>42#include <type_traits>43#include <utility>44#include <vector>46#include <sys/types.h>48#ifndef WIN3249#include <sys/wait.h>50#endif52#include <boost/test/unit_test.hpp>54using namespace std::literals;55using namespace util::hex_literals;56using util::ConstevalHexDigit;57using util::Join;58using util::RemovePrefix;59using util::RemovePrefixView;60using util::ReplaceAll;61using util::Split;62using util::SplitString;63using util::TrimString;64using util::TrimStringView;66static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};68/* defined in logging.cpp */69namespace BCLog {70std::string LogEscapeMessage(std::string_view str);71}73BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)75namespace {76class NoCopyOrMove77{78public:79int i;80explicit NoCopyOrMove(int i) : i{i} { }82NoCopyOrMove() = delete;83NoCopyOrMove(const NoCopyOrMove&) = delete;84NoCopyOrMove(NoCopyOrMove&&) = delete;85NoCopyOrMove& operator=(const NoCopyOrMove&) = delete;86NoCopyOrMove& operator=(NoCopyOrMove&&) = delete;88operator bool() const { return i != 0; }90int get_ip1() { return i + 1; }91bool test()92{93// Check that Assume can be used within a lambda and still call methods94[&]() { Assume(get_ip1()); }();95return Assume(get_ip1() != 5);96}97};98} // namespace100BOOST_AUTO_TEST_CASE(util_check)101{102// Check that Assert can forward103const std::unique_ptr<int> p_two = Assert(std::make_unique<int>(2));104// Check that Assert works on lvalues and rvalues105const int two = *Assert(p_two);106Assert(two == 2);107Assert(true);108// Check that Assume can be used as unary expression109const bool result{Assume(two == 2)};110Assert(result);112// Check that Assert doesn't require copy/move113NoCopyOrMove x{9};114Assert(x).i += 3;115Assert(x).test();117// Check nested Asserts118BOOST_CHECK_EQUAL(Assert((Assert(x).test() ? 3 : 0)), 3);120// Check -Wdangling-gsl does not trigger when copying the int. (It would121// trigger on "const int&")122const int nine{*Assert(std::optional<int>{9})};123BOOST_CHECK_EQUAL(9, nine);124}126BOOST_AUTO_TEST_CASE(util_criticalsection)127{128RecursiveMutex cs;130do {131LOCK(cs);132break;134BOOST_ERROR("break was swallowed!");135} while(0);137do {138TRY_LOCK(cs, lockTest);139if (lockTest) {140BOOST_CHECK(true); // Needed to suppress "Test case [...] did not check any assertions"141break;142}144BOOST_ERROR("break was swallowed!");145} while(0);146}148constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";149constexpr uint8_t HEX_PARSE_OUTPUT[] = {1500x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,1510x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,1520xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,1530xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,1540x5f155};156static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));157BOOST_AUTO_TEST_CASE(parse_hex)158{159std::vector<unsigned char> result;161// Basic test vector162std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT));163constexpr std::array<std::byte, 65> hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};164auto hex_literal_span{MakeUCharSpan(hex_literal_array)};165BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());167const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};168auto hex_literal_vec_span = MakeUCharSpan(hex_literal_vector);169BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end());171constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};172BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());174result = operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();175BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());177result = ParseHex(HEX_PARSE_INPUT);178BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());180result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value();181BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());183// Spaces between bytes must be supported184expected = {0x12, 0x34, 0x56, 0x78};185result = ParseHex("12 34 56 78");186BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());187result = TryParseHex<uint8_t>("12 34 56 78").value();188BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());190// Leading space must be supported191expected = {0x89, 0x34, 0x56, 0x78};192result = ParseHex(" 89 34 56 78");193BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());194result = TryParseHex<uint8_t>(" 89 34 56 78").value();195BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());197// Mixed case and spaces are supported198expected = {0xff, 0xaa};199result = ParseHex(" Ff aA ");200BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());201result = TryParseHex<uint8_t>(" Ff aA ").value();202BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());204// Empty string is supported205static_assert(""_hex.empty());206static_assert(""_hex_u8.empty());207BOOST_CHECK_EQUAL(""_hex_v.size(), 0);208BOOST_CHECK_EQUAL(""_hex_v_u8.size(), 0);209BOOST_CHECK_EQUAL(ParseHex("").size(), 0);210BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0);212// Spaces between nibbles is treated as invalid213BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0);214BOOST_CHECK(!TryParseHex("AAF F").has_value());216// Embedded null is treated as invalid217const std::string with_embedded_null{" 11 "s218" \0 "219" 22 "s};220BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);221BOOST_CHECK_EQUAL(ParseHex(with_embedded_null).size(), 0);222BOOST_CHECK(!TryParseHex(with_embedded_null).has_value());224// Non-hex is treated as invalid225BOOST_CHECK_EQUAL(ParseHex("1234 invalid 1234").size(), 0);226BOOST_CHECK(!TryParseHex("1234 invalid 1234").has_value());228// Truncated input is treated as invalid229BOOST_CHECK_EQUAL(ParseHex("12 3").size(), 0);230BOOST_CHECK(!TryParseHex("12 3").has_value());231}233BOOST_AUTO_TEST_CASE(consteval_hex_digit)234{235BOOST_CHECK_EQUAL(ConstevalHexDigit('0'), 0);236BOOST_CHECK_EQUAL(ConstevalHexDigit('9'), 9);237BOOST_CHECK_EQUAL(ConstevalHexDigit('a'), 0xa);238BOOST_CHECK_EQUAL(ConstevalHexDigit('f'), 0xf);239}241BOOST_AUTO_TEST_CASE(util_HexStr)242{243BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT);244BOOST_CHECK_EQUAL(HexStr(std::span{HEX_PARSE_OUTPUT}.last(0)), "");245BOOST_CHECK_EQUAL(HexStr(std::span{HEX_PARSE_OUTPUT}.first(0)), "");247{248constexpr std::string_view out_exp{"04678afdb0"};249constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};250const std::span<const uint8_t> in_u{MakeUCharSpan(in_s)};
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.