test_dir.c 83 KB

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