52#include <boost/test/unit_test.hpp>
54using namespace std::literals;
75 explicit NoCopyOrMove(
int i) : i{i} { }
77 NoCopyOrMove() =
delete;
78 NoCopyOrMove(
const NoCopyOrMove&) =
delete;
79 NoCopyOrMove(NoCopyOrMove&&) =
delete;
80 NoCopyOrMove& operator=(
const NoCopyOrMove&) =
delete;
81 NoCopyOrMove& operator=(NoCopyOrMove&&) =
delete;
83 operator bool()
const {
return i != 0; }
98 const std::unique_ptr<int>
p_two =
Assert(std::make_unique<int>(2));
117 const int nine{*
Assert(std::optional<int>{9})};
143constexpr char HEX_PARSE_INPUT[] =
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
145 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
146 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
147 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
148 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
154 std::vector<unsigned char> result;
179 expected = {0x12, 0x34, 0x56, 0x78};
186 expected = {0x89, 0x34, 0x56, 0x78};
193 expected = {0xff, 0xaa};
200 static_assert(
""_hex.empty());
201 static_assert(
""_hex_u8.empty());
243 constexpr std::string_view
out_exp{
"04678afdb0"};
254 auto input = std::string();
255 for (
size_t i=0; i<256; ++i) {
256 input.push_back(
static_cast<char>(i));
261 static constexpr auto hexmap = std::string_view(
"0123456789abcdef");
262 for (
size_t i = 0; i < 256; ++i) {
286 BOOST_CHECK_EQUAL(Join(std::vector<std::string>{
"foo",
"bar"},
", "),
"foo, bar");
297 const std::string original(
"A test \"%s\" string '%s'.");
306 test_replaceall(original,
"%s",
"foo",
"A test \"foo\" string 'foo'.");
307 test_replaceall(original,
"\"",
"foo",
"A test foo%sfoo string '%s'.");
308 test_replaceall(original,
"'",
"foo",
"A test \"%s\" string foo%sfoo.");
310 test_replaceall(
"%w and %w",
"%w",
"$&$`$'$1$$",
"$&$`$'$1$$ and $&$`$'$1$$");
317 BOOST_CHECK_EQUAL(TrimStringView(
"\t \n \n \f\n\r\t\v\tfoo \n \f\n\r\t\v\tbar\t \n \f\n\r\t\v\t\n "),
"foo \n \f\n\r\t\v\tbar");
319 BOOST_CHECK_EQUAL(TrimStringView(
"\t \n foo \n\tbar\t \n ",
"fobar"),
"\t \n foo \n\tbar\t \n ");
322 BOOST_CHECK_EQUAL(TrimString(std::string(
"\0 foo \0 ", 8)), std::string(
"\0 foo \0", 7));
323 BOOST_CHECK_EQUAL(TrimStringView(std::string(
" foo ", 5)), std::string(
"foo", 3));
324 BOOST_CHECK_EQUAL(TrimString(std::string(
"\t\t\0\0\n\n", 6)), std::string(
"\0\0", 2));
325 BOOST_CHECK_EQUAL(TrimStringView(std::string(
"\x05\x04\x03\x02\x01\x00", 6)), std::string(
"\x05\x04\x03\x02\x01\x00", 6));
326 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));
327 BOOST_CHECK_EQUAL(TrimStringView(std::string(
"\x05\x04\x03\x02\x01\x00", 6), std::string(
"\x05\x04\x03\x02\x01\x00", 6)),
"");
359 const std::string valid{
"2000-01-01T00:00:01Z"};
361 for (
auto mut{0
U}; mut < valid.size(); ++mut) {
362 std::string invalid{valid};
534 while(mask<
mod-1)mask=(mask<<1)+1;
538 for (
int i = 0; i < 10000; i++) {
541 rval=m_rng.rand32()&mask;
563#define B "check_prefix"
564#define E "check_postfix"
573 size_t st = 12345678;
633 constexpr T MAXI{std::numeric_limits<T>::max()};
654 constexpr T MINI{std::numeric_limits<T>::min()};
655 constexpr T MAXI{std::numeric_limits<T>::max()};
770 return strtoll(str.c_str(),
nullptr, 10);
809 {
"-9223372036854775809", std::numeric_limits<int64_t>::min()},
810 {
"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL},
811 {
"9223372036854775807", 9'223'372'036'854'775'807},
812 {
"9223372036854775808", std::numeric_limits<int64_t>::max()},
866 std::optional<uint64_t> n;
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.");
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";
1058 const fs::path lockname =
".lock";
1081 fs::create_directories(
dirname);
1141 fs::remove(
dirname / lockname);
1180static std::string
SpanToStr(
const std::span<const char>& span)
1182 return std::string(span.begin(), span.end());
1189 std::span<const char>
sp;
1193 input =
"MilkToastHoney";
1195 success = Const(
"",
sp);
1199 success = Const(
"Milk",
sp,
false);
1203 success = Const(
"Milk",
sp);
1207 success = Const(
"Bread",
sp,
false);
1210 success = Const(
"Bread",
sp);
1213 success = Const(
"Toast",
sp,
false);
1217 success = Const(
"Toast",
sp);
1221 success = Const(
"Honeybadger",
sp);
1224 success = Const(
"Honey",
sp,
false);
1228 success = Const(
"Honey",
sp);
1232 input =
"Foo(Bar(xy,z()))";
1235 success = Func(
"FooBar",
sp);
1238 success = Func(
"Foo(",
sp);
1241 success = Func(
"Foo",
sp);
1245 success = Func(
"Bar",
sp);
1249 success = Func(
"xy",
sp);
1253 std::span<const char> result;
1255 input =
"(n*(n-1))/2";
1267 input =
"(aaaaa,bbbbb()),c";
1279 input =
"((a),(b),(c)),xxx";
1286 std::vector<std::span<const char>>
results;
1296 input =
"one#two#three";
1301 input =
"one#two#three";
1308 results = Split(input,
'#',
true);
1314 input =
"*foo*bar*";
1322 results = Split(input,
'*',
true);
1334 std::vector<std::string> result = SplitString(
"",
'-');
1341 std::vector<std::string> result = SplitString(
"-",
'-');
1349 std::vector<std::string> result = SplitString(
"--",
'-');
1358 std::vector<std::string> result = SplitString(
"abc",
'-');
1365 std::vector<std::string> result = SplitString(
"a-b",
'-');
1373 std::vector<std::string> result = SplitString(
"AAA",
'a');
1380 using V = std::vector<std::string>;
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(
"a,b.c:d;e",
"") ==
V({
"a,b.c:d;e"}));
1384 BOOST_TEST(SplitString(
"aaa",
"bcdefg") ==
V({
"aaa"}));
1385 BOOST_TEST(SplitString(
"x\0a,b"s,
"\0"s) ==
V({
"x",
"a,b"}));
1386 BOOST_TEST(SplitString(
"x\0a,b"s,
'\0') ==
V({
"x",
"a,b"}));
1387 BOOST_TEST(SplitString(
"x\0a,b"s,
"\0,"s) ==
V({
"x",
"a",
"b"}));
1388 BOOST_TEST(SplitString(
"abcdefg",
"bcd") ==
V({
"a",
"",
"",
"efg"}));
1397 const Tracker* origin;
1401 Tracker() noexcept : origin(
this) {}
1402 Tracker(
const Tracker&
t) noexcept : origin(
t.origin), copies(
t.copies + 1) {}
1403 Tracker(Tracker&&
t) noexcept : origin(
t.origin), copies(
t.copies) {}
1404 Tracker& operator=(
const Tracker&
t)
noexcept
1408 copies =
t.copies + 1;
1463 auto v6 =
Cat(std::move(v1),
v3);
1472 auto v7 =
Cat(v2, std::move(
v4));
1483 auto v8 =
Cat(std::move(v2), std::move(
v3));
1498 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
1499 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
1500 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
1501 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
1504 const std::string message =
"Trust no one";
1507 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
1513 "Confirm the private key is invalid");
1516 "Sign with an invalid private key");
1521 "Confirm the private key is valid");
1524 "Sign with a valid private key");
1534 "signature should be irrelevant",
1540 "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
1541 "signature should be irrelevant",
1547 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1548 "invalid signature, not in base64 encoding",
1549 "message should be irrelevant"),
1554 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1555 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
1556 "message should be irrelevant"),
1561 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1562 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1563 "I never signed this"),
1568 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1569 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1575 "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
1576 "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
1661 fs::path
tmpfolder = m_args.GetDataDirBase();
1664 for (
int i = 0; i < 30; i++) {
1668 std::ofstream file{
tmpfile.std_path()};
1694 fs::path
tmpfolder = m_args.GetDataDirBase();
1699 std::ifstream file{
tmpfile.std_path()};
1708 std::vector<uint8_t> v = {1, 2, 3};
1715 std::vector<bool> v = {
false,
true,
false,
false,
true,
true};
1722 std::deque<int> v = {1, 3, 3, 7};
1729template <
typename T>
1732 constexpr auto MAX{std::numeric_limits<T>::max()};
1750 if constexpr (std::is_signed_v<T>) {
1751 constexpr auto MIN{std::numeric_limits<T>::min()};
1764template <
typename T>
1767 constexpr auto MAX{std::numeric_limits<T>::max()};
1785 if constexpr (std::is_signed_v<T>) {
1786 constexpr auto MIN{std::numeric_limits<T>::min()};
1817template <
class Int, auto
bytes>
1880 constexpr unsigned bits{129};
1881 constexpr size_t digits{std::numeric_limits<size_t>::digits};
int64_t CAmount
Amount in satoshis (Can be negative)
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.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
A token bucket rate limiter.
bool decrement(double n=1.0, double floor=0.0)
Consume n tokens.
void increment(const time_point &now)
Refill tokens based on elapsed time since last call.
double value() const
Current token balance.
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...
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.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
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)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
std::vector< std::string > SplitString(std::string_view str, char sep)
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::vector< T > Split(std::span< const char > sp LIFETIMEBOUND, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
std::string_view TrimStringView(std::string_view str LIFETIMEBOUND, std::string_view pattern=" \f\n\r\t\v")
void ReplaceAll(std::string &in_out, std::string_view search, std::string_view substitute)
Replace every non-overlapping occurrence of search with substitute, treating both literally; the repl...
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)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
LockResult LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::optional< T > CheckedAdd(const T i, const T j) noexcept
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
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.
@ 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.
auto MakeByteSpan(const V &v) noexcept
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
auto 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.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
#define TRY_LOCK(cs, name)
@ ZEROS
Seed with a compile time constant of zeros.
BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32))
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array+uint256::size())
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
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 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.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
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 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 UninterruptibleSleep(const std::chrono::microseconds &n)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601Date(int64_t nTime)
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
std::string FormatRFC1123DateTime(int64_t time)
RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 Used in HTTP/1....
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t TicksSeconds(Duration d)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
int64_t atoi64_legacy(const std::string &str)
constexpr uint8_t HEX_PARSE_OUTPUT[]
constexpr char HEX_PARSE_INPUT[]
static void TestOtherProcess(fs::path dirname, fs::path lockname, int fd)
static constexpr char ExitCommand
static constexpr char UnlockCommand
static std::string SpanToStr(const std::span< const char > &span)
static void TestAddMatrixOverflow()
static void TestAddMatrix()
BOOST_AUTO_TEST_CASE(util_check)
static void RunToIntegralTests()
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR
static constexpr char LockCommand
void TestCheckedLeftShift()
void TestSaturatingLeftShift()
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.
void ClearShrink(V &v) noexcept
Clear a vector (or std::deque) and release its allocated memory.