test_consdiff.c 35 KB

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