test_consdiff.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 "memarea.h"
  9. #include "log_test_helpers.h"
  10. #define tt_str_eq_line(a,b) \
  11. tt_assert(line_str_eq((b),(a)))
  12. static void
  13. test_consdiff_smartlist_slice(void *arg)
  14. {
  15. smartlist_t *sl = smartlist_new();
  16. smartlist_slice_t *sls;
  17. /* Create a regular smartlist. */
  18. (void)arg;
  19. smartlist_add(sl, (void*)1);
  20. smartlist_add(sl, (void*)2);
  21. smartlist_add(sl, (void*)3);
  22. smartlist_add(sl, (void*)4);
  23. smartlist_add(sl, (void*)5);
  24. /* See if the slice was done correctly. */
  25. sls = smartlist_slice(sl, 2, 5);
  26. tt_ptr_op(sl, OP_EQ, sls->list);
  27. tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
  28. tt_ptr_op((void*)5, OP_EQ,
  29. smartlist_get(sls->list, sls->offset + (sls->len-1)));
  30. tor_free(sls);
  31. /* See that using -1 as the end does get to the last element. */
  32. sls = smartlist_slice(sl, 2, -1);
  33. tt_ptr_op(sl, OP_EQ, sls->list);
  34. tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
  35. tt_ptr_op((void*)5, OP_EQ,
  36. smartlist_get(sls->list, sls->offset + (sls->len-1)));
  37. done:
  38. tor_free(sls);
  39. smartlist_free(sl);
  40. }
  41. static void
  42. test_consdiff_smartlist_slice_string_pos(void *arg)
  43. {
  44. smartlist_t *sl = smartlist_new();
  45. smartlist_slice_t *sls;
  46. memarea_t *area = memarea_new();
  47. /* Create a regular smartlist. */
  48. (void)arg;
  49. consensus_split_lines(sl, "a\nd\nc\na\nb\n", area);
  50. /* See that smartlist_slice_string_pos respects the bounds of the slice. */
  51. sls = smartlist_slice(sl, 2, 5);
  52. cdline_t a_line = { "a", 1 };
  53. tt_int_op(3, OP_EQ, smartlist_slice_string_pos(sls, &a_line));
  54. cdline_t d_line = { "d", 1 };
  55. tt_int_op(-1, OP_EQ, smartlist_slice_string_pos(sls, &d_line));
  56. done:
  57. tor_free(sls);
  58. smartlist_free(sl);
  59. memarea_drop_all(area);
  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. memarea_t *area = memarea_new();
  69. /* Expected lcs lengths in regular and reverse order. */
  70. int e_lengths1[] = { 0, 1, 2, 3, 3, 4 };
  71. int e_lengths2[] = { 0, 1, 1, 2, 3, 4 };
  72. (void)arg;
  73. consensus_split_lines(sl1, "a\nb\nc\nd\ne\n", area);
  74. consensus_split_lines(sl2, "a\nc\nd\ni\ne\n", area);
  75. sls1 = smartlist_slice(sl1, 0, -1);
  76. sls2 = smartlist_slice(sl2, 0, -1);
  77. lengths1 = lcs_lengths(sls1, sls2, 1);
  78. lengths2 = lcs_lengths(sls1, sls2, -1);
  79. tt_mem_op(e_lengths1, OP_EQ, lengths1, sizeof(int) * 6);
  80. tt_mem_op(e_lengths2, OP_EQ, lengths2, sizeof(int) * 6);
  81. done:
  82. tor_free(lengths1);
  83. tor_free(lengths2);
  84. tor_free(sls1);
  85. tor_free(sls2);
  86. smartlist_free(sl1);
  87. smartlist_free(sl2);
  88. memarea_drop_all(area);
  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. memarea_t *area = memarea_new();
  99. (void)arg;
  100. consensus_split_lines(sl1, "a\nb\nb\nb\nd\n", area);
  101. consensus_split_lines(sl2, "a\nc\nc\nc\nd\n", area);
  102. consensus_split_lines(sl3, "a\nb\nb\nb\na\n", area);
  103. consensus_split_lines(sl4, "c\nb\nb\nb\nc\n", area);
  104. sls1 = smartlist_slice(sl1, 0, -1);
  105. sls2 = smartlist_slice(sl2, 0, -1);
  106. sls3 = smartlist_slice(sl3, 0, -1);
  107. sls4 = smartlist_slice(sl4, 0, -1);
  108. /* They should be trimmed by one line at each end. */
  109. tt_int_op(5, OP_EQ, sls1->len);
  110. tt_int_op(5, OP_EQ, sls2->len);
  111. trim_slices(sls1, sls2);
  112. tt_int_op(3, OP_EQ, sls1->len);
  113. tt_int_op(3, OP_EQ, sls2->len);
  114. /* They should not be trimmed at all. */
  115. tt_int_op(5, OP_EQ, sls3->len);
  116. tt_int_op(5, OP_EQ, sls4->len);
  117. trim_slices(sls3, sls4);
  118. tt_int_op(5, OP_EQ, sls3->len);
  119. tt_int_op(5, OP_EQ, sls4->len);
  120. done:
  121. tor_free(sls1);
  122. tor_free(sls2);
  123. tor_free(sls3);
  124. tor_free(sls4);
  125. smartlist_free(sl1);
  126. smartlist_free(sl2);
  127. smartlist_free(sl3);
  128. smartlist_free(sl4);
  129. memarea_drop_all(area);
  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. memarea_t *area = memarea_new();
  140. (void)arg;
  141. consensus_split_lines(sl1, "a\nb\na\na\n", area);
  142. consensus_split_lines(sl2, "a\na\na\na\n", area);
  143. /* Length of sls1 is 0. */
  144. sls1 = smartlist_slice(sl1, 0, 0);
  145. sls2 = smartlist_slice(sl2, 1, 3);
  146. set_changed(changed1, changed2, sls1, sls2);
  147. /* The former is not changed, the latter changes all of its elements. */
  148. tt_assert(!bitarray_is_set(changed1, 0));
  149. tt_assert(!bitarray_is_set(changed1, 1));
  150. tt_assert(!bitarray_is_set(changed1, 2));
  151. tt_assert(!bitarray_is_set(changed1, 3));
  152. tt_assert(!bitarray_is_set(changed2, 0));
  153. tt_assert(bitarray_is_set(changed2, 1));
  154. tt_assert(bitarray_is_set(changed2, 2));
  155. tt_assert(!bitarray_is_set(changed2, 3));
  156. bitarray_clear(changed2, 1);
  157. bitarray_clear(changed2, 2);
  158. /* Length of sls1 is 1 and its element is in sls2. */
  159. tor_free(sls1);
  160. sls1 = smartlist_slice(sl1, 0, 1);
  161. set_changed(changed1, changed2, sls1, sls2);
  162. /* The latter changes all elements but the (first) common one. */
  163. tt_assert(!bitarray_is_set(changed1, 0));
  164. tt_assert(!bitarray_is_set(changed1, 1));
  165. tt_assert(!bitarray_is_set(changed1, 2));
  166. tt_assert(!bitarray_is_set(changed1, 3));
  167. tt_assert(!bitarray_is_set(changed2, 0));
  168. tt_assert(!bitarray_is_set(changed2, 1));
  169. tt_assert(bitarray_is_set(changed2, 2));
  170. tt_assert(!bitarray_is_set(changed2, 3));
  171. bitarray_clear(changed2, 2);
  172. /* Length of sls1 is 1 and its element is not in sls2. */
  173. tor_free(sls1);
  174. sls1 = smartlist_slice(sl1, 1, 2);
  175. set_changed(changed1, changed2, sls1, sls2);
  176. /* The former changes its element, the latter changes all elements. */
  177. tt_assert(!bitarray_is_set(changed1, 0));
  178. tt_assert(bitarray_is_set(changed1, 1));
  179. tt_assert(!bitarray_is_set(changed1, 2));
  180. tt_assert(!bitarray_is_set(changed1, 3));
  181. tt_assert(!bitarray_is_set(changed2, 0));
  182. tt_assert(bitarray_is_set(changed2, 1));
  183. tt_assert(bitarray_is_set(changed2, 2));
  184. tt_assert(!bitarray_is_set(changed2, 3));
  185. done:
  186. bitarray_free(changed1);
  187. bitarray_free(changed2);
  188. smartlist_free(sl1);
  189. smartlist_free(sl2);
  190. tor_free(sls1);
  191. tor_free(sls2);
  192. memarea_drop_all(area);
  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. memarea_t *area = memarea_new();
  203. (void)arg;
  204. consensus_split_lines(sl1, "a\na\na\na\n", area);
  205. consensus_split_lines(sl2, "a\na\na\na\n", area);
  206. sls1 = smartlist_slice(sl1, 0, -1);
  207. sls2 = smartlist_slice(sl2, 0, -1);
  208. calc_changes(sls1, sls2, changed1, changed2);
  209. /* Nothing should be set to changed. */
  210. tt_assert(!bitarray_is_set(changed1, 0));
  211. tt_assert(!bitarray_is_set(changed1, 1));
  212. tt_assert(!bitarray_is_set(changed1, 2));
  213. tt_assert(!bitarray_is_set(changed1, 3));
  214. tt_assert(!bitarray_is_set(changed2, 0));
  215. tt_assert(!bitarray_is_set(changed2, 1));
  216. tt_assert(!bitarray_is_set(changed2, 2));
  217. tt_assert(!bitarray_is_set(changed2, 3));
  218. smartlist_clear(sl2);
  219. consensus_split_lines(sl2, "a\nb\na\nb\n", area);
  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_clear(sl2);
  239. consensus_split_lines(sl2, "b\nb\nb\nb\n", area);
  240. tor_free(sls1);
  241. tor_free(sls2);
  242. sls1 = smartlist_slice(sl1, 0, -1);
  243. sls2 = smartlist_slice(sl2, 0, -1);
  244. calc_changes(sls1, sls2, changed1, changed2);
  245. /* All elements are changed. */
  246. tt_assert(bitarray_is_set(changed1, 0));
  247. tt_assert(bitarray_is_set(changed1, 1));
  248. tt_assert(bitarray_is_set(changed1, 2));
  249. tt_assert(bitarray_is_set(changed1, 3));
  250. tt_assert(bitarray_is_set(changed2, 0));
  251. tt_assert(bitarray_is_set(changed2, 1));
  252. tt_assert(bitarray_is_set(changed2, 2));
  253. tt_assert(bitarray_is_set(changed2, 3));
  254. done:
  255. bitarray_free(changed1);
  256. bitarray_free(changed2);
  257. smartlist_free(sl1);
  258. smartlist_free(sl2);
  259. tor_free(sls1);
  260. tor_free(sls2);
  261. memarea_drop_all(area);
  262. }
  263. static void
  264. test_consdiff_get_id_hash(void *arg)
  265. {
  266. (void)arg;
  267. cdline_t line1 = { "r name", 6 };
  268. cdline_t line2 = { "r name _hash_isnt_base64 etc", 28 };
  269. cdline_t line3 = { "r name hash+valid+base64 etc", 28 };
  270. cdline_t tmp;
  271. /* No hash. */
  272. tt_int_op(-1, OP_EQ, get_id_hash(&line1, &tmp));
  273. /* The hash contains characters that are not base64. */
  274. tt_int_op(-1, OP_EQ, get_id_hash(&line2, &tmp));
  275. /* valid hash. */
  276. tt_int_op(0, OP_EQ, get_id_hash(&line3, &tmp));
  277. tt_ptr_op(tmp.s, OP_EQ, line3.s + 7);
  278. tt_uint_op(tmp.len, OP_EQ, line3.len - 11);
  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. cdline_t line0 = { "foo", 3 };
  288. tt_int_op(0, OP_EQ, is_valid_router_entry(&line0));
  289. /* These are already tested with get_id_hash, but make sure it's run
  290. * properly. */
  291. cdline_t line1 = { "r name", 6 };
  292. cdline_t line2 = { "r name _hash_isnt_base64 etc", 28 };
  293. cdline_t line3 = { "r name hash+valid+base64 etc", 28 };
  294. tt_int_op(0, OP_EQ, is_valid_router_entry(&line1));
  295. tt_int_op(0, OP_EQ, is_valid_router_entry(&line2));
  296. tt_int_op(1, OP_EQ, is_valid_router_entry(&line3));
  297. done:
  298. ;
  299. }
  300. static void
  301. test_consdiff_next_router(void *arg)
  302. {
  303. smartlist_t *sl = smartlist_new();
  304. memarea_t *area = memarea_new();
  305. (void)arg;
  306. smartlist_add_linecpy(sl, area, "foo");
  307. smartlist_add_linecpy(sl, area,
  308. "r name hash+longer+than+27+chars+and+valid+base64 etc");
  309. smartlist_add_linecpy(sl, area, "foo");
  310. smartlist_add_linecpy(sl, area, "foo");
  311. smartlist_add_linecpy(sl, area,
  312. "r name hash+longer+than+27+chars+and+valid+base64 etc");
  313. smartlist_add_linecpy(sl, area, "foo");
  314. /* Not currently on a router entry line, finding the next one. */
  315. tt_int_op(1, OP_EQ, next_router(sl, 0));
  316. tt_int_op(4, OP_EQ, next_router(sl, 2));
  317. /* Already at the beginning of a router entry line, ignore it. */
  318. tt_int_op(4, OP_EQ, next_router(sl, 1));
  319. /* There are no more router entries, so return the line after the last. */
  320. tt_int_op(6, OP_EQ, next_router(sl, 4));
  321. tt_int_op(6, OP_EQ, next_router(sl, 5));
  322. done:
  323. smartlist_free(sl);
  324. memarea_drop_all(area);
  325. }
  326. static int
  327. base64cmp_wrapper(const char *a, const char *b)
  328. {
  329. cdline_t aa = { a, a ? (uint32_t) strlen(a) : 0 };
  330. cdline_t bb = { b, b ? (uint32_t) strlen(b) : 0 };
  331. return base64cmp(&aa, &bb);
  332. }
  333. static void
  334. test_consdiff_base64cmp(void *arg)
  335. {
  336. /* NULL arguments. */
  337. (void)arg;
  338. tt_int_op(0, OP_EQ, base64cmp_wrapper(NULL, NULL));
  339. tt_int_op(-1, OP_EQ, base64cmp_wrapper(NULL, "foo"));
  340. tt_int_op(1, OP_EQ, base64cmp_wrapper("bar", NULL));
  341. /* Nil base64 values. */
  342. tt_int_op(0, OP_EQ, base64cmp_wrapper("", ""));
  343. tt_int_op(0, OP_EQ, base64cmp_wrapper("_", "&"));
  344. /* Exact same valid strings. */
  345. tt_int_op(0, OP_EQ, base64cmp_wrapper("abcABC/+", "abcABC/+"));
  346. /* Both end with an invalid base64 char other than '\0'. */
  347. tt_int_op(0, OP_EQ, base64cmp_wrapper("abcABC/+ ", "abcABC/+ "));
  348. /* Only one ends with an invalid base64 char other than '\0'. */
  349. tt_int_op(-1, OP_EQ, base64cmp_wrapper("abcABC/+ ", "abcABC/+a"));
  350. /* Comparisons that would return differently with strcmp(). */
  351. tt_int_op(-1, OP_EQ, strcmp("/foo", "Afoo"));
  352. tt_int_op(1, OP_EQ, base64cmp_wrapper("/foo", "Afoo"));
  353. tt_int_op(1, OP_EQ, strcmp("Afoo", "0foo"));
  354. tt_int_op(-1, OP_EQ, base64cmp_wrapper("Afoo", "0foo"));
  355. /* Comparisons that would return the same as with strcmp(). */
  356. tt_int_op(1, OP_EQ, strcmp("afoo", "Afoo"));
  357. tt_int_op(1, OP_EQ, base64cmp_wrapper("afoo", "Afoo"));
  358. /* Different lengths */
  359. tt_int_op(-1, OP_EQ, base64cmp_wrapper("afoo", "afooo"));
  360. tt_int_op(1, OP_EQ, base64cmp_wrapper("afooo", "afoo"));
  361. done:
  362. ;
  363. }
  364. static void
  365. test_consdiff_gen_ed_diff(void *arg)
  366. {
  367. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  368. int i;
  369. memarea_t *area = memarea_new();
  370. setup_capture_of_logs(LOG_WARN);
  371. (void)arg;
  372. cons1 = smartlist_new();
  373. cons2 = smartlist_new();
  374. /* Identity hashes are not sorted properly, return NULL. */
  375. smartlist_add_linecpy(cons1, area, "r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  376. smartlist_add_linecpy(cons1, area, "foo");
  377. smartlist_add_linecpy(cons1, area, "r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  378. smartlist_add_linecpy(cons1, area, "bar");
  379. smartlist_add_linecpy(cons2, area, "r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  380. smartlist_add_linecpy(cons2, area, "foo");
  381. smartlist_add_linecpy(cons2, area, "r name ccccccccccccccccccccccccccc etc");
  382. smartlist_add_linecpy(cons2, area, "bar");
  383. diff = gen_ed_diff(cons1, cons2, area);
  384. tt_ptr_op(NULL, OP_EQ, diff);
  385. expect_single_log_msg_containing("Refusing to generate consensus diff "
  386. "because the base consensus doesn't have its router entries sorted "
  387. "properly.");
  388. /* Same, but now with the second consensus. */
  389. mock_clean_saved_logs();
  390. diff = gen_ed_diff(cons2, cons1, area);
  391. tt_ptr_op(NULL, OP_EQ, diff);
  392. expect_single_log_msg_containing("Refusing to generate consensus diff "
  393. "because the target consensus doesn't have its router entries sorted "
  394. "properly.");
  395. /* Same as the two above, but with the reversed thing immediately after a
  396. match. (The code handles this differently) */
  397. smartlist_del(cons1, 0);
  398. smartlist_add_linecpy(cons1, area, "r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  399. mock_clean_saved_logs();
  400. diff = gen_ed_diff(cons1, cons2, area);
  401. tt_ptr_op(NULL, OP_EQ, diff);
  402. expect_single_log_msg_containing("Refusing to generate consensus diff "
  403. "because the base consensus doesn't have its router entries sorted "
  404. "properly.");
  405. mock_clean_saved_logs();
  406. diff = gen_ed_diff(cons2, cons1, area);
  407. tt_ptr_op(NULL, OP_EQ, diff);
  408. expect_single_log_msg_containing("Refusing to generate consensus diff "
  409. "because the target consensus doesn't have its router entries sorted "
  410. "properly.");
  411. /* Identity hashes are repeated, return NULL. */
  412. smartlist_clear(cons1);
  413. smartlist_add_linecpy(cons1, area, "r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  414. smartlist_add_linecpy(cons1, area, "foo");
  415. smartlist_add_linecpy(cons1, area, "r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  416. smartlist_add_linecpy(cons1, area, "bar");
  417. mock_clean_saved_logs();
  418. diff = gen_ed_diff(cons1, cons2, area);
  419. tt_ptr_op(NULL, OP_EQ, diff);
  420. expect_single_log_msg_containing("Refusing to generate consensus diff "
  421. "because the base consensus doesn't have its router entries sorted "
  422. "properly.");
  423. /* We have to add a line that is just a dot, return NULL. */
  424. smartlist_clear(cons1);
  425. smartlist_clear(cons2);
  426. smartlist_add_linecpy(cons1, area, "foo1");
  427. smartlist_add_linecpy(cons1, area, "foo2");
  428. smartlist_add_linecpy(cons2, area, "foo1");
  429. smartlist_add_linecpy(cons2, area, ".");
  430. smartlist_add_linecpy(cons2, area, "foo2");
  431. mock_clean_saved_logs();
  432. diff = gen_ed_diff(cons1, cons2, area);
  433. tt_ptr_op(NULL, OP_EQ, diff);
  434. expect_single_log_msg_containing("Cannot generate consensus diff "
  435. "because one of the lines to be added is \".\".");
  436. #define MAX_LINE_COUNT (10000)
  437. /* Too many lines to be fed to the quadratic-time function. */
  438. smartlist_clear(cons1);
  439. smartlist_clear(cons2);
  440. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add_linecpy(cons1, area, "a");
  441. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add_linecpy(cons1, area, "b");
  442. mock_clean_saved_logs();
  443. diff = gen_ed_diff(cons1, cons2, area);
  444. tt_ptr_op(NULL, OP_EQ, diff);
  445. expect_single_log_msg_containing("Refusing to generate consensus diff "
  446. "because we found too few common router ids.");
  447. /* We have dot lines, but they don't interfere with the script format. */
  448. smartlist_clear(cons1);
  449. smartlist_clear(cons2);
  450. smartlist_add_linecpy(cons1, area, "foo1");
  451. smartlist_add_linecpy(cons1, area, ".");
  452. smartlist_add_linecpy(cons1, area, ".");
  453. smartlist_add_linecpy(cons1, area, "foo2");
  454. smartlist_add_linecpy(cons2, area, "foo1");
  455. smartlist_add_linecpy(cons2, area, ".");
  456. smartlist_add_linecpy(cons2, area, "foo2");
  457. diff = gen_ed_diff(cons1, cons2, area);
  458. tt_ptr_op(NULL, OP_NE, diff);
  459. smartlist_free(diff);
  460. /* Empty diff tests. */
  461. smartlist_clear(cons1);
  462. smartlist_clear(cons2);
  463. diff = gen_ed_diff(cons1, cons2, area);
  464. tt_ptr_op(NULL, OP_NE, diff);
  465. tt_int_op(0, OP_EQ, smartlist_len(diff));
  466. smartlist_free(diff);
  467. smartlist_add_linecpy(cons1, area, "foo");
  468. smartlist_add_linecpy(cons1, area, "bar");
  469. smartlist_add_linecpy(cons2, area, "foo");
  470. smartlist_add_linecpy(cons2, area, "bar");
  471. diff = gen_ed_diff(cons1, cons2, area);
  472. tt_ptr_op(NULL, OP_NE, diff);
  473. tt_int_op(0, OP_EQ, smartlist_len(diff));
  474. smartlist_free(diff);
  475. /* Everything is deleted. */
  476. smartlist_clear(cons2);
  477. diff = gen_ed_diff(cons1, cons2, area);
  478. tt_ptr_op(NULL, OP_NE, diff);
  479. tt_int_op(1, OP_EQ, smartlist_len(diff));
  480. tt_str_eq_line("1,2d", smartlist_get(diff, 0));
  481. smartlist_free(diff);
  482. /* Everything is added. */
  483. diff = gen_ed_diff(cons2, cons1, area);
  484. tt_ptr_op(NULL, OP_NE, diff);
  485. tt_int_op(4, OP_EQ, smartlist_len(diff));
  486. tt_str_eq_line("0a", smartlist_get(diff, 0));
  487. tt_str_eq_line("foo", smartlist_get(diff, 1));
  488. tt_str_eq_line("bar", smartlist_get(diff, 2));
  489. tt_str_eq_line(".", smartlist_get(diff, 3));
  490. smartlist_free(diff);
  491. /* Everything is changed. */
  492. smartlist_add_linecpy(cons2, area, "foo2");
  493. smartlist_add_linecpy(cons2, area, "bar2");
  494. diff = gen_ed_diff(cons1, cons2, area);
  495. tt_ptr_op(NULL, OP_NE, diff);
  496. tt_int_op(4, OP_EQ, smartlist_len(diff));
  497. tt_str_eq_line("1,2c", smartlist_get(diff, 0));
  498. tt_str_eq_line("foo2", smartlist_get(diff, 1));
  499. tt_str_eq_line("bar2", smartlist_get(diff, 2));
  500. tt_str_eq_line(".", smartlist_get(diff, 3));
  501. smartlist_free(diff);
  502. /* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */
  503. smartlist_clear(cons1);
  504. smartlist_clear(cons2);
  505. consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area);
  506. consensus_split_lines(cons2, "A\nC\nO\nE\nU\n", area);
  507. diff = gen_ed_diff(cons1, cons2, area);
  508. tt_ptr_op(NULL, OP_NE, diff);
  509. tt_int_op(7, OP_EQ, smartlist_len(diff));
  510. tt_str_eq_line("5a", smartlist_get(diff, 0));
  511. tt_str_eq_line("U", smartlist_get(diff, 1));
  512. tt_str_eq_line(".", smartlist_get(diff, 2));
  513. tt_str_eq_line("4c", smartlist_get(diff, 3));
  514. tt_str_eq_line("O", smartlist_get(diff, 4));
  515. tt_str_eq_line(".", smartlist_get(diff, 5));
  516. tt_str_eq_line("2d", smartlist_get(diff, 6));
  517. smartlist_free(diff);
  518. smartlist_clear(cons1);
  519. smartlist_clear(cons2);
  520. consensus_split_lines(cons1, "B\n", area);
  521. consensus_split_lines(cons2, "A\nB\n", area);
  522. diff = gen_ed_diff(cons1, cons2, area);
  523. tt_ptr_op(NULL, OP_NE, diff);
  524. tt_int_op(3, OP_EQ, smartlist_len(diff));
  525. tt_str_eq_line("0a", smartlist_get(diff, 0));
  526. tt_str_eq_line("A", smartlist_get(diff, 1));
  527. tt_str_eq_line(".", smartlist_get(diff, 2));
  528. /* TODO: small real use-cases, i.e. consensuses. */
  529. done:
  530. teardown_capture_of_logs();
  531. smartlist_free(cons1);
  532. smartlist_free(cons2);
  533. smartlist_free(diff);
  534. memarea_drop_all(area);
  535. }
  536. static void
  537. test_consdiff_apply_ed_diff(void *arg)
  538. {
  539. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  540. memarea_t *area = memarea_new();
  541. (void)arg;
  542. cons1 = smartlist_new();
  543. diff = smartlist_new();
  544. setup_capture_of_logs(LOG_WARN);
  545. consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area);
  546. /* Command without range. */
  547. smartlist_add_linecpy(diff, area, "a");
  548. cons2 = apply_ed_diff(cons1, diff, 0);
  549. tt_ptr_op(NULL, OP_EQ, cons2);
  550. smartlist_clear(diff);
  551. expect_single_log_msg_containing("an ed command was missing a line number");
  552. /* Range without command. */
  553. smartlist_add_linecpy(diff, area, "1");
  554. mock_clean_saved_logs();
  555. cons2 = apply_ed_diff(cons1, diff, 0);
  556. tt_ptr_op(NULL, OP_EQ, cons2);
  557. expect_single_log_msg_containing("a line with no ed command was found");
  558. smartlist_clear(diff);
  559. /* Range without end. */
  560. smartlist_add_linecpy(diff, area, "1,");
  561. mock_clean_saved_logs();
  562. cons2 = apply_ed_diff(cons1, diff, 0);
  563. tt_ptr_op(NULL, OP_EQ, cons2);
  564. expect_single_log_msg_containing("an ed command was missing a range "
  565. "end line number.");
  566. smartlist_clear(diff);
  567. /* Incoherent ranges. */
  568. smartlist_add_linecpy(diff, area, "1,1");
  569. mock_clean_saved_logs();
  570. cons2 = apply_ed_diff(cons1, diff, 0);
  571. tt_ptr_op(NULL, OP_EQ, cons2);
  572. expect_single_log_msg_containing("an invalid range was found");
  573. smartlist_clear(diff);
  574. smartlist_add_linecpy(diff, area, "3,2");
  575. mock_clean_saved_logs();
  576. cons2 = apply_ed_diff(cons1, diff, 0);
  577. tt_ptr_op(NULL, OP_EQ, cons2);
  578. expect_single_log_msg_containing("an invalid range was found");
  579. smartlist_clear(diff);
  580. /* Unexpected range for add command. */
  581. smartlist_add_linecpy(diff, area, "1,2a");
  582. mock_clean_saved_logs();
  583. cons2 = apply_ed_diff(cons1, diff, 0);
  584. tt_ptr_op(NULL, OP_EQ, cons2);
  585. expect_single_log_msg_containing("add lines after a range");
  586. smartlist_clear(diff);
  587. /* Script is not in reverse order. */
  588. smartlist_add_linecpy(diff, area, "1d");
  589. smartlist_add_linecpy(diff, area, "3d");
  590. mock_clean_saved_logs();
  591. cons2 = apply_ed_diff(cons1, diff, 0);
  592. tt_ptr_op(NULL, OP_EQ, cons2);
  593. expect_single_log_msg_containing("its commands are not properly sorted");
  594. smartlist_clear(diff);
  595. /* Script contains unrecognised commands longer than one char. */
  596. smartlist_add_linecpy(diff, area, "1foo");
  597. mock_clean_saved_logs();
  598. cons2 = apply_ed_diff(cons1, diff, 0);
  599. tt_ptr_op(NULL, OP_EQ, cons2);
  600. expect_single_log_msg_containing("an ed command longer than one char was "
  601. "found");
  602. smartlist_clear(diff);
  603. /* Script contains unrecognised commands. */
  604. smartlist_add_linecpy(diff, area, "1e");
  605. mock_clean_saved_logs();
  606. cons2 = apply_ed_diff(cons1, diff, 0);
  607. tt_ptr_op(NULL, OP_EQ, cons2);
  608. expect_single_log_msg_containing("an unrecognised ed command was found");
  609. smartlist_clear(diff);
  610. /* Command that should be followed by at least one line and a ".", but
  611. * isn't. */
  612. smartlist_add_linecpy(diff, area, "0a");
  613. mock_clean_saved_logs();
  614. cons2 = apply_ed_diff(cons1, diff, 0);
  615. tt_ptr_op(NULL, OP_EQ, cons2);
  616. expect_single_log_msg_containing("it has an ed command that tries to "
  617. "insert zero lines.");
  618. /* Now it is followed by a ".", but it inserts zero lines. */
  619. smartlist_add_linecpy(diff, area, ".");
  620. mock_clean_saved_logs();
  621. cons2 = apply_ed_diff(cons1, diff, 0);
  622. tt_ptr_op(NULL, OP_EQ, cons2);
  623. expect_single_log_msg_containing("it has an ed command that tries to "
  624. "insert zero lines.");
  625. smartlist_clear(diff);
  626. /* Now it it inserts something, but has no terminator. */
  627. smartlist_add_linecpy(diff, area, "0a");
  628. smartlist_add_linecpy(diff, area, "hello");
  629. mock_clean_saved_logs();
  630. cons2 = apply_ed_diff(cons1, diff, 0);
  631. tt_ptr_op(NULL, OP_EQ, cons2);
  632. expect_single_log_msg_containing("lines to be inserted that don't end with "
  633. "a \".\".");
  634. smartlist_clear(diff);
  635. /* Test appending text, 'a'. */
  636. consensus_split_lines(diff, "3a\nU\nO\n.\n0a\nV\n.\n", area);
  637. cons2 = apply_ed_diff(cons1, diff, 0);
  638. tt_ptr_op(NULL, OP_NE, cons2);
  639. tt_int_op(8, OP_EQ, smartlist_len(cons2));
  640. tt_str_eq_line("V", smartlist_get(cons2, 0));
  641. tt_str_eq_line("A", smartlist_get(cons2, 1));
  642. tt_str_eq_line("B", smartlist_get(cons2, 2));
  643. tt_str_eq_line("C", smartlist_get(cons2, 3));
  644. tt_str_eq_line("U", smartlist_get(cons2, 4));
  645. tt_str_eq_line("O", smartlist_get(cons2, 5));
  646. tt_str_eq_line("D", smartlist_get(cons2, 6));
  647. tt_str_eq_line("E", smartlist_get(cons2, 7));
  648. smartlist_clear(diff);
  649. smartlist_free(cons2);
  650. /* Test deleting text, 'd'. */
  651. consensus_split_lines(diff, "4d\n1,2d\n", area);
  652. cons2 = apply_ed_diff(cons1, diff, 0);
  653. tt_ptr_op(NULL, OP_NE, cons2);
  654. tt_int_op(2, OP_EQ, smartlist_len(cons2));
  655. tt_str_eq_line("C", smartlist_get(cons2, 0));
  656. tt_str_eq_line("E", smartlist_get(cons2, 1));
  657. smartlist_clear(diff);
  658. smartlist_free(cons2);
  659. /* Test changing text, 'c'. */
  660. consensus_split_lines(diff, "4c\nT\nX\n.\n1, 2c\nM\n.\n", area);
  661. cons2 = apply_ed_diff(cons1, diff, 0);
  662. tt_ptr_op(NULL, OP_NE, cons2);
  663. tt_int_op(5, OP_EQ, smartlist_len(cons2));
  664. tt_str_eq_line("M", smartlist_get(cons2, 0));
  665. tt_str_eq_line("C", smartlist_get(cons2, 1));
  666. tt_str_eq_line("T", smartlist_get(cons2, 2));
  667. tt_str_eq_line("X", smartlist_get(cons2, 3));
  668. tt_str_eq_line("E", smartlist_get(cons2, 4));
  669. smartlist_clear(diff);
  670. smartlist_free(cons2);
  671. /* Test 'a', 'd' and 'c' together. */
  672. consensus_split_lines(diff, "4c\nT\nX\n.\n2d\n0a\nM\n.\n", area);
  673. cons2 = apply_ed_diff(cons1, diff, 0);
  674. tt_ptr_op(NULL, OP_NE, cons2);
  675. tt_int_op(6, OP_EQ, smartlist_len(cons2));
  676. tt_str_eq_line("M", smartlist_get(cons2, 0));
  677. tt_str_eq_line("A", smartlist_get(cons2, 1));
  678. tt_str_eq_line("C", smartlist_get(cons2, 2));
  679. tt_str_eq_line("T", smartlist_get(cons2, 3));
  680. tt_str_eq_line("X", smartlist_get(cons2, 4));
  681. tt_str_eq_line("E", smartlist_get(cons2, 5));
  682. done:
  683. teardown_capture_of_logs();
  684. smartlist_free(cons1);
  685. smartlist_free(cons2);
  686. smartlist_free(diff);
  687. memarea_drop_all(area);
  688. }
  689. static void
  690. test_consdiff_gen_diff(void *arg)
  691. {
  692. char *cons1_str=NULL, *cons2_str=NULL;
  693. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  694. consensus_digest_t digests1, digests2;
  695. memarea_t *area = memarea_new();
  696. (void)arg;
  697. cons1 = smartlist_new();
  698. cons2 = smartlist_new();
  699. /* Identity hashes are not sorted properly, return NULL.
  700. * Already tested in gen_ed_diff, but see that a NULL ed diff also makes
  701. * gen_diff return NULL. */
  702. cons1_str = tor_strdup(
  703. "network-status-version foo\n"
  704. "r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
  705. "r name aaaaaaaaaaaaaaaaa etc\nbar\n"
  706. "directory-signature foo bar\nbar\n"
  707. );
  708. cons2_str = tor_strdup(
  709. "network-status-version foo\n"
  710. "r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
  711. "r name ccccccccccccccccc etc\nbar\n"
  712. "directory-signature foo bar\nbar\n"
  713. );
  714. tt_int_op(0, OP_EQ,
  715. consensus_compute_digest(cons1_str, &digests1));
  716. tt_int_op(0, OP_EQ,
  717. consensus_compute_digest(cons2_str, &digests2));
  718. consensus_split_lines(cons1, cons1_str, area);
  719. consensus_split_lines(cons2, cons2_str, area);
  720. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  721. tt_ptr_op(NULL, OP_EQ, diff);
  722. /* Check that the headers are done properly. */
  723. tor_free(cons1_str);
  724. cons1_str = tor_strdup(
  725. "network-status-version foo\n"
  726. "r name ccccccccccccccccc etc\nfoo\n"
  727. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  728. "directory-signature foo bar\nbar\n"
  729. );
  730. tt_int_op(0, OP_EQ,
  731. consensus_compute_digest(cons1_str, &digests1));
  732. smartlist_clear(cons1);
  733. consensus_split_lines(cons1, cons1_str, area);
  734. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  735. tt_ptr_op(NULL, OP_NE, diff);
  736. tt_int_op(7, OP_EQ, smartlist_len(diff));
  737. tt_assert(line_str_eq(smartlist_get(diff, 0),
  738. "network-status-diff-version 1"));
  739. tt_assert(line_str_eq(smartlist_get(diff, 1), "hash "
  740. "06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4 "
  741. "7AFECEFA4599BA33D603653E3D2368F648DF4AC4723929B0F7CF39281596B0C1"));
  742. tt_assert(line_str_eq(smartlist_get(diff, 2), "3,4d"));
  743. tt_assert(line_str_eq(smartlist_get(diff, 3), "1a"));
  744. tt_assert(line_str_eq(smartlist_get(diff, 4),
  745. "r name aaaaaaaaaaaaaaaaa etc"));
  746. tt_assert(line_str_eq(smartlist_get(diff, 5), "foo"));
  747. tt_assert(line_str_eq(smartlist_get(diff, 6), "."));
  748. /* TODO: small real use-cases, i.e. consensuses. */
  749. done:
  750. tor_free(cons1_str);
  751. tor_free(cons2_str);
  752. smartlist_free(cons1);
  753. smartlist_free(cons2);
  754. smartlist_free(diff);
  755. memarea_drop_all(area);
  756. }
  757. static void
  758. test_consdiff_apply_diff(void *arg)
  759. {
  760. smartlist_t *cons1=NULL, *diff=NULL;
  761. char *cons1_str=NULL, *cons2 = NULL;
  762. consensus_digest_t digests1;
  763. (void)arg;
  764. memarea_t *area = memarea_new();
  765. cons1 = smartlist_new();
  766. diff = smartlist_new();
  767. setup_capture_of_logs(LOG_INFO);
  768. cons1_str = tor_strdup(
  769. "network-status-version foo\n"
  770. "r name ccccccccccccccccc etc\nfoo\n"
  771. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  772. "directory-signature foo bar\nbar\n"
  773. );
  774. tt_int_op(0, OP_EQ,
  775. consensus_compute_digest(cons1_str, &digests1));
  776. consensus_split_lines(cons1, cons1_str, area);
  777. /* diff doesn't have enough lines. */
  778. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  779. tt_ptr_op(NULL, OP_EQ, cons2);
  780. expect_single_log_msg_containing("too short")
  781. /* first line doesn't match format-version string. */
  782. smartlist_add_linecpy(diff, area, "foo-bar");
  783. smartlist_add_linecpy(diff, area, "header-line");
  784. mock_clean_saved_logs();
  785. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  786. tt_ptr_op(NULL, OP_EQ, cons2);
  787. expect_single_log_msg_containing("format is not known")
  788. /* The first word of the second header line is not "hash". */
  789. smartlist_clear(diff);
  790. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  791. smartlist_add_linecpy(diff, area, "word a b");
  792. smartlist_add_linecpy(diff, area, "x");
  793. mock_clean_saved_logs();
  794. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  795. tt_ptr_op(NULL, OP_EQ, cons2);
  796. expect_single_log_msg_containing("does not include the necessary digests")
  797. /* Wrong number of words after "hash". */
  798. smartlist_clear(diff);
  799. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  800. smartlist_add_linecpy(diff, area, "hash a b c");
  801. mock_clean_saved_logs();
  802. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  803. tt_ptr_op(NULL, OP_EQ, cons2);
  804. expect_single_log_msg_containing("does not include the necessary digests")
  805. /* base16 digests do not have the expected length. */
  806. smartlist_clear(diff);
  807. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  808. smartlist_add_linecpy(diff, area, "hash aaa bbb");
  809. mock_clean_saved_logs();
  810. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  811. tt_ptr_op(NULL, OP_EQ, cons2);
  812. expect_single_log_msg_containing("includes base16-encoded digests of "
  813. "incorrect size")
  814. /* base16 digests contain non-base16 characters. */
  815. smartlist_clear(diff);
  816. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  817. smartlist_add_linecpy(diff, area, "hash"
  818. " ????????????????????????????????????????????????????????????????"
  819. " ----------------------------------------------------------------");
  820. mock_clean_saved_logs();
  821. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  822. tt_ptr_op(NULL, OP_EQ, cons2);
  823. expect_single_log_msg_containing("includes malformed digests")
  824. /* Invalid ed diff.
  825. * As tested in apply_ed_diff, but check that apply_diff does return NULL if
  826. * the ed diff can't be applied. */
  827. smartlist_clear(diff);
  828. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  829. smartlist_add_linecpy(diff, area, "hash"
  830. /* sha3 of cons1. */
  831. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  832. /* sha256 of cons2. */
  833. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  834. smartlist_add_linecpy(diff, area, "foobar");
  835. mock_clean_saved_logs();
  836. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  837. tt_ptr_op(NULL, OP_EQ, cons2);
  838. expect_single_log_msg_containing("because an ed command was missing a line "
  839. "number")
  840. /* Base consensus doesn't match its digest as found in the diff. */
  841. smartlist_clear(diff);
  842. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  843. smartlist_add_linecpy(diff, area, "hash"
  844. /* bogus sha256. */
  845. " 3333333333333333333333333333333333333333333333333333333333333333"
  846. /* sha256 of cons2. */
  847. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  848. mock_clean_saved_logs();
  849. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  850. tt_ptr_op(NULL, OP_EQ, cons2);
  851. expect_log_msg_containing("base consensus doesn't match the digest "
  852. "as found");
  853. /* Resulting consensus doesn't match its digest as found in the diff. */
  854. smartlist_clear(diff);
  855. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  856. smartlist_add_linecpy(diff, area, "hash"
  857. /* sha3 of cons1. */
  858. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  859. /* bogus sha3. */
  860. " 3333333333333333333333333333333333333333333333333333333333333333");
  861. mock_clean_saved_logs();
  862. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  863. tt_ptr_op(NULL, OP_EQ, cons2);
  864. expect_log_msg_containing("resulting consensus doesn't match the "
  865. "digest as found");
  866. #if 0
  867. /* XXXX No longer possible, since we aren't using the other algorithm. */
  868. /* Resulting consensus digest cannot be computed */
  869. smartlist_clear(diff);
  870. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  871. smartlist_add_linecpy(diff, area, "hash"
  872. /* sha3 of cons1. */
  873. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  874. /* bogus sha3. */
  875. " 3333333333333333333333333333333333333333333333333333333333333333");
  876. smartlist_add_linecpy(diff, area, "1,2d"); // remove starting line
  877. mock_clean_saved_logs();
  878. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  879. tt_ptr_op(NULL, OP_EQ, cons2);
  880. expect_log_msg_containing("Could not compute digests of the consensus "
  881. "resulting from applying a consensus diff.");
  882. #endif
  883. /* Very simple test, only to see that nothing errors. */
  884. smartlist_clear(diff);
  885. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  886. smartlist_add_linecpy(diff, area, "hash"
  887. /* sha3 of cons1. */
  888. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  889. /* sha3 of cons2. */
  890. " 90A418881B2FCAB3D9E60EE02E4D666D56CFA38F8A3B7AA3E0ADBA530DDA9353");
  891. smartlist_add_linecpy(diff, area, "3c");
  892. smartlist_add_linecpy(diff, area, "sample");
  893. smartlist_add_linecpy(diff, area, ".");
  894. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  895. tt_ptr_op(NULL, OP_NE, cons2);
  896. tt_str_op(
  897. "network-status-version foo\n"
  898. "r name ccccccccccccccccc etc\nsample\n"
  899. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  900. "directory-signature foo bar\nbar\n", OP_EQ,
  901. cons2);
  902. tor_free(cons2);
  903. /* Check that lowercase letters in base16-encoded digests work too. */
  904. smartlist_clear(diff);
  905. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  906. smartlist_add_linecpy(diff, area, "hash"
  907. /* sha3 of cons1. */
  908. " 06646d6cf563a41869d3b02e73254372ae3140046c5e7d83c9f71e54976af9b4"
  909. /* sha3 of cons2. */
  910. " 90a418881b2fcab3d9e60ee02e4d666d56cfa38f8a3b7aa3e0adba530dda9353");
  911. smartlist_add_linecpy(diff, area, "3c");
  912. smartlist_add_linecpy(diff, area, "sample");
  913. smartlist_add_linecpy(diff, area, ".");
  914. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  915. tt_ptr_op(NULL, OP_NE, cons2);
  916. tt_str_op(
  917. "network-status-version foo\n"
  918. "r name ccccccccccccccccc etc\nsample\n"
  919. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  920. "directory-signature foo bar\nbar\n", OP_EQ,
  921. cons2);
  922. tor_free(cons2);
  923. smartlist_clear(diff);
  924. done:
  925. teardown_capture_of_logs();
  926. tor_free(cons1_str);
  927. smartlist_free(cons1);
  928. smartlist_free(diff);
  929. memarea_drop_all(area);
  930. }
  931. #define CONSDIFF_LEGACY(name) \
  932. { #name, test_consdiff_ ## name , 0, NULL, NULL }
  933. struct testcase_t consdiff_tests[] = {
  934. CONSDIFF_LEGACY(smartlist_slice),
  935. CONSDIFF_LEGACY(smartlist_slice_string_pos),
  936. CONSDIFF_LEGACY(lcs_lengths),
  937. CONSDIFF_LEGACY(trim_slices),
  938. CONSDIFF_LEGACY(set_changed),
  939. CONSDIFF_LEGACY(calc_changes),
  940. CONSDIFF_LEGACY(get_id_hash),
  941. CONSDIFF_LEGACY(is_valid_router_entry),
  942. CONSDIFF_LEGACY(next_router),
  943. CONSDIFF_LEGACY(base64cmp),
  944. CONSDIFF_LEGACY(gen_ed_diff),
  945. CONSDIFF_LEGACY(apply_ed_diff),
  946. CONSDIFF_LEGACY(gen_diff),
  947. CONSDIFF_LEGACY(apply_diff),
  948. END_OF_TESTCASES
  949. };