test_containers.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "fp_pair.h"
  8. #include "test.h"
  9. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  10. * *<b>b</b>. */
  11. static int
  12. compare_strs_(const void **a, const void **b)
  13. {
  14. const char *s1 = *a, *s2 = *b;
  15. return strcmp(s1, s2);
  16. }
  17. /** Helper: return a tristate based on comparing the strings in <b>a</b> and
  18. * *<b>b</b>. */
  19. static int
  20. compare_strs_for_bsearch_(const void *a, const void **b)
  21. {
  22. const char *s1 = a, *s2 = *b;
  23. return strcmp(s1, s2);
  24. }
  25. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  26. * *<b>b</b>, excluding a's first character, and ignoring case. */
  27. static int
  28. compare_without_first_ch_(const void *a, const void **b)
  29. {
  30. const char *s1 = a, *s2 = *b;
  31. return strcasecmp(s1+1, s2);
  32. }
  33. /** Run unit tests for basic dynamic-sized array functionality. */
  34. static void
  35. test_container_smartlist_basic(void)
  36. {
  37. smartlist_t *sl;
  38. /* XXXX test sort_digests, uniq_strings, uniq_digests */
  39. /* Test smartlist add, del_keeporder, insert, get. */
  40. sl = smartlist_new();
  41. smartlist_add(sl, (void*)1);
  42. smartlist_add(sl, (void*)2);
  43. smartlist_add(sl, (void*)3);
  44. smartlist_add(sl, (void*)4);
  45. smartlist_del_keeporder(sl, 1);
  46. smartlist_insert(sl, 1, (void*)22);
  47. smartlist_insert(sl, 0, (void*)0);
  48. smartlist_insert(sl, 5, (void*)555);
  49. test_eq_ptr((void*)0, smartlist_get(sl,0));
  50. test_eq_ptr((void*)1, smartlist_get(sl,1));
  51. test_eq_ptr((void*)22, smartlist_get(sl,2));
  52. test_eq_ptr((void*)3, smartlist_get(sl,3));
  53. test_eq_ptr((void*)4, smartlist_get(sl,4));
  54. test_eq_ptr((void*)555, smartlist_get(sl,5));
  55. /* Try deleting in the middle. */
  56. smartlist_del(sl, 1);
  57. test_eq_ptr((void*)555, smartlist_get(sl, 1));
  58. /* Try deleting at the end. */
  59. smartlist_del(sl, 4);
  60. test_eq(4, smartlist_len(sl));
  61. /* test isin. */
  62. test_assert(smartlist_contains(sl, (void*)3));
  63. test_assert(!smartlist_contains(sl, (void*)99));
  64. done:
  65. smartlist_free(sl);
  66. }
  67. /** Run unit tests for smartlist-of-strings functionality. */
  68. static void
  69. test_container_smartlist_strings(void)
  70. {
  71. smartlist_t *sl = smartlist_new();
  72. char *cp=NULL, *cp_alloc=NULL;
  73. size_t sz;
  74. /* Test split and join */
  75. test_eq(0, smartlist_len(sl));
  76. smartlist_split_string(sl, "abc", ":", 0, 0);
  77. test_eq(1, smartlist_len(sl));
  78. test_streq("abc", smartlist_get(sl, 0));
  79. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  80. test_eq(4, smartlist_len(sl));
  81. test_streq("a", smartlist_get(sl, 1));
  82. test_streq("bc", smartlist_get(sl, 2));
  83. test_streq("", smartlist_get(sl, 3));
  84. cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
  85. test_streq(cp_alloc, "abcabc");
  86. tor_free(cp_alloc);
  87. cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
  88. test_streq(cp_alloc, "abc!a!bc!");
  89. tor_free(cp_alloc);
  90. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  91. test_streq(cp_alloc, "abcXYaXYbcXY");
  92. tor_free(cp_alloc);
  93. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  94. test_streq(cp_alloc, "abcXYaXYbcXYXY");
  95. tor_free(cp_alloc);
  96. cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
  97. test_streq(cp_alloc, "abcabc");
  98. tor_free(cp_alloc);
  99. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  100. test_eq(8, smartlist_len(sl));
  101. test_streq("", smartlist_get(sl, 4));
  102. test_streq("def", smartlist_get(sl, 5));
  103. test_streq(" ", smartlist_get(sl, 6));
  104. test_streq("ghijk", smartlist_get(sl, 7));
  105. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  106. smartlist_clear(sl);
  107. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  108. test_eq(3, smartlist_len(sl));
  109. test_streq("a", smartlist_get(sl,0));
  110. test_streq("bbd", smartlist_get(sl,1));
  111. test_streq("cdef", smartlist_get(sl,2));
  112. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  113. SPLIT_SKIP_SPACE, 0);
  114. test_eq(8, smartlist_len(sl));
  115. test_streq("z", smartlist_get(sl,3));
  116. test_streq("zhasd", smartlist_get(sl,4));
  117. test_streq("", smartlist_get(sl,5));
  118. test_streq("bnud", smartlist_get(sl,6));
  119. test_streq("", smartlist_get(sl,7));
  120. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  121. smartlist_clear(sl);
  122. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  123. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  124. test_eq(4, smartlist_len(sl));
  125. test_streq("ab", smartlist_get(sl,0));
  126. test_streq("c", smartlist_get(sl,1));
  127. test_streq("d", smartlist_get(sl,2));
  128. test_streq("ef", smartlist_get(sl,3));
  129. smartlist_split_string(sl, "ghi\tj", NULL,
  130. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  131. test_eq(6, smartlist_len(sl));
  132. test_streq("ghi", smartlist_get(sl,4));
  133. test_streq("j", smartlist_get(sl,5));
  134. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  135. smartlist_clear(sl);
  136. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  137. test_streq(cp_alloc, "");
  138. tor_free(cp_alloc);
  139. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  140. test_streq(cp_alloc, "XY");
  141. tor_free(cp_alloc);
  142. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  143. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  144. test_eq(3, smartlist_len(sl));
  145. test_streq("z", smartlist_get(sl, 0));
  146. test_streq("zhasd", smartlist_get(sl, 1));
  147. test_streq("bnud", smartlist_get(sl, 2));
  148. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  149. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  150. test_eq(5, smartlist_len(sl));
  151. test_streq("z", smartlist_get(sl, 3));
  152. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  153. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  154. smartlist_clear(sl);
  155. smartlist_split_string(sl, "abcd\n", "\n",
  156. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  157. test_eq(1, smartlist_len(sl));
  158. test_streq("abcd", smartlist_get(sl, 0));
  159. smartlist_split_string(sl, "efgh", "\n",
  160. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  161. test_eq(2, smartlist_len(sl));
  162. test_streq("efgh", smartlist_get(sl, 1));
  163. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  164. smartlist_clear(sl);
  165. /* Test swapping, shuffling, and sorting. */
  166. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  167. test_eq(7, smartlist_len(sl));
  168. smartlist_sort(sl, compare_strs_);
  169. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  170. test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
  171. tor_free(cp_alloc);
  172. smartlist_swap(sl, 1, 5);
  173. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  174. test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
  175. tor_free(cp_alloc);
  176. smartlist_shuffle(sl);
  177. test_eq(7, smartlist_len(sl));
  178. test_assert(smartlist_contains_string(sl, "and"));
  179. test_assert(smartlist_contains_string(sl, "router"));
  180. test_assert(smartlist_contains_string(sl, "by"));
  181. test_assert(smartlist_contains_string(sl, "nickm"));
  182. test_assert(smartlist_contains_string(sl, "onion"));
  183. test_assert(smartlist_contains_string(sl, "arma"));
  184. test_assert(smartlist_contains_string(sl, "the"));
  185. /* Test bsearch. */
  186. smartlist_sort(sl, compare_strs_);
  187. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  188. compare_without_first_ch_));
  189. test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
  190. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));
  191. /* Test bsearch_idx */
  192. {
  193. int f;
  194. smartlist_t *tmp = NULL;
  195. test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
  196. test_eq(f, 0);
  197. test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
  198. test_eq(f, 1);
  199. test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
  200. test_eq(f, 0);
  201. test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
  202. test_eq(f, 1);
  203. test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
  204. test_eq(f, 0);
  205. test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
  206. test_eq(f, 0);
  207. /* Test trivial cases for list of length 0 or 1 */
  208. tmp = smartlist_new();
  209. test_eq(0, smartlist_bsearch_idx(tmp, "foo",
  210. compare_strs_for_bsearch_, &f));
  211. test_eq(f, 0);
  212. smartlist_insert(tmp, 0, (void *)("bar"));
  213. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  214. compare_strs_for_bsearch_, &f));
  215. test_eq(f, 0);
  216. test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
  217. compare_strs_for_bsearch_, &f));
  218. test_eq(f, 0);
  219. test_eq(0, smartlist_bsearch_idx(tmp, "bar",
  220. compare_strs_for_bsearch_, &f));
  221. test_eq(f, 1);
  222. /* ... and one for length 2 */
  223. smartlist_insert(tmp, 1, (void *)("foo"));
  224. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  225. compare_strs_for_bsearch_, &f));
  226. test_eq(f, 1);
  227. test_eq(2, smartlist_bsearch_idx(tmp, "goo",
  228. compare_strs_for_bsearch_, &f));
  229. test_eq(f, 0);
  230. smartlist_free(tmp);
  231. }
  232. /* Test reverse() and pop_last() */
  233. smartlist_reverse(sl);
  234. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  235. test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
  236. tor_free(cp_alloc);
  237. cp_alloc = smartlist_pop_last(sl);
  238. test_streq(cp_alloc, "and");
  239. tor_free(cp_alloc);
  240. test_eq(smartlist_len(sl), 6);
  241. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  242. smartlist_clear(sl);
  243. cp_alloc = smartlist_pop_last(sl);
  244. test_eq_ptr(cp_alloc, NULL);
  245. /* Test uniq() */
  246. smartlist_split_string(sl,
  247. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  248. ",", 0, 0);
  249. smartlist_sort(sl, compare_strs_);
  250. smartlist_uniq(sl, compare_strs_, tor_free_);
  251. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  252. test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
  253. tor_free(cp_alloc);
  254. /* Test contains_string, contains_string_case and contains_int_as_string */
  255. test_assert(smartlist_contains_string(sl, "noon"));
  256. test_assert(!smartlist_contains_string(sl, "noonoon"));
  257. test_assert(smartlist_contains_string_case(sl, "nOOn"));
  258. test_assert(!smartlist_contains_string_case(sl, "nooNooN"));
  259. test_assert(smartlist_contains_int_as_string(sl, 50));
  260. test_assert(!smartlist_contains_int_as_string(sl, 60));
  261. /* Test smartlist_choose */
  262. {
  263. int i;
  264. int allsame = 1;
  265. int allin = 1;
  266. void *first = smartlist_choose(sl);
  267. test_assert(smartlist_contains(sl, first));
  268. for (i = 0; i < 100; ++i) {
  269. void *second = smartlist_choose(sl);
  270. if (second != first)
  271. allsame = 0;
  272. if (!smartlist_contains(sl, second))
  273. allin = 0;
  274. }
  275. test_assert(!allsame);
  276. test_assert(allin);
  277. }
  278. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  279. smartlist_clear(sl);
  280. /* Test string_remove and remove and join_strings2 */
  281. smartlist_split_string(sl,
  282. "Some say the Earth will end in ice and some in fire",
  283. " ", 0, 0);
  284. cp = smartlist_get(sl, 4);
  285. test_streq(cp, "will");
  286. smartlist_add(sl, cp);
  287. smartlist_remove(sl, cp);
  288. tor_free(cp);
  289. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  290. test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  291. tor_free(cp_alloc);
  292. smartlist_string_remove(sl, "in");
  293. cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
  294. test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
  295. test_eq((int)sz, 40);
  296. done:
  297. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  298. smartlist_free(sl);
  299. tor_free(cp_alloc);
  300. }
  301. /** Run unit tests for smartlist set manipulation functions. */
  302. static void
  303. test_container_smartlist_overlap(void)
  304. {
  305. smartlist_t *sl = smartlist_new();
  306. smartlist_t *ints = smartlist_new();
  307. smartlist_t *odds = smartlist_new();
  308. smartlist_t *evens = smartlist_new();
  309. smartlist_t *primes = smartlist_new();
  310. int i;
  311. for (i=1; i < 10; i += 2)
  312. smartlist_add(odds, (void*)(uintptr_t)i);
  313. for (i=0; i < 10; i += 2)
  314. smartlist_add(evens, (void*)(uintptr_t)i);
  315. /* add_all */
  316. smartlist_add_all(ints, odds);
  317. smartlist_add_all(ints, evens);
  318. test_eq(smartlist_len(ints), 10);
  319. smartlist_add(primes, (void*)2);
  320. smartlist_add(primes, (void*)3);
  321. smartlist_add(primes, (void*)5);
  322. smartlist_add(primes, (void*)7);
  323. /* overlap */
  324. test_assert(smartlist_overlap(ints, odds));
  325. test_assert(smartlist_overlap(odds, primes));
  326. test_assert(smartlist_overlap(evens, primes));
  327. test_assert(!smartlist_overlap(odds, evens));
  328. /* intersect */
  329. smartlist_add_all(sl, odds);
  330. smartlist_intersect(sl, primes);
  331. test_eq(smartlist_len(sl), 3);
  332. test_assert(smartlist_contains(sl, (void*)3));
  333. test_assert(smartlist_contains(sl, (void*)5));
  334. test_assert(smartlist_contains(sl, (void*)7));
  335. /* subtract */
  336. smartlist_add_all(sl, primes);
  337. smartlist_subtract(sl, odds);
  338. test_eq(smartlist_len(sl), 1);
  339. test_assert(smartlist_contains(sl, (void*)2));
  340. done:
  341. smartlist_free(odds);
  342. smartlist_free(evens);
  343. smartlist_free(ints);
  344. smartlist_free(primes);
  345. smartlist_free(sl);
  346. }
  347. /** Run unit tests for smartlist-of-digests functions. */
  348. static void
  349. test_container_smartlist_digests(void)
  350. {
  351. smartlist_t *sl = smartlist_new();
  352. /* contains_digest */
  353. smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
  354. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  355. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  356. test_eq(0, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA"));
  357. test_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA"));
  358. test_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA"));
  359. test_eq(0, smartlist_contains_digest(sl, "\00090AAB2AAABaasdAAAAA"));
  360. /* sort digests */
  361. smartlist_sort_digests(sl);
  362. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  363. test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  364. test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  365. test_eq(3, smartlist_len(sl));
  366. /* uniq_digests */
  367. smartlist_uniq_digests(sl);
  368. test_eq(2, smartlist_len(sl));
  369. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  370. test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  371. done:
  372. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  373. smartlist_free(sl);
  374. }
  375. /** Run unit tests for concatenate-a-smartlist-of-strings functions. */
  376. static void
  377. test_container_smartlist_join(void)
  378. {
  379. smartlist_t *sl = smartlist_new();
  380. smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
  381. *sl4 = smartlist_new();
  382. char *joined=NULL;
  383. /* unique, sorted. */
  384. smartlist_split_string(sl,
  385. "Abashments Ambush Anchorman Bacon Banks Borscht "
  386. "Bunks Inhumane Insurance Knish Know Manners "
  387. "Maraschinos Stamina Sunbonnets Unicorns Wombats",
  388. " ", 0, 0);
  389. /* non-unique, sorted. */
  390. smartlist_split_string(sl2,
  391. "Ambush Anchorman Anchorman Anemias Anemias Bacon "
  392. "Crossbowmen Inhumane Insurance Knish Know Manners "
  393. "Manners Maraschinos Wombats Wombats Work",
  394. " ", 0, 0);
  395. SMARTLIST_FOREACH_JOIN(sl, char *, cp1,
  396. sl2, char *, cp2,
  397. strcmp(cp1,cp2),
  398. smartlist_add(sl3, cp2)) {
  399. test_streq(cp1, cp2);
  400. smartlist_add(sl4, cp1);
  401. } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
  402. SMARTLIST_FOREACH(sl3, const char *, cp,
  403. test_assert(smartlist_contains(sl2, cp) &&
  404. !smartlist_contains_string(sl, cp)));
  405. SMARTLIST_FOREACH(sl4, const char *, cp,
  406. test_assert(smartlist_contains(sl, cp) &&
  407. smartlist_contains_string(sl2, cp)));
  408. joined = smartlist_join_strings(sl3, ",", 0, NULL);
  409. test_streq(joined, "Anemias,Anemias,Crossbowmen,Work");
  410. tor_free(joined);
  411. joined = smartlist_join_strings(sl4, ",", 0, NULL);
  412. test_streq(joined, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
  413. "Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
  414. tor_free(joined);
  415. done:
  416. smartlist_free(sl4);
  417. smartlist_free(sl3);
  418. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  419. smartlist_free(sl2);
  420. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  421. smartlist_free(sl);
  422. tor_free(joined);
  423. }
  424. /** Run unit tests for bitarray code */
  425. static void
  426. test_container_bitarray(void)
  427. {
  428. bitarray_t *ba = NULL;
  429. int i, j, ok=1;
  430. ba = bitarray_init_zero(1);
  431. test_assert(ba);
  432. test_assert(! bitarray_is_set(ba, 0));
  433. bitarray_set(ba, 0);
  434. test_assert(bitarray_is_set(ba, 0));
  435. bitarray_clear(ba, 0);
  436. test_assert(! bitarray_is_set(ba, 0));
  437. bitarray_free(ba);
  438. ba = bitarray_init_zero(1023);
  439. for (i = 1; i < 64; ) {
  440. for (j = 0; j < 1023; ++j) {
  441. if (j % i)
  442. bitarray_set(ba, j);
  443. else
  444. bitarray_clear(ba, j);
  445. }
  446. for (j = 0; j < 1023; ++j) {
  447. if (!bool_eq(bitarray_is_set(ba, j), j%i))
  448. ok = 0;
  449. }
  450. test_assert(ok);
  451. if (i < 7)
  452. ++i;
  453. else if (i == 28)
  454. i = 32;
  455. else
  456. i += 7;
  457. }
  458. done:
  459. if (ba)
  460. bitarray_free(ba);
  461. }
  462. /** Run unit tests for digest set code (implemented as a hashtable or as a
  463. * bloom filter) */
  464. static void
  465. test_container_digestset(void)
  466. {
  467. smartlist_t *included = smartlist_new();
  468. char d[DIGEST_LEN];
  469. int i;
  470. int ok = 1;
  471. int false_positives = 0;
  472. digestset_t *set = NULL;
  473. for (i = 0; i < 1000; ++i) {
  474. crypto_rand(d, DIGEST_LEN);
  475. smartlist_add(included, tor_memdup(d, DIGEST_LEN));
  476. }
  477. set = digestset_new(1000);
  478. SMARTLIST_FOREACH(included, const char *, cp,
  479. if (digestset_contains(set, cp))
  480. ok = 0);
  481. test_assert(ok);
  482. SMARTLIST_FOREACH(included, const char *, cp,
  483. digestset_add(set, cp));
  484. SMARTLIST_FOREACH(included, const char *, cp,
  485. if (!digestset_contains(set, cp))
  486. ok = 0);
  487. test_assert(ok);
  488. for (i = 0; i < 1000; ++i) {
  489. crypto_rand(d, DIGEST_LEN);
  490. if (digestset_contains(set, d))
  491. ++false_positives;
  492. }
  493. test_assert(false_positives < 50); /* Should be far lower. */
  494. done:
  495. if (set)
  496. digestset_free(set);
  497. SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
  498. smartlist_free(included);
  499. }
  500. typedef struct pq_entry_t {
  501. const char *val;
  502. int idx;
  503. } pq_entry_t;
  504. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  505. static int
  506. compare_strings_for_pqueue_(const void *p1, const void *p2)
  507. {
  508. const pq_entry_t *e1=p1, *e2=p2;
  509. return strcmp(e1->val, e2->val);
  510. }
  511. /** Run unit tests for heap-based priority queue functions. */
  512. static void
  513. test_container_pqueue(void)
  514. {
  515. smartlist_t *sl = smartlist_new();
  516. int (*cmp)(const void *, const void*);
  517. const int offset = STRUCT_OFFSET(pq_entry_t, idx);
  518. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  519. ENTRY(cows);
  520. ENTRY(zebras);
  521. ENTRY(fish);
  522. ENTRY(frogs);
  523. ENTRY(apples);
  524. ENTRY(squid);
  525. ENTRY(daschunds);
  526. ENTRY(eggplants);
  527. ENTRY(weissbier);
  528. ENTRY(lobsters);
  529. ENTRY(roquefort);
  530. ENTRY(chinchillas);
  531. ENTRY(fireflies);
  532. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  533. cmp = compare_strings_for_pqueue_;
  534. smartlist_pqueue_add(sl, cmp, offset, &cows);
  535. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  536. smartlist_pqueue_add(sl, cmp, offset, &fish);
  537. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  538. smartlist_pqueue_add(sl, cmp, offset, &apples);
  539. smartlist_pqueue_add(sl, cmp, offset, &squid);
  540. smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  541. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  542. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  543. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  544. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  545. OK();
  546. test_eq(smartlist_len(sl), 11);
  547. test_eq_ptr(smartlist_get(sl, 0), &apples);
  548. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
  549. test_eq(smartlist_len(sl), 10);
  550. OK();
  551. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
  552. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
  553. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  554. OK();
  555. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  556. OK();
  557. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
  558. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
  559. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
  560. OK();
  561. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  562. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  563. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
  564. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
  565. OK();
  566. test_eq(smartlist_len(sl), 3);
  567. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  568. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
  569. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
  570. test_eq(smartlist_len(sl), 0);
  571. OK();
  572. /* Now test remove. */
  573. smartlist_pqueue_add(sl, cmp, offset, &cows);
  574. smartlist_pqueue_add(sl, cmp, offset, &fish);
  575. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  576. smartlist_pqueue_add(sl, cmp, offset, &apples);
  577. smartlist_pqueue_add(sl, cmp, offset, &squid);
  578. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  579. test_eq(smartlist_len(sl), 6);
  580. OK();
  581. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  582. test_eq(smartlist_len(sl), 5);
  583. OK();
  584. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  585. test_eq(smartlist_len(sl), 4);
  586. OK();
  587. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  588. test_eq(smartlist_len(sl), 3);
  589. OK();
  590. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  591. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  592. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  593. test_eq(smartlist_len(sl), 0);
  594. OK();
  595. #undef OK
  596. done:
  597. smartlist_free(sl);
  598. }
  599. /** Run unit tests for string-to-void* map functions */
  600. static void
  601. test_container_strmap(void)
  602. {
  603. strmap_t *map;
  604. strmap_iter_t *iter;
  605. const char *k;
  606. void *v;
  607. char *visited = NULL;
  608. smartlist_t *found_keys = NULL;
  609. map = strmap_new();
  610. test_assert(map);
  611. test_eq(strmap_size(map), 0);
  612. test_assert(strmap_isempty(map));
  613. v = strmap_set(map, "K1", (void*)99);
  614. test_eq_ptr(v, NULL);
  615. test_assert(!strmap_isempty(map));
  616. v = strmap_set(map, "K2", (void*)101);
  617. test_eq_ptr(v, NULL);
  618. v = strmap_set(map, "K1", (void*)100);
  619. test_eq_ptr(v, (void*)99);
  620. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  621. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  622. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  623. strmap_assert_ok(map);
  624. v = strmap_remove(map,"K2");
  625. strmap_assert_ok(map);
  626. test_eq_ptr(v, (void*)101);
  627. test_eq_ptr(strmap_get(map,"K2"), NULL);
  628. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  629. strmap_set(map, "K2", (void*)101);
  630. strmap_set(map, "K3", (void*)102);
  631. strmap_set(map, "K4", (void*)103);
  632. test_eq(strmap_size(map), 4);
  633. strmap_assert_ok(map);
  634. strmap_set(map, "K5", (void*)104);
  635. strmap_set(map, "K6", (void*)105);
  636. strmap_assert_ok(map);
  637. /* Test iterator. */
  638. iter = strmap_iter_init(map);
  639. found_keys = smartlist_new();
  640. while (!strmap_iter_done(iter)) {
  641. strmap_iter_get(iter,&k,&v);
  642. smartlist_add(found_keys, tor_strdup(k));
  643. test_eq_ptr(v, strmap_get(map, k));
  644. if (!strcmp(k, "K2")) {
  645. iter = strmap_iter_next_rmv(map,iter);
  646. } else {
  647. iter = strmap_iter_next(map,iter);
  648. }
  649. }
  650. /* Make sure we removed K2, but not the others. */
  651. test_eq_ptr(strmap_get(map, "K2"), NULL);
  652. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  653. /* Make sure we visited everyone once */
  654. smartlist_sort_strings(found_keys);
  655. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  656. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  657. strmap_assert_ok(map);
  658. /* Clean up after ourselves. */
  659. strmap_free(map, NULL);
  660. map = NULL;
  661. /* Now try some lc functions. */
  662. map = strmap_new();
  663. strmap_set_lc(map,"Ab.C", (void*)1);
  664. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  665. strmap_assert_ok(map);
  666. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  667. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  668. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  669. strmap_assert_ok(map);
  670. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  671. done:
  672. if (map)
  673. strmap_free(map,NULL);
  674. if (found_keys) {
  675. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  676. smartlist_free(found_keys);
  677. }
  678. tor_free(visited);
  679. }
  680. /** Run unit tests for getting the median of a list. */
  681. static void
  682. test_container_order_functions(void)
  683. {
  684. int lst[25], n = 0;
  685. // int a=12,b=24,c=25,d=60,e=77;
  686. #define median() median_int(lst, n)
  687. lst[n++] = 12;
  688. test_eq(12, median()); /* 12 */
  689. lst[n++] = 77;
  690. //smartlist_shuffle(sl);
  691. test_eq(12, median()); /* 12, 77 */
  692. lst[n++] = 77;
  693. //smartlist_shuffle(sl);
  694. test_eq(77, median()); /* 12, 77, 77 */
  695. lst[n++] = 24;
  696. test_eq(24, median()); /* 12,24,77,77 */
  697. lst[n++] = 60;
  698. lst[n++] = 12;
  699. lst[n++] = 25;
  700. //smartlist_shuffle(sl);
  701. test_eq(25, median()); /* 12,12,24,25,60,77,77 */
  702. #undef median
  703. done:
  704. ;
  705. }
  706. static void
  707. test_di_map(void *arg)
  708. {
  709. di_digest256_map_t *map = NULL;
  710. const uint8_t key1[] = "In view of the fact that it was ";
  711. const uint8_t key2[] = "superficially convincing, being ";
  712. const uint8_t key3[] = "properly enciphered in a one-tim";
  713. const uint8_t key4[] = "e cipher scheduled for use today";
  714. char *v1 = tor_strdup(", it came close to causing a disaster...");
  715. char *v2 = tor_strdup("I regret to have to advise you that the mission");
  716. char *v3 = tor_strdup("was actually initiated...");
  717. /* -- John Brunner, _The Shockwave Rider_ */
  718. (void)arg;
  719. /* Try searching on an empty map. */
  720. tt_ptr_op(NULL, ==, dimap_search(map, key1, NULL));
  721. tt_ptr_op(NULL, ==, dimap_search(map, key2, NULL));
  722. tt_ptr_op(v3, ==, dimap_search(map, key2, v3));
  723. dimap_free(map, NULL);
  724. map = NULL;
  725. /* Add a single entry. */
  726. dimap_add_entry(&map, key1, v1);
  727. tt_ptr_op(NULL, ==, dimap_search(map, key2, NULL));
  728. tt_ptr_op(v3, ==, dimap_search(map, key2, v3));
  729. tt_ptr_op(v1, ==, dimap_search(map, key1, NULL));
  730. /* Now try it with three entries in the map. */
  731. dimap_add_entry(&map, key2, v2);
  732. dimap_add_entry(&map, key3, v3);
  733. tt_ptr_op(v1, ==, dimap_search(map, key1, NULL));
  734. tt_ptr_op(v2, ==, dimap_search(map, key2, NULL));
  735. tt_ptr_op(v3, ==, dimap_search(map, key3, NULL));
  736. tt_ptr_op(NULL, ==, dimap_search(map, key4, NULL));
  737. tt_ptr_op(v1, ==, dimap_search(map, key4, v1));
  738. done:
  739. tor_free(v1);
  740. tor_free(v2);
  741. tor_free(v3);
  742. dimap_free(map, NULL);
  743. }
  744. /** Run unit tests for fp_pair-to-void* map functions */
  745. static void
  746. test_container_fp_pair_map(void)
  747. {
  748. fp_pair_map_t *map;
  749. fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  750. void *v;
  751. fp_pair_map_iter_t *iter;
  752. fp_pair_t k;
  753. map = fp_pair_map_new();
  754. test_assert(map);
  755. test_eq(fp_pair_map_size(map), 0);
  756. test_assert(fp_pair_map_isempty(map));
  757. memset(fp1.first, 0x11, DIGEST_LEN);
  758. memset(fp1.second, 0x12, DIGEST_LEN);
  759. memset(fp2.first, 0x21, DIGEST_LEN);
  760. memset(fp2.second, 0x22, DIGEST_LEN);
  761. memset(fp3.first, 0x31, DIGEST_LEN);
  762. memset(fp3.second, 0x32, DIGEST_LEN);
  763. memset(fp4.first, 0x41, DIGEST_LEN);
  764. memset(fp4.second, 0x42, DIGEST_LEN);
  765. memset(fp5.first, 0x51, DIGEST_LEN);
  766. memset(fp5.second, 0x52, DIGEST_LEN);
  767. memset(fp6.first, 0x61, DIGEST_LEN);
  768. memset(fp6.second, 0x62, DIGEST_LEN);
  769. v = fp_pair_map_set(map, &fp1, (void*)99);
  770. test_eq(v, NULL);
  771. test_assert(!fp_pair_map_isempty(map));
  772. v = fp_pair_map_set(map, &fp2, (void*)101);
  773. test_eq(v, NULL);
  774. v = fp_pair_map_set(map, &fp1, (void*)100);
  775. test_eq(v, (void*)99);
  776. test_eq_ptr(fp_pair_map_get(map, &fp1), (void*)100);
  777. test_eq_ptr(fp_pair_map_get(map, &fp2), (void*)101);
  778. test_eq_ptr(fp_pair_map_get(map, &fp3), NULL);
  779. fp_pair_map_assert_ok(map);
  780. v = fp_pair_map_remove(map, &fp2);
  781. fp_pair_map_assert_ok(map);
  782. test_eq_ptr(v, (void*)101);
  783. test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  784. test_eq_ptr(fp_pair_map_remove(map, &fp2), NULL);
  785. fp_pair_map_set(map, &fp2, (void*)101);
  786. fp_pair_map_set(map, &fp3, (void*)102);
  787. fp_pair_map_set(map, &fp4, (void*)103);
  788. test_eq(fp_pair_map_size(map), 4);
  789. fp_pair_map_assert_ok(map);
  790. fp_pair_map_set(map, &fp5, (void*)104);
  791. fp_pair_map_set(map, &fp6, (void*)105);
  792. fp_pair_map_assert_ok(map);
  793. /* Test iterator. */
  794. iter = fp_pair_map_iter_init(map);
  795. while (!fp_pair_map_iter_done(iter)) {
  796. fp_pair_map_iter_get(iter, &k, &v);
  797. test_eq_ptr(v, fp_pair_map_get(map, &k));
  798. if (tor_memeq(&fp2, &k, sizeof(fp2))) {
  799. iter = fp_pair_map_iter_next_rmv(map, iter);
  800. } else {
  801. iter = fp_pair_map_iter_next(map, iter);
  802. }
  803. }
  804. /* Make sure we removed fp2, but not the others. */
  805. test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  806. test_eq_ptr(fp_pair_map_get(map, &fp5), (void*)104);
  807. fp_pair_map_assert_ok(map);
  808. /* Clean up after ourselves. */
  809. fp_pair_map_free(map, NULL);
  810. map = NULL;
  811. done:
  812. if (map)
  813. fp_pair_map_free(map, NULL);
  814. }
  815. #define CONTAINER_LEGACY(name) \
  816. { #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
  817. struct testcase_t container_tests[] = {
  818. CONTAINER_LEGACY(smartlist_basic),
  819. CONTAINER_LEGACY(smartlist_strings),
  820. CONTAINER_LEGACY(smartlist_overlap),
  821. CONTAINER_LEGACY(smartlist_digests),
  822. CONTAINER_LEGACY(smartlist_join),
  823. CONTAINER_LEGACY(bitarray),
  824. CONTAINER_LEGACY(digestset),
  825. CONTAINER_LEGACY(strmap),
  826. CONTAINER_LEGACY(pqueue),
  827. CONTAINER_LEGACY(order_functions),
  828. { "di_map", test_di_map, 0, NULL, NULL },
  829. CONTAINER_LEGACY(fp_pair_map),
  830. END_OF_TESTCASES
  831. };