test_consdiff.c 36 KB

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