test_dir.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include <math.h>
  7. #define DIRSERV_PRIVATE
  8. #define DIRVOTE_PRIVATE
  9. #define ROUTER_PRIVATE
  10. #define ROUTERLIST_PRIVATE
  11. #define HIBERNATE_PRIVATE
  12. #include "or.h"
  13. #include "config.h"
  14. #include "directory.h"
  15. #include "dirserv.h"
  16. #include "dirvote.h"
  17. #include "hibernate.h"
  18. #include "networkstatus.h"
  19. #include "router.h"
  20. #include "routerlist.h"
  21. #include "routerparse.h"
  22. #include "test.h"
  23. static void
  24. test_dir_nicknames(void)
  25. {
  26. test_assert( is_legal_nickname("a"));
  27. test_assert(!is_legal_nickname(""));
  28. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  29. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  30. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  31. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  32. /* valid */
  33. test_assert( is_legal_nickname_or_hexdigest(
  34. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  35. test_assert( is_legal_nickname_or_hexdigest(
  36. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  37. test_assert( is_legal_nickname_or_hexdigest(
  38. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  39. /* too short */
  40. test_assert(!is_legal_nickname_or_hexdigest(
  41. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  42. /* illegal char */
  43. test_assert(!is_legal_nickname_or_hexdigest(
  44. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  45. /* hex part too long */
  46. test_assert(!is_legal_nickname_or_hexdigest(
  47. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  48. test_assert(!is_legal_nickname_or_hexdigest(
  49. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  50. /* Bad nickname */
  51. test_assert(!is_legal_nickname_or_hexdigest(
  52. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  53. test_assert(!is_legal_nickname_or_hexdigest(
  54. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  55. test_assert(!is_legal_nickname_or_hexdigest(
  56. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  57. test_assert(!is_legal_nickname_or_hexdigest(
  58. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  59. "abcdefghijklmnoppqrst"));
  60. /* Bad extra char. */
  61. test_assert(!is_legal_nickname_or_hexdigest(
  62. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  63. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  64. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  65. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  66. done:
  67. ;
  68. }
  69. /** Run unit tests for router descriptor generation logic. */
  70. static void
  71. test_dir_formats(void)
  72. {
  73. char *buf = NULL;
  74. char buf2[8192];
  75. char platform[256];
  76. char fingerprint[FINGERPRINT_LEN+1];
  77. char *pk1_str = NULL, *pk2_str = NULL, *cp;
  78. size_t pk1_str_len, pk2_str_len;
  79. routerinfo_t *r1=NULL, *r2=NULL;
  80. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  81. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  82. addr_policy_t *ex1, *ex2;
  83. routerlist_t *dir1 = NULL, *dir2 = NULL;
  84. or_options_t *options = get_options_mutable();
  85. const addr_policy_t *p;
  86. pk1 = pk_generate(0);
  87. pk2 = pk_generate(1);
  88. test_assert(pk1 && pk2);
  89. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  90. get_platform_str(platform, sizeof(platform));
  91. r1 = tor_malloc_zero(sizeof(routerinfo_t));
  92. r1->address = tor_strdup("18.244.0.1");
  93. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  94. r1->cache_info.published_on = 0;
  95. r1->or_port = 9000;
  96. r1->dir_port = 9003;
  97. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  98. r1->ipv6_orport = 9999;
  99. r1->onion_pkey = crypto_pk_dup_key(pk1);
  100. r1->identity_pkey = crypto_pk_dup_key(pk2);
  101. r1->bandwidthrate = 1000;
  102. r1->bandwidthburst = 5000;
  103. r1->bandwidthcapacity = 10000;
  104. r1->exit_policy = NULL;
  105. r1->nickname = tor_strdup("Magri");
  106. r1->platform = tor_strdup(platform);
  107. ex1 = tor_malloc_zero(sizeof(addr_policy_t));
  108. ex2 = tor_malloc_zero(sizeof(addr_policy_t));
  109. ex1->policy_type = ADDR_POLICY_ACCEPT;
  110. tor_addr_from_ipv4h(&ex1->addr, 0);
  111. ex1->maskbits = 0;
  112. ex1->prt_min = ex1->prt_max = 80;
  113. ex2->policy_type = ADDR_POLICY_REJECT;
  114. tor_addr_from_ipv4h(&ex2->addr, 18<<24);
  115. ex2->maskbits = 8;
  116. ex2->prt_min = ex2->prt_max = 24;
  117. r2 = tor_malloc_zero(sizeof(routerinfo_t));
  118. r2->address = tor_strdup("1.1.1.1");
  119. r2->addr = 0x0a030201u; /* 10.3.2.1 */
  120. r2->platform = tor_strdup(platform);
  121. r2->cache_info.published_on = 5;
  122. r2->or_port = 9005;
  123. r2->dir_port = 0;
  124. r2->onion_pkey = crypto_pk_dup_key(pk2);
  125. r2->identity_pkey = crypto_pk_dup_key(pk1);
  126. r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
  127. r2->exit_policy = smartlist_new();
  128. smartlist_add(r2->exit_policy, ex1);
  129. smartlist_add(r2->exit_policy, ex2);
  130. r2->nickname = tor_strdup("Fred");
  131. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  132. &pk1_str_len));
  133. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  134. &pk2_str_len));
  135. /* XXXX025 router_dump_to_string should really take this from ri.*/
  136. options->ContactInfo = tor_strdup("Magri White "
  137. "<magri@elsewhere.example.com>");
  138. buf = router_dump_router_to_string(r1, pk2);
  139. tor_free(options->ContactInfo);
  140. test_assert(buf);
  141. strlcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
  142. "or-address [1:2:3:4::]:9999\n"
  143. "platform Tor "VERSION" on ", sizeof(buf2));
  144. strlcat(buf2, get_uname(), sizeof(buf2));
  145. strlcat(buf2, "\n"
  146. "protocols Link 1 2 Circuit 1\n"
  147. "published 1970-01-01 00:00:00\n"
  148. "fingerprint ", sizeof(buf2));
  149. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  150. strlcat(buf2, fingerprint, sizeof(buf2));
  151. strlcat(buf2, "\nuptime 0\n"
  152. /* XXX the "0" above is hard-coded, but even if we made it reflect
  153. * uptime, that still wouldn't make it right, because the two
  154. * descriptors might be made on different seconds... hm. */
  155. "bandwidth 1000 5000 10000\n"
  156. "onion-key\n", sizeof(buf2));
  157. strlcat(buf2, pk1_str, sizeof(buf2));
  158. strlcat(buf2, "signing-key\n", sizeof(buf2));
  159. strlcat(buf2, pk2_str, sizeof(buf2));
  160. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  161. strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
  162. sizeof(buf2));
  163. strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
  164. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  165. * twice */
  166. test_streq(buf, buf2);
  167. tor_free(buf);
  168. buf = router_dump_router_to_string(r1, pk2);
  169. test_assert(buf);
  170. cp = buf;
  171. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
  172. test_assert(rp1);
  173. test_streq(rp1->address, r1->address);
  174. test_eq(rp1->or_port, r1->or_port);
  175. //test_eq(rp1->dir_port, r1->dir_port);
  176. test_eq(rp1->bandwidthrate, r1->bandwidthrate);
  177. test_eq(rp1->bandwidthburst, r1->bandwidthburst);
  178. test_eq(rp1->bandwidthcapacity, r1->bandwidthcapacity);
  179. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  180. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  181. //test_assert(rp1->exit_policy == NULL);
  182. strlcpy(buf2,
  183. "router Fred 1.1.1.1 9005 0 0\n"
  184. "platform Tor "VERSION" on ", sizeof(buf2));
  185. strlcat(buf2, get_uname(), sizeof(buf2));
  186. strlcat(buf2, "\n"
  187. "protocols Link 1 2 Circuit 1\n"
  188. "published 1970-01-01 00:00:05\n"
  189. "fingerprint ", sizeof(buf2));
  190. test_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
  191. strlcat(buf2, fingerprint, sizeof(buf2));
  192. strlcat(buf2, "\nuptime 0\n"
  193. "bandwidth 3000 3000 3000\n", sizeof(buf2));
  194. strlcat(buf2, "onion-key\n", sizeof(buf2));
  195. strlcat(buf2, pk2_str, sizeof(buf2));
  196. strlcat(buf2, "signing-key\n", sizeof(buf2));
  197. strlcat(buf2, pk1_str, sizeof(buf2));
  198. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  199. strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
  200. strlcat(buf2, "router-signature\n", sizeof(buf2));
  201. buf = router_dump_router_to_string(r2, pk1);
  202. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  203. * twice */
  204. test_streq(buf, buf2);
  205. tor_free(buf);
  206. buf = router_dump_router_to_string(r2, pk1);
  207. cp = buf;
  208. rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
  209. test_assert(rp2);
  210. test_streq(rp2->address, r2->address);
  211. test_eq(rp2->or_port, r2->or_port);
  212. test_eq(rp2->dir_port, r2->dir_port);
  213. test_eq(rp2->bandwidthrate, r2->bandwidthrate);
  214. test_eq(rp2->bandwidthburst, r2->bandwidthburst);
  215. test_eq(rp2->bandwidthcapacity, r2->bandwidthcapacity);
  216. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  217. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  218. test_eq(smartlist_len(rp2->exit_policy), 2);
  219. p = smartlist_get(rp2->exit_policy, 0);
  220. test_eq(p->policy_type, ADDR_POLICY_ACCEPT);
  221. test_assert(tor_addr_is_null(&p->addr));
  222. test_eq(p->maskbits, 0);
  223. test_eq(p->prt_min, 80);
  224. test_eq(p->prt_max, 80);
  225. p = smartlist_get(rp2->exit_policy, 1);
  226. test_eq(p->policy_type, ADDR_POLICY_REJECT);
  227. test_assert(tor_addr_eq(&p->addr, &ex2->addr));
  228. test_eq(p->maskbits, 8);
  229. test_eq(p->prt_min, 24);
  230. test_eq(p->prt_max, 24);
  231. #if 0
  232. /* Okay, now for the directories. */
  233. {
  234. fingerprint_list = smartlist_new();
  235. crypto_pk_get_fingerprint(pk2, buf, 1);
  236. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  237. crypto_pk_get_fingerprint(pk1, buf, 1);
  238. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  239. }
  240. #endif
  241. dirserv_free_fingerprint_list();
  242. done:
  243. if (r1)
  244. routerinfo_free(r1);
  245. if (r2)
  246. routerinfo_free(r2);
  247. tor_free(buf);
  248. tor_free(pk1_str);
  249. tor_free(pk2_str);
  250. if (pk1) crypto_pk_free(pk1);
  251. if (pk2) crypto_pk_free(pk2);
  252. if (rp1) routerinfo_free(rp1);
  253. tor_free(dir1); /* XXXX And more !*/
  254. tor_free(dir2); /* And more !*/
  255. }
  256. static void
  257. test_dir_versions(void)
  258. {
  259. tor_version_t ver1;
  260. /* Try out version parsing functionality */
  261. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  262. test_eq(0, ver1.major);
  263. test_eq(3, ver1.minor);
  264. test_eq(4, ver1.micro);
  265. test_eq(VER_PRE, ver1.status);
  266. test_eq(2, ver1.patchlevel);
  267. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  268. test_eq(0, ver1.major);
  269. test_eq(3, ver1.minor);
  270. test_eq(4, ver1.micro);
  271. test_eq(VER_RC, ver1.status);
  272. test_eq(1, ver1.patchlevel);
  273. test_eq(0, tor_version_parse("1.3.4", &ver1));
  274. test_eq(1, ver1.major);
  275. test_eq(3, ver1.minor);
  276. test_eq(4, ver1.micro);
  277. test_eq(VER_RELEASE, ver1.status);
  278. test_eq(0, ver1.patchlevel);
  279. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  280. test_eq(1, ver1.major);
  281. test_eq(3, ver1.minor);
  282. test_eq(4, ver1.micro);
  283. test_eq(VER_RELEASE, ver1.status);
  284. test_eq(999, ver1.patchlevel);
  285. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  286. test_eq(0, ver1.major);
  287. test_eq(1, ver1.minor);
  288. test_eq(2, ver1.micro);
  289. test_eq(4, ver1.patchlevel);
  290. test_eq(VER_RELEASE, ver1.status);
  291. test_streq("alpha", ver1.status_tag);
  292. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  293. test_eq(0, ver1.major);
  294. test_eq(1, ver1.minor);
  295. test_eq(2, ver1.micro);
  296. test_eq(4, ver1.patchlevel);
  297. test_eq(VER_RELEASE, ver1.status);
  298. test_streq("", ver1.status_tag);
  299. #define tt_versionstatus_op(vs1, op, vs2) \
  300. tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
  301. (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
  302. #define test_v_i_o(val, ver, lst) \
  303. tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
  304. /* make sure tor_version_is_obsolete() works */
  305. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  306. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  307. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  308. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  309. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  310. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  311. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  312. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  313. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  314. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  315. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  316. /* Not on list, but newer than any in same series. */
  317. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  318. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  319. /* Series newer than any on list. */
  320. 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");
  321. /* Series older than any on list. */
  322. 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");
  323. /* Not on list, not newer than any on same series. */
  324. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  325. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  326. /* On list, not newer than any on same series. */
  327. test_v_i_o(VS_UNRECOMMENDED,
  328. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  329. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  330. test_eq(1, tor_version_as_new_as(
  331. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  332. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  333. "0.0.8rc2"));
  334. test_eq(0, tor_version_as_new_as(
  335. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  336. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  337. /* Now try svn revisions. */
  338. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  339. "Tor 0.2.1.0-dev (r99)"));
  340. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
  341. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  342. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  343. "Tor 0.2.1.0-dev on Colossus"));
  344. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  345. "Tor 0.2.1.0-dev (r100)"));
  346. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  347. "Tor 0.2.1.0-dev (r100) on AM"));
  348. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
  349. "Tor 0.2.1.0-dev (r99)"));
  350. test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
  351. "Tor 0.2.1.0-dev (r99)"));
  352. /* Now try git revisions */
  353. test_eq(0, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
  354. test_eq(0, ver1.major);
  355. test_eq(5, ver1.minor);
  356. test_eq(6, ver1.micro);
  357. test_eq(7, ver1.patchlevel);
  358. test_eq(3, ver1.git_tag_len);
  359. test_memeq(ver1.git_tag, "\xff\x00\xff", 3);
  360. test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
  361. test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
  362. test_eq(0, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
  363. done:
  364. ;
  365. }
  366. /** Run unit tests for directory fp_pair functions. */
  367. static void
  368. test_dir_fp_pairs(void)
  369. {
  370. smartlist_t *sl = smartlist_new();
  371. fp_pair_t *pair;
  372. dir_split_resource_into_fingerprint_pairs(
  373. /* Two pairs, out of order, with one duplicate. */
  374. "73656372657420646174612E0000000000FFFFFF-"
  375. "557365204145532d32353620696e73746561642e+"
  376. "73656372657420646174612E0000000000FFFFFF-"
  377. "557365204145532d32353620696e73746561642e+"
  378. "48657861646563696d616c2069736e277420736f-"
  379. "676f6f6420666f7220686964696e6720796f7572.z", sl);
  380. test_eq(smartlist_len(sl), 2);
  381. pair = smartlist_get(sl, 0);
  382. test_memeq(pair->first, "Hexadecimal isn't so", DIGEST_LEN);
  383. test_memeq(pair->second, "good for hiding your", DIGEST_LEN);
  384. pair = smartlist_get(sl, 1);
  385. test_memeq(pair->first, "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN);
  386. test_memeq(pair->second, "Use AES-256 instead.", DIGEST_LEN);
  387. done:
  388. SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
  389. smartlist_free(sl);
  390. }
  391. static void
  392. test_dir_split_fps(void *testdata)
  393. {
  394. smartlist_t *sl = smartlist_new();
  395. char *mem_op_hex_tmp = NULL;
  396. (void)testdata;
  397. /* Some example hex fingerprints and their base64 equivalents */
  398. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  399. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  400. #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
  401. #define HEX256_1 \
  402. "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
  403. #define HEX256_2 \
  404. "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
  405. #define HEX256_3 \
  406. "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
  407. #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
  408. #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
  409. #define B64_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
  410. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  411. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  412. #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
  413. /* no flags set */
  414. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  415. tt_int_op(smartlist_len(sl), ==, 3);
  416. tt_str_op(smartlist_get(sl, 0), ==, "A");
  417. tt_str_op(smartlist_get(sl, 1), ==, "C");
  418. tt_str_op(smartlist_get(sl, 2), ==, "B");
  419. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  420. smartlist_clear(sl);
  421. /* uniq strings. */
  422. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  423. tt_int_op(smartlist_len(sl), ==, 3);
  424. tt_str_op(smartlist_get(sl, 0), ==, "A");
  425. tt_str_op(smartlist_get(sl, 1), ==, "B");
  426. tt_str_op(smartlist_get(sl, 2), ==, "C");
  427. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  428. smartlist_clear(sl);
  429. /* Decode hex. */
  430. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  431. tt_int_op(smartlist_len(sl), ==, 2);
  432. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  433. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  434. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  435. smartlist_clear(sl);
  436. /* decode hex and drop weirdness. */
  437. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  438. sl, NULL, DSR_HEX);
  439. tt_int_op(smartlist_len(sl), ==, 2);
  440. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  441. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  442. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  443. smartlist_clear(sl);
  444. /* Decode long hex */
  445. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  446. sl, NULL, DSR_HEX|DSR_DIGEST256);
  447. tt_int_op(smartlist_len(sl), ==, 3);
  448. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  449. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  450. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
  451. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  452. smartlist_clear(sl);
  453. /* Decode hex and sort. */
  454. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  455. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  456. tt_int_op(smartlist_len(sl), ==, 3);
  457. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
  458. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  459. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
  460. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  461. smartlist_clear(sl);
  462. /* Decode long hex and sort */
  463. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  464. "+"HEX256_1,
  465. sl, NULL,
  466. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  467. tt_int_op(smartlist_len(sl), ==, 3);
  468. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
  469. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  470. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
  471. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  472. smartlist_clear(sl);
  473. /* Decode base64 */
  474. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  475. tt_int_op(smartlist_len(sl), ==, 2);
  476. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  477. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  478. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  479. smartlist_clear(sl);
  480. /* Decode long base64 */
  481. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  482. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  483. tt_int_op(smartlist_len(sl), ==, 2);
  484. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  485. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  486. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  487. smartlist_clear(sl);
  488. dir_split_resource_into_fingerprints(B64_256_1,
  489. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  490. tt_int_op(smartlist_len(sl), ==, 1);
  491. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  492. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  493. smartlist_clear(sl);
  494. done:
  495. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  496. smartlist_free(sl);
  497. tor_free(mem_op_hex_tmp);
  498. }
  499. static void
  500. test_dir_measured_bw(void)
  501. {
  502. measured_bw_line_t mbwl;
  503. int i;
  504. const char *lines_pass[] = {
  505. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  506. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  507. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  508. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  509. "bw=1024 junk=007\n",
  510. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  511. "bw=1024 junk=007\n",
  512. "end"
  513. };
  514. const char *lines_fail[] = {
  515. /* Test possible python stupidity on input */
  516. "node_id=None bw=1024\n",
  517. "node_id=$None bw=1024\n",
  518. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  519. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  520. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  521. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  522. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  523. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  524. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  525. /* Test incomplete writes due to race conditions, partial copies, etc */
  526. "node_i",
  527. "node_i\n",
  528. "node_id=",
  529. "node_id=\n",
  530. "node_id=$557365204145532d32353620696e73746561642e bw=",
  531. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  532. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  533. "node_id=$557365204145532d32353620696e7374",
  534. "node_id=$557365204145532d32353620696e7374\n",
  535. "",
  536. "\n",
  537. " \n ",
  538. " \n\n",
  539. /* Test assorted noise */
  540. " node_id= ",
  541. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  542. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  543. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  544. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  545. "end"
  546. };
  547. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  548. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  549. test_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
  550. }
  551. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  552. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  553. test_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
  554. test_assert(mbwl.bw == 1024);
  555. test_assert(strcmp(mbwl.node_hex,
  556. "557365204145532d32353620696e73746561642e") == 0);
  557. }
  558. done:
  559. return;
  560. }
  561. static void
  562. test_dir_param_voting(void)
  563. {
  564. networkstatus_t vote1, vote2, vote3, vote4;
  565. smartlist_t *votes = smartlist_new();
  566. char *res = NULL;
  567. /* dirvote_compute_params only looks at the net_params field of the votes,
  568. so that's all we need to set.
  569. */
  570. memset(&vote1, 0, sizeof(vote1));
  571. memset(&vote2, 0, sizeof(vote2));
  572. memset(&vote3, 0, sizeof(vote3));
  573. memset(&vote4, 0, sizeof(vote4));
  574. vote1.net_params = smartlist_new();
  575. vote2.net_params = smartlist_new();
  576. vote3.net_params = smartlist_new();
  577. vote4.net_params = smartlist_new();
  578. smartlist_split_string(vote1.net_params,
  579. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  580. smartlist_split_string(vote2.net_params,
  581. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  582. smartlist_split_string(vote3.net_params,
  583. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  584. smartlist_split_string(vote4.net_params,
  585. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  586. test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  587. test_eq(222, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  588. test_eq(80, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  589. test_eq(-8, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  590. test_eq(0, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  591. smartlist_add(votes, &vote1);
  592. /* Do the first tests without adding all the other votes, for
  593. * networks without many dirauths. */
  594. res = dirvote_compute_params(votes, 11, 6);
  595. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  596. tor_free(res);
  597. res = dirvote_compute_params(votes, 12, 2);
  598. test_streq(res, "");
  599. tor_free(res);
  600. res = dirvote_compute_params(votes, 12, 1);
  601. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  602. tor_free(res);
  603. smartlist_add(votes, &vote2);
  604. res = dirvote_compute_params(votes, 11, 2);
  605. test_streq(res, "ab=27 abcd=20 cw=5 x-yz=-99");
  606. tor_free(res);
  607. res = dirvote_compute_params(votes, 12, 2);
  608. test_streq(res, "ab=27 cw=5 x-yz=-99");
  609. tor_free(res);
  610. res = dirvote_compute_params(votes, 12, 3);
  611. test_streq(res, "ab=27 cw=5 x-yz=-99");
  612. tor_free(res);
  613. res = dirvote_compute_params(votes, 12, 6);
  614. test_streq(res, "");
  615. tor_free(res);
  616. smartlist_add(votes, &vote3);
  617. res = dirvote_compute_params(votes, 11, 3);
  618. test_streq(res, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
  619. tor_free(res);
  620. res = dirvote_compute_params(votes, 12, 3);
  621. test_streq(res, "ab=27 abcd=20 cw=50 x-yz=-9");
  622. tor_free(res);
  623. res = dirvote_compute_params(votes, 12, 5);
  624. test_streq(res, "cw=50 x-yz=-9");
  625. tor_free(res);
  626. res = dirvote_compute_params(votes, 12, 9);
  627. test_streq(res, "cw=50 x-yz=-9");
  628. tor_free(res);
  629. smartlist_add(votes, &vote4);
  630. res = dirvote_compute_params(votes, 11, 4);
  631. test_streq(res, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
  632. tor_free(res);
  633. res = dirvote_compute_params(votes, 12, 4);
  634. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  635. tor_free(res);
  636. res = dirvote_compute_params(votes, 12, 5);
  637. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  638. tor_free(res);
  639. /* Test that the special-cased "at least three dirauths voted for
  640. * this param" logic works as expected. */
  641. res = dirvote_compute_params(votes, 12, 6);
  642. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  643. tor_free(res);
  644. res = dirvote_compute_params(votes, 12, 10);
  645. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  646. tor_free(res);
  647. done:
  648. tor_free(res);
  649. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  650. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  651. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  652. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  653. smartlist_free(vote1.net_params);
  654. smartlist_free(vote2.net_params);
  655. smartlist_free(vote3.net_params);
  656. smartlist_free(vote4.net_params);
  657. smartlist_free(votes);
  658. return;
  659. }
  660. extern const char AUTHORITY_CERT_1[];
  661. extern const char AUTHORITY_SIGNKEY_1[];
  662. extern const char AUTHORITY_CERT_2[];
  663. extern const char AUTHORITY_SIGNKEY_2[];
  664. extern const char AUTHORITY_CERT_3[];
  665. extern const char AUTHORITY_SIGNKEY_3[];
  666. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  667. * same voting authority, and that they do in fact have all the same
  668. * information. */
  669. static void
  670. test_same_voter(networkstatus_voter_info_t *v1,
  671. networkstatus_voter_info_t *v2)
  672. {
  673. test_streq(v1->nickname, v2->nickname);
  674. test_memeq(v1->identity_digest, v2->identity_digest, DIGEST_LEN);
  675. test_streq(v1->address, v2->address);
  676. test_eq(v1->addr, v2->addr);
  677. test_eq(v1->dir_port, v2->dir_port);
  678. test_eq(v1->or_port, v2->or_port);
  679. test_streq(v1->contact, v2->contact);
  680. test_memeq(v1->vote_digest, v2->vote_digest, DIGEST_LEN);
  681. done:
  682. ;
  683. }
  684. /** Helper: Make a new routerinfo containing the right information for a
  685. * given vote_routerstatus_t. */
  686. static routerinfo_t *
  687. generate_ri_from_rs(const vote_routerstatus_t *vrs)
  688. {
  689. routerinfo_t *r;
  690. const routerstatus_t *rs = &vrs->status;
  691. static time_t published = 0;
  692. r = tor_malloc_zero(sizeof(routerinfo_t));
  693. memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
  694. memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
  695. DIGEST_LEN);
  696. r->cache_info.do_not_cache = 1;
  697. r->cache_info.routerlist_index = -1;
  698. r->cache_info.signed_descriptor_body =
  699. tor_strdup("123456789012345678901234567890123");
  700. r->cache_info.signed_descriptor_len =
  701. strlen(r->cache_info.signed_descriptor_body);
  702. r->exit_policy = smartlist_new();
  703. r->cache_info.published_on = ++published + time(NULL);
  704. if (rs->has_bandwidth) {
  705. /*
  706. * Multiply by 1000 because the routerinfo_t and the routerstatus_t
  707. * seem to use different units (*sigh*) and because we seem stuck on
  708. * icky and perverse decimal kilobytes (*double sigh*) - see
  709. * router_get_advertised_bandwidth_capped() of routerlist.c and
  710. * routerstatus_format_entry() of dirserv.c.
  711. */
  712. r->bandwidthrate = rs->bandwidth * 1000;
  713. r->bandwidthcapacity = rs->bandwidth * 1000;
  714. }
  715. return r;
  716. }
  717. /** Helper: get a detached signatures document for one or two
  718. * consensuses. */
  719. static char *
  720. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  721. {
  722. char *r;
  723. smartlist_t *sl;
  724. tor_assert(ns && ns->flavor == FLAV_NS);
  725. sl = smartlist_new();
  726. smartlist_add(sl,ns);
  727. if (ns2)
  728. smartlist_add(sl,ns2);
  729. r = networkstatus_get_detached_signatures(sl);
  730. smartlist_free(sl);
  731. return r;
  732. }
  733. /**
  734. * Generate a routerstatus for v3_networkstatus test
  735. */
  736. static vote_routerstatus_t *
  737. gen_routerstatus_for_v3ns(int idx, time_t now)
  738. {
  739. vote_routerstatus_t *vrs=NULL;
  740. routerstatus_t *rs;
  741. tor_addr_t addr_ipv6;
  742. switch (idx) {
  743. case 0:
  744. /* Generate the first routerstatus. */
  745. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  746. rs = &vrs->status;
  747. vrs->version = tor_strdup("0.1.2.14");
  748. rs->published_on = now-1500;
  749. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  750. memset(rs->identity_digest, 3, DIGEST_LEN);
  751. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  752. rs->addr = 0x99008801;
  753. rs->or_port = 443;
  754. rs->dir_port = 8000;
  755. /* all flags but running cleared */
  756. rs->is_flagged_running = 1;
  757. break;
  758. case 1:
  759. /* Generate the second routerstatus. */
  760. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  761. rs = &vrs->status;
  762. vrs->version = tor_strdup("0.2.0.5");
  763. rs->published_on = now-1000;
  764. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  765. memset(rs->identity_digest, 5, DIGEST_LEN);
  766. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  767. rs->addr = 0x99009901;
  768. rs->or_port = 443;
  769. rs->dir_port = 0;
  770. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  771. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  772. rs->ipv6_orport = 4711;
  773. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  774. rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
  775. break;
  776. case 2:
  777. /* Generate the third routerstatus. */
  778. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  779. rs = &vrs->status;
  780. vrs->version = tor_strdup("0.1.0.3");
  781. rs->published_on = now-1000;
  782. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  783. memset(rs->identity_digest, 33, DIGEST_LEN);
  784. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  785. rs->addr = 0xAA009901;
  786. rs->or_port = 400;
  787. rs->dir_port = 9999;
  788. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  789. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  790. rs->is_possible_guard = 1;
  791. break;
  792. case 3:
  793. /* Generate a fourth routerstatus that is not running. */
  794. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  795. rs = &vrs->status;
  796. vrs->version = tor_strdup("0.1.6.3");
  797. rs->published_on = now-1000;
  798. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  799. memset(rs->identity_digest, 34, DIGEST_LEN);
  800. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  801. rs->addr = 0xC0000203;
  802. rs->or_port = 500;
  803. rs->dir_port = 1999;
  804. /* Running flag (and others) cleared */
  805. break;
  806. case 4:
  807. /* No more for this test; return NULL */
  808. vrs = NULL;
  809. break;
  810. default:
  811. /* Shouldn't happen */
  812. test_assert(0);
  813. }
  814. if (vrs) {
  815. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  816. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  817. "m 9,10,11,12,13,14,15,16,17 "
  818. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  819. idx);
  820. }
  821. done:
  822. return vrs;
  823. }
  824. /** Apply tweaks to the vote list for each voter */
  825. static int
  826. vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
  827. {
  828. vote_routerstatus_t *vrs;
  829. const char *msg = NULL;
  830. test_assert(v);
  831. (void)now;
  832. if (voter == 1) {
  833. measured_bw_line_t mbw;
  834. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  835. mbw.bw = 1024;
  836. test_assert(measured_bw_line_apply(&mbw,
  837. v->routerstatus_list) == 1);
  838. } else if (voter == 2 || voter == 3) {
  839. /* Monkey around with the list a bit */
  840. vrs = smartlist_get(v->routerstatus_list, 2);
  841. smartlist_del_keeporder(v->routerstatus_list, 2);
  842. tor_free(vrs->version);
  843. tor_free(vrs);
  844. vrs = smartlist_get(v->routerstatus_list, 0);
  845. vrs->status.is_fast = 1;
  846. if (voter == 3) {
  847. vrs = smartlist_get(v->routerstatus_list, 0);
  848. smartlist_del_keeporder(v->routerstatus_list, 0);
  849. tor_free(vrs->version);
  850. tor_free(vrs);
  851. vrs = smartlist_get(v->routerstatus_list, 0);
  852. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  853. test_assert(router_add_to_routerlist(
  854. generate_ri_from_rs(vrs), &msg,0,0) >= 0);
  855. }
  856. }
  857. done:
  858. return 0;
  859. }
  860. /**
  861. * Test a parsed vote_routerstatus_t for v3_networkstatus test
  862. */
  863. static void
  864. test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
  865. {
  866. routerstatus_t *rs;
  867. tor_addr_t addr_ipv6;
  868. test_assert(vrs);
  869. rs = &(vrs->status);
  870. test_assert(rs);
  871. /* Split out by digests to test */
  872. if (tor_memeq(rs->identity_digest,
  873. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  874. "\x3\x3\x3\x3",
  875. DIGEST_LEN) &&
  876. (voter == 1)) {
  877. /* Check the first routerstatus. */
  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"
  883. "\x3\x3\x3\x3",
  884. DIGEST_LEN);
  885. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  886. test_eq(rs->addr, 0x99008801);
  887. test_eq(rs->or_port, 443);
  888. test_eq(rs->dir_port, 8000);
  889. test_eq(vrs->flags, U64_LITERAL(16)); // no flags except "running"
  890. } else if (tor_memeq(rs->identity_digest,
  891. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  892. "\x5\x5\x5\x5",
  893. DIGEST_LEN) &&
  894. (voter == 1 || voter == 2)) {
  895. test_memeq(rs->identity_digest,
  896. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  897. "\x5\x5\x5\x5",
  898. DIGEST_LEN);
  899. if (voter == 1) {
  900. /* Check the second routerstatus. */
  901. test_streq(vrs->version, "0.2.0.5");
  902. test_eq(rs->published_on, now-1000);
  903. test_streq(rs->nickname, "router1");
  904. }
  905. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  906. test_eq(rs->addr, 0x99009901);
  907. test_eq(rs->or_port, 443);
  908. test_eq(rs->dir_port, 0);
  909. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  910. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  911. test_eq(rs->ipv6_orport, 4711);
  912. if (voter == 1) {
  913. test_eq(vrs->flags, U64_LITERAL(254)); // all flags except "authority."
  914. } else {
  915. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
  916. test_eq(vrs->flags, U64_LITERAL(974));
  917. }
  918. } else if (tor_memeq(rs->identity_digest,
  919. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  920. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  921. DIGEST_LEN) &&
  922. (voter == 1 || voter == 2)) {
  923. /* Check the measured bandwidth bits */
  924. test_assert(vrs->has_measured_bw &&
  925. vrs->measured_bw == 1024);
  926. } else {
  927. /*
  928. * Didn't expect this, but the old unit test only checked some of them,
  929. * so don't assert.
  930. */
  931. /* test_assert(0); */
  932. }
  933. done:
  934. return;
  935. }
  936. /**
  937. * Test a consensus for v3_networkstatus_test
  938. */
  939. static void
  940. test_consensus_for_v3ns(networkstatus_t *con, time_t now)
  941. {
  942. (void)now;
  943. test_assert(con);
  944. test_assert(!con->cert);
  945. test_eq(2, smartlist_len(con->routerstatus_list));
  946. /* There should be two listed routers: one with identity 3, one with
  947. * identity 5. */
  948. done:
  949. return;
  950. }
  951. /**
  952. * Test a router list entry for v3_networkstatus test
  953. */
  954. static void
  955. test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
  956. {
  957. tor_addr_t addr_ipv6;
  958. test_assert(rs);
  959. /* There should be two listed routers: one with identity 3, one with
  960. * identity 5. */
  961. /* This one showed up in 2 digests. */
  962. if (tor_memeq(rs->identity_digest,
  963. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  964. "\x3\x3",
  965. DIGEST_LEN)) {
  966. test_memeq(rs->identity_digest,
  967. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  968. DIGEST_LEN);
  969. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  970. test_assert(!rs->is_authority);
  971. test_assert(!rs->is_exit);
  972. test_assert(!rs->is_fast);
  973. test_assert(!rs->is_possible_guard);
  974. test_assert(!rs->is_stable);
  975. /* (If it wasn't running it wouldn't be here) */
  976. test_assert(rs->is_flagged_running);
  977. test_assert(!rs->is_v2_dir);
  978. test_assert(!rs->is_valid);
  979. test_assert(!rs->is_named);
  980. /* XXXX check version */
  981. } else if (tor_memeq(rs->identity_digest,
  982. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  983. "\x5\x5\x5\x5",
  984. DIGEST_LEN)) {
  985. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  986. test_memeq(rs->identity_digest,
  987. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  988. DIGEST_LEN);
  989. test_streq(rs->nickname, "router1");
  990. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  991. test_eq(rs->published_on, now-1000);
  992. test_eq(rs->addr, 0x99009901);
  993. test_eq(rs->or_port, 443);
  994. test_eq(rs->dir_port, 0);
  995. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  996. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  997. test_eq(rs->ipv6_orport, 4711);
  998. test_assert(!rs->is_authority);
  999. test_assert(rs->is_exit);
  1000. test_assert(rs->is_fast);
  1001. test_assert(rs->is_possible_guard);
  1002. test_assert(rs->is_stable);
  1003. test_assert(rs->is_flagged_running);
  1004. test_assert(rs->is_v2_dir);
  1005. test_assert(rs->is_valid);
  1006. test_assert(!rs->is_named);
  1007. /* XXXX check version */
  1008. } else {
  1009. /* Weren't expecting this... */
  1010. test_assert(0);
  1011. }
  1012. done:
  1013. return;
  1014. }
  1015. /** Run a unit tests for generating and parsing networkstatuses, with
  1016. * the supply test fns. */
  1017. static void
  1018. test_a_networkstatus(
  1019. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  1020. int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  1021. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  1022. void (*consensus_test)(networkstatus_t *con, time_t now),
  1023. void (*rs_test)(routerstatus_t *rs, time_t now))
  1024. {
  1025. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  1026. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  1027. crypto_pk_t *sign_skey_leg1=NULL;
  1028. const char *msg=NULL;
  1029. /*
  1030. * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
  1031. * returns non-zero, it changed net_params and we should skip the tests for
  1032. * that later as they will fail.
  1033. */
  1034. int params_tweaked = 0;
  1035. time_t now = time(NULL);
  1036. networkstatus_voter_info_t *voter;
  1037. document_signature_t *sig;
  1038. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  1039. *con_md=NULL;
  1040. vote_routerstatus_t *vrs;
  1041. routerstatus_t *rs;
  1042. int idx, n_rs, n_vrs;
  1043. char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL, *cp;
  1044. smartlist_t *votes = smartlist_new();
  1045. /* For generating the two other consensuses. */
  1046. char *detached_text1=NULL, *detached_text2=NULL;
  1047. char *consensus_text2=NULL, *consensus_text3=NULL;
  1048. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  1049. char *consensus_text_md=NULL;
  1050. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  1051. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  1052. test_assert(vrs_gen);
  1053. test_assert(rs_test);
  1054. test_assert(vrs_test);
  1055. /* Parse certificates and keys. */
  1056. cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  1057. test_assert(cert1);
  1058. test_assert(cert1->is_cross_certified);
  1059. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  1060. test_assert(cert2);
  1061. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  1062. test_assert(cert3);
  1063. sign_skey_1 = crypto_pk_new();
  1064. sign_skey_2 = crypto_pk_new();
  1065. sign_skey_3 = crypto_pk_new();
  1066. sign_skey_leg1 = pk_generate(4);
  1067. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  1068. AUTHORITY_SIGNKEY_1, -1));
  1069. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  1070. AUTHORITY_SIGNKEY_2, -1));
  1071. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  1072. AUTHORITY_SIGNKEY_3, -1));
  1073. test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  1074. test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  1075. /*
  1076. * Set up a vote; generate it; try to parse it.
  1077. */
  1078. vote = tor_malloc_zero(sizeof(networkstatus_t));
  1079. vote->type = NS_TYPE_VOTE;
  1080. vote->published = now;
  1081. vote->valid_after = now+1000;
  1082. vote->fresh_until = now+2000;
  1083. vote->valid_until = now+3000;
  1084. vote->vote_seconds = 100;
  1085. vote->dist_seconds = 200;
  1086. vote->supported_methods = smartlist_new();
  1087. smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
  1088. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  1089. vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  1090. vote->known_flags = smartlist_new();
  1091. smartlist_split_string(vote->known_flags,
  1092. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  1093. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1094. vote->voters = smartlist_new();
  1095. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  1096. voter->nickname = tor_strdup("Voter1");
  1097. voter->address = tor_strdup("1.2.3.4");
  1098. voter->addr = 0x01020304;
  1099. voter->dir_port = 80;
  1100. voter->or_port = 9000;
  1101. voter->contact = tor_strdup("voter@example.com");
  1102. crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
  1103. smartlist_add(vote->voters, voter);
  1104. vote->cert = authority_cert_dup(cert1);
  1105. vote->net_params = smartlist_new();
  1106. smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
  1107. NULL, 0, 0);
  1108. vote->routerstatus_list = smartlist_new();
  1109. /* add routerstatuses */
  1110. idx = 0;
  1111. do {
  1112. vrs = vrs_gen(idx, now);
  1113. if (vrs) {
  1114. smartlist_add(vote->routerstatus_list, vrs);
  1115. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs),
  1116. &msg,0,0)>=0);
  1117. ++idx;
  1118. }
  1119. } while (vrs);
  1120. n_vrs = idx;
  1121. /* dump the vote and try to parse it. */
  1122. v1_text = format_networkstatus_vote(sign_skey_1, vote);
  1123. test_assert(v1_text);
  1124. v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
  1125. test_assert(v1);
  1126. /* Make sure the parsed thing was right. */
  1127. test_eq(v1->type, NS_TYPE_VOTE);
  1128. test_eq(v1->published, vote->published);
  1129. test_eq(v1->valid_after, vote->valid_after);
  1130. test_eq(v1->fresh_until, vote->fresh_until);
  1131. test_eq(v1->valid_until, vote->valid_until);
  1132. test_eq(v1->vote_seconds, vote->vote_seconds);
  1133. test_eq(v1->dist_seconds, vote->dist_seconds);
  1134. test_streq(v1->client_versions, vote->client_versions);
  1135. test_streq(v1->server_versions, vote->server_versions);
  1136. test_assert(v1->voters && smartlist_len(v1->voters));
  1137. voter = smartlist_get(v1->voters, 0);
  1138. test_streq(voter->nickname, "Voter1");
  1139. test_streq(voter->address, "1.2.3.4");
  1140. test_eq(voter->addr, 0x01020304);
  1141. test_eq(voter->dir_port, 80);
  1142. test_eq(voter->or_port, 9000);
  1143. test_streq(voter->contact, "voter@example.com");
  1144. test_assert(v1->cert);
  1145. test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  1146. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  1147. test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  1148. tor_free(cp);
  1149. test_eq(smartlist_len(v1->routerstatus_list), n_vrs);
  1150. if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
  1151. /* Check the routerstatuses. */
  1152. for (idx = 0; idx < n_vrs; ++idx) {
  1153. vrs = smartlist_get(v1->routerstatus_list, idx);
  1154. test_assert(vrs);
  1155. vrs_test(vrs, 1, now);
  1156. }
  1157. /* Generate second vote. It disagrees on some of the times,
  1158. * and doesn't list versions, and knows some crazy flags */
  1159. vote->published = now+1;
  1160. vote->fresh_until = now+3005;
  1161. vote->dist_seconds = 300;
  1162. authority_cert_free(vote->cert);
  1163. vote->cert = authority_cert_dup(cert2);
  1164. vote->net_params = smartlist_new();
  1165. smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
  1166. NULL, 0, 0);
  1167. tor_free(vote->client_versions);
  1168. tor_free(vote->server_versions);
  1169. voter = smartlist_get(vote->voters, 0);
  1170. tor_free(voter->nickname);
  1171. tor_free(voter->address);
  1172. voter->nickname = tor_strdup("Voter2");
  1173. voter->address = tor_strdup("2.3.4.5");
  1174. voter->addr = 0x02030405;
  1175. crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
  1176. smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
  1177. smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
  1178. smartlist_sort_strings(vote->known_flags);
  1179. /* generate and parse v2. */
  1180. v2_text = format_networkstatus_vote(sign_skey_2, vote);
  1181. test_assert(v2_text);
  1182. v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
  1183. test_assert(v2);
  1184. if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
  1185. /* Check that flags come out right.*/
  1186. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1187. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1188. "Running:Stable:V2Dir:Valid");
  1189. tor_free(cp);
  1190. /* Check the routerstatuses. */
  1191. n_vrs = smartlist_len(v2->routerstatus_list);
  1192. for (idx = 0; idx < n_vrs; ++idx) {
  1193. vrs = smartlist_get(v2->routerstatus_list, idx);
  1194. test_assert(vrs);
  1195. vrs_test(vrs, 2, now);
  1196. }
  1197. /* Generate the third vote. */
  1198. vote->published = now;
  1199. vote->fresh_until = now+2003;
  1200. vote->dist_seconds = 250;
  1201. authority_cert_free(vote->cert);
  1202. vote->cert = authority_cert_dup(cert3);
  1203. vote->net_params = smartlist_new();
  1204. smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
  1205. NULL, 0, 0);
  1206. smartlist_add(vote->supported_methods, tor_strdup("4"));
  1207. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
  1208. vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
  1209. voter = smartlist_get(vote->voters, 0);
  1210. tor_free(voter->nickname);
  1211. tor_free(voter->address);
  1212. voter->nickname = tor_strdup("Voter3");
  1213. voter->address = tor_strdup("3.4.5.6");
  1214. voter->addr = 0x03040506;
  1215. crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
  1216. /* This one has a legacy id. */
  1217. memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
  1218. v3_text = format_networkstatus_vote(sign_skey_3, vote);
  1219. test_assert(v3_text);
  1220. v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
  1221. test_assert(v3);
  1222. if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
  1223. /* Compute a consensus as voter 3. */
  1224. smartlist_add(votes, v3);
  1225. smartlist_add(votes, v1);
  1226. smartlist_add(votes, v2);
  1227. consensus_text = networkstatus_compute_consensus(votes, 3,
  1228. cert3->identity_key,
  1229. sign_skey_3,
  1230. "AAAAAAAAAAAAAAAAAAAA",
  1231. sign_skey_leg1,
  1232. FLAV_NS);
  1233. test_assert(consensus_text);
  1234. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  1235. NS_TYPE_CONSENSUS);
  1236. test_assert(con);
  1237. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1238. // v1_text, v2_text, v3_text);
  1239. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1240. cert3->identity_key,
  1241. sign_skey_3,
  1242. "AAAAAAAAAAAAAAAAAAAA",
  1243. sign_skey_leg1,
  1244. FLAV_MICRODESC);
  1245. test_assert(consensus_text_md);
  1246. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1247. NS_TYPE_CONSENSUS);
  1248. test_assert(con_md);
  1249. test_eq(con_md->flavor, FLAV_MICRODESC);
  1250. /* Check consensus contents. */
  1251. test_assert(con->type == NS_TYPE_CONSENSUS);
  1252. test_eq(con->published, 0); /* this field only appears in votes. */
  1253. test_eq(con->valid_after, now+1000);
  1254. test_eq(con->fresh_until, now+2003); /* median */
  1255. test_eq(con->valid_until, now+3000);
  1256. test_eq(con->vote_seconds, 100);
  1257. test_eq(con->dist_seconds, 250); /* median */
  1258. test_streq(con->client_versions, "0.1.2.14");
  1259. test_streq(con->server_versions, "0.1.2.15,0.1.2.16");
  1260. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1261. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1262. "Running:Stable:V2Dir:Valid");
  1263. tor_free(cp);
  1264. if (!params_tweaked) {
  1265. /* Skip this one if vote_tweaks() messed with the param lists */
  1266. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1267. test_streq(cp, "circuitwindow=80:foo=660");
  1268. tor_free(cp);
  1269. }
  1270. test_eq(4, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1271. /* The voter id digests should be in this order. */
  1272. test_assert(memcmp(cert2->cache_info.identity_digest,
  1273. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1274. test_assert(memcmp(cert1->cache_info.identity_digest,
  1275. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1276. test_same_voter(smartlist_get(con->voters, 1),
  1277. smartlist_get(v2->voters, 0));
  1278. test_same_voter(smartlist_get(con->voters, 2),
  1279. smartlist_get(v1->voters, 0));
  1280. test_same_voter(smartlist_get(con->voters, 3),
  1281. smartlist_get(v3->voters, 0));
  1282. consensus_test(con, now);
  1283. /* Check the routerstatuses. */
  1284. n_rs = smartlist_len(con->routerstatus_list);
  1285. for (idx = 0; idx < n_rs; ++idx) {
  1286. rs = smartlist_get(con->routerstatus_list, idx);
  1287. test_assert(rs);
  1288. rs_test(rs, now);
  1289. }
  1290. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1291. * The second one hasn't signed. The fourth one has signed: validate it. */
  1292. voter = smartlist_get(con->voters, 1);
  1293. test_eq(smartlist_len(voter->sigs), 0);
  1294. voter = smartlist_get(con->voters, 3);
  1295. test_eq(smartlist_len(voter->sigs), 1);
  1296. sig = smartlist_get(voter->sigs, 0);
  1297. test_assert(sig->signature);
  1298. test_assert(!sig->good_signature);
  1299. test_assert(!sig->bad_signature);
  1300. test_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1301. test_assert(sig->signature);
  1302. test_assert(sig->good_signature);
  1303. test_assert(!sig->bad_signature);
  1304. {
  1305. const char *msg=NULL;
  1306. /* Compute the other two signed consensuses. */
  1307. smartlist_shuffle(votes);
  1308. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1309. cert2->identity_key,
  1310. sign_skey_2, NULL,NULL,
  1311. FLAV_NS);
  1312. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1313. cert2->identity_key,
  1314. sign_skey_2, NULL,NULL,
  1315. FLAV_MICRODESC);
  1316. smartlist_shuffle(votes);
  1317. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1318. cert1->identity_key,
  1319. sign_skey_1, NULL,NULL,
  1320. FLAV_NS);
  1321. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1322. cert1->identity_key,
  1323. sign_skey_1, NULL,NULL,
  1324. FLAV_MICRODESC);
  1325. test_assert(consensus_text2);
  1326. test_assert(consensus_text3);
  1327. test_assert(consensus_text_md2);
  1328. test_assert(consensus_text_md3);
  1329. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1330. NS_TYPE_CONSENSUS);
  1331. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1332. NS_TYPE_CONSENSUS);
  1333. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1334. NS_TYPE_CONSENSUS);
  1335. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1336. NS_TYPE_CONSENSUS);
  1337. test_assert(con2);
  1338. test_assert(con3);
  1339. test_assert(con_md2);
  1340. test_assert(con_md3);
  1341. /* All three should have the same digest. */
  1342. test_memeq(&con->digests, &con2->digests, sizeof(digests_t));
  1343. test_memeq(&con->digests, &con3->digests, sizeof(digests_t));
  1344. test_memeq(&con_md->digests, &con_md2->digests, sizeof(digests_t));
  1345. test_memeq(&con_md->digests, &con_md3->digests, sizeof(digests_t));
  1346. /* Extract a detached signature from con3. */
  1347. detached_text1 = get_detached_sigs(con3, con_md3);
  1348. tt_assert(detached_text1);
  1349. /* Try to parse it. */
  1350. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1351. tt_assert(dsig1);
  1352. /* Are parsed values as expected? */
  1353. test_eq(dsig1->valid_after, con3->valid_after);
  1354. test_eq(dsig1->fresh_until, con3->fresh_until);
  1355. test_eq(dsig1->valid_until, con3->valid_until);
  1356. {
  1357. digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1358. test_assert(dsig_digests);
  1359. test_memeq(dsig_digests->d[DIGEST_SHA1], con3->digests.d[DIGEST_SHA1],
  1360. DIGEST_LEN);
  1361. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1362. test_assert(dsig_digests);
  1363. test_memeq(dsig_digests->d[DIGEST_SHA256],
  1364. con_md3->digests.d[DIGEST_SHA256],
  1365. DIGEST256_LEN);
  1366. }
  1367. {
  1368. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1369. test_assert(dsig_signatures);
  1370. test_eq(1, smartlist_len(dsig_signatures));
  1371. sig = smartlist_get(dsig_signatures, 0);
  1372. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1373. DIGEST_LEN);
  1374. test_eq(sig->alg, DIGEST_SHA1);
  1375. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1376. test_assert(dsig_signatures);
  1377. test_eq(1, smartlist_len(dsig_signatures));
  1378. sig = smartlist_get(dsig_signatures, 0);
  1379. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1380. DIGEST_LEN);
  1381. test_eq(sig->alg, DIGEST_SHA256);
  1382. }
  1383. /* Try adding it to con2. */
  1384. detached_text2 = get_detached_sigs(con2,con_md2);
  1385. test_eq(1, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1386. LOG_INFO, &msg));
  1387. tor_free(detached_text2);
  1388. test_eq(1, networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1389. LOG_INFO, &msg));
  1390. tor_free(detached_text2);
  1391. detached_text2 = get_detached_sigs(con2,con_md2);
  1392. //printf("\n<%s>\n", detached_text2);
  1393. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1394. test_assert(dsig2);
  1395. /*
  1396. printf("\n");
  1397. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1398. char hd[64];
  1399. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1400. printf("%s\n", hd);
  1401. });
  1402. */
  1403. test_eq(2,
  1404. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1405. test_eq(2,
  1406. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  1407. "microdesc")));
  1408. /* Try adding to con2 twice; verify that nothing changes. */
  1409. test_eq(0, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1410. LOG_INFO, &msg));
  1411. /* Add to con. */
  1412. test_eq(2, networkstatus_add_detached_signatures(con, dsig2, "test",
  1413. LOG_INFO, &msg));
  1414. /* Check signatures */
  1415. voter = smartlist_get(con->voters, 1);
  1416. sig = smartlist_get(voter->sigs, 0);
  1417. test_assert(sig);
  1418. test_assert(!networkstatus_check_document_signature(con, sig, cert2));
  1419. voter = smartlist_get(con->voters, 2);
  1420. sig = smartlist_get(voter->sigs, 0);
  1421. test_assert(sig);
  1422. test_assert(!networkstatus_check_document_signature(con, sig, cert1));
  1423. }
  1424. done:
  1425. smartlist_free(votes);
  1426. tor_free(v1_text);
  1427. tor_free(v2_text);
  1428. tor_free(v3_text);
  1429. tor_free(consensus_text);
  1430. tor_free(consensus_text_md);
  1431. if (vote)
  1432. networkstatus_vote_free(vote);
  1433. if (v1)
  1434. networkstatus_vote_free(v1);
  1435. if (v2)
  1436. networkstatus_vote_free(v2);
  1437. if (v3)
  1438. networkstatus_vote_free(v3);
  1439. if (con)
  1440. networkstatus_vote_free(con);
  1441. if (con_md)
  1442. networkstatus_vote_free(con_md);
  1443. if (sign_skey_1)
  1444. crypto_pk_free(sign_skey_1);
  1445. if (sign_skey_2)
  1446. crypto_pk_free(sign_skey_2);
  1447. if (sign_skey_3)
  1448. crypto_pk_free(sign_skey_3);
  1449. if (sign_skey_leg1)
  1450. crypto_pk_free(sign_skey_leg1);
  1451. if (cert1)
  1452. authority_cert_free(cert1);
  1453. if (cert2)
  1454. authority_cert_free(cert2);
  1455. if (cert3)
  1456. authority_cert_free(cert3);
  1457. tor_free(consensus_text2);
  1458. tor_free(consensus_text3);
  1459. tor_free(consensus_text_md2);
  1460. tor_free(consensus_text_md3);
  1461. tor_free(detached_text1);
  1462. tor_free(detached_text2);
  1463. if (con2)
  1464. networkstatus_vote_free(con2);
  1465. if (con3)
  1466. networkstatus_vote_free(con3);
  1467. if (con_md2)
  1468. networkstatus_vote_free(con_md2);
  1469. if (con_md3)
  1470. networkstatus_vote_free(con_md3);
  1471. if (dsig1)
  1472. ns_detached_signatures_free(dsig1);
  1473. if (dsig2)
  1474. ns_detached_signatures_free(dsig2);
  1475. }
  1476. /** Run unit tests for generating and parsing V3 consensus networkstatus
  1477. * documents. */
  1478. static void
  1479. test_dir_v3_networkstatus(void)
  1480. {
  1481. test_a_networkstatus(gen_routerstatus_for_v3ns,
  1482. vote_tweaks_for_v3ns,
  1483. test_vrs_for_v3ns,
  1484. test_consensus_for_v3ns,
  1485. test_routerstatus_for_v3ns);
  1486. }
  1487. static void
  1488. test_dir_scale_bw(void *testdata)
  1489. {
  1490. double v[8] = { 2.0/3,
  1491. 7.0,
  1492. 1.0,
  1493. 3.0,
  1494. 1.0/5,
  1495. 1.0/7,
  1496. 12.0,
  1497. 24.0 };
  1498. u64_dbl_t vals[8];
  1499. uint64_t total;
  1500. int i;
  1501. (void) testdata;
  1502. for (i=0; i<8; ++i)
  1503. vals[i].dbl = v[i];
  1504. scale_array_elements_to_u64(vals, 8, &total);
  1505. tt_int_op((int)total, ==, 48);
  1506. total = 0;
  1507. for (i=0; i<8; ++i) {
  1508. total += vals[i].u64;
  1509. }
  1510. tt_assert(total >= (U64_LITERAL(1)<<60));
  1511. tt_assert(total <= (U64_LITERAL(1)<<62));
  1512. for (i=0; i<8; ++i) {
  1513. double ratio = ((double)vals[i].u64) / vals[2].u64;
  1514. tt_double_op(fabs(ratio - v[i]), <, .00001);
  1515. }
  1516. done:
  1517. ;
  1518. }
  1519. static void
  1520. test_dir_random_weighted(void *testdata)
  1521. {
  1522. int histogram[10];
  1523. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  1524. u64_dbl_t inp[10];
  1525. int i, choice;
  1526. const int n = 50000;
  1527. double max_sq_error;
  1528. (void) testdata;
  1529. /* Try a ten-element array with values from 0 through 10. The values are
  1530. * in a scrambled order to make sure we don't depend on order. */
  1531. memset(histogram,0,sizeof(histogram));
  1532. for (i=0; i<10; ++i) {
  1533. inp[i].u64 = vals[i];
  1534. total += vals[i];
  1535. }
  1536. tt_int_op(total, ==, 45);
  1537. for (i=0; i<n; ++i) {
  1538. choice = choose_array_element_by_weight(inp, 10);
  1539. tt_int_op(choice, >=, 0);
  1540. tt_int_op(choice, <, 10);
  1541. histogram[choice]++;
  1542. }
  1543. /* Now see if we chose things about frequently enough. */
  1544. max_sq_error = 0;
  1545. for (i=0; i<10; ++i) {
  1546. int expected = (int)(n*vals[i]/total);
  1547. double frac_diff = 0, sq;
  1548. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1549. if (expected)
  1550. frac_diff = (histogram[i] - expected) / ((double)expected);
  1551. else
  1552. tt_int_op(histogram[i], ==, 0);
  1553. sq = frac_diff * frac_diff;
  1554. if (sq > max_sq_error)
  1555. max_sq_error = sq;
  1556. }
  1557. /* It should almost always be much much less than this. If you want to
  1558. * figure out the odds, please feel free. */
  1559. tt_double_op(max_sq_error, <, .05);
  1560. /* Now try a singleton; do we choose it? */
  1561. for (i = 0; i < 100; ++i) {
  1562. choice = choose_array_element_by_weight(inp, 1);
  1563. tt_int_op(choice, ==, 0);
  1564. }
  1565. /* Now try an array of zeros. We should choose randomly. */
  1566. memset(histogram,0,sizeof(histogram));
  1567. for (i = 0; i < 5; ++i)
  1568. inp[i].u64 = 0;
  1569. for (i = 0; i < n; ++i) {
  1570. choice = choose_array_element_by_weight(inp, 5);
  1571. tt_int_op(choice, >=, 0);
  1572. tt_int_op(choice, <, 5);
  1573. histogram[choice]++;
  1574. }
  1575. /* Now see if we chose things about frequently enough. */
  1576. max_sq_error = 0;
  1577. for (i=0; i<5; ++i) {
  1578. int expected = n/5;
  1579. double frac_diff = 0, sq;
  1580. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1581. frac_diff = (histogram[i] - expected) / ((double)expected);
  1582. sq = frac_diff * frac_diff;
  1583. if (sq > max_sq_error)
  1584. max_sq_error = sq;
  1585. }
  1586. /* It should almost always be much much less than this. If you want to
  1587. * figure out the odds, please feel free. */
  1588. tt_double_op(max_sq_error, <, .05);
  1589. done:
  1590. ;
  1591. }
  1592. /* Function pointers for test_dir_clip_unmeasured_bw() */
  1593. static uint32_t alternate_clip_bw = 0;
  1594. /**
  1595. * Generate a routerstatus for clip_unmeasured_bw test; based on the
  1596. * v3_networkstatus ones.
  1597. */
  1598. static vote_routerstatus_t *
  1599. gen_routerstatus_for_umbw(int idx, time_t now)
  1600. {
  1601. vote_routerstatus_t *vrs = NULL;
  1602. routerstatus_t *rs;
  1603. tor_addr_t addr_ipv6;
  1604. uint32_t max_unmeasured_bw = (alternate_clip_bw > 0) ?
  1605. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW;
  1606. switch (idx) {
  1607. case 0:
  1608. /* Generate the first routerstatus. */
  1609. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1610. rs = &vrs->status;
  1611. vrs->version = tor_strdup("0.1.2.14");
  1612. rs->published_on = now-1500;
  1613. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  1614. memset(rs->identity_digest, 3, DIGEST_LEN);
  1615. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  1616. rs->addr = 0x99008801;
  1617. rs->or_port = 443;
  1618. rs->dir_port = 8000;
  1619. /* all flags but running cleared */
  1620. rs->is_flagged_running = 1;
  1621. /*
  1622. * This one has measured bandwidth below the clip cutoff, and
  1623. * so shouldn't be clipped; we'll have to test that it isn't
  1624. * later.
  1625. */
  1626. vrs->has_measured_bw = 1;
  1627. rs->has_bandwidth = 1;
  1628. vrs->measured_bw = rs->bandwidth = max_unmeasured_bw / 2;
  1629. break;
  1630. case 1:
  1631. /* Generate the second routerstatus. */
  1632. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1633. rs = &vrs->status;
  1634. vrs->version = tor_strdup("0.2.0.5");
  1635. rs->published_on = now-1000;
  1636. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  1637. memset(rs->identity_digest, 5, DIGEST_LEN);
  1638. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  1639. rs->addr = 0x99009901;
  1640. rs->or_port = 443;
  1641. rs->dir_port = 0;
  1642. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1643. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  1644. rs->ipv6_orport = 4711;
  1645. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  1646. rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
  1647. /*
  1648. * This one has measured bandwidth above the clip cutoff, and
  1649. * so shouldn't be clipped; we'll have to test that it isn't
  1650. * later.
  1651. */
  1652. vrs->has_measured_bw = 1;
  1653. rs->has_bandwidth = 1;
  1654. vrs->measured_bw = rs->bandwidth = 2 * max_unmeasured_bw;
  1655. break;
  1656. case 2:
  1657. /* Generate the third routerstatus. */
  1658. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1659. rs = &vrs->status;
  1660. vrs->version = tor_strdup("0.1.0.3");
  1661. rs->published_on = now-1000;
  1662. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  1663. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  1664. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  1665. rs->addr = 0xAA009901;
  1666. rs->or_port = 400;
  1667. rs->dir_port = 9999;
  1668. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  1669. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  1670. rs->is_possible_guard = 1;
  1671. /*
  1672. * This one has unmeasured bandwidth above the clip cutoff, and
  1673. * so should be clipped; we'll have to test that it isn't
  1674. * later.
  1675. */
  1676. vrs->has_measured_bw = 0;
  1677. rs->has_bandwidth = 1;
  1678. vrs->measured_bw = 0;
  1679. rs->bandwidth = 2 * max_unmeasured_bw;
  1680. break;
  1681. case 3:
  1682. /* Generate a fourth routerstatus that is not running. */
  1683. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1684. rs = &vrs->status;
  1685. vrs->version = tor_strdup("0.1.6.3");
  1686. rs->published_on = now-1000;
  1687. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  1688. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  1689. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  1690. rs->addr = 0xC0000203;
  1691. rs->or_port = 500;
  1692. rs->dir_port = 1999;
  1693. /* all flags but running cleared */
  1694. rs->is_flagged_running = 1;
  1695. /*
  1696. * This one has unmeasured bandwidth below the clip cutoff, and
  1697. * so shouldn't be clipped; we'll have to test that it isn't
  1698. * later.
  1699. */
  1700. vrs->has_measured_bw = 0;
  1701. rs->has_bandwidth = 1;
  1702. vrs->measured_bw = 0;
  1703. rs->bandwidth = max_unmeasured_bw / 2;
  1704. break;
  1705. case 4:
  1706. /* No more for this test; return NULL */
  1707. vrs = NULL;
  1708. break;
  1709. default:
  1710. /* Shouldn't happen */
  1711. test_assert(0);
  1712. }
  1713. if (vrs) {
  1714. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  1715. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  1716. "m 9,10,11,12,13,14,15,16,17 "
  1717. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  1718. idx);
  1719. }
  1720. done:
  1721. return vrs;
  1722. }
  1723. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  1724. * just adding the right consensus methods to let clipping happen */
  1725. static int
  1726. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  1727. {
  1728. char *maxbw_param = NULL;
  1729. int rv = 0;
  1730. test_assert(v);
  1731. (void)voter;
  1732. (void)now;
  1733. test_assert(v->supported_methods);
  1734. smartlist_clear(v->supported_methods);
  1735. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW */
  1736. smartlist_split_string(v->supported_methods,
  1737. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
  1738. NULL, 0, -1);
  1739. /* If we're using a non-default clip bandwidth, add it to net_params */
  1740. if (alternate_clip_bw > 0) {
  1741. tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
  1742. test_assert(maxbw_param);
  1743. if (maxbw_param) {
  1744. smartlist_add(v->net_params, maxbw_param);
  1745. rv = 1;
  1746. }
  1747. }
  1748. done:
  1749. return rv;
  1750. }
  1751. /**
  1752. * Test a parsed vote_routerstatus_t for umbw test.
  1753. */
  1754. static void
  1755. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  1756. {
  1757. routerstatus_t *rs;
  1758. tor_addr_t addr_ipv6;
  1759. uint32_t max_unmeasured_bw = (alternate_clip_bw > 0) ?
  1760. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW;
  1761. (void)voter;
  1762. test_assert(vrs);
  1763. rs = &(vrs->status);
  1764. test_assert(rs);
  1765. /* Split out by digests to test */
  1766. if (tor_memeq(rs->identity_digest,
  1767. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1768. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1769. DIGEST_LEN)) {
  1770. /*
  1771. * Check the first routerstatus - measured bandwidth below the clip
  1772. * cutoff.
  1773. */
  1774. test_streq(vrs->version, "0.1.2.14");
  1775. test_eq(rs->published_on, now-1500);
  1776. test_streq(rs->nickname, "router2");
  1777. test_memeq(rs->identity_digest,
  1778. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1779. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1780. DIGEST_LEN);
  1781. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1782. test_eq(rs->addr, 0x99008801);
  1783. test_eq(rs->or_port, 443);
  1784. test_eq(rs->dir_port, 8000);
  1785. test_assert(rs->has_bandwidth);
  1786. test_assert(vrs->has_measured_bw);
  1787. test_eq(rs->bandwidth, max_unmeasured_bw / 2);
  1788. test_eq(vrs->measured_bw, max_unmeasured_bw / 2);
  1789. } else if (tor_memeq(rs->identity_digest,
  1790. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1791. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1792. DIGEST_LEN)) {
  1793. /*
  1794. * Check the second routerstatus - measured bandwidth above the clip
  1795. * cutoff.
  1796. */
  1797. test_streq(vrs->version, "0.2.0.5");
  1798. test_eq(rs->published_on, now-1000);
  1799. test_streq(rs->nickname, "router1");
  1800. test_memeq(rs->identity_digest,
  1801. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1802. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1803. DIGEST_LEN);
  1804. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1805. test_eq(rs->addr, 0x99009901);
  1806. test_eq(rs->or_port, 443);
  1807. test_eq(rs->dir_port, 0);
  1808. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1809. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1810. test_eq(rs->ipv6_orport, 4711);
  1811. test_assert(rs->has_bandwidth);
  1812. test_assert(vrs->has_measured_bw);
  1813. test_eq(rs->bandwidth, max_unmeasured_bw * 2);
  1814. test_eq(vrs->measured_bw, max_unmeasured_bw * 2);
  1815. } else if (tor_memeq(rs->identity_digest,
  1816. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1817. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1818. DIGEST_LEN)) {
  1819. /*
  1820. * Check the third routerstatus - unmeasured bandwidth above the clip
  1821. * cutoff; this one should be clipped later on in the consensus, but
  1822. * appears unclipped in the vote.
  1823. */
  1824. test_assert(rs->has_bandwidth);
  1825. test_assert(!(vrs->has_measured_bw));
  1826. test_eq(rs->bandwidth, max_unmeasured_bw * 2);
  1827. test_eq(vrs->measured_bw, 0);
  1828. } else if (tor_memeq(rs->identity_digest,
  1829. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  1830. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  1831. DIGEST_LEN)) {
  1832. /*
  1833. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  1834. * cutoff; this one should not be clipped.
  1835. */
  1836. test_assert(rs->has_bandwidth);
  1837. test_assert(!(vrs->has_measured_bw));
  1838. test_eq(rs->bandwidth, max_unmeasured_bw / 2);
  1839. test_eq(vrs->measured_bw, 0);
  1840. } else {
  1841. test_assert(0);
  1842. }
  1843. done:
  1844. return;
  1845. }
  1846. /**
  1847. * Test a consensus for v3_networkstatus_test
  1848. */
  1849. static void
  1850. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  1851. {
  1852. (void)now;
  1853. test_assert(con);
  1854. test_assert(!con->cert);
  1855. /* test_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW); */
  1856. test_assert(con->consensus_method >= 16);
  1857. test_eq(4, smartlist_len(con->routerstatus_list));
  1858. /* There should be four listed routers; all voters saw the same in this */
  1859. done:
  1860. return;
  1861. }
  1862. /**
  1863. * Test a router list entry for umbw test
  1864. */
  1865. static void
  1866. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  1867. {
  1868. tor_addr_t addr_ipv6;
  1869. uint32_t max_unmeasured_bw = (alternate_clip_bw > 0) ?
  1870. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW;
  1871. test_assert(rs);
  1872. /* There should be four listed routers, as constructed above */
  1873. if (tor_memeq(rs->identity_digest,
  1874. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1875. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1876. DIGEST_LEN)) {
  1877. test_memeq(rs->identity_digest,
  1878. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1879. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1880. DIGEST_LEN);
  1881. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1882. test_assert(!rs->is_authority);
  1883. test_assert(!rs->is_exit);
  1884. test_assert(!rs->is_fast);
  1885. test_assert(!rs->is_possible_guard);
  1886. test_assert(!rs->is_stable);
  1887. /* (If it wasn't running it wouldn't be here) */
  1888. test_assert(rs->is_flagged_running);
  1889. test_assert(!rs->is_v2_dir);
  1890. test_assert(!rs->is_valid);
  1891. test_assert(!rs->is_named);
  1892. /* This one should have measured bandwidth below the clip cutoff */
  1893. test_assert(rs->has_bandwidth);
  1894. test_eq(rs->bandwidth, max_unmeasured_bw / 2);
  1895. test_assert(!(rs->bw_is_unmeasured));
  1896. } else if (tor_memeq(rs->identity_digest,
  1897. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1898. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1899. DIGEST_LEN)) {
  1900. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  1901. test_memeq(rs->identity_digest,
  1902. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1903. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1904. DIGEST_LEN);
  1905. test_streq(rs->nickname, "router1");
  1906. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1907. test_eq(rs->published_on, now-1000);
  1908. test_eq(rs->addr, 0x99009901);
  1909. test_eq(rs->or_port, 443);
  1910. test_eq(rs->dir_port, 0);
  1911. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1912. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1913. test_eq(rs->ipv6_orport, 4711);
  1914. test_assert(!rs->is_authority);
  1915. test_assert(rs->is_exit);
  1916. test_assert(rs->is_fast);
  1917. test_assert(rs->is_possible_guard);
  1918. test_assert(rs->is_stable);
  1919. test_assert(rs->is_flagged_running);
  1920. test_assert(rs->is_v2_dir);
  1921. test_assert(rs->is_valid);
  1922. test_assert(!rs->is_named);
  1923. /* This one should have measured bandwidth above the clip cutoff */
  1924. test_assert(rs->has_bandwidth);
  1925. test_eq(rs->bandwidth, max_unmeasured_bw * 2);
  1926. test_assert(!(rs->bw_is_unmeasured));
  1927. } else if (tor_memeq(rs->identity_digest,
  1928. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1929. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1930. DIGEST_LEN)) {
  1931. /*
  1932. * This one should have unmeasured bandwidth above the clip cutoff,
  1933. * and so should be clipped
  1934. */
  1935. test_assert(rs->has_bandwidth);
  1936. test_eq(rs->bandwidth, max_unmeasured_bw);
  1937. test_assert(rs->bw_is_unmeasured);
  1938. } else if (tor_memeq(rs->identity_digest,
  1939. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  1940. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  1941. DIGEST_LEN)) {
  1942. /*
  1943. * This one should have unmeasured bandwidth below the clip cutoff,
  1944. * and so should not be clipped
  1945. */
  1946. test_assert(rs->has_bandwidth);
  1947. test_eq(rs->bandwidth, max_unmeasured_bw / 2);
  1948. test_assert(rs->bw_is_unmeasured);
  1949. } else {
  1950. /* Weren't expecting this... */
  1951. test_assert(0);
  1952. }
  1953. done:
  1954. return;
  1955. }
  1956. /**
  1957. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  1958. * method 17; this uses the same test_a_networkstatus() function that the
  1959. * v3_networkstatus test uses.
  1960. */
  1961. static void
  1962. test_dir_clip_unmeasured_bw(void)
  1963. {
  1964. /* Run the test with the default clip bandwidth */
  1965. alternate_clip_bw = 0;
  1966. test_a_networkstatus(gen_routerstatus_for_umbw,
  1967. vote_tweaks_for_umbw,
  1968. test_vrs_for_umbw,
  1969. test_consensus_for_umbw,
  1970. test_routerstatus_for_umbw);
  1971. }
  1972. /**
  1973. * This version of test_dir_clip_unmeasured_bw() uses a non-default choice of
  1974. * clip bandwidth.
  1975. */
  1976. static void
  1977. test_dir_clip_unmeasured_bw_alt(void)
  1978. {
  1979. /*
  1980. * Try a different one; this value is chosen so that the below-the-cutoff
  1981. * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
  1982. * DEFAULT_MAX_UNMEASURED_BW and if the consensus incorrectly uses that
  1983. * cutoff it will fail the test.
  1984. */
  1985. alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW;
  1986. test_a_networkstatus(gen_routerstatus_for_umbw,
  1987. vote_tweaks_for_umbw,
  1988. test_vrs_for_umbw,
  1989. test_consensus_for_umbw,
  1990. test_routerstatus_for_umbw);
  1991. }
  1992. extern time_t time_of_process_start; /* from main.c */
  1993. static void
  1994. test_dir_v2_dir(void *arg)
  1995. {
  1996. /* Runs in a forked process: acts like a v2 directory just enough to make and
  1997. * sign a v2 networkstatus opinion */
  1998. cached_dir_t *v2 = NULL;
  1999. or_options_t *options = get_options_mutable();
  2000. crypto_pk_t *id_key = pk_generate(4);
  2001. (void) arg;
  2002. options->ORPort_set = 1; /* So we believe we're a server. */
  2003. options->DirPort_set = 1;
  2004. options->Address = tor_strdup("99.99.99.99");
  2005. options->Nickname = tor_strdup("TestV2Auth");
  2006. options->ContactInfo = tor_strdup("TestV2Auth <testv2auth@example.com>");
  2007. {
  2008. /* Give it a DirPort */
  2009. smartlist_t *ports = (smartlist_t *)get_configured_ports();
  2010. port_cfg_t *port = tor_malloc_zero(sizeof(port_cfg_t));
  2011. port->type = CONN_TYPE_DIR_LISTENER;
  2012. port->port = 9999;
  2013. smartlist_add(ports, port);
  2014. }
  2015. set_server_identity_key(id_key);
  2016. set_client_identity_key(id_key);
  2017. /* Add a router. */
  2018. {
  2019. was_router_added_t wra;
  2020. const char *msg = NULL;
  2021. routerinfo_t *r1 = tor_malloc_zero(sizeof(routerinfo_t));
  2022. r1->address = tor_strdup("18.244.0.1");
  2023. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  2024. r1->cache_info.published_on = time(NULL)-60;
  2025. r1->or_port = 9000;
  2026. r1->dir_port = 9003;
  2027. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  2028. r1->ipv6_orport = 9999;
  2029. r1->onion_pkey = pk_generate(1);
  2030. r1->identity_pkey = pk_generate(2);
  2031. r1->bandwidthrate = 1000;
  2032. r1->bandwidthburst = 5000;
  2033. r1->bandwidthcapacity = 10000;
  2034. r1->exit_policy = NULL;
  2035. r1->nickname = tor_strdup("Magri");
  2036. r1->platform = tor_strdup("Tor 0.2.7.7-gamma");
  2037. r1->cache_info.routerlist_index = -1;
  2038. r1->cache_info.signed_descriptor_body =
  2039. router_dump_router_to_string(r1, r1->identity_pkey);
  2040. r1->cache_info.signed_descriptor_len =
  2041. strlen(r1->cache_info.signed_descriptor_body);
  2042. wra = router_add_to_routerlist(r1, &msg, 0, 0);
  2043. tt_int_op(wra, ==, ROUTER_ADDED_SUCCESSFULLY);
  2044. }
  2045. /* Prevent call of rep_hist_note_router_unreachable(). */
  2046. time_of_process_start = time(NULL);
  2047. /* Make a directory so there's somewhere to store the thing */
  2048. #ifdef _WIN32
  2049. mkdir(get_fname("cached-status"));
  2050. #else
  2051. mkdir(get_fname("cached-status"), 0700);
  2052. #endif
  2053. v2 = generate_v2_networkstatus_opinion();
  2054. tt_assert(v2);
  2055. done:
  2056. crypto_pk_free(id_key);
  2057. cached_dir_decref(v2);
  2058. }
  2059. static void
  2060. test_dir_fmt_control_ns(void *arg)
  2061. {
  2062. char *s = NULL;
  2063. routerstatus_t rs;
  2064. (void)arg;
  2065. memset(&rs, 0, sizeof(rs));
  2066. rs.published_on = 1364925198;
  2067. strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
  2068. memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
  2069. memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
  2070. rs.addr = 0x20304050;
  2071. rs.or_port = 9001;
  2072. rs.dir_port = 9002;
  2073. rs.is_exit = 1;
  2074. rs.is_fast = 1;
  2075. rs.is_flagged_running = 1;
  2076. rs.has_bandwidth = 1;
  2077. rs.bandwidth = 1000;
  2078. s = networkstatus_getinfo_helper_single(&rs);
  2079. tt_assert(s);
  2080. tt_str_op(s, ==,
  2081. "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
  2082. "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
  2083. "32.48.64.80 9001 9002\n"
  2084. "s Exit Fast Running\n"
  2085. "w Bandwidth=1000\n");
  2086. done:
  2087. tor_free(s);
  2088. }
  2089. #define DIR_LEGACY(name) \
  2090. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
  2091. #define DIR(name,flags) \
  2092. { #name, test_dir_##name, (flags), NULL, NULL }
  2093. struct testcase_t dir_tests[] = {
  2094. DIR_LEGACY(nicknames),
  2095. DIR_LEGACY(formats),
  2096. DIR_LEGACY(versions),
  2097. DIR_LEGACY(fp_pairs),
  2098. DIR(split_fps, 0),
  2099. DIR_LEGACY(measured_bw),
  2100. DIR_LEGACY(param_voting),
  2101. DIR_LEGACY(v3_networkstatus),
  2102. DIR(random_weighted, 0),
  2103. DIR(scale_bw, 0),
  2104. DIR_LEGACY(clip_unmeasured_bw),
  2105. DIR_LEGACY(clip_unmeasured_bw_alt),
  2106. DIR(v2_dir, TT_FORK),
  2107. DIR(fmt_control_ns, 0),
  2108. END_OF_TESTCASES
  2109. };