test_consdiff.c 36 KB

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