test_consdiff.c 34 KB

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