Bitcoin Core  22.99.0
P2P Digital Currency
util_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <util/system.h>
6 
7 #include <clientversion.h>
8 #include <hash.h> // For Hash()
9 #include <key.h> // For CKey
10 #include <sync.h>
11 #include <test/util/logging.h>
12 #include <test/util/setup_common.h>
13 #include <test/util/str.h>
14 #include <uint256.h>
15 #include <util/getuniquepath.h>
16 #include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
17 #include <util/moneystr.h>
18 #include <util/spanparsing.h>
19 #include <util/strencodings.h>
20 #include <util/string.h>
21 #include <util/time.h>
22 #include <util/vector.h>
23 
24 #include <array>
25 #include <optional>
26 #include <stdint.h>
27 #include <string.h>
28 #include <thread>
29 #include <univalue.h>
30 #include <utility>
31 #include <vector>
32 #ifndef WIN32
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #endif
37 
38 #include <boost/test/unit_test.hpp>
39 
40 using namespace std::literals;
41 static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};
42 
43 /* defined in logging.cpp */
44 namespace BCLog {
45  std::string LogEscapeMessage(const std::string& str);
46 }
47 
49 
50 BOOST_AUTO_TEST_CASE(util_datadir)
51 {
52  // Use local args variable instead of m_args to avoid making assumptions about test setup
53  ArgsManager args;
54  args.ForceSetArg("-datadir", m_path_root.string());
55 
56  const fs::path dd_norm = args.GetDataDirBase();
57 
58  args.ForceSetArg("-datadir", dd_norm.string() + "/");
59  args.ClearPathCache();
60  BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
61 
62  args.ForceSetArg("-datadir", dd_norm.string() + "/.");
63  args.ClearPathCache();
64  BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
65 
66  args.ForceSetArg("-datadir", dd_norm.string() + "/./");
67  args.ClearPathCache();
68  BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
69 
70  args.ForceSetArg("-datadir", dd_norm.string() + "/.//");
71  args.ClearPathCache();
72  BOOST_CHECK_EQUAL(dd_norm, args.GetDataDirBase());
73 }
74 
76 {
77  // Check that Assert can forward
78  const std::unique_ptr<int> p_two = Assert(std::make_unique<int>(2));
79  // Check that Assert works on lvalues and rvalues
80  const int two = *Assert(p_two);
81  Assert(two == 2);
82  Assert(true);
83  // Check that Assume can be used as unary expression
84  const bool result{Assume(two == 2)};
85  Assert(result);
86 }
87 
88 BOOST_AUTO_TEST_CASE(util_criticalsection)
89 {
91 
92  do {
93  LOCK(cs);
94  break;
95 
96  BOOST_ERROR("break was swallowed!");
97  } while(0);
98 
99  do {
100  TRY_LOCK(cs, lockTest);
101  if (lockTest) {
102  BOOST_CHECK(true); // Needed to suppress "Test case [...] did not check any assertions"
103  break;
104  }
105 
106  BOOST_ERROR("break was swallowed!");
107  } while(0);
108 }
109 
110 static const unsigned char ParseHex_expected[65] = {
111  0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
112  0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
113  0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
114  0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
115  0x5f
116 };
117 BOOST_AUTO_TEST_CASE(util_ParseHex)
118 {
119  std::vector<unsigned char> result;
120  std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
121  // Basic test vector
122  result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
123  BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
124 
125  // Spaces between bytes must be supported
126  result = ParseHex("12 34 56 78");
127  BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
128 
129  // Leading space must be supported (used in BerkeleyEnvironment::Salvage)
130  result = ParseHex(" 89 34 56 78");
131  BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
132 
133  // Stop parsing at invalid value
134  result = ParseHex("1234 invalid 1234");
135  BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
136 }
137 
139 {
142  "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
143 
148  "");
149 
152  "");
153 
154  std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
155 
157  HexStr(ParseHex_vec),
158  "04678afdb0"
159  );
160 }
161 
163 {
164  // Normal version
165  BOOST_CHECK_EQUAL(Join({}, ", "), "");
166  BOOST_CHECK_EQUAL(Join({"foo"}, ", "), "foo");
167  BOOST_CHECK_EQUAL(Join({"foo", "bar"}, ", "), "foo, bar");
168 
169  // Version with unary operator
170  const auto op_upper = [](const std::string& s) { return ToUpper(s); };
171  BOOST_CHECK_EQUAL(Join<std::string>({}, ", ", op_upper), "");
172  BOOST_CHECK_EQUAL(Join<std::string>({"foo"}, ", ", op_upper), "FOO");
173  BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
174 }
175 
176 BOOST_AUTO_TEST_CASE(util_TrimString)
177 {
178  BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
179  BOOST_CHECK_EQUAL(TrimString("\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");
180  BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n "), "foo \n\tbar");
181  BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
182  BOOST_CHECK_EQUAL(TrimString("foo bar"), "foo bar");
183  BOOST_CHECK_EQUAL(TrimString("foo bar", "fobar"), " ");
184  BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)), std::string("\0 foo \0", 7));
185  BOOST_CHECK_EQUAL(TrimString(std::string(" foo ", 5)), std::string("foo", 3));
186  BOOST_CHECK_EQUAL(TrimString(std::string("\t\t\0\0\n\n", 6)), std::string("\0\0", 2));
187  BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
188  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));
189  BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
190 }
191 
192 BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
193 {
194  BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
195  BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
196 
197  BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
198  BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
199  BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
200 
201  auto time = GetTimeSeconds();
203 }
204 
205 BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
206 {
207  BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
208 }
209 
211 {
212  TestArgsManager() { m_network_only_args.clear(); }
213  void ReadConfigString(const std::string str_config)
214  {
215  std::istringstream streamConfig(str_config);
216  {
217  LOCK(cs_args);
218  m_settings.ro_config.clear();
219  m_config_sections.clear();
220  }
221  std::string error;
222  BOOST_REQUIRE(ReadConfigStream(streamConfig, "", error));
223  }
224  void SetNetworkOnlyArg(const std::string arg)
225  {
226  LOCK(cs_args);
227  m_network_only_args.insert(arg);
228  }
229  void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
230  {
231  for (const auto& arg : args) {
232  AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
233  }
234  }
238  using ArgsManager::cs_args;
239  using ArgsManager::m_network;
240  using ArgsManager::m_settings;
241 };
242 
245 {
246 public:
247  struct Expect {
249  bool default_string = false;
250  bool default_int = false;
251  bool default_bool = false;
252  const char* string_value = nullptr;
253  std::optional<int64_t> int_value;
254  std::optional<bool> bool_value;
255  std::optional<std::vector<std::string>> list_value;
256  const char* error = nullptr;
257 
258  explicit Expect(util::SettingsValue s) : setting(std::move(s)) {}
259  Expect& DefaultString() { default_string = true; return *this; }
260  Expect& DefaultInt() { default_int = true; return *this; }
261  Expect& DefaultBool() { default_bool = true; return *this; }
262  Expect& String(const char* s) { string_value = s; return *this; }
263  Expect& Int(int64_t i) { int_value = i; return *this; }
264  Expect& Bool(bool b) { bool_value = b; return *this; }
265  Expect& List(std::vector<std::string> m) { list_value = std::move(m); return *this; }
266  Expect& Error(const char* e) { error = e; return *this; }
267  };
268 
269  void CheckValue(unsigned int flags, const char* arg, const Expect& expect)
270  {
271  TestArgsManager test;
272  test.SetupArgs({{"-value", flags}});
273  const char* argv[] = {"ignored", arg};
274  std::string error;
275  bool success = test.ParseParameters(arg ? 2 : 1, (char**)argv, error);
276 
277  BOOST_CHECK_EQUAL(test.GetSetting("-value").write(), expect.setting.write());
278  auto settings_list = test.GetSettingsList("-value");
279  if (expect.setting.isNull() || expect.setting.isFalse()) {
280  BOOST_CHECK_EQUAL(settings_list.size(), 0U);
281  } else {
282  BOOST_CHECK_EQUAL(settings_list.size(), 1U);
283  BOOST_CHECK_EQUAL(settings_list[0].write(), expect.setting.write());
284  }
285 
286  if (expect.error) {
287  BOOST_CHECK(!success);
288  BOOST_CHECK_NE(error.find(expect.error), std::string::npos);
289  } else {
290  BOOST_CHECK(success);
292  }
293 
294  if (expect.default_string) {
295  BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), "zzzzz");
296  } else if (expect.string_value) {
297  BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), expect.string_value);
298  } else {
299  BOOST_CHECK(!success);
300  }
301 
302  if (expect.default_int) {
303  BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), 99999);
304  } else if (expect.int_value) {
305  BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), *expect.int_value);
306  } else {
307  BOOST_CHECK(!success);
308  }
309 
310  if (expect.default_bool) {
311  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), false);
312  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), true);
313  } else if (expect.bool_value) {
314  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), *expect.bool_value);
315  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), *expect.bool_value);
316  } else {
317  BOOST_CHECK(!success);
318  }
319 
320  if (expect.list_value) {
321  auto l = test.GetArgs("-value");
322  BOOST_CHECK_EQUAL_COLLECTIONS(l.begin(), l.end(), expect.list_value->begin(), expect.list_value->end());
323  } else {
324  BOOST_CHECK(!success);
325  }
326  }
327 };
328 
330 {
331  using M = ArgsManager;
332 
333  CheckValue(M::ALLOW_ANY, nullptr, Expect{{}}.DefaultString().DefaultInt().DefaultBool().List({}));
334  CheckValue(M::ALLOW_ANY, "-novalue", Expect{false}.String("0").Int(0).Bool(false).List({}));
335  CheckValue(M::ALLOW_ANY, "-novalue=", Expect{false}.String("0").Int(0).Bool(false).List({}));
336  CheckValue(M::ALLOW_ANY, "-novalue=0", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
337  CheckValue(M::ALLOW_ANY, "-novalue=1", Expect{false}.String("0").Int(0).Bool(false).List({}));
338  CheckValue(M::ALLOW_ANY, "-novalue=2", Expect{false}.String("0").Int(0).Bool(false).List({}));
339  CheckValue(M::ALLOW_ANY, "-novalue=abc", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
340  CheckValue(M::ALLOW_ANY, "-value", Expect{""}.String("").Int(0).Bool(true).List({""}));
341  CheckValue(M::ALLOW_ANY, "-value=", Expect{""}.String("").Int(0).Bool(true).List({""}));
342  CheckValue(M::ALLOW_ANY, "-value=0", Expect{"0"}.String("0").Int(0).Bool(false).List({"0"}));
343  CheckValue(M::ALLOW_ANY, "-value=1", Expect{"1"}.String("1").Int(1).Bool(true).List({"1"}));
344  CheckValue(M::ALLOW_ANY, "-value=2", Expect{"2"}.String("2").Int(2).Bool(true).List({"2"}));
345  CheckValue(M::ALLOW_ANY, "-value=abc", Expect{"abc"}.String("abc").Int(0).Bool(false).List({"abc"}));
346 }
347 
349  std::string Parse(const char* arg)
350  {
351  TestArgsManager test;
352  test.SetupArgs({{"-includeconf", ArgsManager::ALLOW_ANY}});
353  std::array argv{"ignored", arg};
354  std::string error;
355  (void)test.ParseParameters(argv.size(), argv.data(), error);
356  return error;
357  }
358 };
359 
361 {
362  BOOST_CHECK_EQUAL(Parse("-noincludeconf"), "");
363  BOOST_CHECK_EQUAL(Parse("-includeconf"), "-includeconf cannot be used from commandline; -includeconf=\"\"");
364  BOOST_CHECK_EQUAL(Parse("-includeconf=file"), "-includeconf cannot be used from commandline; -includeconf=\"file\"");
365 }
366 
367 BOOST_AUTO_TEST_CASE(util_ParseParameters)
368 {
369  TestArgsManager testArgs;
370  const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
371  const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
372  const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY);
373  const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
374 
375  const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
376 
377  std::string error;
378  LOCK(testArgs.cs_args);
379  testArgs.SetupArgs({a, b, ccc, d});
380  BOOST_CHECK(testArgs.ParseParameters(0, (char**)argv_test, error));
381  BOOST_CHECK(testArgs.m_settings.command_line_options.empty() && testArgs.m_settings.ro_config.empty());
382 
383  BOOST_CHECK(testArgs.ParseParameters(1, (char**)argv_test, error));
384  BOOST_CHECK(testArgs.m_settings.command_line_options.empty() && testArgs.m_settings.ro_config.empty());
385 
386  BOOST_CHECK(testArgs.ParseParameters(7, (char**)argv_test, error));
387  // expectation: -ignored is ignored (program name argument),
388  // -a, -b and -ccc end up in map, -d ignored because it is after
389  // a non-option argument (non-GNU option parsing)
390  BOOST_CHECK(testArgs.m_settings.command_line_options.size() == 3 && testArgs.m_settings.ro_config.empty());
391  BOOST_CHECK(testArgs.IsArgSet("-a") && testArgs.IsArgSet("-b") && testArgs.IsArgSet("-ccc")
392  && !testArgs.IsArgSet("f") && !testArgs.IsArgSet("-d"));
393  BOOST_CHECK(testArgs.m_settings.command_line_options.count("a") && testArgs.m_settings.command_line_options.count("b") && testArgs.m_settings.command_line_options.count("ccc")
394  && !testArgs.m_settings.command_line_options.count("f") && !testArgs.m_settings.command_line_options.count("d"));
395 
396  BOOST_CHECK(testArgs.m_settings.command_line_options["a"].size() == 1);
397  BOOST_CHECK(testArgs.m_settings.command_line_options["a"].front().get_str() == "");
398  BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].size() == 2);
399  BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].front().get_str() == "argument");
400  BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].back().get_str() == "multiple");
401  BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
402 }
403 
404 BOOST_AUTO_TEST_CASE(util_ParseInvalidParameters)
405 {
406  TestArgsManager test;
407  test.SetupArgs({{"-registered", ArgsManager::ALLOW_ANY}});
408 
409  const char* argv[] = {"ignored", "-registered"};
410  std::string error;
411  BOOST_CHECK(test.ParseParameters(2, (char**)argv, error));
413 
414  argv[1] = "-unregistered";
415  BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
416  BOOST_CHECK_EQUAL(error, "Invalid parameter -unregistered");
417 
418  // Make sure registered parameters prefixed with a chain name trigger errors.
419  // (Previously, they were accepted and ignored.)
420  argv[1] = "-test.registered";
421  BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
422  BOOST_CHECK_EQUAL(error, "Invalid parameter -test.registered");
423 }
424 
425 static void TestParse(const std::string& str, bool expected_bool, int64_t expected_int)
426 {
427  TestArgsManager test;
428  test.SetupArgs({{"-value", ArgsManager::ALLOW_ANY}});
429  std::string arg = "-value=" + str;
430  const char* argv[] = {"ignored", arg.c_str()};
431  std::string error;
432  BOOST_CHECK(test.ParseParameters(2, (char**)argv, error));
433  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), expected_bool);
434  BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), expected_bool);
435  BOOST_CHECK_EQUAL(test.GetArg("-value", 99998), expected_int);
436  BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), expected_int);
437 }
438 
439 // Test bool and int parsing.
440 BOOST_AUTO_TEST_CASE(util_ArgParsing)
441 {
442  // Some of these cases could be ambiguous or surprising to users, and might
443  // be worth triggering errors or warnings in the future. But for now basic
444  // test coverage is useful to avoid breaking backwards compatibility
445  // unintentionally.
446  TestParse("", true, 0);
447  TestParse(" ", false, 0);
448  TestParse("0", false, 0);
449  TestParse("0 ", false, 0);
450  TestParse(" 0", false, 0);
451  TestParse("+0", false, 0);
452  TestParse("-0", false, 0);
453  TestParse("5", true, 5);
454  TestParse("5 ", true, 5);
455  TestParse(" 5", true, 5);
456  TestParse("+5", true, 5);
457  TestParse("-5", true, -5);
458  TestParse("0 5", false, 0);
459  TestParse("5 0", true, 5);
460  TestParse("050", true, 50);
461  TestParse("0.", false, 0);
462  TestParse("5.", true, 5);
463  TestParse("0.0", false, 0);
464  TestParse("0.5", false, 0);
465  TestParse("5.0", true, 5);
466  TestParse("5.5", true, 5);
467  TestParse("x", false, 0);
468  TestParse("x0", false, 0);
469  TestParse("x5", false, 0);
470  TestParse("0x", false, 0);
471  TestParse("5x", true, 5);
472  TestParse("0x5", false, 0);
473  TestParse("false", false, 0);
474  TestParse("true", false, 0);
475  TestParse("yes", false, 0);
476  TestParse("no", false, 0);
477 }
478 
479 BOOST_AUTO_TEST_CASE(util_GetBoolArg)
480 {
481  TestArgsManager testArgs;
482  const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
483  const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
484  const auto c = std::make_pair("-c", ArgsManager::ALLOW_ANY);
485  const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
486  const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
487  const auto f = std::make_pair("-f", ArgsManager::ALLOW_ANY);
488 
489  const char *argv_test[] = {
490  "ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
491  std::string error;
492  LOCK(testArgs.cs_args);
493  testArgs.SetupArgs({a, b, c, d, e, f});
494  BOOST_CHECK(testArgs.ParseParameters(7, (char**)argv_test, error));
495 
496  // Each letter should be set.
497  for (const char opt : "abcdef")
498  BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt);
499 
500  // Nothing else should be in the map
501  BOOST_CHECK(testArgs.m_settings.command_line_options.size() == 6 &&
502  testArgs.m_settings.ro_config.empty());
503 
504  // The -no prefix should get stripped on the way in.
505  BOOST_CHECK(!testArgs.IsArgSet("-nob"));
506 
507  // The -b option is flagged as negated, and nothing else is
508  BOOST_CHECK(testArgs.IsArgNegated("-b"));
509  BOOST_CHECK(!testArgs.IsArgNegated("-a"));
510 
511  // Check expected values.
512  BOOST_CHECK(testArgs.GetBoolArg("-a", false) == true);
513  BOOST_CHECK(testArgs.GetBoolArg("-b", true) == false);
514  BOOST_CHECK(testArgs.GetBoolArg("-c", true) == false);
515  BOOST_CHECK(testArgs.GetBoolArg("-d", false) == true);
516  BOOST_CHECK(testArgs.GetBoolArg("-e", true) == false);
517  BOOST_CHECK(testArgs.GetBoolArg("-f", true) == false);
518 }
519 
520 BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
521 {
522  // Test some awful edge cases that hopefully no user will ever exercise.
523  TestArgsManager testArgs;
524 
525  // Params test
526  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
527  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
528  const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
529  testArgs.SetupArgs({foo, bar});
530  std::string error;
531  BOOST_CHECK(testArgs.ParseParameters(4, (char**)argv_test, error));
532 
533  // This was passed twice, second one overrides the negative setting.
534  BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
535  BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "");
536 
537  // A double negative is a positive, and not marked as negated.
538  BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
539  BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
540 
541  // Config test
542  const char *conf_test = "nofoo=1\nfoo=1\nnobar=0\n";
543  BOOST_CHECK(testArgs.ParseParameters(1, (char**)argv_test, error));
544  testArgs.ReadConfigString(conf_test);
545 
546  // This was passed twice, second one overrides the negative setting,
547  // and the value.
548  BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
549  BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "1");
550 
551  // A double negative is a positive, and does not count as negated.
552  BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
553  BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
554 
555  // Combined test
556  const char *combo_test_args[] = {"ignored", "-nofoo", "-bar"};
557  const char *combo_test_conf = "foo=1\nnobar=1\n";
558  BOOST_CHECK(testArgs.ParseParameters(3, (char**)combo_test_args, error));
559  testArgs.ReadConfigString(combo_test_conf);
560 
561  // Command line overrides, but doesn't erase old setting
562  BOOST_CHECK(testArgs.IsArgNegated("-foo"));
563  BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "0");
564  BOOST_CHECK(testArgs.GetArgs("-foo").size() == 0);
565 
566  // Command line overrides, but doesn't erase old setting
567  BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
568  BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "");
569  BOOST_CHECK(testArgs.GetArgs("-bar").size() == 1
570  && testArgs.GetArgs("-bar").front() == "");
571 }
572 
573 BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
574 {
575  const char *str_config =
576  "a=\n"
577  "b=1\n"
578  "ccc=argument\n"
579  "ccc=multiple\n"
580  "d=e\n"
581  "nofff=1\n"
582  "noggg=0\n"
583  "h=1\n"
584  "noh=1\n"
585  "noi=1\n"
586  "i=1\n"
587  "sec1.ccc=extend1\n"
588  "\n"
589  "[sec1]\n"
590  "ccc=extend2\n"
591  "d=eee\n"
592  "h=1\n"
593  "[sec2]\n"
594  "ccc=extend3\n"
595  "iii=2\n";
596 
597  TestArgsManager test_args;
598  LOCK(test_args.cs_args);
599  const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
600  const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
601  const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY);
602  const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
603  const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
604  const auto fff = std::make_pair("-fff", ArgsManager::ALLOW_ANY);
605  const auto ggg = std::make_pair("-ggg", ArgsManager::ALLOW_ANY);
606  const auto h = std::make_pair("-h", ArgsManager::ALLOW_ANY);
607  const auto i = std::make_pair("-i", ArgsManager::ALLOW_ANY);
608  const auto iii = std::make_pair("-iii", ArgsManager::ALLOW_ANY);
609  test_args.SetupArgs({a, b, ccc, d, e, fff, ggg, h, i, iii});
610 
611  test_args.ReadConfigString(str_config);
612  // expectation: a, b, ccc, d, fff, ggg, h, i end up in map
613  // so do sec1.ccc, sec1.d, sec1.h, sec2.ccc, sec2.iii
614 
615  BOOST_CHECK(test_args.m_settings.command_line_options.empty());
616  BOOST_CHECK(test_args.m_settings.ro_config.size() == 3);
617  BOOST_CHECK(test_args.m_settings.ro_config[""].size() == 8);
618  BOOST_CHECK(test_args.m_settings.ro_config["sec1"].size() == 3);
619  BOOST_CHECK(test_args.m_settings.ro_config["sec2"].size() == 2);
620 
621  BOOST_CHECK(test_args.m_settings.ro_config[""].count("a"));
622  BOOST_CHECK(test_args.m_settings.ro_config[""].count("b"));
623  BOOST_CHECK(test_args.m_settings.ro_config[""].count("ccc"));
624  BOOST_CHECK(test_args.m_settings.ro_config[""].count("d"));
625  BOOST_CHECK(test_args.m_settings.ro_config[""].count("fff"));
626  BOOST_CHECK(test_args.m_settings.ro_config[""].count("ggg"));
627  BOOST_CHECK(test_args.m_settings.ro_config[""].count("h"));
628  BOOST_CHECK(test_args.m_settings.ro_config[""].count("i"));
629  BOOST_CHECK(test_args.m_settings.ro_config["sec1"].count("ccc"));
630  BOOST_CHECK(test_args.m_settings.ro_config["sec1"].count("h"));
631  BOOST_CHECK(test_args.m_settings.ro_config["sec2"].count("ccc"));
632  BOOST_CHECK(test_args.m_settings.ro_config["sec2"].count("iii"));
633 
634  BOOST_CHECK(test_args.IsArgSet("-a"));
635  BOOST_CHECK(test_args.IsArgSet("-b"));
636  BOOST_CHECK(test_args.IsArgSet("-ccc"));
637  BOOST_CHECK(test_args.IsArgSet("-d"));
638  BOOST_CHECK(test_args.IsArgSet("-fff"));
639  BOOST_CHECK(test_args.IsArgSet("-ggg"));
640  BOOST_CHECK(test_args.IsArgSet("-h"));
641  BOOST_CHECK(test_args.IsArgSet("-i"));
642  BOOST_CHECK(!test_args.IsArgSet("-zzz"));
643  BOOST_CHECK(!test_args.IsArgSet("-iii"));
644 
645  BOOST_CHECK_EQUAL(test_args.GetArg("-a", "xxx"), "");
646  BOOST_CHECK_EQUAL(test_args.GetArg("-b", "xxx"), "1");
647  BOOST_CHECK_EQUAL(test_args.GetArg("-ccc", "xxx"), "argument");
648  BOOST_CHECK_EQUAL(test_args.GetArg("-d", "xxx"), "e");
649  BOOST_CHECK_EQUAL(test_args.GetArg("-fff", "xxx"), "0");
650  BOOST_CHECK_EQUAL(test_args.GetArg("-ggg", "xxx"), "1");
651  BOOST_CHECK_EQUAL(test_args.GetArg("-h", "xxx"), "0");
652  BOOST_CHECK_EQUAL(test_args.GetArg("-i", "xxx"), "1");
653  BOOST_CHECK_EQUAL(test_args.GetArg("-zzz", "xxx"), "xxx");
654  BOOST_CHECK_EQUAL(test_args.GetArg("-iii", "xxx"), "xxx");
655 
656  for (const bool def : {false, true}) {
657  BOOST_CHECK(test_args.GetBoolArg("-a", def));
658  BOOST_CHECK(test_args.GetBoolArg("-b", def));
659  BOOST_CHECK(!test_args.GetBoolArg("-ccc", def));
660  BOOST_CHECK(!test_args.GetBoolArg("-d", def));
661  BOOST_CHECK(!test_args.GetBoolArg("-fff", def));
662  BOOST_CHECK(test_args.GetBoolArg("-ggg", def));
663  BOOST_CHECK(!test_args.GetBoolArg("-h", def));
664  BOOST_CHECK(test_args.GetBoolArg("-i", def));
665  BOOST_CHECK(test_args.GetBoolArg("-zzz", def) == def);
666  BOOST_CHECK(test_args.GetBoolArg("-iii", def) == def);
667  }
668 
669  BOOST_CHECK(test_args.GetArgs("-a").size() == 1
670  && test_args.GetArgs("-a").front() == "");
671  BOOST_CHECK(test_args.GetArgs("-b").size() == 1
672  && test_args.GetArgs("-b").front() == "1");
673  BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2
674  && test_args.GetArgs("-ccc").front() == "argument"
675  && test_args.GetArgs("-ccc").back() == "multiple");
676  BOOST_CHECK(test_args.GetArgs("-fff").size() == 0);
677  BOOST_CHECK(test_args.GetArgs("-nofff").size() == 0);
678  BOOST_CHECK(test_args.GetArgs("-ggg").size() == 1
679  && test_args.GetArgs("-ggg").front() == "1");
680  BOOST_CHECK(test_args.GetArgs("-noggg").size() == 0);
681  BOOST_CHECK(test_args.GetArgs("-h").size() == 0);
682  BOOST_CHECK(test_args.GetArgs("-noh").size() == 0);
683  BOOST_CHECK(test_args.GetArgs("-i").size() == 1
684  && test_args.GetArgs("-i").front() == "1");
685  BOOST_CHECK(test_args.GetArgs("-noi").size() == 0);
686  BOOST_CHECK(test_args.GetArgs("-zzz").size() == 0);
687 
688  BOOST_CHECK(!test_args.IsArgNegated("-a"));
689  BOOST_CHECK(!test_args.IsArgNegated("-b"));
690  BOOST_CHECK(!test_args.IsArgNegated("-ccc"));
691  BOOST_CHECK(!test_args.IsArgNegated("-d"));
692  BOOST_CHECK(test_args.IsArgNegated("-fff"));
693  BOOST_CHECK(!test_args.IsArgNegated("-ggg"));
694  BOOST_CHECK(test_args.IsArgNegated("-h")); // last setting takes precedence
695  BOOST_CHECK(!test_args.IsArgNegated("-i")); // last setting takes precedence
696  BOOST_CHECK(!test_args.IsArgNegated("-zzz"));
697 
698  // Test sections work
699  test_args.SelectConfigNetwork("sec1");
700 
701  // same as original
702  BOOST_CHECK_EQUAL(test_args.GetArg("-a", "xxx"), "");
703  BOOST_CHECK_EQUAL(test_args.GetArg("-b", "xxx"), "1");
704  BOOST_CHECK_EQUAL(test_args.GetArg("-fff", "xxx"), "0");
705  BOOST_CHECK_EQUAL(test_args.GetArg("-ggg", "xxx"), "1");
706  BOOST_CHECK_EQUAL(test_args.GetArg("-zzz", "xxx"), "xxx");
707  BOOST_CHECK_EQUAL(test_args.GetArg("-iii", "xxx"), "xxx");
708  // d is overridden
709  BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
710  // section-specific setting
711  BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
712  // section takes priority for multiple values
713  BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend1");
714  // check multiple values works
715  const std::vector<std::string> sec1_ccc_expected = {"extend1","extend2","argument","multiple"};
716  const auto& sec1_ccc_res = test_args.GetArgs("-ccc");
717  BOOST_CHECK_EQUAL_COLLECTIONS(sec1_ccc_res.begin(), sec1_ccc_res.end(), sec1_ccc_expected.begin(), sec1_ccc_expected.end());
718 
719  test_args.SelectConfigNetwork("sec2");
720 
721  // same as original
722  BOOST_CHECK(test_args.GetArg("-a", "xxx") == "");
723  BOOST_CHECK(test_args.GetArg("-b", "xxx") == "1");
724  BOOST_CHECK(test_args.GetArg("-d", "xxx") == "e");
725  BOOST_CHECK(test_args.GetArg("-fff", "xxx") == "0");
726  BOOST_CHECK(test_args.GetArg("-ggg", "xxx") == "1");
727  BOOST_CHECK(test_args.GetArg("-zzz", "xxx") == "xxx");
728  BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
729  // section-specific setting
730  BOOST_CHECK(test_args.GetArg("-iii", "xxx") == "2");
731  // section takes priority for multiple values
732  BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend3");
733  // check multiple values works
734  const std::vector<std::string> sec2_ccc_expected = {"extend3","argument","multiple"};
735  const auto& sec2_ccc_res = test_args.GetArgs("-ccc");
736  BOOST_CHECK_EQUAL_COLLECTIONS(sec2_ccc_res.begin(), sec2_ccc_res.end(), sec2_ccc_expected.begin(), sec2_ccc_expected.end());
737 
738  // Test section only options
739 
740  test_args.SetNetworkOnlyArg("-d");
741  test_args.SetNetworkOnlyArg("-ccc");
742  test_args.SetNetworkOnlyArg("-h");
743 
745  BOOST_CHECK(test_args.GetArg("-d", "xxx") == "e");
746  BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
747  BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
748 
749  test_args.SelectConfigNetwork("sec1");
750  BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
751  BOOST_CHECK(test_args.GetArgs("-d").size() == 1);
752  BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
753  BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
754 
755  test_args.SelectConfigNetwork("sec2");
756  BOOST_CHECK(test_args.GetArg("-d", "xxx") == "xxx");
757  BOOST_CHECK(test_args.GetArgs("-d").size() == 0);
758  BOOST_CHECK(test_args.GetArgs("-ccc").size() == 1);
759  BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
760 }
761 
763 {
764  TestArgsManager testArgs;
765  LOCK(testArgs.cs_args);
766  testArgs.m_settings.command_line_options.clear();
767  testArgs.m_settings.command_line_options["strtest1"] = {"string..."};
768  // strtest2 undefined on purpose
769  testArgs.m_settings.command_line_options["inttest1"] = {"12345"};
770  testArgs.m_settings.command_line_options["inttest2"] = {"81985529216486895"};
771  // inttest3 undefined on purpose
772  testArgs.m_settings.command_line_options["booltest1"] = {""};
773  // booltest2 undefined on purpose
774  testArgs.m_settings.command_line_options["booltest3"] = {"0"};
775  testArgs.m_settings.command_line_options["booltest4"] = {"1"};
776 
777  // priorities
778  testArgs.m_settings.command_line_options["pritest1"] = {"a", "b"};
779  testArgs.m_settings.ro_config[""]["pritest2"] = {"a", "b"};
780  testArgs.m_settings.command_line_options["pritest3"] = {"a"};
781  testArgs.m_settings.ro_config[""]["pritest3"] = {"b"};
782  testArgs.m_settings.command_line_options["pritest4"] = {"a","b"};
783  testArgs.m_settings.ro_config[""]["pritest4"] = {"c","d"};
784 
785  BOOST_CHECK_EQUAL(testArgs.GetArg("strtest1", "default"), "string...");
786  BOOST_CHECK_EQUAL(testArgs.GetArg("strtest2", "default"), "default");
787  BOOST_CHECK_EQUAL(testArgs.GetArg("inttest1", -1), 12345);
788  BOOST_CHECK_EQUAL(testArgs.GetArg("inttest2", -1), 81985529216486895LL);
789  BOOST_CHECK_EQUAL(testArgs.GetArg("inttest3", -1), -1);
790  BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest1", false), true);
791  BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest2", false), false);
792  BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest3", false), false);
793  BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest4", false), true);
794 
795  BOOST_CHECK_EQUAL(testArgs.GetArg("pritest1", "default"), "b");
796  BOOST_CHECK_EQUAL(testArgs.GetArg("pritest2", "default"), "a");
797  BOOST_CHECK_EQUAL(testArgs.GetArg("pritest3", "default"), "a");
798  BOOST_CHECK_EQUAL(testArgs.GetArg("pritest4", "default"), "b");
799 }
800 
801 BOOST_AUTO_TEST_CASE(util_GetChainName)
802 {
803  TestArgsManager test_args;
804  const auto testnet = std::make_pair("-testnet", ArgsManager::ALLOW_ANY);
805  const auto regtest = std::make_pair("-regtest", ArgsManager::ALLOW_ANY);
806  test_args.SetupArgs({testnet, regtest});
807 
808  const char* argv_testnet[] = {"cmd", "-testnet"};
809  const char* argv_regtest[] = {"cmd", "-regtest"};
810  const char* argv_test_no_reg[] = {"cmd", "-testnet", "-noregtest"};
811  const char* argv_both[] = {"cmd", "-testnet", "-regtest"};
812 
813  // equivalent to "-testnet"
814  // regtest in testnet section is ignored
815  const char* testnetconf = "testnet=1\nregtest=0\n[test]\nregtest=1";
816  std::string error;
817 
818  BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
819  BOOST_CHECK_EQUAL(test_args.GetChainName(), "main");
820 
821  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
822  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
823 
824  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
825  BOOST_CHECK_EQUAL(test_args.GetChainName(), "regtest");
826 
827  BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_test_no_reg, error));
828  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
829 
830  BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
831  BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
832 
833  BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
834  test_args.ReadConfigString(testnetconf);
835  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
836 
837  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
838  test_args.ReadConfigString(testnetconf);
839  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
840 
841  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
842  test_args.ReadConfigString(testnetconf);
843  BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
844 
845  BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_test_no_reg, error));
846  test_args.ReadConfigString(testnetconf);
847  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
848 
849  BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
850  test_args.ReadConfigString(testnetconf);
851  BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
852 
853  // check setting the network to test (and thus making
854  // [test] regtest=1 potentially relevant) doesn't break things
855  test_args.SelectConfigNetwork("test");
856 
857  BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
858  test_args.ReadConfigString(testnetconf);
859  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
860 
861  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
862  test_args.ReadConfigString(testnetconf);
863  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
864 
865  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
866  test_args.ReadConfigString(testnetconf);
867  BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
868 
869  BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_test_no_reg, error));
870  test_args.ReadConfigString(testnetconf);
871  BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
872 
873  BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
874  test_args.ReadConfigString(testnetconf);
875  BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
876 }
877 
878 // Test different ways settings can be merged, and verify results. This test can
879 // be used to confirm that updates to settings code don't change behavior
880 // unintentionally.
881 //
882 // The test covers:
883 //
884 // - Combining different setting actions. Possible actions are: configuring a
885 // setting, negating a setting (adding "-no" prefix), and configuring/negating
886 // settings in a network section (adding "main." or "test." prefixes).
887 //
888 // - Combining settings from command line arguments and a config file.
889 //
890 // - Combining SoftSet and ForceSet calls.
891 //
892 // - Testing "main" and "test" network values to make sure settings from network
893 // sections are applied and to check for mainnet-specific behaviors like
894 // inheriting settings from the default section.
895 //
896 // - Testing network-specific settings like "-wallet", that may be ignored
897 // outside a network section, and non-network specific settings like "-server"
898 // that aren't sensitive to the network.
899 //
903  static constexpr int MAX_ACTIONS = 3;
904 
905  enum Action { NONE, SET, NEGATE, SECTION_SET, SECTION_NEGATE };
906  using ActionList = Action[MAX_ACTIONS];
907 
909  template <typename Fn>
910  void ForEachMergeSetup(Fn&& fn)
911  {
912  ActionList arg_actions = {};
913  // command_line_options do not have sections. Only iterate over SET and NEGATE
914  ForEachNoDup(arg_actions, SET, NEGATE, [&] {
915  ActionList conf_actions = {};
916  ForEachNoDup(conf_actions, SET, SECTION_NEGATE, [&] {
917  for (bool soft_set : {false, true}) {
918  for (bool force_set : {false, true}) {
919  for (const std::string& section : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::SIGNET}) {
920  for (const std::string& network : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::SIGNET}) {
921  for (bool net_specific : {false, true}) {
922  fn(arg_actions, conf_actions, soft_set, force_set, section, network, net_specific);
923  }
924  }
925  }
926  }
927  }
928  });
929  });
930  }
931 
933  std::vector<std::string> GetValues(const ActionList& actions,
934  const std::string& section,
935  const std::string& name,
936  const std::string& value_prefix)
937  {
938  std::vector<std::string> values;
939  int suffix = 0;
940  for (Action action : actions) {
941  if (action == NONE) break;
942  std::string prefix;
943  if (action == SECTION_SET || action == SECTION_NEGATE) prefix = section + ".";
944  if (action == SET || action == SECTION_SET) {
945  for (int i = 0; i < 2; ++i) {
946  values.push_back(prefix + name + "=" + value_prefix + ToString(++suffix));
947  }
948  }
949  if (action == NEGATE || action == SECTION_NEGATE) {
950  values.push_back(prefix + "no" + name + "=1");
951  }
952  }
953  return values;
954  }
955 };
956 
957 // Regression test covering different ways config settings can be merged. The
958 // test parses and merges settings, representing the results as strings that get
959 // compared against an expected hash. To debug, the result strings can be dumped
960 // to a file (see comments below).
962 {
963  CHash256 out_sha;
964  FILE* out_file = nullptr;
965  if (const char* out_path = getenv("ARGS_MERGE_TEST_OUT")) {
966  out_file = fsbridge::fopen(out_path, "w");
967  if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
968  }
969 
970  ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions, bool soft_set, bool force_set,
971  const std::string& section, const std::string& network, bool net_specific) {
972  TestArgsManager parser;
973  LOCK(parser.cs_args);
974 
975  std::string desc = "net=";
976  desc += network;
977  parser.m_network = network;
978 
979  const std::string& name = net_specific ? "wallet" : "server";
980  const std::string key = "-" + name;
982  if (net_specific) parser.SetNetworkOnlyArg(key);
983 
984  auto args = GetValues(arg_actions, section, name, "a");
985  std::vector<const char*> argv = {"ignored"};
986  for (auto& arg : args) {
987  arg.insert(0, "-");
988  desc += " ";
989  desc += arg;
990  argv.push_back(arg.c_str());
991  }
992  std::string error;
993  BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));
995 
996  std::string conf;
997  for (auto& conf_val : GetValues(conf_actions, section, name, "c")) {
998  desc += " ";
999  desc += conf_val;
1000  conf += conf_val;
1001  conf += "\n";
1002  }
1003  std::istringstream conf_stream(conf);
1004  BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));
1005  BOOST_CHECK_EQUAL(error, "");
1006 
1007  if (soft_set) {
1008  desc += " soft";
1009  parser.SoftSetArg(key, "soft1");
1010  parser.SoftSetArg(key, "soft2");
1011  }
1012 
1013  if (force_set) {
1014  desc += " force";
1015  parser.ForceSetArg(key, "force1");
1016  parser.ForceSetArg(key, "force2");
1017  }
1018 
1019  desc += " || ";
1020 
1021  if (!parser.IsArgSet(key)) {
1022  desc += "unset";
1023  BOOST_CHECK(!parser.IsArgNegated(key));
1024  BOOST_CHECK_EQUAL(parser.GetArg(key, "default"), "default");
1025  BOOST_CHECK(parser.GetArgs(key).empty());
1026  } else if (parser.IsArgNegated(key)) {
1027  desc += "negated";
1028  BOOST_CHECK_EQUAL(parser.GetArg(key, "default"), "0");
1029  BOOST_CHECK(parser.GetArgs(key).empty());
1030  } else {
1031  desc += parser.GetArg(key, "default");
1032  desc += " |";
1033  for (const auto& arg : parser.GetArgs(key)) {
1034  desc += " ";
1035  desc += arg;
1036  }
1037  }
1038 
1039  std::set<std::string> ignored = parser.GetUnsuitableSectionOnlyArgs();
1040  if (!ignored.empty()) {
1041  desc += " | ignored";
1042  for (const auto& arg : ignored) {
1043  desc += " ";
1044  desc += arg;
1045  }
1046  }
1047 
1048  desc += "\n";
1049 
1050  out_sha.Write(MakeUCharSpan(desc));
1051  if (out_file) {
1052  BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
1053  }
1054  });
1055 
1056  if (out_file) {
1057  if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
1058  out_file = nullptr;
1059  }
1060 
1061  unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
1062  out_sha.Finalize(out_sha_bytes);
1063  std::string out_sha_hex = HexStr(out_sha_bytes);
1064 
1065  // If check below fails, should manually dump the results with:
1066  //
1067  // ARGS_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=util_tests/util_ArgsMerge
1068  //
1069  // And verify diff against previous results to make sure the changes are expected.
1070  //
1071  // Results file is formatted like:
1072  //
1073  // <input> || <IsArgSet/IsArgNegated/GetArg output> | <GetArgs output> | <GetUnsuitable output>
1074  BOOST_CHECK_EQUAL(out_sha_hex, "d1e436c1cd510d0ec44d5205d4b4e3bee6387d316e0075c58206cb16603f3d82");
1075 }
1076 
1077 // Similar test as above, but for ArgsManager::GetChainName function.
1079  static constexpr int MAX_ACTIONS = 2;
1080 
1081  enum Action { NONE, ENABLE_TEST, DISABLE_TEST, NEGATE_TEST, ENABLE_REG, DISABLE_REG, NEGATE_REG };
1082  using ActionList = Action[MAX_ACTIONS];
1083 
1085  template <typename Fn>
1086  void ForEachMergeSetup(Fn&& fn)
1087  {
1088  ActionList arg_actions = {};
1089  ForEachNoDup(arg_actions, ENABLE_TEST, NEGATE_REG, [&] {
1090  ActionList conf_actions = {};
1091  ForEachNoDup(conf_actions, ENABLE_TEST, NEGATE_REG, [&] { fn(arg_actions, conf_actions); });
1092  });
1093  }
1094 };
1095 
1097 {
1098  CHash256 out_sha;
1099  FILE* out_file = nullptr;
1100  if (const char* out_path = getenv("CHAIN_MERGE_TEST_OUT")) {
1101  out_file = fsbridge::fopen(out_path, "w");
1102  if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
1103  }
1104 
1105  ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions) {
1106  TestArgsManager parser;
1107  LOCK(parser.cs_args);
1108  parser.AddArg("-regtest", "regtest", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
1109  parser.AddArg("-testnet", "testnet", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
1110 
1111  auto arg = [](Action action) { return action == ENABLE_TEST ? "-testnet=1" :
1112  action == DISABLE_TEST ? "-testnet=0" :
1113  action == NEGATE_TEST ? "-notestnet=1" :
1114  action == ENABLE_REG ? "-regtest=1" :
1115  action == DISABLE_REG ? "-regtest=0" :
1116  action == NEGATE_REG ? "-noregtest=1" : nullptr; };
1117 
1118  std::string desc;
1119  std::vector<const char*> argv = {"ignored"};
1120  for (Action action : arg_actions) {
1121  const char* argstr = arg(action);
1122  if (!argstr) break;
1123  argv.push_back(argstr);
1124  desc += " ";
1125  desc += argv.back();
1126  }
1127  std::string error;
1128  BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));
1129  BOOST_CHECK_EQUAL(error, "");
1130 
1131  std::string conf;
1132  for (Action action : conf_actions) {
1133  const char* argstr = arg(action);
1134  if (!argstr) break;
1135  desc += " ";
1136  desc += argstr + 1;
1137  conf += argstr + 1;
1138  conf += "\n";
1139  }
1140  std::istringstream conf_stream(conf);
1141  BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));
1142  BOOST_CHECK_EQUAL(error, "");
1143 
1144  desc += " || ";
1145  try {
1146  desc += parser.GetChainName();
1147  } catch (const std::runtime_error& e) {
1148  desc += "error: ";
1149  desc += e.what();
1150  }
1151  desc += "\n";
1152 
1153  out_sha.Write(MakeUCharSpan(desc));
1154  if (out_file) {
1155  BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
1156  }
1157  });
1158 
1159  if (out_file) {
1160  if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
1161  out_file = nullptr;
1162  }
1163 
1164  unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
1165  out_sha.Finalize(out_sha_bytes);
1166  std::string out_sha_hex = HexStr(out_sha_bytes);
1167 
1168  // If check below fails, should manually dump the results with:
1169  //
1170  // CHAIN_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=util_tests/util_ChainMerge
1171  //
1172  // And verify diff against previous results to make sure the changes are expected.
1173  //
1174  // Results file is formatted like:
1175  //
1176  // <input> || <output>
1177  BOOST_CHECK_EQUAL(out_sha_hex, "f263493e300023b6509963887444c41386f44b63bc30047eb8402e8c1144854c");
1178 }
1179 
1180 BOOST_AUTO_TEST_CASE(util_ReadWriteSettings)
1181 {
1182  // Test writing setting.
1183  TestArgsManager args1;
1184  args1.ForceSetArg("-datadir", m_path_root.string());
1185  args1.LockSettings([&](util::Settings& settings) { settings.rw_settings["name"] = "value"; });
1186  args1.WriteSettingsFile();
1187 
1188  // Test reading setting.
1189  TestArgsManager args2;
1190  args2.ForceSetArg("-datadir", m_path_root.string());
1191  args2.ReadSettingsFile();
1192  args2.LockSettings([&](util::Settings& settings) { BOOST_CHECK_EQUAL(settings.rw_settings["name"].get_str(), "value"); });
1193 
1194  // Test error logging, and remove previously written setting.
1195  {
1196  ASSERT_DEBUG_LOG("Failed renaming settings file");
1197  fs::remove(args1.GetDataDirBase() / "settings.json");
1198  fs::create_directory(args1.GetDataDirBase() / "settings.json");
1199  args2.WriteSettingsFile();
1200  fs::remove(args1.GetDataDirBase() / "settings.json");
1201  }
1202 }
1203 
1204 BOOST_AUTO_TEST_CASE(util_FormatMoney)
1205 {
1206  BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
1207  BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789");
1208  BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00");
1209 
1210  BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00");
1211  BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00");
1212  BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00");
1213  BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00");
1214  BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00");
1215  BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00");
1216  BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00");
1217  BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00");
1219  BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10");
1220  BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01");
1221  BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001");
1222  BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001");
1223  BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001");
1224  BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
1225  BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
1226  BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
1227 
1228  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max()), "92233720368.54775807");
1229  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 1), "92233720368.54775806");
1230  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 2), "92233720368.54775805");
1231  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 3), "92233720368.54775804");
1232  // ...
1233  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 3), "-92233720368.54775805");
1234  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 2), "-92233720368.54775806");
1235  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 1), "-92233720368.54775807");
1236  BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min()), "-92233720368.54775808");
1237 }
1238 
1239 BOOST_AUTO_TEST_CASE(util_ParseMoney)
1240 {
1241  BOOST_CHECK_EQUAL(ParseMoney("0.0").value(), 0);
1242 
1243  BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789);
1244 
1245  BOOST_CHECK_EQUAL(ParseMoney("10000000.00").value(), COIN*10000000);
1246  BOOST_CHECK_EQUAL(ParseMoney("1000000.00").value(), COIN*1000000);
1247  BOOST_CHECK_EQUAL(ParseMoney("100000.00").value(), COIN*100000);
1248  BOOST_CHECK_EQUAL(ParseMoney("10000.00").value(), COIN*10000);
1249  BOOST_CHECK_EQUAL(ParseMoney("1000.00").value(), COIN*1000);
1250  BOOST_CHECK_EQUAL(ParseMoney("100.00").value(), COIN*100);
1251  BOOST_CHECK_EQUAL(ParseMoney("10.00").value(), COIN*10);
1252  BOOST_CHECK_EQUAL(ParseMoney("1.00").value(), COIN);
1253  BOOST_CHECK_EQUAL(ParseMoney("1").value(), COIN);
1254  BOOST_CHECK_EQUAL(ParseMoney(" 1").value(), COIN);
1255  BOOST_CHECK_EQUAL(ParseMoney("1 ").value(), COIN);
1256  BOOST_CHECK_EQUAL(ParseMoney(" 1 ").value(), COIN);
1257  BOOST_CHECK_EQUAL(ParseMoney("0.1").value(), COIN/10);
1258  BOOST_CHECK_EQUAL(ParseMoney("0.01").value(), COIN/100);
1259  BOOST_CHECK_EQUAL(ParseMoney("0.001").value(), COIN/1000);
1260  BOOST_CHECK_EQUAL(ParseMoney("0.0001").value(), COIN/10000);
1261  BOOST_CHECK_EQUAL(ParseMoney("0.00001").value(), COIN/100000);
1262  BOOST_CHECK_EQUAL(ParseMoney("0.000001").value(), COIN/1000000);
1263  BOOST_CHECK_EQUAL(ParseMoney("0.0000001").value(), COIN/10000000);
1264  BOOST_CHECK_EQUAL(ParseMoney("0.00000001").value(), COIN/100000000);
1265  BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001 ").value(), COIN/100000000);
1266  BOOST_CHECK_EQUAL(ParseMoney("0.00000001 ").value(), COIN/100000000);
1267  BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001").value(), COIN/100000000);
1268 
1269  // Parsing amount that can not be represented should fail
1270  BOOST_CHECK(!ParseMoney("100000000.00"));
1271  BOOST_CHECK(!ParseMoney("0.000000001"));
1272 
1273  // Parsing empty string should fail
1274  BOOST_CHECK(!ParseMoney(""));
1275  BOOST_CHECK(!ParseMoney(" "));
1276  BOOST_CHECK(!ParseMoney(" "));
1277 
1278  // Parsing two numbers should fail
1279  BOOST_CHECK(!ParseMoney("1 2"));
1280  BOOST_CHECK(!ParseMoney(" 1 2 "));
1281  BOOST_CHECK(!ParseMoney(" 1.2 3 "));
1282  BOOST_CHECK(!ParseMoney(" 1 2.3 "));
1283 
1284  // Attempted 63 bit overflow should fail
1285  BOOST_CHECK(!ParseMoney("92233720368.54775808"));
1286 
1287  // Parsing negative amounts must fail
1288  BOOST_CHECK(!ParseMoney("-1"));
1289 
1290  // Parsing strings with embedded NUL characters should fail
1291  BOOST_CHECK(!ParseMoney("\0-1"s));
1293  BOOST_CHECK(!ParseMoney("1\0"s));
1294 }
1295 
1297 {
1298  BOOST_CHECK(IsHex("00"));
1299  BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
1300  BOOST_CHECK(IsHex("ff"));
1301  BOOST_CHECK(IsHex("FF"));
1302 
1303  BOOST_CHECK(!IsHex(""));
1304  BOOST_CHECK(!IsHex("0"));
1305  BOOST_CHECK(!IsHex("a"));
1306  BOOST_CHECK(!IsHex("eleven"));
1307  BOOST_CHECK(!IsHex("00xx00"));
1308  BOOST_CHECK(!IsHex("0x0000"));
1309 }
1310 
1311 BOOST_AUTO_TEST_CASE(util_IsHexNumber)
1312 {
1313  BOOST_CHECK(IsHexNumber("0x0"));
1314  BOOST_CHECK(IsHexNumber("0"));
1315  BOOST_CHECK(IsHexNumber("0x10"));
1316  BOOST_CHECK(IsHexNumber("10"));
1317  BOOST_CHECK(IsHexNumber("0xff"));
1318  BOOST_CHECK(IsHexNumber("ff"));
1319  BOOST_CHECK(IsHexNumber("0xFfa"));
1320  BOOST_CHECK(IsHexNumber("Ffa"));
1321  BOOST_CHECK(IsHexNumber("0x00112233445566778899aabbccddeeffAABBCCDDEEFF"));
1322  BOOST_CHECK(IsHexNumber("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
1323 
1324  BOOST_CHECK(!IsHexNumber("")); // empty string not allowed
1325  BOOST_CHECK(!IsHexNumber("0x")); // empty string after prefix not allowed
1326  BOOST_CHECK(!IsHexNumber("0x0 ")); // no spaces at end,
1327  BOOST_CHECK(!IsHexNumber(" 0x0")); // or beginning,
1328  BOOST_CHECK(!IsHexNumber("0x 0")); // or middle,
1329  BOOST_CHECK(!IsHexNumber(" ")); // etc.
1330  BOOST_CHECK(!IsHexNumber("0x0ga")); // invalid character
1331  BOOST_CHECK(!IsHexNumber("x0")); // broken prefix
1332  BOOST_CHECK(!IsHexNumber("0x0x00")); // two prefixes not allowed
1333 
1334 }
1335 
1336 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
1337 {
1339  for (int mod=2;mod<11;mod++)
1340  {
1341  int mask = 1;
1342  // Really rough binomial confidence approximation.
1343  int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
1344  //mask is 2^ceil(log2(mod))-1
1345  while(mask<mod-1)mask=(mask<<1)+1;
1346 
1347  int count = 0;
1348  //How often does it get a zero from the uniform range [0,mod)?
1349  for (int i = 0; i < 10000; i++) {
1350  uint32_t rval;
1351  do{
1352  rval=InsecureRand32()&mask;
1353  }while(rval>=(uint32_t)mod);
1354  count += rval==0;
1355  }
1356  BOOST_CHECK(count<=10000/mod+err);
1357  BOOST_CHECK(count>=10000/mod-err);
1358  }
1359 }
1360 
1361 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
1362 {
1363  BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
1364  BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
1365  BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
1366  BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
1367  BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
1368  BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
1369  BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
1370 }
1371 
1372 /* Test strprintf formatting directives.
1373  * Put a string before and after to ensure sanity of element sizes on stack. */
1374 #define B "check_prefix"
1375 #define E "check_postfix"
1376 BOOST_AUTO_TEST_CASE(strprintf_numbers)
1377 {
1378  int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
1379  uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
1380  BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
1381  BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
1382  BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
1383 
1384  size_t st = 12345678; /* unsigned size_t test value */
1385  ssize_t sst = -12345678; /* signed size_t test value */
1386  BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
1387  BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
1388  BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
1389 
1390  ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
1391  ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
1392  BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
1393  BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
1394  BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
1395 }
1396 #undef B
1397 #undef E
1398 
1399 /* Check for mingw/wine issue #3494
1400  * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
1401  */
1403 {
1404  BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
1405 }
1406 
1407 BOOST_AUTO_TEST_CASE(util_time_GetTime)
1408 {
1409  SetMockTime(111);
1410  // Check that mock time does not change after a sleep
1411  for (const auto& num_sleep : {0, 1}) {
1412  UninterruptibleSleep(std::chrono::milliseconds{num_sleep});
1413  BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
1414  BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
1415  BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
1416  BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
1417  }
1418 
1419  SetMockTime(0);
1420  // Check that system time changes after a sleep
1421  const auto ms_0 = GetTime<std::chrono::milliseconds>();
1422  const auto us_0 = GetTime<std::chrono::microseconds>();
1423  UninterruptibleSleep(std::chrono::milliseconds{1});
1424  BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
1425  BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
1426 }
1427 
1429 {
1430  BOOST_CHECK_EQUAL(IsDigit('0'), true);
1431  BOOST_CHECK_EQUAL(IsDigit('1'), true);
1432  BOOST_CHECK_EQUAL(IsDigit('8'), true);
1433  BOOST_CHECK_EQUAL(IsDigit('9'), true);
1434 
1435  BOOST_CHECK_EQUAL(IsDigit('0' - 1), false);
1436  BOOST_CHECK_EQUAL(IsDigit('9' + 1), false);
1437  BOOST_CHECK_EQUAL(IsDigit(0), false);
1438  BOOST_CHECK_EQUAL(IsDigit(1), false);
1439  BOOST_CHECK_EQUAL(IsDigit(8), false);
1440  BOOST_CHECK_EQUAL(IsDigit(9), false);
1441 }
1442 
1443 BOOST_AUTO_TEST_CASE(test_ParseInt32)
1444 {
1445  int32_t n;
1446  // Valid values
1447  BOOST_CHECK(ParseInt32("1234", nullptr));
1448  BOOST_CHECK(ParseInt32("0", &n) && n == 0);
1449  BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
1450  BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
1451  BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
1452  BOOST_CHECK(ParseInt32("-2147483648", &n) && n == (-2147483647 - 1)); // (-2147483647 - 1) equals INT_MIN
1453  BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
1454  BOOST_CHECK(ParseInt32("00000000000000001234", &n) && n == 1234);
1455  BOOST_CHECK(ParseInt32("-00000000000000001234", &n) && n == -1234);
1456  BOOST_CHECK(ParseInt32("00000000000000000000", &n) && n == 0);
1457  BOOST_CHECK(ParseInt32("-00000000000000000000", &n) && n == 0);
1458  // Invalid values
1459  BOOST_CHECK(!ParseInt32("", &n));
1460  BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
1461  BOOST_CHECK(!ParseInt32("1 ", &n));
1462  BOOST_CHECK(!ParseInt32("++1", &n));
1463  BOOST_CHECK(!ParseInt32("+-1", &n));
1464  BOOST_CHECK(!ParseInt32("-+1", &n));
1465  BOOST_CHECK(!ParseInt32("--1", &n));
1466  BOOST_CHECK(!ParseInt32("1a", &n));
1467  BOOST_CHECK(!ParseInt32("aap", &n));
1468  BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
1470  // Overflow and underflow
1471  BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
1472  BOOST_CHECK(!ParseInt32("2147483648", nullptr));
1473  BOOST_CHECK(!ParseInt32("-32482348723847471234", nullptr));
1474  BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
1475 }
1476 
1477 BOOST_AUTO_TEST_CASE(test_ParseInt64)
1478 {
1479  int64_t n;
1480  // Valid values
1481  BOOST_CHECK(ParseInt64("1234", nullptr));
1482  BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
1483  BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
1484  BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
1485  BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
1486  BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
1487  BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807);
1488  BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1);
1489  BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
1490  // Invalid values
1491  BOOST_CHECK(!ParseInt64("", &n));
1492  BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
1493  BOOST_CHECK(!ParseInt64("1 ", &n));
1494  BOOST_CHECK(!ParseInt64("1a", &n));
1495  BOOST_CHECK(!ParseInt64("aap", &n));
1496  BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
1498  // Overflow and underflow
1499  BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
1500  BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
1501  BOOST_CHECK(!ParseInt64("-32482348723847471234", nullptr));
1502  BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
1503 }
1504 
1505 BOOST_AUTO_TEST_CASE(test_ParseUInt8)
1506 {
1507  uint8_t n;
1508  // Valid values
1509  BOOST_CHECK(ParseUInt8("255", nullptr));
1510  BOOST_CHECK(ParseUInt8("0", &n) && n == 0);
1511  BOOST_CHECK(ParseUInt8("255", &n) && n == 255);
1512  BOOST_CHECK(ParseUInt8("0255", &n) && n == 255); // no octal
1513  BOOST_CHECK(ParseUInt8("255", &n) && n == static_cast<uint8_t>(255));
1514  BOOST_CHECK(ParseUInt8("+255", &n) && n == 255);
1515  BOOST_CHECK(ParseUInt8("00000000000000000012", &n) && n == 12);
1516  BOOST_CHECK(ParseUInt8("00000000000000000000", &n) && n == 0);
1517  // Invalid values
1518  BOOST_CHECK(!ParseUInt8("-00000000000000000000", &n));
1519  BOOST_CHECK(!ParseUInt8("", &n));
1520  BOOST_CHECK(!ParseUInt8(" 1", &n)); // no padding inside
1521  BOOST_CHECK(!ParseUInt8(" -1", &n));
1522  BOOST_CHECK(!ParseUInt8("++1", &n));
1523  BOOST_CHECK(!ParseUInt8("+-1", &n));
1524  BOOST_CHECK(!ParseUInt8("-+1", &n));
1525  BOOST_CHECK(!ParseUInt8("--1", &n));
1526  BOOST_CHECK(!ParseUInt8("-1", &n));
1527  BOOST_CHECK(!ParseUInt8("1 ", &n));
1528  BOOST_CHECK(!ParseUInt8("1a", &n));
1529  BOOST_CHECK(!ParseUInt8("aap", &n));
1530  BOOST_CHECK(!ParseUInt8("0x1", &n)); // no hex
1532  // Overflow and underflow
1533  BOOST_CHECK(!ParseUInt8("-255", &n));
1534  BOOST_CHECK(!ParseUInt8("256", &n));
1535  BOOST_CHECK(!ParseUInt8("-123", &n));
1536  BOOST_CHECK(!ParseUInt8("-123", nullptr));
1537  BOOST_CHECK(!ParseUInt8("256", nullptr));
1538 }
1539 
1540 BOOST_AUTO_TEST_CASE(test_ParseUInt16)
1541 {
1542  uint16_t n;
1543  // Valid values
1544  BOOST_CHECK(ParseUInt16("1234", nullptr));
1545  BOOST_CHECK(ParseUInt16("0", &n) && n == 0);
1546  BOOST_CHECK(ParseUInt16("1234", &n) && n == 1234);
1547  BOOST_CHECK(ParseUInt16("01234", &n) && n == 1234); // no octal
1548  BOOST_CHECK(ParseUInt16("65535", &n) && n == static_cast<uint16_t>(65535));
1549  BOOST_CHECK(ParseUInt16("+65535", &n) && n == 65535);
1550  BOOST_CHECK(ParseUInt16("00000000000000000012", &n) && n == 12);
1551  BOOST_CHECK(ParseUInt16("00000000000000000000", &n) && n == 0);
1552  // Invalid values
1553  BOOST_CHECK(!ParseUInt16("-00000000000000000000", &n));
1554  BOOST_CHECK(!ParseUInt16("", &n));
1555  BOOST_CHECK(!ParseUInt16(" 1", &n)); // no padding inside
1556  BOOST_CHECK(!ParseUInt16(" -1", &n));
1557  BOOST_CHECK(!ParseUInt16("++1", &n));
1558  BOOST_CHECK(!ParseUInt16("+-1", &n));
1559  BOOST_CHECK(!ParseUInt16("-+1", &n));
1560  BOOST_CHECK(!ParseUInt16("--1", &n));
1561  BOOST_CHECK(!ParseUInt16("-1", &n));
1562  BOOST_CHECK(!ParseUInt16("1 ", &n));
1563  BOOST_CHECK(!ParseUInt16("1a", &n));
1564  BOOST_CHECK(!ParseUInt16("aap", &n));
1565  BOOST_CHECK(!ParseUInt16("0x1", &n)); // no hex
1567  // Overflow and underflow
1568  BOOST_CHECK(!ParseUInt16("-65535", &n));
1569  BOOST_CHECK(!ParseUInt16("65536", &n));
1570  BOOST_CHECK(!ParseUInt16("-123", &n));
1571  BOOST_CHECK(!ParseUInt16("-123", nullptr));
1572  BOOST_CHECK(!ParseUInt16("65536", nullptr));
1573 }
1574 
1575 BOOST_AUTO_TEST_CASE(test_ParseUInt32)
1576 {
1577  uint32_t n;
1578  // Valid values
1579  BOOST_CHECK(ParseUInt32("1234", nullptr));
1580  BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
1581  BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
1582  BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
1583  BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
1584  BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648);
1585  BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295);
1586  BOOST_CHECK(ParseUInt32("+1234", &n) && n == 1234);
1587  BOOST_CHECK(ParseUInt32("00000000000000001234", &n) && n == 1234);
1588  BOOST_CHECK(ParseUInt32("00000000000000000000", &n) && n == 0);
1589  // Invalid values
1590  BOOST_CHECK(!ParseUInt32("-00000000000000000000", &n));
1591  BOOST_CHECK(!ParseUInt32("", &n));
1592  BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
1593  BOOST_CHECK(!ParseUInt32(" -1", &n));
1594  BOOST_CHECK(!ParseUInt32("++1", &n));
1595  BOOST_CHECK(!ParseUInt32("+-1", &n));
1596  BOOST_CHECK(!ParseUInt32("-+1", &n));
1597  BOOST_CHECK(!ParseUInt32("--1", &n));
1598  BOOST_CHECK(!ParseUInt32("-1", &n));
1599  BOOST_CHECK(!ParseUInt32("1 ", &n));
1600  BOOST_CHECK(!ParseUInt32("1a", &n));
1601  BOOST_CHECK(!ParseUInt32("aap", &n));
1602  BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
1604  // Overflow and underflow
1605  BOOST_CHECK(!ParseUInt32("-2147483648", &n));
1606  BOOST_CHECK(!ParseUInt32("4294967296", &n));
1607  BOOST_CHECK(!ParseUInt32("-1234", &n));
1608  BOOST_CHECK(!ParseUInt32("-32482348723847471234", nullptr));
1609  BOOST_CHECK(!ParseUInt32("32482348723847471234", nullptr));
1610 }
1611 
1612 BOOST_AUTO_TEST_CASE(test_ParseUInt64)
1613 {
1614  uint64_t n;
1615  // Valid values
1616  BOOST_CHECK(ParseUInt64("1234", nullptr));
1617  BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
1618  BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
1619  BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
1620  BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
1621  BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
1622  BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
1623  BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
1624  // Invalid values
1625  BOOST_CHECK(!ParseUInt64("", &n));
1626  BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
1627  BOOST_CHECK(!ParseUInt64(" -1", &n));
1628  BOOST_CHECK(!ParseUInt64("1 ", &n));
1629  BOOST_CHECK(!ParseUInt64("1a", &n));
1630  BOOST_CHECK(!ParseUInt64("aap", &n));
1631  BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
1633  // Overflow and underflow
1634  BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
1635  BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
1636  BOOST_CHECK(!ParseUInt64("-32482348723847471234", nullptr));
1637  BOOST_CHECK(!ParseUInt64("-2147483648", &n));
1638  BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
1639  BOOST_CHECK(!ParseUInt64("-1234", &n));
1640 }
1641 
1642 BOOST_AUTO_TEST_CASE(test_ParseDouble)
1643 {
1644  double n;
1645  // Valid values
1646  BOOST_CHECK(ParseDouble("1234", nullptr));
1647  BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
1648  BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
1649  BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
1650  BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
1651  BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
1652  BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
1653  BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
1654  BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
1655  // Invalid values
1656  BOOST_CHECK(!ParseDouble("", &n));
1657  BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
1658  BOOST_CHECK(!ParseDouble("1 ", &n));
1659  BOOST_CHECK(!ParseDouble("1a", &n));
1660  BOOST_CHECK(!ParseDouble("aap", &n));
1661  BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
1663  // Overflow and underflow
1664  BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
1665  BOOST_CHECK(!ParseDouble("1e10000", nullptr));
1666 }
1667 
1668 BOOST_AUTO_TEST_CASE(test_FormatParagraph)
1669 {
1670  BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
1671  BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
1672  BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
1673  BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
1674  BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
1675  BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
1676  BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
1677 
1678  // Make sure we don't indent a fully-new line following a too-long line ending
1679  BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc");
1680 
1681  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");
1682 
1683  // Test wrap length is exact
1684  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");
1685  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");
1686  // Indent should be included in length of lines
1687  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");
1688 
1689  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.");
1690  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.");
1691  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.");
1692  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.");
1693 }
1694 
1695 BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
1696 {
1697  std::vector<std::string> comments;
1698  comments.push_back(std::string("comment1"));
1699  std::vector<std::string> comments2;
1700  comments2.push_back(std::string("comment1"));
1701  comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
1702  BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:9.99.0/"));
1703  BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:9.99.0(comment1)/"));
1704  BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:9.99.0(comment1; Comment2; .,_?@-; )/"));
1705 }
1706 
1707 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
1708 {
1709  int64_t amount = 0;
1710  BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
1711  BOOST_CHECK_EQUAL(amount, 0LL);
1712  BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
1713  BOOST_CHECK_EQUAL(amount, 100000000LL);
1714  BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
1715  BOOST_CHECK_EQUAL(amount, 0LL);
1716  BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
1717  BOOST_CHECK_EQUAL(amount, -10000000LL);
1718  BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
1719  BOOST_CHECK_EQUAL(amount, 110000000LL);
1720  BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
1721  BOOST_CHECK_EQUAL(amount, 110000000LL);
1722  BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
1723  BOOST_CHECK_EQUAL(amount, 1100000000LL);
1724  BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
1725  BOOST_CHECK_EQUAL(amount, 11000000LL);
1726  BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
1727  BOOST_CHECK_EQUAL(amount, 100000000000LL);
1728  BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
1729  BOOST_CHECK_EQUAL(amount, -100000000000LL);
1730  BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
1731  BOOST_CHECK_EQUAL(amount, 1LL);
1732  BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
1733  BOOST_CHECK_EQUAL(amount, 1LL);
1734  BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
1735  BOOST_CHECK_EQUAL(amount, -1LL);
1736  BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
1737  BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
1738  BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
1739  BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
1740  BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
1741  BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
1742 
1743  BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
1744  BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
1745  BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
1746  BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
1747  BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
1748  BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
1749  BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
1750  BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
1751  BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
1752  BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
1753  BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
1754  BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
1755  BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
1756  BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
1757  BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
1758  BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
1759  BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
1760  BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
1761  BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
1762  BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
1763  BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
1764  BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
1765  BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
1766  BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
1767  BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
1768  BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
1769  BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
1770 
1771  // Test with 3 decimal places for fee rates in sat/vB.
1772  BOOST_CHECK(ParseFixedPoint("0.001", 3, &amount));
1773  BOOST_CHECK_EQUAL(amount, CAmount{1});
1774  BOOST_CHECK(!ParseFixedPoint("0.0009", 3, &amount));
1775  BOOST_CHECK(!ParseFixedPoint("31.00100001", 3, &amount));
1776  BOOST_CHECK(!ParseFixedPoint("31.0011", 3, &amount));
1777  BOOST_CHECK(!ParseFixedPoint("31.99999999", 3, &amount));
1778  BOOST_CHECK(!ParseFixedPoint("31.999999999999999999999", 3, &amount));
1779 }
1780 
1781 static void TestOtherThread(fs::path dirname, std::string lockname, bool *result)
1782 {
1783  *result = LockDirectory(dirname, lockname);
1784 }
1785 
1786 #ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork()
1787 static constexpr char LockCommand = 'L';
1788 static constexpr char UnlockCommand = 'U';
1789 static constexpr char ExitCommand = 'X';
1790 
1791 [[noreturn]] static void TestOtherProcess(fs::path dirname, std::string lockname, int fd)
1792 {
1793  char ch;
1794  while (true) {
1795  int rv = read(fd, &ch, 1); // Wait for command
1796  assert(rv == 1);
1797  switch(ch) {
1798  case LockCommand:
1799  ch = LockDirectory(dirname, lockname);
1800  rv = write(fd, &ch, 1);
1801  assert(rv == 1);
1802  break;
1803  case UnlockCommand:
1805  ch = true; // Always succeeds
1806  rv = write(fd, &ch, 1);
1807  assert(rv == 1);
1808  break;
1809  case ExitCommand:
1810  close(fd);
1811  exit(0);
1812  default:
1813  assert(0);
1814  }
1815  }
1816 }
1817 #endif
1818 
1819 BOOST_AUTO_TEST_CASE(test_LockDirectory)
1820 {
1821  fs::path dirname = m_args.GetDataDirBase() / "lock_dir";
1822  const std::string lockname = ".lock";
1823 #ifndef WIN32
1824  // Revert SIGCHLD to default, otherwise boost.test will catch and fail on
1825  // it: there is BOOST_TEST_IGNORE_SIGCHLD but that only works when defined
1826  // at build-time of the boost library
1827  void (*old_handler)(int) = signal(SIGCHLD, SIG_DFL);
1828 
1829  // Fork another process for testing before creating the lock, so that we
1830  // won't fork while holding the lock (which might be undefined, and is not
1831  // relevant as test case as that is avoided with -daemonize).
1832  int fd[2];
1833  BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fd), 0);
1834  pid_t pid = fork();
1835  if (!pid) {
1836  BOOST_CHECK_EQUAL(close(fd[1]), 0); // Child: close parent end
1837  TestOtherProcess(dirname, lockname, fd[0]);
1838  }
1839  BOOST_CHECK_EQUAL(close(fd[0]), 0); // Parent: close child end
1840 #endif
1841  // Lock on non-existent directory should fail
1842  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), false);
1843 
1844  fs::create_directories(dirname);
1845 
1846  // Probing lock on new directory should succeed
1847  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1848 
1849  // Persistent lock on new directory should succeed
1850  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), true);
1851 
1852  // Another lock on the directory from the same thread should succeed
1853  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), true);
1854 
1855  // Another lock on the directory from a different thread within the same process should succeed
1856  bool threadresult;
1857  std::thread thr(TestOtherThread, dirname, lockname, &threadresult);
1858  thr.join();
1859  BOOST_CHECK_EQUAL(threadresult, true);
1860 #ifndef WIN32
1861  // Try to acquire lock in child process while we're holding it, this should fail.
1862  char ch;
1863  BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1864  BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1865  BOOST_CHECK_EQUAL((bool)ch, false);
1866 
1867  // Give up our lock
1869  // Probing lock from our side now should succeed, but not hold on to the lock.
1870  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1871 
1872  // Try to acquire the lock in the child process, this should be successful.
1873  BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1874  BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1875  BOOST_CHECK_EQUAL((bool)ch, true);
1876 
1877  // When we try to probe the lock now, it should fail.
1878  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), false);
1879 
1880  // Unlock the lock in the child process
1881  BOOST_CHECK_EQUAL(write(fd[1], &UnlockCommand, 1), 1);
1882  BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1883  BOOST_CHECK_EQUAL((bool)ch, true);
1884 
1885  // When we try to probe the lock now, it should succeed.
1886  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1887 
1888  // Re-lock the lock in the child process, then wait for it to exit, check
1889  // successful return. After that, we check that exiting the process
1890  // has released the lock as we would expect by probing it.
1891  int processstatus;
1892  BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1893  BOOST_CHECK_EQUAL(write(fd[1], &ExitCommand, 1), 1);
1894  BOOST_CHECK_EQUAL(waitpid(pid, &processstatus, 0), pid);
1895  BOOST_CHECK_EQUAL(processstatus, 0);
1896  BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1897 
1898  // Restore SIGCHLD
1899  signal(SIGCHLD, old_handler);
1900  BOOST_CHECK_EQUAL(close(fd[1]), 0); // Close our side of the socketpair
1901 #endif
1902  // Clean up
1904  fs::remove_all(dirname);
1905 }
1906 
1907 BOOST_AUTO_TEST_CASE(test_DirIsWritable)
1908 {
1909  // Should be able to write to the data dir.
1910  fs::path tmpdirname = m_args.GetDataDirBase();
1911  BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
1912 
1913  // Should not be able to write to a non-existent dir.
1914  tmpdirname = GetUniquePath(tmpdirname);
1915  BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
1916 
1917  fs::create_directory(tmpdirname);
1918  // Should be able to write to it now.
1919  BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
1920  fs::remove(tmpdirname);
1921 }
1922 
1924 {
1925  BOOST_CHECK_EQUAL(ToLower('@'), '@');
1926  BOOST_CHECK_EQUAL(ToLower('A'), 'a');
1927  BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
1928  BOOST_CHECK_EQUAL(ToLower('['), '[');
1929  BOOST_CHECK_EQUAL(ToLower(0), 0);
1930  BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
1931 
1932  BOOST_CHECK_EQUAL(ToLower(""), "");
1933  BOOST_CHECK_EQUAL(ToLower("#HODL"), "#hodl");
1934  BOOST_CHECK_EQUAL(ToLower("\x00\xfe\xff"), "\x00\xfe\xff");
1935 }
1936 
1938 {
1939  BOOST_CHECK_EQUAL(ToUpper('`'), '`');
1940  BOOST_CHECK_EQUAL(ToUpper('a'), 'A');
1941  BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
1942  BOOST_CHECK_EQUAL(ToUpper('{'), '{');
1943  BOOST_CHECK_EQUAL(ToUpper(0), 0);
1944  BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
1945 
1946  BOOST_CHECK_EQUAL(ToUpper(""), "");
1947  BOOST_CHECK_EQUAL(ToUpper("#hodl"), "#HODL");
1948  BOOST_CHECK_EQUAL(ToUpper("\x00\xfe\xff"), "\x00\xfe\xff");
1949 }
1950 
1951 BOOST_AUTO_TEST_CASE(test_Capitalize)
1952 {
1953  BOOST_CHECK_EQUAL(Capitalize(""), "");
1954  BOOST_CHECK_EQUAL(Capitalize("bitcoin"), "Bitcoin");
1955  BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
1956 }
1957 
1958 static std::string SpanToStr(const Span<const char>& span)
1959 {
1960  return std::string(span.begin(), span.end());
1961 }
1962 
1963 BOOST_AUTO_TEST_CASE(test_spanparsing)
1964 {
1965  using namespace spanparsing;
1966  std::string input;
1967  Span<const char> sp;
1968  bool success;
1969 
1970  // Const(...): parse a constant, update span to skip it if successful
1971  input = "MilkToastHoney";
1972  sp = input;
1973  success = Const("", sp); // empty
1974  BOOST_CHECK(success);
1975  BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney");
1976 
1977  success = Const("Milk", sp);
1978  BOOST_CHECK(success);
1979  BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney");
1980 
1981  success = Const("Bread", sp);
1982  BOOST_CHECK(!success);
1983 
1984  success = Const("Toast", sp);
1985  BOOST_CHECK(success);
1986  BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey");
1987 
1988  success = Const("Honeybadger", sp);
1989  BOOST_CHECK(!success);
1990 
1991  success = Const("Honey", sp);
1992  BOOST_CHECK(success);
1993  BOOST_CHECK_EQUAL(SpanToStr(sp), "");
1994 
1995  // Func(...): parse a function call, update span to argument if successful
1996  input = "Foo(Bar(xy,z()))";
1997  sp = input;
1998 
1999  success = Func("FooBar", sp);
2000  BOOST_CHECK(!success);
2001 
2002  success = Func("Foo(", sp);
2003  BOOST_CHECK(!success);
2004 
2005  success = Func("Foo", sp);
2006  BOOST_CHECK(success);
2007  BOOST_CHECK_EQUAL(SpanToStr(sp), "Bar(xy,z())");
2008 
2009  success = Func("Bar", sp);
2010  BOOST_CHECK(success);
2011  BOOST_CHECK_EQUAL(SpanToStr(sp), "xy,z()");
2012 
2013  success = Func("xy", sp);
2014  BOOST_CHECK(!success);
2015 
2016  // Expr(...): return expression that span begins with, update span to skip it
2017  Span<const char> result;
2018 
2019  input = "(n*(n-1))/2";
2020  sp = input;
2021  result = Expr(sp);
2022  BOOST_CHECK_EQUAL(SpanToStr(result), "(n*(n-1))/2");
2023  BOOST_CHECK_EQUAL(SpanToStr(sp), "");
2024 
2025  input = "foo,bar";
2026  sp = input;
2027  result = Expr(sp);
2028  BOOST_CHECK_EQUAL(SpanToStr(result), "foo");
2029  BOOST_CHECK_EQUAL(SpanToStr(sp), ",bar");
2030 
2031  input = "(aaaaa,bbbbb()),c";
2032  sp = input;
2033  result = Expr(sp);
2034  BOOST_CHECK_EQUAL(SpanToStr(result), "(aaaaa,bbbbb())");
2035  BOOST_CHECK_EQUAL(SpanToStr(sp), ",c");
2036 
2037  input = "xyz)foo";
2038  sp = input;
2039  result = Expr(sp);
2040  BOOST_CHECK_EQUAL(SpanToStr(result), "xyz");
2041  BOOST_CHECK_EQUAL(SpanToStr(sp), ")foo");
2042 
2043  input = "((a),(b),(c)),xxx";
2044  sp = input;
2045  result = Expr(sp);
2046  BOOST_CHECK_EQUAL(SpanToStr(result), "((a),(b),(c))");
2047  BOOST_CHECK_EQUAL(SpanToStr(sp), ",xxx");
2048 
2049  // Split(...): split a string on every instance of sep, return vector
2050  std::vector<Span<const char>> results;
2051 
2052  input = "xxx";
2053  results = Split(input, 'x');
2054  BOOST_CHECK_EQUAL(results.size(), 4U);
2055  BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
2056  BOOST_CHECK_EQUAL(SpanToStr(results[1]), "");
2057  BOOST_CHECK_EQUAL(SpanToStr(results[2]), "");
2058  BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
2059 
2060  input = "one#two#three";
2061  results = Split(input, '-');
2062  BOOST_CHECK_EQUAL(results.size(), 1U);
2063  BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one#two#three");
2064 
2065  input = "one#two#three";
2066  results = Split(input, '#');
2067  BOOST_CHECK_EQUAL(results.size(), 3U);
2068  BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one");
2069  BOOST_CHECK_EQUAL(SpanToStr(results[1]), "two");
2070  BOOST_CHECK_EQUAL(SpanToStr(results[2]), "three");
2071 
2072  input = "*foo*bar*";
2073  results = Split(input, '*');
2074  BOOST_CHECK_EQUAL(results.size(), 4U);
2075  BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
2076  BOOST_CHECK_EQUAL(SpanToStr(results[1]), "foo");
2077  BOOST_CHECK_EQUAL(SpanToStr(results[2]), "bar");
2078  BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
2079 }
2080 
2081 BOOST_AUTO_TEST_CASE(test_LogEscapeMessage)
2082 {
2083  // ASCII and UTF-8 must pass through unaltered.
2084  BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Valid log message貓"), "Valid log message貓");
2085  // Newlines must pass through unaltered.
2086  BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Message\n with newlines\n"), "Message\n with newlines\n");
2087  // Other control characters are escaped in C syntax.
2088  BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("\x01\x7f Corrupted log message\x0d"), R"(\x01\x7f Corrupted log message\x0d)");
2089  // Embedded NULL characters are escaped too.
2090  const std::string NUL("O\x00O", 3);
2091  BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage(NUL), R"(O\x00O)");
2092 }
2093 
2094 namespace {
2095 
2096 struct Tracker
2097 {
2099  const Tracker* origin;
2101  int copies;
2102 
2103  Tracker() noexcept : origin(this), copies(0) {}
2104  Tracker(const Tracker& t) noexcept : origin(t.origin), copies(t.copies + 1) {}
2105  Tracker(Tracker&& t) noexcept : origin(t.origin), copies(t.copies) {}
2106  Tracker& operator=(const Tracker& t) noexcept
2107  {
2108  origin = t.origin;
2109  copies = t.copies + 1;
2110  return *this;
2111  }
2112 };
2113 
2114 }
2115 
2116 BOOST_AUTO_TEST_CASE(test_tracked_vector)
2117 {
2118  Tracker t1;
2119  Tracker t2;
2120  Tracker t3;
2121 
2122  BOOST_CHECK(t1.origin == &t1);
2123  BOOST_CHECK(t2.origin == &t2);
2124  BOOST_CHECK(t3.origin == &t3);
2125 
2126  auto v1 = Vector(t1);
2127  BOOST_CHECK_EQUAL(v1.size(), 1U);
2128  BOOST_CHECK(v1[0].origin == &t1);
2129  BOOST_CHECK_EQUAL(v1[0].copies, 1);
2130 
2131  auto v2 = Vector(std::move(t2));
2132  BOOST_CHECK_EQUAL(v2.size(), 1U);
2133  BOOST_CHECK(v2[0].origin == &t2);
2134  BOOST_CHECK_EQUAL(v2[0].copies, 0);
2135 
2136  auto v3 = Vector(t1, std::move(t2));
2137  BOOST_CHECK_EQUAL(v3.size(), 2U);
2138  BOOST_CHECK(v3[0].origin == &t1);
2139  BOOST_CHECK(v3[1].origin == &t2);
2140  BOOST_CHECK_EQUAL(v3[0].copies, 1);
2141  BOOST_CHECK_EQUAL(v3[1].copies, 0);
2142 
2143  auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));
2144  BOOST_CHECK_EQUAL(v4.size(), 3U);
2145  BOOST_CHECK(v4[0].origin == &t1);
2146  BOOST_CHECK(v4[1].origin == &t2);
2147  BOOST_CHECK(v4[2].origin == &t3);
2148  BOOST_CHECK_EQUAL(v4[0].copies, 1);
2149  BOOST_CHECK_EQUAL(v4[1].copies, 1);
2150  BOOST_CHECK_EQUAL(v4[2].copies, 0);
2151 
2152  auto v5 = Cat(v1, v4);
2153  BOOST_CHECK_EQUAL(v5.size(), 4U);
2154  BOOST_CHECK(v5[0].origin == &t1);
2155  BOOST_CHECK(v5[1].origin == &t1);
2156  BOOST_CHECK(v5[2].origin == &t2);
2157  BOOST_CHECK(v5[3].origin == &t3);
2158  BOOST_CHECK_EQUAL(v5[0].copies, 2);
2159  BOOST_CHECK_EQUAL(v5[1].copies, 2);
2160  BOOST_CHECK_EQUAL(v5[2].copies, 2);
2161  BOOST_CHECK_EQUAL(v5[3].copies, 1);
2162 
2163  auto v6 = Cat(std::move(v1), v3);
2164  BOOST_CHECK_EQUAL(v6.size(), 3U);
2165  BOOST_CHECK(v6[0].origin == &t1);
2166  BOOST_CHECK(v6[1].origin == &t1);
2167  BOOST_CHECK(v6[2].origin == &t2);
2168  BOOST_CHECK_EQUAL(v6[0].copies, 1);
2169  BOOST_CHECK_EQUAL(v6[1].copies, 2);
2170  BOOST_CHECK_EQUAL(v6[2].copies, 1);
2171 
2172  auto v7 = Cat(v2, std::move(v4));
2173  BOOST_CHECK_EQUAL(v7.size(), 4U);
2174  BOOST_CHECK(v7[0].origin == &t2);
2175  BOOST_CHECK(v7[1].origin == &t1);
2176  BOOST_CHECK(v7[2].origin == &t2);
2177  BOOST_CHECK(v7[3].origin == &t3);
2178  BOOST_CHECK_EQUAL(v7[0].copies, 1);
2179  BOOST_CHECK_EQUAL(v7[1].copies, 1);
2180  BOOST_CHECK_EQUAL(v7[2].copies, 1);
2181  BOOST_CHECK_EQUAL(v7[3].copies, 0);
2182 
2183  auto v8 = Cat(std::move(v2), std::move(v3));
2184  BOOST_CHECK_EQUAL(v8.size(), 3U);
2185  BOOST_CHECK(v8[0].origin == &t2);
2186  BOOST_CHECK(v8[1].origin == &t1);
2187  BOOST_CHECK(v8[2].origin == &t2);
2188  BOOST_CHECK_EQUAL(v8[0].copies, 0);
2189  BOOST_CHECK_EQUAL(v8[1].copies, 1);
2190  BOOST_CHECK_EQUAL(v8[2].copies, 0);
2191 }
2192 
2194 {
2195  const std::array<unsigned char, 32> privkey_bytes = {
2196  // just some random data
2197  // derived address from this private key: 15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs
2198  0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
2199  0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
2200  0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
2201  0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
2202  };
2203 
2204  const std::string message = "Trust no one";
2205 
2206  const std::string expected_signature =
2207  "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
2208 
2209  CKey privkey;
2210  std::string generated_signature;
2211 
2212  BOOST_REQUIRE_MESSAGE(!privkey.IsValid(),
2213  "Confirm the private key is invalid");
2214 
2215  BOOST_CHECK_MESSAGE(!MessageSign(privkey, message, generated_signature),
2216  "Sign with an invalid private key");
2217 
2218  privkey.Set(privkey_bytes.begin(), privkey_bytes.end(), true);
2219 
2220  BOOST_REQUIRE_MESSAGE(privkey.IsValid(),
2221  "Confirm the private key is valid");
2222 
2223  BOOST_CHECK_MESSAGE(MessageSign(privkey, message, generated_signature),
2224  "Sign with a valid private key");
2225 
2226  BOOST_CHECK_EQUAL(expected_signature, generated_signature);
2227 }
2228 
2229 BOOST_AUTO_TEST_CASE(message_verify)
2230 {
2232  MessageVerify(
2233  "invalid address",
2234  "signature should be irrelevant",
2235  "message too"),
2237 
2239  MessageVerify(
2240  "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
2241  "signature should be irrelevant",
2242  "message too"),
2244 
2246  MessageVerify(
2247  "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
2248  "invalid signature, not in base64 encoding",
2249  "message should be irrelevant"),
2251 
2253  MessageVerify(
2254  "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
2255  "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
2256  "message should be irrelevant"),
2258 
2260  MessageVerify(
2261  "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
2262  "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
2263  "I never signed this"),
2265 
2267  MessageVerify(
2268  "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
2269  "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
2270  "Trust no one"),
2272 
2274  MessageVerify(
2275  "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
2276  "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
2277  "Trust me"),
2279 }
2280 
2282 {
2283  const std::string unsigned_tx = "...";
2284  const std::string prefixed_message =
2285  std::string(1, (char)MESSAGE_MAGIC.length()) +
2286  MESSAGE_MAGIC +
2287  std::string(1, (char)unsigned_tx.length()) +
2288  unsigned_tx;
2289 
2290  const uint256 signature_hash = Hash(unsigned_tx);
2291  const uint256 message_hash1 = Hash(prefixed_message);
2292  const uint256 message_hash2 = MessageHash(unsigned_tx);
2293 
2294  BOOST_CHECK_EQUAL(message_hash1, message_hash2);
2295  BOOST_CHECK_NE(message_hash1, signature_hash);
2296 }
2297 
2298 BOOST_AUTO_TEST_CASE(remove_prefix)
2299 {
2300  BOOST_CHECK_EQUAL(RemovePrefix("./util/system.h", "./"), "util/system.h");
2301  BOOST_CHECK_EQUAL(RemovePrefix("foo", "foo"), "");
2302  BOOST_CHECK_EQUAL(RemovePrefix("foo", "fo"), "o");
2303  BOOST_CHECK_EQUAL(RemovePrefix("foo", "f"), "oo");
2304  BOOST_CHECK_EQUAL(RemovePrefix("foo", ""), "foo");
2305  BOOST_CHECK_EQUAL(RemovePrefix("fo", "foo"), "fo");
2306  BOOST_CHECK_EQUAL(RemovePrefix("f", "foo"), "f");
2307  BOOST_CHECK_EQUAL(RemovePrefix("", "foo"), "");
2308  BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
2309 }
2310 
ForEachNoDup
void ForEachNoDup(CharType(&string)[StringLength], CharType min_char, CharType max_char, Fn &&fn)
Iterate over string values and call function for each string without successive duplicate characters.
Definition: str.h:32
ArgsMergeTestingSetup::Action
Action
Definition: util_tests.cpp:905
ChainMergeTestingSetup::ActionList
Action[MAX_ACTIONS] ActionList
Definition: util_tests.cpp:1082
MessageSign
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: message.cpp:56
FormatISO8601Date
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:145
ParseInt64
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:312
Parse
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition: descriptor.cpp:1393
BCLog::LogEscapeMessage
std::string LogEscapeMessage(const std::string &str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
Definition: logging.cpp:233
str.h
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:600
ArgsManager::ClearPathCache
void ClearPathCache()
Clear cached directory paths.
Definition: system.cpp:454
MessageVerificationResult::ERR_MALFORMED_SIGNATURE
@ ERR_MALFORMED_SIGNATURE
The provided signature couldn't be parsed (maybe invalid base64).
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
CheckValueTest::Expect
Definition: util_tests.cpp:247
count
static int count
Definition: tests.c:41
ReleaseDirectoryLocks
void ReleaseDirectoryLocks()
Release all directory locks.
Definition: system.cpp:125
assert
assert(!tx.IsCoinBase())
TestArgsManager::TestArgsManager
TestArgsManager()
Definition: util_tests.cpp:212
CheckValueTest::Expect::String
Expect & String(const char *s)
Definition: util_tests.cpp:262
CheckValueTest::Expect::int_value
std::optional< int64_t > int_value
Definition: util_tests.cpp:253
TestArgsManager
Definition: util_tests.cpp:210
ParseFixedPoint
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:481
util::Settings
Stored settings.
Definition: settings.h:31
SeedInsecureRand
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: setup_common.h:56
DirIsWritable
bool DirIsWritable(const fs::path &directory)
Definition: system.cpp:131
ParseDouble
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
Definition: strencodings.cpp:385
CHash256::Write
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
TestArgsManager::GetSetting
util::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: system.cpp:1022
IsHexNumber
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
Definition: strencodings.cpp:71
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:24
flags
int flags
Definition: bitcoin-tx.cpp:513
CheckValueTest::Expect::List
Expect & List(std::vector< std::string > m)
Definition: util_tests.cpp:265
ArgsManager::GetSetting
util::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: system.cpp:1022
setup_common.h
ArgsMergeTestingSetup::ForEachMergeSetup
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
Definition: util_tests.cpp:910
moneystr.h
sync.h
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
Definition: span.h:249
Cat
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:31
string.h
ParseISO8601DateTime
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:158
uint256.h
IsHex
bool IsHex(const std::string &str)
Definition: strencodings.cpp:61
ArgsManager::ALLOW_ANY
@ ALLOW_ANY
Definition: system.h:166
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
ArgsManager::GetChainName
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:988
CheckValueTest
Test GetSetting and GetArg type coercion, negation, and default value handling.
Definition: util_tests.cpp:244
FormatSubVersion
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...
Definition: clientversion.cpp:59
ArgsManager::IsArgSet
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:492
CheckValueTest::Expect::bool_value
std::optional< bool > bool_value
Definition: util_tests.cpp:254
spanparsing::Split
std::vector< Span< const char > > Split(const Span< const char > &sp, char sep)
Split a string on every instance of sep, returning a vector.
Definition: spanparsing.cpp:51
ArgsMergeTestingSetup
Definition: util_tests.cpp:900
CBaseChainParams::TESTNET
static const std::string TESTNET
Definition: chainparamsbase.h:23
MessageVerify
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: message.cpp:24
GetTime
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
MessageVerificationResult::ERR_NOT_SIGNED
@ ERR_NOT_SIGNED
The message was not signed with the private key of the provided address.
AnnotatedMixin< std::recursive_mutex >
SAFE_CHARS_UA_COMMENT
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:24
spanparsing::Func
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Definition: spanparsing.cpp:23
clientversion.h
InsecureRand32
static uint32_t InsecureRand32()
Definition: setup_common.h:65
expect
#define expect(bit)
Definition: univalue_read.cpp:255
ChainMergeTestingSetup::Action
Action
Definition: util_tests.cpp:1081
ParseHex_expected
static const unsigned char ParseHex_expected[65]
Definition: util_tests.cpp:110
TRY_LOCK
#define TRY_LOCK(cs, name)
Definition: sync.h:230
TrimString
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:18
TestArgsManager::GetSettingsList
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: system.cpp:1029
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:29
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(util_datadir)
Definition: util_tests.cpp:50
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
ParseUInt16
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
Definition: strencodings.cpp:339
CoinStatsHashType::NONE
@ NONE
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
FormatISO8601DateTime
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:132
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
TestOtherThread
static void TestOtherThread(fs::path dirname, std::string lockname, bool *result)
Definition: util_tests.cpp:1781
LockDirectory
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only)
Definition: system.cpp:95
UniValue
Definition: univalue.h:19
cs
static void pool cs
Definition: mempool_eviction.cpp:12
TestArgsManager::ReadConfigStream
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: system.cpp:867
prefix
const char * prefix
Definition: rest.cpp:712
NoIncludeConfTest::Parse
std::string Parse(const char *arg)
Definition: util_tests.cpp:349
ArgsManager::GetDataDirBase
const fs::path & GetDataDirBase() const
Get data directory path.
Definition: system.h:274
CheckValueTest::Expect::DefaultBool
Expect & DefaultBool()
Definition: util_tests.cpp:261
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
Assume
#define Assume(val)
Assume is the identity function.
Definition: check.h:72
ChainMergeTestingSetup::ForEachMergeSetup
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
Definition: util_tests.cpp:1086
strencodings.h
ChainMergeTestingSetup
Definition: util_tests.cpp:1078
ArgsManager::cs_args
RecursiveMutex cs_args
Definition: system.h:187
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
ASSERT_DEBUG_LOG
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:39
E
#define E
Definition: util_tests.cpp:1375
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
Definition: util_tests.cpp:329
values
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Definition: scriptnum_tests.cpp:17
ArgsManager::SoftSetArg
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: system.cpp:606
ExitCommand
static constexpr char ExitCommand
Definition: util_tests.cpp:1789
message.h
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
ArgsMergeTestingSetup::GetValues
std::vector< std::string > GetValues(const ActionList &actions, const std::string &section, const std::string &name, const std::string &value_prefix)
Translate actions into a list of <key>=setting strings.
Definition: util_tests.cpp:933
ArgsManager::AddArg
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:640
CheckValueTest::Expect::setting
util::SettingsValue setting
Definition: util_tests.cpp:248
ArgsManager::WriteSettingsFile
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr) const
Write settings file.
Definition: system.cpp:563
univalue.h
TestParse
static void TestParse(const std::string &str, bool expected_bool, int64_t expected_int)
Definition: util_tests.cpp:425
time.h
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:92
ArgsManager::GetUnsuitableSectionOnlyArgs
const std::set< std::string > GetUnsuitableSectionOnlyArgs() const
Log warnings for options in m_section_only_args when they are specified in the default section but no...
Definition: system.cpp:262
ArgsManager::ForceSetArg
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:622
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:588
MessageVerificationResult::ERR_INVALID_ADDRESS
@ ERR_INVALID_ADDRESS
The provided address is invalid.
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
OptionsCategory::OPTIONS
@ OPTIONS
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:170
B
#define B
Definition: util_tests.cpp:1374
CHash256::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
CheckValueTest::CheckValue
void CheckValue(unsigned int flags, const char *arg, const Expect &expect)
Definition: util_tests.cpp:269
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:587
ToUpper
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
Definition: strencodings.cpp:580
LockCommand
static constexpr char LockCommand
Definition: util_tests.cpp:1787
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:116
ArgsManager::ParseParameters
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:304
spanparsing.h
uint256
256-bit opaque blob.
Definition: uint256.h:124
STRING_WITH_EMBEDDED_NULL_CHAR
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR
Definition: util_tests.cpp:41
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED
@ ERR_PUBKEY_NOT_RECOVERED
A public key could not be recovered from the provided signature and message.
RemovePrefix
std::string RemovePrefix(const std::string &str, const std::string &prefix)
Definition: string.h:28
spanparsing::Const
bool Const(const std::string &str, Span< const char > &sp)
Parse a constant.
Definition: spanparsing.cpp:14
CheckValueTest::Expect::Bool
Expect & Bool(bool b)
Definition: util_tests.cpp:264
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:164
logging.h
TestArgsManager::SetNetworkOnlyArg
void SetNetworkOnlyArg(const std::string arg)
Definition: util_tests.cpp:224
MessageVerificationResult::ERR_ADDRESS_NO_KEY
@ ERR_ADDRESS_NO_KEY
The provided address is valid but does not refer to a public key.
ArgsManager::ReadConfigStream
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: system.cpp:867
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
name
const char * name
Definition: rest.cpp:43
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:77
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
ArgsManager::ReadSettingsFile
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
Definition: system.cpp:538
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
CheckValueTest::Expect::DefaultInt
Expect & DefaultInt()
Definition: util_tests.cpp:260
ArgsManager::SelectConfigNetwork
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: system.cpp:298
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
ArgsManager::LockSettings
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: system.h:431
key.h
ParseUInt64
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:368
ArgsMergeTestingSetup::ActionList
Action[MAX_ACTIONS] ActionList
Definition: util_tests.cpp:906
ArgsManager::GetSettingsList
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: system.cpp:1029
Join
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:44
CheckValueTest::Expect::Error
Expect & Error(const char *e)
Definition: util_tests.cpp:266
ArgsMergeTestingSetup::SET
@ SET
Definition: util_tests.cpp:905
ArgsManager::IsArgNegated
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: system.cpp:583
ParseInt32
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:296
SanitizeString
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
Definition: strencodings.cpp:27
CheckValueTest::Expect::Expect
Expect(util::SettingsValue s)
Definition: util_tests.cpp:258
CheckValueTest::Expect::Int
Expect & Int(int64_t i)
Definition: util_tests.cpp:263
CheckValueTest::Expect::list_value
std::optional< std::vector< std::string > > list_value
Definition: util_tests.cpp:255
BCLog
Definition: timer.h:18
CKey
An encapsulated private key.
Definition: key.h:26
ArgsManager
Definition: system.h:158
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
vector.h
LOCK
#define LOCK(cs)
Definition: sync.h:226
ParseMoney
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
Definition: moneystr.cpp:41
spanparsing::Expr
Span< const char > Expr(Span< const char > &sp)
Extract the expression that sp begins with.
Definition: spanparsing.cpp:32
MessageVerificationResult::OK
@ OK
The message verification was successful.
TestArgsManager::ReadConfigString
void ReadConfigString(const std::string str_config)
Definition: util_tests.cpp:213
COIN
static const CAmount COIN
Definition: amount.h:14
getuniquepath.h
BOOST_CHECK_THROW
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
NoIncludeConfTest
Definition: util_tests.cpp:348
util::Settings::rw_settings
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
SpanToStr
static std::string SpanToStr(const Span< const char > &span)
Definition: util_tests.cpp:1958
TestArgsManager::SetupArgs
void SetupArgs(const std::vector< std::pair< std::string, unsigned int >> &args)
Definition: util_tests.cpp:229
ArgsManager::GetArgs
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:483
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
spanparsing
Definition: spanparsing.cpp:12
MESSAGE_MAGIC
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
Definition: message.cpp:22
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
UnlockCommand
static constexpr char UnlockCommand
Definition: util_tests.cpp:1788
TestArgsManager::cs_args
RecursiveMutex cs_args
Definition: system.h:187
ParseUInt8
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
Definition: strencodings.cpp:327
GetUniquePath
fs::path GetUniquePath(const fs::path &base)
Helper function for getting a unique path.
Definition: getuniquepath.cpp:9
Span::end
constexpr C * end() const noexcept
Definition: span.h:171
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:351
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
FormatParagraph
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.
Definition: strencodings.cpp:399
TestOtherProcess
static void TestOtherProcess(fs::path dirname, std::string lockname, int fd)
Definition: util_tests.cpp:1791
CheckValueTest::Expect::DefaultString
Expect & DefaultString()
Definition: util_tests.cpp:259
CBaseChainParams::SIGNET
static const std::string SIGNET
Definition: chainparamsbase.h:24
GetTimeSeconds
int64_t GetTimeSeconds()
Returns the system time (not mockable)
Definition: time.cpp:127
ToLower
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
Definition: strencodings.cpp:573
FormatMoney
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
Definition: moneystr.cpp:15
MessageHash
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition: message.cpp:72
ChainMergeTestingSetup::NONE
@ NONE
Definition: util_tests.cpp:1081