test_util.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2009, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONTROL_PRIVATE
  7. #define MEMPOOL_PRIVATE
  8. #include "or.h"
  9. #include "test.h"
  10. #include "mempool.h"
  11. #include "memarea.h"
  12. /** Test basic utility functionality. */
  13. static void
  14. test_util_misc(void)
  15. {
  16. struct timeval start, end;
  17. struct tm a_time;
  18. char timestr[RFC1123_TIME_LEN+1];
  19. char buf[1024];
  20. time_t t_res;
  21. int i;
  22. uint32_t u32;
  23. uint16_t u16;
  24. char *cp, *k, *v;
  25. const char *str;
  26. start.tv_sec = 5;
  27. start.tv_usec = 5000;
  28. end.tv_sec = 5;
  29. end.tv_usec = 5000;
  30. test_eq(0L, tv_udiff(&start, &end));
  31. end.tv_usec = 7000;
  32. test_eq(2000L, tv_udiff(&start, &end));
  33. end.tv_sec = 6;
  34. test_eq(1002000L, tv_udiff(&start, &end));
  35. end.tv_usec = 0;
  36. test_eq(995000L, tv_udiff(&start, &end));
  37. end.tv_sec = 4;
  38. test_eq(-1005000L, tv_udiff(&start, &end));
  39. end.tv_usec = 999990;
  40. start.tv_sec = 1;
  41. start.tv_usec = 500;
  42. /* The test values here are confirmed to be correct on a platform
  43. * with a working timegm. */
  44. a_time.tm_year = 2003-1900;
  45. a_time.tm_mon = 7;
  46. a_time.tm_mday = 30;
  47. a_time.tm_hour = 6;
  48. a_time.tm_min = 14;
  49. a_time.tm_sec = 55;
  50. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  51. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  52. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  53. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  54. a_time.tm_mday = 10;
  55. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  56. format_rfc1123_time(timestr, 0);
  57. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  58. format_rfc1123_time(timestr, (time_t)1091580502UL);
  59. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  60. t_res = 0;
  61. i = parse_rfc1123_time(timestr, &t_res);
  62. test_eq(i,0);
  63. test_eq(t_res, (time_t)1091580502UL);
  64. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  65. tor_gettimeofday(&start);
  66. /* Tests for corner cases of strl operations */
  67. test_eq(5, strlcpy(buf, "Hello", 0));
  68. strlcpy(buf, "Hello", sizeof(buf));
  69. test_eq(10, strlcat(buf, "Hello", 5));
  70. /* Test tor_strstrip() */
  71. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  72. tor_strstrip(buf, ",!");
  73. test_streq(buf, "Testing 1 2 3");
  74. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  75. tor_strstrip(buf, "!? ");
  76. test_streq(buf, "Testing123");
  77. /* Test parse_addr_port */
  78. cp = NULL; u32 = 3; u16 = 3;
  79. test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
  80. test_streq(cp, "1.2.3.4");
  81. test_eq(u32, 0x01020304u);
  82. test_eq(u16, 0);
  83. tor_free(cp);
  84. test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
  85. test_streq(cp, "4.3.2.1");
  86. test_eq(u32, 0x04030201u);
  87. test_eq(u16, 99);
  88. tor_free(cp);
  89. test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
  90. &cp, NULL, &u16));
  91. test_streq(cp, "nonexistent.address");
  92. test_eq(u16, 4040);
  93. tor_free(cp);
  94. test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
  95. test_streq(cp, "localhost");
  96. test_eq(u32, 0x7f000001u);
  97. test_eq(u16, 9999);
  98. tor_free(cp);
  99. u32 = 3;
  100. test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
  101. test_eq(cp, NULL);
  102. test_eq(u32, 0x7f000001u);
  103. test_eq(u16, 0);
  104. tor_free(cp);
  105. test_eq(0, addr_mask_get_bits(0x0u));
  106. test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
  107. test_eq(16, addr_mask_get_bits(0xFFFF0000u));
  108. test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
  109. test_eq(1, addr_mask_get_bits(0x80000000u));
  110. /* Test tor_parse_long. */
  111. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  112. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  113. test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
  114. /* Test tor_parse_ulong */
  115. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  116. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  117. /* Test tor_parse_uint64. */
  118. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  119. test_assert(i == 1);
  120. test_streq(cp, " x");
  121. test_assert(U64_LITERAL(12345678901) ==
  122. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  123. test_assert(i == 1);
  124. test_streq(cp, "");
  125. test_assert(U64_LITERAL(0) ==
  126. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  127. test_assert(i == 0);
  128. {
  129. /* Test tor_parse_double. */
  130. double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
  131. test_assert(i == 1);
  132. test_assert(DBL_TO_U64(d) == 10);
  133. d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
  134. test_assert(i == 1);
  135. test_assert(DBL_TO_U64(d) == 0);
  136. d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
  137. test_assert(i == 0);
  138. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
  139. test_assert(i == 0);
  140. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
  141. test_assert(i == 1);
  142. d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
  143. test_assert(i == 1);
  144. }
  145. /* Test failing snprintf cases */
  146. test_eq(-1, tor_snprintf(buf, 0, "Foo"));
  147. test_eq(-1, tor_snprintf(buf, 2, "Foo"));
  148. /* Test printf with uint64 */
  149. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  150. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  151. test_streq(buf, "x!12345678901!x");
  152. /* Test parse_config_line_from_str */
  153. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  154. "k2\n"
  155. "k3 \n" "\n" " \n" "#comment\n"
  156. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  157. "kseven \"a quoted 'string\"\n"
  158. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  159. , sizeof(buf));
  160. str = buf;
  161. str = parse_config_line_from_str(str, &k, &v);
  162. test_streq(k, "k");
  163. test_streq(v, "v");
  164. tor_free(k); tor_free(v);
  165. test_assert(!strcmpstart(str, "key value with"));
  166. str = parse_config_line_from_str(str, &k, &v);
  167. test_streq(k, "key");
  168. test_streq(v, "value with spaces");
  169. tor_free(k); tor_free(v);
  170. test_assert(!strcmpstart(str, "keykey"));
  171. str = parse_config_line_from_str(str, &k, &v);
  172. test_streq(k, "keykey");
  173. test_streq(v, "val");
  174. tor_free(k); tor_free(v);
  175. test_assert(!strcmpstart(str, "k2\n"));
  176. str = parse_config_line_from_str(str, &k, &v);
  177. test_streq(k, "k2");
  178. test_streq(v, "");
  179. tor_free(k); tor_free(v);
  180. test_assert(!strcmpstart(str, "k3 \n"));
  181. str = parse_config_line_from_str(str, &k, &v);
  182. test_streq(k, "k3");
  183. test_streq(v, "");
  184. tor_free(k); tor_free(v);
  185. test_assert(!strcmpstart(str, "#comment"));
  186. str = parse_config_line_from_str(str, &k, &v);
  187. test_streq(k, "k4");
  188. test_streq(v, "");
  189. tor_free(k); tor_free(v);
  190. test_assert(!strcmpstart(str, "k5#abc"));
  191. str = parse_config_line_from_str(str, &k, &v);
  192. test_streq(k, "k5");
  193. test_streq(v, "");
  194. tor_free(k); tor_free(v);
  195. test_assert(!strcmpstart(str, "k6"));
  196. str = parse_config_line_from_str(str, &k, &v);
  197. test_streq(k, "k6");
  198. test_streq(v, "val");
  199. tor_free(k); tor_free(v);
  200. test_assert(!strcmpstart(str, "kseven"));
  201. str = parse_config_line_from_str(str, &k, &v);
  202. test_streq(k, "kseven");
  203. test_streq(v, "a quoted \'string");
  204. tor_free(k); tor_free(v);
  205. test_assert(!strcmpstart(str, "k8 "));
  206. str = parse_config_line_from_str(str, &k, &v);
  207. test_streq(k, "k8");
  208. test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  209. tor_free(k); tor_free(v);
  210. test_streq(str, "");
  211. /* Test for strcmpstart and strcmpend. */
  212. test_assert(strcmpstart("abcdef", "abcdef")==0);
  213. test_assert(strcmpstart("abcdef", "abc")==0);
  214. test_assert(strcmpstart("abcdef", "abd")<0);
  215. test_assert(strcmpstart("abcdef", "abb")>0);
  216. test_assert(strcmpstart("ab", "abb")<0);
  217. test_assert(strcmpend("abcdef", "abcdef")==0);
  218. test_assert(strcmpend("abcdef", "def")==0);
  219. test_assert(strcmpend("abcdef", "deg")<0);
  220. test_assert(strcmpend("abcdef", "dee")>0);
  221. test_assert(strcmpend("ab", "abb")<0);
  222. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  223. test_assert(strcasecmpend("abcdef", "dEF")==0);
  224. test_assert(strcasecmpend("abcDEf", "deg")<0);
  225. test_assert(strcasecmpend("abcdef", "DEE")>0);
  226. test_assert(strcasecmpend("ab", "abB")<0);
  227. /* Test mem_is_zero */
  228. memset(buf,0,128);
  229. buf[128] = 'x';
  230. test_assert(tor_digest_is_zero(buf));
  231. test_assert(tor_mem_is_zero(buf, 10));
  232. test_assert(tor_mem_is_zero(buf, 20));
  233. test_assert(tor_mem_is_zero(buf, 128));
  234. test_assert(!tor_mem_is_zero(buf, 129));
  235. buf[60] = (char)255;
  236. test_assert(!tor_mem_is_zero(buf, 128));
  237. buf[0] = (char)1;
  238. test_assert(!tor_mem_is_zero(buf, 10));
  239. /* Test inet_ntop */
  240. {
  241. char tmpbuf[TOR_ADDR_BUF_LEN];
  242. const char *ip = "176.192.208.224";
  243. struct in_addr in;
  244. tor_inet_pton(AF_INET, ip, &in);
  245. tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf));
  246. test_streq(tmpbuf, ip);
  247. }
  248. /* Test 'escaped' */
  249. test_streq("\"\"", escaped(""));
  250. test_streq("\"abcd\"", escaped("abcd"));
  251. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  252. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  253. test_assert(NULL == escaped(NULL));
  254. /* Test strndup and memdup */
  255. {
  256. const char *s = "abcdefghijklmnopqrstuvwxyz";
  257. cp = tor_strndup(s, 30);
  258. test_streq(cp, s); /* same string, */
  259. test_neq(cp, s); /* but different pointers. */
  260. tor_free(cp);
  261. cp = tor_strndup(s, 5);
  262. test_streq(cp, "abcde");
  263. tor_free(cp);
  264. s = "a\0b\0c\0d\0e\0";
  265. cp = tor_memdup(s,10);
  266. test_memeq(cp, s, 10); /* same ram, */
  267. test_neq(cp, s); /* but different pointers. */
  268. tor_free(cp);
  269. }
  270. /* Test str-foo functions */
  271. cp = tor_strdup("abcdef");
  272. test_assert(tor_strisnonupper(cp));
  273. cp[3] = 'D';
  274. test_assert(!tor_strisnonupper(cp));
  275. tor_strupper(cp);
  276. test_streq(cp, "ABCDEF");
  277. test_assert(tor_strisprint(cp));
  278. cp[3] = 3;
  279. test_assert(!tor_strisprint(cp));
  280. tor_free(cp);
  281. /* Test eat_whitespace. */
  282. {
  283. const char *s = " \n a";
  284. test_eq_ptr(eat_whitespace(s), s+4);
  285. s = "abcd";
  286. test_eq_ptr(eat_whitespace(s), s);
  287. s = "#xyz\nab";
  288. test_eq_ptr(eat_whitespace(s), s+5);
  289. }
  290. /* Test memmem and memstr */
  291. {
  292. const char *haystack = "abcde";
  293. tor_assert(!tor_memmem(haystack, 5, "ef", 2));
  294. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  295. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  296. haystack = "ababcad";
  297. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  298. test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
  299. test_assert(!tor_memstr(haystack, 7, "fe"));
  300. test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
  301. }
  302. /* Test wrap_string */
  303. {
  304. smartlist_t *sl = smartlist_create();
  305. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  306. 10, "", "");
  307. cp = smartlist_join_strings(sl, "", 0, NULL);
  308. test_streq(cp,
  309. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
  310. tor_free(cp);
  311. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  312. smartlist_clear(sl);
  313. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  314. 16, "### ", "# ");
  315. cp = smartlist_join_strings(sl, "", 0, NULL);
  316. test_streq(cp,
  317. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  318. "# woot.\n");
  319. tor_free(cp);
  320. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  321. smartlist_free(sl);
  322. }
  323. /* now make sure time works. */
  324. tor_gettimeofday(&end);
  325. /* We might've timewarped a little. */
  326. test_assert(tv_udiff(&start, &end) >= -5000);
  327. /* Test tor_log2(). */
  328. test_eq(tor_log2(64), 6);
  329. test_eq(tor_log2(65), 6);
  330. test_eq(tor_log2(63), 5);
  331. test_eq(tor_log2(1), 0);
  332. test_eq(tor_log2(2), 1);
  333. test_eq(tor_log2(3), 1);
  334. test_eq(tor_log2(4), 2);
  335. test_eq(tor_log2(5), 2);
  336. test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
  337. test_eq(tor_log2(UINT64_MAX), 63);
  338. /* Test round_to_power_of_2 */
  339. test_eq(round_to_power_of_2(120), 128);
  340. test_eq(round_to_power_of_2(128), 128);
  341. test_eq(round_to_power_of_2(130), 128);
  342. test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
  343. U64_LITERAL(1)<<55);
  344. test_eq(round_to_power_of_2(0), 2);
  345. done:
  346. ;
  347. }
  348. /** mutex for thread test to stop the threads hitting data at the same time. */
  349. static tor_mutex_t *_thread_test_mutex = NULL;
  350. /** mutexes for the thread test to make sure that the threads have to
  351. * interleave somewhat. */
  352. static tor_mutex_t *_thread_test_start1 = NULL,
  353. *_thread_test_start2 = NULL;
  354. /** Shared strmap for the thread test. */
  355. static strmap_t *_thread_test_strmap = NULL;
  356. /** The name of thread1 for the thread test */
  357. static char *_thread1_name = NULL;
  358. /** The name of thread2 for the thread test */
  359. static char *_thread2_name = NULL;
  360. static void _thread_test_func(void* _s) ATTR_NORETURN;
  361. /** How many iterations have the threads in the unit test run? */
  362. static int t1_count = 0, t2_count = 0;
  363. /** Helper function for threading unit tests: This function runs in a
  364. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  365. * should start, then it repeatedly alters _test_thread_strmap protected by
  366. * _thread_test_mutex. */
  367. static void
  368. _thread_test_func(void* _s)
  369. {
  370. char *s = _s;
  371. int i, *count;
  372. tor_mutex_t *m;
  373. char buf[64];
  374. char **cp;
  375. if (!strcmp(s, "thread 1")) {
  376. m = _thread_test_start1;
  377. cp = &_thread1_name;
  378. count = &t1_count;
  379. } else {
  380. m = _thread_test_start2;
  381. cp = &_thread2_name;
  382. count = &t2_count;
  383. }
  384. tor_mutex_acquire(m);
  385. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  386. *cp = tor_strdup(buf);
  387. for (i=0; i<10000; ++i) {
  388. tor_mutex_acquire(_thread_test_mutex);
  389. strmap_set(_thread_test_strmap, "last to run", *cp);
  390. ++*count;
  391. tor_mutex_release(_thread_test_mutex);
  392. }
  393. tor_mutex_acquire(_thread_test_mutex);
  394. strmap_set(_thread_test_strmap, s, *cp);
  395. tor_mutex_release(_thread_test_mutex);
  396. tor_mutex_release(m);
  397. spawn_exit();
  398. }
  399. /** Run unit tests for threading logic. */
  400. static void
  401. test_util_threads(void)
  402. {
  403. char *s1 = NULL, *s2 = NULL;
  404. int done = 0, timedout = 0;
  405. time_t started;
  406. #ifndef TOR_IS_MULTITHREADED
  407. /* Skip this test if we aren't threading. We should be threading most
  408. * everywhere by now. */
  409. if (1)
  410. return;
  411. #endif
  412. _thread_test_mutex = tor_mutex_new();
  413. _thread_test_start1 = tor_mutex_new();
  414. _thread_test_start2 = tor_mutex_new();
  415. _thread_test_strmap = strmap_new();
  416. s1 = tor_strdup("thread 1");
  417. s2 = tor_strdup("thread 2");
  418. tor_mutex_acquire(_thread_test_start1);
  419. tor_mutex_acquire(_thread_test_start2);
  420. spawn_func(_thread_test_func, s1);
  421. spawn_func(_thread_test_func, s2);
  422. tor_mutex_release(_thread_test_start2);
  423. tor_mutex_release(_thread_test_start1);
  424. started = time(NULL);
  425. while (!done) {
  426. tor_mutex_acquire(_thread_test_mutex);
  427. strmap_assert_ok(_thread_test_strmap);
  428. if (strmap_get(_thread_test_strmap, "thread 1") &&
  429. strmap_get(_thread_test_strmap, "thread 2")) {
  430. done = 1;
  431. } else if (time(NULL) > started + 25) {
  432. timedout = done = 1;
  433. }
  434. tor_mutex_release(_thread_test_mutex);
  435. }
  436. tor_mutex_free(_thread_test_mutex);
  437. tor_mutex_acquire(_thread_test_start1);
  438. tor_mutex_release(_thread_test_start1);
  439. tor_mutex_acquire(_thread_test_start2);
  440. tor_mutex_release(_thread_test_start2);
  441. if (timedout) {
  442. printf("\nTimed out: %d %d", t1_count, t2_count);
  443. test_assert(strmap_get(_thread_test_strmap, "thread 1"));
  444. test_assert(strmap_get(_thread_test_strmap, "thread 2"));
  445. test_assert(!timedout);
  446. }
  447. /* different thread IDs. */
  448. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  449. strmap_get(_thread_test_strmap, "thread 2")));
  450. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  451. strmap_get(_thread_test_strmap, "last to run")) ||
  452. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  453. strmap_get(_thread_test_strmap, "last to run")));
  454. done:
  455. tor_free(s1);
  456. tor_free(s2);
  457. tor_free(_thread1_name);
  458. tor_free(_thread2_name);
  459. if (_thread_test_strmap)
  460. strmap_free(_thread_test_strmap, NULL);
  461. if (_thread_test_start1)
  462. tor_mutex_free(_thread_test_start1);
  463. if (_thread_test_start2)
  464. tor_mutex_free(_thread_test_start2);
  465. }
  466. /** Run unit tests for compression functions */
  467. static void
  468. test_util_gzip(void)
  469. {
  470. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  471. const char *ccp2;
  472. size_t len1, len2;
  473. tor_zlib_state_t *state = NULL;
  474. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  475. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  476. if (is_gzip_supported()) {
  477. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  478. GZIP_METHOD));
  479. test_assert(buf2);
  480. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  481. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  482. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  483. GZIP_METHOD, 1, LOG_INFO));
  484. test_assert(buf3);
  485. test_streq(buf1,buf3);
  486. tor_free(buf2);
  487. tor_free(buf3);
  488. }
  489. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  490. ZLIB_METHOD));
  491. test_assert(buf2);
  492. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  493. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  494. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  495. ZLIB_METHOD, 1, LOG_INFO));
  496. test_assert(buf3);
  497. test_streq(buf1,buf3);
  498. /* Check whether we can uncompress concatenated, compressed strings. */
  499. tor_free(buf3);
  500. buf2 = tor_realloc(buf2, len1*2);
  501. memcpy(buf2+len1, buf2, len1);
  502. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  503. ZLIB_METHOD, 1, LOG_INFO));
  504. test_eq(len2, (strlen(buf1)+1)*2);
  505. test_memeq(buf3,
  506. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  507. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  508. (strlen(buf1)+1)*2);
  509. tor_free(buf1);
  510. tor_free(buf2);
  511. tor_free(buf3);
  512. /* Check whether we can uncompress partial strings. */
  513. buf1 =
  514. tor_strdup("String with low redundancy that won't be compressed much.");
  515. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  516. ZLIB_METHOD));
  517. tor_assert(len1>16);
  518. /* when we allow an incomplete string, we should succeed.*/
  519. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  520. ZLIB_METHOD, 0, LOG_INFO));
  521. buf3[len2]='\0';
  522. tor_assert(len2 > 5);
  523. tor_assert(!strcmpstart(buf1, buf3));
  524. /* when we demand a complete string, this must fail. */
  525. tor_free(buf3);
  526. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  527. ZLIB_METHOD, 1, LOG_INFO));
  528. tor_assert(!buf3);
  529. /* Now, try streaming compression. */
  530. tor_free(buf1);
  531. tor_free(buf2);
  532. tor_free(buf3);
  533. state = tor_zlib_new(1, ZLIB_METHOD);
  534. tor_assert(state);
  535. cp1 = buf1 = tor_malloc(1024);
  536. len1 = 1024;
  537. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  538. len2 = 21;
  539. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  540. == TOR_ZLIB_OK);
  541. test_eq(len2, 0); /* Make sure we compressed it all. */
  542. test_assert(cp1 > buf1);
  543. len2 = 0;
  544. cp2 = cp1;
  545. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  546. == TOR_ZLIB_DONE);
  547. test_eq(len2, 0);
  548. test_assert(cp1 > cp2); /* Make sure we really added something. */
  549. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  550. ZLIB_METHOD, 1, LOG_WARN));
  551. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  552. done:
  553. if (state)
  554. tor_zlib_free(state);
  555. tor_free(buf2);
  556. tor_free(buf3);
  557. tor_free(buf1);
  558. }
  559. /** Run unit tests for mmap() wrapper functionality. */
  560. static void
  561. test_util_mmap(void)
  562. {
  563. char *fname1 = tor_strdup(get_fname("mapped_1"));
  564. char *fname2 = tor_strdup(get_fname("mapped_2"));
  565. char *fname3 = tor_strdup(get_fname("mapped_3"));
  566. const size_t buflen = 17000;
  567. char *buf = tor_malloc(17000);
  568. tor_mmap_t *mapping = NULL;
  569. crypto_rand(buf, buflen);
  570. mapping = tor_mmap_file(fname1);
  571. test_assert(! mapping);
  572. write_str_to_file(fname1, "Short file.", 1);
  573. write_bytes_to_file(fname2, buf, buflen, 1);
  574. write_bytes_to_file(fname3, buf, 16384, 1);
  575. mapping = tor_mmap_file(fname1);
  576. test_assert(mapping);
  577. test_eq(mapping->size, strlen("Short file."));
  578. test_streq(mapping->data, "Short file.");
  579. #ifdef MS_WINDOWS
  580. tor_munmap_file(mapping);
  581. mapping = NULL;
  582. test_assert(unlink(fname1) == 0);
  583. #else
  584. /* make sure we can unlink. */
  585. test_assert(unlink(fname1) == 0);
  586. test_streq(mapping->data, "Short file.");
  587. tor_munmap_file(mapping);
  588. mapping = NULL;
  589. #endif
  590. /* Now a zero-length file. */
  591. write_str_to_file(fname1, "", 1);
  592. mapping = tor_mmap_file(fname1);
  593. test_eq(mapping, NULL);
  594. test_eq(ERANGE, errno);
  595. unlink(fname1);
  596. /* Make sure that we fail to map a no-longer-existent file. */
  597. mapping = tor_mmap_file(fname1);
  598. test_assert(mapping == NULL);
  599. /* Now try a big file that stretches across a few pages and isn't aligned */
  600. mapping = tor_mmap_file(fname2);
  601. test_assert(mapping);
  602. test_eq(mapping->size, buflen);
  603. test_memeq(mapping->data, buf, buflen);
  604. tor_munmap_file(mapping);
  605. mapping = NULL;
  606. /* Now try a big aligned file. */
  607. mapping = tor_mmap_file(fname3);
  608. test_assert(mapping);
  609. test_eq(mapping->size, 16384);
  610. test_memeq(mapping->data, buf, 16384);
  611. tor_munmap_file(mapping);
  612. mapping = NULL;
  613. done:
  614. unlink(fname1);
  615. unlink(fname2);
  616. unlink(fname3);
  617. tor_free(fname1);
  618. tor_free(fname2);
  619. tor_free(fname3);
  620. tor_free(buf);
  621. if (mapping)
  622. tor_munmap_file(mapping);
  623. }
  624. /** Run unit tests for escaping/unescaping data for use by controllers. */
  625. static void
  626. test_util_control_formats(void)
  627. {
  628. char *out = NULL;
  629. const char *inp =
  630. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  631. size_t sz;
  632. sz = read_escaped_data(inp, strlen(inp), &out);
  633. test_streq(out,
  634. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  635. test_eq(sz, strlen(out));
  636. done:
  637. tor_free(out);
  638. }
  639. static void
  640. test_util_sscanf(void)
  641. {
  642. unsigned u1, u2, u3;
  643. char s1[10], s2[10], s3[10], ch;
  644. int r;
  645. r = tor_sscanf("hello world", "hello world"); /* String match: success */
  646. test_eq(r, 0);
  647. r = tor_sscanf("hello world 3", "hello worlb %u", &u1); /* String fail */
  648. test_eq(r, 0);
  649. r = tor_sscanf("12345", "%u", &u1); /* Simple number */
  650. test_eq(r, 1);
  651. test_eq(u1, 12345u);
  652. r = tor_sscanf("", "%u", &u1); /* absent number */
  653. test_eq(r, 0);
  654. r = tor_sscanf("A", "%u", &u1); /* bogus number */
  655. test_eq(r, 0);
  656. r = tor_sscanf("4294967295", "%u", &u1); /* UINT32_MAX should work. */
  657. test_eq(r, 1);
  658. test_eq(u1, 4294967295u);
  659. r = tor_sscanf("4294967296", "%u", &u1); /* Always say -1 at 32 bits. */
  660. test_eq(r, 0);
  661. r = tor_sscanf("123456", "%2u%u", &u1, &u2); /* Width */
  662. test_eq(r, 2);
  663. test_eq(u1, 12u);
  664. test_eq(u2, 3456u);
  665. r = tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3); /* separators */
  666. test_eq(r, 3);
  667. test_eq(u1, 12u);
  668. test_eq(u2, 3u);
  669. test_eq(u3, 456u);
  670. r = tor_sscanf("12:3:045", "%2u:%2u:%3u", &u1, &u2, &u3); /* 0s */
  671. test_eq(r, 3);
  672. test_eq(u1, 12u);
  673. test_eq(u2, 3u);
  674. test_eq(u3, 45u);
  675. /* %u does not match space.*/
  676. r = tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3);
  677. test_eq(r, 2);
  678. /* %u does not match negative numbers. */
  679. r = tor_sscanf("12:3:-4", "%2u:%2u:%3u", &u1, &u2, &u3);
  680. test_eq(r, 2);
  681. /* Arbitrary amounts of 0-padding are okay */
  682. r = tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3);
  683. test_eq(r, 3);
  684. test_eq(u1, 12u);
  685. test_eq(u2, 3u);
  686. test_eq(u3, 99u);
  687. r = tor_sscanf("99% fresh", "%3u%% fresh", &u1); /* percents are scannable.*/
  688. test_eq(r, 1);
  689. test_eq(u1, 99);
  690. r = tor_sscanf("hello", "%s", s1); /* %s needs a number. */
  691. test_eq(r, -1);
  692. r = tor_sscanf("hello", "%3s%7s", s1, s2); /* %s matches characters. */
  693. test_eq(r, 2);
  694. test_streq(s1, "hel");
  695. test_streq(s2, "lo");
  696. r = tor_sscanf("WD40", "%2s%u", s3, &u1); /* %s%u */
  697. test_eq(r, 2);
  698. test_streq(s3, "WD");
  699. test_eq(u1, 40);
  700. r = tor_sscanf("76trombones", "%6u%9s", &u1, s1); /* %u%s */
  701. test_eq(r, 2);
  702. test_eq(u1, 76);
  703. test_streq(s1, "trombones");
  704. r = tor_sscanf("hello world", "%9s %9s", s1, s2); /* %s doesn't eat space. */
  705. test_eq(r, 2);
  706. test_streq(s1, "hello");
  707. test_streq(s2, "world");
  708. r = tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3); /* %s can be empty. */
  709. test_eq(r, 3);
  710. test_streq(s1, "hi");
  711. test_streq(s2, "");
  712. test_streq(s3, "");
  713. r = tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  714. test_eq(r, 3);
  715. r = tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  716. test_eq(r, 4);
  717. done:
  718. ;
  719. }
  720. /** Run unittests for memory pool allocator */
  721. static void
  722. test_util_mempool(void)
  723. {
  724. mp_pool_t *pool = NULL;
  725. smartlist_t *allocated = NULL;
  726. int i;
  727. pool = mp_pool_new(1, 100);
  728. test_assert(pool);
  729. test_assert(pool->new_chunk_capacity >= 100);
  730. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  731. mp_pool_destroy(pool);
  732. pool = NULL;
  733. pool = mp_pool_new(241, 2500);
  734. test_assert(pool);
  735. test_assert(pool->new_chunk_capacity >= 10);
  736. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  737. test_eq(pool->item_alloc_size & 0x03, 0);
  738. test_assert(pool->new_chunk_capacity < 60);
  739. allocated = smartlist_create();
  740. for (i = 0; i < 20000; ++i) {
  741. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  742. void *m = mp_pool_get(pool);
  743. memset(m, 0x09, 241);
  744. smartlist_add(allocated, m);
  745. //printf("%d: %p\n", i, m);
  746. //mp_pool_assert_ok(pool);
  747. } else {
  748. int idx = crypto_rand_int(smartlist_len(allocated));
  749. void *m = smartlist_get(allocated, idx);
  750. //printf("%d: free %p\n", i, m);
  751. smartlist_del(allocated, idx);
  752. mp_pool_release(m);
  753. //mp_pool_assert_ok(pool);
  754. }
  755. if (crypto_rand_int(777)==0)
  756. mp_pool_clean(pool, 1, 1);
  757. if (i % 777)
  758. mp_pool_assert_ok(pool);
  759. }
  760. done:
  761. if (allocated) {
  762. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  763. mp_pool_assert_ok(pool);
  764. mp_pool_clean(pool, 0, 0);
  765. mp_pool_assert_ok(pool);
  766. smartlist_free(allocated);
  767. }
  768. if (pool)
  769. mp_pool_destroy(pool);
  770. }
  771. /** Run unittests for memory area allocator */
  772. static void
  773. test_util_memarea(void)
  774. {
  775. memarea_t *area = memarea_new();
  776. char *p1, *p2, *p3, *p1_orig;
  777. void *malloced_ptr = NULL;
  778. int i;
  779. test_assert(area);
  780. p1_orig = p1 = memarea_alloc(area,64);
  781. p2 = memarea_alloc_zero(area,52);
  782. p3 = memarea_alloc(area,11);
  783. test_assert(memarea_owns_ptr(area, p1));
  784. test_assert(memarea_owns_ptr(area, p2));
  785. test_assert(memarea_owns_ptr(area, p3));
  786. /* Make sure we left enough space. */
  787. test_assert(p1+64 <= p2);
  788. test_assert(p2+52 <= p3);
  789. /* Make sure we aligned. */
  790. test_eq(((uintptr_t)p1) % sizeof(void*), 0);
  791. test_eq(((uintptr_t)p2) % sizeof(void*), 0);
  792. test_eq(((uintptr_t)p3) % sizeof(void*), 0);
  793. test_assert(!memarea_owns_ptr(area, p3+8192));
  794. test_assert(!memarea_owns_ptr(area, p3+30));
  795. test_assert(tor_mem_is_zero(p2, 52));
  796. /* Make sure we don't overalign. */
  797. p1 = memarea_alloc(area, 1);
  798. p2 = memarea_alloc(area, 1);
  799. test_eq(p1+sizeof(void*), p2);
  800. {
  801. malloced_ptr = tor_malloc(64);
  802. test_assert(!memarea_owns_ptr(area, malloced_ptr));
  803. tor_free(malloced_ptr);
  804. }
  805. /* memarea_memdup */
  806. {
  807. malloced_ptr = tor_malloc(64);
  808. crypto_rand((char*)malloced_ptr, 64);
  809. p1 = memarea_memdup(area, malloced_ptr, 64);
  810. test_assert(p1 != malloced_ptr);
  811. test_memeq(p1, malloced_ptr, 64);
  812. tor_free(malloced_ptr);
  813. }
  814. /* memarea_strdup. */
  815. p1 = memarea_strdup(area,"");
  816. p2 = memarea_strdup(area, "abcd");
  817. test_assert(p1);
  818. test_assert(p2);
  819. test_streq(p1, "");
  820. test_streq(p2, "abcd");
  821. /* memarea_strndup. */
  822. {
  823. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  824. /* (From Turandot, act 3.) */
  825. size_t len = strlen(s);
  826. p1 = memarea_strndup(area, s, 1000);
  827. p2 = memarea_strndup(area, s, 10);
  828. test_streq(p1, s);
  829. test_assert(p2 >= p1 + len + 1);
  830. test_memeq(s, p2, 10);
  831. test_eq(p2[10], '\0');
  832. p3 = memarea_strndup(area, s, len);
  833. test_streq(p3, s);
  834. p3 = memarea_strndup(area, s, len-1);
  835. test_memeq(s, p3, len-1);
  836. test_eq(p3[len-1], '\0');
  837. }
  838. memarea_clear(area);
  839. p1 = memarea_alloc(area, 1);
  840. test_eq(p1, p1_orig);
  841. memarea_clear(area);
  842. /* Check for running over an area's size. */
  843. for (i = 0; i < 512; ++i) {
  844. p1 = memarea_alloc(area, crypto_rand_int(5)+1);
  845. test_assert(memarea_owns_ptr(area, p1));
  846. }
  847. memarea_assert_ok(area);
  848. /* Make sure we can allocate a too-big object. */
  849. p1 = memarea_alloc_zero(area, 9000);
  850. p2 = memarea_alloc_zero(area, 16);
  851. test_assert(memarea_owns_ptr(area, p1));
  852. test_assert(memarea_owns_ptr(area, p2));
  853. done:
  854. memarea_drop_all(area);
  855. tor_free(malloced_ptr);
  856. }
  857. /** Run unit tests for utility functions to get file names relative to
  858. * the data directory. */
  859. static void
  860. test_util_datadir(void)
  861. {
  862. char buf[1024];
  863. char *f = NULL;
  864. char *temp_dir = NULL;
  865. temp_dir = get_datadir_fname(NULL);
  866. f = get_datadir_fname("state");
  867. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  868. test_streq(f, buf);
  869. tor_free(f);
  870. f = get_datadir_fname2("cache", "thingy");
  871. tor_snprintf(buf, sizeof(buf),
  872. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  873. test_streq(f, buf);
  874. tor_free(f);
  875. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  876. tor_snprintf(buf, sizeof(buf),
  877. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  878. test_streq(f, buf);
  879. tor_free(f);
  880. f = get_datadir_fname_suffix("cache", ".foo");
  881. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  882. temp_dir);
  883. test_streq(f, buf);
  884. done:
  885. tor_free(f);
  886. tor_free(temp_dir);
  887. }
  888. static void
  889. test_util_strtok(void)
  890. {
  891. char buf[128];
  892. char buf2[128];
  893. char *cp1, *cp2;
  894. strlcpy(buf, "Graved on the dark in gestures of descent", sizeof(buf));
  895. strlcpy(buf2, "they.seemed;their!own;most.perfect;monument", sizeof(buf2));
  896. /* -- "Year's End", Richard Wilbur */
  897. test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
  898. test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  899. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  900. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  901. test_streq("on", S1());
  902. test_streq("the", S1());
  903. test_streq("dark", S1());
  904. test_streq("seemed", S2());
  905. test_streq("their", S2());
  906. test_streq("own", S2());
  907. test_streq("in", S1());
  908. test_streq("gestures", S1());
  909. test_streq("of", S1());
  910. test_streq("most", S2());
  911. test_streq("perfect", S2());
  912. test_streq("descent", S1());
  913. test_streq("monument", S2());
  914. test_assert(NULL == S1());
  915. test_assert(NULL == S2());
  916. done:
  917. ;
  918. }
  919. #define UTIL_LEGACY(name) \
  920. { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
  921. struct testcase_t util_tests[] = {
  922. UTIL_LEGACY(misc),
  923. UTIL_LEGACY(gzip),
  924. UTIL_LEGACY(datadir),
  925. UTIL_LEGACY(mempool),
  926. UTIL_LEGACY(memarea),
  927. UTIL_LEGACY(control_formats),
  928. UTIL_LEGACY(mmap),
  929. UTIL_LEGACY(threads),
  930. UTIL_LEGACY(sscanf),
  931. UTIL_LEGACY(strtok),
  932. END_OF_TESTCASES
  933. };