test_consdiff.c 40 KB

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