25 #include <util/time.h> 42 #include <sys/types.h> 48 #include <boost/test/unit_test.hpp> 76 explicit NoCopyOrMove(
int i) : i{i} { }
78 NoCopyOrMove() =
delete;
79 NoCopyOrMove(
const NoCopyOrMove&) =
delete;
80 NoCopyOrMove(NoCopyOrMove&&) =
delete;
81 NoCopyOrMove& operator=(
const NoCopyOrMove&) =
delete;
82 NoCopyOrMove& operator=(NoCopyOrMove&&) =
delete;
84 operator bool()
const {
return i != 0; }
86 int get_ip1() {
return i + 1; }
90 [&]() {
Assume(get_ip1()); }();
91 return Assume(get_ip1() != 5);
99 const std::unique_ptr<int> p_two =
Assert(std::make_unique<int>(2));
101 const int two = *
Assert(p_two);
118 const int nine{*
Assert(std::optional<int>{9})};
130 BOOST_ERROR(
"break was swallowed!");
140 BOOST_ERROR(
"break was swallowed!");
144 constexpr
char HEX_PARSE_INPUT[] =
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
146 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
147 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
148 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
149 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
155 std::vector<unsigned char>
result;
159 constexpr std::array<std::byte, 65> hex_literal_array{
operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
161 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
163 const std::vector<std::byte> hex_literal_vector{
operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
164 auto hex_literal_vec_span =
MakeUCharSpan(hex_literal_vector);
165 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end());
167 constexpr std::array<uint8_t, 65> hex_literal_array_uint8{
operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
168 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
170 result =
operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();
171 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
174 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
177 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
180 expected = {0x12, 0x34, 0x56, 0x78};
182 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
183 result = TryParseHex<uint8_t>(
"12 34 56 78").value();
184 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
187 expected = {0x89, 0x34, 0x56, 0x78};
189 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
190 result = TryParseHex<uint8_t>(
" 89 34 56 78").value();
191 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
194 expected = {0xff, 0xaa};
196 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
197 result = TryParseHex<uint8_t>(
" Ff aA ").value();
198 BOOST_CHECK_EQUAL_COLLECTIONS(
result.begin(),
result.end(), expected.begin(), expected.end());
201 static_assert(
""_hex.empty());
202 static_assert(
""_hex_u8.empty());
213 const std::string with_embedded_null{
" 11 "s 244 constexpr std::string_view out_exp{
"04678afdb0"};
247 const std::span<const std::byte> in_b{
MakeByteSpan(in_s)};
255 auto input = std::string();
256 for (
size_t i=0; i<256; ++i) {
257 input.push_back(static_cast<char>(i));
261 BOOST_TEST_REQUIRE(hex.size() == 512);
262 static constexpr
auto hexmap = std::string_view(
"0123456789abcdef");
263 for (
size_t i = 0; i < 256; ++i) {
264 auto upper = hexmap.find(hex[i * 2]);
265 auto lower = hexmap.find(hex[i * 2 + 1]);
266 BOOST_TEST_REQUIRE(upper != std::string_view::npos);
267 BOOST_TEST_REQUIRE(lower != std::string_view::npos);
268 BOOST_TEST_REQUIRE(i == upper*16 + lower);
275 std::array mut_arr{uint8_t{0xaa}, uint8_t{0xbb}};
277 mut_bytes[1] = std::byte{0x11};
290 const auto op_upper = [](
const std::string&
s) {
return ToUpper(
s); };
298 const std::string original(
"A test \"%s\" string '%s'.");
299 auto test_replaceall = [&original](
const std::string& search,
const std::string& substitute,
const std::string& expected) {
300 auto test = original;
305 test_replaceall(
"",
"foo", original);
306 test_replaceall(original,
"foo",
"foo");
307 test_replaceall(
"%s",
"foo",
"A test \"foo\" string 'foo'.");
308 test_replaceall(
"\"",
"foo",
"A test foo%sfoo string '%s'.");
309 test_replaceall(
"'",
"foo",
"A test \"%s\" string foo%sfoo.");
324 BOOST_CHECK_EQUAL(
TrimString(std::string(
"\x05\x04\x03\x02\x01\x00", 6), std::string(
"\x05\x04\x03\x02\x01", 5)), std::string(
"\0", 1));
357 const std::string valid{
"2000-01-01T00:00:01Z"};
359 for (
auto mut{0U}; mut < valid.size(); ++mut) {
360 std::string invalid{valid};
446 BOOST_CHECK_EQUAL(ParseMoney("12345.").value(), COIN * 12345); 448 BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789); 450 BOOST_CHECK_EQUAL(ParseMoney("10000000.00").value(), COIN*10000000); 451 BOOST_CHECK_EQUAL(ParseMoney("1000000.00").value(), COIN*1000000); 452 BOOST_CHECK_EQUAL(ParseMoney("100000.00").value(), COIN*100000); 453 BOOST_CHECK_EQUAL(ParseMoney("10000.00").value(), COIN*10000); 454 BOOST_CHECK_EQUAL(ParseMoney("1000.00").value(), COIN*1000); 455 BOOST_CHECK_EQUAL(ParseMoney("100.00").value(), COIN*100); 456 BOOST_CHECK_EQUAL(ParseMoney("10.00").value(), COIN*10); 457 BOOST_CHECK_EQUAL(ParseMoney("1.00").value(), COIN); 458 BOOST_CHECK_EQUAL(ParseMoney("1").value(), COIN); 459 BOOST_CHECK_EQUAL(ParseMoney(" 1").value(), COIN); 460 BOOST_CHECK_EQUAL(ParseMoney("1 ").value(), COIN); 461 BOOST_CHECK_EQUAL(ParseMoney(" 1 ").value(), COIN); 462 BOOST_CHECK_EQUAL(ParseMoney("0.1").value(), COIN/10); 463 BOOST_CHECK_EQUAL(ParseMoney("0.01").value(), COIN/100); 464 BOOST_CHECK_EQUAL(ParseMoney("0.001").value(), COIN/1000); 465 BOOST_CHECK_EQUAL(ParseMoney("0.0001").value(), COIN/10000); 466 BOOST_CHECK_EQUAL(ParseMoney("0.00001").value(), COIN/100000); 467 BOOST_CHECK_EQUAL(ParseMoney("0.000001").value(), COIN/1000000); 468 BOOST_CHECK_EQUAL(ParseMoney("0.0000001").value(), COIN/10000000); 469 BOOST_CHECK_EQUAL(ParseMoney("0.00000001").value(), COIN/100000000); 470 BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001 ").value(), COIN/100000000); 471 BOOST_CHECK_EQUAL(ParseMoney("0.00000001 ").value(), COIN/100000000); 472 BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001").value(), COIN/100000000); 474 // Parsing amount that cannot be represented should fail 475 BOOST_CHECK(!ParseMoney("100000000.00")); 476 BOOST_CHECK(!ParseMoney("0.000000001")); 478 // Parsing empty string should fail 479 BOOST_CHECK(!ParseMoney("")); 480 BOOST_CHECK(!ParseMoney(" ")); 481 BOOST_CHECK(!ParseMoney(" ")); 483 // Parsing two numbers should fail 484 BOOST_CHECK(!ParseMoney("..")); 485 BOOST_CHECK(!ParseMoney("0..0")); 486 BOOST_CHECK(!ParseMoney("1 2")); 487 BOOST_CHECK(!ParseMoney(" 1 2 ")); 488 BOOST_CHECK(!ParseMoney(" 1.2 3 ")); 489 BOOST_CHECK(!ParseMoney(" 1 2.3 ")); 491 // Embedded whitespace should fail 492 BOOST_CHECK(!ParseMoney(" -1 .2 ")); 493 BOOST_CHECK(!ParseMoney(" 1 .2 ")); 494 BOOST_CHECK(!ParseMoney(" +1 .2 ")); 496 // Attempted 63 bit overflow should fail 497 BOOST_CHECK(!ParseMoney("92233720368.54775808")); 499 // Parsing negative amounts must fail 500 BOOST_CHECK(!ParseMoney("-1")); 502 // Parsing strings with embedded NUL characters should fail 503 BOOST_CHECK(!ParseMoney("\0-1"s)); 504 BOOST_CHECK(!ParseMoney(STRING_WITH_EMBEDDED_NULL_CHAR)); 505 BOOST_CHECK(!ParseMoney("1\0"s)); 508 BOOST_AUTO_TEST_CASE(util_IsHex) 510 BOOST_CHECK(IsHex("00")); 511 BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF")); 512 BOOST_CHECK(IsHex("ff")); 513 BOOST_CHECK(IsHex("FF")); 515 BOOST_CHECK(!IsHex("")); 516 BOOST_CHECK(!IsHex("0")); 517 BOOST_CHECK(!IsHex("a")); 518 BOOST_CHECK(!IsHex("eleven")); 519 BOOST_CHECK(!IsHex("00xx00")); 520 BOOST_CHECK(!IsHex("0x0000")); 523 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) 525 SeedRandomForTest(SeedRand::ZEROS); 526 for (int mod=2;mod<11;mod++) 529 // Really rough binomial confidence approximation. 530 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.); 531 //mask is 2^ceil(log2(mod))-1 532 while(mask<mod-1)mask=(mask<<1)+1; 535 //How often does it get a zero from the uniform range [0,mod)? 536 for (int i = 0; i < 10000; i++) { 539 rval=m_rng.rand32()&mask; 540 }while(rval>=(uint32_t)mod); 543 BOOST_CHECK(count<=10000/mod+err); 544 BOOST_CHECK(count>=10000/mod-err); 548 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual) 550 BOOST_CHECK(TimingResistantEqual(std::string(""), std::string(""))); 551 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string(""))); 552 BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc"))); 553 BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa"))); 554 BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a"))); 555 BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc"))); 556 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba"))); 559 /* Test strprintf formatting directives. 560 * Put a string before and after to ensure sanity of element sizes on stack. */ 561 #define B "check_prefix" 562 #define E "check_postfix" 563 BOOST_AUTO_TEST_CASE(strprintf_numbers) 565 int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */ 566 uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */ 567 BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E); 568 BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E); 569 BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E); 571 size_t st = 12345678; /* unsigned size_t test value */ 572 ssize_t sst = -12345678; /* signed size_t test value */ 573 BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E); 574 BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E); 575 BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E); 577 ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */ 578 ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */ 579 BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E); 580 BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E); 581 BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E); 586 BOOST_AUTO_TEST_CASE(util_mocktime) 589 // Check that mock time does not change after a sleep 590 for (const auto& num_sleep : {0ms, 1ms}) { 591 UninterruptibleSleep(num_sleep); 592 BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter 593 BOOST_CHECK_EQUAL(111, Now<NodeSeconds>().time_since_epoch().count()); 594 BOOST_CHECK_EQUAL(111, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now())); 595 BOOST_CHECK_EQUAL(111, TicksSinceEpoch<SecondsDouble>(Now<NodeSeconds>())); 596 BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count()); 597 BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count()); 598 BOOST_CHECK_EQUAL(111000, TicksSinceEpoch<std::chrono::milliseconds>(NodeClock::now())); 599 BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count()); 604 BOOST_AUTO_TEST_CASE(util_ticksseconds) 606 BOOST_CHECK_EQUAL(TicksSeconds(0s), 0); 607 BOOST_CHECK_EQUAL(TicksSeconds(1s), 1); 608 BOOST_CHECK_EQUAL(TicksSeconds(999ms), 0); 609 BOOST_CHECK_EQUAL(TicksSeconds(1000ms), 1); 610 BOOST_CHECK_EQUAL(TicksSeconds(1500ms), 1); 613 BOOST_AUTO_TEST_CASE(test_IsDigit) 615 BOOST_CHECK_EQUAL(IsDigit('0
'), true); 616 BOOST_CHECK_EQUAL(IsDigit('1
'), true); 617 BOOST_CHECK_EQUAL(IsDigit('8
'), true); 618 BOOST_CHECK_EQUAL(IsDigit('9
'), true); 620 BOOST_CHECK_EQUAL(IsDigit('0
' - 1), false); 621 BOOST_CHECK_EQUAL(IsDigit('9
' + 1), false); 622 BOOST_CHECK_EQUAL(IsDigit(0), false); 623 BOOST_CHECK_EQUAL(IsDigit(1), false); 624 BOOST_CHECK_EQUAL(IsDigit(8), false); 625 BOOST_CHECK_EQUAL(IsDigit(9), false); 628 /* Check for overflow */ 629 template <typename T> 630 static void TestAddMatrixOverflow() 632 constexpr T MAXI{std::numeric_limits<T>::max()}; 633 BOOST_CHECK(!CheckedAdd(T{1}, MAXI)); 634 BOOST_CHECK(!CheckedAdd(MAXI, MAXI)); 635 BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI)); 636 BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(MAXI, MAXI)); 638 BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value()); 639 BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value()); 640 BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value()); 641 BOOST_CHECK_EQUAL(MAXI - 1, CheckedAdd(T{1}, MAXI - 2).value()); 642 BOOST_CHECK_EQUAL(0, SaturatingAdd(T{0}, T{0})); 643 BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{0}, MAXI)); 644 BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI - 1)); 645 BOOST_CHECK_EQUAL(MAXI - 1, SaturatingAdd(T{1}, MAXI - 2)); 648 /* Check for overflow or underflow */ 649 template <typename T> 650 static void TestAddMatrix() 652 TestAddMatrixOverflow<T>(); 653 constexpr T MINI{std::numeric_limits<T>::min()}; 654 constexpr T MAXI{std::numeric_limits<T>::max()}; 655 BOOST_CHECK(!CheckedAdd(T{-1}, MINI)); 656 BOOST_CHECK(!CheckedAdd(MINI, MINI)); 657 BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI)); 658 BOOST_CHECK_EQUAL(MINI, SaturatingAdd(MINI, MINI)); 660 BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value()); 661 BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value()); 662 BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value()); 663 BOOST_CHECK_EQUAL(MINI + 1, CheckedAdd(T{-1}, MINI + 2).value()); 664 BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{0}, MINI)); 665 BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI + 1)); 666 BOOST_CHECK_EQUAL(MINI + 1, SaturatingAdd(T{-1}, MINI + 2)); 667 BOOST_CHECK_EQUAL(-1, SaturatingAdd(MINI, MAXI)); 670 BOOST_AUTO_TEST_CASE(util_overflow) 672 TestAddMatrixOverflow<unsigned>(); 673 TestAddMatrix<signed>(); 676 template <typename T> 677 static void RunToIntegralTests() 679 BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR)); 680 BOOST_CHECK(!ToIntegral<T>(" 1")); 681 BOOST_CHECK(!ToIntegral<T>("1 ")); 682 BOOST_CHECK(!ToIntegral<T>("1a")); 683 BOOST_CHECK(!ToIntegral<T>("1.1")); 684 BOOST_CHECK(!ToIntegral<T>("1.9")); 685 BOOST_CHECK(!ToIntegral<T>("+01.9")); 686 BOOST_CHECK(!ToIntegral<T>("-")); 687 BOOST_CHECK(!ToIntegral<T>("+")); 688 BOOST_CHECK(!ToIntegral<T>(" -1")); 689 BOOST_CHECK(!ToIntegral<T>("-1 ")); 690 BOOST_CHECK(!ToIntegral<T>(" -1 ")); 691 BOOST_CHECK(!ToIntegral<T>("+1")); 692 BOOST_CHECK(!ToIntegral<T>(" +1")); 693 BOOST_CHECK(!ToIntegral<T>(" +1 ")); 694 BOOST_CHECK(!ToIntegral<T>("+-1")); 695 BOOST_CHECK(!ToIntegral<T>("-+1")); 696 BOOST_CHECK(!ToIntegral<T>("++1")); 697 BOOST_CHECK(!ToIntegral<T>("--1")); 698 BOOST_CHECK(!ToIntegral<T>("")); 699 BOOST_CHECK(!ToIntegral<T>("aap")); 700 BOOST_CHECK(!ToIntegral<T>("0x1")); 701 BOOST_CHECK(!ToIntegral<T>("-32482348723847471234")); 702 BOOST_CHECK(!ToIntegral<T>("32482348723847471234")); 705 BOOST_AUTO_TEST_CASE(test_ToIntegral) 707 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
710 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000001234").value(), 1'234);
712 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000000000").value(), 0); 713 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-00000000000000000000").value(), 0); 714 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
717 RunToIntegralTests<uint64_t>();
718 RunToIntegralTests<int64_t>();
719 RunToIntegralTests<uint32_t>();
720 RunToIntegralTests<int32_t>();
721 RunToIntegralTests<uint16_t>();
722 RunToIntegralTests<int16_t>();
723 RunToIntegralTests<uint8_t>();
724 RunToIntegralTests<int8_t>();
726 BOOST_CHECK(!ToIntegral<int64_t>(
"-9223372036854775809"));
727 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"-9223372036854775808").value(), -9
'223'372
'036'854
'775'807LL - 1LL);
728 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"9223372036854775807").value(), 9
'223'372
'036'854
'775'807);
729 BOOST_CHECK(!ToIntegral<int64_t>(
"9223372036854775808"));
733 BOOST_CHECK_EQUAL(ToIntegral<uint64_t>(
"18446744073709551615").value(), 18
'446'744
'073'709
'551'615ULL);
734 BOOST_CHECK(!ToIntegral<uint64_t>(
"18446744073709551616"));
738 BOOST_CHECK_EQUAL(ToIntegral<int32_t>("2147483647").value(), 2'147
'483'647);
744 BOOST_CHECK(!ToIntegral<uint32_t>("4294967296")); 746 BOOST_CHECK(!ToIntegral<int16_t>("-32769")); 747 BOOST_CHECK_EQUAL(ToIntegral<int16_t>("-32768").value(), -32'768);
749 BOOST_CHECK(!ToIntegral<int16_t>("32768")); 751 BOOST_CHECK(!ToIntegral<uint16_t>("-1")); 752 BOOST_CHECK_EQUAL(ToIntegral<uint16_t>("0").value(), 0U); 753 BOOST_CHECK_EQUAL(ToIntegral<uint16_t>("65535").value(), 65'535U);
769 return strtoll(str.c_str(),
nullptr, 10);
775 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0"), 0); 776 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("01234"), 1'234);
778 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" 1"), 1); 779 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1 "), 1); 780 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1a"), 1); 781 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.1"), 1); 782 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.9"), 1); 783 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+01.9"), 1); 784 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1"), -1); 785 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1"), -1); 786 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1 "), -1); 787 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1 "), -1); 788 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+1"), 1); 789 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1"), 1); 790 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1 "), 1); 792 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+-1"), 0); 793 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-+1"), 0); 794 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("++1"), 0); 795 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("--1"), 0); 796 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(""), 0); 797 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("aap"), 0); 798 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0x1"), 0); 799 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), -2'147
'483'647 - 1);
800 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"32482348723847471234"), 2
'147'483
'647); 802 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), -9'223
'372'036
'854'775
'807LL - 1LL); 803 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775808"), -9'223
'372'036
'854'775
'807LL - 1LL); 804 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775807"), 9'223
'372'036
'854'775
'807); 805 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 9'223
'372'036
'854'775
'807); 807 std::map<std::string, int64_t> atoi64_test_pairs = { 808 {"-9223372036854775809", std::numeric_limits<int64_t>::min()}, 809 {"-9223372036854775808", -9'223
'372'036
'854'775
'807LL - 1LL}, 810 {"9223372036854775807", 9'223
'372'036
'854'775
'807}, 811 {"9223372036854775808", std::numeric_limits<int64_t>::max()}, 818 for (const auto& pair : atoi64_test_pairs) { 819 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), pair.second); 822 // Ensure legacy compatibility with previous versions of Bitcoin Core's atoi64
823 for (
const auto& pair : atoi64_test_pairs) {
829 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551615"), 18
'446'744
'073'709
'551'615ULL);
830 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551616"), 18
'446'744
'073'709
'551'615ULL);
832 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-2147483649"), -2
'147'483
'648LL); 833 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483648"), -2'147
'483'648LL);
835 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 2'147
'483'647);
839 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>(
"4294967295"), 4
'294'967
'295U); 840 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 4'294
'967'295U);
843 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32768"), -32'768);
845 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 32'767);
850 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 65'535U);
865 std::optional<uint64_t> n;
867 n = ToIntegral<uint64_t>(
"1234", 16);
869 n = ToIntegral<uint64_t>(
"a", 16);
871 n = ToIntegral<uint64_t>(
"0000000a", 16);
873 n = ToIntegral<uint64_t>(
"100", 16);
875 n = ToIntegral<uint64_t>(
"DEADbeef", 16);
877 n = ToIntegral<uint64_t>(
"FfFfFfFf", 16);
879 n = ToIntegral<uint64_t>(
"123456789", 16);
881 n = ToIntegral<uint64_t>(
"0", 16);
883 n = ToIntegral<uint64_t>(
"FfFfFfFfFfFfFfFf", 16);
885 n = ToIntegral<int64_t>(
"-1", 16);
893 BOOST_CHECK(!ToIntegral<uint64_t>(
"FfFfFfFfFfFfFfFf0", 16));
909 BOOST_CHECK_EQUAL(
FormatParagraph(
"This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length until it gets here", 79),
"This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length\nuntil it gets here");
912 BOOST_CHECK_EQUAL(
FormatParagraph(
"a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79),
"a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
913 BOOST_CHECK_EQUAL(
FormatParagraph(
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79),
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
915 BOOST_CHECK_EQUAL(
FormatParagraph(
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg h i j k", 79, 4),
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\n f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg\n h i j k");
917 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string. This is a second sentence in the very long test string.", 79),
"This is a very long test string. This is a second sentence in the very long\ntest string.");
918 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string.\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79),
"This is a very long test string.\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
919 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79),
"This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
920 BOOST_CHECK_EQUAL(
FormatParagraph(
"Testing that normal newlines do not get indented.\nLike here.", 79),
"Testing that normal newlines do not get indented.\nLike here.");
925 std::vector<std::string> comments;
926 comments.emplace_back(
"comment1");
927 std::vector<std::string> comments2;
928 comments2.emplace_back(
"comment1");
1009 #ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork() 1024 int rv = read(fd, &ch, 1);
1036 rv = write(fd, &ch, 1);
1042 rv = write(fd, &ch, 1);
1057 fs::path dirname = m_args.GetDataDirBase() /
"lock_dir";
1081 fs::create_directories(dirname);
1141 fs::remove_all(dirname);
1179 static std::string
SpanToStr(
const std::span<const char>& span)
1181 return std::string(span.begin(), span.end());
1188 std::span<const char> sp;
1192 input =
"MilkToastHoney";
1194 success =
Const(
"", sp);
1198 success =
Const(
"Milk", sp,
false);
1202 success =
Const(
"Milk", sp);
1206 success =
Const(
"Bread", sp,
false);
1209 success =
Const(
"Bread", sp);
1212 success =
Const(
"Toast", sp,
false);
1216 success =
Const(
"Toast", sp);
1220 success =
Const(
"Honeybadger", sp);
1223 success =
Const(
"Honey", sp,
false);
1227 success =
Const(
"Honey", sp);
1231 input =
"Foo(Bar(xy,z()))";
1234 success =
Func(
"FooBar", sp);
1237 success =
Func(
"Foo(", sp);
1240 success =
Func(
"Foo", sp);
1244 success =
Func(
"Bar", sp);
1248 success =
Func(
"xy", sp);
1252 std::span<const char>
result;
1254 input =
"(n*(n-1))/2";
1266 input =
"(aaaaa,bbbbb()),c";
1278 input =
"((a),(b),(c)),xxx";
1285 std::vector<std::span<const char>> results;
1288 results =
Split(input,
'x');
1295 input =
"one#two#three";
1296 results =
Split(input,
'-');
1300 input =
"one#two#three";
1301 results =
Split(input,
'#');
1307 results =
Split(input,
'#',
true);
1313 input =
"*foo*bar*";
1314 results =
Split(input,
'*');
1321 results =
Split(input,
'*',
true);
1379 using V = std::vector<std::string>;
1380 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;") == V({
"a",
"b.c:d",
"e"}));
1381 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;:.") == V({
"a",
"b",
"c",
"d",
"e"}));
1382 BOOST_TEST(
SplitString(
"a,b.c:d;e",
"") == V({
"a,b.c:d;e"}));
1383 BOOST_TEST(
SplitString(
"aaa",
"bcdefg") == V({
"aaa"}));
1384 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0"s) == V({
"x",
"a,b"}));
1385 BOOST_TEST(
SplitString(
"x\0a,b"s,
'\0') == V({
"x",
"a,b"}));
1386 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0,"s) == V({
"x",
"a",
"b"}));
1387 BOOST_TEST(
SplitString(
"abcdefg",
"bcd") == V({
"a",
"",
"",
"efg"}));
1400 const std::string NUL(
"O\x00O", 3);
1409 const Tracker* origin;
1413 Tracker() noexcept : origin(this) {}
1414 Tracker(
const Tracker& t) noexcept : origin(
t.origin), copies(
t.copies + 1) {}
1415 Tracker(Tracker&& t) noexcept : origin(
t.origin), copies(
t.copies) {}
1416 Tracker& operator=(
const Tracker& t) noexcept
1420 copies =
t.copies + 1;
1443 auto v2 =
Vector(std::move(t2));
1448 auto v3 =
Vector(t1, std::move(t2));
1455 auto v4 =
Vector(std::move(v3[0]), v3[1], std::move(t3));
1464 auto v5 =
Cat(v1, v4);
1475 auto v6 =
Cat(std::move(v1), v3);
1484 auto v7 =
Cat(v2, std::move(v4));
1495 auto v8 =
Cat(std::move(v2), std::move(v3));
1507 const std::array<unsigned char, 32> privkey_bytes = {
1510 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
1511 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
1512 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
1513 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
1516 const std::string message =
"Trust no one";
1518 const std::string expected_signature =
1519 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
1522 std::string generated_signature;
1524 BOOST_REQUIRE_MESSAGE(!privkey.
IsValid(),
1525 "Confirm the private key is invalid");
1527 BOOST_CHECK_MESSAGE(!
MessageSign(privkey, message, generated_signature),
1528 "Sign with an invalid private key");
1530 privkey.
Set(privkey_bytes.begin(), privkey_bytes.end(),
true);
1532 BOOST_REQUIRE_MESSAGE(privkey.
IsValid(),
1533 "Confirm the private key is valid");
1535 BOOST_CHECK_MESSAGE(
MessageSign(privkey, message, generated_signature),
1536 "Sign with a valid private key");
1546 "signature should be irrelevant",
1552 "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
1553 "signature should be irrelevant",
1559 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1560 "invalid signature, not in base64 encoding",
1561 "message should be irrelevant"),
1566 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1567 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
1568 "message should be irrelevant"),
1573 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1574 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1575 "I never signed this"),
1580 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1581 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1587 "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
1588 "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
1595 const std::string unsigned_tx =
"...";
1596 const std::string prefixed_message =
1599 std::string(1, (
char)unsigned_tx.length()) +
1603 const uint256 message_hash1 =
Hash(prefixed_message);
1607 BOOST_CHECK_NE(message_hash1, signature_hash);
1638 BOOST_CHECK_EQUAL(ParseByteUnits("3G", noop).value(), 3ULL << 30); 1640 BOOST_CHECK_EQUAL(ParseByteUnits("4t", noop).value(), 4'000
'000'000
'000ULL); 1641 BOOST_CHECK_EQUAL(ParseByteUnits("4T", noop).value(), 4ULL << 40); 1643 // check default multiplier 1644 BOOST_CHECK_EQUAL(ParseByteUnits("5", ByteUnit::K).value(), 5ULL << 10); 1647 BOOST_CHECK(!ParseByteUnits("", noop)); 1648 BOOST_CHECK(!ParseByteUnits("foo", noop)); 1651 BOOST_CHECK(!ParseByteUnits("123m ", noop)); 1652 BOOST_CHECK(!ParseByteUnits(" 123m", noop)); 1655 BOOST_CHECK(!ParseByteUnits("-123m", noop)); 1656 BOOST_CHECK(!ParseByteUnits("+123m", noop)); 1659 BOOST_CHECK_EQUAL(ParseByteUnits("020M", noop).value(), 20ULL << 20); 1661 // fractions not allowed 1662 BOOST_CHECK(!ParseByteUnits("0.5T", noop)); 1665 BOOST_CHECK(!ParseByteUnits("18446744073709551615g", noop)); 1668 BOOST_CHECK(!ParseByteUnits("1x", noop)); 1671 BOOST_AUTO_TEST_CASE(util_ReadBinaryFile) 1673 fs::path tmpfolder = m_args.GetDataDirBase(); 1674 fs::path tmpfile = tmpfolder / "read_binary.dat"; 1675 std::string expected_text; 1676 for (int i = 0; i < 30; i++) { 1677 expected_text += "0123456789"; 1680 std::ofstream file{tmpfile.std_path()}; 1681 file << expected_text; 1684 // read all contents in file 1685 auto [valid, text] = ReadBinaryFile(tmpfile); 1687 BOOST_CHECK_EQUAL(text, expected_text); 1690 // read half contents in file 1691 auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); 1693 BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2)); 1696 // read from non-existent file 1697 fs::path invalid_file = tmpfolder / "invalid_binary.dat"; 1698 auto [valid, text] = ReadBinaryFile(invalid_file); 1699 BOOST_CHECK(!valid); 1700 BOOST_CHECK(text.empty()); 1704 BOOST_AUTO_TEST_CASE(util_WriteBinaryFile) 1706 fs::path tmpfolder = m_args.GetDataDirBase(); 1707 fs::path tmpfile = tmpfolder / "write_binary.dat"; 1708 std::string expected_text = "bitcoin"; 1709 auto valid = WriteBinaryFile(tmpfile, expected_text); 1710 std::string actual_text; 1711 std::ifstream file{tmpfile.std_path()}; 1712 file >> actual_text; 1714 BOOST_CHECK_EQUAL(actual_text, expected_text); 1717 BOOST_AUTO_TEST_CASE(clearshrink_test) 1720 std::vector<uint8_t> v = {1, 2, 3}; 1722 BOOST_CHECK_EQUAL(v.size(), 0); 1723 BOOST_CHECK_EQUAL(v.capacity(), 0); 1727 std::vector<bool> v = {false, true, false, false, true, true}; 1729 BOOST_CHECK_EQUAL(v.size(), 0); 1730 BOOST_CHECK_EQUAL(v.capacity(), 0); 1734 std::deque<int> v = {1, 3, 3, 7}; 1736 BOOST_CHECK_EQUAL(v.size(), 0); 1737 // std::deque has no capacity() we can observe. 1741 template <typename T> 1742 void TestCheckedLeftShift() 1744 constexpr auto MAX{std::numeric_limits<T>::max()}; 1747 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(0, 1), 0); 1748 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(0, 127), 0); 1749 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(1, 1), 2); 1750 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(2, 2), 8); 1751 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(MAX >> 1, 1), MAX - 1); 1754 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1); 1757 BOOST_CHECK(!CheckedLeftShift<T>((MAX >> 1) + 1, 1)); 1758 BOOST_CHECK(!CheckedLeftShift<T>(MAX, 1)); 1759 BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits)); 1760 BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits + 1)); 1762 if constexpr (std::is_signed_v<T>) { 1763 constexpr auto MIN{std::numeric_limits<T>::min()}; 1765 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(-1, 1), -2); 1766 BOOST_CHECK_EQUAL(CheckedLeftShift<T>((MIN >> 2), 1), MIN / 2); 1767 BOOST_CHECK_EQUAL(CheckedLeftShift<T>((MIN >> 1) + 1, 1), MIN + 2); 1768 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(MIN >> 1, 1), MIN); 1769 // Overflow negative 1770 BOOST_CHECK(!CheckedLeftShift<T>((MIN >> 1) - 1, 1)); 1771 BOOST_CHECK(!CheckedLeftShift<T>(MIN >> 1, 2)); 1772 BOOST_CHECK(!CheckedLeftShift<T>(-1, 100)); 1776 template <typename T> 1777 void TestSaturatingLeftShift() 1779 constexpr auto MAX{std::numeric_limits<T>::max()}; 1782 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(0, 1), 0); 1783 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(0, 127), 0); 1784 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, 1), 2); 1785 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(2, 2), 8); 1786 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MAX >> 1, 1), MAX - 1); 1789 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1); 1792 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MAX >> 1) + 1, 1), MAX); 1793 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MAX, 1), MAX); 1794 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits), MAX); 1795 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits + 1), MAX); 1797 if constexpr (std::is_signed_v<T>) { 1798 constexpr auto MIN{std::numeric_limits<T>::min()}; 1800 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(-1, 1), -2); 1801 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 2), 1), MIN / 2); 1802 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 1) + 1, 1), MIN + 2); 1803 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MIN >> 1, 1), MIN); 1804 // Saturation negative 1805 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 1) - 1, 1), MIN); 1806 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MIN >> 1, 2), MIN); 1807 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(-1, 100), MIN); 1811 BOOST_AUTO_TEST_CASE(checked_left_shift_test) 1813 TestCheckedLeftShift<uint8_t>(); 1814 TestCheckedLeftShift<int8_t>(); 1815 TestCheckedLeftShift<size_t>(); 1816 TestCheckedLeftShift<uint64_t>(); 1817 TestCheckedLeftShift<int64_t>(); 1820 BOOST_AUTO_TEST_CASE(saturating_left_shift_test) 1822 TestSaturatingLeftShift<uint8_t>(); 1823 TestSaturatingLeftShift<int8_t>(); 1824 TestSaturatingLeftShift<size_t>(); 1825 TestSaturatingLeftShift<uint64_t>(); 1826 TestSaturatingLeftShift<int64_t>(); 1829 BOOST_AUTO_TEST_CASE(mib_string_literal_test) 1831 BOOST_CHECK_EQUAL(0_MiB, 0); 1832 BOOST_CHECK_EQUAL(1_MiB, 1024 * 1024); 1833 const auto max_mib{std::numeric_limits<size_t>::max() >> 20}; 1834 BOOST_CHECK_EXCEPTION(operator""_MiB(static_cast<unsigned long long>(max_mib) + 1), std::overflow_error, HasReason("MiB value too large for size_t byte conversion")); 1837 BOOST_AUTO_TEST_SUITE_END() void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
constexpr uint8_t HEX_PARSE_OUTPUT[]
auto MakeByteSpan(const V &v) noexcept
std::vector< std::string > SplitString(std::string_view str, char sep)
std::span< const char > Expr(std::span< const char > &sp)
Extract the expression that sp begins with.
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span
Like the std::span constructor, but for (const) unsigned char member types only.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
#define TRY_LOCK(cs, name)
A public key could not be recovered from the provided signature and message.
The message verification was successful.
The provided address is invalid.
The provided signature couldn't be parsed (maybe invalid base64).
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
bool Const(const std::string &str, std::span< const char > &sp, bool skip)
Parse a constant.
constexpr char HEX_PARSE_INPUT[]
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
std::string RemovePrefix(std::string_view str, std::string_view prefix)
std::string FormatISO8601Date(int64_t nTime)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
int64_t atoi64_legacy(const std::string &str)
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
bool Func(const std::string &str, std::span< const char > &sp)
Parse a function call.
BOOST_AUTO_TEST_CASE(util_check)
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
int64_t CAmount
Amount in satoshis (Can be negative)
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
static constexpr char ExitCommand
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR
std::optional< uint64_t > ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.
LockResult LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
void ReleaseDirectoryLocks()
Release all directory locks.
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line...
static constexpr char LockCommand
auto MakeWritableByteSpan(V &&v) noexcept
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
#define Assume(val)
Assume is the identity function.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
std::string FormatRFC1123DateTime(int64_t time)
RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 Used in HTTP/1.1 responses.
#define BOOST_CHECK_EQUAL(v1, v2)
The message was not signed with the private key of the provided address.
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
Format the subversion field according to BIP 14 spec (https://github.com/bitcoin/bips/blob/master/bip...
The provided address is valid but does not refer to a public key.
static void TestOtherProcess(fs::path dirname, fs::path lockname, int fd)
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
An encapsulated private key.
static std::string SpanToStr(const std::span< const char > &span)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static constexpr char UnlockCommand
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
#define Assert(val)
Identity function.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
bool IsValid() const
Check whether this private key is valid.
#define BOOST_CHECK(expr)
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
std::vector< T > Split(const std::span< const char > &sp, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.