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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright (c) 2011-2014 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 "random.h"
  8. #include "sync.h"
  9. #include "utilstrencodings.h"
  10. #include "utilmoneystr.h"
  11. #include "test/test_bitcoin.h"
  12. #include <stdint.h>
  13. #include <vector>
  14. #include <boost/test/unit_test.hpp>
  15. using namespace std;
  16. BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
  17. BOOST_AUTO_TEST_CASE(util_criticalsection)
  18. {
  19. CCriticalSection cs;
  20. do {
  21. LOCK(cs);
  22. break;
  23. BOOST_ERROR("break was swallowed!");
  24. } while(0);
  25. do {
  26. TRY_LOCK(cs, lockTest);
  27. if (lockTest)
  28. break;
  29. BOOST_ERROR("break was swallowed!");
  30. } while(0);
  31. }
  32. static const unsigned char ParseHex_expected[65] = {
  33. 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
  34. 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
  35. 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
  36. 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
  37. 0x5f
  38. };
  39. BOOST_AUTO_TEST_CASE(util_ParseHex)
  40. {
  41. std::vector<unsigned char> result;
  42. std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
  43. // Basic test vector
  44. result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
  45. BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
  46. // Spaces between bytes must be supported
  47. result = ParseHex("12 34 56 78");
  48. BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
  49. // Stop parsing at invalid value
  50. result = ParseHex("1234 invalid 1234");
  51. BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
  52. }
  53. BOOST_AUTO_TEST_CASE(util_HexStr)
  54. {
  55. BOOST_CHECK_EQUAL(
  56. HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
  57. "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
  58. BOOST_CHECK_EQUAL(
  59. HexStr(ParseHex_expected, ParseHex_expected + 5, true),
  60. "04 67 8a fd b0");
  61. BOOST_CHECK_EQUAL(
  62. HexStr(ParseHex_expected, ParseHex_expected, true),
  63. "");
  64. std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
  65. BOOST_CHECK_EQUAL(
  66. HexStr(ParseHex_vec, true),
  67. "04 67 8a fd b0");
  68. }
  69. BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
  70. {
  71. BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00");
  72. BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07");
  73. BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
  74. BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36");
  75. BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
  76. }
  77. BOOST_AUTO_TEST_CASE(util_ParseParameters)
  78. {
  79. const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
  80. ParseParameters(0, (char**)argv_test);
  81. BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
  82. ParseParameters(1, (char**)argv_test);
  83. BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
  84. ParseParameters(5, (char**)argv_test);
  85. // expectation: -ignored is ignored (program name argument),
  86. // -a, -b and -ccc end up in map, -d ignored because it is after
  87. // a non-option argument (non-GNU option parsing)
  88. BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
  89. BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
  90. && !mapArgs.count("f") && !mapArgs.count("-d"));
  91. BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
  92. && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
  93. BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
  94. BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
  95. }
  96. BOOST_AUTO_TEST_CASE(util_GetArg)
  97. {
  98. mapArgs.clear();
  99. mapArgs["strtest1"] = "string...";
  100. // strtest2 undefined on purpose
  101. mapArgs["inttest1"] = "12345";
  102. mapArgs["inttest2"] = "81985529216486895";
  103. // inttest3 undefined on purpose
  104. mapArgs["booltest1"] = "";
  105. // booltest2 undefined on purpose
  106. mapArgs["booltest3"] = "0";
  107. mapArgs["booltest4"] = "1";
  108. BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
  109. BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
  110. BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
  111. BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
  112. BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
  113. BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true);
  114. BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false);
  115. BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false);
  116. BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
  117. }
  118. BOOST_AUTO_TEST_CASE(util_FormatMoney)
  119. {
  120. BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
  121. BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
  122. BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
  123. BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
  124. BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
  125. BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
  126. BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
  127. BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
  128. BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
  129. BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
  130. BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
  131. BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
  132. BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
  133. BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
  134. BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
  135. BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
  136. BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
  137. BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
  138. BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
  139. BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
  140. BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
  141. BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
  142. }
  143. BOOST_AUTO_TEST_CASE(util_ParseMoney)
  144. {
  145. CAmount ret = 0;
  146. BOOST_CHECK(ParseMoney("0.0", ret));
  147. BOOST_CHECK_EQUAL(ret, 0);
  148. BOOST_CHECK(ParseMoney("12345.6789", ret));
  149. BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
  150. BOOST_CHECK(ParseMoney("100000000.00", ret));
  151. BOOST_CHECK_EQUAL(ret, COIN*100000000);
  152. BOOST_CHECK(ParseMoney("10000000.00", ret));
  153. BOOST_CHECK_EQUAL(ret, COIN*10000000);
  154. BOOST_CHECK(ParseMoney("1000000.00", ret));
  155. BOOST_CHECK_EQUAL(ret, COIN*1000000);
  156. BOOST_CHECK(ParseMoney("100000.00", ret));
  157. BOOST_CHECK_EQUAL(ret, COIN*100000);
  158. BOOST_CHECK(ParseMoney("10000.00", ret));
  159. BOOST_CHECK_EQUAL(ret, COIN*10000);
  160. BOOST_CHECK(ParseMoney("1000.00", ret));
  161. BOOST_CHECK_EQUAL(ret, COIN*1000);
  162. BOOST_CHECK(ParseMoney("100.00", ret));
  163. BOOST_CHECK_EQUAL(ret, COIN*100);
  164. BOOST_CHECK(ParseMoney("10.00", ret));
  165. BOOST_CHECK_EQUAL(ret, COIN*10);
  166. BOOST_CHECK(ParseMoney("1.00", ret));
  167. BOOST_CHECK_EQUAL(ret, COIN);
  168. BOOST_CHECK(ParseMoney("0.1", ret));
  169. BOOST_CHECK_EQUAL(ret, COIN/10);
  170. BOOST_CHECK(ParseMoney("0.01", ret));
  171. BOOST_CHECK_EQUAL(ret, COIN/100);
  172. BOOST_CHECK(ParseMoney("0.001", ret));
  173. BOOST_CHECK_EQUAL(ret, COIN/1000);
  174. BOOST_CHECK(ParseMoney("0.0001", ret));
  175. BOOST_CHECK_EQUAL(ret, COIN/10000);
  176. BOOST_CHECK(ParseMoney("0.00001", ret));
  177. BOOST_CHECK_EQUAL(ret, COIN/100000);
  178. BOOST_CHECK(ParseMoney("0.000001", ret));
  179. BOOST_CHECK_EQUAL(ret, COIN/1000000);
  180. BOOST_CHECK(ParseMoney("0.0000001", ret));
  181. BOOST_CHECK_EQUAL(ret, COIN/10000000);
  182. BOOST_CHECK(ParseMoney("0.00000001", ret));
  183. BOOST_CHECK_EQUAL(ret, COIN/100000000);
  184. // Attempted 63 bit overflow should fail
  185. BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
  186. }
  187. BOOST_AUTO_TEST_CASE(util_IsHex)
  188. {
  189. BOOST_CHECK(IsHex("00"));
  190. BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
  191. BOOST_CHECK(IsHex("ff"));
  192. BOOST_CHECK(IsHex("FF"));
  193. BOOST_CHECK(!IsHex(""));
  194. BOOST_CHECK(!IsHex("0"));
  195. BOOST_CHECK(!IsHex("a"));
  196. BOOST_CHECK(!IsHex("eleven"));
  197. BOOST_CHECK(!IsHex("00xx00"));
  198. BOOST_CHECK(!IsHex("0x0000"));
  199. }
  200. BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
  201. {
  202. int i;
  203. int count=0;
  204. seed_insecure_rand(true);
  205. for (int mod=2;mod<11;mod++)
  206. {
  207. int mask = 1;
  208. // Really rough binomal confidence approximation.
  209. int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
  210. //mask is 2^ceil(log2(mod))-1
  211. while(mask<mod-1)mask=(mask<<1)+1;
  212. count = 0;
  213. //How often does it get a zero from the uniform range [0,mod)?
  214. for (i=0;i<10000;i++)
  215. {
  216. uint32_t rval;
  217. do{
  218. rval=insecure_rand()&mask;
  219. }while(rval>=(uint32_t)mod);
  220. count += rval==0;
  221. }
  222. BOOST_CHECK(count<=10000/mod+err);
  223. BOOST_CHECK(count>=10000/mod-err);
  224. }
  225. }
  226. BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
  227. {
  228. BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
  229. BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
  230. BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
  231. BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
  232. BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
  233. BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
  234. BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
  235. }
  236. /* Test strprintf formatting directives.
  237. * Put a string before and after to ensure sanity of element sizes on stack. */
  238. #define B "check_prefix"
  239. #define E "check_postfix"
  240. BOOST_AUTO_TEST_CASE(strprintf_numbers)
  241. {
  242. int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
  243. uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
  244. BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
  245. BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
  246. BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
  247. size_t st = 12345678; /* unsigned size_t test value */
  248. ssize_t sst = -12345678; /* signed size_t test value */
  249. BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
  250. BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
  251. BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
  252. ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
  253. ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
  254. BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
  255. BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
  256. BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
  257. }
  258. #undef B
  259. #undef E
  260. /* Check for mingw/wine issue #3494
  261. * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
  262. */
  263. BOOST_AUTO_TEST_CASE(gettime)
  264. {
  265. BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
  266. }
  267. BOOST_AUTO_TEST_CASE(test_ParseInt32)
  268. {
  269. int32_t n;
  270. // Valid values
  271. BOOST_CHECK(ParseInt32("1234", NULL));
  272. BOOST_CHECK(ParseInt32("0", &n) && n == 0);
  273. BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
  274. BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
  275. BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
  276. BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648);
  277. BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
  278. // Invalid values
  279. BOOST_CHECK(!ParseInt32("1a", &n));
  280. BOOST_CHECK(!ParseInt32("aap", &n));
  281. BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
  282. // Overflow and underflow
  283. BOOST_CHECK(!ParseInt32("-2147483649", NULL));
  284. BOOST_CHECK(!ParseInt32("2147483648", NULL));
  285. BOOST_CHECK(!ParseInt32("-32482348723847471234", NULL));
  286. BOOST_CHECK(!ParseInt32("32482348723847471234", NULL));
  287. }
  288. BOOST_AUTO_TEST_CASE(test_FormatParagraph)
  289. {
  290. BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
  291. BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
  292. BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), "test");
  293. BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
  294. BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
  295. BOOST_CHECK_EQUAL(FormatParagraph("testerde test ", 4, 0), "testerde\ntest");
  296. BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
  297. BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string. This is a second sentence in the very long test string."), "This is a very long test string. This is a second sentence in the very long\ntest string.");
  298. }
  299. BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
  300. {
  301. std::vector<std::string> comments;
  302. comments.push_back(std::string("comment1"));
  303. std::vector<std::string> comments2;
  304. comments2.push_back(std::string("comment1"));
  305. comments2.push_back(std::string("comment2"));
  306. BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
  307. BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
  308. BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; comment2)/"));
  309. }
  310. BOOST_AUTO_TEST_SUITE_END()