19 #include <sys/types.h>
23 #include <boost/test/unit_test.hpp>
35 BOOST_ERROR(
"break was swallowed!");
43 BOOST_ERROR(
"break was swallowed!");
47 static const unsigned char ParseHex_expected[65] = {
48 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
49 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
50 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
51 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
56 std::vector<unsigned char> result;
57 std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected +
sizeof(ParseHex_expected));
59 result =
ParseHex(
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
60 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
64 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
67 result =
ParseHex(
"1234 invalid 1234");
68 BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
75 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
79 ParseHex_expected +
sizeof(ParseHex_expected),
80 ParseHex_expected +
sizeof(ParseHex_expected))),
87 std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
103 const auto op_upper = [](
const std::string& s) {
return ToUpper(s); };
106 BOOST_CHECK_EQUAL(Join<std::string>({
"foo",
"bar"},
", ", op_upper),
"FOO, BAR");
136 std::istringstream streamConfig(str_config);
153 const char *argv_test[] = {
"-ignored",
"-a",
"-b",
"-ccc=argument",
"-ccc=multiple",
"f",
"-d=e"};
181 const char *argv_test[] = {
182 "ignored",
"-a",
"-nob",
"-c=0",
"-d=1",
"-e=false",
"-f=true"};
186 for (
char opt :
"abcdef")
215 const char *argv_test[] = {
"ignored",
"-nofoo",
"-foo",
"-nobar=0"};
227 const char *conf_test =
"nofoo=1\nfoo=1\nnobar=0\n";
241 const char *combo_test_args[] = {
"ignored",
"-nofoo",
"-bar"};
242 const char *combo_test_conf =
"foo=1\nnobar=1\n";
255 && testArgs.
GetArgs(
"-bar").front() ==
"");
260 const char *str_config =
319 && test_args.
GetArg(
"-b",
"xxx") ==
"1"
320 && test_args.
GetArg(
"-ccc",
"xxx") ==
"argument"
321 && test_args.
GetArg(
"-d",
"xxx") ==
"e"
322 && test_args.
GetArg(
"-fff",
"xxx") ==
"0"
323 && test_args.
GetArg(
"-ggg",
"xxx") ==
"1"
324 && test_args.
GetArg(
"-h",
"xxx") ==
"0"
325 && test_args.
GetArg(
"-i",
"xxx") ==
"1"
326 && test_args.
GetArg(
"-zzz",
"xxx") ==
"xxx"
327 && test_args.
GetArg(
"-iii",
"xxx") ==
"xxx"
330 for (
bool def : {
false,
true}) {
345 && test_args.
GetArgs(
"-a").front() ==
"");
347 && test_args.
GetArgs(
"-b").front() ==
"1");
349 && test_args.
GetArgs(
"-ccc").front() ==
"argument"
350 && test_args.
GetArgs(
"-ccc").back() ==
"multiple");
354 && test_args.
GetArgs(
"-ggg").front() ==
"1");
359 && test_args.
GetArgs(
"-i").front() ==
"1");
378 && test_args.
GetArg(
"-b",
"xxx") ==
"1"
379 && test_args.
GetArg(
"-fff",
"xxx") ==
"0"
380 && test_args.
GetArg(
"-ggg",
"xxx") ==
"1"
381 && test_args.
GetArg(
"-zzz",
"xxx") ==
"xxx"
382 && test_args.
GetArg(
"-iii",
"xxx") ==
"xxx"
391 const std::vector<std::string> sec1_ccc_expected = {
"extend1",
"extend2",
"argument",
"multiple"};
392 const auto& sec1_ccc_res = test_args.
GetArgs(
"-ccc");
393 BOOST_CHECK_EQUAL_COLLECTIONS(sec1_ccc_res.begin(), sec1_ccc_res.end(), sec1_ccc_expected.begin(), sec1_ccc_expected.end());
399 && test_args.
GetArg(
"-b",
"xxx") ==
"1"
400 && test_args.
GetArg(
"-d",
"xxx") ==
"e"
401 && test_args.
GetArg(
"-fff",
"xxx") ==
"0"
402 && test_args.
GetArg(
"-ggg",
"xxx") ==
"1"
403 && test_args.
GetArg(
"-zzz",
"xxx") ==
"xxx"
404 && test_args.
GetArg(
"-h",
"xxx") ==
"0"
411 const std::vector<std::string> sec2_ccc_expected = {
"extend3",
"argument",
"multiple"};
412 const auto& sec2_ccc_res = test_args.
GetArgs(
"-ccc");
413 BOOST_CHECK_EQUAL_COLLECTIONS(sec2_ccc_res.begin(), sec2_ccc_res.end(), sec2_ccc_expected.begin(), sec2_ccc_expected.end());
481 const char* argv_testnet[] = {
"cmd",
"-testnet"};
482 const char* argv_regtest[] = {
"cmd",
"-regtest"};
483 const char* argv_test_no_reg[] = {
"cmd",
"-testnet",
"-noregtest"};
484 const char* argv_both[] = {
"cmd",
"-testnet",
"-regtest"};
488 const char* testnetconf =
"testnet=1\nregtest=0\n[test]\nregtest=1";
652 for (
int mod=2;mod<11;mod++)
656 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
658 while(mask<mod-1)mask=(mask<<1)+1;
662 for (i=0;i<10000;i++)
666 rval=InsecureRand32()&mask;
667 }
while(rval>=(uint32_t)mod);
688 #define B "check_prefix"
689 #define E "check_postfix"
692 int64_t s64t = -9223372036854775807LL;
693 uint64_t u64t = 18446744073709551615ULL;
698 size_t st = 12345678;
699 ssize_t sst = -12345678;
704 ptrdiff_t pt = 87654321;
705 ptrdiff_t spt = -87654321;
725 for (
const auto& num_sleep : {0, 1}) {
735 const auto ms_0 = GetTime<std::chrono::milliseconds>();
736 const auto us_0 = GetTime<std::chrono::microseconds>();
738 BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
739 BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
776 const char test_bytes[] = {
'1', 0,
'1'};
777 std::string teststr(test_bytes,
sizeof(test_bytes));
806 const char test_bytes[] = {
'1', 0,
'1'};
807 std::string teststr(test_bytes,
sizeof(test_bytes));
836 const char test_bytes[] = {
'1', 0,
'1'};
837 std::string teststr(test_bytes,
sizeof(test_bytes));
853 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string. This is a second sentence in the very long test string."),
"This is a very long test string. This is a second sentence in the very long\ntest string.");
858 std::vector<std::string> comments;
859 comments.push_back(std::string(
"comment1"));
860 std::vector<std::string> comments2;
861 comments2.push_back(std::string(
"comment1"));
862 comments2.push_back(std::string(
"comment2"));
942 std::string testVector;
946 testVector =
"#HODL";
950 testVector =
"\x00\xfe\xff";
1004 static void TestOtherThread(fs::path dirname, std::string lockname,
bool *result)
1010 static constexpr
char LockCommand =
'L';
1011 static constexpr
char UnlockCommand =
'U';
1012 static constexpr
char ExitCommand =
'X';
1014 static void TestOtherProcess(fs::path dirname, std::string lockname,
int fd)
1019 rv = read(fd, &ch, 1);
1024 rv = write(fd, &ch, 1);
1030 rv = write(fd, &ch, 1);
1044 fs::path dirname = fs::temp_directory_path() / fs::unique_path();
1045 const std::string lockname =
".lock";
1050 void (*old_handler)(int) = signal(SIGCHLD, SIG_DFL);
1060 TestOtherProcess(dirname, lockname, fd[0]);
1067 fs::create_directories(dirname);
1080 std::thread thr(TestOtherThread, dirname, lockname, &threadresult);
1122 signal(SIGCHLD, old_handler);
1127 fs::remove_all(dirname);
1133 fs::path tmpdirname = fs::temp_directory_path();
1137 tmpdirname = fs::temp_directory_path() / fs::unique_path();
1140 fs::create_directory(tmpdirname);
1143 fs::remove(tmpdirname);
1151 const Tracker* origin;
1155 Tracker() noexcept : origin(this), copies(0) {}
1156 Tracker(
const Tracker& t) noexcept : origin(t.origin), copies(t.copies + 1) {}
1157 Tracker(Tracker&& t) noexcept : origin(t.origin), copies(t.copies) {}
1158 Tracker& operator=(
const Tracker& t) noexcept
1161 copies = t.copies + 1;
1164 Tracker& operator=(Tracker&& t) noexcept
1189 auto v2 =
Vector(std::move(t2));
1194 auto v3 =
Vector(t1, std::move(t2));
1201 auto v4 =
Vector(std::move(v3[0]), v3[1], std::move(t3));
1210 auto v5 =
Cat(v1, v4);
1221 auto v6 =
Cat(std::move(v1), v3);
1230 auto v7 =
Cat(v2, std::move(v4));
1241 auto v8 =
Cat(std::move(v2), std::move(v3));
int64_t CAmount
Amount in PIV (Can be negative)
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
void ParseParameters(int argc, const char *const argv[])
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
std::map< std::string, std::vector< std::string > > m_override_args
std::set< std::string > m_network_only_args
void SelectConfigNetwork(const std::string &network)
Select the network in use.
std::map< std::string, std::vector< std::string > > m_config_args
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
void ReadConfigStream(std::istream &stream)
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
static const std::string MAIN
Chain name strings.
A Span is an object that can refer to a contiguous sequence of objects.
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()
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::string Join(const std::vector< T > &list, const std::string &separator, UnaryOp unary_op)
Join a list of items.
std::map< std::string, std::vector< std::string > > & GetOverrideArgs()
void SetNetworkOnlyArg(const std::string arg)
void ReadConfigString(const std::string str_config)
std::map< std::string, std::vector< std::string > > & GetConfigArgs()
#define TRY_LOCK(cs, name)
bool DirIsWritable(const fs::path &directory)
void ReleaseDirectoryLocks()
Release all directory locks.
bool LockDirectory(const fs::path &directory, const std::string &lockfile_name, bool probe_only)
@ ZEROS
Seed with a compile time constant of zeros.
BOOST_AUTO_TEST_CASE(util_criticalsection)
bool ParseMoney(const std::string &str, CAmount &nRet)
std::string FormatMoney(const CAmount &n, bool fPlus)
Money parsing/formatting utilities.
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string FormatParagraph(const std::string 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::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::vector< unsigned char > ParseHex(const char *psz)
bool validateURL(const std::string &strURL)
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(const std::string &str)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
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::string FormatISO8601Date(int64_t nTime)
void SetMockTime(int64_t nMockTimeIn)
For testing.
std::string FormatISO8601Time(int64_t nTime)
std::string FormatISO8601DateTimeForBackup(int64_t nTime)
void MilliSleep(int64_t n)
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
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.