test_consdiff.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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_clear(cons1);
  518. smartlist_clear(cons2);
  519. consensus_split_lines(cons1, "B\n", area);
  520. consensus_split_lines(cons2, "A\nB\n", area);
  521. diff = gen_ed_diff(cons1, cons2, area);
  522. tt_ptr_op(NULL, OP_NE, diff);
  523. tt_int_op(3, OP_EQ, smartlist_len(diff));
  524. tt_str_eq_line("0a", smartlist_get(diff, 0));
  525. tt_str_eq_line("A", smartlist_get(diff, 1));
  526. tt_str_eq_line(".", smartlist_get(diff, 2));
  527. /* TODO: small real use-cases, i.e. consensuses. */
  528. done:
  529. teardown_capture_of_logs();
  530. smartlist_free(cons1);
  531. smartlist_free(cons2);
  532. smartlist_free(diff);
  533. memarea_drop_all(area);
  534. }
  535. static void
  536. test_consdiff_apply_ed_diff(void *arg)
  537. {
  538. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  539. memarea_t *area = memarea_new();
  540. (void)arg;
  541. cons1 = smartlist_new();
  542. diff = smartlist_new();
  543. setup_capture_of_logs(LOG_WARN);
  544. consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area);
  545. /* Command without range. */
  546. smartlist_add_linecpy(diff, area, "a");
  547. cons2 = apply_ed_diff(cons1, diff, 0);
  548. tt_ptr_op(NULL, OP_EQ, cons2);
  549. smartlist_clear(diff);
  550. expect_single_log_msg_containing("an ed command was missing a line number");
  551. /* Range without command. */
  552. smartlist_add_linecpy(diff, area, "1");
  553. mock_clean_saved_logs();
  554. cons2 = apply_ed_diff(cons1, diff, 0);
  555. tt_ptr_op(NULL, OP_EQ, cons2);
  556. expect_single_log_msg_containing("a line with no ed command was found");
  557. smartlist_clear(diff);
  558. /* Range without end. */
  559. smartlist_add_linecpy(diff, area, "1,");
  560. mock_clean_saved_logs();
  561. cons2 = apply_ed_diff(cons1, diff, 0);
  562. tt_ptr_op(NULL, OP_EQ, cons2);
  563. expect_single_log_msg_containing("an ed command was missing a range "
  564. "end line number.");
  565. smartlist_clear(diff);
  566. /* Incoherent ranges. */
  567. smartlist_add_linecpy(diff, area, "1,1");
  568. mock_clean_saved_logs();
  569. cons2 = apply_ed_diff(cons1, diff, 0);
  570. tt_ptr_op(NULL, OP_EQ, cons2);
  571. expect_single_log_msg_containing("an invalid range was found");
  572. smartlist_clear(diff);
  573. smartlist_add_linecpy(diff, area, "3,2");
  574. mock_clean_saved_logs();
  575. cons2 = apply_ed_diff(cons1, diff, 0);
  576. tt_ptr_op(NULL, OP_EQ, cons2);
  577. expect_single_log_msg_containing("an invalid range was found");
  578. smartlist_clear(diff);
  579. /* Script is not in reverse order. */
  580. smartlist_add_linecpy(diff, area, "1d");
  581. smartlist_add_linecpy(diff, area, "3d");
  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("its commands are not properly sorted");
  586. smartlist_clear(diff);
  587. /* Script contains unrecognised commands longer than one char. */
  588. smartlist_add_linecpy(diff, area, "1foo");
  589. mock_clean_saved_logs();
  590. cons2 = apply_ed_diff(cons1, diff, 0);
  591. tt_ptr_op(NULL, OP_EQ, cons2);
  592. expect_single_log_msg_containing("an ed command longer than one char was "
  593. "found");
  594. smartlist_clear(diff);
  595. /* Script contains unrecognised commands. */
  596. smartlist_add_linecpy(diff, area, "1e");
  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 unrecognised ed command was found");
  601. smartlist_clear(diff);
  602. /* Command that should be followed by at least one line and a ".", but
  603. * isn't. */
  604. smartlist_add_linecpy(diff, area, "0a");
  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("it has an ed command that tries to "
  609. "insert zero lines.");
  610. /* Now it is followed by a ".", but it inserts zero lines. */
  611. smartlist_add_linecpy(diff, area, ".");
  612. mock_clean_saved_logs();
  613. cons2 = apply_ed_diff(cons1, diff, 0);
  614. tt_ptr_op(NULL, OP_EQ, cons2);
  615. expect_single_log_msg_containing("it has an ed command that tries to "
  616. "insert zero lines.");
  617. smartlist_clear(diff);
  618. /* Now it it inserts something, but has no terminator. */
  619. smartlist_add_linecpy(diff, area, "0a");
  620. smartlist_add_linecpy(diff, area, "hello");
  621. mock_clean_saved_logs();
  622. cons2 = apply_ed_diff(cons1, diff, 0);
  623. tt_ptr_op(NULL, OP_EQ, cons2);
  624. expect_single_log_msg_containing("lines to be inserted that don't end with "
  625. "a \".\".");
  626. smartlist_clear(diff);
  627. /* Test appending text, 'a'. */
  628. consensus_split_lines(diff, "3a\nU\nO\n.\n0a\nV\n.\n", area);
  629. cons2 = apply_ed_diff(cons1, diff, 0);
  630. tt_ptr_op(NULL, OP_NE, cons2);
  631. tt_int_op(8, OP_EQ, smartlist_len(cons2));
  632. tt_str_eq_line("V", smartlist_get(cons2, 0));
  633. tt_str_eq_line("A", smartlist_get(cons2, 1));
  634. tt_str_eq_line("B", smartlist_get(cons2, 2));
  635. tt_str_eq_line("C", smartlist_get(cons2, 3));
  636. tt_str_eq_line("U", smartlist_get(cons2, 4));
  637. tt_str_eq_line("O", smartlist_get(cons2, 5));
  638. tt_str_eq_line("D", smartlist_get(cons2, 6));
  639. tt_str_eq_line("E", smartlist_get(cons2, 7));
  640. smartlist_clear(diff);
  641. smartlist_free(cons2);
  642. /* Test deleting text, 'd'. */
  643. consensus_split_lines(diff, "4d\n1,2d\n", area);
  644. cons2 = apply_ed_diff(cons1, diff, 0);
  645. tt_ptr_op(NULL, OP_NE, cons2);
  646. tt_int_op(2, OP_EQ, smartlist_len(cons2));
  647. tt_str_eq_line("C", smartlist_get(cons2, 0));
  648. tt_str_eq_line("E", smartlist_get(cons2, 1));
  649. smartlist_clear(diff);
  650. smartlist_free(cons2);
  651. /* Test changing text, 'c'. */
  652. consensus_split_lines(diff, "4c\nT\nX\n.\n1, 2c\nM\n.\n", area);
  653. cons2 = apply_ed_diff(cons1, diff, 0);
  654. tt_ptr_op(NULL, OP_NE, cons2);
  655. tt_int_op(5, OP_EQ, smartlist_len(cons2));
  656. tt_str_eq_line("M", smartlist_get(cons2, 0));
  657. tt_str_eq_line("C", smartlist_get(cons2, 1));
  658. tt_str_eq_line("T", smartlist_get(cons2, 2));
  659. tt_str_eq_line("X", smartlist_get(cons2, 3));
  660. tt_str_eq_line("E", smartlist_get(cons2, 4));
  661. smartlist_clear(diff);
  662. smartlist_free(cons2);
  663. /* Test 'a', 'd' and 'c' together. */
  664. consensus_split_lines(diff, "4c\nT\nX\n.\n2d\n0a\nM\n.\n", area);
  665. cons2 = apply_ed_diff(cons1, diff, 0);
  666. tt_ptr_op(NULL, OP_NE, cons2);
  667. tt_int_op(6, OP_EQ, smartlist_len(cons2));
  668. tt_str_eq_line("M", smartlist_get(cons2, 0));
  669. tt_str_eq_line("A", smartlist_get(cons2, 1));
  670. tt_str_eq_line("C", smartlist_get(cons2, 2));
  671. tt_str_eq_line("T", smartlist_get(cons2, 3));
  672. tt_str_eq_line("X", smartlist_get(cons2, 4));
  673. tt_str_eq_line("E", smartlist_get(cons2, 5));
  674. done:
  675. teardown_capture_of_logs();
  676. smartlist_free(cons1);
  677. smartlist_free(cons2);
  678. smartlist_free(diff);
  679. memarea_drop_all(area);
  680. }
  681. static void
  682. test_consdiff_gen_diff(void *arg)
  683. {
  684. char *cons1_str=NULL, *cons2_str=NULL;
  685. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  686. consensus_digest_t digests1, digests2;
  687. memarea_t *area = memarea_new();
  688. (void)arg;
  689. cons1 = smartlist_new();
  690. cons2 = smartlist_new();
  691. /* Identity hashes are not sorted properly, return NULL.
  692. * Already tested in gen_ed_diff, but see that a NULL ed diff also makes
  693. * gen_diff return NULL. */
  694. cons1_str = tor_strdup(
  695. "network-status-version foo\n"
  696. "r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
  697. "r name aaaaaaaaaaaaaaaaa etc\nbar\n"
  698. "directory-signature foo bar\nbar\n"
  699. );
  700. cons2_str = tor_strdup(
  701. "network-status-version foo\n"
  702. "r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
  703. "r name ccccccccccccccccc etc\nbar\n"
  704. "directory-signature foo bar\nbar\n"
  705. );
  706. tt_int_op(0, OP_EQ,
  707. consensus_compute_digest(cons1_str, &digests1));
  708. tt_int_op(0, OP_EQ,
  709. consensus_compute_digest(cons2_str, &digests2));
  710. consensus_split_lines(cons1, cons1_str, area);
  711. consensus_split_lines(cons2, cons2_str, area);
  712. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  713. tt_ptr_op(NULL, OP_EQ, diff);
  714. /* Check that the headers are done properly. */
  715. tor_free(cons1_str);
  716. cons1_str = tor_strdup(
  717. "network-status-version foo\n"
  718. "r name ccccccccccccccccc etc\nfoo\n"
  719. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  720. "directory-signature foo bar\nbar\n"
  721. );
  722. tt_int_op(0, OP_EQ,
  723. consensus_compute_digest(cons1_str, &digests1));
  724. smartlist_clear(cons1);
  725. consensus_split_lines(cons1, cons1_str, area);
  726. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  727. tt_ptr_op(NULL, OP_NE, diff);
  728. tt_int_op(7, OP_EQ, smartlist_len(diff));
  729. tt_assert(line_str_eq(smartlist_get(diff, 0),
  730. "network-status-diff-version 1"));
  731. tt_assert(line_str_eq(smartlist_get(diff, 1), "hash "
  732. "06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4 "
  733. "7AFECEFA4599BA33D603653E3D2368F648DF4AC4723929B0F7CF39281596B0C1"));
  734. tt_assert(line_str_eq(smartlist_get(diff, 2), "3,4d"));
  735. tt_assert(line_str_eq(smartlist_get(diff, 3), "1a"));
  736. tt_assert(line_str_eq(smartlist_get(diff, 4),
  737. "r name aaaaaaaaaaaaaaaaa etc"));
  738. tt_assert(line_str_eq(smartlist_get(diff, 5), "foo"));
  739. tt_assert(line_str_eq(smartlist_get(diff, 6), "."));
  740. /* TODO: small real use-cases, i.e. consensuses. */
  741. done:
  742. tor_free(cons1_str);
  743. tor_free(cons2_str);
  744. smartlist_free(cons1);
  745. smartlist_free(cons2);
  746. smartlist_free(diff);
  747. memarea_drop_all(area);
  748. }
  749. static void
  750. test_consdiff_apply_diff(void *arg)
  751. {
  752. smartlist_t *cons1=NULL, *diff=NULL;
  753. char *cons1_str=NULL, *cons2 = NULL;
  754. consensus_digest_t digests1;
  755. (void)arg;
  756. memarea_t *area = memarea_new();
  757. cons1 = smartlist_new();
  758. diff = smartlist_new();
  759. setup_capture_of_logs(LOG_INFO);
  760. cons1_str = tor_strdup(
  761. "network-status-version foo\n"
  762. "r name ccccccccccccccccc etc\nfoo\n"
  763. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  764. "directory-signature foo bar\nbar\n"
  765. );
  766. tt_int_op(0, OP_EQ,
  767. consensus_compute_digest(cons1_str, &digests1));
  768. consensus_split_lines(cons1, cons1_str, area);
  769. /* diff doesn't have enough lines. */
  770. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  771. tt_ptr_op(NULL, OP_EQ, cons2);
  772. expect_single_log_msg_containing("too short")
  773. /* first line doesn't match format-version string. */
  774. smartlist_add_linecpy(diff, area, "foo-bar");
  775. smartlist_add_linecpy(diff, area, "header-line");
  776. mock_clean_saved_logs();
  777. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  778. tt_ptr_op(NULL, OP_EQ, cons2);
  779. expect_single_log_msg_containing("format is not known")
  780. /* The first word of the second header line is not "hash". */
  781. smartlist_clear(diff);
  782. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  783. smartlist_add_linecpy(diff, area, "word a b");
  784. smartlist_add_linecpy(diff, area, "x");
  785. mock_clean_saved_logs();
  786. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  787. tt_ptr_op(NULL, OP_EQ, cons2);
  788. expect_single_log_msg_containing("does not include the necessary digests")
  789. /* Wrong number of words after "hash". */
  790. smartlist_clear(diff);
  791. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  792. smartlist_add_linecpy(diff, area, "hash a b c");
  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. /* base16 digests do not have the expected length. */
  798. smartlist_clear(diff);
  799. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  800. smartlist_add_linecpy(diff, area, "hash aaa bbb");
  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("includes base16-encoded digests of "
  805. "incorrect size")
  806. /* base16 digests contain non-base16 characters. */
  807. smartlist_clear(diff);
  808. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  809. smartlist_add_linecpy(diff, area, "hash"
  810. " ????????????????????????????????????????????????????????????????"
  811. " ----------------------------------------------------------------");
  812. mock_clean_saved_logs();
  813. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  814. tt_ptr_op(NULL, OP_EQ, cons2);
  815. expect_single_log_msg_containing("includes malformed digests")
  816. /* Invalid ed diff.
  817. * As tested in apply_ed_diff, but check that apply_diff does return NULL if
  818. * the ed diff can't be applied. */
  819. smartlist_clear(diff);
  820. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  821. smartlist_add_linecpy(diff, area, "hash"
  822. /* sha3 of cons1. */
  823. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  824. /* sha256 of cons2. */
  825. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  826. smartlist_add_linecpy(diff, area, "foobar");
  827. mock_clean_saved_logs();
  828. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  829. tt_ptr_op(NULL, OP_EQ, cons2);
  830. expect_single_log_msg_containing("because an ed command was missing a line "
  831. "number")
  832. /* Base consensus doesn't match its digest as found in the diff. */
  833. smartlist_clear(diff);
  834. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  835. smartlist_add_linecpy(diff, area, "hash"
  836. /* bogus sha256. */
  837. " 3333333333333333333333333333333333333333333333333333333333333333"
  838. /* sha256 of cons2. */
  839. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  840. mock_clean_saved_logs();
  841. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  842. tt_ptr_op(NULL, OP_EQ, cons2);
  843. expect_log_msg_containing("base consensus doesn't match the digest "
  844. "as found");
  845. /* Resulting consensus doesn't match its digest as found in the diff. */
  846. smartlist_clear(diff);
  847. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  848. smartlist_add_linecpy(diff, area, "hash"
  849. /* sha3 of cons1. */
  850. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  851. /* bogus sha3. */
  852. " 3333333333333333333333333333333333333333333333333333333333333333");
  853. mock_clean_saved_logs();
  854. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  855. tt_ptr_op(NULL, OP_EQ, cons2);
  856. expect_log_msg_containing("resulting consensus doesn't match the "
  857. "digest as found");
  858. #if 0
  859. /* XXXX No longer possible, since we aren't using the other algorithm. */
  860. /* Resulting consensus digest cannot be computed */
  861. smartlist_clear(diff);
  862. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  863. smartlist_add_linecpy(diff, area, "hash"
  864. /* sha3 of cons1. */
  865. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  866. /* bogus sha3. */
  867. " 3333333333333333333333333333333333333333333333333333333333333333");
  868. smartlist_add_linecpy(diff, area, "1,2d"); // remove starting line
  869. mock_clean_saved_logs();
  870. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  871. tt_ptr_op(NULL, OP_EQ, cons2);
  872. expect_log_msg_containing("Could not compute digests of the consensus "
  873. "resulting from applying a consensus diff.");
  874. #endif
  875. /* Very simple test, only to see that nothing errors. */
  876. smartlist_clear(diff);
  877. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  878. smartlist_add_linecpy(diff, area, "hash"
  879. /* sha3 of cons1. */
  880. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  881. /* sha3 of cons2. */
  882. " 90A418881B2FCAB3D9E60EE02E4D666D56CFA38F8A3B7AA3E0ADBA530DDA9353");
  883. smartlist_add_linecpy(diff, area, "3c");
  884. smartlist_add_linecpy(diff, area, "sample");
  885. smartlist_add_linecpy(diff, area, ".");
  886. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  887. tt_ptr_op(NULL, OP_NE, cons2);
  888. tt_str_op(
  889. "network-status-version foo\n"
  890. "r name ccccccccccccccccc etc\nsample\n"
  891. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  892. "directory-signature foo bar\nbar\n", OP_EQ,
  893. cons2);
  894. tor_free(cons2);
  895. /* Check that lowercase letters in base16-encoded digests work too. */
  896. smartlist_clear(diff);
  897. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  898. smartlist_add_linecpy(diff, area, "hash"
  899. /* sha3 of cons1. */
  900. " 06646d6cf563a41869d3b02e73254372ae3140046c5e7d83c9f71e54976af9b4"
  901. /* sha3 of cons2. */
  902. " 90a418881b2fcab3d9e60ee02e4d666d56cfa38f8a3b7aa3e0adba530dda9353");
  903. smartlist_add_linecpy(diff, area, "3c");
  904. smartlist_add_linecpy(diff, area, "sample");
  905. smartlist_add_linecpy(diff, area, ".");
  906. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  907. tt_ptr_op(NULL, OP_NE, cons2);
  908. tt_str_op(
  909. "network-status-version foo\n"
  910. "r name ccccccccccccccccc etc\nsample\n"
  911. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  912. "directory-signature foo bar\nbar\n", OP_EQ,
  913. cons2);
  914. tor_free(cons2);
  915. smartlist_clear(diff);
  916. done:
  917. teardown_capture_of_logs();
  918. tor_free(cons1_str);
  919. smartlist_free(cons1);
  920. smartlist_free(diff);
  921. memarea_drop_all(area);
  922. }
  923. #define CONSDIFF_LEGACY(name) \
  924. { #name, test_consdiff_ ## name , 0, NULL, NULL }
  925. struct testcase_t consdiff_tests[] = {
  926. CONSDIFF_LEGACY(smartlist_slice),
  927. CONSDIFF_LEGACY(smartlist_slice_string_pos),
  928. CONSDIFF_LEGACY(lcs_lengths),
  929. CONSDIFF_LEGACY(trim_slices),
  930. CONSDIFF_LEGACY(set_changed),
  931. CONSDIFF_LEGACY(calc_changes),
  932. CONSDIFF_LEGACY(get_id_hash),
  933. CONSDIFF_LEGACY(is_valid_router_entry),
  934. CONSDIFF_LEGACY(next_router),
  935. CONSDIFF_LEGACY(base64cmp),
  936. CONSDIFF_LEGACY(gen_ed_diff),
  937. CONSDIFF_LEGACY(apply_ed_diff),
  938. CONSDIFF_LEGACY(gen_diff),
  939. CONSDIFF_LEGACY(apply_diff),
  940. END_OF_TESTCASES
  941. };