test_dir.c 83 KB

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