test_consdiff.c 39 KB

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