test_dir.c 156 KB

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