test_consdiff.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. /* Same as the two above, but with the reversed thing immediately after a
  379. match. (The code handles this differently) */
  380. smartlist_del(cons1, 0);
  381. smartlist_add(cons1, (char*)"r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
  382. mock_clean_saved_logs();
  383. diff = gen_ed_diff(cons1, cons2);
  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. mock_clean_saved_logs();
  389. diff = gen_ed_diff(cons2, cons1);
  390. tt_ptr_op(NULL, OP_EQ, diff);
  391. expect_single_log_msg_containing("Refusing to generate consensus diff "
  392. "because the target consensus doesn't have its router entries sorted "
  393. "properly.");
  394. /* Identity hashes are repeated, return NULL. */
  395. smartlist_clear(cons1);
  396. smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  397. smartlist_add(cons1, (char*)"foo");
  398. smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
  399. smartlist_add(cons1, (char*)"bar");
  400. mock_clean_saved_logs();
  401. diff = gen_ed_diff(cons1, cons2);
  402. tt_ptr_op(NULL, OP_EQ, diff);
  403. expect_single_log_msg_containing("Refusing to generate consensus diff "
  404. "because the base consensus doesn't have its router entries sorted "
  405. "properly.");
  406. /* We have to add a line that is just a dot, return NULL. */
  407. smartlist_clear(cons1);
  408. smartlist_clear(cons2);
  409. smartlist_add(cons1, (char*)"foo1");
  410. smartlist_add(cons1, (char*)"foo2");
  411. smartlist_add(cons2, (char*)"foo1");
  412. smartlist_add(cons2, (char*)".");
  413. smartlist_add(cons2, (char*)"foo2");
  414. mock_clean_saved_logs();
  415. diff = gen_ed_diff(cons1, cons2);
  416. tt_ptr_op(NULL, OP_EQ, diff);
  417. expect_single_log_msg_containing("Cannot generate consensus diff "
  418. "because one of the lines to be added is \".\".");
  419. #define MAX_LINE_COUNT (10000)
  420. /* Too many lines to be fed to the quadratic-time function. */
  421. smartlist_clear(cons1);
  422. smartlist_clear(cons2);
  423. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"a");
  424. for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"b");
  425. mock_clean_saved_logs();
  426. diff = gen_ed_diff(cons1, cons2);
  427. tt_ptr_op(NULL, OP_EQ, diff);
  428. expect_single_log_msg_containing("Refusing to generate consensus diff "
  429. "because we found too few common router ids.");
  430. /* We have dot lines, but they don't interfere with the script format. */
  431. smartlist_clear(cons1);
  432. smartlist_clear(cons2);
  433. smartlist_add(cons1, (char*)"foo1");
  434. smartlist_add(cons1, (char*)".");
  435. smartlist_add(cons1, (char*)".");
  436. smartlist_add(cons1, (char*)"foo2");
  437. smartlist_add(cons2, (char*)"foo1");
  438. smartlist_add(cons2, (char*)".");
  439. smartlist_add(cons2, (char*)"foo2");
  440. diff = gen_ed_diff(cons1, cons2);
  441. tt_ptr_op(NULL, OP_NE, diff);
  442. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  443. smartlist_free(diff);
  444. /* Empty diff tests. */
  445. smartlist_clear(cons1);
  446. smartlist_clear(cons2);
  447. diff = gen_ed_diff(cons1, cons2);
  448. tt_ptr_op(NULL, OP_NE, diff);
  449. tt_int_op(0, OP_EQ, smartlist_len(diff));
  450. smartlist_free(diff);
  451. smartlist_add(cons1, (char*)"foo");
  452. smartlist_add(cons1, (char*)"bar");
  453. smartlist_add(cons2, (char*)"foo");
  454. smartlist_add(cons2, (char*)"bar");
  455. diff = gen_ed_diff(cons1, cons2);
  456. tt_ptr_op(NULL, OP_NE, diff);
  457. tt_int_op(0, OP_EQ, smartlist_len(diff));
  458. smartlist_free(diff);
  459. /* Everything is deleted. */
  460. smartlist_clear(cons2);
  461. diff = gen_ed_diff(cons1, cons2);
  462. tt_ptr_op(NULL, OP_NE, diff);
  463. tt_int_op(1, OP_EQ, smartlist_len(diff));
  464. tt_str_op("1,2d", OP_EQ, smartlist_get(diff, 0));
  465. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  466. smartlist_free(diff);
  467. /* Everything is added. */
  468. diff = gen_ed_diff(cons2, cons1);
  469. tt_ptr_op(NULL, OP_NE, diff);
  470. tt_int_op(4, OP_EQ, smartlist_len(diff));
  471. tt_str_op("0a", OP_EQ, smartlist_get(diff, 0));
  472. tt_str_op("foo", OP_EQ, smartlist_get(diff, 1));
  473. tt_str_op("bar", OP_EQ, smartlist_get(diff, 2));
  474. tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
  475. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  476. smartlist_free(diff);
  477. /* Everything is changed. */
  478. smartlist_add(cons2, (char*)"foo2");
  479. smartlist_add(cons2, (char*)"bar2");
  480. diff = gen_ed_diff(cons1, cons2);
  481. tt_ptr_op(NULL, OP_NE, diff);
  482. tt_int_op(4, OP_EQ, smartlist_len(diff));
  483. tt_str_op("1,2c", OP_EQ, smartlist_get(diff, 0));
  484. tt_str_op("foo2", OP_EQ, smartlist_get(diff, 1));
  485. tt_str_op("bar2", OP_EQ, smartlist_get(diff, 2));
  486. tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
  487. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  488. smartlist_free(diff);
  489. /* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */
  490. smartlist_clear(cons1);
  491. smartlist_clear(cons2);
  492. smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
  493. smartlist_split_string(cons2, "A:C:O:E:U", ":", 0, 0);
  494. free_cons_entries = 1;
  495. diff = gen_ed_diff(cons1, cons2);
  496. tt_ptr_op(NULL, OP_NE, diff);
  497. tt_int_op(7, OP_EQ, smartlist_len(diff));
  498. tt_str_op("5a", OP_EQ, smartlist_get(diff, 0));
  499. tt_str_op("U", OP_EQ, smartlist_get(diff, 1));
  500. tt_str_op(".", OP_EQ, smartlist_get(diff, 2));
  501. tt_str_op("4c", OP_EQ, smartlist_get(diff, 3));
  502. tt_str_op("O", OP_EQ, smartlist_get(diff, 4));
  503. tt_str_op(".", OP_EQ, smartlist_get(diff, 5));
  504. tt_str_op("2d", OP_EQ, smartlist_get(diff, 6));
  505. /* TODO: small real use-cases, i.e. consensuses. */
  506. done:
  507. teardown_capture_of_logs();
  508. if (free_cons_entries) {
  509. if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
  510. if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  511. }
  512. smartlist_free(cons1);
  513. smartlist_free(cons2);
  514. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  515. smartlist_free(diff);
  516. }
  517. static void
  518. test_consdiff_apply_ed_diff(void *arg)
  519. {
  520. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  521. (void)arg;
  522. cons1 = smartlist_new();
  523. diff = smartlist_new();
  524. setup_capture_of_logs(LOG_WARN);
  525. smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
  526. /* Command without range. */
  527. smartlist_add(diff, (char*)"a");
  528. cons2 = apply_ed_diff(cons1, diff);
  529. tt_ptr_op(NULL, OP_EQ, cons2);
  530. smartlist_clear(diff);
  531. expect_single_log_msg_containing("an ed command was missing a line number");
  532. /* Range without command. */
  533. smartlist_add(diff, (char*)"1");
  534. mock_clean_saved_logs();
  535. cons2 = apply_ed_diff(cons1, diff);
  536. tt_ptr_op(NULL, OP_EQ, cons2);
  537. expect_single_log_msg_containing("a line with no ed command was found");
  538. smartlist_clear(diff);
  539. /* Range without end. */
  540. smartlist_add(diff, (char*)"1,");
  541. mock_clean_saved_logs();
  542. cons2 = apply_ed_diff(cons1, diff);
  543. tt_ptr_op(NULL, OP_EQ, cons2);
  544. expect_single_log_msg_containing("an ed command was missing a range "
  545. "end line number.");
  546. smartlist_clear(diff);
  547. /* Incoherent ranges. */
  548. smartlist_add(diff, (char*)"1,1");
  549. mock_clean_saved_logs();
  550. cons2 = apply_ed_diff(cons1, diff);
  551. tt_ptr_op(NULL, OP_EQ, cons2);
  552. expect_single_log_msg_containing("an invalid range was found");
  553. smartlist_clear(diff);
  554. smartlist_add(diff, (char*)"3,2");
  555. mock_clean_saved_logs();
  556. cons2 = apply_ed_diff(cons1, diff);
  557. tt_ptr_op(NULL, OP_EQ, cons2);
  558. expect_single_log_msg_containing("an invalid range was found");
  559. smartlist_clear(diff);
  560. /* Script is not in reverse order. */
  561. smartlist_add(diff, (char*)"1d");
  562. smartlist_add(diff, (char*)"3d");
  563. mock_clean_saved_logs();
  564. cons2 = apply_ed_diff(cons1, diff);
  565. tt_ptr_op(NULL, OP_EQ, cons2);
  566. expect_single_log_msg_containing("its commands are not properly sorted");
  567. smartlist_clear(diff);
  568. /* Script contains unrecognised commands longer than one char. */
  569. smartlist_add(diff, (char*)"1foo");
  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("an ed command longer than one char was "
  574. "found");
  575. smartlist_clear(diff);
  576. /* Script contains unrecognised commands. */
  577. smartlist_add(diff, (char*)"1e");
  578. mock_clean_saved_logs();
  579. cons2 = apply_ed_diff(cons1, diff);
  580. tt_ptr_op(NULL, OP_EQ, cons2);
  581. expect_single_log_msg_containing("an unrecognised ed command was found");
  582. smartlist_clear(diff);
  583. /* Command that should be followed by at least one line and a ".", but
  584. * isn't. */
  585. smartlist_add(diff, (char*)"0a");
  586. mock_clean_saved_logs();
  587. cons2 = apply_ed_diff(cons1, diff);
  588. tt_ptr_op(NULL, OP_EQ, cons2);
  589. expect_single_log_msg_containing("it has an ed command that tries to "
  590. "insert zero lines.");
  591. /* Now it is followed by a ".", but it inserts zero lines. */
  592. smartlist_add(diff, (char*)".");
  593. mock_clean_saved_logs();
  594. cons2 = apply_ed_diff(cons1, diff);
  595. tt_ptr_op(NULL, OP_EQ, cons2);
  596. expect_single_log_msg_containing("it has an ed command that tries to "
  597. "insert zero lines.");
  598. smartlist_clear(diff);
  599. /* Now it it inserts something, but has no terminator. */
  600. smartlist_add(diff, (char*)"0a");
  601. smartlist_add(diff, (char*)"hello");
  602. mock_clean_saved_logs();
  603. cons2 = apply_ed_diff(cons1, diff);
  604. tt_ptr_op(NULL, OP_EQ, cons2);
  605. expect_single_log_msg_containing("lines to be inserted that don't end with "
  606. "a \".\".");
  607. smartlist_clear(diff);
  608. /* Test appending text, 'a'. */
  609. smartlist_split_string(diff, "3a:U:O:.:0a:V:.", ":", 0, 0);
  610. cons2 = apply_ed_diff(cons1, diff);
  611. tt_ptr_op(NULL, OP_NE, cons2);
  612. tt_int_op(8, OP_EQ, smartlist_len(cons2));
  613. tt_str_op("V", OP_EQ, smartlist_get(cons2, 0));
  614. tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
  615. tt_str_op("B", OP_EQ, smartlist_get(cons2, 2));
  616. tt_str_op("C", OP_EQ, smartlist_get(cons2, 3));
  617. tt_str_op("U", OP_EQ, smartlist_get(cons2, 4));
  618. tt_str_op("O", OP_EQ, smartlist_get(cons2, 5));
  619. tt_str_op("D", OP_EQ, smartlist_get(cons2, 6));
  620. tt_str_op("E", OP_EQ, smartlist_get(cons2, 7));
  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 deleting text, 'd'. */
  626. smartlist_split_string(diff, "4d:1,2d", ":", 0, 0);
  627. cons2 = apply_ed_diff(cons1, diff);
  628. tt_ptr_op(NULL, OP_NE, cons2);
  629. tt_int_op(2, OP_EQ, smartlist_len(cons2));
  630. tt_str_op("C", OP_EQ, smartlist_get(cons2, 0));
  631. tt_str_op("E", OP_EQ, smartlist_get(cons2, 1));
  632. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  633. smartlist_clear(diff);
  634. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  635. smartlist_free(cons2);
  636. /* Test changing text, 'c'. */
  637. smartlist_split_string(diff, "4c:T:X:.:1, 2c:M:.", ":", 0, 0);
  638. cons2 = apply_ed_diff(cons1, diff);
  639. tt_ptr_op(NULL, OP_NE, cons2);
  640. tt_int_op(5, OP_EQ, smartlist_len(cons2));
  641. tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
  642. tt_str_op("C", OP_EQ, smartlist_get(cons2, 1));
  643. tt_str_op("T", OP_EQ, smartlist_get(cons2, 2));
  644. tt_str_op("X", OP_EQ, smartlist_get(cons2, 3));
  645. tt_str_op("E", OP_EQ, smartlist_get(cons2, 4));
  646. SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  647. smartlist_clear(diff);
  648. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  649. smartlist_free(cons2);
  650. /* Test 'a', 'd' and 'c' together. */
  651. smartlist_split_string(diff, "4c:T:X:.:2d:0a:M:.", ":", 0, 0);
  652. cons2 = apply_ed_diff(cons1, diff);
  653. tt_ptr_op(NULL, OP_NE, cons2);
  654. tt_int_op(6, OP_EQ, smartlist_len(cons2));
  655. tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
  656. tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
  657. tt_str_op("C", OP_EQ, smartlist_get(cons2, 2));
  658. tt_str_op("T", OP_EQ, smartlist_get(cons2, 3));
  659. tt_str_op("X", OP_EQ, smartlist_get(cons2, 4));
  660. tt_str_op("E", OP_EQ, smartlist_get(cons2, 5));
  661. done:
  662. teardown_capture_of_logs();
  663. if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
  664. if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  665. smartlist_free(cons1);
  666. smartlist_free(cons2);
  667. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  668. smartlist_free(diff);
  669. }
  670. static void
  671. test_consdiff_gen_diff(void *arg)
  672. {
  673. char *cons1_str=NULL, *cons2_str=NULL;
  674. smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
  675. common_digests_t digests1, digests2;
  676. (void)arg;
  677. cons1 = smartlist_new();
  678. cons2 = smartlist_new();
  679. /* Identity hashes are not sorted properly, return NULL.
  680. * Already tested in gen_ed_diff, but see that a NULL ed diff also makes
  681. * gen_diff return NULL. */
  682. cons1_str = tor_strdup(
  683. "header\nnetwork-status-version foo\n"
  684. "r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
  685. "r name aaaaaaaaaaaaaaaaa etc\nbar\n"
  686. "directory-signature foo bar\nbar\n"
  687. );
  688. cons2_str = tor_strdup(
  689. "header\nnetwork-status-version foo\n"
  690. "r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
  691. "r name ccccccccccccccccc etc\nbar\n"
  692. "directory-signature foo bar\nbar\n"
  693. );
  694. tt_int_op(0, OP_EQ,
  695. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  696. tt_int_op(0, OP_EQ,
  697. router_get_networkstatus_v3_hashes(cons2_str, &digests2));
  698. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  699. tor_split_lines(cons2, cons2_str, (int)strlen(cons2_str));
  700. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
  701. tt_ptr_op(NULL, OP_EQ, diff);
  702. /* Check that the headers are done properly. */
  703. tor_free(cons1_str);
  704. cons1_str = tor_strdup(
  705. "header\nnetwork-status-version foo\n"
  706. "r name ccccccccccccccccc etc\nfoo\n"
  707. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  708. "directory-signature foo bar\nbar\n"
  709. );
  710. tt_int_op(0, OP_EQ,
  711. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  712. smartlist_clear(cons1);
  713. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  714. diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
  715. tt_ptr_op(NULL, OP_NE, diff);
  716. tt_int_op(7, OP_EQ, smartlist_len(diff));
  717. tt_str_op("network-status-diff-version 1", OP_EQ, smartlist_get(diff, 0));
  718. tt_str_op("hash "
  719. "C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A "
  720. "B193E5FBFE5C009AEDE56F9218E6421A1AE5C19F43E091786A73F43F60409B60",
  721. OP_EQ, smartlist_get(diff, 1));
  722. tt_str_op("4,5d", OP_EQ, smartlist_get(diff, 2));
  723. tt_str_op("2a", OP_EQ, smartlist_get(diff, 3));
  724. tt_str_op("r name aaaaaaaaaaaaaaaaa etc", OP_EQ, smartlist_get(diff, 4));
  725. tt_str_op("foo", OP_EQ, smartlist_get(diff, 5));
  726. tt_str_op(".", OP_EQ, smartlist_get(diff, 6));
  727. /* TODO: small real use-cases, i.e. consensuses. */
  728. done:
  729. tor_free(cons1_str);
  730. tor_free(cons2_str);
  731. smartlist_free(cons1);
  732. smartlist_free(cons2);
  733. if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
  734. smartlist_free(diff);
  735. }
  736. static void
  737. test_consdiff_apply_diff(void *arg)
  738. {
  739. smartlist_t *cons1=NULL, *diff=NULL;
  740. char *cons1_str=NULL, *cons2 = NULL;
  741. common_digests_t digests1;
  742. (void)arg;
  743. cons1 = smartlist_new();
  744. diff = smartlist_new();
  745. setup_capture_of_logs(LOG_INFO);
  746. cons1_str = tor_strdup(
  747. "header\nnetwork-status-version foo\n"
  748. "r name ccccccccccccccccc etc\nfoo\n"
  749. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  750. "directory-signature foo bar\nbar\n"
  751. );
  752. tt_int_op(0, OP_EQ,
  753. router_get_networkstatus_v3_hashes(cons1_str, &digests1));
  754. tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
  755. /* diff doesn't have enough lines. */
  756. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  757. tt_ptr_op(NULL, OP_EQ, cons2);
  758. expect_single_log_msg_containing("too short")
  759. /* first line doesn't match format-version string. */
  760. smartlist_add(diff, (char*)"foo-bar");
  761. smartlist_add(diff, (char*)"header-line");
  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("format is not known")
  766. /* The first word of the second header line is not "hash". */
  767. smartlist_clear(diff);
  768. smartlist_add(diff, (char*)"network-status-diff-version 1");
  769. smartlist_add(diff, (char*)"word a b");
  770. smartlist_add(diff, (char*)"x");
  771. mock_clean_saved_logs();
  772. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  773. tt_ptr_op(NULL, OP_EQ, cons2);
  774. expect_single_log_msg_containing("does not include the necessary digests")
  775. /* Wrong number of words after "hash". */
  776. smartlist_clear(diff);
  777. smartlist_add(diff, (char*)"network-status-diff-version 1");
  778. smartlist_add(diff, (char*)"hash a b c");
  779. mock_clean_saved_logs();
  780. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  781. tt_ptr_op(NULL, OP_EQ, cons2);
  782. expect_single_log_msg_containing("does not include the necessary digests")
  783. /* base16 sha256 digests do not have the expected length. */
  784. smartlist_clear(diff);
  785. smartlist_add(diff, (char*)"network-status-diff-version 1");
  786. smartlist_add(diff, (char*)"hash aaa bbb");
  787. mock_clean_saved_logs();
  788. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  789. tt_ptr_op(NULL, OP_EQ, cons2);
  790. expect_single_log_msg_containing("includes base16-encoded digests of "
  791. "incorrect size")
  792. /* base16 sha256 digests contain non-base16 characters. */
  793. smartlist_clear(diff);
  794. smartlist_add(diff, (char*)"network-status-diff-version 1");
  795. smartlist_add(diff, (char*)"hash"
  796. " ????????????????????????????????????????????????????????????????"
  797. " ----------------------------------------------------------------");
  798. mock_clean_saved_logs();
  799. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  800. tt_ptr_op(NULL, OP_EQ, cons2);
  801. expect_single_log_msg_containing("includes malformed digests")
  802. /* Invalid ed diff.
  803. * As tested in apply_ed_diff, but check that apply_diff does return NULL if
  804. * the ed diff can't be applied. */
  805. smartlist_clear(diff);
  806. smartlist_add(diff, (char*)"network-status-diff-version 1");
  807. smartlist_add(diff, (char*)"hash"
  808. /* sha256 of cons1. */
  809. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  810. /* sha256 of cons2. */
  811. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  812. smartlist_add(diff, (char*)"foobar");
  813. mock_clean_saved_logs();
  814. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  815. tt_ptr_op(NULL, OP_EQ, cons2);
  816. expect_single_log_msg_containing("because an ed command was missing a line "
  817. "number")
  818. /* Base consensus doesn't match its digest as found in the diff. */
  819. smartlist_clear(diff);
  820. smartlist_add(diff, (char*)"network-status-diff-version 1");
  821. smartlist_add(diff, (char*)"hash"
  822. /* bogus sha256. */
  823. " 3333333333333333333333333333333333333333333333333333333333333333"
  824. /* sha256 of cons2. */
  825. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  826. mock_clean_saved_logs();
  827. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  828. tt_ptr_op(NULL, OP_EQ, cons2);
  829. expect_log_msg_containing("base consensus doesn't match the digest "
  830. "as found");
  831. /* Resulting consensus doesn't match its digest as found in the diff. */
  832. smartlist_clear(diff);
  833. smartlist_add(diff, (char*)"network-status-diff-version 1");
  834. smartlist_add(diff, (char*)"hash"
  835. /* sha256 of cons1. */
  836. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  837. /* bogus sha256. */
  838. " 3333333333333333333333333333333333333333333333333333333333333333");
  839. mock_clean_saved_logs();
  840. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  841. tt_ptr_op(NULL, OP_EQ, cons2);
  842. expect_log_msg_containing("resulting consensus doesn't match the "
  843. "digest as found");
  844. /* Resulting consensus digest cannot be computed */
  845. smartlist_clear(diff);
  846. smartlist_add(diff, (char*)"network-status-diff-version 1");
  847. smartlist_add(diff, (char*)"hash"
  848. /* sha256 of cons1. */
  849. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  850. /* bogus sha256. */
  851. " 3333333333333333333333333333333333333333333333333333333333333333");
  852. smartlist_add(diff, (char*)"1,2d"); // remove starting line
  853. mock_clean_saved_logs();
  854. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  855. tt_ptr_op(NULL, OP_EQ, cons2);
  856. expect_log_msg_containing("Could not compute digests of the consensus "
  857. "resulting from applying a consensus diff.");
  858. /* Very simple test, only to see that nothing errors. */
  859. smartlist_clear(diff);
  860. smartlist_add(diff, (char*)"network-status-diff-version 1");
  861. smartlist_add(diff, (char*)"hash"
  862. /* sha256 of cons1. */
  863. " C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
  864. /* sha256 of cons2. */
  865. " 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
  866. smartlist_add(diff, (char*)"4c");
  867. smartlist_add(diff, (char*)"sample");
  868. smartlist_add(diff, (char*)".");
  869. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  870. tt_ptr_op(NULL, OP_NE, cons2);
  871. tt_str_op(
  872. "header\nnetwork-status-version foo\n"
  873. "r name ccccccccccccccccc etc\nsample\n"
  874. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  875. "directory-signature foo bar\nbar\n", OP_EQ,
  876. cons2);
  877. tor_free(cons2);
  878. /* Check that lowercase letters in base16-encoded digests work too. */
  879. smartlist_clear(diff);
  880. smartlist_add(diff, (char*)"network-status-diff-version 1");
  881. smartlist_add(diff, (char*)"hash"
  882. /* sha256 of cons1. */
  883. " c2199b6827514f39ed9b3f2e2e73735c6c5468fd636240bb454c526220de702a"
  884. /* sha256 of cons2. */
  885. " 635d34593020c08e5ecd865f9986e29d50028efa62843766a8197ad228a7f6aa");
  886. smartlist_add(diff, (char*)"4c");
  887. smartlist_add(diff, (char*)"sample");
  888. smartlist_add(diff, (char*)".");
  889. cons2 = consdiff_apply_diff(cons1, diff, &digests1);
  890. tt_ptr_op(NULL, OP_NE, cons2);
  891. tt_str_op(
  892. "header\nnetwork-status-version foo\n"
  893. "r name ccccccccccccccccc etc\nsample\n"
  894. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  895. "directory-signature foo bar\nbar\n", OP_EQ,
  896. cons2);
  897. tor_free(cons2);
  898. smartlist_clear(diff);
  899. done:
  900. teardown_capture_of_logs();
  901. tor_free(cons1_str);
  902. smartlist_free(cons1);
  903. smartlist_free(diff);
  904. }
  905. #define CONSDIFF_LEGACY(name) \
  906. { #name, test_consdiff_ ## name , 0, NULL, NULL }
  907. struct testcase_t consdiff_tests[] = {
  908. CONSDIFF_LEGACY(smartlist_slice),
  909. CONSDIFF_LEGACY(smartlist_slice_string_pos),
  910. CONSDIFF_LEGACY(lcs_lengths),
  911. CONSDIFF_LEGACY(trim_slices),
  912. CONSDIFF_LEGACY(set_changed),
  913. CONSDIFF_LEGACY(calc_changes),
  914. CONSDIFF_LEGACY(get_id_hash),
  915. CONSDIFF_LEGACY(is_valid_router_entry),
  916. CONSDIFF_LEGACY(next_router),
  917. CONSDIFF_LEGACY(base64cmp),
  918. CONSDIFF_LEGACY(gen_ed_diff),
  919. CONSDIFF_LEGACY(apply_ed_diff),
  920. CONSDIFF_LEGACY(gen_diff),
  921. CONSDIFF_LEGACY(apply_diff),
  922. END_OF_TESTCASES
  923. };