test_dir.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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 "crypto_ed25519.h"
  16. #include "directory.h"
  17. #include "dirserv.h"
  18. #include "dirvote.h"
  19. #include "hibernate.h"
  20. #include "networkstatus.h"
  21. #include "router.h"
  22. #include "routerkeys.h"
  23. #include "routerlist.h"
  24. #include "routerparse.h"
  25. #include "routerset.h"
  26. #include "test.h"
  27. #include "test_dir_common.h"
  28. #include "torcert.h"
  29. static void
  30. test_dir_nicknames(void *arg)
  31. {
  32. (void)arg;
  33. tt_assert( is_legal_nickname("a"));
  34. tt_assert(!is_legal_nickname(""));
  35. tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  36. tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
  37. tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  38. tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  39. /* valid */
  40. tt_assert( is_legal_nickname_or_hexdigest(
  41. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  42. tt_assert( is_legal_nickname_or_hexdigest(
  43. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  44. tt_assert( is_legal_nickname_or_hexdigest(
  45. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  46. /* too short */
  47. tt_assert(!is_legal_nickname_or_hexdigest(
  48. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  49. /* illegal char */
  50. tt_assert(!is_legal_nickname_or_hexdigest(
  51. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  52. /* hex part too long */
  53. tt_assert(!is_legal_nickname_or_hexdigest(
  54. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  55. tt_assert(!is_legal_nickname_or_hexdigest(
  56. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  57. /* Bad nickname */
  58. tt_assert(!is_legal_nickname_or_hexdigest(
  59. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  60. tt_assert(!is_legal_nickname_or_hexdigest(
  61. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  62. tt_assert(!is_legal_nickname_or_hexdigest(
  63. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  64. tt_assert(!is_legal_nickname_or_hexdigest(
  65. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  66. "abcdefghijklmnoppqrst"));
  67. /* Bad extra char. */
  68. tt_assert(!is_legal_nickname_or_hexdigest(
  69. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  70. tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  71. tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  72. tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  73. done:
  74. ;
  75. }
  76. /** Run unit tests for router descriptor generation logic. */
  77. static void
  78. test_dir_formats(void *arg)
  79. {
  80. char *buf = NULL;
  81. char buf2[8192];
  82. char platform[256];
  83. char fingerprint[FINGERPRINT_LEN+1];
  84. char *pk1_str = NULL, *pk2_str = NULL, *cp;
  85. size_t pk1_str_len, pk2_str_len;
  86. routerinfo_t *r1=NULL, *r2=NULL;
  87. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  88. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  89. addr_policy_t *ex1, *ex2;
  90. routerlist_t *dir1 = NULL, *dir2 = NULL;
  91. uint8_t *rsa_cc = NULL;
  92. or_options_t *options = get_options_mutable();
  93. const addr_policy_t *p;
  94. time_t now = time(NULL);
  95. (void)arg;
  96. pk1 = pk_generate(0);
  97. pk2 = pk_generate(1);
  98. tt_assert(pk1 && pk2);
  99. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  100. get_platform_str(platform, sizeof(platform));
  101. r1 = tor_malloc_zero(sizeof(routerinfo_t));
  102. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  103. r1->cache_info.published_on = 0;
  104. r1->or_port = 9000;
  105. r1->dir_port = 9003;
  106. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  107. r1->ipv6_orport = 9999;
  108. r1->onion_pkey = crypto_pk_dup_key(pk1);
  109. r1->identity_pkey = crypto_pk_dup_key(pk2);
  110. r1->bandwidthrate = 1000;
  111. r1->bandwidthburst = 5000;
  112. r1->bandwidthcapacity = 10000;
  113. r1->exit_policy = NULL;
  114. r1->nickname = tor_strdup("Magri");
  115. r1->platform = tor_strdup(platform);
  116. ex1 = tor_malloc_zero(sizeof(addr_policy_t));
  117. ex2 = tor_malloc_zero(sizeof(addr_policy_t));
  118. ex1->policy_type = ADDR_POLICY_ACCEPT;
  119. tor_addr_from_ipv4h(&ex1->addr, 0);
  120. ex1->maskbits = 0;
  121. ex1->prt_min = ex1->prt_max = 80;
  122. ex2->policy_type = ADDR_POLICY_REJECT;
  123. tor_addr_from_ipv4h(&ex2->addr, 18<<24);
  124. ex2->maskbits = 8;
  125. ex2->prt_min = ex2->prt_max = 24;
  126. r2 = tor_malloc_zero(sizeof(routerinfo_t));
  127. r2->addr = 0x0a030201u; /* 10.3.2.1 */
  128. ed25519_keypair_t kp1, kp2;
  129. ed25519_secret_key_from_seed(&kp1.seckey,
  130. (const uint8_t*)"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
  131. ed25519_public_key_generate(&kp1.pubkey, &kp1.seckey);
  132. ed25519_secret_key_from_seed(&kp2.seckey,
  133. (const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  134. ed25519_public_key_generate(&kp2.pubkey, &kp2.seckey);
  135. r2->signing_key_cert = tor_cert_create(&kp1,
  136. CERT_TYPE_ID_SIGNING,
  137. &kp2.pubkey,
  138. now, 86400,
  139. CERT_FLAG_INCLUDE_SIGNING_KEY);
  140. char cert_buf[256];
  141. base64_encode(cert_buf, sizeof(cert_buf),
  142. (const char*)r2->signing_key_cert->encoded,
  143. r2->signing_key_cert->encoded_len,
  144. BASE64_ENCODE_MULTILINE);
  145. r2->platform = tor_strdup(platform);
  146. r2->cache_info.published_on = 5;
  147. r2->or_port = 9005;
  148. r2->dir_port = 0;
  149. r2->onion_pkey = crypto_pk_dup_key(pk2);
  150. curve25519_keypair_t r2_onion_keypair;
  151. curve25519_keypair_generate(&r2_onion_keypair, 0);
  152. r2->onion_curve25519_pkey = tor_memdup(&r2_onion_keypair.pubkey,
  153. sizeof(curve25519_public_key_t));
  154. r2->identity_pkey = crypto_pk_dup_key(pk1);
  155. r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
  156. r2->exit_policy = smartlist_new();
  157. smartlist_add(r2->exit_policy, ex1);
  158. smartlist_add(r2->exit_policy, ex2);
  159. r2->nickname = tor_strdup("Fred");
  160. tt_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  161. &pk1_str_len));
  162. tt_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  163. &pk2_str_len));
  164. /* XXXX025 router_dump_to_string should really take this from ri.*/
  165. options->ContactInfo = tor_strdup("Magri White "
  166. "<magri@elsewhere.example.com>");
  167. options->ORPort_set = options->DirPort_set = options->AssumeReachable = 1;
  168. buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
  169. tor_free(options->ContactInfo);
  170. /* Reset for later */
  171. options->ORPort_set = options->DirPort_set = options->AssumeReachable = 0;
  172. tt_assert(buf);
  173. strlcpy(buf2, "router Magri 192.168.0.1 9000 0 9003\n"
  174. "or-address [1:2:3:4::]:9999\n"
  175. "platform Tor "VERSION" on ", sizeof(buf2));
  176. strlcat(buf2, get_uname(), sizeof(buf2));
  177. strlcat(buf2, "\n"
  178. "protocols Link 1 2 Circuit 1\n"
  179. "published 1970-01-01 00:00:00\n"
  180. "fingerprint ", sizeof(buf2));
  181. tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  182. strlcat(buf2, fingerprint, sizeof(buf2));
  183. strlcat(buf2, "\nuptime 0\n"
  184. /* XXX the "0" above is hard-coded, but even if we made it reflect
  185. * uptime, that still wouldn't make it right, because the two
  186. * descriptors might be made on different seconds... hm. */
  187. "bandwidth 1000 5000 10000\n"
  188. "onion-key\n", sizeof(buf2));
  189. strlcat(buf2, pk1_str, sizeof(buf2));
  190. strlcat(buf2, "signing-key\n", sizeof(buf2));
  191. strlcat(buf2, pk2_str, sizeof(buf2));
  192. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  193. strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
  194. sizeof(buf2));
  195. strlcat(buf2, "reject *:*\n", sizeof(buf2));
  196. strlcat(buf2, "tunnelled-dir-server\nrouter-signature\n", sizeof(buf2));
  197. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  198. * twice */
  199. tt_str_op(buf,OP_EQ, buf2);
  200. tor_free(buf);
  201. buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
  202. tt_assert(buf);
  203. cp = buf;
  204. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
  205. tt_assert(rp1);
  206. tt_int_op(rp1->addr,OP_EQ, r1->addr);
  207. tt_int_op(rp1->or_port,OP_EQ, r1->or_port);
  208. tt_int_op(rp1->dir_port,OP_EQ, r1->dir_port);
  209. tt_int_op(rp1->bandwidthrate,OP_EQ, r1->bandwidthrate);
  210. tt_int_op(rp1->bandwidthburst,OP_EQ, r1->bandwidthburst);
  211. tt_int_op(rp1->bandwidthcapacity,OP_EQ, r1->bandwidthcapacity);
  212. tt_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  213. tt_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  214. tt_assert(rp1->supports_tunnelled_dir_requests);
  215. //tt_assert(rp1->exit_policy == NULL);
  216. tor_free(buf);
  217. strlcpy(buf2,
  218. "router Fred 10.3.2.1 9005 0 0\n"
  219. "identity-ed25519\n"
  220. "-----BEGIN ED25519 CERT-----\n", sizeof(buf2));
  221. strlcat(buf2, cert_buf, sizeof(buf2));
  222. strlcat(buf2, "-----END ED25519 CERT-----\n", sizeof(buf2));
  223. strlcat(buf2, "master-key-ed25519 ", sizeof(buf2));
  224. {
  225. char k[ED25519_BASE64_LEN+1];
  226. tt_assert(ed25519_public_to_base64(k, &r2->signing_key_cert->signing_key)
  227. >= 0);
  228. strlcat(buf2, k, sizeof(buf2));
  229. strlcat(buf2, "\n", sizeof(buf2));
  230. }
  231. strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2));
  232. strlcat(buf2, get_uname(), sizeof(buf2));
  233. strlcat(buf2, "\n"
  234. "protocols Link 1 2 Circuit 1\n"
  235. "published 1970-01-01 00:00:05\n"
  236. "fingerprint ", sizeof(buf2));
  237. tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
  238. strlcat(buf2, fingerprint, sizeof(buf2));
  239. strlcat(buf2, "\nuptime 0\n"
  240. "bandwidth 3000 3000 3000\n", sizeof(buf2));
  241. strlcat(buf2, "onion-key\n", sizeof(buf2));
  242. strlcat(buf2, pk2_str, sizeof(buf2));
  243. strlcat(buf2, "signing-key\n", sizeof(buf2));
  244. strlcat(buf2, pk1_str, sizeof(buf2));
  245. int rsa_cc_len;
  246. rsa_cc = make_tap_onion_key_crosscert(pk2,
  247. &kp1.pubkey,
  248. pk1,
  249. &rsa_cc_len);
  250. tt_assert(rsa_cc);
  251. base64_encode(cert_buf, sizeof(cert_buf), (char*)rsa_cc, rsa_cc_len,
  252. BASE64_ENCODE_MULTILINE);
  253. strlcat(buf2, "onion-key-crosscert\n"
  254. "-----BEGIN CROSSCERT-----\n", sizeof(buf2));
  255. strlcat(buf2, cert_buf, sizeof(buf2));
  256. strlcat(buf2, "-----END CROSSCERT-----\n", sizeof(buf2));
  257. int ntor_cc_sign;
  258. {
  259. tor_cert_t *ntor_cc = NULL;
  260. ntor_cc = make_ntor_onion_key_crosscert(&r2_onion_keypair,
  261. &kp1.pubkey,
  262. r2->cache_info.published_on,
  263. MIN_ONION_KEY_LIFETIME,
  264. &ntor_cc_sign);
  265. tt_assert(ntor_cc);
  266. base64_encode(cert_buf, sizeof(cert_buf),
  267. (char*)ntor_cc->encoded, ntor_cc->encoded_len,
  268. BASE64_ENCODE_MULTILINE);
  269. tor_cert_free(ntor_cc);
  270. }
  271. tor_snprintf(buf2+strlen(buf2), sizeof(buf2)-strlen(buf2),
  272. "ntor-onion-key-crosscert %d\n"
  273. "-----BEGIN ED25519 CERT-----\n"
  274. "%s"
  275. "-----END ED25519 CERT-----\n", ntor_cc_sign, cert_buf);
  276. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  277. strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
  278. base64_encode(cert_buf, sizeof(cert_buf),
  279. (const char*)r2_onion_keypair.pubkey.public_key, 32,
  280. BASE64_ENCODE_MULTILINE);
  281. strlcat(buf2, cert_buf, sizeof(buf2));
  282. strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
  283. strlcat(buf2, "tunnelled-dir-server\n", sizeof(buf2));
  284. strlcat(buf2, "router-sig-ed25519 ", sizeof(buf2));
  285. options->ORPort_set = 1;
  286. buf = router_dump_router_to_string(r2, pk1, pk2, &r2_onion_keypair, &kp2);
  287. tt_assert(buf);
  288. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  289. * twice */
  290. tt_str_op(buf, OP_EQ, buf2);
  291. tor_free(buf);
  292. buf = router_dump_router_to_string(r2, pk1, NULL, NULL, NULL);
  293. /* Reset for later */
  294. options->ORPort_set = 0;
  295. cp = buf;
  296. rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
  297. tt_assert(rp2);
  298. tt_int_op(rp2->addr,OP_EQ, r2->addr);
  299. tt_int_op(rp2->or_port,OP_EQ, r2->or_port);
  300. tt_int_op(rp2->dir_port,OP_EQ, r2->dir_port);
  301. tt_int_op(rp2->bandwidthrate,OP_EQ, r2->bandwidthrate);
  302. tt_int_op(rp2->bandwidthburst,OP_EQ, r2->bandwidthburst);
  303. tt_int_op(rp2->bandwidthcapacity,OP_EQ, r2->bandwidthcapacity);
  304. tt_mem_op(rp2->onion_curve25519_pkey->public_key,OP_EQ,
  305. r2->onion_curve25519_pkey->public_key,
  306. CURVE25519_PUBKEY_LEN);
  307. tt_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  308. tt_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  309. tt_assert(rp2->supports_tunnelled_dir_requests);
  310. tt_int_op(smartlist_len(rp2->exit_policy),OP_EQ, 2);
  311. p = smartlist_get(rp2->exit_policy, 0);
  312. tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_ACCEPT);
  313. tt_assert(tor_addr_is_null(&p->addr));
  314. tt_int_op(p->maskbits,OP_EQ, 0);
  315. tt_int_op(p->prt_min,OP_EQ, 80);
  316. tt_int_op(p->prt_max,OP_EQ, 80);
  317. p = smartlist_get(rp2->exit_policy, 1);
  318. tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_REJECT);
  319. tt_assert(tor_addr_eq(&p->addr, &ex2->addr));
  320. tt_int_op(p->maskbits,OP_EQ, 8);
  321. tt_int_op(p->prt_min,OP_EQ, 24);
  322. tt_int_op(p->prt_max,OP_EQ, 24);
  323. #if 0
  324. /* Okay, now for the directories. */
  325. {
  326. fingerprint_list = smartlist_new();
  327. crypto_pk_get_fingerprint(pk2, buf, 1);
  328. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  329. crypto_pk_get_fingerprint(pk1, buf, 1);
  330. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  331. }
  332. #endif
  333. dirserv_free_fingerprint_list();
  334. done:
  335. if (r1)
  336. routerinfo_free(r1);
  337. if (r2)
  338. routerinfo_free(r2);
  339. if (rp2)
  340. routerinfo_free(rp2);
  341. tor_free(rsa_cc);
  342. tor_free(buf);
  343. tor_free(pk1_str);
  344. tor_free(pk2_str);
  345. if (pk1) crypto_pk_free(pk1);
  346. if (pk2) crypto_pk_free(pk2);
  347. if (rp1) routerinfo_free(rp1);
  348. tor_free(dir1); /* XXXX And more !*/
  349. tor_free(dir2); /* And more !*/
  350. }
  351. #include "failing_routerdescs.inc"
  352. static void
  353. test_dir_routerinfo_parsing(void *arg)
  354. {
  355. (void) arg;
  356. int again;
  357. routerinfo_t *ri = NULL;
  358. #define CHECK_OK(s) \
  359. do { \
  360. routerinfo_free(ri); \
  361. ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
  362. tt_assert(ri); \
  363. } while (0)
  364. #define CHECK_FAIL(s, againval) \
  365. do { \
  366. routerinfo_free(ri); \
  367. again = 999; \
  368. ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
  369. tt_assert(ri == NULL); \
  370. tt_int_op(again, OP_EQ, (againval)); \
  371. } while (0)
  372. CHECK_OK(EX_RI_MINIMAL);
  373. CHECK_OK(EX_RI_MAXIMAL);
  374. CHECK_OK(EX_RI_MINIMAL_ED);
  375. /* good annotations prepended */
  376. routerinfo_free(ri);
  377. ri = router_parse_entry_from_string(EX_RI_MINIMAL, NULL, 0, 0,
  378. "@purpose bridge\n", NULL);
  379. tt_assert(ri != NULL);
  380. tt_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  381. routerinfo_free(ri);
  382. /* bad annotations prepended. */
  383. ri = router_parse_entry_from_string(EX_RI_MINIMAL,
  384. NULL, 0, 0, "@purpose\n", NULL);
  385. tt_assert(ri == NULL);
  386. /* bad annotations on router. */
  387. ri = router_parse_entry_from_string("@purpose\nrouter x\n", NULL, 0, 1,
  388. NULL, NULL);
  389. tt_assert(ri == NULL);
  390. /* unwanted annotations on router. */
  391. ri = router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL, 0, 0,
  392. NULL, NULL);
  393. tt_assert(ri == NULL);
  394. /* No signature. */
  395. ri = router_parse_entry_from_string("router x\n", NULL, 0, 0,
  396. NULL, NULL);
  397. tt_assert(ri == NULL);
  398. /* Not a router */
  399. routerinfo_free(ri);
  400. ri = router_parse_entry_from_string("hello\n", NULL, 0, 0, NULL, NULL);
  401. tt_assert(ri == NULL);
  402. CHECK_FAIL(EX_RI_BAD_SIG1, 1);
  403. CHECK_FAIL(EX_RI_BAD_SIG2, 1);
  404. CHECK_FAIL(EX_RI_BAD_TOKENS, 0);
  405. CHECK_FAIL(EX_RI_BAD_PUBLISHED, 0);
  406. CHECK_FAIL(EX_RI_NEG_BANDWIDTH, 0);
  407. CHECK_FAIL(EX_RI_BAD_BANDWIDTH, 0);
  408. CHECK_FAIL(EX_RI_BAD_BANDWIDTH2, 0);
  409. CHECK_FAIL(EX_RI_BAD_ONIONKEY1, 0);
  410. CHECK_FAIL(EX_RI_BAD_ONIONKEY2, 0);
  411. CHECK_FAIL(EX_RI_BAD_PORTS, 0);
  412. CHECK_FAIL(EX_RI_BAD_IP, 0);
  413. CHECK_FAIL(EX_RI_BAD_DIRPORT, 0);
  414. CHECK_FAIL(EX_RI_BAD_NAME2, 0);
  415. CHECK_FAIL(EX_RI_BAD_UPTIME, 0);
  416. CHECK_FAIL(EX_RI_BAD_BANDWIDTH3, 0);
  417. CHECK_FAIL(EX_RI_BAD_NTOR_KEY, 0);
  418. CHECK_FAIL(EX_RI_BAD_FINGERPRINT, 0);
  419. CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT, 0);
  420. CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6, 0);
  421. CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY, 0);
  422. CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY, 0);
  423. CHECK_FAIL(EX_RI_BAD_FAMILY, 0);
  424. CHECK_FAIL(EX_RI_ZERO_ORPORT, 0);
  425. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT, 0);
  426. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2, 0);
  427. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN, 0);
  428. CHECK_FAIL(EX_RI_ED_BAD_SIG1, 0);
  429. CHECK_FAIL(EX_RI_ED_BAD_SIG2, 0);
  430. CHECK_FAIL(EX_RI_ED_BAD_SIG3, 0);
  431. CHECK_FAIL(EX_RI_ED_BAD_SIG4, 0);
  432. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1, 0);
  433. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3, 0);
  434. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4, 0);
  435. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5, 0);
  436. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6, 0);
  437. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7, 0);
  438. CHECK_FAIL(EX_RI_ED_MISPLACED1, 0);
  439. CHECK_FAIL(EX_RI_ED_MISPLACED2, 0);
  440. CHECK_FAIL(EX_RI_ED_BAD_CERT1, 0);
  441. CHECK_FAIL(EX_RI_ED_BAD_CERT2, 0);
  442. CHECK_FAIL(EX_RI_ED_BAD_CERT3, 0);
  443. /* This is allowed; we just ignore it. */
  444. CHECK_OK(EX_RI_BAD_EI_DIGEST);
  445. CHECK_OK(EX_RI_BAD_EI_DIGEST2);
  446. #undef CHECK_FAIL
  447. #undef CHECK_OK
  448. done:
  449. routerinfo_free(ri);
  450. }
  451. #include "example_extrainfo.inc"
  452. static void
  453. routerinfo_free_wrapper_(void *arg)
  454. {
  455. routerinfo_free(arg);
  456. }
  457. static void
  458. test_dir_extrainfo_parsing(void *arg)
  459. {
  460. (void) arg;
  461. #define CHECK_OK(s) \
  462. do { \
  463. extrainfo_free(ei); \
  464. ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
  465. tt_assert(ei); \
  466. } while (0)
  467. #define CHECK_FAIL(s, againval) \
  468. do { \
  469. extrainfo_free(ei); \
  470. again = 999; \
  471. ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
  472. tt_assert(ei == NULL); \
  473. tt_int_op(again, OP_EQ, (againval)); \
  474. } while (0)
  475. #define ADD(name) \
  476. do { \
  477. ri = tor_malloc_zero(sizeof(routerinfo_t)); \
  478. crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
  479. tt_assert(! crypto_pk_read_public_key_from_string(pk, \
  480. name##_KEY, strlen(name##_KEY))); \
  481. tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
  482. digestmap_set((digestmap_t*)map, d, ri); \
  483. ri = NULL; \
  484. } while (0)
  485. routerinfo_t *ri = NULL;
  486. char d[20];
  487. struct digest_ri_map_t *map = NULL;
  488. extrainfo_t *ei = NULL;
  489. int again;
  490. CHECK_OK(EX_EI_MINIMAL);
  491. tt_assert(ei->pending_sig);
  492. CHECK_OK(EX_EI_MAXIMAL);
  493. tt_assert(ei->pending_sig);
  494. CHECK_OK(EX_EI_GOOD_ED_EI);
  495. tt_assert(ei->pending_sig);
  496. map = (struct digest_ri_map_t *)digestmap_new();
  497. ADD(EX_EI_MINIMAL);
  498. ADD(EX_EI_MAXIMAL);
  499. ADD(EX_EI_GOOD_ED_EI);
  500. ADD(EX_EI_BAD_FP);
  501. ADD(EX_EI_BAD_NICKNAME);
  502. ADD(EX_EI_BAD_TOKENS);
  503. ADD(EX_EI_BAD_START);
  504. ADD(EX_EI_BAD_PUBLISHED);
  505. ADD(EX_EI_ED_MISSING_SIG);
  506. ADD(EX_EI_ED_MISSING_CERT);
  507. ADD(EX_EI_ED_BAD_CERT1);
  508. ADD(EX_EI_ED_BAD_CERT2);
  509. ADD(EX_EI_ED_BAD_SIG1);
  510. ADD(EX_EI_ED_BAD_SIG2);
  511. ADD(EX_EI_ED_MISPLACED_CERT);
  512. ADD(EX_EI_ED_MISPLACED_SIG);
  513. CHECK_OK(EX_EI_MINIMAL);
  514. tt_assert(!ei->pending_sig);
  515. CHECK_OK(EX_EI_MAXIMAL);
  516. tt_assert(!ei->pending_sig);
  517. CHECK_OK(EX_EI_GOOD_ED_EI);
  518. tt_assert(!ei->pending_sig);
  519. CHECK_FAIL(EX_EI_BAD_SIG1,1);
  520. CHECK_FAIL(EX_EI_BAD_SIG2,1);
  521. CHECK_FAIL(EX_EI_BAD_SIG3,1);
  522. CHECK_FAIL(EX_EI_BAD_FP,0);
  523. CHECK_FAIL(EX_EI_BAD_NICKNAME,0);
  524. CHECK_FAIL(EX_EI_BAD_TOKENS,0);
  525. CHECK_FAIL(EX_EI_BAD_START,0);
  526. CHECK_FAIL(EX_EI_BAD_PUBLISHED,0);
  527. CHECK_FAIL(EX_EI_ED_MISSING_SIG,0);
  528. CHECK_FAIL(EX_EI_ED_MISSING_CERT,0);
  529. CHECK_FAIL(EX_EI_ED_BAD_CERT1,0);
  530. CHECK_FAIL(EX_EI_ED_BAD_CERT2,0);
  531. CHECK_FAIL(EX_EI_ED_BAD_SIG1,0);
  532. CHECK_FAIL(EX_EI_ED_BAD_SIG2,0);
  533. CHECK_FAIL(EX_EI_ED_MISPLACED_CERT,0);
  534. CHECK_FAIL(EX_EI_ED_MISPLACED_SIG,0);
  535. #undef CHECK_OK
  536. #undef CHECK_FAIL
  537. done:
  538. extrainfo_free(ei);
  539. routerinfo_free(ri);
  540. digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
  541. }
  542. static void
  543. test_dir_parse_router_list(void *arg)
  544. {
  545. (void) arg;
  546. smartlist_t *invalid = smartlist_new();
  547. smartlist_t *dest = smartlist_new();
  548. smartlist_t *chunks = smartlist_new();
  549. int dest_has_ri = 1;
  550. char *list = NULL;
  551. const char *cp;
  552. digestmap_t *map = NULL;
  553. char *mem_op_hex_tmp = NULL;
  554. routerinfo_t *ri = NULL;
  555. char d[DIGEST_LEN];
  556. smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL)); // ri 0
  557. smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS)); // bad ri 0
  558. smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL)); // ei 0
  559. smartlist_add(chunks, tor_strdup(EX_EI_BAD_SIG2)); // bad ei --
  560. smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));// bad ei 0
  561. smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG1)); // bad ri --
  562. smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED)); // bad ei 1
  563. smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL)); // ri 1
  564. smartlist_add(chunks, tor_strdup(EX_RI_BAD_FAMILY)); // bad ri 1
  565. smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL)); // ei 1
  566. list = smartlist_join_strings(chunks, "", 0, NULL);
  567. /* First, parse the routers. */
  568. cp = list;
  569. tt_int_op(0,OP_EQ,
  570. router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
  571. 0, 0, NULL, invalid));
  572. tt_int_op(2, OP_EQ, smartlist_len(dest));
  573. tt_ptr_op(cp, OP_EQ, list + strlen(list));
  574. routerinfo_t *r = smartlist_get(dest, 0);
  575. tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
  576. EX_RI_MINIMAL, strlen(EX_RI_MINIMAL));
  577. r = smartlist_get(dest, 1);
  578. tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
  579. EX_RI_MAXIMAL, strlen(EX_RI_MAXIMAL));
  580. tt_int_op(2, OP_EQ, smartlist_len(invalid));
  581. test_memeq_hex(smartlist_get(invalid, 0),
  582. "ab9eeaa95e7d45740185b4e519c76ead756277a9");
  583. test_memeq_hex(smartlist_get(invalid, 1),
  584. "9a651ee03b64325959e8f1b46f2b689b30750b4c");
  585. /* Now tidy up */
  586. SMARTLIST_FOREACH(dest, routerinfo_t *, ri, routerinfo_free(ri));
  587. SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
  588. smartlist_clear(dest);
  589. smartlist_clear(invalid);
  590. /* And check extrainfos. */
  591. dest_has_ri = 0;
  592. map = (digestmap_t*)router_get_routerlist()->identity_map;
  593. ADD(EX_EI_MINIMAL);
  594. ADD(EX_EI_MAXIMAL);
  595. ADD(EX_EI_BAD_NICKNAME);
  596. ADD(EX_EI_BAD_PUBLISHED);
  597. cp = list;
  598. tt_int_op(0,OP_EQ,
  599. router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
  600. 1, 0, NULL, invalid));
  601. tt_int_op(2, OP_EQ, smartlist_len(dest));
  602. extrainfo_t *e = smartlist_get(dest, 0);
  603. tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
  604. EX_EI_MAXIMAL, strlen(EX_EI_MAXIMAL));
  605. e = smartlist_get(dest, 1);
  606. tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
  607. EX_EI_MINIMAL, strlen(EX_EI_MINIMAL));
  608. tt_int_op(2, OP_EQ, smartlist_len(invalid));
  609. test_memeq_hex(smartlist_get(invalid, 0),
  610. "d5df4aa62ee9ffc9543d41150c9864908e0390af");
  611. test_memeq_hex(smartlist_get(invalid, 1),
  612. "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
  613. done:
  614. tor_free(list);
  615. if (dest_has_ri)
  616. SMARTLIST_FOREACH(dest, routerinfo_t *, rt, routerinfo_free(rt));
  617. else
  618. SMARTLIST_FOREACH(dest, extrainfo_t *, ei, extrainfo_free(ei));
  619. smartlist_free(dest);
  620. SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
  621. smartlist_free(invalid);
  622. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  623. smartlist_free(chunks);
  624. routerinfo_free(ri);
  625. if (map) {
  626. digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
  627. router_get_routerlist()->identity_map =
  628. (struct digest_ri_map_t*)digestmap_new();
  629. }
  630. tor_free(mem_op_hex_tmp);
  631. #undef ADD
  632. }
  633. static download_status_t dls_minimal;
  634. static download_status_t dls_maximal;
  635. static download_status_t dls_bad_fingerprint;
  636. static download_status_t dls_bad_sig2;
  637. static download_status_t dls_bad_ports;
  638. static download_status_t dls_bad_tokens;
  639. static int mock_router_get_dl_status_unrecognized = 0;
  640. static int mock_router_get_dl_status_calls = 0;
  641. static download_status_t *
  642. mock_router_get_dl_status(const char *d)
  643. {
  644. ++mock_router_get_dl_status_calls;
  645. char hex[HEX_DIGEST_LEN+1];
  646. base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
  647. if (!strcmp(hex, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
  648. return &dls_minimal;
  649. } else if (!strcmp(hex, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
  650. return &dls_maximal;
  651. } else if (!strcmp(hex, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
  652. return &dls_bad_fingerprint;
  653. } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
  654. return &dls_bad_sig2;
  655. } else if (!strcmp(hex, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
  656. return &dls_bad_ports;
  657. } else if (!strcmp(hex, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
  658. return &dls_bad_tokens;
  659. } else {
  660. ++mock_router_get_dl_status_unrecognized;
  661. return NULL;
  662. }
  663. }
  664. static void
  665. test_dir_load_routers(void *arg)
  666. {
  667. (void) arg;
  668. smartlist_t *chunks = smartlist_new();
  669. smartlist_t *wanted = smartlist_new();
  670. char buf[DIGEST_LEN];
  671. char *mem_op_hex_tmp = NULL;
  672. char *list = NULL;
  673. #define ADD(str) \
  674. do { \
  675. tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
  676. smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
  677. } while (0)
  678. MOCK(router_get_dl_status_by_descriptor_digest, mock_router_get_dl_status);
  679. update_approx_time(1412510400);
  680. smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL));
  681. smartlist_add(chunks, tor_strdup(EX_RI_BAD_FINGERPRINT));
  682. smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG2));
  683. smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL));
  684. smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS));
  685. smartlist_add(chunks, tor_strdup(EX_RI_BAD_TOKENS));
  686. /* not ADDing MINIMIAL */
  687. ADD(EX_RI_MAXIMAL);
  688. ADD(EX_RI_BAD_FINGERPRINT);
  689. ADD(EX_RI_BAD_SIG2);
  690. /* Not ADDing BAD_PORTS */
  691. ADD(EX_RI_BAD_TOKENS);
  692. list = smartlist_join_strings(chunks, "", 0, NULL);
  693. tt_int_op(1, OP_EQ,
  694. router_load_routers_from_string(list, NULL, SAVED_IN_JOURNAL,
  695. wanted, 1, NULL));
  696. /* The "maximal" router was added. */
  697. /* "minimal" was not. */
  698. tt_int_op(smartlist_len(router_get_routerlist()->routers),OP_EQ,1);
  699. routerinfo_t *r = smartlist_get(router_get_routerlist()->routers, 0);
  700. test_memeq_hex(r->cache_info.signed_descriptor_digest,
  701. "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
  702. tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
  703. tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
  704. /* "Bad fingerprint" and "Bad tokens" should have gotten marked
  705. * non-retriable. */
  706. tt_want_int_op(mock_router_get_dl_status_calls, OP_EQ, 2);
  707. tt_want_int_op(mock_router_get_dl_status_unrecognized, OP_EQ, 0);
  708. tt_int_op(dls_bad_fingerprint.n_download_failures, OP_EQ, 255);
  709. tt_int_op(dls_bad_tokens.n_download_failures, OP_EQ, 255);
  710. /* bad_sig2 and bad ports" are retriable -- one since only the signature
  711. * was bad, and one because we didn't ask for it. */
  712. tt_int_op(dls_bad_sig2.n_download_failures, OP_EQ, 0);
  713. tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
  714. /* Wanted still contains "BAD_SIG2" */
  715. tt_int_op(smartlist_len(wanted), OP_EQ, 1);
  716. tt_str_op(smartlist_get(wanted, 0), OP_EQ,
  717. "E0A3753CEFD54128EAB239F294954121DB23D2EF");
  718. #undef ADD
  719. done:
  720. tor_free(mem_op_hex_tmp);
  721. UNMOCK(router_get_dl_status_by_descriptor_digest);
  722. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  723. smartlist_free(chunks);
  724. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  725. smartlist_free(wanted);
  726. tor_free(list);
  727. }
  728. static int mock_get_by_ei_dd_calls = 0;
  729. static int mock_get_by_ei_dd_unrecognized = 0;
  730. static signed_descriptor_t sd_ei_minimal;
  731. static signed_descriptor_t sd_ei_bad_nickname;
  732. static signed_descriptor_t sd_ei_maximal;
  733. static signed_descriptor_t sd_ei_bad_tokens;
  734. static signed_descriptor_t sd_ei_bad_sig2;
  735. static signed_descriptor_t *
  736. mock_get_by_ei_desc_digest(const char *d)
  737. {
  738. ++mock_get_by_ei_dd_calls;
  739. char hex[HEX_DIGEST_LEN+1];
  740. base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
  741. if (!strcmp(hex, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
  742. return &sd_ei_minimal;
  743. } else if (!strcmp(hex, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
  744. return &sd_ei_maximal;
  745. } else if (!strcmp(hex, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
  746. return &sd_ei_bad_nickname;
  747. } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
  748. return &sd_ei_bad_sig2;
  749. } else if (!strcmp(hex, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
  750. return &sd_ei_bad_tokens;
  751. } else {
  752. ++mock_get_by_ei_dd_unrecognized;
  753. return NULL;
  754. }
  755. }
  756. static smartlist_t *mock_ei_insert_list = NULL;
  757. static was_router_added_t
  758. mock_ei_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
  759. {
  760. (void) rl;
  761. (void) warn_if_incompatible;
  762. smartlist_add(mock_ei_insert_list, ei);
  763. return ROUTER_ADDED_SUCCESSFULLY;
  764. }
  765. static void
  766. test_dir_load_extrainfo(void *arg)
  767. {
  768. (void) arg;
  769. smartlist_t *chunks = smartlist_new();
  770. smartlist_t *wanted = smartlist_new();
  771. char buf[DIGEST_LEN];
  772. char *mem_op_hex_tmp = NULL;
  773. char *list = NULL;
  774. #define ADD(str) \
  775. do { \
  776. tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
  777. smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
  778. } while (0)
  779. mock_ei_insert_list = smartlist_new();
  780. MOCK(router_get_by_extrainfo_digest, mock_get_by_ei_desc_digest);
  781. MOCK(extrainfo_insert, mock_ei_insert);
  782. smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL));
  783. smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));
  784. smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL));
  785. smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED));
  786. smartlist_add(chunks, tor_strdup(EX_EI_BAD_TOKENS));
  787. /* not ADDing MINIMIAL */
  788. ADD(EX_EI_MAXIMAL);
  789. ADD(EX_EI_BAD_NICKNAME);
  790. /* Not ADDing BAD_PUBLISHED */
  791. ADD(EX_EI_BAD_TOKENS);
  792. ADD(EX_EI_BAD_SIG2);
  793. list = smartlist_join_strings(chunks, "", 0, NULL);
  794. router_load_extrainfo_from_string(list, NULL, SAVED_IN_JOURNAL, wanted, 1);
  795. /* The "maximal" router was added. */
  796. /* "minimal" was also added, even though we didn't ask for it, since
  797. * that's what we do with extrainfos. */
  798. tt_int_op(smartlist_len(mock_ei_insert_list),OP_EQ,2);
  799. extrainfo_t *e = smartlist_get(mock_ei_insert_list, 0);
  800. test_memeq_hex(e->cache_info.signed_descriptor_digest,
  801. "11E0EDF526950739F7769810FCACAB8C882FAEEE");
  802. e = smartlist_get(mock_ei_insert_list, 1);
  803. test_memeq_hex(e->cache_info.signed_descriptor_digest,
  804. "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
  805. tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
  806. tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
  807. /* "Bad nickname" and "Bad tokens" should have gotten marked
  808. * non-retriable. */
  809. tt_want_int_op(mock_get_by_ei_dd_calls, OP_EQ, 2);
  810. tt_want_int_op(mock_get_by_ei_dd_unrecognized, OP_EQ, 0);
  811. tt_int_op(sd_ei_bad_nickname.ei_dl_status.n_download_failures, OP_EQ, 255);
  812. tt_int_op(sd_ei_bad_tokens.ei_dl_status.n_download_failures, OP_EQ, 255);
  813. /* bad_ports is retriable -- because we didn't ask for it. */
  814. tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
  815. /* Wanted still contains "BAD_SIG2" */
  816. tt_int_op(smartlist_len(wanted), OP_EQ, 1);
  817. tt_str_op(smartlist_get(wanted, 0), OP_EQ,
  818. "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
  819. #undef ADD
  820. done:
  821. tor_free(mem_op_hex_tmp);
  822. UNMOCK(router_get_by_extrainfo_digest);
  823. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  824. smartlist_free(chunks);
  825. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  826. smartlist_free(wanted);
  827. tor_free(list);
  828. }
  829. static void
  830. test_dir_versions(void *arg)
  831. {
  832. tor_version_t ver1;
  833. /* Try out version parsing functionality */
  834. (void)arg;
  835. tt_int_op(0,OP_EQ, tor_version_parse("0.3.4pre2-cvs", &ver1));
  836. tt_int_op(0,OP_EQ, ver1.major);
  837. tt_int_op(3,OP_EQ, ver1.minor);
  838. tt_int_op(4,OP_EQ, ver1.micro);
  839. tt_int_op(VER_PRE,OP_EQ, ver1.status);
  840. tt_int_op(2,OP_EQ, ver1.patchlevel);
  841. tt_int_op(0,OP_EQ, tor_version_parse("0.3.4rc1", &ver1));
  842. tt_int_op(0,OP_EQ, ver1.major);
  843. tt_int_op(3,OP_EQ, ver1.minor);
  844. tt_int_op(4,OP_EQ, ver1.micro);
  845. tt_int_op(VER_RC,OP_EQ, ver1.status);
  846. tt_int_op(1,OP_EQ, ver1.patchlevel);
  847. tt_int_op(0,OP_EQ, tor_version_parse("1.3.4", &ver1));
  848. tt_int_op(1,OP_EQ, ver1.major);
  849. tt_int_op(3,OP_EQ, ver1.minor);
  850. tt_int_op(4,OP_EQ, ver1.micro);
  851. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  852. tt_int_op(0,OP_EQ, ver1.patchlevel);
  853. tt_int_op(0,OP_EQ, tor_version_parse("1.3.4.999", &ver1));
  854. tt_int_op(1,OP_EQ, ver1.major);
  855. tt_int_op(3,OP_EQ, ver1.minor);
  856. tt_int_op(4,OP_EQ, ver1.micro);
  857. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  858. tt_int_op(999,OP_EQ, ver1.patchlevel);
  859. tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4-alpha", &ver1));
  860. tt_int_op(0,OP_EQ, ver1.major);
  861. tt_int_op(1,OP_EQ, ver1.minor);
  862. tt_int_op(2,OP_EQ, ver1.micro);
  863. tt_int_op(4,OP_EQ, ver1.patchlevel);
  864. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  865. tt_str_op("alpha",OP_EQ, ver1.status_tag);
  866. tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4", &ver1));
  867. tt_int_op(0,OP_EQ, ver1.major);
  868. tt_int_op(1,OP_EQ, ver1.minor);
  869. tt_int_op(2,OP_EQ, ver1.micro);
  870. tt_int_op(4,OP_EQ, ver1.patchlevel);
  871. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  872. tt_str_op("",OP_EQ, ver1.status_tag);
  873. tt_int_op(0, OP_EQ, tor_version_parse("10.1", &ver1));
  874. tt_int_op(10, OP_EQ, ver1.major);
  875. tt_int_op(1, OP_EQ, ver1.minor);
  876. tt_int_op(0, OP_EQ, ver1.micro);
  877. tt_int_op(0, OP_EQ, ver1.patchlevel);
  878. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  879. tt_str_op("", OP_EQ, ver1.status_tag);
  880. tt_int_op(0, OP_EQ, tor_version_parse("5.99.999", &ver1));
  881. tt_int_op(5, OP_EQ, ver1.major);
  882. tt_int_op(99, OP_EQ, ver1.minor);
  883. tt_int_op(999, OP_EQ, ver1.micro);
  884. tt_int_op(0, OP_EQ, ver1.patchlevel);
  885. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  886. tt_str_op("", OP_EQ, ver1.status_tag);
  887. tt_int_op(0, OP_EQ, tor_version_parse("10.1-alpha", &ver1));
  888. tt_int_op(10, OP_EQ, ver1.major);
  889. tt_int_op(1, OP_EQ, ver1.minor);
  890. tt_int_op(0, OP_EQ, ver1.micro);
  891. tt_int_op(0, OP_EQ, ver1.patchlevel);
  892. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  893. tt_str_op("alpha", OP_EQ, ver1.status_tag);
  894. tt_int_op(0, OP_EQ, tor_version_parse("2.1.700-alpha", &ver1));
  895. tt_int_op(2, OP_EQ, ver1.major);
  896. tt_int_op(1, OP_EQ, ver1.minor);
  897. tt_int_op(700, OP_EQ, ver1.micro);
  898. tt_int_op(0, OP_EQ, ver1.patchlevel);
  899. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  900. tt_str_op("alpha", OP_EQ, ver1.status_tag);
  901. tt_int_op(0, OP_EQ, tor_version_parse("1.6.8-alpha-dev", &ver1));
  902. tt_int_op(1, OP_EQ, ver1.major);
  903. tt_int_op(6, OP_EQ, ver1.minor);
  904. tt_int_op(8, OP_EQ, ver1.micro);
  905. tt_int_op(0, OP_EQ, ver1.patchlevel);
  906. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  907. tt_str_op("alpha-dev", OP_EQ, ver1.status_tag);
  908. #define tt_versionstatus_op(vs1, op, vs2) \
  909. tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
  910. (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
  911. #define test_v_i_o(val, ver, lst) \
  912. tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
  913. /* make sure tor_version_is_obsolete() works */
  914. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  915. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  916. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  917. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  918. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  919. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  920. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  921. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  922. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  923. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  924. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  925. /* Not on list, but newer than any in same series. */
  926. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  927. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  928. /* Series newer than any on list. */
  929. 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");
  930. /* Series older than any on list. */
  931. 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");
  932. /* Not on list, not newer than any on same series. */
  933. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  934. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  935. /* On list, not newer than any on same series. */
  936. test_v_i_o(VS_UNRECOMMENDED,
  937. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  938. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  939. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  940. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  941. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  942. "0.0.8rc2"));
  943. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  944. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  945. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  946. /* Now try svn revisions. */
  947. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  948. "Tor 0.2.1.0-dev (r99)"));
  949. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  950. "Tor 0.2.1.0-dev (r100) on Banana Jr",
  951. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  952. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  953. "Tor 0.2.1.0-dev on Colossus"));
  954. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  955. "Tor 0.2.1.0-dev (r100)"));
  956. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  957. "Tor 0.2.1.0-dev (r100) on AM"));
  958. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev",
  959. "Tor 0.2.1.0-dev (r99)"));
  960. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.1",
  961. "Tor 0.2.1.0-dev (r99)"));
  962. /* Now try git revisions */
  963. tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
  964. tt_int_op(0,OP_EQ, ver1.major);
  965. tt_int_op(5,OP_EQ, ver1.minor);
  966. tt_int_op(6,OP_EQ, ver1.micro);
  967. tt_int_op(7,OP_EQ, ver1.patchlevel);
  968. tt_int_op(3,OP_EQ, ver1.git_tag_len);
  969. tt_mem_op(ver1.git_tag,OP_EQ, "\xff\x00\xff", 3);
  970. tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
  971. tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
  972. tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
  973. done:
  974. ;
  975. }
  976. /** Run unit tests for directory fp_pair functions. */
  977. static void
  978. test_dir_fp_pairs(void *arg)
  979. {
  980. smartlist_t *sl = smartlist_new();
  981. fp_pair_t *pair;
  982. (void)arg;
  983. dir_split_resource_into_fingerprint_pairs(
  984. /* Two pairs, out of order, with one duplicate. */
  985. "73656372657420646174612E0000000000FFFFFF-"
  986. "557365204145532d32353620696e73746561642e+"
  987. "73656372657420646174612E0000000000FFFFFF-"
  988. "557365204145532d32353620696e73746561642e+"
  989. "48657861646563696d616c2069736e277420736f-"
  990. "676f6f6420666f7220686964696e6720796f7572.z", sl);
  991. tt_int_op(smartlist_len(sl),OP_EQ, 2);
  992. pair = smartlist_get(sl, 0);
  993. tt_mem_op(pair->first,OP_EQ, "Hexadecimal isn't so", DIGEST_LEN);
  994. tt_mem_op(pair->second,OP_EQ, "good for hiding your", DIGEST_LEN);
  995. pair = smartlist_get(sl, 1);
  996. tt_mem_op(pair->first,OP_EQ, "secret data.\0\0\0\0\0\xff\xff\xff",
  997. DIGEST_LEN);
  998. tt_mem_op(pair->second,OP_EQ, "Use AES-256 instead.", DIGEST_LEN);
  999. done:
  1000. SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
  1001. smartlist_free(sl);
  1002. }
  1003. static void
  1004. test_dir_split_fps(void *testdata)
  1005. {
  1006. smartlist_t *sl = smartlist_new();
  1007. char *mem_op_hex_tmp = NULL;
  1008. (void)testdata;
  1009. /* Some example hex fingerprints and their base64 equivalents */
  1010. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  1011. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  1012. #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
  1013. #define HEX256_1 \
  1014. "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
  1015. #define HEX256_2 \
  1016. "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
  1017. #define HEX256_3 \
  1018. "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
  1019. #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
  1020. #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
  1021. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  1022. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  1023. /* no flags set */
  1024. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  1025. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1026. tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
  1027. tt_str_op(smartlist_get(sl, 1), OP_EQ, "C");
  1028. tt_str_op(smartlist_get(sl, 2), OP_EQ, "B");
  1029. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1030. smartlist_clear(sl);
  1031. /* uniq strings. */
  1032. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  1033. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1034. tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
  1035. tt_str_op(smartlist_get(sl, 1), OP_EQ, "B");
  1036. tt_str_op(smartlist_get(sl, 2), OP_EQ, "C");
  1037. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1038. smartlist_clear(sl);
  1039. /* Decode hex. */
  1040. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  1041. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1042. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1043. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1044. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1045. smartlist_clear(sl);
  1046. /* decode hex and drop weirdness. */
  1047. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  1048. sl, NULL, DSR_HEX);
  1049. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1050. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1051. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1052. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1053. smartlist_clear(sl);
  1054. /* Decode long hex */
  1055. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  1056. sl, NULL, DSR_HEX|DSR_DIGEST256);
  1057. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1058. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1059. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1060. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_3);
  1061. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1062. smartlist_clear(sl);
  1063. /* Decode hex and sort. */
  1064. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  1065. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  1066. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1067. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX3);
  1068. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1069. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX1);
  1070. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1071. smartlist_clear(sl);
  1072. /* Decode long hex and sort */
  1073. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  1074. "+"HEX256_1,
  1075. sl, NULL,
  1076. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  1077. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1078. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_3);
  1079. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1080. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_1);
  1081. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1082. smartlist_clear(sl);
  1083. /* Decode base64 */
  1084. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  1085. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1086. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1087. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1088. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1089. smartlist_clear(sl);
  1090. /* Decode long base64 */
  1091. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  1092. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  1093. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1094. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1095. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1096. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1097. smartlist_clear(sl);
  1098. dir_split_resource_into_fingerprints(B64_256_1,
  1099. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  1100. tt_int_op(smartlist_len(sl), OP_EQ, 1);
  1101. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1102. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1103. smartlist_clear(sl);
  1104. done:
  1105. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1106. smartlist_free(sl);
  1107. tor_free(mem_op_hex_tmp);
  1108. }
  1109. static void
  1110. test_dir_measured_bw_kb(void *arg)
  1111. {
  1112. measured_bw_line_t mbwl;
  1113. int i;
  1114. const char *lines_pass[] = {
  1115. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  1116. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  1117. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  1118. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  1119. "bw=1024 junk=007\n",
  1120. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  1121. "bw=1024 junk=007\n",
  1122. "end"
  1123. };
  1124. const char *lines_fail[] = {
  1125. /* Test possible python stupidity on input */
  1126. "node_id=None bw=1024\n",
  1127. "node_id=$None bw=1024\n",
  1128. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  1129. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  1130. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  1131. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  1132. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  1133. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  1134. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  1135. /* Test incomplete writes due to race conditions, partial copies, etc */
  1136. "node_i",
  1137. "node_i\n",
  1138. "node_id=",
  1139. "node_id=\n",
  1140. "node_id=$557365204145532d32353620696e73746561642e bw=",
  1141. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  1142. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  1143. "node_id=$557365204145532d32353620696e7374",
  1144. "node_id=$557365204145532d32353620696e7374\n",
  1145. "",
  1146. "\n",
  1147. " \n ",
  1148. " \n\n",
  1149. /* Test assorted noise */
  1150. " node_id= ",
  1151. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  1152. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  1153. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  1154. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  1155. "end"
  1156. };
  1157. (void)arg;
  1158. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  1159. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  1160. tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
  1161. }
  1162. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  1163. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  1164. tt_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
  1165. tt_assert(mbwl.bw_kb == 1024);
  1166. tt_assert(strcmp(mbwl.node_hex,
  1167. "557365204145532d32353620696e73746561642e") == 0);
  1168. }
  1169. done:
  1170. return;
  1171. }
  1172. #define MBWC_INIT_TIME 1000
  1173. /** Do the measured bandwidth cache unit test */
  1174. static void
  1175. test_dir_measured_bw_kb_cache(void *arg)
  1176. {
  1177. /* Initial fake time_t for testing */
  1178. time_t curr = MBWC_INIT_TIME;
  1179. /* Some measured_bw_line_ts */
  1180. measured_bw_line_t mbwl[3];
  1181. /* For receiving output on cache queries */
  1182. long bw;
  1183. time_t as_of;
  1184. /* First, clear the cache and assert that it's empty */
  1185. (void)arg;
  1186. dirserv_clear_measured_bw_cache();
  1187. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1188. /*
  1189. * Set up test mbwls; none of the dirserv_cache_*() functions care about
  1190. * the node_hex field.
  1191. */
  1192. memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
  1193. mbwl[0].bw_kb = 20;
  1194. memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
  1195. mbwl[1].bw_kb = 40;
  1196. memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
  1197. mbwl[2].bw_kb = 80;
  1198. /* Try caching something */
  1199. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  1200. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1201. /* Okay, let's see if we can retrieve it */
  1202. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, &as_of));
  1203. tt_int_op(bw,OP_EQ, 20);
  1204. tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
  1205. /* Try retrieving it without some outputs */
  1206. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL, NULL));
  1207. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, NULL));
  1208. tt_int_op(bw,OP_EQ, 20);
  1209. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL,&as_of));
  1210. tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
  1211. /* Now expire it */
  1212. curr += MAX_MEASUREMENT_AGE + 1;
  1213. dirserv_expire_measured_bw_cache(curr);
  1214. /* Check that the cache is empty */
  1215. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1216. /* Check that we can't retrieve it */
  1217. tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl[0].node_id, NULL,NULL));
  1218. /* Try caching a few things now */
  1219. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  1220. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1221. curr += MAX_MEASUREMENT_AGE / 4;
  1222. dirserv_cache_measured_bw(&(mbwl[1]), curr);
  1223. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
  1224. curr += MAX_MEASUREMENT_AGE / 4;
  1225. dirserv_cache_measured_bw(&(mbwl[2]), curr);
  1226. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
  1227. curr += MAX_MEASUREMENT_AGE / 4 + 1;
  1228. /* Do an expire that's too soon to get any of them */
  1229. dirserv_expire_measured_bw_cache(curr);
  1230. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
  1231. /* Push the oldest one off the cliff */
  1232. curr += MAX_MEASUREMENT_AGE / 4;
  1233. dirserv_expire_measured_bw_cache(curr);
  1234. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
  1235. /* And another... */
  1236. curr += MAX_MEASUREMENT_AGE / 4;
  1237. dirserv_expire_measured_bw_cache(curr);
  1238. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1239. /* This should empty it out again */
  1240. curr += MAX_MEASUREMENT_AGE / 4;
  1241. dirserv_expire_measured_bw_cache(curr);
  1242. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1243. done:
  1244. return;
  1245. }
  1246. static void
  1247. test_dir_param_voting(void *arg)
  1248. {
  1249. networkstatus_t vote1, vote2, vote3, vote4;
  1250. smartlist_t *votes = smartlist_new();
  1251. char *res = NULL;
  1252. /* dirvote_compute_params only looks at the net_params field of the votes,
  1253. so that's all we need to set.
  1254. */
  1255. (void)arg;
  1256. memset(&vote1, 0, sizeof(vote1));
  1257. memset(&vote2, 0, sizeof(vote2));
  1258. memset(&vote3, 0, sizeof(vote3));
  1259. memset(&vote4, 0, sizeof(vote4));
  1260. vote1.net_params = smartlist_new();
  1261. vote2.net_params = smartlist_new();
  1262. vote3.net_params = smartlist_new();
  1263. vote4.net_params = smartlist_new();
  1264. smartlist_split_string(vote1.net_params,
  1265. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  1266. smartlist_split_string(vote2.net_params,
  1267. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  1268. smartlist_split_string(vote3.net_params,
  1269. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  1270. smartlist_split_string(vote4.net_params,
  1271. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  1272. tt_int_op(100,OP_EQ, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  1273. tt_int_op(222,OP_EQ, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  1274. tt_int_op(80,OP_EQ, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  1275. tt_int_op(-8,OP_EQ, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  1276. tt_int_op(0,OP_EQ, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  1277. smartlist_add(votes, &vote1);
  1278. /* Do the first tests without adding all the other votes, for
  1279. * networks without many dirauths. */
  1280. res = dirvote_compute_params(votes, 12, 2);
  1281. tt_str_op(res,OP_EQ, "");
  1282. tor_free(res);
  1283. res = dirvote_compute_params(votes, 12, 1);
  1284. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-99");
  1285. tor_free(res);
  1286. smartlist_add(votes, &vote2);
  1287. res = dirvote_compute_params(votes, 12, 2);
  1288. tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
  1289. tor_free(res);
  1290. res = dirvote_compute_params(votes, 12, 3);
  1291. tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
  1292. tor_free(res);
  1293. res = dirvote_compute_params(votes, 12, 6);
  1294. tt_str_op(res,OP_EQ, "");
  1295. tor_free(res);
  1296. smartlist_add(votes, &vote3);
  1297. res = dirvote_compute_params(votes, 12, 3);
  1298. tt_str_op(res,OP_EQ, "ab=27 abcd=20 cw=50 x-yz=-9");
  1299. tor_free(res);
  1300. res = dirvote_compute_params(votes, 12, 5);
  1301. tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
  1302. tor_free(res);
  1303. res = dirvote_compute_params(votes, 12, 9);
  1304. tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
  1305. tor_free(res);
  1306. smartlist_add(votes, &vote4);
  1307. res = dirvote_compute_params(votes, 12, 4);
  1308. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1309. tor_free(res);
  1310. res = dirvote_compute_params(votes, 12, 5);
  1311. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1312. tor_free(res);
  1313. /* Test that the special-cased "at least three dirauths voted for
  1314. * this param" logic works as expected. */
  1315. res = dirvote_compute_params(votes, 12, 6);
  1316. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1317. tor_free(res);
  1318. res = dirvote_compute_params(votes, 12, 10);
  1319. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1320. tor_free(res);
  1321. done:
  1322. tor_free(res);
  1323. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  1324. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  1325. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  1326. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  1327. smartlist_free(vote1.net_params);
  1328. smartlist_free(vote2.net_params);
  1329. smartlist_free(vote3.net_params);
  1330. smartlist_free(vote4.net_params);
  1331. smartlist_free(votes);
  1332. return;
  1333. }
  1334. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  1335. * same voting authority, and that they do in fact have all the same
  1336. * information. */
  1337. static void
  1338. test_same_voter(networkstatus_voter_info_t *v1,
  1339. networkstatus_voter_info_t *v2)
  1340. {
  1341. tt_str_op(v1->nickname,OP_EQ, v2->nickname);
  1342. tt_mem_op(v1->identity_digest,OP_EQ, v2->identity_digest, DIGEST_LEN);
  1343. tt_str_op(v1->address,OP_EQ, v2->address);
  1344. tt_int_op(v1->addr,OP_EQ, v2->addr);
  1345. tt_int_op(v1->dir_port,OP_EQ, v2->dir_port);
  1346. tt_int_op(v1->or_port,OP_EQ, v2->or_port);
  1347. tt_str_op(v1->contact,OP_EQ, v2->contact);
  1348. tt_mem_op(v1->vote_digest,OP_EQ, v2->vote_digest, DIGEST_LEN);
  1349. done:
  1350. ;
  1351. }
  1352. /** Helper: get a detached signatures document for one or two
  1353. * consensuses. */
  1354. static char *
  1355. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  1356. {
  1357. char *r;
  1358. smartlist_t *sl;
  1359. tor_assert(ns && ns->flavor == FLAV_NS);
  1360. sl = smartlist_new();
  1361. smartlist_add(sl,ns);
  1362. if (ns2)
  1363. smartlist_add(sl,ns2);
  1364. r = networkstatus_get_detached_signatures(sl);
  1365. smartlist_free(sl);
  1366. return r;
  1367. }
  1368. /** Apply tweaks to the vote list for each voter */
  1369. static int
  1370. vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
  1371. {
  1372. vote_routerstatus_t *vrs;
  1373. const char *msg = NULL;
  1374. tt_assert(v);
  1375. (void)now;
  1376. if (voter == 1) {
  1377. measured_bw_line_t mbw;
  1378. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  1379. mbw.bw_kb = 1024;
  1380. tt_assert(measured_bw_line_apply(&mbw,
  1381. v->routerstatus_list) == 1);
  1382. } else if (voter == 2 || voter == 3) {
  1383. /* Monkey around with the list a bit */
  1384. vrs = smartlist_get(v->routerstatus_list, 2);
  1385. smartlist_del_keeporder(v->routerstatus_list, 2);
  1386. vote_routerstatus_free(vrs);
  1387. vrs = smartlist_get(v->routerstatus_list, 0);
  1388. vrs->status.is_fast = 1;
  1389. if (voter == 3) {
  1390. vrs = smartlist_get(v->routerstatus_list, 0);
  1391. smartlist_del_keeporder(v->routerstatus_list, 0);
  1392. vote_routerstatus_free(vrs);
  1393. vrs = smartlist_get(v->routerstatus_list, 0);
  1394. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  1395. tt_assert(router_add_to_routerlist(
  1396. dir_common_generate_ri_from_rs(vrs), &msg,0,0) >= 0);
  1397. }
  1398. }
  1399. done:
  1400. return 0;
  1401. }
  1402. /**
  1403. * Test a parsed vote_routerstatus_t for v3_networkstatus test
  1404. */
  1405. static void
  1406. test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
  1407. {
  1408. routerstatus_t *rs;
  1409. tor_addr_t addr_ipv6;
  1410. tt_assert(vrs);
  1411. rs = &(vrs->status);
  1412. tt_assert(rs);
  1413. /* Split out by digests to test */
  1414. if (tor_memeq(rs->identity_digest,
  1415. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1416. "\x3\x3\x3\x3",
  1417. DIGEST_LEN) &&
  1418. (voter == 1)) {
  1419. /* Check the first routerstatus. */
  1420. tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
  1421. tt_int_op(rs->published_on,OP_EQ, now-1500);
  1422. tt_str_op(rs->nickname,OP_EQ, "router2");
  1423. tt_mem_op(rs->identity_digest,OP_EQ,
  1424. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1425. "\x3\x3\x3\x3",
  1426. DIGEST_LEN);
  1427. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1428. tt_int_op(rs->addr,OP_EQ, 0x99008801);
  1429. tt_int_op(rs->or_port,OP_EQ, 443);
  1430. tt_int_op(rs->dir_port,OP_EQ, 8000);
  1431. /* no flags except "running" (16) and "v2dir" (64) */
  1432. tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(80));
  1433. } else if (tor_memeq(rs->identity_digest,
  1434. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1435. "\x5\x5\x5\x5",
  1436. DIGEST_LEN) &&
  1437. (voter == 1 || voter == 2)) {
  1438. tt_mem_op(rs->identity_digest,OP_EQ,
  1439. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1440. "\x5\x5\x5\x5",
  1441. DIGEST_LEN);
  1442. if (voter == 1) {
  1443. /* Check the second routerstatus. */
  1444. tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
  1445. tt_int_op(rs->published_on,OP_EQ, now-1000);
  1446. tt_str_op(rs->nickname,OP_EQ, "router1");
  1447. }
  1448. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1449. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  1450. tt_int_op(rs->or_port,OP_EQ, 443);
  1451. tt_int_op(rs->dir_port,OP_EQ, 0);
  1452. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1453. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1454. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  1455. if (voter == 1) {
  1456. /* all except "authority" (1) */
  1457. tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(254));
  1458. } else {
  1459. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
  1460. tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(974));
  1461. }
  1462. } else if (tor_memeq(rs->identity_digest,
  1463. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1464. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1465. DIGEST_LEN) &&
  1466. (voter == 1 || voter == 2)) {
  1467. /* Check the measured bandwidth bits */
  1468. tt_assert(vrs->has_measured_bw &&
  1469. vrs->measured_bw_kb == 1024);
  1470. } else {
  1471. /*
  1472. * Didn't expect this, but the old unit test only checked some of them,
  1473. * so don't assert.
  1474. */
  1475. /* tt_assert(0); */
  1476. }
  1477. done:
  1478. return;
  1479. }
  1480. /**
  1481. * Test a consensus for v3_networkstatus_test
  1482. */
  1483. static void
  1484. test_consensus_for_v3ns(networkstatus_t *con, time_t now)
  1485. {
  1486. (void)now;
  1487. tt_assert(con);
  1488. tt_assert(!con->cert);
  1489. tt_int_op(2,OP_EQ, smartlist_len(con->routerstatus_list));
  1490. /* There should be two listed routers: one with identity 3, one with
  1491. * identity 5. */
  1492. done:
  1493. return;
  1494. }
  1495. /**
  1496. * Test a router list entry for v3_networkstatus test
  1497. */
  1498. static void
  1499. test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
  1500. {
  1501. tor_addr_t addr_ipv6;
  1502. tt_assert(rs);
  1503. /* There should be two listed routers: one with identity 3, one with
  1504. * identity 5. */
  1505. /* This one showed up in 2 digests. */
  1506. if (tor_memeq(rs->identity_digest,
  1507. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1508. "\x3\x3",
  1509. DIGEST_LEN)) {
  1510. tt_mem_op(rs->identity_digest,OP_EQ,
  1511. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1512. DIGEST_LEN);
  1513. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1514. tt_assert(!rs->is_authority);
  1515. tt_assert(!rs->is_exit);
  1516. tt_assert(!rs->is_fast);
  1517. tt_assert(!rs->is_possible_guard);
  1518. tt_assert(!rs->is_stable);
  1519. /* (If it wasn't running it wouldn't be here) */
  1520. tt_assert(rs->is_flagged_running);
  1521. tt_assert(!rs->is_valid);
  1522. tt_assert(!rs->is_named);
  1523. tt_assert(rs->is_v2_dir);
  1524. /* XXXX check version */
  1525. } else if (tor_memeq(rs->identity_digest,
  1526. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1527. "\x5\x5\x5\x5",
  1528. DIGEST_LEN)) {
  1529. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  1530. tt_mem_op(rs->identity_digest,OP_EQ,
  1531. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1532. DIGEST_LEN);
  1533. tt_str_op(rs->nickname,OP_EQ, "router1");
  1534. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1535. tt_int_op(rs->published_on,OP_EQ, now-1000);
  1536. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  1537. tt_int_op(rs->or_port,OP_EQ, 443);
  1538. tt_int_op(rs->dir_port,OP_EQ, 0);
  1539. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1540. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1541. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  1542. tt_assert(!rs->is_authority);
  1543. tt_assert(rs->is_exit);
  1544. tt_assert(rs->is_fast);
  1545. tt_assert(rs->is_possible_guard);
  1546. tt_assert(rs->is_stable);
  1547. tt_assert(rs->is_flagged_running);
  1548. tt_assert(rs->is_valid);
  1549. tt_assert(rs->is_v2_dir);
  1550. tt_assert(!rs->is_named);
  1551. /* XXXX check version */
  1552. } else {
  1553. /* Weren't expecting this... */
  1554. tt_assert(0);
  1555. }
  1556. done:
  1557. return;
  1558. }
  1559. /** Run a unit tests for generating and parsing networkstatuses, with
  1560. * the supply test fns. */
  1561. static void
  1562. test_a_networkstatus(
  1563. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  1564. int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  1565. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  1566. void (*consensus_test)(networkstatus_t *con, time_t now),
  1567. void (*rs_test)(routerstatus_t *rs, time_t now))
  1568. {
  1569. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  1570. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  1571. crypto_pk_t *sign_skey_leg1=NULL;
  1572. /*
  1573. * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
  1574. * returns non-zero, it changed net_params and we should skip the tests for
  1575. * that later as they will fail.
  1576. */
  1577. int params_tweaked = 0;
  1578. time_t now = time(NULL);
  1579. networkstatus_voter_info_t *voter;
  1580. document_signature_t *sig;
  1581. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  1582. *con_md=NULL;
  1583. vote_routerstatus_t *vrs;
  1584. routerstatus_t *rs;
  1585. int idx, n_rs, n_vrs;
  1586. char *consensus_text=NULL, *cp=NULL;
  1587. smartlist_t *votes = smartlist_new();
  1588. /* For generating the two other consensuses. */
  1589. char *detached_text1=NULL, *detached_text2=NULL;
  1590. char *consensus_text2=NULL, *consensus_text3=NULL;
  1591. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  1592. char *consensus_text_md=NULL;
  1593. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  1594. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  1595. tt_assert(vrs_gen);
  1596. tt_assert(rs_test);
  1597. tt_assert(vrs_test);
  1598. tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
  1599. &sign_skey_1, &sign_skey_2,
  1600. &sign_skey_3));
  1601. sign_skey_leg1 = pk_generate(4);
  1602. tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
  1603. &v1, &n_vrs, now, 1));
  1604. tt_assert(v1);
  1605. /* Make sure the parsed thing was right. */
  1606. tt_int_op(v1->type,OP_EQ, NS_TYPE_VOTE);
  1607. tt_int_op(v1->published,OP_EQ, vote->published);
  1608. tt_int_op(v1->valid_after,OP_EQ, vote->valid_after);
  1609. tt_int_op(v1->fresh_until,OP_EQ, vote->fresh_until);
  1610. tt_int_op(v1->valid_until,OP_EQ, vote->valid_until);
  1611. tt_int_op(v1->vote_seconds,OP_EQ, vote->vote_seconds);
  1612. tt_int_op(v1->dist_seconds,OP_EQ, vote->dist_seconds);
  1613. tt_str_op(v1->client_versions,OP_EQ, vote->client_versions);
  1614. tt_str_op(v1->server_versions,OP_EQ, vote->server_versions);
  1615. tt_assert(v1->voters && smartlist_len(v1->voters));
  1616. voter = smartlist_get(v1->voters, 0);
  1617. tt_str_op(voter->nickname,OP_EQ, "Voter1");
  1618. tt_str_op(voter->address,OP_EQ, "1.2.3.4");
  1619. tt_int_op(voter->addr,OP_EQ, 0x01020304);
  1620. tt_int_op(voter->dir_port,OP_EQ, 80);
  1621. tt_int_op(voter->or_port,OP_EQ, 9000);
  1622. tt_str_op(voter->contact,OP_EQ, "voter@example.com");
  1623. tt_assert(v1->cert);
  1624. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  1625. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  1626. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  1627. tor_free(cp);
  1628. tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
  1629. tor_free(vote);
  1630. if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
  1631. /* Check the routerstatuses. */
  1632. for (idx = 0; idx < n_vrs; ++idx) {
  1633. vrs = smartlist_get(v1->routerstatus_list, idx);
  1634. tt_assert(vrs);
  1635. vrs_test(vrs, 1, now);
  1636. }
  1637. /* Generate second vote. It disagrees on some of the times,
  1638. * and doesn't list versions, and knows some crazy flags.
  1639. * Generate and parse v2. */
  1640. tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
  1641. &v2, &n_vrs, now, 1));
  1642. tt_assert(v2);
  1643. if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
  1644. /* Check that flags come out right.*/
  1645. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1646. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1647. "Running:Stable:V2Dir:Valid");
  1648. tor_free(cp);
  1649. /* Check the routerstatuses. */
  1650. n_vrs = smartlist_len(v2->routerstatus_list);
  1651. for (idx = 0; idx < n_vrs; ++idx) {
  1652. vrs = smartlist_get(v2->routerstatus_list, idx);
  1653. tt_assert(vrs);
  1654. vrs_test(vrs, 2, now);
  1655. }
  1656. tor_free(vote);
  1657. /* Generate the third vote with a legacy id. */
  1658. tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
  1659. &v3, &n_vrs, now, 1));
  1660. tt_assert(v3);
  1661. if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
  1662. /* Compute a consensus as voter 3. */
  1663. smartlist_add(votes, v3);
  1664. smartlist_add(votes, v1);
  1665. smartlist_add(votes, v2);
  1666. consensus_text = networkstatus_compute_consensus(votes, 3,
  1667. cert3->identity_key,
  1668. sign_skey_3,
  1669. "AAAAAAAAAAAAAAAAAAAA",
  1670. sign_skey_leg1,
  1671. FLAV_NS);
  1672. tt_assert(consensus_text);
  1673. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  1674. NS_TYPE_CONSENSUS);
  1675. tt_assert(con);
  1676. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1677. // v1_text, v2_text, v3_text);
  1678. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1679. cert3->identity_key,
  1680. sign_skey_3,
  1681. "AAAAAAAAAAAAAAAAAAAA",
  1682. sign_skey_leg1,
  1683. FLAV_MICRODESC);
  1684. tt_assert(consensus_text_md);
  1685. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1686. NS_TYPE_CONSENSUS);
  1687. tt_assert(con_md);
  1688. tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
  1689. /* Check consensus contents. */
  1690. tt_assert(con->type == NS_TYPE_CONSENSUS);
  1691. tt_int_op(con->published,OP_EQ, 0); /* this field only appears in votes. */
  1692. tt_int_op(con->valid_after,OP_EQ, now+1000);
  1693. tt_int_op(con->fresh_until,OP_EQ, now+2003); /* median */
  1694. tt_int_op(con->valid_until,OP_EQ, now+3000);
  1695. tt_int_op(con->vote_seconds,OP_EQ, 100);
  1696. tt_int_op(con->dist_seconds,OP_EQ, 250); /* median */
  1697. tt_str_op(con->client_versions,OP_EQ, "0.1.2.14");
  1698. tt_str_op(con->server_versions,OP_EQ, "0.1.2.15,0.1.2.16");
  1699. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1700. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1701. "Running:Stable:V2Dir:Valid");
  1702. tor_free(cp);
  1703. if (!params_tweaked) {
  1704. /* Skip this one if vote_tweaks() messed with the param lists */
  1705. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1706. tt_str_op(cp,OP_EQ, "circuitwindow=80:foo=660");
  1707. tor_free(cp);
  1708. }
  1709. tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1710. /* The voter id digests should be in this order. */
  1711. tt_assert(memcmp(cert2->cache_info.identity_digest,
  1712. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1713. tt_assert(memcmp(cert1->cache_info.identity_digest,
  1714. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1715. test_same_voter(smartlist_get(con->voters, 1),
  1716. smartlist_get(v2->voters, 0));
  1717. test_same_voter(smartlist_get(con->voters, 2),
  1718. smartlist_get(v1->voters, 0));
  1719. test_same_voter(smartlist_get(con->voters, 3),
  1720. smartlist_get(v3->voters, 0));
  1721. consensus_test(con, now);
  1722. /* Check the routerstatuses. */
  1723. n_rs = smartlist_len(con->routerstatus_list);
  1724. for (idx = 0; idx < n_rs; ++idx) {
  1725. rs = smartlist_get(con->routerstatus_list, idx);
  1726. tt_assert(rs);
  1727. rs_test(rs, now);
  1728. }
  1729. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1730. * The second one hasn't signed. The fourth one has signed: validate it. */
  1731. voter = smartlist_get(con->voters, 1);
  1732. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 0);
  1733. voter = smartlist_get(con->voters, 3);
  1734. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 1);
  1735. sig = smartlist_get(voter->sigs, 0);
  1736. tt_assert(sig->signature);
  1737. tt_assert(!sig->good_signature);
  1738. tt_assert(!sig->bad_signature);
  1739. tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1740. tt_assert(sig->signature);
  1741. tt_assert(sig->good_signature);
  1742. tt_assert(!sig->bad_signature);
  1743. {
  1744. const char *msg=NULL;
  1745. /* Compute the other two signed consensuses. */
  1746. smartlist_shuffle(votes);
  1747. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1748. cert2->identity_key,
  1749. sign_skey_2, NULL,NULL,
  1750. FLAV_NS);
  1751. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1752. cert2->identity_key,
  1753. sign_skey_2, NULL,NULL,
  1754. FLAV_MICRODESC);
  1755. smartlist_shuffle(votes);
  1756. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1757. cert1->identity_key,
  1758. sign_skey_1, NULL,NULL,
  1759. FLAV_NS);
  1760. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1761. cert1->identity_key,
  1762. sign_skey_1, NULL,NULL,
  1763. FLAV_MICRODESC);
  1764. tt_assert(consensus_text2);
  1765. tt_assert(consensus_text3);
  1766. tt_assert(consensus_text_md2);
  1767. tt_assert(consensus_text_md3);
  1768. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1769. NS_TYPE_CONSENSUS);
  1770. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1771. NS_TYPE_CONSENSUS);
  1772. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1773. NS_TYPE_CONSENSUS);
  1774. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1775. NS_TYPE_CONSENSUS);
  1776. tt_assert(con2);
  1777. tt_assert(con3);
  1778. tt_assert(con_md2);
  1779. tt_assert(con_md3);
  1780. /* All three should have the same digest. */
  1781. tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(digests_t));
  1782. tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(digests_t));
  1783. tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests, sizeof(digests_t));
  1784. tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests, sizeof(digests_t));
  1785. /* Extract a detached signature from con3. */
  1786. detached_text1 = get_detached_sigs(con3, con_md3);
  1787. tt_assert(detached_text1);
  1788. /* Try to parse it. */
  1789. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1790. tt_assert(dsig1);
  1791. /* Are parsed values as expected? */
  1792. tt_int_op(dsig1->valid_after,OP_EQ, con3->valid_after);
  1793. tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
  1794. tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
  1795. {
  1796. digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1797. tt_assert(dsig_digests);
  1798. tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
  1799. con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
  1800. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1801. tt_assert(dsig_digests);
  1802. tt_mem_op(dsig_digests->d[DIGEST_SHA256],OP_EQ,
  1803. con_md3->digests.d[DIGEST_SHA256],
  1804. DIGEST256_LEN);
  1805. }
  1806. {
  1807. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1808. tt_assert(dsig_signatures);
  1809. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  1810. sig = smartlist_get(dsig_signatures, 0);
  1811. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  1812. DIGEST_LEN);
  1813. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA1);
  1814. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1815. tt_assert(dsig_signatures);
  1816. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  1817. sig = smartlist_get(dsig_signatures, 0);
  1818. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  1819. DIGEST_LEN);
  1820. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA256);
  1821. }
  1822. /* Try adding it to con2. */
  1823. detached_text2 = get_detached_sigs(con2,con_md2);
  1824. tt_int_op(1,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  1825. "test", LOG_INFO, &msg));
  1826. tor_free(detached_text2);
  1827. tt_int_op(1,OP_EQ,
  1828. networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1829. LOG_INFO, &msg));
  1830. tor_free(detached_text2);
  1831. detached_text2 = get_detached_sigs(con2,con_md2);
  1832. //printf("\n<%s>\n", detached_text2);
  1833. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1834. tt_assert(dsig2);
  1835. /*
  1836. printf("\n");
  1837. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1838. char hd[64];
  1839. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1840. printf("%s\n", hd);
  1841. });
  1842. */
  1843. tt_int_op(2,OP_EQ,
  1844. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1845. tt_int_op(2,OP_EQ,
  1846. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  1847. "microdesc")));
  1848. /* Try adding to con2 twice; verify that nothing changes. */
  1849. tt_int_op(0,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  1850. "test", LOG_INFO, &msg));
  1851. /* Add to con. */
  1852. tt_int_op(2,OP_EQ, networkstatus_add_detached_signatures(con, dsig2,
  1853. "test", LOG_INFO, &msg));
  1854. /* Check signatures */
  1855. voter = smartlist_get(con->voters, 1);
  1856. sig = smartlist_get(voter->sigs, 0);
  1857. tt_assert(sig);
  1858. tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
  1859. voter = smartlist_get(con->voters, 2);
  1860. sig = smartlist_get(voter->sigs, 0);
  1861. tt_assert(sig);
  1862. tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
  1863. }
  1864. done:
  1865. tor_free(cp);
  1866. smartlist_free(votes);
  1867. tor_free(consensus_text);
  1868. tor_free(consensus_text_md);
  1869. if (vote)
  1870. networkstatus_vote_free(vote);
  1871. if (v1)
  1872. networkstatus_vote_free(v1);
  1873. if (v2)
  1874. networkstatus_vote_free(v2);
  1875. if (v3)
  1876. networkstatus_vote_free(v3);
  1877. if (con)
  1878. networkstatus_vote_free(con);
  1879. if (con_md)
  1880. networkstatus_vote_free(con_md);
  1881. if (sign_skey_1)
  1882. crypto_pk_free(sign_skey_1);
  1883. if (sign_skey_2)
  1884. crypto_pk_free(sign_skey_2);
  1885. if (sign_skey_3)
  1886. crypto_pk_free(sign_skey_3);
  1887. if (sign_skey_leg1)
  1888. crypto_pk_free(sign_skey_leg1);
  1889. if (cert1)
  1890. authority_cert_free(cert1);
  1891. if (cert2)
  1892. authority_cert_free(cert2);
  1893. if (cert3)
  1894. authority_cert_free(cert3);
  1895. tor_free(consensus_text2);
  1896. tor_free(consensus_text3);
  1897. tor_free(consensus_text_md2);
  1898. tor_free(consensus_text_md3);
  1899. tor_free(detached_text1);
  1900. tor_free(detached_text2);
  1901. if (con2)
  1902. networkstatus_vote_free(con2);
  1903. if (con3)
  1904. networkstatus_vote_free(con3);
  1905. if (con_md2)
  1906. networkstatus_vote_free(con_md2);
  1907. if (con_md3)
  1908. networkstatus_vote_free(con_md3);
  1909. if (dsig1)
  1910. ns_detached_signatures_free(dsig1);
  1911. if (dsig2)
  1912. ns_detached_signatures_free(dsig2);
  1913. }
  1914. /** Run unit tests for generating and parsing V3 consensus networkstatus
  1915. * documents. */
  1916. static void
  1917. test_dir_v3_networkstatus(void *arg)
  1918. {
  1919. (void)arg;
  1920. test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
  1921. vote_tweaks_for_v3ns,
  1922. test_vrs_for_v3ns,
  1923. test_consensus_for_v3ns,
  1924. test_routerstatus_for_v3ns);
  1925. }
  1926. static void
  1927. test_dir_scale_bw(void *testdata)
  1928. {
  1929. double v[8] = { 2.0/3,
  1930. 7.0,
  1931. 1.0,
  1932. 3.0,
  1933. 1.0/5,
  1934. 1.0/7,
  1935. 12.0,
  1936. 24.0 };
  1937. u64_dbl_t vals[8];
  1938. uint64_t total;
  1939. int i;
  1940. (void) testdata;
  1941. for (i=0; i<8; ++i)
  1942. vals[i].dbl = v[i];
  1943. scale_array_elements_to_u64(vals, 8, &total);
  1944. tt_int_op((int)total, OP_EQ, 48);
  1945. total = 0;
  1946. for (i=0; i<8; ++i) {
  1947. total += vals[i].u64;
  1948. }
  1949. tt_assert(total >= (U64_LITERAL(1)<<60));
  1950. tt_assert(total <= (U64_LITERAL(1)<<62));
  1951. for (i=0; i<8; ++i) {
  1952. /* vals[2].u64 is the scaled value of 1.0 */
  1953. double ratio = ((double)vals[i].u64) / vals[2].u64;
  1954. tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
  1955. }
  1956. /* test handling of no entries */
  1957. total = 1;
  1958. scale_array_elements_to_u64(vals, 0, &total);
  1959. tt_assert(total == 0);
  1960. /* make sure we don't read the array when we have no entries
  1961. * may require compiler flags to catch NULL dereferences */
  1962. total = 1;
  1963. scale_array_elements_to_u64(NULL, 0, &total);
  1964. tt_assert(total == 0);
  1965. scale_array_elements_to_u64(NULL, 0, NULL);
  1966. /* test handling of zero totals */
  1967. total = 1;
  1968. vals[0].dbl = 0.0;
  1969. scale_array_elements_to_u64(vals, 1, &total);
  1970. tt_assert(total == 0);
  1971. tt_assert(vals[0].u64 == 0);
  1972. vals[0].dbl = 0.0;
  1973. vals[1].dbl = 0.0;
  1974. scale_array_elements_to_u64(vals, 2, NULL);
  1975. tt_assert(vals[0].u64 == 0);
  1976. tt_assert(vals[1].u64 == 0);
  1977. done:
  1978. ;
  1979. }
  1980. static void
  1981. test_dir_random_weighted(void *testdata)
  1982. {
  1983. int histogram[10];
  1984. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  1985. u64_dbl_t inp[10];
  1986. int i, choice;
  1987. const int n = 50000;
  1988. double max_sq_error;
  1989. (void) testdata;
  1990. /* Try a ten-element array with values from 0 through 10. The values are
  1991. * in a scrambled order to make sure we don't depend on order. */
  1992. memset(histogram,0,sizeof(histogram));
  1993. for (i=0; i<10; ++i) {
  1994. inp[i].u64 = vals[i];
  1995. total += vals[i];
  1996. }
  1997. tt_u64_op(total, OP_EQ, 45);
  1998. for (i=0; i<n; ++i) {
  1999. choice = choose_array_element_by_weight(inp, 10);
  2000. tt_int_op(choice, OP_GE, 0);
  2001. tt_int_op(choice, OP_LT, 10);
  2002. histogram[choice]++;
  2003. }
  2004. /* Now see if we chose things about frequently enough. */
  2005. max_sq_error = 0;
  2006. for (i=0; i<10; ++i) {
  2007. int expected = (int)(n*vals[i]/total);
  2008. double frac_diff = 0, sq;
  2009. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  2010. if (expected)
  2011. frac_diff = (histogram[i] - expected) / ((double)expected);
  2012. else
  2013. tt_int_op(histogram[i], OP_EQ, 0);
  2014. sq = frac_diff * frac_diff;
  2015. if (sq > max_sq_error)
  2016. max_sq_error = sq;
  2017. }
  2018. /* It should almost always be much much less than this. If you want to
  2019. * figure out the odds, please feel free. */
  2020. tt_double_op(max_sq_error, OP_LT, .05);
  2021. /* Now try a singleton; do we choose it? */
  2022. for (i = 0; i < 100; ++i) {
  2023. choice = choose_array_element_by_weight(inp, 1);
  2024. tt_int_op(choice, OP_EQ, 0);
  2025. }
  2026. /* Now try an array of zeros. We should choose randomly. */
  2027. memset(histogram,0,sizeof(histogram));
  2028. for (i = 0; i < 5; ++i)
  2029. inp[i].u64 = 0;
  2030. for (i = 0; i < n; ++i) {
  2031. choice = choose_array_element_by_weight(inp, 5);
  2032. tt_int_op(choice, OP_GE, 0);
  2033. tt_int_op(choice, OP_LT, 5);
  2034. histogram[choice]++;
  2035. }
  2036. /* Now see if we chose things about frequently enough. */
  2037. max_sq_error = 0;
  2038. for (i=0; i<5; ++i) {
  2039. int expected = n/5;
  2040. double frac_diff = 0, sq;
  2041. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  2042. frac_diff = (histogram[i] - expected) / ((double)expected);
  2043. sq = frac_diff * frac_diff;
  2044. if (sq > max_sq_error)
  2045. max_sq_error = sq;
  2046. }
  2047. /* It should almost always be much much less than this. If you want to
  2048. * figure out the odds, please feel free. */
  2049. tt_double_op(max_sq_error, OP_LT, .05);
  2050. done:
  2051. ;
  2052. }
  2053. /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
  2054. static uint32_t alternate_clip_bw = 0;
  2055. /**
  2056. * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
  2057. * v3_networkstatus ones.
  2058. */
  2059. static vote_routerstatus_t *
  2060. gen_routerstatus_for_umbw(int idx, time_t now)
  2061. {
  2062. vote_routerstatus_t *vrs = NULL;
  2063. routerstatus_t *rs;
  2064. tor_addr_t addr_ipv6;
  2065. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2066. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2067. switch (idx) {
  2068. case 0:
  2069. /* Generate the first routerstatus. */
  2070. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2071. rs = &vrs->status;
  2072. vrs->version = tor_strdup("0.1.2.14");
  2073. rs->published_on = now-1500;
  2074. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  2075. memset(rs->identity_digest, 3, DIGEST_LEN);
  2076. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  2077. rs->addr = 0x99008801;
  2078. rs->or_port = 443;
  2079. rs->dir_port = 8000;
  2080. /* all flags but running cleared */
  2081. rs->is_flagged_running = 1;
  2082. /*
  2083. * This one has measured bandwidth below the clip cutoff, and
  2084. * so shouldn't be clipped; we'll have to test that it isn't
  2085. * later.
  2086. */
  2087. vrs->has_measured_bw = 1;
  2088. rs->has_bandwidth = 1;
  2089. vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  2090. break;
  2091. case 1:
  2092. /* Generate the second routerstatus. */
  2093. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2094. rs = &vrs->status;
  2095. vrs->version = tor_strdup("0.2.0.5");
  2096. rs->published_on = now-1000;
  2097. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  2098. memset(rs->identity_digest, 5, DIGEST_LEN);
  2099. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  2100. rs->addr = 0x99009901;
  2101. rs->or_port = 443;
  2102. rs->dir_port = 0;
  2103. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2104. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  2105. rs->ipv6_orport = 4711;
  2106. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  2107. rs->is_valid = rs->is_possible_guard = 1;
  2108. /*
  2109. * This one has measured bandwidth above the clip cutoff, and
  2110. * so shouldn't be clipped; we'll have to test that it isn't
  2111. * later.
  2112. */
  2113. vrs->has_measured_bw = 1;
  2114. rs->has_bandwidth = 1;
  2115. vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  2116. break;
  2117. case 2:
  2118. /* Generate the third routerstatus. */
  2119. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2120. rs = &vrs->status;
  2121. vrs->version = tor_strdup("0.1.0.3");
  2122. rs->published_on = now-1000;
  2123. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  2124. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  2125. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  2126. rs->addr = 0xAA009901;
  2127. rs->or_port = 400;
  2128. rs->dir_port = 9999;
  2129. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  2130. rs->is_flagged_running = rs->is_valid =
  2131. rs->is_possible_guard = 1;
  2132. /*
  2133. * This one has unmeasured bandwidth above the clip cutoff, and
  2134. * so should be clipped; we'll have to test that it isn't
  2135. * later.
  2136. */
  2137. vrs->has_measured_bw = 0;
  2138. rs->has_bandwidth = 1;
  2139. vrs->measured_bw_kb = 0;
  2140. rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  2141. break;
  2142. case 3:
  2143. /* Generate a fourth routerstatus that is not running. */
  2144. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2145. rs = &vrs->status;
  2146. vrs->version = tor_strdup("0.1.6.3");
  2147. rs->published_on = now-1000;
  2148. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  2149. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  2150. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  2151. rs->addr = 0xC0000203;
  2152. rs->or_port = 500;
  2153. rs->dir_port = 1999;
  2154. /* all flags but running cleared */
  2155. rs->is_flagged_running = 1;
  2156. /*
  2157. * This one has unmeasured bandwidth below the clip cutoff, and
  2158. * so shouldn't be clipped; we'll have to test that it isn't
  2159. * later.
  2160. */
  2161. vrs->has_measured_bw = 0;
  2162. rs->has_bandwidth = 1;
  2163. vrs->measured_bw_kb = 0;
  2164. rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  2165. break;
  2166. case 4:
  2167. /* No more for this test; return NULL */
  2168. vrs = NULL;
  2169. break;
  2170. default:
  2171. /* Shouldn't happen */
  2172. tt_assert(0);
  2173. }
  2174. if (vrs) {
  2175. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  2176. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  2177. "m 9,10,11,12,13,14,15,16,17 "
  2178. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  2179. idx);
  2180. }
  2181. done:
  2182. return vrs;
  2183. }
  2184. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  2185. * just adding the right consensus methods to let clipping happen */
  2186. static int
  2187. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  2188. {
  2189. char *maxbw_param = NULL;
  2190. int rv = 0;
  2191. tt_assert(v);
  2192. (void)voter;
  2193. (void)now;
  2194. tt_assert(v->supported_methods);
  2195. SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
  2196. smartlist_clear(v->supported_methods);
  2197. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
  2198. smartlist_split_string(v->supported_methods,
  2199. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
  2200. NULL, 0, -1);
  2201. /* If we're using a non-default clip bandwidth, add it to net_params */
  2202. if (alternate_clip_bw > 0) {
  2203. tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
  2204. tt_assert(maxbw_param);
  2205. if (maxbw_param) {
  2206. smartlist_add(v->net_params, maxbw_param);
  2207. rv = 1;
  2208. }
  2209. }
  2210. done:
  2211. return rv;
  2212. }
  2213. /**
  2214. * Test a parsed vote_routerstatus_t for umbw test.
  2215. */
  2216. static void
  2217. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  2218. {
  2219. routerstatus_t *rs;
  2220. tor_addr_t addr_ipv6;
  2221. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2222. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2223. (void)voter;
  2224. tt_assert(vrs);
  2225. rs = &(vrs->status);
  2226. tt_assert(rs);
  2227. /* Split out by digests to test */
  2228. if (tor_memeq(rs->identity_digest,
  2229. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2230. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2231. DIGEST_LEN)) {
  2232. /*
  2233. * Check the first routerstatus - measured bandwidth below the clip
  2234. * cutoff.
  2235. */
  2236. tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
  2237. tt_int_op(rs->published_on,OP_EQ, now-1500);
  2238. tt_str_op(rs->nickname,OP_EQ, "router2");
  2239. tt_mem_op(rs->identity_digest,OP_EQ,
  2240. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2241. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2242. DIGEST_LEN);
  2243. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2244. tt_int_op(rs->addr,OP_EQ, 0x99008801);
  2245. tt_int_op(rs->or_port,OP_EQ, 443);
  2246. tt_int_op(rs->dir_port,OP_EQ, 8000);
  2247. tt_assert(rs->has_bandwidth);
  2248. tt_assert(vrs->has_measured_bw);
  2249. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2250. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2251. } else if (tor_memeq(rs->identity_digest,
  2252. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2253. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2254. DIGEST_LEN)) {
  2255. /*
  2256. * Check the second routerstatus - measured bandwidth above the clip
  2257. * cutoff.
  2258. */
  2259. tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
  2260. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2261. tt_str_op(rs->nickname,OP_EQ, "router1");
  2262. tt_mem_op(rs->identity_digest,OP_EQ,
  2263. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2264. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2265. DIGEST_LEN);
  2266. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2267. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2268. tt_int_op(rs->or_port,OP_EQ, 443);
  2269. tt_int_op(rs->dir_port,OP_EQ, 0);
  2270. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2271. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2272. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2273. tt_assert(rs->has_bandwidth);
  2274. tt_assert(vrs->has_measured_bw);
  2275. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2276. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2277. } else if (tor_memeq(rs->identity_digest,
  2278. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2279. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2280. DIGEST_LEN)) {
  2281. /*
  2282. * Check the third routerstatus - unmeasured bandwidth above the clip
  2283. * cutoff; this one should be clipped later on in the consensus, but
  2284. * appears unclipped in the vote.
  2285. */
  2286. tt_assert(rs->has_bandwidth);
  2287. tt_assert(!(vrs->has_measured_bw));
  2288. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2289. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  2290. } else if (tor_memeq(rs->identity_digest,
  2291. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  2292. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  2293. DIGEST_LEN)) {
  2294. /*
  2295. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  2296. * cutoff; this one should not be clipped.
  2297. */
  2298. tt_assert(rs->has_bandwidth);
  2299. tt_assert(!(vrs->has_measured_bw));
  2300. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2301. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  2302. } else {
  2303. tt_assert(0);
  2304. }
  2305. done:
  2306. return;
  2307. }
  2308. /**
  2309. * Test a consensus for v3_networkstatus_test
  2310. */
  2311. static void
  2312. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  2313. {
  2314. (void)now;
  2315. tt_assert(con);
  2316. tt_assert(!con->cert);
  2317. // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
  2318. tt_assert(con->consensus_method >= 16);
  2319. tt_int_op(4,OP_EQ, smartlist_len(con->routerstatus_list));
  2320. /* There should be four listed routers; all voters saw the same in this */
  2321. done:
  2322. return;
  2323. }
  2324. /**
  2325. * Test a router list entry for umbw test
  2326. */
  2327. static void
  2328. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  2329. {
  2330. tor_addr_t addr_ipv6;
  2331. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2332. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2333. tt_assert(rs);
  2334. /* There should be four listed routers, as constructed above */
  2335. if (tor_memeq(rs->identity_digest,
  2336. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2337. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2338. DIGEST_LEN)) {
  2339. tt_mem_op(rs->identity_digest,OP_EQ,
  2340. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2341. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2342. DIGEST_LEN);
  2343. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2344. tt_assert(!rs->is_authority);
  2345. tt_assert(!rs->is_exit);
  2346. tt_assert(!rs->is_fast);
  2347. tt_assert(!rs->is_possible_guard);
  2348. tt_assert(!rs->is_stable);
  2349. /* (If it wasn't running it wouldn't be here) */
  2350. tt_assert(rs->is_flagged_running);
  2351. tt_assert(!rs->is_valid);
  2352. tt_assert(!rs->is_named);
  2353. /* This one should have measured bandwidth below the clip cutoff */
  2354. tt_assert(rs->has_bandwidth);
  2355. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2356. tt_assert(!(rs->bw_is_unmeasured));
  2357. } else if (tor_memeq(rs->identity_digest,
  2358. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2359. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2360. DIGEST_LEN)) {
  2361. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  2362. tt_mem_op(rs->identity_digest,OP_EQ,
  2363. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2364. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2365. DIGEST_LEN);
  2366. tt_str_op(rs->nickname,OP_EQ, "router1");
  2367. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2368. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2369. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2370. tt_int_op(rs->or_port,OP_EQ, 443);
  2371. tt_int_op(rs->dir_port,OP_EQ, 0);
  2372. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2373. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2374. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2375. tt_assert(!rs->is_authority);
  2376. tt_assert(rs->is_exit);
  2377. tt_assert(rs->is_fast);
  2378. tt_assert(rs->is_possible_guard);
  2379. tt_assert(rs->is_stable);
  2380. tt_assert(rs->is_flagged_running);
  2381. tt_assert(rs->is_valid);
  2382. tt_assert(!rs->is_named);
  2383. /* This one should have measured bandwidth above the clip cutoff */
  2384. tt_assert(rs->has_bandwidth);
  2385. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2386. tt_assert(!(rs->bw_is_unmeasured));
  2387. } else if (tor_memeq(rs->identity_digest,
  2388. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2389. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2390. DIGEST_LEN)) {
  2391. /*
  2392. * This one should have unmeasured bandwidth above the clip cutoff,
  2393. * and so should be clipped
  2394. */
  2395. tt_assert(rs->has_bandwidth);
  2396. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb);
  2397. tt_assert(rs->bw_is_unmeasured);
  2398. } else if (tor_memeq(rs->identity_digest,
  2399. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  2400. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  2401. DIGEST_LEN)) {
  2402. /*
  2403. * This one should have unmeasured bandwidth below the clip cutoff,
  2404. * and so should not be clipped
  2405. */
  2406. tt_assert(rs->has_bandwidth);
  2407. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2408. tt_assert(rs->bw_is_unmeasured);
  2409. } else {
  2410. /* Weren't expecting this... */
  2411. tt_assert(0);
  2412. }
  2413. done:
  2414. return;
  2415. }
  2416. /**
  2417. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  2418. * method 17; this uses the same test_a_networkstatus() function that the
  2419. * v3_networkstatus test uses.
  2420. */
  2421. static void
  2422. test_dir_clip_unmeasured_bw_kb(void *arg)
  2423. {
  2424. /* Run the test with the default clip bandwidth */
  2425. (void)arg;
  2426. alternate_clip_bw = 0;
  2427. test_a_networkstatus(gen_routerstatus_for_umbw,
  2428. vote_tweaks_for_umbw,
  2429. test_vrs_for_umbw,
  2430. test_consensus_for_umbw,
  2431. test_routerstatus_for_umbw);
  2432. }
  2433. /**
  2434. * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
  2435. * of clip bandwidth.
  2436. */
  2437. static void
  2438. test_dir_clip_unmeasured_bw_kb_alt(void *arg)
  2439. {
  2440. /*
  2441. * Try a different one; this value is chosen so that the below-the-cutoff
  2442. * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
  2443. * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
  2444. * cutoff it will fail the test.
  2445. */
  2446. (void)arg;
  2447. alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
  2448. test_a_networkstatus(gen_routerstatus_for_umbw,
  2449. vote_tweaks_for_umbw,
  2450. test_vrs_for_umbw,
  2451. test_consensus_for_umbw,
  2452. test_routerstatus_for_umbw);
  2453. }
  2454. static void
  2455. test_dir_fmt_control_ns(void *arg)
  2456. {
  2457. char *s = NULL;
  2458. routerstatus_t rs;
  2459. (void)arg;
  2460. memset(&rs, 0, sizeof(rs));
  2461. rs.published_on = 1364925198;
  2462. strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
  2463. memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
  2464. memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
  2465. rs.addr = 0x20304050;
  2466. rs.or_port = 9001;
  2467. rs.dir_port = 9002;
  2468. rs.is_exit = 1;
  2469. rs.is_fast = 1;
  2470. rs.is_flagged_running = 1;
  2471. rs.has_bandwidth = 1;
  2472. rs.is_v2_dir = 1;
  2473. rs.bandwidth_kb = 1000;
  2474. s = networkstatus_getinfo_helper_single(&rs);
  2475. tt_assert(s);
  2476. tt_str_op(s, OP_EQ,
  2477. "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
  2478. "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
  2479. "32.48.64.80 9001 9002\n"
  2480. "s Exit Fast Running V2Dir\n"
  2481. "w Bandwidth=1000\n");
  2482. done:
  2483. tor_free(s);
  2484. }
  2485. static int mock_get_options_calls = 0;
  2486. static or_options_t *mock_options = NULL;
  2487. static void
  2488. reset_options(or_options_t *options, int *get_options_calls)
  2489. {
  2490. memset(options, 0, sizeof(or_options_t));
  2491. options->TestingTorNetwork = 1;
  2492. *get_options_calls = 0;
  2493. }
  2494. static const or_options_t *
  2495. mock_get_options(void)
  2496. {
  2497. ++mock_get_options_calls;
  2498. tor_assert(mock_options);
  2499. return mock_options;
  2500. }
  2501. static void
  2502. reset_routerstatus(routerstatus_t *rs,
  2503. const char *hex_identity_digest,
  2504. int32_t ipv4_addr)
  2505. {
  2506. memset(rs, 0, sizeof(routerstatus_t));
  2507. base16_decode(rs->identity_digest, sizeof(rs->identity_digest),
  2508. hex_identity_digest, HEX_DIGEST_LEN);
  2509. /* A zero address matches everything, so the address needs to be set.
  2510. * But the specific value is irrelevant. */
  2511. rs->addr = ipv4_addr;
  2512. }
  2513. #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  2514. #define ROUTER_A_IPV4 0xAA008801
  2515. #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  2516. #define ROUTER_B_IPV4 0xBB008801
  2517. #define ROUTERSET_ALL_STR "*"
  2518. #define ROUTERSET_A_STR ROUTER_A_ID_STR
  2519. #define ROUTERSET_NONE_STR ""
  2520. /*
  2521. * Test that dirserv_set_routerstatus_testing sets router flags correctly
  2522. * Using "*" sets flags on A and B
  2523. * Using "A" sets flags on A
  2524. * Using "" sets flags on Neither
  2525. * If the router is not included:
  2526. * - if *Strict is set, the flag is set to 0,
  2527. * - otherwise, the flag is not modified. */
  2528. static void
  2529. test_dir_dirserv_set_routerstatus_testing(void *arg)
  2530. {
  2531. (void)arg;
  2532. /* Init options */
  2533. mock_options = malloc(sizeof(or_options_t));
  2534. reset_options(mock_options, &mock_get_options_calls);
  2535. MOCK(get_options, mock_get_options);
  2536. /* Init routersets */
  2537. routerset_t *routerset_all = routerset_new();
  2538. routerset_parse(routerset_all, ROUTERSET_ALL_STR, "All routers");
  2539. routerset_t *routerset_a = routerset_new();
  2540. routerset_parse(routerset_a, ROUTERSET_A_STR, "Router A only");
  2541. routerset_t *routerset_none = routerset_new();
  2542. /* Routersets are empty when provided by routerset_new(),
  2543. * so this is not strictly necessary */
  2544. routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
  2545. /* Init routerstatuses */
  2546. routerstatus_t *rs_a = malloc(sizeof(routerstatus_t));
  2547. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2548. routerstatus_t *rs_b = malloc(sizeof(routerstatus_t));
  2549. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2550. /* Sanity check that routersets correspond to routerstatuses.
  2551. * Return values are {2, 3, 4} */
  2552. /* We want 3 ("*" means match all addresses) */
  2553. tt_assert(routerset_contains_routerstatus(routerset_all, rs_a, 0) == 3);
  2554. tt_assert(routerset_contains_routerstatus(routerset_all, rs_b, 0) == 3);
  2555. /* We want 4 (match id_digest [or nickname]) */
  2556. tt_assert(routerset_contains_routerstatus(routerset_a, rs_a, 0) == 4);
  2557. tt_assert(routerset_contains_routerstatus(routerset_a, rs_b, 0) == 0);
  2558. tt_assert(routerset_contains_routerstatus(routerset_none, rs_a, 0) == 0);
  2559. tt_assert(routerset_contains_routerstatus(routerset_none, rs_b, 0) == 0);
  2560. /* Check that "*" sets flags on all routers: Exit
  2561. * Check the flags aren't being confused with each other */
  2562. reset_options(mock_options, &mock_get_options_calls);
  2563. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2564. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2565. mock_options->TestingDirAuthVoteExit = routerset_all;
  2566. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2567. dirserv_set_routerstatus_testing(rs_a);
  2568. tt_assert(mock_get_options_calls == 1);
  2569. dirserv_set_routerstatus_testing(rs_b);
  2570. tt_assert(mock_get_options_calls == 2);
  2571. tt_assert(rs_a->is_exit == 1);
  2572. tt_assert(rs_b->is_exit == 1);
  2573. /* Be paranoid - check no other flags are set */
  2574. tt_assert(rs_a->is_possible_guard == 0);
  2575. tt_assert(rs_b->is_possible_guard == 0);
  2576. tt_assert(rs_a->is_hs_dir == 0);
  2577. tt_assert(rs_b->is_hs_dir == 0);
  2578. /* Check that "*" sets flags on all routers: Guard & HSDir
  2579. * Cover the remaining flags in one test */
  2580. reset_options(mock_options, &mock_get_options_calls);
  2581. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2582. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2583. mock_options->TestingDirAuthVoteGuard = routerset_all;
  2584. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2585. mock_options->TestingDirAuthVoteHSDir = routerset_all;
  2586. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2587. dirserv_set_routerstatus_testing(rs_a);
  2588. tt_assert(mock_get_options_calls == 1);
  2589. dirserv_set_routerstatus_testing(rs_b);
  2590. tt_assert(mock_get_options_calls == 2);
  2591. tt_assert(rs_a->is_possible_guard == 1);
  2592. tt_assert(rs_b->is_possible_guard == 1);
  2593. tt_assert(rs_a->is_hs_dir == 1);
  2594. tt_assert(rs_b->is_hs_dir == 1);
  2595. /* Be paranoid - check exit isn't set */
  2596. tt_assert(rs_a->is_exit == 0);
  2597. tt_assert(rs_b->is_exit == 0);
  2598. /* Check routerset A sets all flags on router A,
  2599. * but leaves router B unmodified */
  2600. reset_options(mock_options, &mock_get_options_calls);
  2601. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2602. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2603. mock_options->TestingDirAuthVoteExit = routerset_a;
  2604. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2605. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2606. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2607. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2608. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2609. dirserv_set_routerstatus_testing(rs_a);
  2610. tt_assert(mock_get_options_calls == 1);
  2611. dirserv_set_routerstatus_testing(rs_b);
  2612. tt_assert(mock_get_options_calls == 2);
  2613. tt_assert(rs_a->is_exit == 1);
  2614. tt_assert(rs_b->is_exit == 0);
  2615. tt_assert(rs_a->is_possible_guard == 1);
  2616. tt_assert(rs_b->is_possible_guard == 0);
  2617. tt_assert(rs_a->is_hs_dir == 1);
  2618. tt_assert(rs_b->is_hs_dir == 0);
  2619. /* Check routerset A unsets all flags on router B when Strict is set */
  2620. reset_options(mock_options, &mock_get_options_calls);
  2621. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2622. mock_options->TestingDirAuthVoteExit = routerset_a;
  2623. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  2624. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2625. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  2626. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2627. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  2628. rs_b->is_exit = 1;
  2629. rs_b->is_possible_guard = 1;
  2630. rs_b->is_hs_dir = 1;
  2631. dirserv_set_routerstatus_testing(rs_b);
  2632. tt_assert(mock_get_options_calls == 1);
  2633. tt_assert(rs_b->is_exit == 0);
  2634. tt_assert(rs_b->is_possible_guard == 0);
  2635. tt_assert(rs_b->is_hs_dir == 0);
  2636. /* Check routerset A doesn't modify flags on router B without Strict set */
  2637. reset_options(mock_options, &mock_get_options_calls);
  2638. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2639. mock_options->TestingDirAuthVoteExit = routerset_a;
  2640. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2641. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2642. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2643. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2644. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2645. rs_b->is_exit = 1;
  2646. rs_b->is_possible_guard = 1;
  2647. rs_b->is_hs_dir = 1;
  2648. dirserv_set_routerstatus_testing(rs_b);
  2649. tt_assert(mock_get_options_calls == 1);
  2650. tt_assert(rs_b->is_exit == 1);
  2651. tt_assert(rs_b->is_possible_guard == 1);
  2652. tt_assert(rs_b->is_hs_dir == 1);
  2653. /* Check the empty routerset zeroes all flags
  2654. * on routers A & B with Strict set */
  2655. reset_options(mock_options, &mock_get_options_calls);
  2656. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2657. mock_options->TestingDirAuthVoteExit = routerset_none;
  2658. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  2659. mock_options->TestingDirAuthVoteGuard = routerset_none;
  2660. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  2661. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  2662. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  2663. rs_b->is_exit = 1;
  2664. rs_b->is_possible_guard = 1;
  2665. rs_b->is_hs_dir = 1;
  2666. dirserv_set_routerstatus_testing(rs_b);
  2667. tt_assert(mock_get_options_calls == 1);
  2668. tt_assert(rs_b->is_exit == 0);
  2669. tt_assert(rs_b->is_possible_guard == 0);
  2670. tt_assert(rs_b->is_hs_dir == 0);
  2671. /* Check the empty routerset doesn't modify any flags
  2672. * on A or B without Strict set */
  2673. reset_options(mock_options, &mock_get_options_calls);
  2674. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2675. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2676. mock_options->TestingDirAuthVoteExit = routerset_none;
  2677. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2678. mock_options->TestingDirAuthVoteGuard = routerset_none;
  2679. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2680. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  2681. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2682. rs_b->is_exit = 1;
  2683. rs_b->is_possible_guard = 1;
  2684. rs_b->is_hs_dir = 1;
  2685. dirserv_set_routerstatus_testing(rs_a);
  2686. tt_assert(mock_get_options_calls == 1);
  2687. dirserv_set_routerstatus_testing(rs_b);
  2688. tt_assert(mock_get_options_calls == 2);
  2689. tt_assert(rs_a->is_exit == 0);
  2690. tt_assert(rs_a->is_possible_guard == 0);
  2691. tt_assert(rs_a->is_hs_dir == 0);
  2692. tt_assert(rs_b->is_exit == 1);
  2693. tt_assert(rs_b->is_possible_guard == 1);
  2694. tt_assert(rs_b->is_hs_dir == 1);
  2695. done:
  2696. free(mock_options);
  2697. mock_options = NULL;
  2698. UNMOCK(get_options);
  2699. routerset_free(routerset_all);
  2700. routerset_free(routerset_a);
  2701. routerset_free(routerset_none);
  2702. free(rs_a);
  2703. free(rs_b);
  2704. }
  2705. static void
  2706. test_dir_http_handling(void *args)
  2707. {
  2708. char *url = NULL;
  2709. (void)args;
  2710. /* Parse http url tests: */
  2711. /* Good headers */
  2712. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
  2713. "Host: example.com\r\n"
  2714. "User-Agent: Mozilla/5.0 (Windows;"
  2715. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2716. &url),OP_EQ, 0);
  2717. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2718. tor_free(url);
  2719. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),OP_EQ, 0);
  2720. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2721. tor_free(url);
  2722. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),
  2723. OP_EQ, 0);
  2724. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2725. tor_free(url);
  2726. /* Should prepend '/tor/' to url if required */
  2727. tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
  2728. "Host: example.com\r\n"
  2729. "User-Agent: Mozilla/5.0 (Windows;"
  2730. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2731. &url),OP_EQ, 0);
  2732. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2733. tor_free(url);
  2734. /* Bad headers -- no HTTP/1.x*/
  2735. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2736. "Host: example.com\r\n"
  2737. "User-Agent: Mozilla/5.0 (Windows;"
  2738. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2739. &url),OP_EQ, -1);
  2740. tt_assert(!url);
  2741. /* Bad headers */
  2742. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2743. "Host: example.com\r\n"
  2744. "User-Agent: Mozilla/5.0 (Windows;"
  2745. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2746. &url),OP_EQ, -1);
  2747. tt_assert(!url);
  2748. tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),OP_EQ, -1);
  2749. tt_assert(!url);
  2750. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),OP_EQ, -1);
  2751. tt_assert(!url);
  2752. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),
  2753. OP_EQ, -1);
  2754. tt_assert(!url);
  2755. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),OP_EQ, -1);
  2756. tt_assert(!url);
  2757. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),OP_EQ, -1);
  2758. tt_assert(!url);
  2759. done:
  2760. tor_free(url);
  2761. }
  2762. static void
  2763. test_dir_purpose_needs_anonymity(void *arg)
  2764. {
  2765. (void)arg;
  2766. tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE));
  2767. tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_GENERAL));
  2768. tt_int_op(0, ==, purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC,
  2769. ROUTER_PURPOSE_GENERAL));
  2770. done: ;
  2771. }
  2772. static void
  2773. test_dir_fetch_type(void *arg)
  2774. {
  2775. (void)arg;
  2776. tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
  2777. NULL) == MICRODESC_DIRINFO);
  2778. tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
  2779. NULL) == BRIDGE_DIRINFO);
  2780. tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
  2781. "microdesc") == (V3_DIRINFO | MICRODESC_DIRINFO));
  2782. done: ;
  2783. }
  2784. static void
  2785. test_dir_packages(void *arg)
  2786. {
  2787. smartlist_t *votes = smartlist_new();
  2788. char *res = NULL;
  2789. (void)arg;
  2790. #define BAD(s) \
  2791. tt_int_op(0, ==, validate_recommended_package_line(s));
  2792. #define GOOD(s) \
  2793. tt_int_op(1, ==, validate_recommended_package_line(s));
  2794. GOOD("tor 0.2.6.3-alpha "
  2795. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2796. "sha256=sssdlkfjdsklfjdskfljasdklfj");
  2797. GOOD("tor 0.2.6.3-alpha "
  2798. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2799. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
  2800. BAD("tor 0.2.6.3-alpha "
  2801. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2802. "sha256=sssdlkfjdsklfjdskfljasdklfj=");
  2803. BAD("tor 0.2.6.3-alpha "
  2804. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2805. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
  2806. BAD("tor 0.2.6.3-alpha "
  2807. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
  2808. BAD("tor 0.2.6.3-alpha "
  2809. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
  2810. BAD("tor 0.2.6.3-alpha ");
  2811. BAD("tor 0.2.6.3-alpha");
  2812. BAD("tor ");
  2813. BAD("tor");
  2814. BAD("");
  2815. BAD("=foobar sha256="
  2816. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2817. BAD("= = sha256="
  2818. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2819. BAD("sha512= sha256="
  2820. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2821. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2822. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2823. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2824. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2825. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2826. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2827. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  2828. ns->package_lines = smartlist_new());
  2829. #define ADD(i, s) \
  2830. smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
  2831. (void*)(s));
  2832. /* Only one vote for this one. */
  2833. ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
  2834. /* Only two matching entries for this one, but 3 voters */
  2835. ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
  2836. ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  2837. ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  2838. /* Only two matching entries for this one, but at least 4 voters */
  2839. ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
  2840. ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  2841. ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  2842. ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
  2843. /* This one has only invalid votes. */
  2844. ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
  2845. ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
  2846. ADD(2, "haffenreffer 1.2 ");
  2847. ADD(3, "haffenreffer ");
  2848. ADD(4, "haffenreffer");
  2849. /* Three matching votes for this; it should actually go in! */
  2850. ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  2851. ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  2852. ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  2853. ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
  2854. ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
  2855. /* Three votes for A, three votes for B */
  2856. ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  2857. ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  2858. ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  2859. ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  2860. ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  2861. ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  2862. /* Three votes for A, two votes for B */
  2863. ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  2864. ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  2865. ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  2866. ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  2867. ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  2868. /* Four votes for A, two for B. */
  2869. ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  2870. ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  2871. ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  2872. ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  2873. ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  2874. ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  2875. /* Five votes for A ... all from the same guy. Three for B. */
  2876. ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2877. ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2878. ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2879. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  2880. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  2881. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  2882. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  2883. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  2884. /* As above but new replaces old: no two match. */
  2885. ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2886. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2887. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
  2888. ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  2889. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  2890. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  2891. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  2892. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  2893. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  2894. res = compute_consensus_package_lines(votes);
  2895. tt_assert(res);
  2896. tt_str_op(res, ==,
  2897. "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
  2898. "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
  2899. "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
  2900. "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
  2901. "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
  2902. );
  2903. #undef ADD
  2904. #undef BAD
  2905. #undef GOOD
  2906. done:
  2907. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  2908. { smartlist_free(ns->package_lines); tor_free(ns); });
  2909. smartlist_free(votes);
  2910. tor_free(res);
  2911. }
  2912. static void
  2913. test_dir_download_status_schedule(void *arg)
  2914. {
  2915. (void)arg;
  2916. download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
  2917. DL_WANT_AUTHORITY,
  2918. DL_SCHED_INCREMENT_FAILURE };
  2919. download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_CONSENSUS,
  2920. DL_WANT_ANY_DIRSERVER,
  2921. DL_SCHED_INCREMENT_ATTEMPT};
  2922. download_status_t dls_bridge = { 0, 0, 0, DL_SCHED_BRIDGE,
  2923. DL_WANT_AUTHORITY,
  2924. DL_SCHED_INCREMENT_FAILURE};
  2925. int increment = -1;
  2926. int expected_increment = -1;
  2927. time_t current_time = time(NULL);
  2928. int delay1 = -1;
  2929. int delay2 = -1;
  2930. smartlist_t *schedule = smartlist_new();
  2931. /* Make a dummy schedule */
  2932. smartlist_add(schedule, (void *)&delay1);
  2933. smartlist_add(schedule, (void *)&delay2);
  2934. /* check a range of values */
  2935. delay1 = 1000;
  2936. increment = download_status_schedule_get_delay(&dls_failure,
  2937. schedule,
  2938. TIME_MIN);
  2939. expected_increment = delay1;
  2940. tt_assert(increment == expected_increment);
  2941. tt_assert(dls_failure.next_attempt_at == TIME_MIN + expected_increment);
  2942. #if TIME_T_IS_SIGNED
  2943. delay1 = INT_MAX;
  2944. increment = download_status_schedule_get_delay(&dls_failure,
  2945. schedule,
  2946. -1);
  2947. expected_increment = delay1;
  2948. tt_assert(increment == expected_increment);
  2949. tt_assert(dls_failure.next_attempt_at == TIME_MAX);
  2950. #endif
  2951. delay1 = 0;
  2952. increment = download_status_schedule_get_delay(&dls_attempt,
  2953. schedule,
  2954. 0);
  2955. expected_increment = delay1;
  2956. tt_assert(increment == expected_increment);
  2957. tt_assert(dls_attempt.next_attempt_at == 0 + expected_increment);
  2958. delay1 = 1000;
  2959. increment = download_status_schedule_get_delay(&dls_attempt,
  2960. schedule,
  2961. 1);
  2962. expected_increment = delay1;
  2963. tt_assert(increment == expected_increment);
  2964. tt_assert(dls_attempt.next_attempt_at == 1 + expected_increment);
  2965. delay1 = INT_MAX;
  2966. increment = download_status_schedule_get_delay(&dls_bridge,
  2967. schedule,
  2968. current_time);
  2969. expected_increment = delay1;
  2970. tt_assert(increment == expected_increment);
  2971. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  2972. delay1 = 1;
  2973. increment = download_status_schedule_get_delay(&dls_bridge,
  2974. schedule,
  2975. TIME_MAX);
  2976. expected_increment = delay1;
  2977. tt_assert(increment == expected_increment);
  2978. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  2979. /* see what happens when we reach the end */
  2980. dls_attempt.n_download_attempts++;
  2981. dls_bridge.n_download_failures++;
  2982. delay2 = 100;
  2983. increment = download_status_schedule_get_delay(&dls_attempt,
  2984. schedule,
  2985. current_time);
  2986. expected_increment = delay2;
  2987. tt_assert(increment == expected_increment);
  2988. tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
  2989. delay2 = 1;
  2990. increment = download_status_schedule_get_delay(&dls_bridge,
  2991. schedule,
  2992. current_time);
  2993. expected_increment = delay2;
  2994. tt_assert(increment == expected_increment);
  2995. tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
  2996. /* see what happens when we try to go off the end */
  2997. dls_attempt.n_download_attempts++;
  2998. dls_bridge.n_download_failures++;
  2999. delay2 = 5;
  3000. increment = download_status_schedule_get_delay(&dls_attempt,
  3001. schedule,
  3002. current_time);
  3003. expected_increment = delay2;
  3004. tt_assert(increment == expected_increment);
  3005. tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
  3006. delay2 = 17;
  3007. increment = download_status_schedule_get_delay(&dls_bridge,
  3008. schedule,
  3009. current_time);
  3010. expected_increment = delay2;
  3011. tt_assert(increment == expected_increment);
  3012. tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
  3013. /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
  3014. dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  3015. dls_bridge.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  3016. delay2 = 35;
  3017. increment = download_status_schedule_get_delay(&dls_attempt,
  3018. schedule,
  3019. current_time);
  3020. expected_increment = INT_MAX;
  3021. tt_assert(increment == expected_increment);
  3022. tt_assert(dls_attempt.next_attempt_at == TIME_MAX);
  3023. delay2 = 99;
  3024. increment = download_status_schedule_get_delay(&dls_bridge,
  3025. schedule,
  3026. current_time);
  3027. expected_increment = INT_MAX;
  3028. tt_assert(increment == expected_increment);
  3029. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  3030. done:
  3031. /* the pointers in schedule are allocated on the stack */
  3032. smartlist_free(schedule);
  3033. }
  3034. static void
  3035. test_dir_download_status_increment(void *arg)
  3036. {
  3037. (void)arg;
  3038. download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
  3039. DL_WANT_AUTHORITY,
  3040. DL_SCHED_INCREMENT_FAILURE };
  3041. download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_BRIDGE,
  3042. DL_WANT_ANY_DIRSERVER,
  3043. DL_SCHED_INCREMENT_ATTEMPT};
  3044. int delay0 = -1;
  3045. int delay1 = -1;
  3046. int delay2 = -1;
  3047. smartlist_t *schedule = smartlist_new();
  3048. or_options_t test_options;
  3049. time_t next_at = TIME_MAX;
  3050. time_t current_time = time(NULL);
  3051. /* Provide some values for the schedule */
  3052. delay0 = 10;
  3053. delay1 = 99;
  3054. delay2 = 20;
  3055. /* Make the schedule */
  3056. smartlist_add(schedule, (void *)&delay0);
  3057. smartlist_add(schedule, (void *)&delay1);
  3058. smartlist_add(schedule, (void *)&delay2);
  3059. /* Put it in the options */
  3060. mock_options = &test_options;
  3061. reset_options(mock_options, &mock_get_options_calls);
  3062. mock_options->TestingClientDownloadSchedule = schedule;
  3063. mock_options->TestingBridgeDownloadSchedule = schedule;
  3064. MOCK(get_options, mock_get_options);
  3065. /* Check that a failure reset works */
  3066. mock_get_options_calls = 0;
  3067. download_status_reset(&dls_failure);
  3068. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3069. * an unrealiable test, because time(NULL) might change. */
  3070. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3071. >= current_time + delay0);
  3072. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3073. != TIME_MAX);
  3074. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3075. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3076. tt_assert(mock_get_options_calls >= 1);
  3077. /* avoid timing inconsistencies */
  3078. dls_failure.next_attempt_at = current_time + delay0;
  3079. /* check that a reset schedule becomes ready at the right time */
  3080. tt_assert(download_status_is_ready(&dls_failure,
  3081. current_time + delay0 - 1,
  3082. 1) == 0);
  3083. tt_assert(download_status_is_ready(&dls_failure,
  3084. current_time + delay0,
  3085. 1) == 1);
  3086. tt_assert(download_status_is_ready(&dls_failure,
  3087. current_time + delay0 + 1,
  3088. 1) == 1);
  3089. /* Check that a failure increment works */
  3090. mock_get_options_calls = 0;
  3091. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3092. current_time);
  3093. tt_assert(next_at == current_time + delay1);
  3094. tt_assert(download_status_get_n_failures(&dls_failure) == 1);
  3095. tt_assert(download_status_get_n_attempts(&dls_failure) == 1);
  3096. tt_assert(mock_get_options_calls >= 1);
  3097. /* check that an incremented schedule becomes ready at the right time */
  3098. tt_assert(download_status_is_ready(&dls_failure,
  3099. current_time + delay1 - 1,
  3100. 1) == 0);
  3101. tt_assert(download_status_is_ready(&dls_failure,
  3102. current_time + delay1,
  3103. 1) == 1);
  3104. tt_assert(download_status_is_ready(&dls_failure,
  3105. current_time + delay1 + 1,
  3106. 1) == 1);
  3107. /* check that a schedule isn't ready if it's had too many failures */
  3108. tt_assert(download_status_is_ready(&dls_failure,
  3109. current_time + delay1 + 10,
  3110. 0) == 0);
  3111. /* Check that failure increments don't happen on 503 for clients, but that
  3112. * attempt increments do. */
  3113. mock_get_options_calls = 0;
  3114. next_at = download_status_increment_failure(&dls_failure, 503, "test", 0,
  3115. current_time);
  3116. tt_assert(next_at == current_time + delay1);
  3117. tt_assert(download_status_get_n_failures(&dls_failure) == 1);
  3118. tt_assert(download_status_get_n_attempts(&dls_failure) == 2);
  3119. tt_assert(mock_get_options_calls >= 1);
  3120. /* Check that failure increments do happen on 503 for servers */
  3121. mock_get_options_calls = 0;
  3122. next_at = download_status_increment_failure(&dls_failure, 503, "test", 1,
  3123. current_time);
  3124. tt_assert(next_at == current_time + delay2);
  3125. tt_assert(download_status_get_n_failures(&dls_failure) == 2);
  3126. tt_assert(download_status_get_n_attempts(&dls_failure) == 3);
  3127. tt_assert(mock_get_options_calls >= 1);
  3128. /* Check what happens when we run off the end of the schedule */
  3129. mock_get_options_calls = 0;
  3130. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3131. current_time);
  3132. tt_assert(next_at == current_time + delay2);
  3133. tt_assert(download_status_get_n_failures(&dls_failure) == 3);
  3134. tt_assert(download_status_get_n_attempts(&dls_failure) == 4);
  3135. tt_assert(mock_get_options_calls >= 1);
  3136. /* Check what happens when we hit the failure limit */
  3137. mock_get_options_calls = 0;
  3138. download_status_mark_impossible(&dls_failure);
  3139. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3140. current_time);
  3141. tt_assert(next_at == TIME_MAX);
  3142. tt_assert(download_status_get_n_failures(&dls_failure)
  3143. == IMPOSSIBLE_TO_DOWNLOAD);
  3144. tt_assert(download_status_get_n_attempts(&dls_failure)
  3145. == IMPOSSIBLE_TO_DOWNLOAD);
  3146. tt_assert(mock_get_options_calls >= 1);
  3147. /* Check that a failure reset doesn't reset at the limit */
  3148. mock_get_options_calls = 0;
  3149. download_status_reset(&dls_failure);
  3150. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3151. == TIME_MAX);
  3152. tt_assert(download_status_get_n_failures(&dls_failure)
  3153. == IMPOSSIBLE_TO_DOWNLOAD);
  3154. tt_assert(download_status_get_n_attempts(&dls_failure)
  3155. == IMPOSSIBLE_TO_DOWNLOAD);
  3156. tt_assert(mock_get_options_calls == 0);
  3157. /* Check that a failure reset resets just before the limit */
  3158. mock_get_options_calls = 0;
  3159. dls_failure.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3160. dls_failure.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3161. download_status_reset(&dls_failure);
  3162. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3163. * an unrealiable test, because time(NULL) might change. */
  3164. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3165. >= current_time + delay0);
  3166. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3167. != TIME_MAX);
  3168. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3169. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3170. tt_assert(mock_get_options_calls >= 1);
  3171. /* Check that failure increments do happen on attempt-based schedules,
  3172. * but that the retry is set at the end of time */
  3173. mock_get_options_calls = 0;
  3174. next_at = download_status_increment_failure(&dls_attempt, 404, "test", 0,
  3175. current_time);
  3176. tt_assert(next_at == TIME_MAX);
  3177. tt_assert(download_status_get_n_failures(&dls_attempt) == 1);
  3178. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3179. tt_assert(mock_get_options_calls == 0);
  3180. /* Check that an attempt reset works */
  3181. mock_get_options_calls = 0;
  3182. download_status_reset(&dls_attempt);
  3183. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3184. * an unrealiable test, because time(NULL) might change. */
  3185. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3186. >= current_time + delay0);
  3187. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3188. != TIME_MAX);
  3189. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3190. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3191. tt_assert(mock_get_options_calls >= 1);
  3192. /* avoid timing inconsistencies */
  3193. dls_attempt.next_attempt_at = current_time + delay0;
  3194. /* check that a reset schedule becomes ready at the right time */
  3195. tt_assert(download_status_is_ready(&dls_attempt,
  3196. current_time + delay0 - 1,
  3197. 1) == 0);
  3198. tt_assert(download_status_is_ready(&dls_attempt,
  3199. current_time + delay0,
  3200. 1) == 1);
  3201. tt_assert(download_status_is_ready(&dls_attempt,
  3202. current_time + delay0 + 1,
  3203. 1) == 1);
  3204. /* Check that an attempt increment works */
  3205. mock_get_options_calls = 0;
  3206. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3207. current_time);
  3208. tt_assert(next_at == current_time + delay1);
  3209. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3210. tt_assert(download_status_get_n_attempts(&dls_attempt) == 1);
  3211. tt_assert(mock_get_options_calls >= 1);
  3212. /* check that an incremented schedule becomes ready at the right time */
  3213. tt_assert(download_status_is_ready(&dls_attempt,
  3214. current_time + delay1 - 1,
  3215. 1) == 0);
  3216. tt_assert(download_status_is_ready(&dls_attempt,
  3217. current_time + delay1,
  3218. 1) == 1);
  3219. tt_assert(download_status_is_ready(&dls_attempt,
  3220. current_time + delay1 + 1,
  3221. 1) == 1);
  3222. /* check that a schedule isn't ready if it's had too many attempts */
  3223. tt_assert(download_status_is_ready(&dls_attempt,
  3224. current_time + delay1 + 10,
  3225. 0) == 0);
  3226. /* Check what happens when we reach then run off the end of the schedule */
  3227. mock_get_options_calls = 0;
  3228. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3229. current_time);
  3230. tt_assert(next_at == current_time + delay2);
  3231. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3232. tt_assert(download_status_get_n_attempts(&dls_attempt) == 2);
  3233. tt_assert(mock_get_options_calls >= 1);
  3234. mock_get_options_calls = 0;
  3235. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3236. current_time);
  3237. tt_assert(next_at == current_time + delay2);
  3238. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3239. tt_assert(download_status_get_n_attempts(&dls_attempt) == 3);
  3240. tt_assert(mock_get_options_calls >= 1);
  3241. /* Check what happens when we hit the attempt limit */
  3242. mock_get_options_calls = 0;
  3243. download_status_mark_impossible(&dls_attempt);
  3244. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3245. current_time);
  3246. tt_assert(next_at == TIME_MAX);
  3247. tt_assert(download_status_get_n_failures(&dls_attempt)
  3248. == IMPOSSIBLE_TO_DOWNLOAD);
  3249. tt_assert(download_status_get_n_attempts(&dls_attempt)
  3250. == IMPOSSIBLE_TO_DOWNLOAD);
  3251. tt_assert(mock_get_options_calls >= 1);
  3252. /* Check that an attempt reset doesn't reset at the limit */
  3253. mock_get_options_calls = 0;
  3254. download_status_reset(&dls_attempt);
  3255. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3256. == TIME_MAX);
  3257. tt_assert(download_status_get_n_failures(&dls_attempt)
  3258. == IMPOSSIBLE_TO_DOWNLOAD);
  3259. tt_assert(download_status_get_n_attempts(&dls_attempt)
  3260. == IMPOSSIBLE_TO_DOWNLOAD);
  3261. tt_assert(mock_get_options_calls == 0);
  3262. /* Check that an attempt reset resets just before the limit */
  3263. mock_get_options_calls = 0;
  3264. dls_attempt.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3265. dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3266. download_status_reset(&dls_attempt);
  3267. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3268. * an unrealiable test, because time(NULL) might change. */
  3269. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3270. >= current_time + delay0);
  3271. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3272. != TIME_MAX);
  3273. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3274. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3275. tt_assert(mock_get_options_calls >= 1);
  3276. /* Check that attempt increments don't happen on failure-based schedules,
  3277. * and that the attempt is set at the end of time */
  3278. mock_get_options_calls = 0;
  3279. next_at = download_status_increment_attempt(&dls_failure, "test",
  3280. current_time);
  3281. tt_assert(next_at == TIME_MAX);
  3282. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3283. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3284. tt_assert(mock_get_options_calls == 0);
  3285. done:
  3286. /* the pointers in schedule are allocated on the stack */
  3287. smartlist_free(schedule);
  3288. UNMOCK(get_options);
  3289. mock_options = NULL;
  3290. mock_get_options_calls = 0;
  3291. }
  3292. #define DIR_LEGACY(name) \
  3293. { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
  3294. #define DIR(name,flags) \
  3295. { #name, test_dir_##name, (flags), NULL, NULL }
  3296. struct testcase_t dir_tests[] = {
  3297. DIR_LEGACY(nicknames),
  3298. DIR_LEGACY(formats),
  3299. DIR(routerinfo_parsing, 0),
  3300. DIR(extrainfo_parsing, 0),
  3301. DIR(parse_router_list, TT_FORK),
  3302. DIR(load_routers, TT_FORK),
  3303. DIR(load_extrainfo, TT_FORK),
  3304. DIR_LEGACY(versions),
  3305. DIR_LEGACY(fp_pairs),
  3306. DIR(split_fps, 0),
  3307. DIR_LEGACY(measured_bw_kb),
  3308. DIR_LEGACY(measured_bw_kb_cache),
  3309. DIR_LEGACY(param_voting),
  3310. DIR_LEGACY(v3_networkstatus),
  3311. DIR(random_weighted, 0),
  3312. DIR(scale_bw, 0),
  3313. DIR_LEGACY(clip_unmeasured_bw_kb),
  3314. DIR_LEGACY(clip_unmeasured_bw_kb_alt),
  3315. DIR(fmt_control_ns, 0),
  3316. DIR(dirserv_set_routerstatus_testing, 0),
  3317. DIR(http_handling, 0),
  3318. DIR(purpose_needs_anonymity, 0),
  3319. DIR(fetch_type, 0),
  3320. DIR(packages, 0),
  3321. DIR(download_status_schedule, 0),
  3322. DIR(download_status_increment, 0),
  3323. END_OF_TESTCASES
  3324. };