1 // Copyright (c) 2011-2018 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 <primitives/transaction.h>
9 #include <sync.h>
10 #include <util/strencodings.h>
11 #include <util/moneystr.h>
12 #include <test/test_bitcoin.h>
13 
14 #include <stdint.h>
15 #include <vector>
16 #ifndef WIN32
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #endif
21 
22 #include <boost/test/unit_test.hpp>
23 
24 /* defined in logging.cpp */
25 namespace BCLog {
26     std::string LogEscapeMessage(const std::string& str);
27 }
28 
BOOST_FIXTURE_TEST_SUITE(util_tests,BasicTestingSetup)29 BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
30 
31 BOOST_AUTO_TEST_CASE(util_criticalsection)
32 {
33     CCriticalSection cs;
34 
35     do {
36         LOCK(cs);
37         break;
38 
39         BOOST_ERROR("break was swallowed!");
40     } while(0);
41 
42     do {
43         TRY_LOCK(cs, lockTest);
44         if (lockTest)
45             break;
46 
47         BOOST_ERROR("break was swallowed!");
48     } while(0);
49 }
50 
51 static const unsigned char ParseHex_expected[65] = {
52     0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
53     0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
54     0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
55     0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
56     0x5f
57 };
BOOST_AUTO_TEST_CASE(util_ParseHex)58 BOOST_AUTO_TEST_CASE(util_ParseHex)
59 {
60     std::vector<unsigned char> result;
61     std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
62     // Basic test vector
63     result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
64     BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
65 
66     // Spaces between bytes must be supported
67     result = ParseHex("12 34 56 78");
68     BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
69 
70     // Leading space must be supported (used in BerkeleyEnvironment::Salvage)
71     result = ParseHex(" 89 34 56 78");
72     BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
73 
74     // Stop parsing at invalid value
75     result = ParseHex("1234 invalid 1234");
76     BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
77 }
78 
BOOST_AUTO_TEST_CASE(util_HexStr)79 BOOST_AUTO_TEST_CASE(util_HexStr)
80 {
81     BOOST_CHECK_EQUAL(
82         HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
83         "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
84 
85     BOOST_CHECK_EQUAL(
86         HexStr(ParseHex_expected, ParseHex_expected + 5, true),
87         "04 67 8a fd b0");
88 
89     BOOST_CHECK_EQUAL(
90         HexStr(ParseHex_expected + sizeof(ParseHex_expected),
91                ParseHex_expected + sizeof(ParseHex_expected)),
92         "");
93 
94     BOOST_CHECK_EQUAL(
95         HexStr(ParseHex_expected + sizeof(ParseHex_expected),
96                ParseHex_expected + sizeof(ParseHex_expected), true),
97         "");
98 
99     BOOST_CHECK_EQUAL(
100         HexStr(ParseHex_expected, ParseHex_expected),
101         "");
102 
103     BOOST_CHECK_EQUAL(
104         HexStr(ParseHex_expected, ParseHex_expected, true),
105         "");
106 
107     std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
108 
109     BOOST_CHECK_EQUAL(
110         HexStr(ParseHex_vec, true),
111         "04 67 8a fd b0");
112 
113     BOOST_CHECK_EQUAL(
114         HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend()),
115         "b0fd8a6704"
116     );
117 
118     BOOST_CHECK_EQUAL(
119         HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend(), true),
120         "b0 fd 8a 67 04"
121     );
122 
123     BOOST_CHECK_EQUAL(
124         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
125                std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
126         ""
127     );
128 
129     BOOST_CHECK_EQUAL(
130         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
131                std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
132         ""
133     );
134 
135     BOOST_CHECK_EQUAL(
136         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
137                std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
138         "04"
139     );
140 
141     BOOST_CHECK_EQUAL(
142         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
143                std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
144         "04"
145     );
146 
147     BOOST_CHECK_EQUAL(
148         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
149                std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
150         "b0fd8a6704"
151     );
152 
153     BOOST_CHECK_EQUAL(
154         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
155                std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
156         "b0 fd 8a 67 04"
157     );
158 
159     BOOST_CHECK_EQUAL(
160         HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 65),
161                std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
162         "5f1df16b2b704c8a578d0bbaf74d385cde12c11ee50455f3c438ef4c3fbcf649b6de611feae06279a60939e028a8d65c10b73071a6f16719274855feb0fd8a6704"
163     );
164 }
165 
166 
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)167 BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
168 {
169     BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
170 }
171 
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)172 BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
173 {
174     BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
175 }
176 
BOOST_AUTO_TEST_CASE(util_FormatISO8601Time)177 BOOST_AUTO_TEST_CASE(util_FormatISO8601Time)
178 {
179     BOOST_CHECK_EQUAL(FormatISO8601Time(1317425777), "23:36:17Z");
180 }
181 
182 struct TestArgsManager : public ArgsManager
183 {
TestArgsManagerTestArgsManager184     TestArgsManager() { m_network_only_args.clear(); }
GetOverrideArgsTestArgsManager185     std::map<std::string, std::vector<std::string> >& GetOverrideArgs() { return m_override_args; }
GetConfigArgsTestArgsManager186     std::map<std::string, std::vector<std::string> >& GetConfigArgs() { return m_config_args; }
ReadConfigStringTestArgsManager187     void ReadConfigString(const std::string str_config)
188     {
189         std::istringstream streamConfig(str_config);
190         {
191             LOCK(cs_args);
192             m_config_args.clear();
193         }
194         std::string error;
195         BOOST_REQUIRE(ReadConfigStream(streamConfig, error));
196     }
SetNetworkOnlyArgTestArgsManager197     void SetNetworkOnlyArg(const std::string arg)
198     {
199         LOCK(cs_args);
200         m_network_only_args.insert(arg);
201     }
SetupArgsTestArgsManager202     void SetupArgs(int argv, const char* args[])
203     {
204         for (int i = 0; i < argv; ++i) {
205             AddArg(args[i], "", false, OptionsCategory::OPTIONS);
206         }
207     }
208 };
209 
BOOST_AUTO_TEST_CASE(util_ParseParameters)210 BOOST_AUTO_TEST_CASE(util_ParseParameters)
211 {
212     TestArgsManager testArgs;
213     const char* avail_args[] = {"-a", "-b", "-ccc", "-d"};
214     const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
215 
216     std::string error;
217     testArgs.SetupArgs(4, avail_args);
218     BOOST_CHECK(testArgs.ParseParameters(0, (char**)argv_test, error));
219     BOOST_CHECK(testArgs.GetOverrideArgs().empty() && testArgs.GetConfigArgs().empty());
220 
221     BOOST_CHECK(testArgs.ParseParameters(1, (char**)argv_test, error));
222     BOOST_CHECK(testArgs.GetOverrideArgs().empty() && testArgs.GetConfigArgs().empty());
223 
224     BOOST_CHECK(testArgs.ParseParameters(7, (char**)argv_test, error));
225     // expectation: -ignored is ignored (program name argument),
226     // -a, -b and -ccc end up in map, -d ignored because it is after
227     // a non-option argument (non-GNU option parsing)
228     BOOST_CHECK(testArgs.GetOverrideArgs().size() == 3 && testArgs.GetConfigArgs().empty());
229     BOOST_CHECK(testArgs.IsArgSet("-a") && testArgs.IsArgSet("-b") && testArgs.IsArgSet("-ccc")
230                 && !testArgs.IsArgSet("f") && !testArgs.IsArgSet("-d"));
231     BOOST_CHECK(testArgs.GetOverrideArgs().count("-a") && testArgs.GetOverrideArgs().count("-b") && testArgs.GetOverrideArgs().count("-ccc")
232                 && !testArgs.GetOverrideArgs().count("f") && !testArgs.GetOverrideArgs().count("-d"));
233 
234     BOOST_CHECK(testArgs.GetOverrideArgs()["-a"].size() == 1);
235     BOOST_CHECK(testArgs.GetOverrideArgs()["-a"].front() == "");
236     BOOST_CHECK(testArgs.GetOverrideArgs()["-ccc"].size() == 2);
237     BOOST_CHECK(testArgs.GetOverrideArgs()["-ccc"].front() == "argument");
238     BOOST_CHECK(testArgs.GetOverrideArgs()["-ccc"].back() == "multiple");
239     BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
240 }
241 
BOOST_AUTO_TEST_CASE(util_GetBoolArg)242 BOOST_AUTO_TEST_CASE(util_GetBoolArg)
243 {
244     TestArgsManager testArgs;
245     const char* avail_args[] = {"-a", "-b", "-c", "-d", "-e", "-f"};
246     const char *argv_test[] = {
247         "ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
248     std::string error;
249     testArgs.SetupArgs(6, avail_args);
250     BOOST_CHECK(testArgs.ParseParameters(7, (char**)argv_test, error));
251 
252     // Each letter should be set.
253     for (const char opt : "abcdef")
254         BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt);
255 
256     // Nothing else should be in the map
257     BOOST_CHECK(testArgs.GetOverrideArgs().size() == 6 &&
258                 testArgs.GetConfigArgs().empty());
259 
260     // The -no prefix should get stripped on the way in.
261     BOOST_CHECK(!testArgs.IsArgSet("-nob"));
262 
263     // The -b option is flagged as negated, and nothing else is
264     BOOST_CHECK(testArgs.IsArgNegated("-b"));
265     BOOST_CHECK(!testArgs.IsArgNegated("-a"));
266 
267     // Check expected values.
268     BOOST_CHECK(testArgs.GetBoolArg("-a", false) == true);
269     BOOST_CHECK(testArgs.GetBoolArg("-b", true) == false);
270     BOOST_CHECK(testArgs.GetBoolArg("-c", true) == false);
271     BOOST_CHECK(testArgs.GetBoolArg("-d", false) == true);
272     BOOST_CHECK(testArgs.GetBoolArg("-e", true) == false);
273     BOOST_CHECK(testArgs.GetBoolArg("-f", true) == false);
274 }
275 
BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)276 BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
277 {
278     // Test some awful edge cases that hopefully no user will ever exercise.
279     TestArgsManager testArgs;
280 
281     // Params test
282     const char* avail_args[] = {"-foo", "-bar"};
283     const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
284     testArgs.SetupArgs(2, avail_args);
285     std::string error;
286     BOOST_CHECK(testArgs.ParseParameters(4, (char**)argv_test, error));
287 
288     // This was passed twice, second one overrides the negative setting.
289     BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
290     BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "");
291 
292     // A double negative is a positive, and not marked as negated.
293     BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
294     BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
295 
296     // Config test
297     const char *conf_test = "nofoo=1\nfoo=1\nnobar=0\n";
298     BOOST_CHECK(testArgs.ParseParameters(1, (char**)argv_test, error));
299     testArgs.ReadConfigString(conf_test);
300 
301     // This was passed twice, second one overrides the negative setting,
302     // and the value.
303     BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
304     BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "1");
305 
306     // A double negative is a positive, and does not count as negated.
307     BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
308     BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
309 
310     // Combined test
311     const char *combo_test_args[] = {"ignored", "-nofoo", "-bar"};
312     const char *combo_test_conf = "foo=1\nnobar=1\n";
313     BOOST_CHECK(testArgs.ParseParameters(3, (char**)combo_test_args, error));
314     testArgs.ReadConfigString(combo_test_conf);
315 
316     // Command line overrides, but doesn't erase old setting
317     BOOST_CHECK(testArgs.IsArgNegated("-foo"));
318     BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "0");
319     BOOST_CHECK(testArgs.GetArgs("-foo").size() == 0);
320 
321     // Command line overrides, but doesn't erase old setting
322     BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
323     BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "");
324     BOOST_CHECK(testArgs.GetArgs("-bar").size() == 1
325                 && testArgs.GetArgs("-bar").front() == "");
326 }
327 
BOOST_AUTO_TEST_CASE(util_ReadConfigStream)328 BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
329 {
330     const char *str_config =
331        "a=\n"
332        "b=1\n"
333        "ccc=argument\n"
334        "ccc=multiple\n"
335        "d=e\n"
336        "nofff=1\n"
337        "noggg=0\n"
338        "h=1\n"
339        "noh=1\n"
340        "noi=1\n"
341        "i=1\n"
342        "sec1.ccc=extend1\n"
343        "\n"
344        "[sec1]\n"
345        "ccc=extend2\n"
346        "d=eee\n"
347        "h=1\n"
348        "[sec2]\n"
349        "ccc=extend3\n"
350        "iii=2\n";
351 
352     TestArgsManager test_args;
353     const char* avail_args[] = {"-a", "-b", "-ccc", "-d", "-e", "-fff", "-ggg", "-h", "-i", "-iii"};
354     test_args.SetupArgs(10, avail_args);
355 
356     test_args.ReadConfigString(str_config);
357     // expectation: a, b, ccc, d, fff, ggg, h, i end up in map
358     // so do sec1.ccc, sec1.d, sec1.h, sec2.ccc, sec2.iii
359 
360     BOOST_CHECK(test_args.GetOverrideArgs().empty());
361     BOOST_CHECK(test_args.GetConfigArgs().size() == 13);
362 
363     BOOST_CHECK(test_args.GetConfigArgs().count("-a")
364                 && test_args.GetConfigArgs().count("-b")
365                 && test_args.GetConfigArgs().count("-ccc")
366                 && test_args.GetConfigArgs().count("-d")
367                 && test_args.GetConfigArgs().count("-fff")
368                 && test_args.GetConfigArgs().count("-ggg")
369                 && test_args.GetConfigArgs().count("-h")
370                 && test_args.GetConfigArgs().count("-i")
371                );
372     BOOST_CHECK(test_args.GetConfigArgs().count("-sec1.ccc")
373                 && test_args.GetConfigArgs().count("-sec1.h")
374                 && test_args.GetConfigArgs().count("-sec2.ccc")
375                 && test_args.GetConfigArgs().count("-sec2.iii")
376                );
377 
378     BOOST_CHECK(test_args.IsArgSet("-a")
379                 && test_args.IsArgSet("-b")
380                 && test_args.IsArgSet("-ccc")
381                 && test_args.IsArgSet("-d")
382                 && test_args.IsArgSet("-fff")
383                 && test_args.IsArgSet("-ggg")
384                 && test_args.IsArgSet("-h")
385                 && test_args.IsArgSet("-i")
386                 && !test_args.IsArgSet("-zzz")
387                 && !test_args.IsArgSet("-iii")
388                );
389 
390     BOOST_CHECK(test_args.GetArg("-a", "xxx") == ""
391                 && test_args.GetArg("-b", "xxx") == "1"
392                 && test_args.GetArg("-ccc", "xxx") == "argument"
393                 && test_args.GetArg("-d", "xxx") == "e"
394                 && test_args.GetArg("-fff", "xxx") == "0"
395                 && test_args.GetArg("-ggg", "xxx") == "1"
396                 && test_args.GetArg("-h", "xxx") == "0"
397                 && test_args.GetArg("-i", "xxx") == "1"
398                 && test_args.GetArg("-zzz", "xxx") == "xxx"
399                 && test_args.GetArg("-iii", "xxx") == "xxx"
400                );
401 
402     for (const bool def : {false, true}) {
403         BOOST_CHECK(test_args.GetBoolArg("-a", def)
404                      && test_args.GetBoolArg("-b", def)
405                      && !test_args.GetBoolArg("-ccc", def)
406                      && !test_args.GetBoolArg("-d", def)
407                      && !test_args.GetBoolArg("-fff", def)
408                      && test_args.GetBoolArg("-ggg", def)
409                      && !test_args.GetBoolArg("-h", def)
410                      && test_args.GetBoolArg("-i", def)
411                      && test_args.GetBoolArg("-zzz", def) == def
412                      && test_args.GetBoolArg("-iii", def) == def
413                    );
414     }
415 
416     BOOST_CHECK(test_args.GetArgs("-a").size() == 1
417                 && test_args.GetArgs("-a").front() == "");
418     BOOST_CHECK(test_args.GetArgs("-b").size() == 1
419                 && test_args.GetArgs("-b").front() == "1");
420     BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2
421                 && test_args.GetArgs("-ccc").front() == "argument"
422                 && test_args.GetArgs("-ccc").back() == "multiple");
423     BOOST_CHECK(test_args.GetArgs("-fff").size() == 0);
424     BOOST_CHECK(test_args.GetArgs("-nofff").size() == 0);
425     BOOST_CHECK(test_args.GetArgs("-ggg").size() == 1
426                 && test_args.GetArgs("-ggg").front() == "1");
427     BOOST_CHECK(test_args.GetArgs("-noggg").size() == 0);
428     BOOST_CHECK(test_args.GetArgs("-h").size() == 0);
429     BOOST_CHECK(test_args.GetArgs("-noh").size() == 0);
430     BOOST_CHECK(test_args.GetArgs("-i").size() == 1
431                 && test_args.GetArgs("-i").front() == "1");
432     BOOST_CHECK(test_args.GetArgs("-noi").size() == 0);
433     BOOST_CHECK(test_args.GetArgs("-zzz").size() == 0);
434 
435     BOOST_CHECK(!test_args.IsArgNegated("-a"));
436     BOOST_CHECK(!test_args.IsArgNegated("-b"));
437     BOOST_CHECK(!test_args.IsArgNegated("-ccc"));
438     BOOST_CHECK(!test_args.IsArgNegated("-d"));
439     BOOST_CHECK(test_args.IsArgNegated("-fff"));
440     BOOST_CHECK(!test_args.IsArgNegated("-ggg"));
441     BOOST_CHECK(test_args.IsArgNegated("-h")); // last setting takes precedence
442     BOOST_CHECK(!test_args.IsArgNegated("-i")); // last setting takes precedence
443     BOOST_CHECK(!test_args.IsArgNegated("-zzz"));
444 
445     // Test sections work
446     test_args.SelectConfigNetwork("sec1");
447 
448     // same as original
449     BOOST_CHECK(test_args.GetArg("-a", "xxx") == ""
450                 && test_args.GetArg("-b", "xxx") == "1"
451                 && test_args.GetArg("-fff", "xxx") == "0"
452                 && test_args.GetArg("-ggg", "xxx") == "1"
453                 && test_args.GetArg("-zzz", "xxx") == "xxx"
454                 && test_args.GetArg("-iii", "xxx") == "xxx"
455                );
456     // d is overridden
457     BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
458     // section-specific setting
459     BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
460     // section takes priority for multiple values
461     BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend1");
462     // check multiple values works
463     const std::vector<std::string> sec1_ccc_expected = {"extend1","extend2","argument","multiple"};
464     const auto& sec1_ccc_res = test_args.GetArgs("-ccc");
465     BOOST_CHECK_EQUAL_COLLECTIONS(sec1_ccc_res.begin(), sec1_ccc_res.end(), sec1_ccc_expected.begin(), sec1_ccc_expected.end());
466 
467     test_args.SelectConfigNetwork("sec2");
468 
469     // same as original
470     BOOST_CHECK(test_args.GetArg("-a", "xxx") == ""
471                 && test_args.GetArg("-b", "xxx") == "1"
472                 && test_args.GetArg("-d", "xxx") == "e"
473                 && test_args.GetArg("-fff", "xxx") == "0"
474                 && test_args.GetArg("-ggg", "xxx") == "1"
475                 && test_args.GetArg("-zzz", "xxx") == "xxx"
476                 && test_args.GetArg("-h", "xxx") == "0"
477                );
478     // section-specific setting
479     BOOST_CHECK(test_args.GetArg("-iii", "xxx") == "2");
480     // section takes priority for multiple values
481     BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend3");
482     // check multiple values works
483     const std::vector<std::string> sec2_ccc_expected = {"extend3","argument","multiple"};
484     const auto& sec2_ccc_res = test_args.GetArgs("-ccc");
485     BOOST_CHECK_EQUAL_COLLECTIONS(sec2_ccc_res.begin(), sec2_ccc_res.end(), sec2_ccc_expected.begin(), sec2_ccc_expected.end());
486 
487     // Test section only options
488 
489     test_args.SetNetworkOnlyArg("-d");
490     test_args.SetNetworkOnlyArg("-ccc");
491     test_args.SetNetworkOnlyArg("-h");
492 
493     test_args.SelectConfigNetwork(CBaseChainParams::MAIN);
494     BOOST_CHECK(test_args.GetArg("-d", "xxx") == "e");
495     BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
496     BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
497 
498     test_args.SelectConfigNetwork("sec1");
499     BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
500     BOOST_CHECK(test_args.GetArgs("-d").size() == 1);
501     BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
502     BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
503 
504     test_args.SelectConfigNetwork("sec2");
505     BOOST_CHECK(test_args.GetArg("-d", "xxx") == "xxx");
506     BOOST_CHECK(test_args.GetArgs("-d").size() == 0);
507     BOOST_CHECK(test_args.GetArgs("-ccc").size() == 1);
508     BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
509 }
510 
BOOST_AUTO_TEST_CASE(util_GetArg)511 BOOST_AUTO_TEST_CASE(util_GetArg)
512 {
513     TestArgsManager testArgs;
514     testArgs.GetOverrideArgs().clear();
515     testArgs.GetOverrideArgs()["strtest1"] = {"string..."};
516     // strtest2 undefined on purpose
517     testArgs.GetOverrideArgs()["inttest1"] = {"12345"};
518     testArgs.GetOverrideArgs()["inttest2"] = {"81985529216486895"};
519     // inttest3 undefined on purpose
520     testArgs.GetOverrideArgs()["booltest1"] = {""};
521     // booltest2 undefined on purpose
522     testArgs.GetOverrideArgs()["booltest3"] = {"0"};
523     testArgs.GetOverrideArgs()["booltest4"] = {"1"};
524 
525     // priorities
526     testArgs.GetOverrideArgs()["pritest1"] = {"a", "b"};
527     testArgs.GetConfigArgs()["pritest2"] = {"a", "b"};
528     testArgs.GetOverrideArgs()["pritest3"] = {"a"};
529     testArgs.GetConfigArgs()["pritest3"] = {"b"};
530     testArgs.GetOverrideArgs()["pritest4"] = {"a","b"};
531     testArgs.GetConfigArgs()["pritest4"] = {"c","d"};
532 
533     BOOST_CHECK_EQUAL(testArgs.GetArg("strtest1", "default"), "string...");
534     BOOST_CHECK_EQUAL(testArgs.GetArg("strtest2", "default"), "default");
535     BOOST_CHECK_EQUAL(testArgs.GetArg("inttest1", -1), 12345);
536     BOOST_CHECK_EQUAL(testArgs.GetArg("inttest2", -1), 81985529216486895LL);
537     BOOST_CHECK_EQUAL(testArgs.GetArg("inttest3", -1), -1);
538     BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest1", false), true);
539     BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest2", false), false);
540     BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest3", false), false);
541     BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest4", false), true);
542 
543     BOOST_CHECK_EQUAL(testArgs.GetArg("pritest1", "default"), "b");
544     BOOST_CHECK_EQUAL(testArgs.GetArg("pritest2", "default"), "a");
545     BOOST_CHECK_EQUAL(testArgs.GetArg("pritest3", "default"), "a");
546     BOOST_CHECK_EQUAL(testArgs.GetArg("pritest4", "default"), "b");
547 }
548 
BOOST_AUTO_TEST_CASE(util_GetChainName)549 BOOST_AUTO_TEST_CASE(util_GetChainName)
550 {
551     TestArgsManager test_args;
552     const char* avail_args[] = {"-testnet", "-regtest"};
553     test_args.SetupArgs(2, avail_args);
554 
555     const char* argv_testnet[] = {"cmd", "-testnet"};
556     const char* argv_regtest[] = {"cmd", "-regtest"};
557     const char* argv_test_no_reg[] = {"cmd", "-testnet", "-noregtest"};
558     const char* argv_both[] = {"cmd", "-testnet", "-regtest"};
559 
560     // equivalent to "-testnet"
561     // regtest in testnet section is ignored
562     const char* testnetconf = "testnet=1\nregtest=0\n[test]\nregtest=1";
563     std::string error;
564 
565     BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
566     BOOST_CHECK_EQUAL(test_args.GetChainName(), "main");
567 
568     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
569     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
570 
571     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
572     BOOST_CHECK_EQUAL(test_args.GetChainName(), "regtest");
573 
574     BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_test_no_reg, error));
575     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
576 
577     BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
578     BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
579 
580     BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
581     test_args.ReadConfigString(testnetconf);
582     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
583 
584     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
585     test_args.ReadConfigString(testnetconf);
586     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
587 
588     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
589     test_args.ReadConfigString(testnetconf);
590     BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
591 
592     BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_test_no_reg, error));
593     test_args.ReadConfigString(testnetconf);
594     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
595 
596     BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
597     test_args.ReadConfigString(testnetconf);
598     BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
599 
600     // check setting the network to test (and thus making
601     // [test] regtest=1 potentially relevant) doesn't break things
602     test_args.SelectConfigNetwork("test");
603 
604     BOOST_CHECK(test_args.ParseParameters(0, (char**)argv_testnet, error));
605     test_args.ReadConfigString(testnetconf);
606     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
607 
608     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_testnet, error));
609     test_args.ReadConfigString(testnetconf);
610     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
611 
612     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_regtest, error));
613     test_args.ReadConfigString(testnetconf);
614     BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
615 
616     BOOST_CHECK(test_args.ParseParameters(2, (char**)argv_test_no_reg, error));
617     test_args.ReadConfigString(testnetconf);
618     BOOST_CHECK_EQUAL(test_args.GetChainName(), "test");
619 
620     BOOST_CHECK(test_args.ParseParameters(3, (char**)argv_both, error));
621     test_args.ReadConfigString(testnetconf);
622     BOOST_CHECK_THROW(test_args.GetChainName(), std::runtime_error);
623 }
624 
BOOST_AUTO_TEST_CASE(util_FormatMoney)625 BOOST_AUTO_TEST_CASE(util_FormatMoney)
626 {
627     BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
628     BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789");
629     BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00");
630 
631     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00");
632     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00");
633     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00");
634     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00");
635     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00");
636     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00");
637     BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00");
638     BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00");
639     BOOST_CHECK_EQUAL(FormatMoney(COIN), "1.00");
640     BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10");
641     BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01");
642     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001");
643     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001");
644     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001");
645     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
646     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
647     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
648 }
649 
BOOST_AUTO_TEST_CASE(util_ParseMoney)650 BOOST_AUTO_TEST_CASE(util_ParseMoney)
651 {
652     CAmount ret = 0;
653     BOOST_CHECK(ParseMoney("0.0", ret));
654     BOOST_CHECK_EQUAL(ret, 0);
655 
656     BOOST_CHECK(ParseMoney("12345.6789", ret));
657     BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
658 
659     BOOST_CHECK(ParseMoney("100000000.00", ret));
660     BOOST_CHECK_EQUAL(ret, COIN*100000000);
661     BOOST_CHECK(ParseMoney("10000000.00", ret));
662     BOOST_CHECK_EQUAL(ret, COIN*10000000);
663     BOOST_CHECK(ParseMoney("1000000.00", ret));
664     BOOST_CHECK_EQUAL(ret, COIN*1000000);
665     BOOST_CHECK(ParseMoney("100000.00", ret));
666     BOOST_CHECK_EQUAL(ret, COIN*100000);
667     BOOST_CHECK(ParseMoney("10000.00", ret));
668     BOOST_CHECK_EQUAL(ret, COIN*10000);
669     BOOST_CHECK(ParseMoney("1000.00", ret));
670     BOOST_CHECK_EQUAL(ret, COIN*1000);
671     BOOST_CHECK(ParseMoney("100.00", ret));
672     BOOST_CHECK_EQUAL(ret, COIN*100);
673     BOOST_CHECK(ParseMoney("10.00", ret));
674     BOOST_CHECK_EQUAL(ret, COIN*10);
675     BOOST_CHECK(ParseMoney("1.00", ret));
676     BOOST_CHECK_EQUAL(ret, COIN);
677     BOOST_CHECK(ParseMoney("1", ret));
678     BOOST_CHECK_EQUAL(ret, COIN);
679     BOOST_CHECK(ParseMoney("0.1", ret));
680     BOOST_CHECK_EQUAL(ret, COIN/10);
681     BOOST_CHECK(ParseMoney("0.01", ret));
682     BOOST_CHECK_EQUAL(ret, COIN/100);
683     BOOST_CHECK(ParseMoney("0.001", ret));
684     BOOST_CHECK_EQUAL(ret, COIN/1000);
685     BOOST_CHECK(ParseMoney("0.0001", ret));
686     BOOST_CHECK_EQUAL(ret, COIN/10000);
687     BOOST_CHECK(ParseMoney("0.00001", ret));
688     BOOST_CHECK_EQUAL(ret, COIN/100000);
689     BOOST_CHECK(ParseMoney("0.000001", ret));
690     BOOST_CHECK_EQUAL(ret, COIN/1000000);
691     BOOST_CHECK(ParseMoney("0.0000001", ret));
692     BOOST_CHECK_EQUAL(ret, COIN/10000000);
693     BOOST_CHECK(ParseMoney("0.00000001", ret));
694     BOOST_CHECK_EQUAL(ret, COIN/100000000);
695 
696     // Attempted 63 bit overflow should fail
697     BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
698 
699     // Parsing negative amounts must fail
700     BOOST_CHECK(!ParseMoney("-1", ret));
701 }
702 
BOOST_AUTO_TEST_CASE(util_IsHex)703 BOOST_AUTO_TEST_CASE(util_IsHex)
704 {
705     BOOST_CHECK(IsHex("00"));
706     BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
707     BOOST_CHECK(IsHex("ff"));
708     BOOST_CHECK(IsHex("FF"));
709 
710     BOOST_CHECK(!IsHex(""));
711     BOOST_CHECK(!IsHex("0"));
712     BOOST_CHECK(!IsHex("a"));
713     BOOST_CHECK(!IsHex("eleven"));
714     BOOST_CHECK(!IsHex("00xx00"));
715     BOOST_CHECK(!IsHex("0x0000"));
716 }
717 
BOOST_AUTO_TEST_CASE(util_IsHexNumber)718 BOOST_AUTO_TEST_CASE(util_IsHexNumber)
719 {
720     BOOST_CHECK(IsHexNumber("0x0"));
721     BOOST_CHECK(IsHexNumber("0"));
722     BOOST_CHECK(IsHexNumber("0x10"));
723     BOOST_CHECK(IsHexNumber("10"));
724     BOOST_CHECK(IsHexNumber("0xff"));
725     BOOST_CHECK(IsHexNumber("ff"));
726     BOOST_CHECK(IsHexNumber("0xFfa"));
727     BOOST_CHECK(IsHexNumber("Ffa"));
728     BOOST_CHECK(IsHexNumber("0x00112233445566778899aabbccddeeffAABBCCDDEEFF"));
729     BOOST_CHECK(IsHexNumber("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
730 
731     BOOST_CHECK(!IsHexNumber(""));   // empty string not allowed
732     BOOST_CHECK(!IsHexNumber("0x")); // empty string after prefix not allowed
733     BOOST_CHECK(!IsHexNumber("0x0 ")); // no spaces at end,
734     BOOST_CHECK(!IsHexNumber(" 0x0")); // or beginning,
735     BOOST_CHECK(!IsHexNumber("0x 0")); // or middle,
736     BOOST_CHECK(!IsHexNumber(" "));    // etc.
737     BOOST_CHECK(!IsHexNumber("0x0ga")); // invalid character
738     BOOST_CHECK(!IsHexNumber("x0"));    // broken prefix
739     BOOST_CHECK(!IsHexNumber("0x0x00")); // two prefixes not allowed
740 
741 }
742 
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)743 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
744 {
745     SeedInsecureRand(true);
746     for (int mod=2;mod<11;mod++)
747     {
748         int mask = 1;
749         // Really rough binomial confidence approximation.
750         int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
751         //mask is 2^ceil(log2(mod))-1
752         while(mask<mod-1)mask=(mask<<1)+1;
753 
754         int count = 0;
755         //How often does it get a zero from the uniform range [0,mod)?
756         for (int i = 0; i < 10000; i++) {
757             uint32_t rval;
758             do{
759                 rval=InsecureRand32()&mask;
760             }while(rval>=(uint32_t)mod);
761             count += rval==0;
762         }
763         BOOST_CHECK(count<=10000/mod+err);
764         BOOST_CHECK(count>=10000/mod-err);
765     }
766 }
767 
BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)768 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
769 {
770     BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
771     BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
772     BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
773     BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
774     BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
775     BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
776     BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
777 }
778 
779 /* Test strprintf formatting directives.
780  * Put a string before and after to ensure sanity of element sizes on stack. */
781 #define B "check_prefix"
782 #define E "check_postfix"
BOOST_AUTO_TEST_CASE(strprintf_numbers)783 BOOST_AUTO_TEST_CASE(strprintf_numbers)
784 {
785     int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
786     uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
787     BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
788     BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
789     BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
790 
791     size_t st = 12345678; /* unsigned size_t test value */
792     ssize_t sst = -12345678; /* signed size_t test value */
793     BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
794     BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
795     BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
796 
797     ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
798     ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
799     BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
800     BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
801     BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
802 }
803 #undef B
804 #undef E
805 
806 /* Check for mingw/wine issue #3494
807  * Remove this test before time.ctime(0xffffffff) == 'Sun Feb  7 07:28:15 2106'
808  */
BOOST_AUTO_TEST_CASE(gettime)809 BOOST_AUTO_TEST_CASE(gettime)
810 {
811     BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
812 }
813 
BOOST_AUTO_TEST_CASE(test_IsDigit)814 BOOST_AUTO_TEST_CASE(test_IsDigit)
815 {
816     BOOST_CHECK_EQUAL(IsDigit('0'), true);
817     BOOST_CHECK_EQUAL(IsDigit('1'), true);
818     BOOST_CHECK_EQUAL(IsDigit('8'), true);
819     BOOST_CHECK_EQUAL(IsDigit('9'), true);
820 
821     BOOST_CHECK_EQUAL(IsDigit('0' - 1), false);
822     BOOST_CHECK_EQUAL(IsDigit('9' + 1), false);
823     BOOST_CHECK_EQUAL(IsDigit(0), false);
824     BOOST_CHECK_EQUAL(IsDigit(1), false);
825     BOOST_CHECK_EQUAL(IsDigit(8), false);
826     BOOST_CHECK_EQUAL(IsDigit(9), false);
827 }
828 
BOOST_AUTO_TEST_CASE(test_ParseInt32)829 BOOST_AUTO_TEST_CASE(test_ParseInt32)
830 {
831     int32_t n;
832     // Valid values
833     BOOST_CHECK(ParseInt32("1234", nullptr));
834     BOOST_CHECK(ParseInt32("0", &n) && n == 0);
835     BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
836     BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
837     BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
838     BOOST_CHECK(ParseInt32("-2147483648", &n) && n == (-2147483647 - 1)); // (-2147483647 - 1) equals INT_MIN
839     BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
840     // Invalid values
841     BOOST_CHECK(!ParseInt32("", &n));
842     BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
843     BOOST_CHECK(!ParseInt32("1 ", &n));
844     BOOST_CHECK(!ParseInt32("1a", &n));
845     BOOST_CHECK(!ParseInt32("aap", &n));
846     BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
847     BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
848     const char test_bytes[] = {'1', 0, '1'};
849     std::string teststr(test_bytes, sizeof(test_bytes));
850     BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
851     // Overflow and underflow
852     BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
853     BOOST_CHECK(!ParseInt32("2147483648", nullptr));
854     BOOST_CHECK(!ParseInt32("-32482348723847471234", nullptr));
855     BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
856 }
857 
BOOST_AUTO_TEST_CASE(test_ParseInt64)858 BOOST_AUTO_TEST_CASE(test_ParseInt64)
859 {
860     int64_t n;
861     // Valid values
862     BOOST_CHECK(ParseInt64("1234", nullptr));
863     BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
864     BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
865     BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
866     BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
867     BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
868     BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807);
869     BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1);
870     BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
871     // Invalid values
872     BOOST_CHECK(!ParseInt64("", &n));
873     BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
874     BOOST_CHECK(!ParseInt64("1 ", &n));
875     BOOST_CHECK(!ParseInt64("1a", &n));
876     BOOST_CHECK(!ParseInt64("aap", &n));
877     BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
878     const char test_bytes[] = {'1', 0, '1'};
879     std::string teststr(test_bytes, sizeof(test_bytes));
880     BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
881     // Overflow and underflow
882     BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
883     BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
884     BOOST_CHECK(!ParseInt64("-32482348723847471234", nullptr));
885     BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
886 }
887 
BOOST_AUTO_TEST_CASE(test_ParseUInt32)888 BOOST_AUTO_TEST_CASE(test_ParseUInt32)
889 {
890     uint32_t n;
891     // Valid values
892     BOOST_CHECK(ParseUInt32("1234", nullptr));
893     BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
894     BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
895     BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
896     BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
897     BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648);
898     BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295);
899     // Invalid values
900     BOOST_CHECK(!ParseUInt32("", &n));
901     BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
902     BOOST_CHECK(!ParseUInt32(" -1", &n));
903     BOOST_CHECK(!ParseUInt32("1 ", &n));
904     BOOST_CHECK(!ParseUInt32("1a", &n));
905     BOOST_CHECK(!ParseUInt32("aap", &n));
906     BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
907     BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
908     const char test_bytes[] = {'1', 0, '1'};
909     std::string teststr(test_bytes, sizeof(test_bytes));
910     BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs
911     // Overflow and underflow
912     BOOST_CHECK(!ParseUInt32("-2147483648", &n));
913     BOOST_CHECK(!ParseUInt32("4294967296", &n));
914     BOOST_CHECK(!ParseUInt32("-1234", &n));
915     BOOST_CHECK(!ParseUInt32("-32482348723847471234", nullptr));
916     BOOST_CHECK(!ParseUInt32("32482348723847471234", nullptr));
917 }
918 
BOOST_AUTO_TEST_CASE(test_ParseUInt64)919 BOOST_AUTO_TEST_CASE(test_ParseUInt64)
920 {
921     uint64_t n;
922     // Valid values
923     BOOST_CHECK(ParseUInt64("1234", nullptr));
924     BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
925     BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
926     BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
927     BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
928     BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
929     BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
930     BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
931     // Invalid values
932     BOOST_CHECK(!ParseUInt64("", &n));
933     BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
934     BOOST_CHECK(!ParseUInt64(" -1", &n));
935     BOOST_CHECK(!ParseUInt64("1 ", &n));
936     BOOST_CHECK(!ParseUInt64("1a", &n));
937     BOOST_CHECK(!ParseUInt64("aap", &n));
938     BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
939     const char test_bytes[] = {'1', 0, '1'};
940     std::string teststr(test_bytes, sizeof(test_bytes));
941     BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs
942     // Overflow and underflow
943     BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
944     BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
945     BOOST_CHECK(!ParseUInt64("-32482348723847471234", nullptr));
946     BOOST_CHECK(!ParseUInt64("-2147483648", &n));
947     BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
948     BOOST_CHECK(!ParseUInt64("-1234", &n));
949 }
950 
BOOST_AUTO_TEST_CASE(test_ParseDouble)951 BOOST_AUTO_TEST_CASE(test_ParseDouble)
952 {
953     double n;
954     // Valid values
955     BOOST_CHECK(ParseDouble("1234", nullptr));
956     BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
957     BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
958     BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
959     BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
960     BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
961     BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
962     BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
963     BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
964     // Invalid values
965     BOOST_CHECK(!ParseDouble("", &n));
966     BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
967     BOOST_CHECK(!ParseDouble("1 ", &n));
968     BOOST_CHECK(!ParseDouble("1a", &n));
969     BOOST_CHECK(!ParseDouble("aap", &n));
970     BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
971     const char test_bytes[] = {'1', 0, '1'};
972     std::string teststr(test_bytes, sizeof(test_bytes));
973     BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
974     // Overflow and underflow
975     BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
976     BOOST_CHECK(!ParseDouble("1e10000", nullptr));
977 }
978 
BOOST_AUTO_TEST_CASE(test_FormatParagraph)979 BOOST_AUTO_TEST_CASE(test_FormatParagraph)
980 {
981     BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
982     BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
983     BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
984     BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
985     BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
986     BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
987     BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n    test");
988 
989     // Make sure we don't indent a fully-new line following a too-long line ending
990     BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n    test\nabc");
991 
992     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");
993 
994     // Test wrap length is exact
995     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");
996     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");
997     // Indent should be included in length of lines
998     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");
999 
1000     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.");
1001     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.");
1002     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.");
1003     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.");
1004 }
1005 
BOOST_AUTO_TEST_CASE(test_FormatSubVersion)1006 BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
1007 {
1008     std::vector<std::string> comments;
1009     comments.push_back(std::string("comment1"));
1010     std::vector<std::string> comments2;
1011     comments2.push_back(std::string("comment1"));
1012     comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
1013     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
1014     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
1015     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
1016 }
1017 
BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)1018 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
1019 {
1020     int64_t amount = 0;
1021     BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
1022     BOOST_CHECK_EQUAL(amount, 0LL);
1023     BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
1024     BOOST_CHECK_EQUAL(amount, 100000000LL);
1025     BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
1026     BOOST_CHECK_EQUAL(amount, 0LL);
1027     BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
1028     BOOST_CHECK_EQUAL(amount, -10000000LL);
1029     BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
1030     BOOST_CHECK_EQUAL(amount, 110000000LL);
1031     BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
1032     BOOST_CHECK_EQUAL(amount, 110000000LL);
1033     BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
1034     BOOST_CHECK_EQUAL(amount, 1100000000LL);
1035     BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
1036     BOOST_CHECK_EQUAL(amount, 11000000LL);
1037     BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
1038     BOOST_CHECK_EQUAL(amount, 100000000000LL);
1039     BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
1040     BOOST_CHECK_EQUAL(amount, -100000000000LL);
1041     BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
1042     BOOST_CHECK_EQUAL(amount, 1LL);
1043     BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
1044     BOOST_CHECK_EQUAL(amount, 1LL);
1045     BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
1046     BOOST_CHECK_EQUAL(amount, -1LL);
1047     BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
1048     BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
1049     BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
1050     BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
1051     BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
1052     BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
1053 
1054     BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
1055     BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
1056     BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
1057     BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
1058     BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
1059     BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
1060     BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
1061     BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
1062     BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
1063     BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
1064     BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
1065     BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
1066     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
1067     BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
1068     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
1069     BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
1070     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
1071     BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
1072     BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
1073     BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
1074     BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
1075     BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
1076     BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
1077     BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
1078     BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
1079     BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
1080     BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
1081 }
1082 
TestOtherThread(fs::path dirname,std::string lockname,bool * result)1083 static void TestOtherThread(fs::path dirname, std::string lockname, bool *result)
1084 {
1085     *result = LockDirectory(dirname, lockname);
1086 }
1087 
1088 #ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork()
1089 static constexpr char LockCommand = 'L';
1090 static constexpr char UnlockCommand = 'U';
1091 static constexpr char ExitCommand = 'X';
1092 
TestOtherProcess(fs::path dirname,std::string lockname,int fd)1093 static void TestOtherProcess(fs::path dirname, std::string lockname, int fd)
1094 {
1095     char ch;
1096     while (true) {
1097         int rv = read(fd, &ch, 1); // Wait for command
1098         assert(rv == 1);
1099         switch(ch) {
1100         case LockCommand:
1101             ch = LockDirectory(dirname, lockname);
1102             rv = write(fd, &ch, 1);
1103             assert(rv == 1);
1104             break;
1105         case UnlockCommand:
1106             ReleaseDirectoryLocks();
1107             ch = true; // Always succeeds
1108             rv = write(fd, &ch, 1);
1109             assert(rv == 1);
1110             break;
1111         case ExitCommand:
1112             close(fd);
1113             exit(0);
1114         default:
1115             assert(0);
1116         }
1117     }
1118 }
1119 #endif
1120 
BOOST_AUTO_TEST_CASE(test_LockDirectory)1121 BOOST_AUTO_TEST_CASE(test_LockDirectory)
1122 {
1123     fs::path dirname = SetDataDir("test_LockDirectory") / fs::unique_path();
1124     const std::string lockname = ".lock";
1125 #ifndef WIN32
1126     // Revert SIGCHLD to default, otherwise boost.test will catch and fail on
1127     // it: there is BOOST_TEST_IGNORE_SIGCHLD but that only works when defined
1128     // at build-time of the boost library
1129     void (*old_handler)(int) = signal(SIGCHLD, SIG_DFL);
1130 
1131     // Fork another process for testing before creating the lock, so that we
1132     // won't fork while holding the lock (which might be undefined, and is not
1133     // relevant as test case as that is avoided with -daemonize).
1134     int fd[2];
1135     BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fd), 0);
1136     pid_t pid = fork();
1137     if (!pid) {
1138         BOOST_CHECK_EQUAL(close(fd[1]), 0); // Child: close parent end
1139         TestOtherProcess(dirname, lockname, fd[0]);
1140     }
1141     BOOST_CHECK_EQUAL(close(fd[0]), 0); // Parent: close child end
1142 #endif
1143     // Lock on non-existent directory should fail
1144     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), false);
1145 
1146     fs::create_directories(dirname);
1147 
1148     // Probing lock on new directory should succeed
1149     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1150 
1151     // Persistent lock on new directory should succeed
1152     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), true);
1153 
1154     // Another lock on the directory from the same thread should succeed
1155     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname), true);
1156 
1157     // Another lock on the directory from a different thread within the same process should succeed
1158     bool threadresult;
1159     std::thread thr(TestOtherThread, dirname, lockname, &threadresult);
1160     thr.join();
1161     BOOST_CHECK_EQUAL(threadresult, true);
1162 #ifndef WIN32
1163     // Try to acquire lock in child process while we're holding it, this should fail.
1164     char ch;
1165     BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1166     BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1167     BOOST_CHECK_EQUAL((bool)ch, false);
1168 
1169     // Give up our lock
1170     ReleaseDirectoryLocks();
1171     // Probing lock from our side now should succeed, but not hold on to the lock.
1172     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1173 
1174     // Try to acquire the lock in the child process, this should be successful.
1175     BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1176     BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1177     BOOST_CHECK_EQUAL((bool)ch, true);
1178 
1179     // When we try to probe the lock now, it should fail.
1180     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), false);
1181 
1182     // Unlock the lock in the child process
1183     BOOST_CHECK_EQUAL(write(fd[1], &UnlockCommand, 1), 1);
1184     BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1185     BOOST_CHECK_EQUAL((bool)ch, true);
1186 
1187     // When we try to probe the lock now, it should succeed.
1188     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1189 
1190     // Re-lock the lock in the child process, then wait for it to exit, check
1191     // successful return. After that, we check that exiting the process
1192     // has released the lock as we would expect by probing it.
1193     int processstatus;
1194     BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1195     BOOST_CHECK_EQUAL(write(fd[1], &ExitCommand, 1), 1);
1196     BOOST_CHECK_EQUAL(waitpid(pid, &processstatus, 0), pid);
1197     BOOST_CHECK_EQUAL(processstatus, 0);
1198     BOOST_CHECK_EQUAL(LockDirectory(dirname, lockname, true), true);
1199 
1200     // Restore SIGCHLD
1201     signal(SIGCHLD, old_handler);
1202     BOOST_CHECK_EQUAL(close(fd[1]), 0); // Close our side of the socketpair
1203 #endif
1204     // Clean up
1205     ReleaseDirectoryLocks();
1206     fs::remove_all(dirname);
1207 }
1208 
BOOST_AUTO_TEST_CASE(test_DirIsWritable)1209 BOOST_AUTO_TEST_CASE(test_DirIsWritable)
1210 {
1211     // Should be able to write to the data dir.
1212     fs::path tmpdirname = SetDataDir("test_DirIsWritable");
1213     BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
1214 
1215     // Should not be able to write to a non-existent dir.
1216     tmpdirname = tmpdirname / fs::unique_path();
1217     BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
1218 
1219     fs::create_directory(tmpdirname);
1220     // Should be able to write to it now.
1221     BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
1222     fs::remove(tmpdirname);
1223 }
1224 
BOOST_AUTO_TEST_CASE(test_ToLower)1225 BOOST_AUTO_TEST_CASE(test_ToLower)
1226 {
1227     BOOST_CHECK_EQUAL(ToLower('@'), '@');
1228     BOOST_CHECK_EQUAL(ToLower('A'), 'a');
1229     BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
1230     BOOST_CHECK_EQUAL(ToLower('['), '[');
1231     BOOST_CHECK_EQUAL(ToLower(0), 0);
1232     BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
1233 
1234     std::string testVector;
1235     Downcase(testVector);
1236     BOOST_CHECK_EQUAL(testVector, "");
1237 
1238     testVector = "#HODL";
1239     Downcase(testVector);
1240     BOOST_CHECK_EQUAL(testVector, "#hodl");
1241 
1242     testVector = "\x00\xfe\xff";
1243     Downcase(testVector);
1244     BOOST_CHECK_EQUAL(testVector, "\x00\xfe\xff");
1245 }
1246 
BOOST_AUTO_TEST_CASE(test_ToUpper)1247 BOOST_AUTO_TEST_CASE(test_ToUpper)
1248 {
1249     BOOST_CHECK_EQUAL(ToUpper('`'), '`');
1250     BOOST_CHECK_EQUAL(ToUpper('a'), 'A');
1251     BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
1252     BOOST_CHECK_EQUAL(ToUpper('{'), '{');
1253     BOOST_CHECK_EQUAL(ToUpper(0), 0);
1254     BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
1255 }
1256 
BOOST_AUTO_TEST_CASE(test_Capitalize)1257 BOOST_AUTO_TEST_CASE(test_Capitalize)
1258 {
1259     BOOST_CHECK_EQUAL(Capitalize(""), "");
1260     BOOST_CHECK_EQUAL(Capitalize("bitcoin"), "Bitcoin");
1261     BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
1262 }
1263 
BOOST_AUTO_TEST_CASE(test_LogEscapeMessage)1264 BOOST_AUTO_TEST_CASE(test_LogEscapeMessage)
1265 {
1266     // ASCII and UTF-8 must pass through unaltered.
1267     BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Valid log message貓"), "Valid log message貓");
1268     // Newlines must pass through unaltered.
1269     BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Message\n with newlines\n"), "Message\n with newlines\n");
1270     // Other control characters are escaped in C syntax.
1271     BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("\x01\x7f Corrupted log message\x0d"), R"(\x01\x7f Corrupted log message\x0d)");
1272     // Embedded NULL characters are escaped too.
1273     const std::string NUL("O\x00O", 3);
1274     BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage(NUL), R"(O\x00O)");
1275 }
1276 
1277 BOOST_AUTO_TEST_SUITE_END()
1278