38 #include <sys/types.h>
45 #include <boost/test/unit_test.hpp>
47 using namespace std::literals;
62 explicit NoCopyOrMove(
int i) : i{i} { }
64 NoCopyOrMove() =
delete;
65 NoCopyOrMove(
const NoCopyOrMove&) =
delete;
66 NoCopyOrMove(NoCopyOrMove&&) =
delete;
67 NoCopyOrMove& operator=(
const NoCopyOrMove&) =
delete;
68 NoCopyOrMove& operator=(NoCopyOrMove&&) =
delete;
70 operator bool()
const {
return i != 0; }
72 int get_ip1() {
return i + 1; }
76 [&]() {
Assume(get_ip1()); }();
77 return Assume(get_ip1() != 5);
85 const std::unique_ptr<int> p_two =
Assert(std::make_unique<int>(2));
87 const int two = *
Assert(p_two);
91 const bool result{
Assume(two == 2)};
104 const int nine{*
Assert(std::optional<int>{9})};
116 BOOST_ERROR(
"break was swallowed!");
126 BOOST_ERROR(
"break was swallowed!");
131 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
132 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
133 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
134 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
139 std::vector<unsigned char> result;
142 result =
ParseHex(
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
143 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
144 result = TryParseHex<uint8_t>(
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value();
145 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
149 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
150 result = TryParseHex<uint8_t>(
"12 34 56 78").value();
151 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
155 BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
156 result = TryParseHex<uint8_t>(
" 89 34 56 78").value();
157 BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
161 BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
162 result = TryParseHex<uint8_t>(
" Ff aA ").value();
163 BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
168 result = TryParseHex<uint8_t>(
"").value();
176 const std::string with_embedded_null{
" 11 "s
196 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
210 const std::string out_exp{
"04678afdb0"};
218 auto input = std::string();
219 for (
size_t i=0; i<256; ++i) {
220 input.push_back(
static_cast<char>(i));
224 BOOST_TEST_REQUIRE(hex.size() == 512);
225 static constexpr
auto hexmap = std::string_view(
"0123456789abcdef");
226 for (
size_t i = 0; i < 256; ++i) {
227 auto upper = hexmap.find(hex[i * 2]);
228 auto lower = hexmap.find(hex[i * 2 + 1]);
229 BOOST_TEST_REQUIRE(upper != std::string_view::npos);
230 BOOST_TEST_REQUIRE(lower != std::string_view::npos);
231 BOOST_TEST_REQUIRE(i == upper*16 + lower);
238 std::array mut_arr{uint8_t{0xaa}, uint8_t{0xbb}};
240 mut_bytes[1] = std::byte{0x11};
253 const auto op_upper = [](
const std::string& s) {
return ToUpper(s); };
261 const std::string original(
"A test \"%s\" string '%s'.");
262 auto test_replaceall = [&original](
const std::string& search,
const std::string& substitute,
const std::string& expected) {
263 auto test = original;
268 test_replaceall(
"",
"foo", original);
269 test_replaceall(original,
"foo",
"foo");
270 test_replaceall(
"%s",
"foo",
"A test \"foo\" string 'foo'.");
271 test_replaceall(
"\"",
"foo",
"A test foo%sfoo string '%s'.");
272 test_replaceall(
"'",
"foo",
"A test \"%s\" string foo%sfoo.");
287 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));
454 for (
int mod=2;mod<11;mod++)
458 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
460 while(mask<mod-1)mask=(mask<<1)+1;
464 for (
int i = 0; i < 10000; i++) {
468 }
while(rval>=(uint32_t)mod);
489 #define B "check_prefix"
490 #define E "check_postfix"
493 int64_t s64t = -9223372036854775807LL;
494 uint64_t u64t = 18446744073709551615ULL;
499 size_t st = 12345678;
500 ssize_t sst = -12345678;
505 ptrdiff_t pt = 87654321;
506 ptrdiff_t spt = -87654321;
526 for (
const auto& num_sleep : {0ms, 1ms}) {
540 const auto steady_ms_0 = Now<SteadyMilliseconds>();
541 const auto steady_0 = std::chrono::steady_clock::now();
542 const auto ms_0 = GetTime<std::chrono::milliseconds>();
543 const auto us_0 = GetTime<std::chrono::microseconds>();
545 BOOST_CHECK(steady_ms_0 < Now<SteadyMilliseconds>());
546 BOOST_CHECK(steady_0 + 1ms <= std::chrono::steady_clock::now());
547 BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
548 BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
567 template <
typename T>
570 constexpr
T MAXI{std::numeric_limits<T>::max()};
587 template <
typename T>
590 TestAddMatrixOverflow<T>();
591 constexpr
T MINI{std::numeric_limits<T>::min()};
592 constexpr
T MAXI{std::numeric_limits<T>::max()};
610 TestAddMatrixOverflow<unsigned>();
611 TestAddMatrix<signed>();
648 template <
typename T>
673 BOOST_CHECK(!ToIntegral<T>(
"-32482348723847471234"));
674 BOOST_CHECK(!ToIntegral<T>(
"32482348723847471234"));
689 RunToIntegralTests<uint64_t>();
690 RunToIntegralTests<int64_t>();
691 RunToIntegralTests<uint32_t>();
692 RunToIntegralTests<int32_t>();
693 RunToIntegralTests<uint16_t>();
694 RunToIntegralTests<int16_t>();
695 RunToIntegralTests<uint8_t>();
696 RunToIntegralTests<int8_t>();
698 BOOST_CHECK(!ToIntegral<int64_t>(
"-9223372036854775809"));
699 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);
700 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"9223372036854775807").value(), 9'223'372'036'854'775'807);
701 BOOST_CHECK(!ToIntegral<int64_t>(
"9223372036854775808"));
705 BOOST_CHECK_EQUAL(ToIntegral<uint64_t>(
"18446744073709551615").value(), 18'446'744'073'709'551'615ULL);
706 BOOST_CHECK(!ToIntegral<uint64_t>(
"18446744073709551616"));
741 return strtoll(str.c_str(),
nullptr, 10);
771 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-32482348723847471234"), -2'147'483'647 - 1);
772 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"32482348723847471234"), 2'147'483'647);
774 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"-9223372036854775809"), -9'223'372'036'854'775'807LL - 1LL);
775 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
776 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"9223372036854775807"), 9'223'372'036'854'775'807);
777 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"9223372036854775808"), 9'223'372'036'854'775'807);
779 std::map<std::string, int64_t> atoi64_test_pairs = {
780 {
"-9223372036854775809", std::numeric_limits<int64_t>::min()},
781 {
"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL},
782 {
"9223372036854775807", 9'223'372'036'854'775'807},
783 {
"9223372036854775808", std::numeric_limits<int64_t>::max()},
790 for (
const auto& pair : atoi64_test_pairs) {
795 for (
const auto& pair : atoi64_test_pairs) {
801 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551615"), 18'446'744'073'709'551'615ULL);
802 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551616"), 18'446'744'073'709'551'615ULL);
804 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-2147483649"), -2'147'483'648LL);
805 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-2147483648"), -2'147'483'648LL);
811 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>(
"4294967295"), 4'294'967'295U);
812 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>(
"4294967296"), 4'294'967'295U);
1013 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");
1016 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");
1017 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");
1019 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");
1021 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.");
1022 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.");
1023 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.");
1024 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.");
1029 std::vector<std::string> comments;
1030 comments.emplace_back(
"comment1");
1031 std::vector<std::string> comments2;
1032 comments2.emplace_back(
"comment1");
1128 int rv = read(fd, &ch, 1);
1140 rv = write(fd, &ch, 1);
1146 rv = write(fd, &ch, 1);
1161 fs::path dirname = m_args.GetDataDirBase() /
"lock_dir";
1167 void (*old_handler)(int) = signal(SIGCHLD, SIG_DFL);
1247 signal(SIGCHLD, old_handler);
1252 fs::remove_all(dirname);
1292 return std::string(span.
begin(), span.
end());
1303 input =
"MilkToastHoney";
1305 success =
Const(
"", sp);
1309 success =
Const(
"Milk", sp);
1313 success =
Const(
"Bread", sp);
1316 success =
Const(
"Toast", sp);
1320 success =
Const(
"Honeybadger", sp);
1323 success =
Const(
"Honey", sp);
1328 input =
"Foo(Bar(xy,z()))";
1331 success =
Func(
"FooBar", sp);
1334 success =
Func(
"Foo(", sp);
1337 success =
Func(
"Foo", sp);
1341 success =
Func(
"Bar", sp);
1345 success =
Func(
"xy", sp);
1351 input =
"(n*(n-1))/2";
1363 input =
"(aaaaa,bbbbb()),c";
1375 input =
"((a),(b),(c)),xxx";
1382 std::vector<Span<const char>> results;
1385 results =
Split(input,
'x');
1392 input =
"one#two#three";
1393 results =
Split(input,
'-');
1397 input =
"one#two#three";
1398 results =
Split(input,
'#');
1404 input =
"*foo*bar*";
1405 results =
Split(input,
'*');
1417 std::vector<std::string> result =
SplitString(
"",
'-');
1424 std::vector<std::string> result =
SplitString(
"-",
'-');
1432 std::vector<std::string> result =
SplitString(
"--",
'-');
1441 std::vector<std::string> result =
SplitString(
"abc",
'-');
1448 std::vector<std::string> result =
SplitString(
"a-b",
'-');
1456 std::vector<std::string> result =
SplitString(
"AAA",
'a');
1463 using V = std::vector<std::string>;
1464 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;") == V({
"a",
"b.c:d",
"e"}));
1465 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;:.") == V({
"a",
"b",
"c",
"d",
"e"}));
1466 BOOST_TEST(
SplitString(
"a,b.c:d;e",
"") == V({
"a,b.c:d;e"}));
1467 BOOST_TEST(
SplitString(
"aaa",
"bcdefg") == V({
"aaa"}));
1468 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0"s) == V({
"x",
"a,b"}));
1469 BOOST_TEST(
SplitString(
"x\0a,b"s,
'\0') == V({
"x",
"a,b"}));
1470 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0,"s) == V({
"x",
"a",
"b"}));
1471 BOOST_TEST(
SplitString(
"abcdefg",
"bcd") == V({
"a",
"",
"",
"efg"}));
1484 const std::string NUL(
"O\x00O", 3);
1493 const Tracker* origin;
1497 Tracker() noexcept : origin(this) {}
1498 Tracker(
const Tracker& t) noexcept : origin(
t.origin), copies(
t.copies + 1) {}
1499 Tracker(Tracker&& t) noexcept : origin(
t.origin), copies(
t.copies) {}
1500 Tracker& operator=(
const Tracker& t) noexcept
1503 copies =
t.copies + 1;
1525 auto v2 =
Vector(std::move(t2));
1530 auto v3 =
Vector(t1, std::move(t2));
1537 auto v4 =
Vector(std::move(v3[0]), v3[1], std::move(t3));
1546 auto v5 =
Cat(v1, v4);
1557 auto v6 =
Cat(std::move(v1), v3);
1566 auto v7 =
Cat(v2, std::move(v4));
1577 auto v8 =
Cat(std::move(v2), std::move(v3));
1589 const std::array<unsigned char, 32> privkey_bytes = {
1592 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
1593 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
1594 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
1595 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
1598 const std::string message =
"Trust no one";
1600 const std::string expected_signature =
1601 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
1604 std::string generated_signature;
1606 BOOST_REQUIRE_MESSAGE(!privkey.
IsValid(),
1607 "Confirm the private key is invalid");
1609 BOOST_CHECK_MESSAGE(!
MessageSign(privkey, message, generated_signature),
1610 "Sign with an invalid private key");
1612 privkey.
Set(privkey_bytes.begin(), privkey_bytes.end(),
true);
1614 BOOST_REQUIRE_MESSAGE(privkey.
IsValid(),
1615 "Confirm the private key is valid");
1617 BOOST_CHECK_MESSAGE(
MessageSign(privkey, message, generated_signature),
1618 "Sign with a valid private key");
1628 "signature should be irrelevant",
1634 "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
1635 "signature should be irrelevant",
1641 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1642 "invalid signature, not in base64 encoding",
1643 "message should be irrelevant"),
1648 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1649 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
1650 "message should be irrelevant"),
1655 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1656 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1657 "I never signed this"),
1662 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1663 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1669 "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
1670 "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
1677 const std::string unsigned_tx =
"...";
1678 const std::string prefixed_message =
1681 std::string(1, (
char)unsigned_tx.length()) +
1685 const uint256 message_hash1 =
Hash(prefixed_message);
1689 BOOST_CHECK_NE(message_hash1, signature_hash);
1755 fs::path tmpfolder = m_args.GetDataDirBase();
1756 fs::path tmpfile = tmpfolder /
"read_binary.dat";
1757 std::string expected_text;
1758 for (
int i = 0; i < 30; i++) {
1759 expected_text +=
"0123456789";
1762 std::ofstream file{tmpfile};
1763 file << expected_text;
1773 auto [valid, text] =
ReadBinaryFile(tmpfile, expected_text.size() / 2);
1779 fs::path invalid_file = tmpfolder /
"invalid_binary.dat";
1788 fs::path tmpfolder = m_args.GetDataDirBase();
1789 fs::path tmpfile = tmpfolder /
"write_binary.dat";
1790 std::string expected_text =
"bitcoin";
1792 std::string actual_text;
1793 std::ifstream file{tmpfile};
1794 file >> actual_text;
1802 std::vector<uint8_t> v = {1, 2, 3};
1809 std::vector<bool> v = {
false,
true,
false,
false,
true,
true};
1816 std::deque<int> v = {1, 3, 3, 7};
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
A Span is an object that can refer to a contiguous sequence of objects.
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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...
BOOST_AUTO_TEST_SUITE_END()
void ReleaseDirectoryLocks()
Release all directory locks.
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
#define T(expected, seed, data)
@ ERR_MALFORMED_SIGNATURE
The provided signature couldn't be parsed (maybe invalid base64).
@ ERR_INVALID_ADDRESS
The provided address is invalid.
@ ERR_ADDRESS_NO_KEY
The provided address is valid but does not refer to a public key.
@ ERR_NOT_SIGNED
The message was not signed with the private key of the provided address.
@ OK
The message verification was successful.
@ ERR_PUBKEY_NOT_RECOVERED
A public key could not be recovered from the provided signature and message.
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
std::string LogEscapeMessage(const std::string &str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Span< const char > Expr(Span< const char > &sp)
Extract the expression that sp begins with.
bool Const(const std::string &str, Span< const char > &sp)
Parse a constant.
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
std::vector< T > Split(const Span< const char > &sp, std::string_view separators)
Split a string on any char found in separators, returning a vector.
LockResult LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::optional< T > CheckedAdd(const T i, const T j) noexcept
T SaturatingAdd(const T i, const T j) noexcept
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
Write contents of std::string to a file.
std::pair< bool, std::string > ReadBinaryFile(const fs::path &filename, size_t maxsize)
Read full contents of a file and return them in a std::string.
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string RemovePrefix(std::string_view str, std::string_view prefix)
std::vector< std::string > SplitString(std::string_view str, char sep)
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static time_point now() noexcept
Return current system time or mocked time, if set.
#define TRY_LOCK(cs, name)
@ ZEROS
Seed with a compile time constant of zeros.
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
static uint32_t InsecureRand32()
void UninterruptibleSleep(const std::chrono::microseconds &n)
std::string FormatISO8601Date(int64_t nTime)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
bool IsHexNumber(std::string_view str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
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::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool IsHex(std::string_view str)
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.
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
int64_t atoi64_legacy(const std::string &str)
static void TestOtherProcess(fs::path dirname, fs::path lockname, int fd)
static constexpr char ExitCommand
static constexpr char UnlockCommand
static void TestAddMatrixOverflow()
static void TestAddMatrix()
BOOST_AUTO_TEST_CASE(util_check)
static const unsigned char ParseHex_expected[65]
static void RunToIntegralTests()
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR
static constexpr char LockCommand
static std::string SpanToStr(const Span< const char > &span)
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
void ClearShrink(V &v) noexcept
Clear a vector (or std::deque) and release its allocated memory.