test_containers.c 32 KB

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