test_util.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, 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 "config.h"
  10. #include "control.h"
  11. #include "test.h"
  12. #include "mempool.h"
  13. #include "memarea.h"
  14. static void
  15. test_util_time(void)
  16. {
  17. struct timeval start, end;
  18. struct tm a_time;
  19. char timestr[RFC1123_TIME_LEN+1];
  20. time_t t_res;
  21. int i;
  22. start.tv_sec = 5;
  23. start.tv_usec = 5000;
  24. end.tv_sec = 5;
  25. end.tv_usec = 5000;
  26. test_eq(0L, tv_udiff(&start, &end));
  27. end.tv_usec = 7000;
  28. test_eq(2000L, tv_udiff(&start, &end));
  29. end.tv_sec = 6;
  30. test_eq(1002000L, tv_udiff(&start, &end));
  31. end.tv_usec = 0;
  32. test_eq(995000L, tv_udiff(&start, &end));
  33. end.tv_sec = 4;
  34. test_eq(-1005000L, tv_udiff(&start, &end));
  35. end.tv_usec = 999990;
  36. start.tv_sec = 1;
  37. start.tv_usec = 500;
  38. /* The test values here are confirmed to be correct on a platform
  39. * with a working timegm. */
  40. a_time.tm_year = 2003-1900;
  41. a_time.tm_mon = 7;
  42. a_time.tm_mday = 30;
  43. a_time.tm_hour = 6;
  44. a_time.tm_min = 14;
  45. a_time.tm_sec = 55;
  46. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  47. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  48. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  49. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  50. a_time.tm_mday = 10;
  51. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  52. format_rfc1123_time(timestr, 0);
  53. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  54. format_rfc1123_time(timestr, (time_t)1091580502UL);
  55. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  56. t_res = 0;
  57. i = parse_rfc1123_time(timestr, &t_res);
  58. test_eq(i,0);
  59. test_eq(t_res, (time_t)1091580502UL);
  60. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  61. tor_gettimeofday(&start);
  62. /* now make sure time works. */
  63. tor_gettimeofday(&end);
  64. /* We might've timewarped a little. */
  65. tt_int_op(tv_udiff(&start, &end), >=, -5000);
  66. done:
  67. ;
  68. }
  69. static void
  70. test_util_parse_http_time(void *arg)
  71. {
  72. struct tm a_time;
  73. char b[ISO_TIME_LEN+1];
  74. (void)arg;
  75. #define T(s) do { \
  76. format_iso_time(b, tor_timegm(&a_time)); \
  77. tt_str_op(b, ==, (s)); \
  78. b[0]='\0'; \
  79. } while (0)
  80. /* Test parse_http_time */
  81. test_eq(-1, parse_http_time("", &a_time));
  82. test_eq(-1, parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
  83. test_eq(-1, parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
  84. test_eq(-1, parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
  85. test_eq(-1, parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
  86. test_eq(-1, parse_http_time("Sunday, August the third", &a_time));
  87. test_eq(-1, parse_http_time("Wednesday,,04 Aug 1994 00:48:22 GMT", &a_time));
  88. test_eq(0, parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
  89. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  90. T("1994-08-04 00:48:22");
  91. test_eq(0, parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
  92. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  93. T("1994-08-04 00:48:22");
  94. test_eq(0, parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
  95. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  96. T("1994-08-04 00:48:22");
  97. test_eq(0, parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
  98. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  99. T("1994-08-04 00:48:22");
  100. test_eq(0, parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
  101. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  102. T("1994-08-04 00:48:22");
  103. test_eq(0, parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
  104. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  105. T("1994-08-04 00:48:22");
  106. test_eq(0, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
  107. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  108. T("1994-08-04 00:48:22");
  109. test_eq(0, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
  110. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  111. T("1994-08-04 00:48:22");
  112. test_eq(0, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
  113. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  114. T("1994-08-04 00:48:22");
  115. test_eq(0, parse_http_time("Sun, 1 Jan 2012 00:00:00 GMT", &a_time));
  116. test_eq((time_t)1325376000UL, tor_timegm(&a_time));
  117. T("2012-01-01 00:00:00");
  118. test_eq(0, parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
  119. test_eq((time_t)1356912000UL, tor_timegm(&a_time));
  120. T("2012-12-31 00:00:00");
  121. test_eq(-1, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
  122. test_eq(-1, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
  123. test_eq(-1, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
  124. test_eq(-1, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
  125. test_eq(-1, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
  126. test_eq(-1, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
  127. test_eq(-1, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
  128. test_eq(-1, parse_http_time("2011-03-30 23:59", &a_time));
  129. #undef T
  130. done:
  131. ;
  132. }
  133. static void
  134. test_util_config_line(void)
  135. {
  136. char buf[1024];
  137. char *k=NULL, *v=NULL;
  138. const char *str;
  139. /* Test parse_config_line_from_str */
  140. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  141. "k2\n"
  142. "k3 \n" "\n" " \n" "#comment\n"
  143. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  144. "kseven \"a quoted 'string\"\n"
  145. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  146. "k9 a line that\\\n spans two lines.\n\n"
  147. "k10 more than\\\n one contin\\\nuation\n"
  148. "k11 \\\ncontinuation at the start\n"
  149. "k12 line with a\\\n#comment\n embedded\n"
  150. "k13\\\ncontinuation at the very start\n"
  151. "k14 a line that has a comment and # ends with a slash \\\n"
  152. "k15 this should be the next new line\n"
  153. "k16 a line that has a comment and # ends without a slash \n"
  154. "k17 this should be the next new line\n"
  155. , sizeof(buf));
  156. str = buf;
  157. str = parse_config_line_from_str(str, &k, &v);
  158. test_streq(k, "k");
  159. test_streq(v, "v");
  160. tor_free(k); tor_free(v);
  161. test_assert(!strcmpstart(str, "key value with"));
  162. str = parse_config_line_from_str(str, &k, &v);
  163. test_streq(k, "key");
  164. test_streq(v, "value with spaces");
  165. tor_free(k); tor_free(v);
  166. test_assert(!strcmpstart(str, "keykey"));
  167. str = parse_config_line_from_str(str, &k, &v);
  168. test_streq(k, "keykey");
  169. test_streq(v, "val");
  170. tor_free(k); tor_free(v);
  171. test_assert(!strcmpstart(str, "k2\n"));
  172. str = parse_config_line_from_str(str, &k, &v);
  173. test_streq(k, "k2");
  174. test_streq(v, "");
  175. tor_free(k); tor_free(v);
  176. test_assert(!strcmpstart(str, "k3 \n"));
  177. str = parse_config_line_from_str(str, &k, &v);
  178. test_streq(k, "k3");
  179. test_streq(v, "");
  180. tor_free(k); tor_free(v);
  181. test_assert(!strcmpstart(str, "#comment"));
  182. str = parse_config_line_from_str(str, &k, &v);
  183. test_streq(k, "k4");
  184. test_streq(v, "");
  185. tor_free(k); tor_free(v);
  186. test_assert(!strcmpstart(str, "k5#abc"));
  187. str = parse_config_line_from_str(str, &k, &v);
  188. test_streq(k, "k5");
  189. test_streq(v, "");
  190. tor_free(k); tor_free(v);
  191. test_assert(!strcmpstart(str, "k6"));
  192. str = parse_config_line_from_str(str, &k, &v);
  193. test_streq(k, "k6");
  194. test_streq(v, "val");
  195. tor_free(k); tor_free(v);
  196. test_assert(!strcmpstart(str, "kseven"));
  197. str = parse_config_line_from_str(str, &k, &v);
  198. test_streq(k, "kseven");
  199. test_streq(v, "a quoted \'string");
  200. tor_free(k); tor_free(v);
  201. test_assert(!strcmpstart(str, "k8 "));
  202. str = parse_config_line_from_str(str, &k, &v);
  203. test_streq(k, "k8");
  204. test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  205. tor_free(k); tor_free(v);
  206. str = parse_config_line_from_str(str, &k, &v);
  207. test_streq(k, "k9");
  208. test_streq(v, "a line that spans two lines.");
  209. tor_free(k); tor_free(v);
  210. str = parse_config_line_from_str(str, &k, &v);
  211. test_streq(k, "k10");
  212. test_streq(v, "more than one continuation");
  213. tor_free(k); tor_free(v);
  214. str = parse_config_line_from_str(str, &k, &v);
  215. test_streq(k, "k11");
  216. test_streq(v, "continuation at the start");
  217. tor_free(k); tor_free(v);
  218. str = parse_config_line_from_str(str, &k, &v);
  219. test_streq(k, "k12");
  220. test_streq(v, "line with a embedded");
  221. tor_free(k); tor_free(v);
  222. str = parse_config_line_from_str(str, &k, &v);
  223. test_streq(k, "k13");
  224. test_streq(v, "continuation at the very start");
  225. tor_free(k); tor_free(v);
  226. str = parse_config_line_from_str(str, &k, &v);
  227. test_streq(k, "k14");
  228. test_streq(v, "a line that has a comment and" );
  229. tor_free(k); tor_free(v);
  230. str = parse_config_line_from_str(str, &k, &v);
  231. test_streq(k, "k15");
  232. test_streq(v, "this should be the next new line");
  233. tor_free(k); tor_free(v);
  234. str = parse_config_line_from_str(str, &k, &v);
  235. test_streq(k, "k16");
  236. test_streq(v, "a line that has a comment and" );
  237. tor_free(k); tor_free(v);
  238. str = parse_config_line_from_str(str, &k, &v);
  239. test_streq(k, "k17");
  240. test_streq(v, "this should be the next new line");
  241. tor_free(k); tor_free(v);
  242. test_streq(str, "");
  243. done:
  244. tor_free(k);
  245. tor_free(v);
  246. }
  247. /** Test basic string functionality. */
  248. static void
  249. test_util_strmisc(void)
  250. {
  251. char buf[1024];
  252. int i;
  253. char *cp;
  254. /* Tests for corner cases of strl operations */
  255. test_eq(5, strlcpy(buf, "Hello", 0));
  256. strlcpy(buf, "Hello", sizeof(buf));
  257. test_eq(10, strlcat(buf, "Hello", 5));
  258. /* Test tor_strstrip() */
  259. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  260. tor_strstrip(buf, ",!");
  261. test_streq(buf, "Testing 1 2 3");
  262. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  263. tor_strstrip(buf, "!? ");
  264. test_streq(buf, "Testing123");
  265. /* Test tor_parse_long. */
  266. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  267. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  268. test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
  269. /* Test tor_parse_ulong */
  270. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  271. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  272. /* Test tor_parse_uint64. */
  273. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  274. test_assert(i == 1);
  275. test_streq(cp, " x");
  276. test_assert(U64_LITERAL(12345678901) ==
  277. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  278. test_assert(i == 1);
  279. test_streq(cp, "");
  280. test_assert(U64_LITERAL(0) ==
  281. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  282. test_assert(i == 0);
  283. {
  284. /* Test tor_parse_double. */
  285. double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
  286. test_assert(i == 1);
  287. test_assert(DBL_TO_U64(d) == 10);
  288. d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
  289. test_assert(i == 1);
  290. test_assert(DBL_TO_U64(d) == 0);
  291. d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
  292. test_assert(i == 0);
  293. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
  294. test_assert(i == 0);
  295. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
  296. test_assert(i == 1);
  297. d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
  298. test_assert(i == 1);
  299. }
  300. {
  301. /* Test tor_parse_* where we overflow/underflow the underlying type. */
  302. /* This string should overflow 64-bit ints. */
  303. #define TOOBIG "100000000000000000000000000"
  304. test_eq(0L, tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  305. test_eq(i, 0);
  306. test_eq(0L, tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  307. test_eq(i, 0);
  308. test_eq(0UL, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
  309. test_eq(i, 0);
  310. test_eq(U64_LITERAL(0), tor_parse_uint64(TOOBIG, 10,
  311. 0, UINT64_MAX, &i, NULL));
  312. test_eq(i, 0);
  313. }
  314. /* Test failing snprintf cases */
  315. test_eq(-1, tor_snprintf(buf, 0, "Foo"));
  316. test_eq(-1, tor_snprintf(buf, 2, "Foo"));
  317. /* Test printf with uint64 */
  318. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  319. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  320. test_streq(buf, "x!12345678901!x");
  321. /* Test for strcmpstart and strcmpend. */
  322. test_assert(strcmpstart("abcdef", "abcdef")==0);
  323. test_assert(strcmpstart("abcdef", "abc")==0);
  324. test_assert(strcmpstart("abcdef", "abd")<0);
  325. test_assert(strcmpstart("abcdef", "abb")>0);
  326. test_assert(strcmpstart("ab", "abb")<0);
  327. test_assert(strcmpend("abcdef", "abcdef")==0);
  328. test_assert(strcmpend("abcdef", "def")==0);
  329. test_assert(strcmpend("abcdef", "deg")<0);
  330. test_assert(strcmpend("abcdef", "dee")>0);
  331. test_assert(strcmpend("ab", "abb")<0);
  332. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  333. test_assert(strcasecmpend("abcdef", "dEF")==0);
  334. test_assert(strcasecmpend("abcDEf", "deg")<0);
  335. test_assert(strcasecmpend("abcdef", "DEE")>0);
  336. test_assert(strcasecmpend("ab", "abB")<0);
  337. /* Test mem_is_zero */
  338. memset(buf,0,128);
  339. buf[128] = 'x';
  340. test_assert(tor_digest_is_zero(buf));
  341. test_assert(tor_mem_is_zero(buf, 10));
  342. test_assert(tor_mem_is_zero(buf, 20));
  343. test_assert(tor_mem_is_zero(buf, 128));
  344. test_assert(!tor_mem_is_zero(buf, 129));
  345. buf[60] = (char)255;
  346. test_assert(!tor_mem_is_zero(buf, 128));
  347. buf[0] = (char)1;
  348. test_assert(!tor_mem_is_zero(buf, 10));
  349. /* Test 'escaped' */
  350. test_streq("\"\"", escaped(""));
  351. test_streq("\"abcd\"", escaped("abcd"));
  352. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  353. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  354. test_assert(NULL == escaped(NULL));
  355. /* Test strndup and memdup */
  356. {
  357. const char *s = "abcdefghijklmnopqrstuvwxyz";
  358. cp = tor_strndup(s, 30);
  359. test_streq(cp, s); /* same string, */
  360. test_neq(cp, s); /* but different pointers. */
  361. tor_free(cp);
  362. cp = tor_strndup(s, 5);
  363. test_streq(cp, "abcde");
  364. tor_free(cp);
  365. s = "a\0b\0c\0d\0e\0";
  366. cp = tor_memdup(s,10);
  367. test_memeq(cp, s, 10); /* same ram, */
  368. test_neq(cp, s); /* but different pointers. */
  369. tor_free(cp);
  370. }
  371. /* Test str-foo functions */
  372. cp = tor_strdup("abcdef");
  373. test_assert(tor_strisnonupper(cp));
  374. cp[3] = 'D';
  375. test_assert(!tor_strisnonupper(cp));
  376. tor_strupper(cp);
  377. test_streq(cp, "ABCDEF");
  378. test_assert(tor_strisprint(cp));
  379. cp[3] = 3;
  380. test_assert(!tor_strisprint(cp));
  381. tor_free(cp);
  382. /* Test eat_whitespace. */
  383. {
  384. const char *s = " \n a";
  385. test_eq_ptr(eat_whitespace(s), s+4);
  386. s = "abcd";
  387. test_eq_ptr(eat_whitespace(s), s);
  388. s = "#xyz\nab";
  389. test_eq_ptr(eat_whitespace(s), s+5);
  390. }
  391. /* Test memmem and memstr */
  392. {
  393. const char *haystack = "abcde";
  394. tor_assert(!tor_memmem(haystack, 5, "ef", 2));
  395. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  396. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  397. haystack = "ababcad";
  398. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  399. test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
  400. test_assert(!tor_memstr(haystack, 7, "fe"));
  401. test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
  402. }
  403. /* Test wrap_string */
  404. {
  405. smartlist_t *sl = smartlist_create();
  406. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  407. 10, "", "");
  408. cp = smartlist_join_strings(sl, "", 0, NULL);
  409. test_streq(cp,
  410. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
  411. tor_free(cp);
  412. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  413. smartlist_clear(sl);
  414. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  415. 16, "### ", "# ");
  416. cp = smartlist_join_strings(sl, "", 0, NULL);
  417. test_streq(cp,
  418. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  419. "# woot.\n");
  420. tor_free(cp);
  421. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  422. smartlist_free(sl);
  423. }
  424. done:
  425. ;
  426. }
  427. static void
  428. test_util_pow2(void)
  429. {
  430. /* Test tor_log2(). */
  431. test_eq(tor_log2(64), 6);
  432. test_eq(tor_log2(65), 6);
  433. test_eq(tor_log2(63), 5);
  434. test_eq(tor_log2(1), 0);
  435. test_eq(tor_log2(2), 1);
  436. test_eq(tor_log2(3), 1);
  437. test_eq(tor_log2(4), 2);
  438. test_eq(tor_log2(5), 2);
  439. test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
  440. test_eq(tor_log2(UINT64_MAX), 63);
  441. /* Test round_to_power_of_2 */
  442. test_eq(round_to_power_of_2(120), 128);
  443. test_eq(round_to_power_of_2(128), 128);
  444. test_eq(round_to_power_of_2(130), 128);
  445. test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
  446. U64_LITERAL(1)<<55);
  447. test_eq(round_to_power_of_2(0), 2);
  448. done:
  449. ;
  450. }
  451. /** mutex for thread test to stop the threads hitting data at the same time. */
  452. static tor_mutex_t *_thread_test_mutex = NULL;
  453. /** mutexes for the thread test to make sure that the threads have to
  454. * interleave somewhat. */
  455. static tor_mutex_t *_thread_test_start1 = NULL,
  456. *_thread_test_start2 = NULL;
  457. /** Shared strmap for the thread test. */
  458. static strmap_t *_thread_test_strmap = NULL;
  459. /** The name of thread1 for the thread test */
  460. static char *_thread1_name = NULL;
  461. /** The name of thread2 for the thread test */
  462. static char *_thread2_name = NULL;
  463. static void _thread_test_func(void* _s) ATTR_NORETURN;
  464. /** How many iterations have the threads in the unit test run? */
  465. static int t1_count = 0, t2_count = 0;
  466. /** Helper function for threading unit tests: This function runs in a
  467. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  468. * should start, then it repeatedly alters _test_thread_strmap protected by
  469. * _thread_test_mutex. */
  470. static void
  471. _thread_test_func(void* _s)
  472. {
  473. char *s = _s;
  474. int i, *count;
  475. tor_mutex_t *m;
  476. char buf[64];
  477. char **cp;
  478. if (!strcmp(s, "thread 1")) {
  479. m = _thread_test_start1;
  480. cp = &_thread1_name;
  481. count = &t1_count;
  482. } else {
  483. m = _thread_test_start2;
  484. cp = &_thread2_name;
  485. count = &t2_count;
  486. }
  487. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  488. *cp = tor_strdup(buf);
  489. tor_mutex_acquire(m);
  490. for (i=0; i<10000; ++i) {
  491. tor_mutex_acquire(_thread_test_mutex);
  492. strmap_set(_thread_test_strmap, "last to run", *cp);
  493. ++*count;
  494. tor_mutex_release(_thread_test_mutex);
  495. }
  496. tor_mutex_acquire(_thread_test_mutex);
  497. strmap_set(_thread_test_strmap, s, *cp);
  498. tor_mutex_release(_thread_test_mutex);
  499. tor_mutex_release(m);
  500. spawn_exit();
  501. }
  502. /** Run unit tests for threading logic. */
  503. static void
  504. test_util_threads(void)
  505. {
  506. char *s1 = NULL, *s2 = NULL;
  507. int done = 0, timedout = 0;
  508. time_t started;
  509. #ifndef MS_WINDOWS
  510. struct timeval tv;
  511. tv.tv_sec=0;
  512. tv.tv_usec=10;
  513. #endif
  514. #ifndef TOR_IS_MULTITHREADED
  515. /* Skip this test if we aren't threading. We should be threading most
  516. * everywhere by now. */
  517. if (1)
  518. return;
  519. #endif
  520. _thread_test_mutex = tor_mutex_new();
  521. _thread_test_start1 = tor_mutex_new();
  522. _thread_test_start2 = tor_mutex_new();
  523. _thread_test_strmap = strmap_new();
  524. s1 = tor_strdup("thread 1");
  525. s2 = tor_strdup("thread 2");
  526. tor_mutex_acquire(_thread_test_start1);
  527. tor_mutex_acquire(_thread_test_start2);
  528. spawn_func(_thread_test_func, s1);
  529. spawn_func(_thread_test_func, s2);
  530. tor_mutex_release(_thread_test_start2);
  531. tor_mutex_release(_thread_test_start1);
  532. started = time(NULL);
  533. while (!done) {
  534. tor_mutex_acquire(_thread_test_mutex);
  535. strmap_assert_ok(_thread_test_strmap);
  536. if (strmap_get(_thread_test_strmap, "thread 1") &&
  537. strmap_get(_thread_test_strmap, "thread 2")) {
  538. done = 1;
  539. } else if (time(NULL) > started + 25) {
  540. timedout = done = 1;
  541. }
  542. tor_mutex_release(_thread_test_mutex);
  543. #ifndef MS_WINDOWS
  544. /* Prevent the main thread from starving the worker threads. */
  545. select(0, NULL, NULL, NULL, &tv);
  546. #endif
  547. }
  548. tor_mutex_acquire(_thread_test_start1);
  549. tor_mutex_release(_thread_test_start1);
  550. tor_mutex_acquire(_thread_test_start2);
  551. tor_mutex_release(_thread_test_start2);
  552. tor_mutex_free(_thread_test_mutex);
  553. if (timedout) {
  554. printf("\nTimed out: %d %d", t1_count, t2_count);
  555. test_assert(strmap_get(_thread_test_strmap, "thread 1"));
  556. test_assert(strmap_get(_thread_test_strmap, "thread 2"));
  557. test_assert(!timedout);
  558. }
  559. /* different thread IDs. */
  560. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  561. strmap_get(_thread_test_strmap, "thread 2")));
  562. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  563. strmap_get(_thread_test_strmap, "last to run")) ||
  564. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  565. strmap_get(_thread_test_strmap, "last to run")));
  566. done:
  567. tor_free(s1);
  568. tor_free(s2);
  569. tor_free(_thread1_name);
  570. tor_free(_thread2_name);
  571. if (_thread_test_strmap)
  572. strmap_free(_thread_test_strmap, NULL);
  573. if (_thread_test_start1)
  574. tor_mutex_free(_thread_test_start1);
  575. if (_thread_test_start2)
  576. tor_mutex_free(_thread_test_start2);
  577. }
  578. /** Run unit tests for compression functions */
  579. static void
  580. test_util_gzip(void)
  581. {
  582. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  583. const char *ccp2;
  584. size_t len1, len2;
  585. tor_zlib_state_t *state = NULL;
  586. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  587. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  588. if (is_gzip_supported()) {
  589. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  590. GZIP_METHOD));
  591. test_assert(buf2);
  592. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  593. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  594. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  595. GZIP_METHOD, 1, LOG_INFO));
  596. test_assert(buf3);
  597. test_streq(buf1,buf3);
  598. tor_free(buf2);
  599. tor_free(buf3);
  600. }
  601. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  602. ZLIB_METHOD));
  603. test_assert(buf2);
  604. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  605. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  606. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  607. ZLIB_METHOD, 1, LOG_INFO));
  608. test_assert(buf3);
  609. test_streq(buf1,buf3);
  610. /* Check whether we can uncompress concatenated, compressed strings. */
  611. tor_free(buf3);
  612. buf2 = tor_realloc(buf2, len1*2);
  613. memcpy(buf2+len1, buf2, len1);
  614. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  615. ZLIB_METHOD, 1, LOG_INFO));
  616. test_eq(len2, (strlen(buf1)+1)*2);
  617. test_memeq(buf3,
  618. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  619. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  620. (strlen(buf1)+1)*2);
  621. tor_free(buf1);
  622. tor_free(buf2);
  623. tor_free(buf3);
  624. /* Check whether we can uncompress partial strings. */
  625. buf1 =
  626. tor_strdup("String with low redundancy that won't be compressed much.");
  627. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  628. ZLIB_METHOD));
  629. tor_assert(len1>16);
  630. /* when we allow an incomplete string, we should succeed.*/
  631. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  632. ZLIB_METHOD, 0, LOG_INFO));
  633. buf3[len2]='\0';
  634. tor_assert(len2 > 5);
  635. tor_assert(!strcmpstart(buf1, buf3));
  636. /* when we demand a complete string, this must fail. */
  637. tor_free(buf3);
  638. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  639. ZLIB_METHOD, 1, LOG_INFO));
  640. tor_assert(!buf3);
  641. /* Now, try streaming compression. */
  642. tor_free(buf1);
  643. tor_free(buf2);
  644. tor_free(buf3);
  645. state = tor_zlib_new(1, ZLIB_METHOD);
  646. tor_assert(state);
  647. cp1 = buf1 = tor_malloc(1024);
  648. len1 = 1024;
  649. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  650. len2 = 21;
  651. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  652. == TOR_ZLIB_OK);
  653. test_eq(len2, 0); /* Make sure we compressed it all. */
  654. test_assert(cp1 > buf1);
  655. len2 = 0;
  656. cp2 = cp1;
  657. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  658. == TOR_ZLIB_DONE);
  659. test_eq(len2, 0);
  660. test_assert(cp1 > cp2); /* Make sure we really added something. */
  661. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  662. ZLIB_METHOD, 1, LOG_WARN));
  663. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  664. done:
  665. if (state)
  666. tor_zlib_free(state);
  667. tor_free(buf2);
  668. tor_free(buf3);
  669. tor_free(buf1);
  670. }
  671. /** Run unit tests for mmap() wrapper functionality. */
  672. static void
  673. test_util_mmap(void)
  674. {
  675. char *fname1 = tor_strdup(get_fname("mapped_1"));
  676. char *fname2 = tor_strdup(get_fname("mapped_2"));
  677. char *fname3 = tor_strdup(get_fname("mapped_3"));
  678. const size_t buflen = 17000;
  679. char *buf = tor_malloc(17000);
  680. tor_mmap_t *mapping = NULL;
  681. crypto_rand(buf, buflen);
  682. mapping = tor_mmap_file(fname1);
  683. test_assert(! mapping);
  684. write_str_to_file(fname1, "Short file.", 1);
  685. write_bytes_to_file(fname2, buf, buflen, 1);
  686. write_bytes_to_file(fname3, buf, 16384, 1);
  687. mapping = tor_mmap_file(fname1);
  688. test_assert(mapping);
  689. test_eq(mapping->size, strlen("Short file."));
  690. test_streq(mapping->data, "Short file.");
  691. #ifdef MS_WINDOWS
  692. tor_munmap_file(mapping);
  693. mapping = NULL;
  694. test_assert(unlink(fname1) == 0);
  695. #else
  696. /* make sure we can unlink. */
  697. test_assert(unlink(fname1) == 0);
  698. test_streq(mapping->data, "Short file.");
  699. tor_munmap_file(mapping);
  700. mapping = NULL;
  701. #endif
  702. /* Now a zero-length file. */
  703. write_str_to_file(fname1, "", 1);
  704. mapping = tor_mmap_file(fname1);
  705. test_eq(mapping, NULL);
  706. test_eq(ERANGE, errno);
  707. unlink(fname1);
  708. /* Make sure that we fail to map a no-longer-existent file. */
  709. mapping = tor_mmap_file(fname1);
  710. test_assert(mapping == NULL);
  711. /* Now try a big file that stretches across a few pages and isn't aligned */
  712. mapping = tor_mmap_file(fname2);
  713. test_assert(mapping);
  714. test_eq(mapping->size, buflen);
  715. test_memeq(mapping->data, buf, buflen);
  716. tor_munmap_file(mapping);
  717. mapping = NULL;
  718. /* Now try a big aligned file. */
  719. mapping = tor_mmap_file(fname3);
  720. test_assert(mapping);
  721. test_eq(mapping->size, 16384);
  722. test_memeq(mapping->data, buf, 16384);
  723. tor_munmap_file(mapping);
  724. mapping = NULL;
  725. done:
  726. unlink(fname1);
  727. unlink(fname2);
  728. unlink(fname3);
  729. tor_free(fname1);
  730. tor_free(fname2);
  731. tor_free(fname3);
  732. tor_free(buf);
  733. if (mapping)
  734. tor_munmap_file(mapping);
  735. }
  736. /** Run unit tests for escaping/unescaping data for use by controllers. */
  737. static void
  738. test_util_control_formats(void)
  739. {
  740. char *out = NULL;
  741. const char *inp =
  742. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  743. size_t sz;
  744. sz = read_escaped_data(inp, strlen(inp), &out);
  745. test_streq(out,
  746. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  747. test_eq(sz, strlen(out));
  748. done:
  749. tor_free(out);
  750. }
  751. static void
  752. test_util_sscanf(void)
  753. {
  754. unsigned u1, u2, u3;
  755. char s1[10], s2[10], s3[10], ch;
  756. int r;
  757. r = tor_sscanf("hello world", "hello world"); /* String match: success */
  758. test_eq(r, 0);
  759. r = tor_sscanf("hello world 3", "hello worlb %u", &u1); /* String fail */
  760. test_eq(r, 0);
  761. r = tor_sscanf("12345", "%u", &u1); /* Simple number */
  762. test_eq(r, 1);
  763. test_eq(u1, 12345u);
  764. r = tor_sscanf("", "%u", &u1); /* absent number */
  765. test_eq(r, 0);
  766. r = tor_sscanf("A", "%u", &u1); /* bogus number */
  767. test_eq(r, 0);
  768. r = tor_sscanf("4294967295", "%u", &u1); /* UINT32_MAX should work. */
  769. test_eq(r, 1);
  770. test_eq(u1, 4294967295u);
  771. r = tor_sscanf("4294967296", "%u", &u1); /* Always say -1 at 32 bits. */
  772. test_eq(r, 0);
  773. r = tor_sscanf("123456", "%2u%u", &u1, &u2); /* Width */
  774. test_eq(r, 2);
  775. test_eq(u1, 12u);
  776. test_eq(u2, 3456u);
  777. r = tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3); /* separators */
  778. test_eq(r, 3);
  779. test_eq(u1, 12u);
  780. test_eq(u2, 3u);
  781. test_eq(u3, 456u);
  782. r = tor_sscanf("12:3:045", "%2u:%2u:%3u", &u1, &u2, &u3); /* 0s */
  783. test_eq(r, 3);
  784. test_eq(u1, 12u);
  785. test_eq(u2, 3u);
  786. test_eq(u3, 45u);
  787. /* %u does not match space.*/
  788. r = tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3);
  789. test_eq(r, 2);
  790. /* %u does not match negative numbers. */
  791. r = tor_sscanf("12:3:-4", "%2u:%2u:%3u", &u1, &u2, &u3);
  792. test_eq(r, 2);
  793. /* Arbitrary amounts of 0-padding are okay */
  794. r = tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3);
  795. test_eq(r, 3);
  796. test_eq(u1, 12u);
  797. test_eq(u2, 3u);
  798. test_eq(u3, 99u);
  799. r = tor_sscanf("99% fresh", "%3u%% fresh", &u1); /* percents are scannable.*/
  800. test_eq(r, 1);
  801. test_eq(u1, 99);
  802. r = tor_sscanf("hello", "%s", s1); /* %s needs a number. */
  803. test_eq(r, -1);
  804. r = tor_sscanf("hello", "%3s%7s", s1, s2); /* %s matches characters. */
  805. test_eq(r, 2);
  806. test_streq(s1, "hel");
  807. test_streq(s2, "lo");
  808. r = tor_sscanf("WD40", "%2s%u", s3, &u1); /* %s%u */
  809. test_eq(r, 2);
  810. test_streq(s3, "WD");
  811. test_eq(u1, 40);
  812. r = tor_sscanf("76trombones", "%6u%9s", &u1, s1); /* %u%s */
  813. test_eq(r, 2);
  814. test_eq(u1, 76);
  815. test_streq(s1, "trombones");
  816. r = tor_sscanf("hello world", "%9s %9s", s1, s2); /* %s doesn't eat space. */
  817. test_eq(r, 2);
  818. test_streq(s1, "hello");
  819. test_streq(s2, "world");
  820. r = tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3); /* %s can be empty. */
  821. test_eq(r, 3);
  822. test_streq(s1, "hi");
  823. test_streq(s2, "");
  824. test_streq(s3, "");
  825. r = tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  826. test_eq(r, 3);
  827. r = tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  828. test_eq(r, 4);
  829. done:
  830. ;
  831. }
  832. /** Run unittests for memory pool allocator */
  833. static void
  834. test_util_mempool(void)
  835. {
  836. mp_pool_t *pool = NULL;
  837. smartlist_t *allocated = NULL;
  838. int i;
  839. pool = mp_pool_new(1, 100);
  840. test_assert(pool);
  841. test_assert(pool->new_chunk_capacity >= 100);
  842. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  843. mp_pool_destroy(pool);
  844. pool = NULL;
  845. pool = mp_pool_new(241, 2500);
  846. test_assert(pool);
  847. test_assert(pool->new_chunk_capacity >= 10);
  848. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  849. test_eq(pool->item_alloc_size & 0x03, 0);
  850. test_assert(pool->new_chunk_capacity < 60);
  851. allocated = smartlist_create();
  852. for (i = 0; i < 20000; ++i) {
  853. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  854. void *m = mp_pool_get(pool);
  855. memset(m, 0x09, 241);
  856. smartlist_add(allocated, m);
  857. //printf("%d: %p\n", i, m);
  858. //mp_pool_assert_ok(pool);
  859. } else {
  860. int idx = crypto_rand_int(smartlist_len(allocated));
  861. void *m = smartlist_get(allocated, idx);
  862. //printf("%d: free %p\n", i, m);
  863. smartlist_del(allocated, idx);
  864. mp_pool_release(m);
  865. //mp_pool_assert_ok(pool);
  866. }
  867. if (crypto_rand_int(777)==0)
  868. mp_pool_clean(pool, 1, 1);
  869. if (i % 777)
  870. mp_pool_assert_ok(pool);
  871. }
  872. done:
  873. if (allocated) {
  874. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  875. mp_pool_assert_ok(pool);
  876. mp_pool_clean(pool, 0, 0);
  877. mp_pool_assert_ok(pool);
  878. smartlist_free(allocated);
  879. }
  880. if (pool)
  881. mp_pool_destroy(pool);
  882. }
  883. /** Run unittests for memory area allocator */
  884. static void
  885. test_util_memarea(void)
  886. {
  887. memarea_t *area = memarea_new();
  888. char *p1, *p2, *p3, *p1_orig;
  889. void *malloced_ptr = NULL;
  890. int i;
  891. test_assert(area);
  892. p1_orig = p1 = memarea_alloc(area,64);
  893. p2 = memarea_alloc_zero(area,52);
  894. p3 = memarea_alloc(area,11);
  895. test_assert(memarea_owns_ptr(area, p1));
  896. test_assert(memarea_owns_ptr(area, p2));
  897. test_assert(memarea_owns_ptr(area, p3));
  898. /* Make sure we left enough space. */
  899. test_assert(p1+64 <= p2);
  900. test_assert(p2+52 <= p3);
  901. /* Make sure we aligned. */
  902. test_eq(((uintptr_t)p1) % sizeof(void*), 0);
  903. test_eq(((uintptr_t)p2) % sizeof(void*), 0);
  904. test_eq(((uintptr_t)p3) % sizeof(void*), 0);
  905. test_assert(!memarea_owns_ptr(area, p3+8192));
  906. test_assert(!memarea_owns_ptr(area, p3+30));
  907. test_assert(tor_mem_is_zero(p2, 52));
  908. /* Make sure we don't overalign. */
  909. p1 = memarea_alloc(area, 1);
  910. p2 = memarea_alloc(area, 1);
  911. test_eq(p1+sizeof(void*), p2);
  912. {
  913. malloced_ptr = tor_malloc(64);
  914. test_assert(!memarea_owns_ptr(area, malloced_ptr));
  915. tor_free(malloced_ptr);
  916. }
  917. /* memarea_memdup */
  918. {
  919. malloced_ptr = tor_malloc(64);
  920. crypto_rand((char*)malloced_ptr, 64);
  921. p1 = memarea_memdup(area, malloced_ptr, 64);
  922. test_assert(p1 != malloced_ptr);
  923. test_memeq(p1, malloced_ptr, 64);
  924. tor_free(malloced_ptr);
  925. }
  926. /* memarea_strdup. */
  927. p1 = memarea_strdup(area,"");
  928. p2 = memarea_strdup(area, "abcd");
  929. test_assert(p1);
  930. test_assert(p2);
  931. test_streq(p1, "");
  932. test_streq(p2, "abcd");
  933. /* memarea_strndup. */
  934. {
  935. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  936. /* (From Turandot, act 3.) */
  937. size_t len = strlen(s);
  938. p1 = memarea_strndup(area, s, 1000);
  939. p2 = memarea_strndup(area, s, 10);
  940. test_streq(p1, s);
  941. test_assert(p2 >= p1 + len + 1);
  942. test_memeq(s, p2, 10);
  943. test_eq(p2[10], '\0');
  944. p3 = memarea_strndup(area, s, len);
  945. test_streq(p3, s);
  946. p3 = memarea_strndup(area, s, len-1);
  947. test_memeq(s, p3, len-1);
  948. test_eq(p3[len-1], '\0');
  949. }
  950. memarea_clear(area);
  951. p1 = memarea_alloc(area, 1);
  952. test_eq(p1, p1_orig);
  953. memarea_clear(area);
  954. /* Check for running over an area's size. */
  955. for (i = 0; i < 512; ++i) {
  956. p1 = memarea_alloc(area, crypto_rand_int(5)+1);
  957. test_assert(memarea_owns_ptr(area, p1));
  958. }
  959. memarea_assert_ok(area);
  960. /* Make sure we can allocate a too-big object. */
  961. p1 = memarea_alloc_zero(area, 9000);
  962. p2 = memarea_alloc_zero(area, 16);
  963. test_assert(memarea_owns_ptr(area, p1));
  964. test_assert(memarea_owns_ptr(area, p2));
  965. done:
  966. memarea_drop_all(area);
  967. tor_free(malloced_ptr);
  968. }
  969. /** Run unit tests for utility functions to get file names relative to
  970. * the data directory. */
  971. static void
  972. test_util_datadir(void)
  973. {
  974. char buf[1024];
  975. char *f = NULL;
  976. char *temp_dir = NULL;
  977. temp_dir = get_datadir_fname(NULL);
  978. f = get_datadir_fname("state");
  979. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  980. test_streq(f, buf);
  981. tor_free(f);
  982. f = get_datadir_fname2("cache", "thingy");
  983. tor_snprintf(buf, sizeof(buf),
  984. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  985. test_streq(f, buf);
  986. tor_free(f);
  987. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  988. tor_snprintf(buf, sizeof(buf),
  989. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  990. test_streq(f, buf);
  991. tor_free(f);
  992. f = get_datadir_fname_suffix("cache", ".foo");
  993. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  994. temp_dir);
  995. test_streq(f, buf);
  996. done:
  997. tor_free(f);
  998. tor_free(temp_dir);
  999. }
  1000. static void
  1001. test_util_strtok(void)
  1002. {
  1003. char buf[128];
  1004. char buf2[128];
  1005. char *cp1, *cp2;
  1006. strlcpy(buf, "Graved on the dark in gestures of descent", sizeof(buf));
  1007. strlcpy(buf2, "they.seemed;their!own;most.perfect;monument", sizeof(buf2));
  1008. /* -- "Year's End", Richard Wilbur */
  1009. test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
  1010. test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  1011. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  1012. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  1013. test_streq("on", S1());
  1014. test_streq("the", S1());
  1015. test_streq("dark", S1());
  1016. test_streq("seemed", S2());
  1017. test_streq("their", S2());
  1018. test_streq("own", S2());
  1019. test_streq("in", S1());
  1020. test_streq("gestures", S1());
  1021. test_streq("of", S1());
  1022. test_streq("most", S2());
  1023. test_streq("perfect", S2());
  1024. test_streq("descent", S1());
  1025. test_streq("monument", S2());
  1026. test_assert(NULL == S1());
  1027. test_assert(NULL == S2());
  1028. done:
  1029. ;
  1030. }
  1031. static void
  1032. test_util_find_str_at_start_of_line(void *ptr)
  1033. {
  1034. const char *long_string =
  1035. "hello world. hello world. hello hello. howdy.\n"
  1036. "hello hello world\n";
  1037. (void)ptr;
  1038. /* not-found case. */
  1039. tt_assert(! find_str_at_start_of_line(long_string, "fred"));
  1040. /* not-found case where haystack doesn't end with \n */
  1041. tt_assert(! find_str_at_start_of_line("foobar\nbaz", "fred"));
  1042. /* start-of-string case */
  1043. tt_assert(long_string ==
  1044. find_str_at_start_of_line(long_string, "hello world."));
  1045. /* start-of-line case */
  1046. tt_assert(strchr(long_string,'\n')+1 ==
  1047. find_str_at_start_of_line(long_string, "hello hello"));
  1048. done:
  1049. ;
  1050. }
  1051. static void
  1052. test_util_asprintf(void *ptr)
  1053. {
  1054. #define LOREMIPSUM \
  1055. "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  1056. char *cp=NULL, *cp2=NULL;
  1057. int r;
  1058. (void)ptr;
  1059. /* empty string. */
  1060. r = tor_asprintf(&cp, "%s", "");
  1061. tt_assert(cp);
  1062. tt_int_op(r, ==, strlen(cp));
  1063. tt_str_op(cp, ==, "");
  1064. /* Short string with some printing in it. */
  1065. r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
  1066. tt_assert(cp2);
  1067. tt_int_op(r, ==, strlen(cp2));
  1068. tt_str_op(cp2, ==, "First=101, Second=202");
  1069. tt_assert(cp != cp2);
  1070. tor_free(cp);
  1071. tor_free(cp2);
  1072. /* Glass-box test: a string exactly 128 characters long. */
  1073. r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
  1074. tt_assert(cp);
  1075. tt_int_op(r, ==, 128);
  1076. tt_assert(cp[128] == '\0');
  1077. tt_str_op(cp, ==,
  1078. "Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM);
  1079. tor_free(cp);
  1080. /* String longer than 128 characters */
  1081. r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
  1082. LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
  1083. tt_assert(cp);
  1084. tt_int_op(r, ==, strlen(cp));
  1085. tt_str_op(cp, ==, "1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM);
  1086. done:
  1087. tor_free(cp);
  1088. tor_free(cp2);
  1089. }
  1090. static void
  1091. test_util_listdir(void *ptr)
  1092. {
  1093. smartlist_t *dir_contents = NULL;
  1094. char *fname1=NULL, *fname2=NULL, *dirname=NULL;
  1095. (void)ptr;
  1096. fname1 = tor_strdup(get_fname("hopscotch"));
  1097. fname2 = tor_strdup(get_fname("mumblety-peg"));
  1098. dirname = tor_strdup(get_fname(NULL));
  1099. tt_int_op(write_str_to_file(fname1, "X\n", 0), ==, 0);
  1100. tt_int_op(write_str_to_file(fname2, "Y\n", 0), ==, 0);
  1101. dir_contents = tor_listdir(dirname);
  1102. tt_assert(dir_contents);
  1103. /* make sure that each filename is listed. */
  1104. tt_assert(smartlist_string_isin_case(dir_contents, "hopscotch"));
  1105. tt_assert(smartlist_string_isin_case(dir_contents, "mumblety-peg"));
  1106. tt_assert(!smartlist_string_isin(dir_contents, "."));
  1107. tt_assert(!smartlist_string_isin(dir_contents, ".."));
  1108. done:
  1109. tor_free(fname1);
  1110. tor_free(fname2);
  1111. tor_free(dirname);
  1112. if (dir_contents) {
  1113. SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
  1114. smartlist_free(dir_contents);
  1115. }
  1116. }
  1117. static void
  1118. test_util_parent_dir(void *ptr)
  1119. {
  1120. char *cp;
  1121. (void)ptr;
  1122. #define T(input,expect_ok,output) \
  1123. do { \
  1124. int ok; \
  1125. cp = tor_strdup(input); \
  1126. ok = get_parent_directory(cp); \
  1127. tt_int_op(ok, ==, expect_ok); \
  1128. if (ok==0) \
  1129. tt_str_op(cp, ==, output); \
  1130. tor_free(cp); \
  1131. } while (0);
  1132. T("/home/wombat/knish", 0, "/home/wombat");
  1133. T("/home/wombat/knish/", 0, "/home/wombat");
  1134. T("./home/wombat/knish/", 0, "./home/wombat");
  1135. T("./wombat", 0, ".");
  1136. T("", -1, "");
  1137. T("/", -1, "");
  1138. T("////", -1, "");
  1139. done:
  1140. tor_free(cp);
  1141. }
  1142. #ifdef MS_WINDOWS
  1143. static void
  1144. test_util_load_win_lib(void *ptr)
  1145. {
  1146. HANDLE h = load_windows_system_library("advapi32.dll");
  1147. (void) ptr;
  1148. tt_assert(h);
  1149. done:
  1150. if (h)
  1151. CloseHandle(h);
  1152. }
  1153. #endif
  1154. static void
  1155. test_util_di_ops(void)
  1156. {
  1157. #define LT -1
  1158. #define GT 1
  1159. #define EQ 0
  1160. const struct {
  1161. const char *a; int want_sign; const char *b;
  1162. } examples[] = {
  1163. { "Foo", EQ, "Foo" },
  1164. { "foo", GT, "bar", },
  1165. { "foobar", EQ ,"foobar" },
  1166. { "foobar", LT, "foobaw" },
  1167. { "foobar", GT, "f00bar" },
  1168. { "foobar", GT, "boobar" },
  1169. { "", EQ, "" },
  1170. { NULL, 0, NULL },
  1171. };
  1172. int i;
  1173. for (i = 0; examples[i].a; ++i) {
  1174. size_t len = strlen(examples[i].a);
  1175. int eq1, eq2, neq1, neq2, cmp1, cmp2;
  1176. test_eq(len, strlen(examples[i].b));
  1177. /* We do all of the operations, with operands in both orders. */
  1178. eq1 = tor_memeq(examples[i].a, examples[i].b, len);
  1179. eq2 = tor_memeq(examples[i].b, examples[i].a, len);
  1180. neq1 = tor_memneq(examples[i].a, examples[i].b, len);
  1181. neq2 = tor_memneq(examples[i].b, examples[i].a, len);
  1182. cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
  1183. cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
  1184. /* Check for correctness of cmp1 */
  1185. if (cmp1 < 0 && examples[i].want_sign != LT)
  1186. test_fail();
  1187. else if (cmp1 > 0 && examples[i].want_sign != GT)
  1188. test_fail();
  1189. else if (cmp1 == 0 && examples[i].want_sign != EQ)
  1190. test_fail();
  1191. /* Check for consistency of everything else with cmp1 */
  1192. test_eq(eq1, eq2);
  1193. test_eq(neq1, neq2);
  1194. test_eq(cmp1, -cmp2);
  1195. test_eq(eq1, cmp1 == 0);
  1196. test_eq(neq1, !eq1);
  1197. }
  1198. done:
  1199. ;
  1200. }
  1201. #define UTIL_LEGACY(name) \
  1202. { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
  1203. #define UTIL_TEST(name, flags) \
  1204. { #name, test_util_ ## name, flags, NULL, NULL }
  1205. struct testcase_t util_tests[] = {
  1206. UTIL_LEGACY(time),
  1207. UTIL_TEST(parse_http_time, 0),
  1208. UTIL_LEGACY(config_line),
  1209. UTIL_LEGACY(strmisc),
  1210. UTIL_LEGACY(pow2),
  1211. UTIL_LEGACY(gzip),
  1212. UTIL_LEGACY(datadir),
  1213. UTIL_LEGACY(mempool),
  1214. UTIL_LEGACY(memarea),
  1215. UTIL_LEGACY(control_formats),
  1216. UTIL_LEGACY(mmap),
  1217. UTIL_LEGACY(threads),
  1218. UTIL_LEGACY(sscanf),
  1219. UTIL_LEGACY(strtok),
  1220. UTIL_LEGACY(di_ops),
  1221. UTIL_TEST(find_str_at_start_of_line, 0),
  1222. UTIL_TEST(asprintf, 0),
  1223. UTIL_TEST(listdir, 0),
  1224. UTIL_TEST(parent_dir, 0),
  1225. #ifdef MS_WINDOWS
  1226. UTIL_TEST(load_win_lib, 0),
  1227. #endif
  1228. END_OF_TESTCASES
  1229. };