test_consdiff.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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(strcmp("/foo", "Afoo"), OP_LT, 0);
  352. tt_int_op(base64cmp_wrapper("/foo", "Afoo"), OP_GT, 0);
  353. tt_int_op(strcmp("Afoo", "0foo"), OP_GT, 0);
  354. tt_int_op(base64cmp_wrapper("Afoo", "0foo"), OP_LT, 0);
  355. /* Comparisons that would return the same as with strcmp(). */
  356. tt_int_op(strcmp("afoo", "Afoo"), OP_GT, 0);
  357. tt_int_op(base64cmp_wrapper("afoo", "Afoo"), OP_GT, 0);
  358. /* Different lengths */
  359. tt_int_op(base64cmp_wrapper("afoo", "afooo"), OP_LT, 0);
  360. tt_int_op(base64cmp_wrapper("afooo", "afoo"), OP_GT, 0);
  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. smartlist_free(diff);
  518. smartlist_clear(cons1);
  519. smartlist_clear(cons2);
  520. consensus_split_lines(cons1, "B\n", area);
  521. consensus_split_lines(cons2, "A\nB\n", area);
  522. diff = gen_ed_diff(cons1, cons2, area);
  523. tt_ptr_op(NULL, OP_NE, diff);
  524. tt_int_op(3, OP_EQ, smartlist_len(diff));
  525. tt_str_eq_line("0a", smartlist_get(diff, 0));
  526. tt_str_eq_line("A", smartlist_get(diff, 1));
  527. tt_str_eq_line(".", smartlist_get(diff, 2));
  528. /* TODO: small real use-cases, i.e. consensuses. */
  529. done:
  530. teardown_capture_of_logs();
  531. smartlist_free(cons1);
  532. smartlist_free(cons2);
  533. smartlist_free(diff);
  534. memarea_drop_all(area);
  535. }
  536. static void
  537. test_consdiff_apply_ed_diff(void *arg)
  538. {
  539. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  540. memarea_t *area = memarea_new();
  541. (void)arg;
  542. cons1 = smartlist_new();
  543. diff = smartlist_new();
  544. setup_capture_of_logs(LOG_WARN);
  545. consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area);
  546. /* Command without range. */
  547. smartlist_add_linecpy(diff, area, "a");
  548. cons2 = apply_ed_diff(cons1, diff, 0);
  549. tt_ptr_op(NULL, OP_EQ, cons2);
  550. smartlist_clear(diff);
  551. expect_single_log_msg_containing("an ed command was missing a line number");
  552. /* Range without command. */
  553. smartlist_add_linecpy(diff, area, "1");
  554. mock_clean_saved_logs();
  555. cons2 = apply_ed_diff(cons1, diff, 0);
  556. tt_ptr_op(NULL, OP_EQ, cons2);
  557. expect_single_log_msg_containing("a line with no ed command was found");
  558. smartlist_clear(diff);
  559. /* Range without end. */
  560. smartlist_add_linecpy(diff, area, "1,");
  561. mock_clean_saved_logs();
  562. cons2 = apply_ed_diff(cons1, diff, 0);
  563. tt_ptr_op(NULL, OP_EQ, cons2);
  564. expect_single_log_msg_containing("an ed command was missing a range "
  565. "end line number.");
  566. smartlist_clear(diff);
  567. /* Incoherent ranges. */
  568. smartlist_add_linecpy(diff, area, "1,1");
  569. mock_clean_saved_logs();
  570. cons2 = apply_ed_diff(cons1, diff, 0);
  571. tt_ptr_op(NULL, OP_EQ, cons2);
  572. expect_single_log_msg_containing("an invalid range was found");
  573. smartlist_clear(diff);
  574. smartlist_add_linecpy(diff, area, "3,2");
  575. mock_clean_saved_logs();
  576. cons2 = apply_ed_diff(cons1, diff, 0);
  577. tt_ptr_op(NULL, OP_EQ, cons2);
  578. expect_single_log_msg_containing("an invalid range was found");
  579. smartlist_clear(diff);
  580. /* Unexpected range for add command. */
  581. smartlist_add_linecpy(diff, area, "1,2a");
  582. mock_clean_saved_logs();
  583. cons2 = apply_ed_diff(cons1, diff, 0);
  584. tt_ptr_op(NULL, OP_EQ, cons2);
  585. expect_single_log_msg_containing("add lines after a range");
  586. smartlist_clear(diff);
  587. /* $ for a non-delete command. */
  588. smartlist_add_linecpy(diff, area, "1,$c");
  589. mock_clean_saved_logs();
  590. cons2 = apply_ed_diff(cons1, diff, 0);
  591. tt_ptr_op(NULL, OP_EQ, cons2);
  592. expect_single_log_msg_containing("it wanted to use $ with a command "
  593. "other than delete");
  594. smartlist_clear(diff);
  595. /* Script is not in reverse order. */
  596. smartlist_add_linecpy(diff, area, "1d");
  597. smartlist_add_linecpy(diff, area, "3d");
  598. mock_clean_saved_logs();
  599. cons2 = apply_ed_diff(cons1, diff, 0);
  600. tt_ptr_op(NULL, OP_EQ, cons2);
  601. expect_single_log_msg_containing("its commands are not properly sorted");
  602. smartlist_clear(diff);
  603. /* Script contains unrecognised commands longer than one char. */
  604. smartlist_add_linecpy(diff, area, "1foo");
  605. mock_clean_saved_logs();
  606. cons2 = apply_ed_diff(cons1, diff, 0);
  607. tt_ptr_op(NULL, OP_EQ, cons2);
  608. expect_single_log_msg_containing("an ed command longer than one char was "
  609. "found");
  610. smartlist_clear(diff);
  611. /* Script contains unrecognised commands. */
  612. smartlist_add_linecpy(diff, area, "1e");
  613. mock_clean_saved_logs();
  614. cons2 = apply_ed_diff(cons1, diff, 0);
  615. tt_ptr_op(NULL, OP_EQ, cons2);
  616. expect_single_log_msg_containing("an unrecognised ed command was found");
  617. smartlist_clear(diff);
  618. /* Command that should be followed by at least one line and a ".", but
  619. * isn't. */
  620. smartlist_add_linecpy(diff, area, "0a");
  621. mock_clean_saved_logs();
  622. cons2 = apply_ed_diff(cons1, diff, 0);
  623. tt_ptr_op(NULL, OP_EQ, cons2);
  624. expect_single_log_msg_containing("it has an ed command that tries to "
  625. "insert zero lines.");
  626. /* Now it is followed by a ".", but it inserts zero lines. */
  627. smartlist_add_linecpy(diff, area, ".");
  628. mock_clean_saved_logs();
  629. cons2 = apply_ed_diff(cons1, diff, 0);
  630. tt_ptr_op(NULL, OP_EQ, cons2);
  631. expect_single_log_msg_containing("it has an ed command that tries to "
  632. "insert zero lines.");
  633. smartlist_clear(diff);
  634. /* Now it it inserts something, but has no terminator. */
  635. smartlist_add_linecpy(diff, area, "0a");
  636. smartlist_add_linecpy(diff, area, "hello");
  637. mock_clean_saved_logs();
  638. cons2 = apply_ed_diff(cons1, diff, 0);
  639. tt_ptr_op(NULL, OP_EQ, cons2);
  640. expect_single_log_msg_containing("lines to be inserted that don't end with "
  641. "a \".\".");
  642. smartlist_clear(diff);
  643. /* Ranges must be numeric only and cannot contain spaces. */
  644. smartlist_add_linecpy(diff, area, "0, 4d");
  645. mock_clean_saved_logs();
  646. cons2 = apply_ed_diff(cons1, diff, 0);
  647. tt_ptr_op(NULL, OP_EQ, cons2);
  648. expect_single_log_msg_containing("an ed command was missing a range "
  649. "end line number.");
  650. smartlist_clear(diff);
  651. /* '+' is not a number. */
  652. smartlist_add_linecpy(diff, area, "+0,4d");
  653. mock_clean_saved_logs();
  654. cons2 = apply_ed_diff(cons1, diff, 0);
  655. tt_ptr_op(NULL, OP_EQ, cons2);
  656. expect_single_log_msg_containing("an ed command was missing a line number");
  657. smartlist_clear(diff);
  658. /* range duplication */
  659. smartlist_add_linecpy(diff, area, "0,4d,5d");
  660. mock_clean_saved_logs();
  661. cons2 = apply_ed_diff(cons1, diff, 0);
  662. tt_ptr_op(NULL, OP_EQ, cons2);
  663. expect_single_log_msg_containing("an ed command longer than one char was "
  664. "found");
  665. smartlist_clear(diff);
  666. /* space before command */
  667. smartlist_add_linecpy(diff, area, "0,4 d");
  668. mock_clean_saved_logs();
  669. cons2 = apply_ed_diff(cons1, diff, 0);
  670. tt_ptr_op(NULL, OP_EQ, cons2);
  671. expect_single_log_msg_containing("an ed command longer than one char was "
  672. "found");
  673. smartlist_clear(diff);
  674. /* space inside number */
  675. smartlist_add_linecpy(diff, area, "0,4 5d");
  676. mock_clean_saved_logs();
  677. cons2 = apply_ed_diff(cons1, diff, 0);
  678. tt_ptr_op(NULL, OP_EQ, cons2);
  679. expect_single_log_msg_containing("an ed command longer than one char was "
  680. "found");
  681. smartlist_clear(diff);
  682. /* Test appending text, 'a'. */
  683. consensus_split_lines(diff, "3a\nU\nO\n.\n0a\nV\n.\n", area);
  684. cons2 = apply_ed_diff(cons1, diff, 0);
  685. tt_ptr_op(NULL, OP_NE, cons2);
  686. tt_int_op(8, OP_EQ, smartlist_len(cons2));
  687. tt_str_eq_line("V", smartlist_get(cons2, 0));
  688. tt_str_eq_line("A", smartlist_get(cons2, 1));
  689. tt_str_eq_line("B", smartlist_get(cons2, 2));
  690. tt_str_eq_line("C", smartlist_get(cons2, 3));
  691. tt_str_eq_line("U", smartlist_get(cons2, 4));
  692. tt_str_eq_line("O", smartlist_get(cons2, 5));
  693. tt_str_eq_line("D", smartlist_get(cons2, 6));
  694. tt_str_eq_line("E", smartlist_get(cons2, 7));
  695. smartlist_clear(diff);
  696. smartlist_free(cons2);
  697. /* Test deleting text, 'd'. */
  698. consensus_split_lines(diff, "4d\n1,2d\n", area);
  699. cons2 = apply_ed_diff(cons1, diff, 0);
  700. tt_ptr_op(NULL, OP_NE, cons2);
  701. tt_int_op(2, OP_EQ, smartlist_len(cons2));
  702. tt_str_eq_line("C", smartlist_get(cons2, 0));
  703. tt_str_eq_line("E", smartlist_get(cons2, 1));
  704. smartlist_clear(diff);
  705. smartlist_free(cons2);
  706. /* Test changing text, 'c'. */
  707. consensus_split_lines(diff, "4c\nT\nX\n.\n1,2c\nM\n.\n", area);
  708. cons2 = apply_ed_diff(cons1, diff, 0);
  709. tt_ptr_op(NULL, OP_NE, cons2);
  710. tt_int_op(5, OP_EQ, smartlist_len(cons2));
  711. tt_str_eq_line("M", smartlist_get(cons2, 0));
  712. tt_str_eq_line("C", smartlist_get(cons2, 1));
  713. tt_str_eq_line("T", smartlist_get(cons2, 2));
  714. tt_str_eq_line("X", smartlist_get(cons2, 3));
  715. tt_str_eq_line("E", smartlist_get(cons2, 4));
  716. smartlist_clear(diff);
  717. smartlist_free(cons2);
  718. /* Test 'a', 'd' and 'c' together. */
  719. consensus_split_lines(diff, "4c\nT\nX\n.\n2d\n0a\nM\n.\n", area);
  720. cons2 = apply_ed_diff(cons1, diff, 0);
  721. tt_ptr_op(NULL, OP_NE, cons2);
  722. tt_int_op(6, OP_EQ, smartlist_len(cons2));
  723. tt_str_eq_line("M", smartlist_get(cons2, 0));
  724. tt_str_eq_line("A", smartlist_get(cons2, 1));
  725. tt_str_eq_line("C", smartlist_get(cons2, 2));
  726. tt_str_eq_line("T", smartlist_get(cons2, 3));
  727. tt_str_eq_line("X", smartlist_get(cons2, 4));
  728. tt_str_eq_line("E", smartlist_get(cons2, 5));
  729. done:
  730. teardown_capture_of_logs();
  731. smartlist_free(cons1);
  732. smartlist_free(cons2);
  733. smartlist_free(diff);
  734. memarea_drop_all(area);
  735. }
  736. static void
  737. test_consdiff_gen_diff(void *arg)
  738. {
  739. char *cons1_str=NULL, *cons2_str=NULL;
  740. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  741. consensus_digest_t digests1, digests2;
  742. memarea_t *area = memarea_new();
  743. (void)arg;
  744. cons1 = smartlist_new();
  745. cons2 = smartlist_new();
  746. /* Identity hashes are not sorted properly, return NULL.
  747. * Already tested in gen_ed_diff, but see that a NULL ed diff also makes
  748. * gen_diff return NULL. */
  749. cons1_str = tor_strdup(
  750. "network-status-version foo\n"
  751. "r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
  752. "r name aaaaaaaaaaaaaaaaa etc\nbar\n"
  753. "directory-signature foo bar\nbar\n"
  754. );
  755. cons2_str = tor_strdup(
  756. "network-status-version foo\n"
  757. "r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
  758. "r name ccccccccccccccccc etc\nbar\n"
  759. "directory-signature foo bar\nbar\n"
  760. );
  761. tt_int_op(0, OP_EQ,
  762. consensus_compute_digest_as_signed(cons1_str, &digests1));
  763. tt_int_op(0, OP_EQ,
  764. consensus_compute_digest(cons2_str, &digests2));
  765. consensus_split_lines(cons1, cons1_str, area);
  766. consensus_split_lines(cons2, cons2_str, area);
  767. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  768. tt_ptr_op(NULL, OP_EQ, diff);
  769. /* Check that the headers are done properly. */
  770. tor_free(cons1_str);
  771. cons1_str = tor_strdup(
  772. "network-status-version foo\n"
  773. "r name ccccccccccccccccc etc\nfoo\n"
  774. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  775. "directory-signature foo bar\nbar\n"
  776. );
  777. tt_int_op(0, OP_EQ,
  778. consensus_compute_digest_as_signed(cons1_str, &digests1));
  779. smartlist_clear(cons1);
  780. consensus_split_lines(cons1, cons1_str, area);
  781. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
  782. tt_ptr_op(NULL, OP_NE, diff);
  783. tt_int_op(11, OP_EQ, smartlist_len(diff));
  784. tt_assert(line_str_eq(smartlist_get(diff, 0),
  785. "network-status-diff-version 1"));
  786. tt_assert(line_str_eq(smartlist_get(diff, 1), "hash "
  787. "95D70F5A3CC65F920AA8B44C4563D7781A082674329661884E19E94B79D539C2 "
  788. "7AFECEFA4599BA33D603653E3D2368F648DF4AC4723929B0F7CF39281596B0C1"));
  789. tt_assert(line_str_eq(smartlist_get(diff, 2), "6,$d"));
  790. tt_assert(line_str_eq(smartlist_get(diff, 3), "3,4c"));
  791. tt_assert(line_str_eq(smartlist_get(diff, 4), "bar"));
  792. tt_assert(line_str_eq(smartlist_get(diff, 5),
  793. "directory-signature foo bar"));
  794. tt_assert(line_str_eq(smartlist_get(diff, 6),
  795. "."));
  796. tt_assert(line_str_eq(smartlist_get(diff, 7), "1a"));
  797. tt_assert(line_str_eq(smartlist_get(diff, 8),
  798. "r name aaaaaaaaaaaaaaaaa etc"));
  799. tt_assert(line_str_eq(smartlist_get(diff, 9), "foo"));
  800. tt_assert(line_str_eq(smartlist_get(diff, 10), "."));
  801. /* TODO: small real use-cases, i.e. consensuses. */
  802. done:
  803. tor_free(cons1_str);
  804. tor_free(cons2_str);
  805. smartlist_free(cons1);
  806. smartlist_free(cons2);
  807. smartlist_free(diff);
  808. memarea_drop_all(area);
  809. }
  810. static void
  811. test_consdiff_apply_diff(void *arg)
  812. {
  813. smartlist_t *cons1=NULL, *diff=NULL;
  814. char *cons1_str=NULL, *cons2 = NULL;
  815. consensus_digest_t digests1;
  816. (void)arg;
  817. memarea_t *area = memarea_new();
  818. cons1 = smartlist_new();
  819. diff = smartlist_new();
  820. setup_capture_of_logs(LOG_INFO);
  821. cons1_str = tor_strdup(
  822. "network-status-version foo\n"
  823. "r name ccccccccccccccccc etc\nfoo\n"
  824. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  825. "directory-signature foo bar\nbar\n"
  826. );
  827. tt_int_op(0, OP_EQ,
  828. consensus_compute_digest(cons1_str, &digests1));
  829. consensus_split_lines(cons1, cons1_str, area);
  830. /* diff doesn't have enough lines. */
  831. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  832. tt_ptr_op(NULL, OP_EQ, cons2);
  833. expect_single_log_msg_containing("too short")
  834. /* first line doesn't match format-version string. */
  835. smartlist_add_linecpy(diff, area, "foo-bar");
  836. smartlist_add_linecpy(diff, area, "header-line");
  837. mock_clean_saved_logs();
  838. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  839. tt_ptr_op(NULL, OP_EQ, cons2);
  840. expect_single_log_msg_containing("format is not known")
  841. /* The first word of the second header line is not "hash". */
  842. smartlist_clear(diff);
  843. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  844. smartlist_add_linecpy(diff, area, "word a b");
  845. smartlist_add_linecpy(diff, area, "x");
  846. mock_clean_saved_logs();
  847. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  848. tt_ptr_op(NULL, OP_EQ, cons2);
  849. expect_single_log_msg_containing("does not include the necessary digests")
  850. /* Wrong number of words after "hash". */
  851. smartlist_clear(diff);
  852. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  853. smartlist_add_linecpy(diff, area, "hash a b c");
  854. mock_clean_saved_logs();
  855. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  856. tt_ptr_op(NULL, OP_EQ, cons2);
  857. expect_single_log_msg_containing("does not include the necessary digests")
  858. /* base16 digests do not have the expected length. */
  859. smartlist_clear(diff);
  860. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  861. smartlist_add_linecpy(diff, area, "hash aaa bbb");
  862. mock_clean_saved_logs();
  863. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  864. tt_ptr_op(NULL, OP_EQ, cons2);
  865. expect_single_log_msg_containing("includes base16-encoded digests of "
  866. "incorrect size")
  867. /* base16 digests contain non-base16 characters. */
  868. smartlist_clear(diff);
  869. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  870. smartlist_add_linecpy(diff, area, "hash"
  871. " ????????????????????????????????????????????????????????????????"
  872. " ----------------------------------------------------------------");
  873. mock_clean_saved_logs();
  874. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  875. tt_ptr_op(NULL, OP_EQ, cons2);
  876. expect_single_log_msg_containing("includes malformed digests")
  877. /* Invalid ed diff.
  878. * As tested in apply_ed_diff, but check that apply_diff does return NULL if
  879. * the ed diff can't be applied. */
  880. smartlist_clear(diff);
  881. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  882. smartlist_add_linecpy(diff, area, "hash"
  883. /* sha3 of cons1. */
  884. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  885. /* sha256 of cons2. */
  886. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  887. smartlist_add_linecpy(diff, area, "foobar");
  888. mock_clean_saved_logs();
  889. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  890. tt_ptr_op(NULL, OP_EQ, cons2);
  891. expect_single_log_msg_containing("because an ed command was missing a line "
  892. "number")
  893. /* Base consensus doesn't match its digest as found in the diff. */
  894. smartlist_clear(diff);
  895. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  896. smartlist_add_linecpy(diff, area, "hash"
  897. /* bogus sha256. */
  898. " 3333333333333333333333333333333333333333333333333333333333333333"
  899. /* sha256 of cons2. */
  900. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  901. mock_clean_saved_logs();
  902. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  903. tt_ptr_op(NULL, OP_EQ, cons2);
  904. expect_log_msg_containing("base consensus doesn't match the digest "
  905. "as found");
  906. /* Resulting consensus doesn't match its digest as found in the diff. */
  907. smartlist_clear(diff);
  908. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  909. smartlist_add_linecpy(diff, area, "hash"
  910. /* sha3 of cons1. */
  911. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  912. /* bogus sha3. */
  913. " 3333333333333333333333333333333333333333333333333333333333333333");
  914. mock_clean_saved_logs();
  915. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  916. tt_ptr_op(NULL, OP_EQ, cons2);
  917. expect_log_msg_containing("resulting consensus doesn't match the "
  918. "digest as found");
  919. #if 0
  920. /* XXXX No longer possible, since we aren't using the other algorithm. */
  921. /* Resulting consensus digest cannot be computed */
  922. smartlist_clear(diff);
  923. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  924. smartlist_add_linecpy(diff, area, "hash"
  925. /* sha3 of cons1. */
  926. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  927. /* bogus sha3. */
  928. " 3333333333333333333333333333333333333333333333333333333333333333");
  929. smartlist_add_linecpy(diff, area, "1,2d"); // remove starting line
  930. mock_clean_saved_logs();
  931. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  932. tt_ptr_op(NULL, OP_EQ, cons2);
  933. expect_log_msg_containing("Could not compute digests of the consensus "
  934. "resulting from applying a consensus diff.");
  935. #endif
  936. /* Very simple test, only to see that nothing errors. */
  937. smartlist_clear(diff);
  938. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  939. smartlist_add_linecpy(diff, area, "hash"
  940. /* sha3 of cons1. */
  941. " 06646D6CF563A41869D3B02E73254372AE3140046C5E7D83C9F71E54976AF9B4"
  942. /* sha3 of cons2. */
  943. " 90A418881B2FCAB3D9E60EE02E4D666D56CFA38F8A3B7AA3E0ADBA530DDA9353");
  944. smartlist_add_linecpy(diff, area, "3c");
  945. smartlist_add_linecpy(diff, area, "sample");
  946. smartlist_add_linecpy(diff, area, ".");
  947. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  948. tt_ptr_op(NULL, OP_NE, cons2);
  949. tt_str_op(
  950. "network-status-version foo\n"
  951. "r name ccccccccccccccccc etc\nsample\n"
  952. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  953. "directory-signature foo bar\nbar\n", OP_EQ,
  954. cons2);
  955. tor_free(cons2);
  956. /* Check that lowercase letters in base16-encoded digests work too. */
  957. smartlist_clear(diff);
  958. smartlist_add_linecpy(diff, area, "network-status-diff-version 1");
  959. smartlist_add_linecpy(diff, area, "hash"
  960. /* sha3 of cons1. */
  961. " 06646d6cf563a41869d3b02e73254372ae3140046c5e7d83c9f71e54976af9b4"
  962. /* sha3 of cons2. */
  963. " 90a418881b2fcab3d9e60ee02e4d666d56cfa38f8a3b7aa3e0adba530dda9353");
  964. smartlist_add_linecpy(diff, area, "3c");
  965. smartlist_add_linecpy(diff, area, "sample");
  966. smartlist_add_linecpy(diff, area, ".");
  967. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  968. tt_ptr_op(NULL, OP_NE, cons2);
  969. tt_str_op(
  970. "network-status-version foo\n"
  971. "r name ccccccccccccccccc etc\nsample\n"
  972. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  973. "directory-signature foo bar\nbar\n", OP_EQ,
  974. cons2);
  975. tor_free(cons2);
  976. smartlist_clear(diff);
  977. done:
  978. teardown_capture_of_logs();
  979. tor_free(cons1_str);
  980. smartlist_free(cons1);
  981. smartlist_free(diff);
  982. memarea_drop_all(area);
  983. }
  984. #define CONSDIFF_LEGACY(name) \
  985. { #name, test_consdiff_ ## name , 0, NULL, NULL }
  986. struct testcase_t consdiff_tests[] = {
  987. CONSDIFF_LEGACY(smartlist_slice),
  988. CONSDIFF_LEGACY(smartlist_slice_string_pos),
  989. CONSDIFF_LEGACY(lcs_lengths),
  990. CONSDIFF_LEGACY(trim_slices),
  991. CONSDIFF_LEGACY(set_changed),
  992. CONSDIFF_LEGACY(calc_changes),
  993. CONSDIFF_LEGACY(get_id_hash),
  994. CONSDIFF_LEGACY(is_valid_router_entry),
  995. CONSDIFF_LEGACY(next_router),
  996. CONSDIFF_LEGACY(base64cmp),
  997. CONSDIFF_LEGACY(gen_ed_diff),
  998. CONSDIFF_LEGACY(apply_ed_diff),
  999. CONSDIFF_LEGACY(gen_diff),
  1000. CONSDIFF_LEGACY(apply_diff),
  1001. END_OF_TESTCASES
  1002. };