test_consdiff.c 36 KB

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