test_dir.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define DIRSERV_PRIVATE
  7. #define DIRVOTE_PRIVATE
  8. #define ROUTER_PRIVATE
  9. #define HIBERNATE_PRIVATE
  10. #include "or.h"
  11. #include "directory.h"
  12. #include "dirserv.h"
  13. #include "dirvote.h"
  14. #include "hibernate.h"
  15. #include "networkstatus.h"
  16. #include "router.h"
  17. #include "routerlist.h"
  18. #include "routerparse.h"
  19. #include "test.h"
  20. static void
  21. test_dir_nicknames(void)
  22. {
  23. test_assert( is_legal_nickname("a"));
  24. test_assert(!is_legal_nickname(""));
  25. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  26. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  27. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  28. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  29. /* valid */
  30. test_assert( is_legal_nickname_or_hexdigest(
  31. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  32. test_assert( is_legal_nickname_or_hexdigest(
  33. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  34. test_assert( is_legal_nickname_or_hexdigest(
  35. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  36. /* too short */
  37. test_assert(!is_legal_nickname_or_hexdigest(
  38. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  39. /* illegal char */
  40. test_assert(!is_legal_nickname_or_hexdigest(
  41. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  42. /* hex part too long */
  43. test_assert(!is_legal_nickname_or_hexdigest(
  44. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  45. test_assert(!is_legal_nickname_or_hexdigest(
  46. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  47. /* Bad nickname */
  48. test_assert(!is_legal_nickname_or_hexdigest(
  49. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  50. test_assert(!is_legal_nickname_or_hexdigest(
  51. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  52. test_assert(!is_legal_nickname_or_hexdigest(
  53. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  54. test_assert(!is_legal_nickname_or_hexdigest(
  55. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  56. "abcdefghijklmnoppqrst"));
  57. /* Bad extra char. */
  58. test_assert(!is_legal_nickname_or_hexdigest(
  59. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  60. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  61. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  62. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  63. done:
  64. ;
  65. }
  66. /** Run unit tests for router descriptor generation logic. */
  67. static void
  68. test_dir_formats(void)
  69. {
  70. char buf[8192], buf2[8192];
  71. char platform[256];
  72. char fingerprint[FINGERPRINT_LEN+1];
  73. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  74. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  75. routerinfo_t *r1=NULL, *r2=NULL;
  76. crypto_pk_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  77. routerinfo_t *rp1 = NULL;
  78. addr_policy_t *ex1, *ex2;
  79. routerlist_t *dir1 = NULL, *dir2 = NULL;
  80. pk1 = pk_generate(0);
  81. pk2 = pk_generate(1);
  82. pk3 = pk_generate(2);
  83. test_assert(pk1 && pk2 && pk3);
  84. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  85. get_platform_str(platform, sizeof(platform));
  86. r1 = tor_malloc_zero(sizeof(routerinfo_t));
  87. r1->address = tor_strdup("18.244.0.1");
  88. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  89. r1->cache_info.published_on = 0;
  90. r1->or_port = 9000;
  91. r1->dir_port = 9003;
  92. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  93. r1->ipv6_orport = 9999;
  94. r1->onion_pkey = crypto_pk_dup_key(pk1);
  95. r1->identity_pkey = crypto_pk_dup_key(pk2);
  96. r1->bandwidthrate = 1000;
  97. r1->bandwidthburst = 5000;
  98. r1->bandwidthcapacity = 10000;
  99. r1->exit_policy = NULL;
  100. r1->nickname = tor_strdup("Magri");
  101. r1->platform = tor_strdup(platform);
  102. ex1 = tor_malloc_zero(sizeof(addr_policy_t));
  103. ex2 = tor_malloc_zero(sizeof(addr_policy_t));
  104. ex1->policy_type = ADDR_POLICY_ACCEPT;
  105. tor_addr_from_ipv4h(&ex1->addr, 0);
  106. ex1->maskbits = 0;
  107. ex1->prt_min = ex1->prt_max = 80;
  108. ex2->policy_type = ADDR_POLICY_REJECT;
  109. tor_addr_from_ipv4h(&ex2->addr, 18<<24);
  110. ex2->maskbits = 8;
  111. ex2->prt_min = ex2->prt_max = 24;
  112. r2 = tor_malloc_zero(sizeof(routerinfo_t));
  113. r2->address = tor_strdup("1.1.1.1");
  114. r2->addr = 0x0a030201u; /* 10.3.2.1 */
  115. r2->platform = tor_strdup(platform);
  116. r2->cache_info.published_on = 5;
  117. r2->or_port = 9005;
  118. r2->dir_port = 0;
  119. r2->onion_pkey = crypto_pk_dup_key(pk2);
  120. r2->identity_pkey = crypto_pk_dup_key(pk1);
  121. r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
  122. r2->exit_policy = smartlist_new();
  123. smartlist_add(r2->exit_policy, ex2);
  124. smartlist_add(r2->exit_policy, ex1);
  125. r2->nickname = tor_strdup("Fred");
  126. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  127. &pk1_str_len));
  128. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  129. &pk2_str_len));
  130. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  131. &pk3_str_len));
  132. memset(buf, 0, 2048);
  133. test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
  134. strlcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
  135. "or-address [1:2:3:4::]:9999\n"
  136. "platform Tor "VERSION" on ", sizeof(buf2));
  137. strlcat(buf2, get_uname(), sizeof(buf2));
  138. strlcat(buf2, "\n"
  139. "opt protocols Link 1 2 Circuit 1\n"
  140. "published 1970-01-01 00:00:00\n"
  141. "opt fingerprint ", sizeof(buf2));
  142. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  143. strlcat(buf2, fingerprint, sizeof(buf2));
  144. strlcat(buf2, "\nuptime 0\n"
  145. /* XXX the "0" above is hard-coded, but even if we made it reflect
  146. * uptime, that still wouldn't make it right, because the two
  147. * descriptors might be made on different seconds... hm. */
  148. "bandwidth 1000 5000 10000\n"
  149. "onion-key\n", sizeof(buf2));
  150. strlcat(buf2, pk1_str, sizeof(buf2));
  151. strlcat(buf2, "signing-key\n", sizeof(buf2));
  152. strlcat(buf2, pk2_str, sizeof(buf2));
  153. strlcat(buf2, "opt hidden-service-dir\n", sizeof(buf2));
  154. strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
  155. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  156. * twice */
  157. test_streq(buf, buf2);
  158. test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
  159. cp = buf;
  160. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
  161. test_assert(rp1);
  162. test_streq(rp1->address, r1->address);
  163. test_eq(rp1->or_port, r1->or_port);
  164. //test_eq(rp1->dir_port, r1->dir_port);
  165. test_eq(rp1->bandwidthrate, r1->bandwidthrate);
  166. test_eq(rp1->bandwidthburst, r1->bandwidthburst);
  167. test_eq(rp1->bandwidthcapacity, r1->bandwidthcapacity);
  168. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  169. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  170. //test_assert(rp1->exit_policy == NULL);
  171. #if 0
  172. /* XXX Once we have exit policies, test this again. XXX */
  173. strlcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2));
  174. strlcat(buf2, pk2_str, sizeof(buf2));
  175. strlcat(buf2, "signing-key\n", sizeof(buf2));
  176. strlcat(buf2, pk1_str, sizeof(buf2));
  177. strlcat(buf2, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2));
  178. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  179. test_streq(buf, buf2);
  180. cp = buf;
  181. rp2 = router_parse_entry_from_string(&cp,1);
  182. test_assert(rp2);
  183. test_streq(rp2->address, r2.address);
  184. test_eq(rp2->or_port, r2.or_port);
  185. test_eq(rp2->dir_port, r2.dir_port);
  186. test_eq(rp2->bandwidth, r2.bandwidth);
  187. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  188. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  189. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  190. test_streq(rp2->exit_policy->string, "accept *:80");
  191. test_streq(rp2->exit_policy->address, "*");
  192. test_streq(rp2->exit_policy->port, "80");
  193. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  194. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  195. test_streq(rp2->exit_policy->next->address, "18.*");
  196. test_streq(rp2->exit_policy->next->port, "24");
  197. test_assert(rp2->exit_policy->next->next == NULL);
  198. /* Okay, now for the directories. */
  199. {
  200. fingerprint_list = smartlist_new();
  201. crypto_pk_get_fingerprint(pk2, buf, 1);
  202. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  203. crypto_pk_get_fingerprint(pk1, buf, 1);
  204. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  205. }
  206. {
  207. char d[DIGEST_LEN];
  208. const char *m;
  209. /* XXXX NM re-enable. */
  210. /* Make sure routers aren't too far in the past any more. */
  211. r1->cache_info.published_on = time(NULL);
  212. r2->cache_info.published_on = time(NULL)-3*60*60;
  213. test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
  214. test_eq(dirserv_add_descriptor(buf,&m,""), ROUTER_ADDED_NOTIFY_GENERATOR);
  215. test_assert(router_dump_router_to_string(buf, 2048, r2, pk1)>0);
  216. test_eq(dirserv_add_descriptor(buf,&m,""), ROUTER_ADDED_NOTIFY_GENERATOR);
  217. get_options()->Nickname = tor_strdup("DirServer");
  218. test_assert(!dirserv_dump_directory_to_string(&cp,pk3, 0));
  219. crypto_pk_get_digest(pk3, d);
  220. test_assert(!router_parse_directory(cp));
  221. test_eq(2, smartlist_len(dir1->routers));
  222. tor_free(cp);
  223. }
  224. #endif
  225. dirserv_free_fingerprint_list();
  226. done:
  227. if (r1)
  228. routerinfo_free(r1);
  229. if (r2)
  230. routerinfo_free(r2);
  231. tor_free(pk1_str);
  232. tor_free(pk2_str);
  233. tor_free(pk3_str);
  234. if (pk1) crypto_pk_free(pk1);
  235. if (pk2) crypto_pk_free(pk2);
  236. if (pk3) crypto_pk_free(pk3);
  237. if (rp1) routerinfo_free(rp1);
  238. tor_free(dir1); /* XXXX And more !*/
  239. tor_free(dir2); /* And more !*/
  240. }
  241. static void
  242. test_dir_versions(void)
  243. {
  244. tor_version_t ver1;
  245. /* Try out version parsing functionality */
  246. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  247. test_eq(0, ver1.major);
  248. test_eq(3, ver1.minor);
  249. test_eq(4, ver1.micro);
  250. test_eq(VER_PRE, ver1.status);
  251. test_eq(2, ver1.patchlevel);
  252. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  253. test_eq(0, ver1.major);
  254. test_eq(3, ver1.minor);
  255. test_eq(4, ver1.micro);
  256. test_eq(VER_RC, ver1.status);
  257. test_eq(1, ver1.patchlevel);
  258. test_eq(0, tor_version_parse("1.3.4", &ver1));
  259. test_eq(1, ver1.major);
  260. test_eq(3, ver1.minor);
  261. test_eq(4, ver1.micro);
  262. test_eq(VER_RELEASE, ver1.status);
  263. test_eq(0, ver1.patchlevel);
  264. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  265. test_eq(1, ver1.major);
  266. test_eq(3, ver1.minor);
  267. test_eq(4, ver1.micro);
  268. test_eq(VER_RELEASE, ver1.status);
  269. test_eq(999, ver1.patchlevel);
  270. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  271. test_eq(0, ver1.major);
  272. test_eq(1, ver1.minor);
  273. test_eq(2, ver1.micro);
  274. test_eq(4, ver1.patchlevel);
  275. test_eq(VER_RELEASE, ver1.status);
  276. test_streq("alpha", ver1.status_tag);
  277. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  278. test_eq(0, ver1.major);
  279. test_eq(1, ver1.minor);
  280. test_eq(2, ver1.micro);
  281. test_eq(4, ver1.patchlevel);
  282. test_eq(VER_RELEASE, ver1.status);
  283. test_streq("", ver1.status_tag);
  284. #define tt_versionstatus_op(vs1, op, vs2) \
  285. tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
  286. (_val1 op _val2),"%d",TT_EXIT_TEST_FUNCTION)
  287. #define test_v_i_o(val, ver, lst) \
  288. tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
  289. /* make sure tor_version_is_obsolete() works */
  290. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  291. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  292. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  293. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  294. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  295. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  296. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  297. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  298. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  299. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  300. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  301. /* Not on list, but newer than any in same series. */
  302. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  303. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  304. /* Series newer than any on list. */
  305. test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  306. /* Series older than any on list. */
  307. test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  308. /* Not on list, not newer than any on same series. */
  309. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  310. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  311. /* On list, not newer than any on same series. */
  312. test_v_i_o(VS_UNRECOMMENDED,
  313. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  314. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  315. test_eq(1, tor_version_as_new_as(
  316. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  317. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  318. "0.0.8rc2"));
  319. test_eq(0, tor_version_as_new_as(
  320. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  321. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  322. /* Now try svn revisions. */
  323. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  324. "Tor 0.2.1.0-dev (r99)"));
  325. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
  326. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  327. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  328. "Tor 0.2.1.0-dev on Colossus"));
  329. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  330. "Tor 0.2.1.0-dev (r100)"));
  331. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  332. "Tor 0.2.1.0-dev (r100) on AM"));
  333. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
  334. "Tor 0.2.1.0-dev (r99)"));
  335. test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
  336. "Tor 0.2.1.0-dev (r99)"));
  337. /* Now try git revisions */
  338. test_eq(0, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
  339. test_eq(0, ver1.major);
  340. test_eq(5, ver1.minor);
  341. test_eq(6, ver1.micro);
  342. test_eq(7, ver1.patchlevel);
  343. test_eq(3, ver1.git_tag_len);
  344. test_memeq(ver1.git_tag, "\xff\x00\xff", 3);
  345. test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
  346. test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
  347. test_eq(0, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
  348. done:
  349. ;
  350. }
  351. /** Run unit tests for directory fp_pair functions. */
  352. static void
  353. test_dir_fp_pairs(void)
  354. {
  355. smartlist_t *sl = smartlist_new();
  356. fp_pair_t *pair;
  357. dir_split_resource_into_fingerprint_pairs(
  358. /* Two pairs, out of order, with one duplicate. */
  359. "73656372657420646174612E0000000000FFFFFF-"
  360. "557365204145532d32353620696e73746561642e+"
  361. "73656372657420646174612E0000000000FFFFFF-"
  362. "557365204145532d32353620696e73746561642e+"
  363. "48657861646563696d616c2069736e277420736f-"
  364. "676f6f6420666f7220686964696e6720796f7572.z", sl);
  365. test_eq(smartlist_len(sl), 2);
  366. pair = smartlist_get(sl, 0);
  367. test_memeq(pair->first, "Hexadecimal isn't so", DIGEST_LEN);
  368. test_memeq(pair->second, "good for hiding your", DIGEST_LEN);
  369. pair = smartlist_get(sl, 1);
  370. test_memeq(pair->first, "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN);
  371. test_memeq(pair->second, "Use AES-256 instead.", DIGEST_LEN);
  372. done:
  373. SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
  374. smartlist_free(sl);
  375. }
  376. static void
  377. test_dir_split_fps(void *testdata)
  378. {
  379. smartlist_t *sl = smartlist_new();
  380. char *mem_op_hex_tmp = NULL;
  381. (void)testdata;
  382. /* Some example hex fingerprints and their base64 equivalents */
  383. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  384. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  385. #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
  386. #define HEX256_1 \
  387. "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
  388. #define HEX256_2 \
  389. "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
  390. #define HEX256_3 \
  391. "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
  392. #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
  393. #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
  394. #define B64_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
  395. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  396. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  397. #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
  398. /* no flags set */
  399. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  400. tt_int_op(smartlist_len(sl), ==, 3);
  401. tt_str_op(smartlist_get(sl, 0), ==, "A");
  402. tt_str_op(smartlist_get(sl, 1), ==, "C");
  403. tt_str_op(smartlist_get(sl, 2), ==, "B");
  404. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  405. smartlist_clear(sl);
  406. /* uniq strings. */
  407. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  408. tt_int_op(smartlist_len(sl), ==, 3);
  409. tt_str_op(smartlist_get(sl, 0), ==, "A");
  410. tt_str_op(smartlist_get(sl, 1), ==, "B");
  411. tt_str_op(smartlist_get(sl, 2), ==, "C");
  412. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  413. smartlist_clear(sl);
  414. /* Decode hex. */
  415. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  416. tt_int_op(smartlist_len(sl), ==, 2);
  417. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  418. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  419. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  420. smartlist_clear(sl);
  421. /* decode hex and drop weirdness. */
  422. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  423. sl, NULL, DSR_HEX);
  424. tt_int_op(smartlist_len(sl), ==, 2);
  425. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  426. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  427. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  428. smartlist_clear(sl);
  429. /* Decode long hex */
  430. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  431. sl, NULL, DSR_HEX|DSR_DIGEST256);
  432. tt_int_op(smartlist_len(sl), ==, 3);
  433. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  434. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  435. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
  436. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  437. smartlist_clear(sl);
  438. /* Decode hex and sort. */
  439. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  440. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  441. tt_int_op(smartlist_len(sl), ==, 3);
  442. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
  443. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  444. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
  445. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  446. smartlist_clear(sl);
  447. /* Decode long hex and sort */
  448. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  449. "+"HEX256_1,
  450. sl, NULL,
  451. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  452. tt_int_op(smartlist_len(sl), ==, 3);
  453. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
  454. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  455. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
  456. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  457. smartlist_clear(sl);
  458. /* Decode base64 */
  459. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  460. tt_int_op(smartlist_len(sl), ==, 2);
  461. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  462. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  463. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  464. smartlist_clear(sl);
  465. /* Decode long base64 */
  466. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  467. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  468. tt_int_op(smartlist_len(sl), ==, 2);
  469. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  470. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  471. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  472. smartlist_clear(sl);
  473. dir_split_resource_into_fingerprints(B64_256_1,
  474. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  475. tt_int_op(smartlist_len(sl), ==, 1);
  476. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  477. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  478. smartlist_clear(sl);
  479. done:
  480. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  481. smartlist_free(sl);
  482. tor_free(mem_op_hex_tmp);
  483. }
  484. static void
  485. test_dir_measured_bw(void)
  486. {
  487. measured_bw_line_t mbwl;
  488. int i;
  489. const char *lines_pass[] = {
  490. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  491. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  492. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  493. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  494. "bw=1024 junk=007\n",
  495. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  496. "bw=1024 junk=007\n",
  497. "end"
  498. };
  499. const char *lines_fail[] = {
  500. /* Test possible python stupidity on input */
  501. "node_id=None bw=1024\n",
  502. "node_id=$None bw=1024\n",
  503. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  504. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  505. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  506. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  507. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  508. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  509. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  510. /* Test incomplete writes due to race conditions, partial copies, etc */
  511. "node_i",
  512. "node_i\n",
  513. "node_id=",
  514. "node_id=\n",
  515. "node_id=$557365204145532d32353620696e73746561642e bw=",
  516. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  517. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  518. "node_id=$557365204145532d32353620696e7374",
  519. "node_id=$557365204145532d32353620696e7374\n",
  520. "",
  521. "\n",
  522. " \n ",
  523. " \n\n",
  524. /* Test assorted noise */
  525. " node_id= ",
  526. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  527. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  528. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  529. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  530. "end"
  531. };
  532. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  533. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  534. test_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
  535. }
  536. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  537. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  538. test_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
  539. test_assert(mbwl.bw == 1024);
  540. test_assert(strcmp(mbwl.node_hex,
  541. "557365204145532d32353620696e73746561642e") == 0);
  542. }
  543. done:
  544. return;
  545. }
  546. static void
  547. test_dir_param_voting(void)
  548. {
  549. networkstatus_t vote1, vote2, vote3, vote4;
  550. smartlist_t *votes = smartlist_new();
  551. char *res = NULL;
  552. /* dirvote_compute_params only looks at the net_params field of the votes,
  553. so that's all we need to set.
  554. */
  555. memset(&vote1, 0, sizeof(vote1));
  556. memset(&vote2, 0, sizeof(vote2));
  557. memset(&vote3, 0, sizeof(vote3));
  558. memset(&vote4, 0, sizeof(vote4));
  559. vote1.net_params = smartlist_new();
  560. vote2.net_params = smartlist_new();
  561. vote3.net_params = smartlist_new();
  562. vote4.net_params = smartlist_new();
  563. smartlist_split_string(vote1.net_params,
  564. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  565. smartlist_split_string(vote2.net_params,
  566. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  567. smartlist_split_string(vote3.net_params,
  568. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  569. smartlist_split_string(vote4.net_params,
  570. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  571. test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  572. test_eq(222, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  573. test_eq(80, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  574. test_eq(-8, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  575. test_eq(0, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  576. smartlist_add(votes, &vote1);
  577. /* Do the first tests without adding all the other votes, for
  578. * networks without many dirauths. */
  579. res = dirvote_compute_params(votes, 11, 6);
  580. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  581. tor_free(res);
  582. res = dirvote_compute_params(votes, 12, 2);
  583. test_streq(res, "");
  584. tor_free(res);
  585. res = dirvote_compute_params(votes, 12, 1);
  586. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  587. tor_free(res);
  588. smartlist_add(votes, &vote2);
  589. res = dirvote_compute_params(votes, 11, 2);
  590. test_streq(res, "ab=27 abcd=20 cw=5 x-yz=-99");
  591. tor_free(res);
  592. res = dirvote_compute_params(votes, 12, 2);
  593. test_streq(res, "ab=27 cw=5 x-yz=-99");
  594. tor_free(res);
  595. res = dirvote_compute_params(votes, 12, 3);
  596. test_streq(res, "ab=27 cw=5 x-yz=-99");
  597. tor_free(res);
  598. res = dirvote_compute_params(votes, 12, 6);
  599. test_streq(res, "");
  600. tor_free(res);
  601. smartlist_add(votes, &vote3);
  602. res = dirvote_compute_params(votes, 11, 3);
  603. test_streq(res, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
  604. tor_free(res);
  605. res = dirvote_compute_params(votes, 12, 3);
  606. test_streq(res, "ab=27 abcd=20 cw=50 x-yz=-9");
  607. tor_free(res);
  608. res = dirvote_compute_params(votes, 12, 5);
  609. test_streq(res, "cw=50 x-yz=-9");
  610. tor_free(res);
  611. res = dirvote_compute_params(votes, 12, 9);
  612. test_streq(res, "cw=50 x-yz=-9");
  613. tor_free(res);
  614. smartlist_add(votes, &vote4);
  615. res = dirvote_compute_params(votes, 11, 4);
  616. test_streq(res, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
  617. tor_free(res);
  618. res = dirvote_compute_params(votes, 12, 4);
  619. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  620. tor_free(res);
  621. res = dirvote_compute_params(votes, 12, 5);
  622. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  623. tor_free(res);
  624. /* Test that the special-cased "at least three dirauths voted for
  625. * this param" logic works as expected. */
  626. res = dirvote_compute_params(votes, 12, 6);
  627. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  628. tor_free(res);
  629. res = dirvote_compute_params(votes, 12, 10);
  630. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  631. tor_free(res);
  632. done:
  633. tor_free(res);
  634. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  635. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  636. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  637. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  638. smartlist_free(vote1.net_params);
  639. smartlist_free(vote2.net_params);
  640. smartlist_free(vote3.net_params);
  641. smartlist_free(vote4.net_params);
  642. smartlist_free(votes);
  643. return;
  644. }
  645. extern const char AUTHORITY_CERT_1[];
  646. extern const char AUTHORITY_SIGNKEY_1[];
  647. extern const char AUTHORITY_CERT_2[];
  648. extern const char AUTHORITY_SIGNKEY_2[];
  649. extern const char AUTHORITY_CERT_3[];
  650. extern const char AUTHORITY_SIGNKEY_3[];
  651. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  652. * same voting authority, and that they do in fact have all the same
  653. * information. */
  654. static void
  655. test_same_voter(networkstatus_voter_info_t *v1,
  656. networkstatus_voter_info_t *v2)
  657. {
  658. test_streq(v1->nickname, v2->nickname);
  659. test_memeq(v1->identity_digest, v2->identity_digest, DIGEST_LEN);
  660. test_streq(v1->address, v2->address);
  661. test_eq(v1->addr, v2->addr);
  662. test_eq(v1->dir_port, v2->dir_port);
  663. test_eq(v1->or_port, v2->or_port);
  664. test_streq(v1->contact, v2->contact);
  665. test_memeq(v1->vote_digest, v2->vote_digest, DIGEST_LEN);
  666. done:
  667. ;
  668. }
  669. /** Helper: Make a new routerinfo containing the right information for a
  670. * given vote_routerstatus_t. */
  671. static routerinfo_t *
  672. generate_ri_from_rs(const vote_routerstatus_t *vrs)
  673. {
  674. routerinfo_t *r;
  675. const routerstatus_t *rs = &vrs->status;
  676. static time_t published = 0;
  677. r = tor_malloc_zero(sizeof(routerinfo_t));
  678. memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
  679. memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
  680. DIGEST_LEN);
  681. r->cache_info.do_not_cache = 1;
  682. r->cache_info.routerlist_index = -1;
  683. r->cache_info.signed_descriptor_body =
  684. tor_strdup("123456789012345678901234567890123");
  685. r->cache_info.signed_descriptor_len =
  686. strlen(r->cache_info.signed_descriptor_body);
  687. r->exit_policy = smartlist_new();
  688. r->cache_info.published_on = ++published + time(NULL);
  689. return r;
  690. }
  691. /** Helper: get a detached signatures document for one or two
  692. * consensuses. */
  693. static char *
  694. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  695. {
  696. char *r;
  697. smartlist_t *sl;
  698. tor_assert(ns && ns->flavor == FLAV_NS);
  699. sl = smartlist_new();
  700. smartlist_add(sl,ns);
  701. if (ns2)
  702. smartlist_add(sl,ns2);
  703. r = networkstatus_get_detached_signatures(sl);
  704. smartlist_free(sl);
  705. return r;
  706. }
  707. /** Run unit tests for generating and parsing V3 consensus networkstatus
  708. * documents. */
  709. static void
  710. test_dir_v3_networkstatus(void)
  711. {
  712. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  713. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  714. crypto_pk_t *sign_skey_leg1=NULL;
  715. const char *msg=NULL;
  716. time_t now = time(NULL);
  717. networkstatus_voter_info_t *voter;
  718. document_signature_t *sig;
  719. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  720. *con_md=NULL;
  721. vote_routerstatus_t *vrs;
  722. routerstatus_t *rs;
  723. char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL, *cp;
  724. smartlist_t *votes = smartlist_new();
  725. /* For generating the two other consensuses. */
  726. char *detached_text1=NULL, *detached_text2=NULL;
  727. char *consensus_text2=NULL, *consensus_text3=NULL;
  728. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  729. char *consensus_text_md=NULL;
  730. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  731. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  732. /* Parse certificates and keys. */
  733. cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  734. test_assert(cert1);
  735. test_assert(cert1->is_cross_certified);
  736. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  737. test_assert(cert2);
  738. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  739. test_assert(cert3);
  740. sign_skey_1 = crypto_pk_new();
  741. sign_skey_2 = crypto_pk_new();
  742. sign_skey_3 = crypto_pk_new();
  743. sign_skey_leg1 = pk_generate(4);
  744. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  745. AUTHORITY_SIGNKEY_1, -1));
  746. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  747. AUTHORITY_SIGNKEY_2, -1));
  748. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  749. AUTHORITY_SIGNKEY_3, -1));
  750. test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  751. test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  752. /*
  753. * Set up a vote; generate it; try to parse it.
  754. */
  755. vote = tor_malloc_zero(sizeof(networkstatus_t));
  756. vote->type = NS_TYPE_VOTE;
  757. vote->published = now;
  758. vote->valid_after = now+1000;
  759. vote->fresh_until = now+2000;
  760. vote->valid_until = now+3000;
  761. vote->vote_seconds = 100;
  762. vote->dist_seconds = 200;
  763. vote->supported_methods = smartlist_new();
  764. smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
  765. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  766. vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  767. vote->known_flags = smartlist_new();
  768. smartlist_split_string(vote->known_flags,
  769. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  770. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  771. vote->voters = smartlist_new();
  772. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  773. voter->nickname = tor_strdup("Voter1");
  774. voter->address = tor_strdup("1.2.3.4");
  775. voter->addr = 0x01020304;
  776. voter->dir_port = 80;
  777. voter->or_port = 9000;
  778. voter->contact = tor_strdup("voter@example.com");
  779. crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
  780. smartlist_add(vote->voters, voter);
  781. vote->cert = authority_cert_dup(cert1);
  782. vote->net_params = smartlist_new();
  783. smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
  784. NULL, 0, 0);
  785. vote->routerstatus_list = smartlist_new();
  786. /* add the first routerstatus. */
  787. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  788. rs = &vrs->status;
  789. vrs->version = tor_strdup("0.1.2.14");
  790. rs->published_on = now-1500;
  791. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  792. memset(rs->identity_digest, 3, DIGEST_LEN);
  793. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  794. rs->addr = 0x99008801;
  795. rs->or_port = 443;
  796. rs->dir_port = 8000;
  797. /* all flags but running cleared */
  798. rs->is_flagged_running = 1;
  799. smartlist_add(vote->routerstatus_list, vrs);
  800. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
  801. /* add the second routerstatus. */
  802. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  803. rs = &vrs->status;
  804. vrs->version = tor_strdup("0.2.0.5");
  805. rs->published_on = now-1000;
  806. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  807. memset(rs->identity_digest, 5, DIGEST_LEN);
  808. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  809. rs->addr = 0x99009901;
  810. rs->or_port = 443;
  811. rs->dir_port = 0;
  812. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  813. rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
  814. smartlist_add(vote->routerstatus_list, vrs);
  815. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
  816. /* add the third routerstatus. */
  817. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  818. rs = &vrs->status;
  819. vrs->version = tor_strdup("0.1.0.3");
  820. rs->published_on = now-1000;
  821. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  822. memset(rs->identity_digest, 33, DIGEST_LEN);
  823. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  824. rs->addr = 0xAA009901;
  825. rs->or_port = 400;
  826. rs->dir_port = 9999;
  827. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  828. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  829. rs->is_possible_guard = 1;
  830. smartlist_add(vote->routerstatus_list, vrs);
  831. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
  832. /* add a fourth routerstatus that is not running. */
  833. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  834. rs = &vrs->status;
  835. vrs->version = tor_strdup("0.1.6.3");
  836. rs->published_on = now-1000;
  837. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  838. memset(rs->identity_digest, 34, DIGEST_LEN);
  839. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  840. rs->addr = 0xC0000203;
  841. rs->or_port = 500;
  842. rs->dir_port = 1999;
  843. /* Running flag (and others) cleared */
  844. smartlist_add(vote->routerstatus_list, vrs);
  845. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
  846. /* dump the vote and try to parse it. */
  847. v1_text = format_networkstatus_vote(sign_skey_1, vote);
  848. test_assert(v1_text);
  849. v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
  850. test_assert(v1);
  851. /* Make sure the parsed thing was right. */
  852. test_eq(v1->type, NS_TYPE_VOTE);
  853. test_eq(v1->published, vote->published);
  854. test_eq(v1->valid_after, vote->valid_after);
  855. test_eq(v1->fresh_until, vote->fresh_until);
  856. test_eq(v1->valid_until, vote->valid_until);
  857. test_eq(v1->vote_seconds, vote->vote_seconds);
  858. test_eq(v1->dist_seconds, vote->dist_seconds);
  859. test_streq(v1->client_versions, vote->client_versions);
  860. test_streq(v1->server_versions, vote->server_versions);
  861. test_assert(v1->voters && smartlist_len(v1->voters));
  862. voter = smartlist_get(v1->voters, 0);
  863. test_streq(voter->nickname, "Voter1");
  864. test_streq(voter->address, "1.2.3.4");
  865. test_eq(voter->addr, 0x01020304);
  866. test_eq(voter->dir_port, 80);
  867. test_eq(voter->or_port, 9000);
  868. test_streq(voter->contact, "voter@example.com");
  869. test_assert(v1->cert);
  870. test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  871. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  872. test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  873. tor_free(cp);
  874. test_eq(smartlist_len(v1->routerstatus_list), 4);
  875. /* Check the first routerstatus. */
  876. vrs = smartlist_get(v1->routerstatus_list, 0);
  877. rs = &vrs->status;
  878. test_streq(vrs->version, "0.1.2.14");
  879. test_eq(rs->published_on, now-1500);
  880. test_streq(rs->nickname, "router2");
  881. test_memeq(rs->identity_digest,
  882. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  883. DIGEST_LEN);
  884. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  885. test_eq(rs->addr, 0x99008801);
  886. test_eq(rs->or_port, 443);
  887. test_eq(rs->dir_port, 8000);
  888. test_eq(vrs->flags, U64_LITERAL(16)); // no flags except "running"
  889. /* Check the second routerstatus. */
  890. vrs = smartlist_get(v1->routerstatus_list, 1);
  891. rs = &vrs->status;
  892. test_streq(vrs->version, "0.2.0.5");
  893. test_eq(rs->published_on, now-1000);
  894. test_streq(rs->nickname, "router1");
  895. test_memeq(rs->identity_digest,
  896. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  897. DIGEST_LEN);
  898. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  899. test_eq(rs->addr, 0x99009901);
  900. test_eq(rs->or_port, 443);
  901. test_eq(rs->dir_port, 0);
  902. test_eq(vrs->flags, U64_LITERAL(254)); // all flags except "authority."
  903. {
  904. measured_bw_line_t mbw;
  905. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  906. mbw.bw = 1024;
  907. test_assert(measured_bw_line_apply(&mbw,
  908. v1->routerstatus_list) == 1);
  909. vrs = smartlist_get(v1->routerstatus_list, 2);
  910. test_assert(vrs->status.has_measured_bw &&
  911. vrs->status.measured_bw == 1024);
  912. }
  913. /* Generate second vote. It disagrees on some of the times,
  914. * and doesn't list versions, and knows some crazy flags */
  915. vote->published = now+1;
  916. vote->fresh_until = now+3005;
  917. vote->dist_seconds = 300;
  918. authority_cert_free(vote->cert);
  919. vote->cert = authority_cert_dup(cert2);
  920. vote->net_params = smartlist_new();
  921. smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
  922. NULL, 0, 0);
  923. tor_free(vote->client_versions);
  924. tor_free(vote->server_versions);
  925. voter = smartlist_get(vote->voters, 0);
  926. tor_free(voter->nickname);
  927. tor_free(voter->address);
  928. voter->nickname = tor_strdup("Voter2");
  929. voter->address = tor_strdup("2.3.4.5");
  930. voter->addr = 0x02030405;
  931. crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
  932. smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
  933. smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
  934. smartlist_sort_strings(vote->known_flags);
  935. vrs = smartlist_get(vote->routerstatus_list, 2);
  936. smartlist_del_keeporder(vote->routerstatus_list, 2);
  937. tor_free(vrs->version);
  938. tor_free(vrs);
  939. vrs = smartlist_get(vote->routerstatus_list, 0);
  940. vrs->status.is_fast = 1;
  941. /* generate and parse. */
  942. v2_text = format_networkstatus_vote(sign_skey_2, vote);
  943. test_assert(v2_text);
  944. v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
  945. test_assert(v2);
  946. /* Check that flags come out right.*/
  947. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  948. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  949. "Running:Stable:V2Dir:Valid");
  950. tor_free(cp);
  951. vrs = smartlist_get(v2->routerstatus_list, 1);
  952. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
  953. test_eq(vrs->flags, U64_LITERAL(974));
  954. /* Generate the third vote. */
  955. vote->published = now;
  956. vote->fresh_until = now+2003;
  957. vote->dist_seconds = 250;
  958. authority_cert_free(vote->cert);
  959. vote->cert = authority_cert_dup(cert3);
  960. vote->net_params = smartlist_new();
  961. smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
  962. NULL, 0, 0);
  963. smartlist_add(vote->supported_methods, tor_strdup("4"));
  964. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
  965. vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
  966. voter = smartlist_get(vote->voters, 0);
  967. tor_free(voter->nickname);
  968. tor_free(voter->address);
  969. voter->nickname = tor_strdup("Voter3");
  970. voter->address = tor_strdup("3.4.5.6");
  971. voter->addr = 0x03040506;
  972. crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
  973. /* This one has a legacy id. */
  974. memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
  975. vrs = smartlist_get(vote->routerstatus_list, 0);
  976. smartlist_del_keeporder(vote->routerstatus_list, 0);
  977. tor_free(vrs->version);
  978. tor_free(vrs);
  979. vrs = smartlist_get(vote->routerstatus_list, 0);
  980. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  981. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
  982. v3_text = format_networkstatus_vote(sign_skey_3, vote);
  983. test_assert(v3_text);
  984. v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
  985. test_assert(v3);
  986. /* Compute a consensus as voter 3. */
  987. smartlist_add(votes, v3);
  988. smartlist_add(votes, v1);
  989. smartlist_add(votes, v2);
  990. consensus_text = networkstatus_compute_consensus(votes, 3,
  991. cert3->identity_key,
  992. sign_skey_3,
  993. "AAAAAAAAAAAAAAAAAAAA",
  994. sign_skey_leg1,
  995. FLAV_NS);
  996. test_assert(consensus_text);
  997. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  998. NS_TYPE_CONSENSUS);
  999. test_assert(con);
  1000. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1001. // v1_text, v2_text, v3_text);
  1002. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1003. cert3->identity_key,
  1004. sign_skey_3,
  1005. "AAAAAAAAAAAAAAAAAAAA",
  1006. sign_skey_leg1,
  1007. FLAV_MICRODESC);
  1008. test_assert(consensus_text_md);
  1009. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1010. NS_TYPE_CONSENSUS);
  1011. test_assert(con_md);
  1012. test_eq(con_md->flavor, FLAV_MICRODESC);
  1013. /* Check consensus contents. */
  1014. test_assert(con->type == NS_TYPE_CONSENSUS);
  1015. test_eq(con->published, 0); /* this field only appears in votes. */
  1016. test_eq(con->valid_after, now+1000);
  1017. test_eq(con->fresh_until, now+2003); /* median */
  1018. test_eq(con->valid_until, now+3000);
  1019. test_eq(con->vote_seconds, 100);
  1020. test_eq(con->dist_seconds, 250); /* median */
  1021. test_streq(con->client_versions, "0.1.2.14");
  1022. test_streq(con->server_versions, "0.1.2.15,0.1.2.16");
  1023. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1024. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1025. "Running:Stable:V2Dir:Valid");
  1026. tor_free(cp);
  1027. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1028. test_streq(cp, "circuitwindow=80:foo=660");
  1029. tor_free(cp);
  1030. test_eq(4, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1031. /* The voter id digests should be in this order. */
  1032. test_assert(memcmp(cert2->cache_info.identity_digest,
  1033. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1034. test_assert(memcmp(cert1->cache_info.identity_digest,
  1035. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1036. test_same_voter(smartlist_get(con->voters, 1),
  1037. smartlist_get(v2->voters, 0));
  1038. test_same_voter(smartlist_get(con->voters, 2),
  1039. smartlist_get(v1->voters, 0));
  1040. test_same_voter(smartlist_get(con->voters, 3),
  1041. smartlist_get(v3->voters, 0));
  1042. test_assert(!con->cert);
  1043. test_eq(2, smartlist_len(con->routerstatus_list));
  1044. /* There should be two listed routers: one with identity 3, one with
  1045. * identity 5. */
  1046. /* This one showed up in 2 digests. */
  1047. rs = smartlist_get(con->routerstatus_list, 0);
  1048. test_memeq(rs->identity_digest,
  1049. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1050. DIGEST_LEN);
  1051. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1052. test_assert(!rs->is_authority);
  1053. test_assert(!rs->is_exit);
  1054. test_assert(!rs->is_fast);
  1055. test_assert(!rs->is_possible_guard);
  1056. test_assert(!rs->is_stable);
  1057. /* (If it wasn't running it wouldn't be here) */
  1058. test_assert(rs->is_flagged_running);
  1059. test_assert(!rs->is_v2_dir);
  1060. test_assert(!rs->is_valid);
  1061. test_assert(!rs->is_named);
  1062. /* XXXX check version */
  1063. rs = smartlist_get(con->routerstatus_list, 1);
  1064. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  1065. test_memeq(rs->identity_digest,
  1066. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1067. DIGEST_LEN);
  1068. test_streq(rs->nickname, "router1");
  1069. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1070. test_eq(rs->published_on, now-1000);
  1071. test_eq(rs->addr, 0x99009901);
  1072. test_eq(rs->or_port, 443);
  1073. test_eq(rs->dir_port, 0);
  1074. test_assert(!rs->is_authority);
  1075. test_assert(rs->is_exit);
  1076. test_assert(rs->is_fast);
  1077. test_assert(rs->is_possible_guard);
  1078. test_assert(rs->is_stable);
  1079. test_assert(rs->is_flagged_running);
  1080. test_assert(rs->is_v2_dir);
  1081. test_assert(rs->is_valid);
  1082. test_assert(!rs->is_named);
  1083. /* XXXX check version */
  1084. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1085. * The second one hasn't signed. The fourth one has signed: validate it. */
  1086. voter = smartlist_get(con->voters, 1);
  1087. test_eq(smartlist_len(voter->sigs), 0);
  1088. voter = smartlist_get(con->voters, 3);
  1089. test_eq(smartlist_len(voter->sigs), 1);
  1090. sig = smartlist_get(voter->sigs, 0);
  1091. test_assert(sig->signature);
  1092. test_assert(!sig->good_signature);
  1093. test_assert(!sig->bad_signature);
  1094. test_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1095. test_assert(sig->signature);
  1096. test_assert(sig->good_signature);
  1097. test_assert(!sig->bad_signature);
  1098. {
  1099. const char *msg=NULL;
  1100. /* Compute the other two signed consensuses. */
  1101. smartlist_shuffle(votes);
  1102. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1103. cert2->identity_key,
  1104. sign_skey_2, NULL,NULL,
  1105. FLAV_NS);
  1106. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1107. cert2->identity_key,
  1108. sign_skey_2, NULL,NULL,
  1109. FLAV_MICRODESC);
  1110. smartlist_shuffle(votes);
  1111. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1112. cert1->identity_key,
  1113. sign_skey_1, NULL,NULL,
  1114. FLAV_NS);
  1115. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1116. cert1->identity_key,
  1117. sign_skey_1, NULL,NULL,
  1118. FLAV_MICRODESC);
  1119. test_assert(consensus_text2);
  1120. test_assert(consensus_text3);
  1121. test_assert(consensus_text_md2);
  1122. test_assert(consensus_text_md3);
  1123. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1124. NS_TYPE_CONSENSUS);
  1125. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1126. NS_TYPE_CONSENSUS);
  1127. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1128. NS_TYPE_CONSENSUS);
  1129. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1130. NS_TYPE_CONSENSUS);
  1131. test_assert(con2);
  1132. test_assert(con3);
  1133. test_assert(con_md2);
  1134. test_assert(con_md3);
  1135. /* All three should have the same digest. */
  1136. test_memeq(&con->digests, &con2->digests, sizeof(digests_t));
  1137. test_memeq(&con->digests, &con3->digests, sizeof(digests_t));
  1138. test_memeq(&con_md->digests, &con_md2->digests, sizeof(digests_t));
  1139. test_memeq(&con_md->digests, &con_md3->digests, sizeof(digests_t));
  1140. /* Extract a detached signature from con3. */
  1141. detached_text1 = get_detached_sigs(con3, con_md3);
  1142. tt_assert(detached_text1);
  1143. /* Try to parse it. */
  1144. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1145. tt_assert(dsig1);
  1146. /* Are parsed values as expected? */
  1147. test_eq(dsig1->valid_after, con3->valid_after);
  1148. test_eq(dsig1->fresh_until, con3->fresh_until);
  1149. test_eq(dsig1->valid_until, con3->valid_until);
  1150. {
  1151. digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1152. test_assert(dsig_digests);
  1153. test_memeq(dsig_digests->d[DIGEST_SHA1], con3->digests.d[DIGEST_SHA1],
  1154. DIGEST_LEN);
  1155. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1156. test_assert(dsig_digests);
  1157. test_memeq(dsig_digests->d[DIGEST_SHA256],
  1158. con_md3->digests.d[DIGEST_SHA256],
  1159. DIGEST256_LEN);
  1160. }
  1161. {
  1162. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1163. test_assert(dsig_signatures);
  1164. test_eq(1, smartlist_len(dsig_signatures));
  1165. sig = smartlist_get(dsig_signatures, 0);
  1166. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1167. DIGEST_LEN);
  1168. test_eq(sig->alg, DIGEST_SHA1);
  1169. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1170. test_assert(dsig_signatures);
  1171. test_eq(1, smartlist_len(dsig_signatures));
  1172. sig = smartlist_get(dsig_signatures, 0);
  1173. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1174. DIGEST_LEN);
  1175. test_eq(sig->alg, DIGEST_SHA256);
  1176. }
  1177. /* Try adding it to con2. */
  1178. detached_text2 = get_detached_sigs(con2,con_md2);
  1179. test_eq(1, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1180. LOG_INFO, &msg));
  1181. tor_free(detached_text2);
  1182. test_eq(1, networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1183. LOG_INFO, &msg));
  1184. tor_free(detached_text2);
  1185. detached_text2 = get_detached_sigs(con2,con_md2);
  1186. //printf("\n<%s>\n", detached_text2);
  1187. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1188. test_assert(dsig2);
  1189. /*
  1190. printf("\n");
  1191. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1192. char hd[64];
  1193. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1194. printf("%s\n", hd);
  1195. });
  1196. */
  1197. test_eq(2,
  1198. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1199. test_eq(2,
  1200. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  1201. "microdesc")));
  1202. /* Try adding to con2 twice; verify that nothing changes. */
  1203. test_eq(0, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1204. LOG_INFO, &msg));
  1205. /* Add to con. */
  1206. test_eq(2, networkstatus_add_detached_signatures(con, dsig2, "test",
  1207. LOG_INFO, &msg));
  1208. /* Check signatures */
  1209. voter = smartlist_get(con->voters, 1);
  1210. sig = smartlist_get(voter->sigs, 0);
  1211. test_assert(sig);
  1212. test_assert(!networkstatus_check_document_signature(con, sig, cert2));
  1213. voter = smartlist_get(con->voters, 2);
  1214. sig = smartlist_get(voter->sigs, 0);
  1215. test_assert(sig);
  1216. test_assert(!networkstatus_check_document_signature(con, sig, cert1));
  1217. }
  1218. done:
  1219. smartlist_free(votes);
  1220. tor_free(v1_text);
  1221. tor_free(v2_text);
  1222. tor_free(v3_text);
  1223. tor_free(consensus_text);
  1224. tor_free(consensus_text_md);
  1225. if (vote)
  1226. networkstatus_vote_free(vote);
  1227. if (v1)
  1228. networkstatus_vote_free(v1);
  1229. if (v2)
  1230. networkstatus_vote_free(v2);
  1231. if (v3)
  1232. networkstatus_vote_free(v3);
  1233. if (con)
  1234. networkstatus_vote_free(con);
  1235. if (con_md)
  1236. networkstatus_vote_free(con_md);
  1237. if (sign_skey_1)
  1238. crypto_pk_free(sign_skey_1);
  1239. if (sign_skey_2)
  1240. crypto_pk_free(sign_skey_2);
  1241. if (sign_skey_3)
  1242. crypto_pk_free(sign_skey_3);
  1243. if (sign_skey_leg1)
  1244. crypto_pk_free(sign_skey_leg1);
  1245. if (cert1)
  1246. authority_cert_free(cert1);
  1247. if (cert2)
  1248. authority_cert_free(cert2);
  1249. if (cert3)
  1250. authority_cert_free(cert3);
  1251. tor_free(consensus_text2);
  1252. tor_free(consensus_text3);
  1253. tor_free(consensus_text_md2);
  1254. tor_free(consensus_text_md3);
  1255. tor_free(detached_text1);
  1256. tor_free(detached_text2);
  1257. if (con2)
  1258. networkstatus_vote_free(con2);
  1259. if (con3)
  1260. networkstatus_vote_free(con3);
  1261. if (con_md2)
  1262. networkstatus_vote_free(con_md2);
  1263. if (con_md3)
  1264. networkstatus_vote_free(con_md3);
  1265. if (dsig1)
  1266. ns_detached_signatures_free(dsig1);
  1267. if (dsig2)
  1268. ns_detached_signatures_free(dsig2);
  1269. }
  1270. #define DIR_LEGACY(name) \
  1271. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
  1272. #define DIR(name) \
  1273. { #name, test_dir_##name, 0, NULL, NULL }
  1274. struct testcase_t dir_tests[] = {
  1275. DIR_LEGACY(nicknames),
  1276. DIR_LEGACY(formats),
  1277. DIR_LEGACY(versions),
  1278. DIR_LEGACY(fp_pairs),
  1279. DIR(split_fps),
  1280. DIR_LEGACY(measured_bw),
  1281. DIR_LEGACY(param_voting),
  1282. DIR_LEGACY(v3_networkstatus),
  1283. END_OF_TESTCASES
  1284. };