You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_tests.cpp 26KB

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