test_consdiff.c 31 KB

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