test_consdiff.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /* Copyright (c) 2014, Daniel Martí
  2. * Copyright (c) 2014, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. #include "or.h"
  5. #include "test.h"
  6. #include "consdiff.c"
  7. #ifndef OP_EQ
  8. #define OP_EQ ==
  9. #endif
  10. #ifndef OP_NE
  11. #define OP_NE !=
  12. #endif
  13. static void
  14. test_consdiff_smartlist_slice(void *arg)
  15. {
  16. smartlist_t *sl = smartlist_new();
  17. smartlist_slice_t *sls;
  18. /* Create a regular smartlist. */
  19. (void)arg;
  20. smartlist_add(sl, (void*)1);
  21. smartlist_add(sl, (void*)2);
  22. smartlist_add(sl, (void*)3);
  23. smartlist_add(sl, (void*)4);
  24. smartlist_add(sl, (void*)5);
  25. /* See if the slice was done correctly. */
  26. sls = smartlist_slice(sl, 2, 5);
  27. tt_ptr_op(sl, OP_EQ, sls->list);
  28. tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
  29. tt_ptr_op((void*)5, OP_EQ,
  30. smartlist_get(sls->list, sls->offset + (sls->len-1)));
  31. tor_free(sls);
  32. /* See that using -1 as the end does get to the last element. */
  33. sls = smartlist_slice(sl, 2, -1);
  34. tt_ptr_op(sl, OP_EQ, sls->list);
  35. tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
  36. tt_ptr_op((void*)5, OP_EQ,
  37. smartlist_get(sls->list, sls->offset + (sls->len-1)));
  38. done:
  39. tor_free(sls);
  40. smartlist_free(sl);
  41. }
  42. static void
  43. test_consdiff_smartlist_slice_string_pos(void *arg)
  44. {
  45. smartlist_t *sl = smartlist_new();
  46. smartlist_slice_t *sls;
  47. /* Create a regular smartlist. */
  48. (void)arg;
  49. smartlist_split_string(sl, "a:d:c:a:b", ":", 0, 0);
  50. /* See that smartlist_slice_string_pos respects the bounds of the slice. */
  51. sls = smartlist_slice(sl, 2, 5);
  52. tt_int_op(3, OP_EQ, smartlist_slice_string_pos(sls, "a"));
  53. tt_int_op(-1, OP_EQ, smartlist_slice_string_pos(sls, "d"));
  54. done:
  55. tor_free(sls);
  56. if (sl) SMARTLIST_FOREACH(sl, char*, line, tor_free(line));
  57. smartlist_free(sl);
  58. }
  59. static void
  60. test_consdiff_lcs_lengths(void *arg)
  61. {
  62. smartlist_t *sl1 = smartlist_new();
  63. smartlist_t *sl2 = smartlist_new();
  64. smartlist_slice_t *sls1, *sls2;
  65. int *lengths1, *lengths2;
  66. /* Expected lcs lengths in regular and reverse order. */
  67. int e_lengths1[] = { 0, 1, 2, 3, 3, 4 };
  68. int e_lengths2[] = { 0, 1, 1, 2, 3, 4 };
  69. (void)arg;
  70. smartlist_split_string(sl1, "a:b:c:d:e", ":", 0, 0);
  71. smartlist_split_string(sl2, "a:c:d:i:e", ":", 0, 0);
  72. sls1 = smartlist_slice(sl1, 0, -1);
  73. sls2 = smartlist_slice(sl2, 0, -1);
  74. lengths1 = lcs_lengths(sls1, sls2, 1);
  75. lengths2 = lcs_lengths(sls1, sls2, -1);
  76. tt_mem_op(e_lengths1, OP_EQ, lengths1, sizeof(int) * 6);
  77. tt_mem_op(e_lengths2, OP_EQ, lengths2, sizeof(int) * 6);
  78. done:
  79. tor_free(lengths1);
  80. tor_free(lengths2);
  81. tor_free(sls1);
  82. tor_free(sls2);
  83. if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
  84. if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  85. smartlist_free(sl1);
  86. smartlist_free(sl2);
  87. }
  88. static void
  89. test_consdiff_trim_slices(void *arg)
  90. {
  91. smartlist_t *sl1 = smartlist_new();
  92. smartlist_t *sl2 = smartlist_new();
  93. smartlist_t *sl3 = smartlist_new();
  94. smartlist_t *sl4 = smartlist_new();
  95. smartlist_slice_t *sls1, *sls2, *sls3, *sls4;
  96. (void)arg;
  97. smartlist_split_string(sl1, "a:b:b:b:d", ":", 0, 0);
  98. smartlist_split_string(sl2, "a:c:c:c:d", ":", 0, 0);
  99. smartlist_split_string(sl3, "a:b:b:b:a", ":", 0, 0);
  100. smartlist_split_string(sl4, "c:b:b:b:c", ":", 0, 0);
  101. sls1 = smartlist_slice(sl1, 0, -1);
  102. sls2 = smartlist_slice(sl2, 0, -1);
  103. sls3 = smartlist_slice(sl3, 0, -1);
  104. sls4 = smartlist_slice(sl4, 0, -1);
  105. /* They should be trimmed by one line at each end. */
  106. tt_int_op(5, OP_EQ, sls1->len);
  107. tt_int_op(5, OP_EQ, sls2->len);
  108. trim_slices(sls1, sls2);
  109. tt_int_op(3, OP_EQ, sls1->len);
  110. tt_int_op(3, OP_EQ, sls2->len);
  111. /* They should not be trimmed at all. */
  112. tt_int_op(5, OP_EQ, sls3->len);
  113. tt_int_op(5, OP_EQ, sls4->len);
  114. trim_slices(sls3, sls4);
  115. tt_int_op(5, OP_EQ, sls3->len);
  116. tt_int_op(5, OP_EQ, sls4->len);
  117. done:
  118. tor_free(sls1);
  119. tor_free(sls2);
  120. tor_free(sls3);
  121. tor_free(sls4);
  122. if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
  123. if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  124. if (sl3) SMARTLIST_FOREACH(sl3, char*, line, tor_free(line));
  125. if (sl4) SMARTLIST_FOREACH(sl4, char*, line, tor_free(line));
  126. smartlist_free(sl1);
  127. smartlist_free(sl2);
  128. smartlist_free(sl3);
  129. smartlist_free(sl4);
  130. }
  131. static void
  132. test_consdiff_set_changed(void *arg)
  133. {
  134. smartlist_t *sl1 = smartlist_new();
  135. smartlist_t *sl2 = smartlist_new();
  136. bitarray_t *changed1 = bitarray_init_zero(4);
  137. bitarray_t *changed2 = bitarray_init_zero(4);
  138. smartlist_slice_t *sls1, *sls2;
  139. (void)arg;
  140. smartlist_split_string(sl1, "a:b:a:a", ":", 0, 0);
  141. smartlist_split_string(sl2, "a:a:a:a", ":", 0, 0);
  142. /* Length of sls1 is 0. */
  143. sls1 = smartlist_slice(sl1, 0, 0);
  144. sls2 = smartlist_slice(sl2, 1, 3);
  145. set_changed(changed1, changed2, sls1, sls2);
  146. /* The former is not changed, the latter changes all of its elements. */
  147. tt_assert(!bitarray_is_set(changed1, 0));
  148. tt_assert(!bitarray_is_set(changed1, 1));
  149. tt_assert(!bitarray_is_set(changed1, 2));
  150. tt_assert(!bitarray_is_set(changed1, 3));
  151. tt_assert(!bitarray_is_set(changed2, 0));
  152. tt_assert(bitarray_is_set(changed2, 1));
  153. tt_assert(bitarray_is_set(changed2, 2));
  154. tt_assert(!bitarray_is_set(changed2, 3));
  155. bitarray_clear(changed2, 1);
  156. bitarray_clear(changed2, 2);
  157. /* Length of sls1 is 1 and its element is in sls2. */
  158. tor_free(sls1);
  159. sls1 = smartlist_slice(sl1, 0, 1);
  160. set_changed(changed1, changed2, sls1, sls2);
  161. /* The latter changes all elements but the (first) common one. */
  162. tt_assert(!bitarray_is_set(changed1, 0));
  163. tt_assert(!bitarray_is_set(changed1, 1));
  164. tt_assert(!bitarray_is_set(changed1, 2));
  165. tt_assert(!bitarray_is_set(changed1, 3));
  166. tt_assert(!bitarray_is_set(changed2, 0));
  167. tt_assert(!bitarray_is_set(changed2, 1));
  168. tt_assert(bitarray_is_set(changed2, 2));
  169. tt_assert(!bitarray_is_set(changed2, 3));
  170. bitarray_clear(changed2, 2);
  171. /* Length of sls1 is 1 and its element is not in sls2. */
  172. tor_free(sls1);
  173. sls1 = smartlist_slice(sl1, 1, 2);
  174. set_changed(changed1, changed2, sls1, sls2);
  175. /* The former changes its element, the latter changes all elements. */
  176. tt_assert(!bitarray_is_set(changed1, 0));
  177. tt_assert(bitarray_is_set(changed1, 1));
  178. tt_assert(!bitarray_is_set(changed1, 2));
  179. tt_assert(!bitarray_is_set(changed1, 3));
  180. tt_assert(!bitarray_is_set(changed2, 0));
  181. tt_assert(bitarray_is_set(changed2, 1));
  182. tt_assert(bitarray_is_set(changed2, 2));
  183. tt_assert(!bitarray_is_set(changed2, 3));
  184. done:
  185. bitarray_free(changed1);
  186. bitarray_free(changed2);
  187. if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
  188. if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  189. smartlist_free(sl1);
  190. smartlist_free(sl2);
  191. tor_free(sls1);
  192. tor_free(sls2);
  193. }
  194. static void
  195. test_consdiff_calc_changes(void *arg)
  196. {
  197. smartlist_t *sl1 = smartlist_new();
  198. smartlist_t *sl2 = smartlist_new();
  199. smartlist_slice_t *sls1, *sls2;
  200. bitarray_t *changed1 = bitarray_init_zero(4);
  201. bitarray_t *changed2 = bitarray_init_zero(4);
  202. (void)arg;
  203. smartlist_split_string(sl1, "a:a:a:a", ":", 0, 0);
  204. smartlist_split_string(sl2, "a:a:a:a", ":", 0, 0);
  205. sls1 = smartlist_slice(sl1, 0, -1);
  206. sls2 = smartlist_slice(sl2, 0, -1);
  207. calc_changes(sls1, sls2, changed1, changed2);
  208. /* Nothing should be set to changed. */
  209. tt_assert(!bitarray_is_set(changed1, 0));
  210. tt_assert(!bitarray_is_set(changed1, 1));
  211. tt_assert(!bitarray_is_set(changed1, 2));
  212. tt_assert(!bitarray_is_set(changed1, 3));
  213. tt_assert(!bitarray_is_set(changed2, 0));
  214. tt_assert(!bitarray_is_set(changed2, 1));
  215. tt_assert(!bitarray_is_set(changed2, 2));
  216. tt_assert(!bitarray_is_set(changed2, 3));
  217. SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  218. smartlist_clear(sl2);
  219. smartlist_split_string(sl2, "a:b:a:b", ":", 0, 0);
  220. tor_free(sls1);
  221. tor_free(sls2);
  222. sls1 = smartlist_slice(sl1, 0, -1);
  223. sls2 = smartlist_slice(sl2, 0, -1);
  224. calc_changes(sls1, sls2, changed1, changed2);
  225. /* Two elements are changed. */
  226. tt_assert(!bitarray_is_set(changed1, 0));
  227. tt_assert(bitarray_is_set(changed1, 1));
  228. tt_assert(bitarray_is_set(changed1, 2));
  229. tt_assert(!bitarray_is_set(changed1, 3));
  230. bitarray_clear(changed1, 1);
  231. bitarray_clear(changed1, 2);
  232. tt_assert(!bitarray_is_set(changed2, 0));
  233. tt_assert(bitarray_is_set(changed2, 1));
  234. tt_assert(!bitarray_is_set(changed2, 2));
  235. tt_assert(bitarray_is_set(changed2, 3));
  236. bitarray_clear(changed1, 1);
  237. bitarray_clear(changed1, 3);
  238. SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  239. smartlist_clear(sl2);
  240. smartlist_split_string(sl2, "b:b:b:b", ":", 0, 0);
  241. tor_free(sls1);
  242. tor_free(sls2);
  243. sls1 = smartlist_slice(sl1, 0, -1);
  244. sls2 = smartlist_slice(sl2, 0, -1);
  245. calc_changes(sls1, sls2, changed1, changed2);
  246. /* All elements are changed. */
  247. tt_assert(bitarray_is_set(changed1, 0));
  248. tt_assert(bitarray_is_set(changed1, 1));
  249. tt_assert(bitarray_is_set(changed1, 2));
  250. tt_assert(bitarray_is_set(changed1, 3));
  251. tt_assert(bitarray_is_set(changed2, 0));
  252. tt_assert(bitarray_is_set(changed2, 1));
  253. tt_assert(bitarray_is_set(changed2, 2));
  254. tt_assert(bitarray_is_set(changed2, 3));
  255. done:
  256. bitarray_free(changed1);
  257. bitarray_free(changed2);
  258. if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
  259. if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
  260. smartlist_free(sl1);
  261. smartlist_free(sl2);
  262. tor_free(sls1);
  263. tor_free(sls2);
  264. }
  265. static void
  266. test_consdiff_get_id_hash(void *arg)
  267. {
  268. const char *line, *e_hash;
  269. /* No hash. */
  270. (void)arg;
  271. tt_ptr_op(NULL, OP_EQ, get_id_hash("r name"));
  272. /* The hash contains characters that are not base64. */
  273. tt_ptr_op(NULL, OP_EQ, get_id_hash( "r name _hash_isnt_base64 etc"));
  274. line = "r name hash+valid+base64 etc";
  275. e_hash = line+7;
  276. tt_ptr_op(e_hash, OP_EQ, get_id_hash(line));
  277. done:
  278. ;
  279. }
  280. static void
  281. test_consdiff_is_valid_router_entry(void *arg)
  282. {
  283. /* Doesn't start with "r ". */
  284. (void)arg;
  285. tt_int_op(0, OP_EQ, is_valid_router_entry("foo"));
  286. /* These are already tested with get_id_hash, but make sure it's run
  287. * properly. */
  288. tt_int_op(0, OP_EQ, is_valid_router_entry("r name"));
  289. tt_int_op(0, OP_EQ, is_valid_router_entry("r name _hash_isnt_base64 etc"));
  290. tt_int_op(1, OP_EQ, is_valid_router_entry("r name hash+valid+base64 etc"));
  291. done:
  292. ;
  293. }
  294. static void
  295. test_consdiff_next_router(void *arg)
  296. {
  297. smartlist_t *sl = smartlist_new();
  298. (void)arg;
  299. smartlist_add(sl, (char*)"foo");
  300. smartlist_add(sl,
  301. (char*)"r name hash+longer+than+27+chars+and+valid+base64 etc");
  302. smartlist_add(sl, (char*)"foo");
  303. smartlist_add(sl, (char*)"foo");
  304. smartlist_add(sl,
  305. (char*)"r name hash+longer+than+27+chars+and+valid+base64 etc");
  306. smartlist_add(sl, (char*)"foo");
  307. /* Not currently on a router entry line, finding the next one. */
  308. tt_int_op(1, OP_EQ, next_router(sl, 0));
  309. tt_int_op(4, OP_EQ, next_router(sl, 2));
  310. /* Already at the beginning of a router entry line, ignore it. */
  311. tt_int_op(4, OP_EQ, next_router(sl, 1));
  312. /* There are no more router entries, so return the line after the last. */
  313. tt_int_op(6, OP_EQ, next_router(sl, 4));
  314. tt_int_op(6, OP_EQ, next_router(sl, 5));
  315. done:
  316. smartlist_free(sl);
  317. }
  318. static void
  319. test_consdiff_base64cmp(void *arg)
  320. {
  321. /* NULL arguments. */
  322. (void)arg;
  323. tt_int_op(0, OP_EQ, base64cmp(NULL, NULL));
  324. tt_int_op(-1, OP_EQ, base64cmp(NULL, "foo"));
  325. tt_int_op(1, OP_EQ, base64cmp("bar", NULL));
  326. /* Nil base64 values. */
  327. tt_int_op(0, OP_EQ, base64cmp("", ""));
  328. tt_int_op(0, OP_EQ, base64cmp("_", "&"));
  329. /* Exact same valid strings. */
  330. tt_int_op(0, OP_EQ, base64cmp("abcABC/+", "abcABC/+"));
  331. /* Both end with an invalid base64 char other than '\0'. */
  332. tt_int_op(0, OP_EQ, base64cmp("abcABC/+ ", "abcABC/+ "));
  333. /* Only one ends with an invalid base64 char other than '\0'. */
  334. tt_int_op(0, OP_EQ, base64cmp("abcABC/+ ", "abcABC/+"));
  335. /* Comparisons that would return differently with strcmp(). */
  336. tt_int_op(-1, OP_EQ, strcmp("/foo", "Afoo"));
  337. tt_int_op(1, OP_EQ, base64cmp("/foo", "Afoo"));
  338. tt_int_op(1, OP_EQ, strcmp("Afoo", "0foo"));
  339. tt_int_op(-1, OP_EQ, base64cmp("Afoo", "0foo"));
  340. /* Comparisons that would return the same as with strcmp(). */
  341. tt_int_op(1, OP_EQ, strcmp("afoo", "Afoo"));
  342. tt_int_op(1, OP_EQ, base64cmp("afoo", "Afoo"));
  343. done:
  344. ;
  345. }
  346. static void
  347. test_consdiff_gen_ed_diff(void *arg)
  348. {
  349. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  350. int i;
  351. (void)arg;
  352. cons1 = smartlist_new();
  353. cons2 = smartlist_new();
  354. /* Identity hashes are not sorted properly, return NULL. */
  355. smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  356. smartlist_add(cons1, (char*)"foo");
  357. smartlist_add(cons1, (char*)"r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  358. smartlist_add(cons1, (char*)"bar");
  359. smartlist_add(cons2, (char*)"r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  360. smartlist_add(cons2, (char*)"foo");
  361. smartlist_add(cons2, (char*)"r name ccccccccccccccccccccccccccc etc");
  362. smartlist_add(cons2, (char*)"bar");
  363. diff = gen_ed_diff(cons1, cons2);
  364. tt_ptr_op(NULL, OP_EQ, diff);
  365. /* Same, but now with the second consensus. */
  366. diff = gen_ed_diff(cons2, cons1);
  367. tt_ptr_op(NULL, OP_EQ, diff);
  368. /* Identity hashes are repeated, return NULL. */
  369. smartlist_clear(cons1);
  370. smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  371. smartlist_add(cons1, (char*)"foo");
  372. smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  373. smartlist_add(cons1, (char*)"bar");
  374. diff = gen_ed_diff(cons1, cons2);
  375. tt_ptr_op(NULL, OP_EQ, diff);
  376. /* We have to add a line that is just a dot, return NULL. */
  377. smartlist_clear(cons1);
  378. smartlist_clear(cons2);
  379. smartlist_add(cons1, (char*)"foo1");
  380. smartlist_add(cons1, (char*)"foo2");
  381. smartlist_add(cons2, (char*)"foo1");
  382. smartlist_add(cons2, (char*)".");
  383. smartlist_add(cons2, (char*)"foo2");
  384. diff = gen_ed_diff(cons1, cons2);
  385. tt_ptr_op(NULL, OP_EQ, diff);
  386. #define MAX_LINE_COUNT (10000)
  387. /* Too many lines to be fed to the quadratic-time function. */
  388. smartlist_clear(cons1);
  389. smartlist_clear(cons2);
  390. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"a");
  391. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"b");
  392. diff = gen_ed_diff(cons1, cons2);
  393. tt_ptr_op(NULL, OP_EQ, diff);
  394. /* We have dot lines, but they don't interfere with the script format. */
  395. smartlist_clear(cons1);
  396. smartlist_clear(cons2);
  397. smartlist_add(cons1, (char*)"foo1");
  398. smartlist_add(cons1, (char*)".");
  399. smartlist_add(cons1, (char*)".");
  400. smartlist_add(cons1, (char*)"foo2");
  401. smartlist_add(cons2, (char*)"foo1");
  402. smartlist_add(cons2, (char*)".");
  403. smartlist_add(cons2, (char*)"foo2");
  404. diff = gen_ed_diff(cons1, cons2);
  405. tt_ptr_op(NULL, OP_NE, diff);
  406. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  407. smartlist_free(diff);
  408. /* Empty diff tests. */
  409. smartlist_clear(cons1);
  410. smartlist_clear(cons2);
  411. diff = gen_ed_diff(cons1, cons2);
  412. tt_ptr_op(NULL, !=, diff);
  413. tt_int_op(0, OP_EQ, smartlist_len(diff));
  414. smartlist_free(diff);
  415. smartlist_add(cons1, (char*)"foo");
  416. smartlist_add(cons1, (char*)"bar");
  417. smartlist_add(cons2, (char*)"foo");
  418. smartlist_add(cons2, (char*)"bar");
  419. diff = gen_ed_diff(cons1, cons2);
  420. tt_ptr_op(NULL, OP_NE, diff);
  421. tt_int_op(0, OP_EQ, smartlist_len(diff));
  422. smartlist_free(diff);
  423. /* Everything is deleted. */
  424. smartlist_clear(cons2);
  425. diff = gen_ed_diff(cons1, cons2);
  426. tt_ptr_op(NULL, OP_NE, diff);
  427. tt_int_op(1, OP_EQ, smartlist_len(diff));
  428. tt_str_op("1,2d", OP_EQ, smartlist_get(diff, 0));
  429. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  430. smartlist_free(diff);
  431. /* Everything is added. */
  432. diff = gen_ed_diff(cons2, cons1);
  433. tt_ptr_op(NULL, OP_NE, diff);
  434. tt_int_op(4, OP_EQ, smartlist_len(diff));
  435. tt_str_op("0a", OP_EQ, smartlist_get(diff, 0));
  436. tt_str_op("foo", OP_EQ, smartlist_get(diff, 1));
  437. tt_str_op("bar", OP_EQ, smartlist_get(diff, 2));
  438. tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
  439. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  440. smartlist_free(diff);
  441. /* Everything is changed. */
  442. smartlist_add(cons2, (char*)"foo2");
  443. smartlist_add(cons2, (char*)"bar2");
  444. diff = gen_ed_diff(cons1, cons2);
  445. tt_ptr_op(NULL, OP_NE, diff);
  446. tt_int_op(4, OP_EQ, smartlist_len(diff));
  447. tt_str_op("1,2c", OP_EQ, smartlist_get(diff, 0));
  448. tt_str_op("foo2", OP_EQ, smartlist_get(diff, 1));
  449. tt_str_op("bar2", OP_EQ, smartlist_get(diff, 2));
  450. tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
  451. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  452. smartlist_free(diff);
  453. /* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */
  454. smartlist_clear(cons1);
  455. smartlist_clear(cons2);
  456. smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
  457. smartlist_split_string(cons2, "A:C:O:E:U", ":", 0, 0);
  458. diff = gen_ed_diff(cons1, cons2);
  459. tt_ptr_op(NULL, OP_NE, diff);
  460. tt_int_op(7, OP_EQ, smartlist_len(diff));
  461. tt_str_op("5a", OP_EQ, smartlist_get(diff, 0));
  462. tt_str_op("U", OP_EQ, smartlist_get(diff, 1));
  463. tt_str_op(".", OP_EQ, smartlist_get(diff, 2));
  464. tt_str_op("4c", OP_EQ, smartlist_get(diff, 3));
  465. tt_str_op("O", OP_EQ, smartlist_get(diff, 4));
  466. tt_str_op(".", OP_EQ, smartlist_get(diff, 5));
  467. tt_str_op("2d", OP_EQ, smartlist_get(diff, 6));
  468. /* TODO: small real use-cases, i.e. consensuses. */
  469. done:
  470. if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
  471. if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  472. smartlist_free(cons1);
  473. smartlist_free(cons2);
  474. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  475. smartlist_free(diff);
  476. }
  477. static void
  478. test_consdiff_apply_ed_diff(void *arg)
  479. {
  480. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  481. (void)arg;
  482. cons1 = smartlist_new();
  483. diff = smartlist_new();
  484. smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
  485. /* Command without range. */
  486. smartlist_add(diff, (char*)"a");
  487. cons2 = apply_ed_diff(cons1, diff);
  488. tt_ptr_op(NULL, OP_EQ, cons2);
  489. smartlist_clear(diff);
  490. /* Range without command. */
  491. smartlist_add(diff, (char*)"1");
  492. cons2 = apply_ed_diff(cons1, diff);
  493. tt_ptr_op(NULL, OP_EQ, cons2);
  494. smartlist_clear(diff);
  495. /* Range without end. */
  496. smartlist_add(diff, (char*)"1,");
  497. cons2 = apply_ed_diff(cons1, diff);
  498. tt_ptr_op(NULL, OP_EQ, cons2);
  499. smartlist_clear(diff);
  500. /* Incoherent ranges. */
  501. smartlist_add(diff, (char*)"1,1");
  502. cons2 = apply_ed_diff(cons1, diff);
  503. tt_ptr_op(NULL, OP_EQ, cons2);
  504. smartlist_clear(diff);
  505. smartlist_add(diff, (char*)"3,2");
  506. cons2 = apply_ed_diff(cons1, diff);
  507. tt_ptr_op(NULL, OP_EQ, cons2);
  508. smartlist_clear(diff);
  509. /* Script is not in reverse order. */
  510. smartlist_add(diff, (char*)"1d");
  511. smartlist_add(diff, (char*)"3d");
  512. cons2 = apply_ed_diff(cons1, diff);
  513. tt_ptr_op(NULL, OP_EQ, cons2);
  514. smartlist_clear(diff);
  515. /* Script contains unrecognised commands longer than one char. */
  516. smartlist_add(diff, (char*)"1foo");
  517. cons2 = apply_ed_diff(cons1, diff);
  518. tt_ptr_op(NULL, OP_EQ, cons2);
  519. smartlist_clear(diff);
  520. /* Script contains unrecognised commands. */
  521. smartlist_add(diff, (char*)"1e");
  522. cons2 = apply_ed_diff(cons1, diff);
  523. tt_ptr_op(NULL, OP_EQ, cons2);
  524. smartlist_clear(diff);
  525. /* Command that should be followed by at least one line and a ".", but
  526. * isn't. */
  527. smartlist_add(diff, (char*)"0a");
  528. cons2 = apply_ed_diff(cons1, diff);
  529. tt_ptr_op(NULL, OP_EQ, cons2);
  530. /* Now it is followed by a ".", but it inserts zero lines. */
  531. smartlist_add(diff, (char*)".");
  532. cons2 = apply_ed_diff(cons1, diff);
  533. tt_ptr_op(NULL, OP_EQ, cons2);
  534. smartlist_clear(diff);
  535. /* Test appending text, 'a'. */
  536. smartlist_split_string(diff, "3a:U:O:.:0a:V:.", ":", 0, 0);
  537. cons2 = apply_ed_diff(cons1, diff);
  538. tt_ptr_op(NULL, OP_NE, cons2);
  539. tt_int_op(8, OP_EQ, smartlist_len(cons2));
  540. tt_str_op("V", OP_EQ, smartlist_get(cons2, 0));
  541. tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
  542. tt_str_op("B", OP_EQ, smartlist_get(cons2, 2));
  543. tt_str_op("C", OP_EQ, smartlist_get(cons2, 3));
  544. tt_str_op("U", OP_EQ, smartlist_get(cons2, 4));
  545. tt_str_op("O", OP_EQ, smartlist_get(cons2, 5));
  546. tt_str_op("D", OP_EQ, smartlist_get(cons2, 6));
  547. tt_str_op("E", OP_EQ, smartlist_get(cons2, 7));
  548. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  549. smartlist_clear(diff);
  550. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  551. smartlist_free(cons2);
  552. /* Test deleting text, 'd'. */
  553. smartlist_split_string(diff, "4d:1,2d", ":", 0, 0);
  554. cons2 = apply_ed_diff(cons1, diff);
  555. tt_ptr_op(NULL, OP_NE, cons2);
  556. tt_int_op(2, OP_EQ, smartlist_len(cons2));
  557. tt_str_op("C", OP_EQ, smartlist_get(cons2, 0));
  558. tt_str_op("E", OP_EQ, smartlist_get(cons2, 1));
  559. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  560. smartlist_clear(diff);
  561. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  562. smartlist_free(cons2);
  563. /* Test changing text, 'c'. */
  564. smartlist_split_string(diff, "4c:T:X:.:1, 2c:M:.", ":", 0, 0);
  565. cons2 = apply_ed_diff(cons1, diff);
  566. tt_ptr_op(NULL, OP_NE, cons2);
  567. tt_int_op(5, OP_EQ, smartlist_len(cons2));
  568. tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
  569. tt_str_op("C", OP_EQ, smartlist_get(cons2, 1));
  570. tt_str_op("T", OP_EQ, smartlist_get(cons2, 2));
  571. tt_str_op("X", OP_EQ, smartlist_get(cons2, 3));
  572. tt_str_op("E", OP_EQ, smartlist_get(cons2, 4));
  573. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  574. smartlist_clear(diff);
  575. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  576. smartlist_free(cons2);
  577. /* Test 'a', 'd' and 'c' together. */
  578. smartlist_split_string(diff, "4c:T:X:.:2d:0a:M:.", ":", 0, 0);
  579. cons2 = apply_ed_diff(cons1, diff);
  580. tt_ptr_op(NULL, OP_NE, cons2);
  581. tt_int_op(6, OP_EQ, smartlist_len(cons2));
  582. tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
  583. tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
  584. tt_str_op("C", OP_EQ, smartlist_get(cons2, 2));
  585. tt_str_op("T", OP_EQ, smartlist_get(cons2, 3));
  586. tt_str_op("X", OP_EQ, smartlist_get(cons2, 4));
  587. tt_str_op("E", OP_EQ, smartlist_get(cons2, 5));
  588. done:
  589. if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
  590. if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  591. smartlist_free(cons1);
  592. smartlist_free(cons2);
  593. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  594. smartlist_free(diff);
  595. }
  596. static void
  597. test_consdiff_gen_diff(void *arg)
  598. {
  599. char *cons1_str=NULL, *cons2_str=NULL;
  600. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  601. common_digests_t digests1, digests2;
  602. (void)arg;
  603. cons1 = smartlist_new();
  604. cons2 = smartlist_new();
  605. /* Identity hashes are not sorted properly, return NULL.
  606. * Already tested in gen_ed_diff, but see that a NULL ed diff also makes
  607. * gen_diff return NULL. */
  608. cons1_str = tor_strdup(
  609. "header\nnetwork-status-version foo\n"
  610. "r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
  611. "r name aaaaaaaaaaaaaaaaa etc\nbar\n"
  612. "directory-signature foo bar\nbar\n"
  613. );
  614. cons2_str = tor_strdup(
  615. "header\nnetwork-status-version foo\n"
  616. "r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
  617. "r name ccccccccccccccccc etc\nbar\n"
  618. "directory-signature foo bar\nbar\n"
  619. );
  620. tt_int_op(0, OP_EQ,
  621. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  622. tt_int_op(0, OP_EQ,
  623. router_get_networkstatus_v3_hashes(cons2_str, &digests2));
  624. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  625. tor_split_lines(cons2, cons2_str, (int)strlen(cons2_str));
  626. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
  627. tt_ptr_op(NULL, OP_EQ, diff);
  628. /* Check that the headers are done properly. */
  629. tor_free(cons1_str);
  630. cons1_str = tor_strdup(
  631. "header\nnetwork-status-version foo\n"
  632. "r name ccccccccccccccccc etc\nfoo\n"
  633. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  634. "directory-signature foo bar\nbar\n"
  635. );
  636. tt_int_op(0, OP_EQ,
  637. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  638. smartlist_clear(cons1);
  639. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  640. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
  641. tt_ptr_op(NULL, OP_NE, diff);
  642. tt_int_op(7, OP_EQ, smartlist_len(diff));
  643. tt_str_op("network-status-diff-version 1", OP_EQ, smartlist_get(diff, 0));
  644. tt_str_op("hash "
  645. "C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A "
  646. "B193E5FBFE5C009AEDE56F9218E6421A1AE5C19F43E091786A73F43F60409B60",
  647. OP_EQ, smartlist_get(diff, 1));
  648. tt_str_op("4,5d", OP_EQ, smartlist_get(diff, 2));
  649. tt_str_op("2a", OP_EQ, smartlist_get(diff, 3));
  650. tt_str_op("r name aaaaaaaaaaaaaaaaa etc", OP_EQ, smartlist_get(diff, 4));
  651. tt_str_op("foo", OP_EQ, smartlist_get(diff, 5));
  652. tt_str_op(".", OP_EQ, smartlist_get(diff, 6));
  653. /* TODO: small real use-cases, i.e. consensuses. */
  654. done:
  655. tor_free(cons1_str);
  656. tor_free(cons2_str);
  657. smartlist_free(cons1);
  658. smartlist_free(cons2);
  659. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  660. smartlist_free(diff);
  661. }
  662. static void
  663. test_consdiff_apply_diff(void *arg)
  664. {
  665. smartlist_t *cons1=NULL, *diff=NULL;
  666. char *cons1_str=NULL, *cons2 = NULL;
  667. common_digests_t digests1;
  668. (void)arg;
  669. cons1 = smartlist_new();
  670. diff = smartlist_new();
  671. cons1_str = tor_strdup(
  672. "header\nnetwork-status-version foo\n"
  673. "r name ccccccccccccccccc etc\nfoo\n"
  674. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  675. "directory-signature foo bar\nbar\n"
  676. );
  677. tt_int_op(0, OP_EQ,
  678. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  679. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  680. /* diff doesn't have enough lines. */
  681. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  682. tt_ptr_op(NULL, OP_EQ, cons2);
  683. /* first line doesn't match format-version string. */
  684. smartlist_add(diff, (char*)"foo-bar");
  685. smartlist_add(diff, (char*)"header-line");
  686. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  687. tt_ptr_op(NULL, OP_EQ, cons2);
  688. /* The first word of the second header line is not "hash". */
  689. smartlist_clear(diff);
  690. smartlist_add(diff, (char*)"network-status-diff-version 1");
  691. smartlist_add(diff, (char*)"word a b");
  692. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  693. tt_ptr_op(NULL, OP_EQ, cons2);
  694. /* Wrong number of words after "hash". */
  695. smartlist_clear(diff);
  696. smartlist_add(diff, (char*)"network-status-diff-version 1");
  697. smartlist_add(diff, (char*)"hash a b c");
  698. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  699. tt_ptr_op(NULL, OP_EQ, cons2);
  700. /* base16 sha256 digests do not have the expected length. */
  701. smartlist_clear(diff);
  702. smartlist_add(diff, (char*)"network-status-diff-version 1");
  703. smartlist_add(diff, (char*)"hash aaa bbb");
  704. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  705. tt_ptr_op(NULL, OP_EQ, cons2);
  706. /* base16 sha256 digests contain non-base16 characters. */
  707. smartlist_clear(diff);
  708. smartlist_add(diff, (char*)"network-status-diff-version 1");
  709. smartlist_add(diff, (char*)"hash"
  710. " ????????????????????????????????????????????????????????????????"
  711. " ----------------------------------------------------------------");
  712. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  713. tt_ptr_op(NULL, OP_EQ, cons2);
  714. /* Invalid ed diff.
  715. * As tested in apply_ed_diff, but check that apply_diff does return NULL if
  716. * the ed diff can't be applied. */
  717. smartlist_clear(diff);
  718. smartlist_add(diff, (char*)"network-status-diff-version 1");
  719. smartlist_add(diff, (char*)"hash"
  720. /* sha256 of cons1. */
  721. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  722. /* sha256 of cons2. */
  723. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  724. smartlist_add(diff, (char*)"foobar");
  725. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  726. tt_ptr_op(NULL, OP_EQ, cons2);
  727. /* Base consensus doesn't match its digest as found in the diff. */
  728. smartlist_clear(diff);
  729. smartlist_add(diff, (char*)"network-status-diff-version 1");
  730. smartlist_add(diff, (char*)"hash"
  731. /* bogus sha256. */
  732. " 3333333333333333333333333333333333333333333333333333333333333333"
  733. /* sha256 of cons2. */
  734. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  735. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  736. tt_ptr_op(NULL, OP_EQ, cons2);
  737. /* Resulting consensus doesn't match its digest as found in the diff. */
  738. smartlist_clear(diff);
  739. smartlist_add(diff, (char*)"network-status-diff-version 1");
  740. smartlist_add(diff, (char*)"hash"
  741. /* sha256 of cons1. */
  742. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  743. /* bogus sha256. */
  744. " 3333333333333333333333333333333333333333333333333333333333333333");
  745. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  746. tt_ptr_op(NULL, OP_EQ, cons2);
  747. /* Very simple test, only to see that nothing errors. */
  748. smartlist_clear(diff);
  749. smartlist_add(diff, (char*)"network-status-diff-version 1");
  750. smartlist_add(diff, (char*)"hash"
  751. /* sha256 of cons1. */
  752. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  753. /* sha256 of cons2. */
  754. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  755. smartlist_add(diff, (char*)"4c");
  756. smartlist_add(diff, (char*)"sample");
  757. smartlist_add(diff, (char*)".");
  758. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  759. tt_ptr_op(NULL, OP_NE, cons2);
  760. tt_str_op(
  761. "header\nnetwork-status-version foo\n"
  762. "r name ccccccccccccccccc etc\nsample\n"
  763. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  764. "directory-signature foo bar\nbar\n", OP_EQ,
  765. cons2);
  766. tor_free(cons2);
  767. /* Check that lowercase letters in base16-encoded digests work too. */
  768. smartlist_clear(diff);
  769. smartlist_add(diff, (char*)"network-status-diff-version 1");
  770. smartlist_add(diff, (char*)"hash"
  771. /* sha256 of cons1. */
  772. " c2199b6827514f39ed9b3f2e2e73735c6c5468fd636240bb454c526220de702a"
  773. /* sha256 of cons2. */
  774. " 635d34593020c08e5ecd865f9986e29d50028efa62843766a8197ad228a7f6aa");
  775. smartlist_add(diff, (char*)"4c");
  776. smartlist_add(diff, (char*)"sample");
  777. smartlist_add(diff, (char*)".");
  778. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  779. tt_ptr_op(NULL, OP_NE, cons2);
  780. tt_str_op(
  781. "header\nnetwork-status-version foo\n"
  782. "r name ccccccccccccccccc etc\nsample\n"
  783. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  784. "directory-signature foo bar\nbar\n", OP_EQ,
  785. cons2);
  786. tor_free(cons2);
  787. smartlist_clear(diff);
  788. done:
  789. tor_free(cons1_str);
  790. smartlist_free(cons1);
  791. smartlist_free(diff);
  792. }
  793. #define CONSDIFF_LEGACY(name) \
  794. { #name, test_consdiff_ ## name , 0, NULL, NULL }
  795. struct testcase_t consdiff_tests[] = {
  796. CONSDIFF_LEGACY(smartlist_slice),
  797. CONSDIFF_LEGACY(smartlist_slice_string_pos),
  798. CONSDIFF_LEGACY(lcs_lengths),
  799. CONSDIFF_LEGACY(trim_slices),
  800. CONSDIFF_LEGACY(set_changed),
  801. CONSDIFF_LEGACY(calc_changes),
  802. CONSDIFF_LEGACY(get_id_hash),
  803. CONSDIFF_LEGACY(is_valid_router_entry),
  804. CONSDIFF_LEGACY(next_router),
  805. CONSDIFF_LEGACY(base64cmp),
  806. CONSDIFF_LEGACY(gen_ed_diff),
  807. CONSDIFF_LEGACY(apply_ed_diff),
  808. CONSDIFF_LEGACY(gen_diff),
  809. CONSDIFF_LEGACY(apply_diff),
  810. END_OF_TESTCASES
  811. };