test_dir.c 102 KB

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