test_dir.c 149 KB

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