1 // Copyright (c) 2011-2015 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.h"
6 
7 #include "clientversion.h"
8 #include "primitives/transaction.h"
9 #include "random.h"
10 #include "sync.h"
11 #include "utilstrencodings.h"
12 #include "utilmoneystr.h"
13 #include "test/test_bitcoin.h"
14 
15 #include <stdint.h>
16 #include <vector>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 using namespace std;
21 
BOOST_FIXTURE_TEST_SUITE(util_tests,BasicTestingSetup)22 BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
23 
24 BOOST_AUTO_TEST_CASE(util_criticalsection)
25 {
26     CCriticalSection cs;
27 
28     do {
29         LOCK(cs);
30         break;
31 
32         BOOST_ERROR("break was swallowed!");
33     } while(0);
34 
35     do {
36         TRY_LOCK(cs, lockTest);
37         if (lockTest)
38             break;
39 
40         BOOST_ERROR("break was swallowed!");
41     } while(0);
42 }
43 
44 static const unsigned char ParseHex_expected[65] = {
45     0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
46     0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
47     0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
48     0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
49     0x5f
50 };
BOOST_AUTO_TEST_CASE(util_ParseHex)51 BOOST_AUTO_TEST_CASE(util_ParseHex)
52 {
53     std::vector<unsigned char> result;
54     std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
55     // Basic test vector
56     result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
57     BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
58 
59     // Spaces between bytes must be supported
60     result = ParseHex("12 34 56 78");
61     BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
62 
63     // Leading space must be supported (used in CDBEnv::Salvage)
64     result = ParseHex(" 89 34 56 78");
65     BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
66 
67     // Stop parsing at invalid value
68     result = ParseHex("1234 invalid 1234");
69     BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
70 }
71 
BOOST_AUTO_TEST_CASE(util_HexStr)72 BOOST_AUTO_TEST_CASE(util_HexStr)
73 {
74     BOOST_CHECK_EQUAL(
75         HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
76         "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
77 
78     BOOST_CHECK_EQUAL(
79         HexStr(ParseHex_expected, ParseHex_expected + 5, true),
80         "04 67 8a fd b0");
81 
82     BOOST_CHECK_EQUAL(
83         HexStr(ParseHex_expected, ParseHex_expected, true),
84         "");
85 
86     std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
87 
88     BOOST_CHECK_EQUAL(
89         HexStr(ParseHex_vec, true),
90         "04 67 8a fd b0");
91 }
92 
93 
BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)94 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
95 {
96     BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00");
97     BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07");
98     BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
99     BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36");
100     BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
101 }
102 
BOOST_AUTO_TEST_CASE(util_ParseParameters)103 BOOST_AUTO_TEST_CASE(util_ParseParameters)
104 {
105     const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
106 
107     ParseParameters(0, (char**)argv_test);
108     BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
109 
110     ParseParameters(1, (char**)argv_test);
111     BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
112 
113     ParseParameters(5, (char**)argv_test);
114     // expectation: -ignored is ignored (program name argument),
115     // -a, -b and -ccc end up in map, -d ignored because it is after
116     // a non-option argument (non-GNU option parsing)
117     BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
118     BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
119                 && !mapArgs.count("f") && !mapArgs.count("-d"));
120     BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
121                 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
122 
123     BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
124     BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
125 }
126 
BOOST_AUTO_TEST_CASE(util_GetArg)127 BOOST_AUTO_TEST_CASE(util_GetArg)
128 {
129     mapArgs.clear();
130     mapArgs["strtest1"] = "string...";
131     // strtest2 undefined on purpose
132     mapArgs["inttest1"] = "12345";
133     mapArgs["inttest2"] = "81985529216486895";
134     // inttest3 undefined on purpose
135     mapArgs["booltest1"] = "";
136     // booltest2 undefined on purpose
137     mapArgs["booltest3"] = "0";
138     mapArgs["booltest4"] = "1";
139 
140     BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
141     BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
142     BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
143     BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
144     BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
145     BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true);
146     BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false);
147     BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false);
148     BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
149 }
150 
BOOST_AUTO_TEST_CASE(util_FormatMoney)151 BOOST_AUTO_TEST_CASE(util_FormatMoney)
152 {
153     BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
154     BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789");
155     BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00");
156 
157     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00");
158     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00");
159     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00");
160     BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00");
161     BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00");
162     BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00");
163     BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00");
164     BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00");
165     BOOST_CHECK_EQUAL(FormatMoney(COIN), "1.00");
166     BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10");
167     BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01");
168     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001");
169     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001");
170     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001");
171     BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
172     BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
173     BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
174 }
175 
BOOST_AUTO_TEST_CASE(util_ParseMoney)176 BOOST_AUTO_TEST_CASE(util_ParseMoney)
177 {
178     CAmount ret = 0;
179     BOOST_CHECK(ParseMoney("0.0", ret));
180     BOOST_CHECK_EQUAL(ret, 0);
181 
182     BOOST_CHECK(ParseMoney("12345.6789", ret));
183     BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
184 
185     BOOST_CHECK(ParseMoney("100000000.00", ret));
186     BOOST_CHECK_EQUAL(ret, COIN*100000000);
187     BOOST_CHECK(ParseMoney("10000000.00", ret));
188     BOOST_CHECK_EQUAL(ret, COIN*10000000);
189     BOOST_CHECK(ParseMoney("1000000.00", ret));
190     BOOST_CHECK_EQUAL(ret, COIN*1000000);
191     BOOST_CHECK(ParseMoney("100000.00", ret));
192     BOOST_CHECK_EQUAL(ret, COIN*100000);
193     BOOST_CHECK(ParseMoney("10000.00", ret));
194     BOOST_CHECK_EQUAL(ret, COIN*10000);
195     BOOST_CHECK(ParseMoney("1000.00", ret));
196     BOOST_CHECK_EQUAL(ret, COIN*1000);
197     BOOST_CHECK(ParseMoney("100.00", ret));
198     BOOST_CHECK_EQUAL(ret, COIN*100);
199     BOOST_CHECK(ParseMoney("10.00", ret));
200     BOOST_CHECK_EQUAL(ret, COIN*10);
201     BOOST_CHECK(ParseMoney("1.00", ret));
202     BOOST_CHECK_EQUAL(ret, COIN);
203     BOOST_CHECK(ParseMoney("1", ret));
204     BOOST_CHECK_EQUAL(ret, COIN);
205     BOOST_CHECK(ParseMoney("0.1", ret));
206     BOOST_CHECK_EQUAL(ret, COIN/10);
207     BOOST_CHECK(ParseMoney("0.01", ret));
208     BOOST_CHECK_EQUAL(ret, COIN/100);
209     BOOST_CHECK(ParseMoney("0.001", ret));
210     BOOST_CHECK_EQUAL(ret, COIN/1000);
211     BOOST_CHECK(ParseMoney("0.0001", ret));
212     BOOST_CHECK_EQUAL(ret, COIN/10000);
213     BOOST_CHECK(ParseMoney("0.00001", ret));
214     BOOST_CHECK_EQUAL(ret, COIN/100000);
215     BOOST_CHECK(ParseMoney("0.000001", ret));
216     BOOST_CHECK_EQUAL(ret, COIN/1000000);
217     BOOST_CHECK(ParseMoney("0.0000001", ret));
218     BOOST_CHECK_EQUAL(ret, COIN/10000000);
219     BOOST_CHECK(ParseMoney("0.00000001", ret));
220     BOOST_CHECK_EQUAL(ret, COIN/100000000);
221 
222     // Attempted 63 bit overflow should fail
223     BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
224 
225     // Parsing negative amounts must fail
226     BOOST_CHECK(!ParseMoney("-1", ret));
227 }
228 
BOOST_AUTO_TEST_CASE(util_IsHex)229 BOOST_AUTO_TEST_CASE(util_IsHex)
230 {
231     BOOST_CHECK(IsHex("00"));
232     BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
233     BOOST_CHECK(IsHex("ff"));
234     BOOST_CHECK(IsHex("FF"));
235 
236     BOOST_CHECK(!IsHex(""));
237     BOOST_CHECK(!IsHex("0"));
238     BOOST_CHECK(!IsHex("a"));
239     BOOST_CHECK(!IsHex("eleven"));
240     BOOST_CHECK(!IsHex("00xx00"));
241     BOOST_CHECK(!IsHex("0x0000"));
242 }
243 
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)244 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
245 {
246     int i;
247     int count=0;
248 
249     seed_insecure_rand(true);
250 
251     for (int mod=2;mod<11;mod++)
252     {
253         int mask = 1;
254         // Really rough binomal confidence approximation.
255         int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
256         //mask is 2^ceil(log2(mod))-1
257         while(mask<mod-1)mask=(mask<<1)+1;
258 
259         count = 0;
260         //How often does it get a zero from the uniform range [0,mod)?
261         for (i=0;i<10000;i++)
262         {
263             uint32_t rval;
264             do{
265                 rval=insecure_rand()&mask;
266             }while(rval>=(uint32_t)mod);
267             count += rval==0;
268         }
269         BOOST_CHECK(count<=10000/mod+err);
270         BOOST_CHECK(count>=10000/mod-err);
271     }
272 }
273 
BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)274 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
275 {
276     BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
277     BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
278     BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
279     BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
280     BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
281     BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
282     BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
283 }
284 
285 /* Test strprintf formatting directives.
286  * Put a string before and after to ensure sanity of element sizes on stack. */
287 #define B "check_prefix"
288 #define E "check_postfix"
BOOST_AUTO_TEST_CASE(strprintf_numbers)289 BOOST_AUTO_TEST_CASE(strprintf_numbers)
290 {
291     int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
292     uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
293     BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
294     BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
295     BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
296 
297     size_t st = 12345678; /* unsigned size_t test value */
298     ssize_t sst = -12345678; /* signed size_t test value */
299     BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
300     BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
301     BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
302 
303     ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
304     ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
305     BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
306     BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
307     BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
308 }
309 #undef B
310 #undef E
311 
312 /* Check for mingw/wine issue #3494
313  * Remove this test before time.ctime(0xffffffff) == 'Sun Feb  7 07:28:15 2106'
314  */
BOOST_AUTO_TEST_CASE(gettime)315 BOOST_AUTO_TEST_CASE(gettime)
316 {
317     BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
318 }
319 
BOOST_AUTO_TEST_CASE(test_ParseInt32)320 BOOST_AUTO_TEST_CASE(test_ParseInt32)
321 {
322     int32_t n;
323     // Valid values
324     BOOST_CHECK(ParseInt32("1234", NULL));
325     BOOST_CHECK(ParseInt32("0", &n) && n == 0);
326     BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
327     BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
328     BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
329     BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648);
330     BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
331     // Invalid values
332     BOOST_CHECK(!ParseInt32("", &n));
333     BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
334     BOOST_CHECK(!ParseInt32("1 ", &n));
335     BOOST_CHECK(!ParseInt32("1a", &n));
336     BOOST_CHECK(!ParseInt32("aap", &n));
337     BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
338     BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
339     const char test_bytes[] = {'1', 0, '1'};
340     std::string teststr(test_bytes, sizeof(test_bytes));
341     BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
342     // Overflow and underflow
343     BOOST_CHECK(!ParseInt32("-2147483649", NULL));
344     BOOST_CHECK(!ParseInt32("2147483648", NULL));
345     BOOST_CHECK(!ParseInt32("-32482348723847471234", NULL));
346     BOOST_CHECK(!ParseInt32("32482348723847471234", NULL));
347 }
348 
BOOST_AUTO_TEST_CASE(test_ParseInt64)349 BOOST_AUTO_TEST_CASE(test_ParseInt64)
350 {
351     int64_t n;
352     // Valid values
353     BOOST_CHECK(ParseInt64("1234", NULL));
354     BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
355     BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
356     BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
357     BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
358     BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
359     BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807);
360     BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1);
361     BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
362     // Invalid values
363     BOOST_CHECK(!ParseInt64("", &n));
364     BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
365     BOOST_CHECK(!ParseInt64("1 ", &n));
366     BOOST_CHECK(!ParseInt64("1a", &n));
367     BOOST_CHECK(!ParseInt64("aap", &n));
368     BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
369     const char test_bytes[] = {'1', 0, '1'};
370     std::string teststr(test_bytes, sizeof(test_bytes));
371     BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
372     // Overflow and underflow
373     BOOST_CHECK(!ParseInt64("-9223372036854775809", NULL));
374     BOOST_CHECK(!ParseInt64("9223372036854775808", NULL));
375     BOOST_CHECK(!ParseInt64("-32482348723847471234", NULL));
376     BOOST_CHECK(!ParseInt64("32482348723847471234", NULL));
377 }
378 
BOOST_AUTO_TEST_CASE(test_ParseUInt32)379 BOOST_AUTO_TEST_CASE(test_ParseUInt32)
380 {
381     uint32_t n;
382     // Valid values
383     BOOST_CHECK(ParseUInt32("1234", NULL));
384     BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
385     BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
386     BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
387     BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
388     BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648);
389     BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295);
390     // Invalid values
391     BOOST_CHECK(!ParseUInt32("", &n));
392     BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
393     BOOST_CHECK(!ParseUInt32(" -1", &n));
394     BOOST_CHECK(!ParseUInt32("1 ", &n));
395     BOOST_CHECK(!ParseUInt32("1a", &n));
396     BOOST_CHECK(!ParseUInt32("aap", &n));
397     BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
398     BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
399     const char test_bytes[] = {'1', 0, '1'};
400     std::string teststr(test_bytes, sizeof(test_bytes));
401     BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs
402     // Overflow and underflow
403     BOOST_CHECK(!ParseUInt32("-2147483648", &n));
404     BOOST_CHECK(!ParseUInt32("4294967296", &n));
405     BOOST_CHECK(!ParseUInt32("-1234", &n));
406     BOOST_CHECK(!ParseUInt32("-32482348723847471234", NULL));
407     BOOST_CHECK(!ParseUInt32("32482348723847471234", NULL));
408 }
409 
BOOST_AUTO_TEST_CASE(test_ParseUInt64)410 BOOST_AUTO_TEST_CASE(test_ParseUInt64)
411 {
412     uint64_t n;
413     // Valid values
414     BOOST_CHECK(ParseUInt64("1234", NULL));
415     BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
416     BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
417     BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
418     BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
419     BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
420     BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
421     BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
422     // Invalid values
423     BOOST_CHECK(!ParseUInt64("", &n));
424     BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
425     BOOST_CHECK(!ParseUInt64(" -1", &n));
426     BOOST_CHECK(!ParseUInt64("1 ", &n));
427     BOOST_CHECK(!ParseUInt64("1a", &n));
428     BOOST_CHECK(!ParseUInt64("aap", &n));
429     BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
430     const char test_bytes[] = {'1', 0, '1'};
431     std::string teststr(test_bytes, sizeof(test_bytes));
432     BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs
433     // Overflow and underflow
434     BOOST_CHECK(!ParseUInt64("-9223372036854775809", NULL));
435     BOOST_CHECK(!ParseUInt64("18446744073709551616", NULL));
436     BOOST_CHECK(!ParseUInt64("-32482348723847471234", NULL));
437     BOOST_CHECK(!ParseUInt64("-2147483648", &n));
438     BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
439     BOOST_CHECK(!ParseUInt64("-1234", &n));
440 }
441 
BOOST_AUTO_TEST_CASE(test_ParseDouble)442 BOOST_AUTO_TEST_CASE(test_ParseDouble)
443 {
444     double n;
445     // Valid values
446     BOOST_CHECK(ParseDouble("1234", NULL));
447     BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
448     BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
449     BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
450     BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
451     BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
452     BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
453     BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
454     BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
455     // Invalid values
456     BOOST_CHECK(!ParseDouble("", &n));
457     BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
458     BOOST_CHECK(!ParseDouble("1 ", &n));
459     BOOST_CHECK(!ParseDouble("1a", &n));
460     BOOST_CHECK(!ParseDouble("aap", &n));
461     BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
462     const char test_bytes[] = {'1', 0, '1'};
463     std::string teststr(test_bytes, sizeof(test_bytes));
464     BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
465     // Overflow and underflow
466     BOOST_CHECK(!ParseDouble("-1e10000", NULL));
467     BOOST_CHECK(!ParseDouble("1e10000", NULL));
468 }
469 
BOOST_AUTO_TEST_CASE(test_FormatParagraph)470 BOOST_AUTO_TEST_CASE(test_FormatParagraph)
471 {
472     BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
473     BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
474     BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
475     BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
476     BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
477     BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
478     BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n    test");
479 
480     // Make sure we don't indent a fully-new line following a too-long line ending
481     BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n    test\nabc");
482 
483     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");
484 
485     // Test wrap length is exact
486     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");
487     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");
488     // Indent should be included in length of lines
489     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");
490 
491     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.");
492     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.");
493     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.");
494     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.");
495 }
496 
BOOST_AUTO_TEST_CASE(test_FormatSubVersion)497 BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
498 {
499     std::vector<std::string> comments;
500     comments.push_back(std::string("comment1"));
501     std::vector<std::string> comments2;
502     comments2.push_back(std::string("comment1"));
503     comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
504     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
505     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
506     BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
507 }
508 
BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)509 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
510 {
511     int64_t amount = 0;
512     BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
513     BOOST_CHECK_EQUAL(amount, 0LL);
514     BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
515     BOOST_CHECK_EQUAL(amount, 100000000LL);
516     BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
517     BOOST_CHECK_EQUAL(amount, 0LL);
518     BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
519     BOOST_CHECK_EQUAL(amount, -10000000LL);
520     BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
521     BOOST_CHECK_EQUAL(amount, 110000000LL);
522     BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
523     BOOST_CHECK_EQUAL(amount, 110000000LL);
524     BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
525     BOOST_CHECK_EQUAL(amount, 1100000000LL);
526     BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
527     BOOST_CHECK_EQUAL(amount, 11000000LL);
528     BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
529     BOOST_CHECK_EQUAL(amount, 100000000000LL);
530     BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
531     BOOST_CHECK_EQUAL(amount, -100000000000LL);
532     BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
533     BOOST_CHECK_EQUAL(amount, 1LL);
534     BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
535     BOOST_CHECK_EQUAL(amount, 1LL);
536     BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
537     BOOST_CHECK_EQUAL(amount, -1LL);
538     BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
539     BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
540     BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
541     BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
542     BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
543     BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
544 
545     BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
546     BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
547     BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
548     BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
549     BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
550     BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
551     BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
552     BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
553     BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
554     BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
555     BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
556     BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
557     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
558     BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
559     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
560     BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
561     BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
562     BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
563     BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
564     BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
565     BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
566     BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
567     BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
568     BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
569     BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
570     BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
571     BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
572 }
573 
574 BOOST_AUTO_TEST_SUITE_END()
575