test_dir.c 84 KB

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