test_config.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONFIG_PRIVATE
  7. #define PT_PRIVATE
  8. #include "or.h"
  9. #include "addressmap.h"
  10. #include "config.h"
  11. #include "confparse.h"
  12. #include "connection_edge.h"
  13. #include "test.h"
  14. #include "util.h"
  15. #include "address.h"
  16. #include "entrynodes.h"
  17. #include "transports.h"
  18. #include "routerlist.h"
  19. #include "networkstatus.h"
  20. #include "router.h"
  21. #include "dirserv.h"
  22. static void
  23. test_config_addressmap(void *arg)
  24. {
  25. char buf[1024];
  26. char address[256];
  27. time_t expires = TIME_MAX;
  28. (void)arg;
  29. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  30. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  31. "MapAddress *.google.com *.torserver.exit\n"
  32. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  33. "MapAddress *.cn.com www.cnn.com\n"
  34. "MapAddress *.cnn.com www.cnn.com\n"
  35. "MapAddress ex.com www.cnn.com\n"
  36. "MapAddress ey.com *.cnn.com\n"
  37. "MapAddress www.torproject.org 1.1.1.1\n"
  38. "MapAddress other.torproject.org "
  39. "this.torproject.org.otherserver.exit\n"
  40. "MapAddress test.torproject.org 2.2.2.2\n"
  41. "MapAddress www.google.com 3.3.3.3\n"
  42. "MapAddress www.example.org 4.4.4.4\n"
  43. "MapAddress 4.4.4.4 7.7.7.7\n"
  44. "MapAddress 4.4.4.4 5.5.5.5\n"
  45. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  46. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  47. , sizeof(buf));
  48. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  49. config_register_addressmaps(get_options());
  50. /* Use old interface for now, so we don't need to rewrite the unit tests */
  51. #define addressmap_rewrite(a,s,eo,ao) \
  52. addressmap_rewrite((a),(s), ~0, (eo),(ao))
  53. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  54. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  55. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  56. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  57. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  58. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  59. /* Where no mapping for FQDN match on top-level domain */
  60. /* MapAddress .google.com .torserver.exit */
  61. strlcpy(address, "reader.google.com", sizeof(address));
  62. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  63. tt_str_op(address,OP_EQ, "reader.torserver.exit");
  64. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  65. strlcpy(address, "reader.yahoo.com", sizeof(address));
  66. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  67. tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
  68. /*MapAddress *.cnn.com www.cnn.com */
  69. strlcpy(address, "cnn.com", sizeof(address));
  70. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  71. tt_str_op(address,OP_EQ, "www.cnn.com");
  72. /* MapAddress .cn.com www.cnn.com */
  73. strlcpy(address, "www.cn.com", sizeof(address));
  74. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  75. tt_str_op(address,OP_EQ, "www.cnn.com");
  76. /* MapAddress ex.com www.cnn.com - no match */
  77. strlcpy(address, "www.ex.com", sizeof(address));
  78. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  79. /* MapAddress ey.com *.cnn.com - invalid expression */
  80. strlcpy(address, "ey.com", sizeof(address));
  81. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  82. /* Where mapping for FQDN match on FQDN */
  83. strlcpy(address, "www.google.com", sizeof(address));
  84. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  85. tt_str_op(address,OP_EQ, "3.3.3.3");
  86. strlcpy(address, "www.torproject.org", sizeof(address));
  87. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  88. tt_str_op(address,OP_EQ, "1.1.1.1");
  89. strlcpy(address, "other.torproject.org", sizeof(address));
  90. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  91. tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
  92. strlcpy(address, "test.torproject.org", sizeof(address));
  93. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  94. tt_str_op(address,OP_EQ, "2.2.2.2");
  95. /* Test a chain of address mappings and the order in which they were added:
  96. "MapAddress www.example.org 4.4.4.4"
  97. "MapAddress 4.4.4.4 7.7.7.7"
  98. "MapAddress 4.4.4.4 5.5.5.5"
  99. */
  100. strlcpy(address, "www.example.org", sizeof(address));
  101. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  102. tt_str_op(address,OP_EQ, "5.5.5.5");
  103. /* Test infinite address mapping results in no change */
  104. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  105. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  106. tt_str_op(address,OP_EQ, "www.infiniteloop.org");
  107. /* Test we don't find false positives */
  108. strlcpy(address, "www.example.com", sizeof(address));
  109. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  110. /* Test top-level-domain matching a bit harder */
  111. config_free_lines(get_options_mutable()->AddressMap);
  112. addressmap_clear_configured();
  113. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  114. "MapAddress *.torproject.org 1.1.1.1\n"
  115. "MapAddress *.net 2.2.2.2\n"
  116. , sizeof(buf));
  117. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  118. config_register_addressmaps(get_options());
  119. strlcpy(address, "www.abc.com", sizeof(address));
  120. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  121. tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
  122. strlcpy(address, "www.def.com", sizeof(address));
  123. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  124. tt_str_op(address,OP_EQ, "www.def.torserver.exit");
  125. strlcpy(address, "www.torproject.org", sizeof(address));
  126. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  127. tt_str_op(address,OP_EQ, "1.1.1.1");
  128. strlcpy(address, "test.torproject.org", sizeof(address));
  129. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  130. tt_str_op(address,OP_EQ, "1.1.1.1");
  131. strlcpy(address, "torproject.net", sizeof(address));
  132. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  133. tt_str_op(address,OP_EQ, "2.2.2.2");
  134. /* We don't support '*' as a mapping directive */
  135. config_free_lines(get_options_mutable()->AddressMap);
  136. addressmap_clear_configured();
  137. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  138. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  139. config_register_addressmaps(get_options());
  140. strlcpy(address, "www.abc.com", sizeof(address));
  141. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  142. strlcpy(address, "www.def.net", sizeof(address));
  143. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  144. strlcpy(address, "www.torproject.org", sizeof(address));
  145. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  146. #undef addressmap_rewrite
  147. done:
  148. config_free_lines(get_options_mutable()->AddressMap);
  149. get_options_mutable()->AddressMap = NULL;
  150. addressmap_free_all();
  151. }
  152. static int
  153. is_private_dir(const char* path)
  154. {
  155. struct stat st;
  156. int r = stat(path, &st);
  157. if (r) {
  158. return 0;
  159. }
  160. #if !defined (_WIN32)
  161. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  162. return 0;
  163. }
  164. #endif
  165. return 1;
  166. }
  167. static void
  168. test_config_check_or_create_data_subdir(void *arg)
  169. {
  170. or_options_t *options = get_options_mutable();
  171. char *datadir;
  172. const char *subdir = "test_stats";
  173. char *subpath;
  174. struct stat st;
  175. int r;
  176. #if !defined (_WIN32)
  177. unsigned group_permission;
  178. #endif
  179. (void)arg;
  180. tor_free(options->DataDirectory);
  181. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  182. subpath = get_datadir_fname(subdir);
  183. #if defined (_WIN32)
  184. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  185. #else
  186. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  187. #endif
  188. r = stat(subpath, &st);
  189. // The subdirectory shouldn't exist yet,
  190. // but should be created by the call to check_or_create_data_subdir.
  191. tt_assert(r && (errno == ENOENT));
  192. tt_assert(!check_or_create_data_subdir(subdir));
  193. tt_assert(is_private_dir(subpath));
  194. // The check should return 0, if the directory already exists
  195. // and is private to the user.
  196. tt_assert(!check_or_create_data_subdir(subdir));
  197. r = stat(subpath, &st);
  198. if (r) {
  199. tt_abort_perror("stat");
  200. }
  201. #if !defined (_WIN32)
  202. group_permission = st.st_mode | 0070;
  203. r = chmod(subpath, group_permission);
  204. if (r) {
  205. tt_abort_perror("chmod");
  206. }
  207. // If the directory exists, but its mode is too permissive
  208. // a call to check_or_create_data_subdir should reset the mode.
  209. tt_assert(!is_private_dir(subpath));
  210. tt_assert(!check_or_create_data_subdir(subdir));
  211. tt_assert(is_private_dir(subpath));
  212. #endif
  213. done:
  214. rmdir(subpath);
  215. tor_free(datadir);
  216. tor_free(subpath);
  217. }
  218. static void
  219. test_config_write_to_data_subdir(void *arg)
  220. {
  221. or_options_t* options = get_options_mutable();
  222. char *datadir;
  223. char *cp = NULL;
  224. const char* subdir = "test_stats";
  225. const char* fname = "test_file";
  226. const char* str =
  227. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  228. "elitr, sed diam nonumy eirmod\n"
  229. "tempor invidunt ut labore et dolore magna aliquyam\n"
  230. "erat, sed diam voluptua.\n"
  231. "At vero eos et accusam et justo duo dolores et ea\n"
  232. "rebum. Stet clita kasd gubergren,\n"
  233. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  234. "Lorem ipsum dolor sit amet,\n"
  235. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  236. "tempor invidunt ut labore et dolore\n"
  237. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  238. "accusam et justo duo dolores et\n"
  239. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  240. "sanctus est Lorem ipsum dolor sit amet.";
  241. char* filepath = NULL;
  242. (void)arg;
  243. tor_free(options->DataDirectory);
  244. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  245. filepath = get_datadir_fname2(subdir, fname);
  246. #if defined (_WIN32)
  247. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  248. #else
  249. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  250. #endif
  251. // Write attempt shoudl fail, if subdirectory doesn't exist.
  252. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  253. tt_assert(! check_or_create_data_subdir(subdir));
  254. // Content of file after write attempt should be
  255. // equal to the original string.
  256. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  257. cp = read_file_to_str(filepath, 0, NULL);
  258. tt_str_op(cp,OP_EQ, str);
  259. tor_free(cp);
  260. // A second write operation should overwrite the old content.
  261. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  262. cp = read_file_to_str(filepath, 0, NULL);
  263. tt_str_op(cp,OP_EQ, str);
  264. tor_free(cp);
  265. done:
  266. (void) unlink(filepath);
  267. rmdir(options->DataDirectory);
  268. tor_free(datadir);
  269. tor_free(filepath);
  270. tor_free(cp);
  271. }
  272. /* Test helper function: Make sure that a bridge line gets parsed
  273. * properly. Also make sure that the resulting bridge_line_t structure
  274. * has its fields set correctly. */
  275. static void
  276. good_bridge_line_test(const char *string, const char *test_addrport,
  277. const char *test_digest, const char *test_transport,
  278. const smartlist_t *test_socks_args)
  279. {
  280. char *tmp = NULL;
  281. bridge_line_t *bridge_line = parse_bridge_line(string);
  282. tt_assert(bridge_line);
  283. /* test addrport */
  284. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  285. tt_str_op(test_addrport,OP_EQ, tmp);
  286. tor_free(tmp);
  287. /* If we were asked to validate a digest, but we did not get a
  288. digest after parsing, we failed. */
  289. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  290. tt_assert(0);
  291. /* If we were not asked to validate a digest, and we got a digest
  292. after parsing, we failed again. */
  293. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  294. tt_assert(0);
  295. /* If we were asked to validate a digest, and we got a digest after
  296. parsing, make sure it's correct. */
  297. if (test_digest) {
  298. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  299. tor_strlower(tmp);
  300. tt_str_op(test_digest,OP_EQ, tmp);
  301. tor_free(tmp);
  302. }
  303. /* If we were asked to validate a transport name, make sure tha it
  304. matches with the transport name that was parsed. */
  305. if (test_transport && !bridge_line->transport_name)
  306. tt_assert(0);
  307. if (!test_transport && bridge_line->transport_name)
  308. tt_assert(0);
  309. if (test_transport)
  310. tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
  311. /* Validate the SOCKS argument smartlist. */
  312. if (test_socks_args && !bridge_line->socks_args)
  313. tt_assert(0);
  314. if (!test_socks_args && bridge_line->socks_args)
  315. tt_assert(0);
  316. if (test_socks_args)
  317. tt_assert(smartlist_strings_eq(test_socks_args,
  318. bridge_line->socks_args));
  319. done:
  320. tor_free(tmp);
  321. bridge_line_free(bridge_line);
  322. }
  323. /* Test helper function: Make sure that a bridge line is
  324. * unparseable. */
  325. static void
  326. bad_bridge_line_test(const char *string)
  327. {
  328. bridge_line_t *bridge_line = parse_bridge_line(string);
  329. if (bridge_line)
  330. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  331. tt_assert(!bridge_line);
  332. done:
  333. bridge_line_free(bridge_line);
  334. }
  335. static void
  336. test_config_parse_bridge_line(void *arg)
  337. {
  338. (void) arg;
  339. good_bridge_line_test("192.0.2.1:4123",
  340. "192.0.2.1:4123", NULL, NULL, NULL);
  341. good_bridge_line_test("192.0.2.1",
  342. "192.0.2.1:443", NULL, NULL, NULL);
  343. good_bridge_line_test("transport [::1]",
  344. "[::1]:443", NULL, "transport", NULL);
  345. good_bridge_line_test("transport 192.0.2.1:12 "
  346. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  347. "192.0.2.1:12",
  348. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  349. "transport", NULL);
  350. {
  351. smartlist_t *sl_tmp = smartlist_new();
  352. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  353. good_bridge_line_test("transport 192.0.2.1:12 "
  354. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  355. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  356. "transport", sl_tmp);
  357. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  358. smartlist_free(sl_tmp);
  359. }
  360. {
  361. smartlist_t *sl_tmp = smartlist_new();
  362. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  363. smartlist_add_asprintf(sl_tmp, "z=z");
  364. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  365. "192.0.2.1:12", NULL, "transport", sl_tmp);
  366. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  367. smartlist_free(sl_tmp);
  368. }
  369. {
  370. smartlist_t *sl_tmp = smartlist_new();
  371. smartlist_add_asprintf(sl_tmp, "dub=come");
  372. smartlist_add_asprintf(sl_tmp, "save=me");
  373. good_bridge_line_test("transport 192.0.2.1:12 "
  374. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  375. "dub=come save=me",
  376. "192.0.2.1:12",
  377. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  378. "transport", sl_tmp);
  379. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  380. smartlist_free(sl_tmp);
  381. }
  382. good_bridge_line_test("192.0.2.1:1231 "
  383. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  384. "192.0.2.1:1231",
  385. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  386. NULL, NULL);
  387. /* Empty line */
  388. bad_bridge_line_test("");
  389. /* bad transport name */
  390. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  391. /* weird ip address */
  392. bad_bridge_line_test("a.b.c.d");
  393. /* invalid fpr */
  394. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  395. /* no k=v in the end */
  396. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  397. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  398. /* no addrport */
  399. bad_bridge_line_test("asdw");
  400. /* huge k=v value that can't fit in SOCKS fields */
  401. bad_bridge_line_test(
  402. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  403. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  404. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  405. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  406. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  407. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  408. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  409. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  410. "aa=b");
  411. }
  412. static void
  413. test_config_parse_transport_options_line(void *arg)
  414. {
  415. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  416. (void) arg;
  417. { /* too small line */
  418. options_sl = get_options_from_transport_options_line("valley", NULL);
  419. tt_assert(!options_sl);
  420. }
  421. { /* no k=v values */
  422. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  423. tt_assert(!options_sl);
  424. }
  425. { /* correct line, but wrong transport specified */
  426. options_sl =
  427. get_options_from_transport_options_line("trebuchet k=v", "rook");
  428. tt_assert(!options_sl);
  429. }
  430. { /* correct -- no transport specified */
  431. sl_tmp = smartlist_new();
  432. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  433. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  434. options_sl =
  435. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  436. NULL);
  437. tt_assert(options_sl);
  438. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  439. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  440. smartlist_free(sl_tmp);
  441. sl_tmp = NULL;
  442. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  443. smartlist_free(options_sl);
  444. options_sl = NULL;
  445. }
  446. { /* correct -- correct transport specified */
  447. sl_tmp = smartlist_new();
  448. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  449. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  450. options_sl =
  451. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  452. "rook");
  453. tt_assert(options_sl);
  454. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  455. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  456. smartlist_free(sl_tmp);
  457. sl_tmp = NULL;
  458. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  459. smartlist_free(options_sl);
  460. options_sl = NULL;
  461. }
  462. done:
  463. if (options_sl) {
  464. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  465. smartlist_free(options_sl);
  466. }
  467. if (sl_tmp) {
  468. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  469. smartlist_free(sl_tmp);
  470. }
  471. }
  472. /* Mocks needed for the transport plugin line test */
  473. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  474. char **proxy_argv, int is_server);
  475. static int transport_add_from_config_mock(const tor_addr_t *addr,
  476. uint16_t port, const char *name,
  477. int socks_ver);
  478. static int transport_is_needed_mock(const char *transport_name);
  479. static int pt_kickstart_proxy_mock_call_count = 0;
  480. static int transport_add_from_config_mock_call_count = 0;
  481. static int transport_is_needed_mock_call_count = 0;
  482. static int transport_is_needed_mock_return = 0;
  483. static void
  484. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  485. char **proxy_argv, int is_server)
  486. {
  487. (void) transport_list;
  488. (void) proxy_argv;
  489. (void) is_server;
  490. /* XXXX check that args are as expected. */
  491. ++pt_kickstart_proxy_mock_call_count;
  492. free_execve_args(proxy_argv);
  493. }
  494. static int
  495. transport_add_from_config_mock(const tor_addr_t *addr,
  496. uint16_t port, const char *name,
  497. int socks_ver)
  498. {
  499. (void) addr;
  500. (void) port;
  501. (void) name;
  502. (void) socks_ver;
  503. /* XXXX check that args are as expected. */
  504. ++transport_add_from_config_mock_call_count;
  505. return 0;
  506. }
  507. static int
  508. transport_is_needed_mock(const char *transport_name)
  509. {
  510. (void) transport_name;
  511. /* XXXX check that arg is as expected. */
  512. ++transport_is_needed_mock_call_count;
  513. return transport_is_needed_mock_return;
  514. }
  515. /**
  516. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  517. * options.
  518. */
  519. static void
  520. test_config_parse_transport_plugin_line(void *arg)
  521. {
  522. (void)arg;
  523. or_options_t *options = get_options_mutable();
  524. int r, tmp;
  525. int old_pt_kickstart_proxy_mock_call_count;
  526. int old_transport_add_from_config_mock_call_count;
  527. int old_transport_is_needed_mock_call_count;
  528. /* Bad transport lines - too short */
  529. r = parse_transport_line(options, "bad", 1, 0);
  530. tt_assert(r < 0);
  531. r = parse_transport_line(options, "bad", 1, 1);
  532. tt_assert(r < 0);
  533. r = parse_transport_line(options, "bad bad", 1, 0);
  534. tt_assert(r < 0);
  535. r = parse_transport_line(options, "bad bad", 1, 1);
  536. tt_assert(r < 0);
  537. /* Test transport list parsing */
  538. r = parse_transport_line(options,
  539. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  540. tt_assert(r == 0);
  541. r = parse_transport_line(options,
  542. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  543. tt_assert(r == 0);
  544. r = parse_transport_line(options,
  545. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  546. tt_assert(r == 0);
  547. r = parse_transport_line(options,
  548. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  549. tt_assert(r == 0);
  550. /* Bad transport identifiers */
  551. r = parse_transport_line(options,
  552. "transport_* exec /usr/bin/fake-transport", 1, 0);
  553. tt_assert(r < 0);
  554. r = parse_transport_line(options,
  555. "transport_* exec /usr/bin/fake-transport", 1, 1);
  556. tt_assert(r < 0);
  557. /* Check SOCKS cases for client transport */
  558. r = parse_transport_line(options,
  559. "transport_1 socks4 1.2.3.4:567", 1, 0);
  560. tt_assert(r == 0);
  561. r = parse_transport_line(options,
  562. "transport_1 socks5 1.2.3.4:567", 1, 0);
  563. tt_assert(r == 0);
  564. /* Proxy case for server transport */
  565. r = parse_transport_line(options,
  566. "transport_1 proxy 1.2.3.4:567", 1, 1);
  567. tt_assert(r == 0);
  568. /* Multiple-transport error exit */
  569. r = parse_transport_line(options,
  570. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  571. tt_assert(r < 0);
  572. r = parse_transport_line(options,
  573. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  574. /* No port error exit */
  575. r = parse_transport_line(options,
  576. "transport_1 socks5 1.2.3.4", 1, 0);
  577. tt_assert(r < 0);
  578. r = parse_transport_line(options,
  579. "transport_1 proxy 1.2.3.4", 1, 1);
  580. tt_assert(r < 0);
  581. /* Unparsable address error exit */
  582. r = parse_transport_line(options,
  583. "transport_1 socks5 1.2.3:6x7", 1, 0);
  584. tt_assert(r < 0);
  585. r = parse_transport_line(options,
  586. "transport_1 proxy 1.2.3:6x7", 1, 1);
  587. tt_assert(r < 0);
  588. /* "Strange {Client|Server}TransportPlugin field" error exit */
  589. r = parse_transport_line(options,
  590. "transport_1 foo bar", 1, 0);
  591. tt_assert(r < 0);
  592. r = parse_transport_line(options,
  593. "transport_1 foo bar", 1, 1);
  594. tt_assert(r < 0);
  595. /* No sandbox mode error exit */
  596. tmp = options->Sandbox;
  597. options->Sandbox = 1;
  598. r = parse_transport_line(options,
  599. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  600. tt_assert(r < 0);
  601. r = parse_transport_line(options,
  602. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  603. tt_assert(r < 0);
  604. options->Sandbox = tmp;
  605. /*
  606. * These final test cases cover code paths that only activate without
  607. * validate_only, so they need mocks in place.
  608. */
  609. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  610. old_pt_kickstart_proxy_mock_call_count =
  611. pt_kickstart_proxy_mock_call_count;
  612. r = parse_transport_line(options,
  613. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  614. tt_assert(r == 0);
  615. tt_assert(pt_kickstart_proxy_mock_call_count ==
  616. old_pt_kickstart_proxy_mock_call_count + 1);
  617. UNMOCK(pt_kickstart_proxy);
  618. /* This one hits a log line in the !validate_only case only */
  619. r = parse_transport_line(options,
  620. "transport_1 proxy 1.2.3.4:567", 0, 1);
  621. tt_assert(r == 0);
  622. /* Check mocked client transport cases */
  623. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  624. MOCK(transport_add_from_config, transport_add_from_config_mock);
  625. MOCK(transport_is_needed, transport_is_needed_mock);
  626. /* Unnecessary transport case */
  627. transport_is_needed_mock_return = 0;
  628. old_pt_kickstart_proxy_mock_call_count =
  629. pt_kickstart_proxy_mock_call_count;
  630. old_transport_add_from_config_mock_call_count =
  631. transport_add_from_config_mock_call_count;
  632. old_transport_is_needed_mock_call_count =
  633. transport_is_needed_mock_call_count;
  634. r = parse_transport_line(options,
  635. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  636. /* Should have succeeded */
  637. tt_assert(r == 0);
  638. /* transport_is_needed() should have been called */
  639. tt_assert(transport_is_needed_mock_call_count ==
  640. old_transport_is_needed_mock_call_count + 1);
  641. /*
  642. * pt_kickstart_proxy() and transport_add_from_config() should
  643. * not have been called.
  644. */
  645. tt_assert(pt_kickstart_proxy_mock_call_count ==
  646. old_pt_kickstart_proxy_mock_call_count);
  647. tt_assert(transport_add_from_config_mock_call_count ==
  648. old_transport_add_from_config_mock_call_count);
  649. /* Necessary transport case */
  650. transport_is_needed_mock_return = 1;
  651. old_pt_kickstart_proxy_mock_call_count =
  652. pt_kickstart_proxy_mock_call_count;
  653. old_transport_add_from_config_mock_call_count =
  654. transport_add_from_config_mock_call_count;
  655. old_transport_is_needed_mock_call_count =
  656. transport_is_needed_mock_call_count;
  657. r = parse_transport_line(options,
  658. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  659. /* Should have succeeded */
  660. tt_assert(r == 0);
  661. /*
  662. * transport_is_needed() and pt_kickstart_proxy() should have been
  663. * called.
  664. */
  665. tt_assert(pt_kickstart_proxy_mock_call_count ==
  666. old_pt_kickstart_proxy_mock_call_count + 1);
  667. tt_assert(transport_is_needed_mock_call_count ==
  668. old_transport_is_needed_mock_call_count + 1);
  669. /* transport_add_from_config() should not have been called. */
  670. tt_assert(transport_add_from_config_mock_call_count ==
  671. old_transport_add_from_config_mock_call_count);
  672. /* proxy case */
  673. transport_is_needed_mock_return = 1;
  674. old_pt_kickstart_proxy_mock_call_count =
  675. pt_kickstart_proxy_mock_call_count;
  676. old_transport_add_from_config_mock_call_count =
  677. transport_add_from_config_mock_call_count;
  678. old_transport_is_needed_mock_call_count =
  679. transport_is_needed_mock_call_count;
  680. r = parse_transport_line(options,
  681. "transport_1 socks5 1.2.3.4:567", 0, 0);
  682. /* Should have succeeded */
  683. tt_assert(r == 0);
  684. /*
  685. * transport_is_needed() and transport_add_from_config() should have
  686. * been called.
  687. */
  688. tt_assert(transport_add_from_config_mock_call_count ==
  689. old_transport_add_from_config_mock_call_count + 1);
  690. tt_assert(transport_is_needed_mock_call_count ==
  691. old_transport_is_needed_mock_call_count + 1);
  692. /* pt_kickstart_proxy() should not have been called. */
  693. tt_assert(pt_kickstart_proxy_mock_call_count ==
  694. old_pt_kickstart_proxy_mock_call_count);
  695. /* Done with mocked client transport cases */
  696. UNMOCK(transport_is_needed);
  697. UNMOCK(transport_add_from_config);
  698. UNMOCK(pt_kickstart_proxy);
  699. done:
  700. /* Make sure we undo all mocks */
  701. UNMOCK(pt_kickstart_proxy);
  702. UNMOCK(transport_add_from_config);
  703. UNMOCK(transport_is_needed);
  704. return;
  705. }
  706. // Tests if an options with MyFamily fingerprints missing '$' normalises
  707. // them correctly and also ensure it also works with multiple fingerprints
  708. static void
  709. test_config_fix_my_family(void *arg)
  710. {
  711. char *err = NULL;
  712. const char *family = "$1111111111111111111111111111111111111111, "
  713. "1111111111111111111111111111111111111112, "
  714. "$1111111111111111111111111111111111111113";
  715. or_options_t* options = options_new();
  716. or_options_t* defaults = options_new();
  717. (void) arg;
  718. options_init(options);
  719. options_init(defaults);
  720. options->MyFamily = tor_strdup(family);
  721. options_validate(NULL, options, defaults, 0, &err) ;
  722. if (err != NULL) {
  723. TT_FAIL(("options_validate failed: %s", err));
  724. }
  725. tt_str_op(options->MyFamily,OP_EQ,
  726. "$1111111111111111111111111111111111111111, "
  727. "$1111111111111111111111111111111111111112, "
  728. "$1111111111111111111111111111111111111113");
  729. done:
  730. if (err != NULL) {
  731. tor_free(err);
  732. }
  733. or_options_free(options);
  734. or_options_free(defaults);
  735. }
  736. static int n_hostname_01010101 = 0;
  737. /** This mock function is meant to replace tor_lookup_hostname().
  738. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  739. * This function increments <b>n_hostname_01010101</b> counter by one
  740. * every time it is called.
  741. */
  742. static int
  743. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  744. {
  745. n_hostname_01010101++;
  746. if (name && addr) {
  747. *addr = ntohl(0x01010101);
  748. }
  749. return 0;
  750. }
  751. static int n_hostname_localhost = 0;
  752. /** This mock function is meant to replace tor_lookup_hostname().
  753. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  754. * This function increments <b>n_hostname_localhost</b> counter by one
  755. * every time it is called.
  756. */
  757. static int
  758. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  759. {
  760. n_hostname_localhost++;
  761. if (name && addr) {
  762. *addr = 0x7f000001;
  763. }
  764. return 0;
  765. }
  766. static int n_hostname_failure = 0;
  767. /** This mock function is meant to replace tor_lookup_hostname().
  768. * It pretends to fail by returning -1 to caller. Also, this function
  769. * increments <b>n_hostname_failure</b> every time it is called.
  770. */
  771. static int
  772. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  773. {
  774. (void)name;
  775. (void)addr;
  776. n_hostname_failure++;
  777. return -1;
  778. }
  779. static int n_gethostname_replacement = 0;
  780. /** This mock function is meant to replace tor_gethostname(). It
  781. * responds with string "onionrouter!" as hostname. This function
  782. * increments <b>n_gethostname_replacement</b> by one every time
  783. * it is called.
  784. */
  785. static int
  786. tor_gethostname_replacement(char *name, size_t namelen)
  787. {
  788. n_gethostname_replacement++;
  789. if (name && namelen) {
  790. strlcpy(name,"onionrouter!",namelen);
  791. }
  792. return 0;
  793. }
  794. static int n_gethostname_localhost = 0;
  795. /** This mock function is meant to replace tor_gethostname(). It
  796. * responds with string "127.0.0.1" as hostname. This function
  797. * increments <b>n_gethostname_localhost</b> by one every time
  798. * it is called.
  799. */
  800. static int
  801. tor_gethostname_localhost(char *name, size_t namelen)
  802. {
  803. n_gethostname_localhost++;
  804. if (name && namelen) {
  805. strlcpy(name,"127.0.0.1",namelen);
  806. }
  807. return 0;
  808. }
  809. static int n_gethostname_failure = 0;
  810. /** This mock function is meant to replace tor_gethostname.
  811. * It pretends to fail by returning -1. This function increments
  812. * <b>n_gethostname_failure</b> by one every time it is called.
  813. */
  814. static int
  815. tor_gethostname_failure(char *name, size_t namelen)
  816. {
  817. (void)name;
  818. (void)namelen;
  819. n_gethostname_failure++;
  820. return -1;
  821. }
  822. static int n_get_interface_address = 0;
  823. /** This mock function is meant to replace get_interface_address().
  824. * It answers with address 8.8.8.8. This function increments
  825. * <b>n_get_interface_address</b> by one every time it is called.
  826. */
  827. static int
  828. get_interface_address_08080808(int severity, uint32_t *addr)
  829. {
  830. (void)severity;
  831. n_get_interface_address++;
  832. if (addr) {
  833. *addr = ntohl(0x08080808);
  834. }
  835. return 0;
  836. }
  837. static int n_get_interface_address6 = 0;
  838. static sa_family_t last_address6_family;
  839. /** This mock function is meant to replace get_interface_address6().
  840. * It answers with IP address 9.9.9.9 iff both of the following are true:
  841. * - <b>family</b> is AF_INET
  842. * - <b>addr</b> pointer is not NULL.
  843. * This function increments <b>n_get_interface_address6</b> by one every
  844. * time it is called.
  845. */
  846. static int
  847. get_interface_address6_replacement(int severity, sa_family_t family,
  848. tor_addr_t *addr)
  849. {
  850. (void)severity;
  851. last_address6_family = family;
  852. n_get_interface_address6++;
  853. if ((family != AF_INET) || !addr) {
  854. return -1;
  855. }
  856. tor_addr_from_ipv4h(addr,0x09090909);
  857. return 0;
  858. }
  859. static int n_get_interface_address_failure = 0;
  860. /**
  861. * This mock function is meant to replace get_interface_address().
  862. * It pretends to fail getting interface address by returning -1.
  863. * <b>n_get_interface_address_failure</b> is incremented by one
  864. * every time this function is called.
  865. */
  866. static int
  867. get_interface_address_failure(int severity, uint32_t *addr)
  868. {
  869. (void)severity;
  870. (void)addr;
  871. n_get_interface_address_failure++;
  872. return -1;
  873. }
  874. static int n_get_interface_address6_failure = 0;
  875. /**
  876. * This mock function is meant to replace get_interface_addres6().
  877. * It will pretend to fail by return -1.
  878. * <b>n_get_interface_address6_failure</b> is incremented by one
  879. * every time this function is called and <b>last_address6_family</b>
  880. * is assigned the value of <b>family</b> argument.
  881. */
  882. static int
  883. get_interface_address6_failure(int severity, sa_family_t family,
  884. tor_addr_t *addr)
  885. {
  886. (void)severity;
  887. (void)addr;
  888. n_get_interface_address6_failure++;
  889. last_address6_family = family;
  890. return -1;
  891. }
  892. static void
  893. test_config_resolve_my_address(void *arg)
  894. {
  895. or_options_t *options;
  896. uint32_t resolved_addr;
  897. const char *method_used;
  898. char *hostname_out = NULL;
  899. int retval;
  900. int prev_n_hostname_01010101;
  901. int prev_n_hostname_localhost;
  902. int prev_n_hostname_failure;
  903. int prev_n_gethostname_replacement;
  904. int prev_n_gethostname_failure;
  905. int prev_n_gethostname_localhost;
  906. int prev_n_get_interface_address;
  907. int prev_n_get_interface_address_failure;
  908. int prev_n_get_interface_address6;
  909. int prev_n_get_interface_address6_failure;
  910. (void)arg;
  911. options = options_new();
  912. options_init(options);
  913. /*
  914. * CASE 1:
  915. * If options->Address is a valid IPv4 address string, we want
  916. * the corresponding address to be parsed and returned.
  917. */
  918. options->Address = tor_strdup("128.52.128.105");
  919. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  920. &method_used,&hostname_out);
  921. tt_want(retval == 0);
  922. tt_want_str_op(method_used,==,"CONFIGURED");
  923. tt_want(hostname_out == NULL);
  924. tt_assert(resolved_addr == 0x80348069);
  925. tor_free(options->Address);
  926. /*
  927. * CASE 2:
  928. * If options->Address is a valid DNS address, we want resolve_my_address()
  929. * function to ask tor_lookup_hostname() for help with resolving it
  930. * and return the address that was resolved (in host order).
  931. */
  932. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  933. tor_free(options->Address);
  934. options->Address = tor_strdup("www.torproject.org");
  935. prev_n_hostname_01010101 = n_hostname_01010101;
  936. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  937. &method_used,&hostname_out);
  938. tt_want(retval == 0);
  939. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  940. tt_want_str_op(method_used,==,"RESOLVED");
  941. tt_want_str_op(hostname_out,==,"www.torproject.org");
  942. tt_assert(resolved_addr == 0x01010101);
  943. UNMOCK(tor_lookup_hostname);
  944. tor_free(options->Address);
  945. tor_free(hostname_out);
  946. /*
  947. * CASE 3:
  948. * Given that options->Address is NULL, we want resolve_my_address()
  949. * to try and use tor_gethostname() to get hostname AND use
  950. * tor_lookup_hostname() to get IP address.
  951. */
  952. resolved_addr = 0;
  953. tor_free(options->Address);
  954. options->Address = NULL;
  955. MOCK(tor_gethostname,tor_gethostname_replacement);
  956. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  957. prev_n_gethostname_replacement = n_gethostname_replacement;
  958. prev_n_hostname_01010101 = n_hostname_01010101;
  959. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  960. &method_used,&hostname_out);
  961. tt_want(retval == 0);
  962. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  963. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  964. tt_want_str_op(method_used,==,"GETHOSTNAME");
  965. tt_want_str_op(hostname_out,==,"onionrouter!");
  966. tt_assert(resolved_addr == 0x01010101);
  967. UNMOCK(tor_gethostname);
  968. UNMOCK(tor_lookup_hostname);
  969. tor_free(hostname_out);
  970. /*
  971. * CASE 4:
  972. * Given that options->Address is a local host address, we want
  973. * resolve_my_address() function to fail.
  974. */
  975. resolved_addr = 0;
  976. tor_free(options->Address);
  977. options->Address = tor_strdup("127.0.0.1");
  978. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  979. &method_used,&hostname_out);
  980. tt_want(resolved_addr == 0);
  981. tt_assert(retval == -1);
  982. tor_free(options->Address);
  983. tor_free(hostname_out);
  984. /*
  985. * CASE 5:
  986. * We want resolve_my_address() to fail if DNS address in options->Address
  987. * cannot be resolved.
  988. */
  989. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  990. prev_n_hostname_failure = n_hostname_failure;
  991. tor_free(options->Address);
  992. options->Address = tor_strdup("www.tor-project.org");
  993. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  994. &method_used,&hostname_out);
  995. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  996. tt_assert(retval == -1);
  997. UNMOCK(tor_lookup_hostname);
  998. tor_free(options->Address);
  999. tor_free(hostname_out);
  1000. /*
  1001. * CASE 6:
  1002. * If options->Address is NULL AND gettting local hostname fails, we want
  1003. * resolve_my_address() to fail as well.
  1004. */
  1005. MOCK(tor_gethostname,tor_gethostname_failure);
  1006. prev_n_gethostname_failure = n_gethostname_failure;
  1007. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1008. &method_used,&hostname_out);
  1009. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1010. tt_assert(retval == -1);
  1011. UNMOCK(tor_gethostname);
  1012. tor_free(hostname_out);
  1013. /*
  1014. * CASE 7:
  1015. * We want resolve_my_address() to try and get network interface address via
  1016. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1017. * resolved into IP address.
  1018. */
  1019. MOCK(tor_gethostname,tor_gethostname_replacement);
  1020. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1021. MOCK(get_interface_address,get_interface_address_08080808);
  1022. prev_n_gethostname_replacement = n_gethostname_replacement;
  1023. prev_n_get_interface_address = n_get_interface_address;
  1024. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1025. &method_used,&hostname_out);
  1026. tt_want(retval == 0);
  1027. tt_want_int_op(n_gethostname_replacement, ==,
  1028. prev_n_gethostname_replacement + 1);
  1029. tt_want_int_op(n_get_interface_address, ==,
  1030. prev_n_get_interface_address + 1);
  1031. tt_want_str_op(method_used,==,"INTERFACE");
  1032. tt_want(hostname_out == NULL);
  1033. tt_assert(resolved_addr == 0x08080808);
  1034. UNMOCK(get_interface_address);
  1035. tor_free(hostname_out);
  1036. /*
  1037. * CASE 8:
  1038. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1039. * is unresolvable. We want resolve_my_address to fail if
  1040. * get_interface_address() fails.
  1041. */
  1042. MOCK(get_interface_address,get_interface_address_failure);
  1043. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1044. prev_n_gethostname_replacement = n_gethostname_replacement;
  1045. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1046. &method_used,&hostname_out);
  1047. tt_want(n_get_interface_address_failure ==
  1048. prev_n_get_interface_address_failure + 1);
  1049. tt_want(n_gethostname_replacement ==
  1050. prev_n_gethostname_replacement + 1);
  1051. tt_assert(retval == -1);
  1052. UNMOCK(get_interface_address);
  1053. tor_free(hostname_out);
  1054. /*
  1055. * CASE 9:
  1056. * Given that options->Address is NULL AND tor_lookup_hostname()
  1057. * fails AND hostname returned by gethostname() resolves
  1058. * to local IP address, we want resolve_my_address() function to
  1059. * call get_interface_address6(.,AF_INET,.) and return IP address
  1060. * the latter function has found.
  1061. */
  1062. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1063. MOCK(tor_gethostname,tor_gethostname_replacement);
  1064. MOCK(get_interface_address6,get_interface_address6_replacement);
  1065. prev_n_gethostname_replacement = n_gethostname_replacement;
  1066. prev_n_hostname_failure = n_hostname_failure;
  1067. prev_n_get_interface_address6 = n_get_interface_address6;
  1068. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1069. &method_used,&hostname_out);
  1070. tt_want(last_address6_family == AF_INET);
  1071. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1072. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1073. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1074. tt_want(retval == 0);
  1075. tt_want_str_op(method_used,==,"INTERFACE");
  1076. tt_assert(resolved_addr == 0x09090909);
  1077. UNMOCK(tor_lookup_hostname);
  1078. UNMOCK(tor_gethostname);
  1079. UNMOCK(get_interface_address6);
  1080. tor_free(hostname_out);
  1081. /*
  1082. * CASE 10: We want resolve_my_address() to fail if all of the following
  1083. * are true:
  1084. * 1. options->Address is not NULL
  1085. * 2. ... but it cannot be converted to struct in_addr by
  1086. * tor_inet_aton()
  1087. * 3. ... and tor_lookup_hostname() fails to resolve the
  1088. * options->Address
  1089. */
  1090. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1091. prev_n_hostname_failure = n_hostname_failure;
  1092. tor_free(options->Address);
  1093. options->Address = tor_strdup("some_hostname");
  1094. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1095. &method_used,&hostname_out);
  1096. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1097. tt_assert(retval == -1);
  1098. UNMOCK(tor_gethostname);
  1099. UNMOCK(tor_lookup_hostname);
  1100. tor_free(hostname_out);
  1101. /*
  1102. * CASE 11:
  1103. * Suppose the following sequence of events:
  1104. * 1. options->Address is NULL
  1105. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1106. * if running on.
  1107. * 3. Hostname from previous step cannot be converted to
  1108. * address by using tor_inet_aton() function.
  1109. * 4. However, tor_lookup_hostname() succeds in resolving the
  1110. * hostname from step 2.
  1111. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1112. * to be internal.
  1113. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1114. * IPv4
  1115. *
  1116. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1117. * and address from step 6.
  1118. */
  1119. tor_free(options->Address);
  1120. options->Address = NULL;
  1121. MOCK(tor_gethostname,tor_gethostname_replacement);
  1122. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1123. MOCK(get_interface_address6,get_interface_address6_replacement);
  1124. prev_n_gethostname_replacement = n_gethostname_replacement;
  1125. prev_n_hostname_localhost = n_hostname_localhost;
  1126. prev_n_get_interface_address6 = n_get_interface_address6;
  1127. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1128. &method_used,&hostname_out);
  1129. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1130. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1131. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1132. tt_str_op(method_used,==,"INTERFACE");
  1133. tt_assert(!hostname_out);
  1134. tt_assert(retval == 0);
  1135. /*
  1136. * CASE 11b:
  1137. * 1-5 as above.
  1138. * 6. get_interface_address6() fails.
  1139. *
  1140. * In this subcase, we want resolve_my_address() to fail.
  1141. */
  1142. UNMOCK(get_interface_address6);
  1143. MOCK(get_interface_address6,get_interface_address6_failure);
  1144. prev_n_gethostname_replacement = n_gethostname_replacement;
  1145. prev_n_hostname_localhost = n_hostname_localhost;
  1146. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1147. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1148. &method_used,&hostname_out);
  1149. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1150. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1151. tt_want(n_get_interface_address6_failure ==
  1152. prev_n_get_interface_address6_failure + 1);
  1153. tt_assert(retval == -1);
  1154. UNMOCK(tor_gethostname);
  1155. UNMOCK(tor_lookup_hostname);
  1156. UNMOCK(get_interface_address6);
  1157. /* CASE 12:
  1158. * Suppose the following happens:
  1159. * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
  1160. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1161. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1162. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1163. * internal.
  1164. *
  1165. * In this case, we want resolve_my_address() to fail.
  1166. */
  1167. tor_free(options->Address);
  1168. options->Address = NULL;
  1169. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1170. MOCK(tor_gethostname,tor_gethostname_localhost);
  1171. prev_n_gethostname_localhost = n_gethostname_localhost;
  1172. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1173. &method_used,&hostname_out);
  1174. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1175. tt_assert(retval == -1);
  1176. UNMOCK(tor_gethostname);
  1177. done:
  1178. tor_free(options->Address);
  1179. tor_free(options->DirAuthorities);
  1180. or_options_free(options);
  1181. tor_free(hostname_out);
  1182. UNMOCK(tor_gethostname);
  1183. UNMOCK(tor_lookup_hostname);
  1184. UNMOCK(get_interface_address);
  1185. UNMOCK(get_interface_address6);
  1186. UNMOCK(tor_gethostname);
  1187. }
  1188. static void
  1189. test_config_adding_trusted_dir_server(void *arg)
  1190. {
  1191. (void)arg;
  1192. const char digest[DIGEST_LEN] = "";
  1193. dir_server_t *ds = NULL;
  1194. tor_addr_port_t ipv6;
  1195. int rv = -1;
  1196. clear_dir_servers();
  1197. routerlist_free_all();
  1198. /* create a trusted ds without an IPv6 address and port */
  1199. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1200. NULL, V3_DIRINFO, 1.0);
  1201. tt_assert(ds);
  1202. dir_server_add(ds);
  1203. tt_assert(get_n_authorities(V3_DIRINFO) == 1);
  1204. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1205. /* create a trusted ds with an IPv6 address and port */
  1206. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1207. tt_assert(rv == 0);
  1208. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
  1209. NULL, V3_DIRINFO, 1.0);
  1210. tt_assert(ds);
  1211. dir_server_add(ds);
  1212. tt_assert(get_n_authorities(V3_DIRINFO) == 2);
  1213. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1214. done:
  1215. clear_dir_servers();
  1216. routerlist_free_all();
  1217. }
  1218. static void
  1219. test_config_adding_fallback_dir_server(void *arg)
  1220. {
  1221. (void)arg;
  1222. const char digest[DIGEST_LEN] = "";
  1223. dir_server_t *ds = NULL;
  1224. tor_addr_t ipv4;
  1225. tor_addr_port_t ipv6;
  1226. int rv = -1;
  1227. clear_dir_servers();
  1228. routerlist_free_all();
  1229. rv = tor_addr_parse(&ipv4, "127.0.0.1");
  1230. tt_assert(rv == AF_INET);
  1231. /* create a trusted ds without an IPv6 address and port */
  1232. ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
  1233. tt_assert(ds);
  1234. dir_server_add(ds);
  1235. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1236. /* create a trusted ds with an IPv6 address and port */
  1237. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1238. tt_assert(rv == 0);
  1239. ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
  1240. tt_assert(ds);
  1241. dir_server_add(ds);
  1242. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1243. done:
  1244. clear_dir_servers();
  1245. routerlist_free_all();
  1246. }
  1247. /* No secrets here:
  1248. * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1249. * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1250. * with added spaces
  1251. */
  1252. #define TEST_DIR_AUTH_LINE_START \
  1253. "foobar orport=12345 " \
  1254. "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
  1255. #define TEST_DIR_AUTH_LINE_END \
  1256. "1.2.3.4:54321 " \
  1257. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1258. #define TEST_DIR_AUTH_IPV6_FLAG \
  1259. "ipv6=[feed::beef]:9 "
  1260. static void
  1261. test_config_parsing_trusted_dir_server(void *arg)
  1262. {
  1263. (void)arg;
  1264. int rv = -1;
  1265. /* parse a trusted dir server without an IPv6 address and port */
  1266. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1267. TEST_DIR_AUTH_LINE_END,
  1268. V3_DIRINFO, 1);
  1269. tt_assert(rv == 0);
  1270. /* parse a trusted dir server with an IPv6 address and port */
  1271. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1272. TEST_DIR_AUTH_IPV6_FLAG
  1273. TEST_DIR_AUTH_LINE_END,
  1274. V3_DIRINFO, 1);
  1275. tt_assert(rv == 0);
  1276. /* Since we are only validating, there is no cleanup. */
  1277. done:
  1278. ;
  1279. }
  1280. #undef TEST_DIR_AUTH_LINE_START
  1281. #undef TEST_DIR_AUTH_LINE_END
  1282. #undef TEST_DIR_AUTH_IPV6_FLAG
  1283. /* No secrets here:
  1284. * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
  1285. */
  1286. #define TEST_DIR_FALLBACK_LINE \
  1287. "1.2.3.4:54321 orport=12345 " \
  1288. "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
  1289. #define TEST_DIR_FALLBACK_IPV6_FLAG \
  1290. "ipv6=[2015:c0de::deed]:9"
  1291. static void
  1292. test_config_parsing_fallback_dir_server(void *arg)
  1293. {
  1294. (void)arg;
  1295. int rv = -1;
  1296. /* parse a trusted dir server without an IPv6 address and port */
  1297. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
  1298. tt_assert(rv == 0);
  1299. /* parse a trusted dir server with an IPv6 address and port */
  1300. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
  1301. TEST_DIR_FALLBACK_IPV6_FLAG,
  1302. 1);
  1303. tt_assert(rv == 0);
  1304. /* Since we are only validating, there is no cleanup. */
  1305. done:
  1306. ;
  1307. }
  1308. #undef TEST_DIR_FALLBACK_LINE
  1309. #undef TEST_DIR_FALLBACK_IPV6_FLAG
  1310. static void
  1311. test_config_adding_default_trusted_dir_servers(void *arg)
  1312. {
  1313. (void)arg;
  1314. clear_dir_servers();
  1315. routerlist_free_all();
  1316. /* Assume we only have one bridge authority */
  1317. add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
  1318. tt_assert(get_n_authorities(BRIDGE_DIRINFO) == 1);
  1319. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1320. /* Assume we have nine V3 authorities */
  1321. add_default_trusted_dir_authorities(V3_DIRINFO);
  1322. tt_assert(get_n_authorities(V3_DIRINFO) == 9);
  1323. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 10);
  1324. done:
  1325. clear_dir_servers();
  1326. routerlist_free_all();
  1327. }
  1328. static int n_add_default_fallback_dir_servers_known_default = 0;
  1329. /**
  1330. * This mock function is meant to replace add_default_fallback_dir_servers().
  1331. * It will parse and add one known default fallback dir server,
  1332. * which has a dir_port of 99.
  1333. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1334. * one every time this function is called.
  1335. */
  1336. static void
  1337. add_default_fallback_dir_servers_known_default(void)
  1338. {
  1339. int i;
  1340. const char *fallback[] = {
  1341. "127.0.0.1:60099 orport=9009 "
  1342. "id=0923456789012345678901234567890123456789",
  1343. NULL
  1344. };
  1345. for (i=0; fallback[i]; i++) {
  1346. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1347. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1348. fallback[i]);
  1349. }
  1350. }
  1351. n_add_default_fallback_dir_servers_known_default++;
  1352. }
  1353. /* Test all the different combinations of adding dir servers */
  1354. static void
  1355. test_config_adding_dir_servers(void *arg)
  1356. {
  1357. (void)arg;
  1358. /* allocate options */
  1359. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  1360. /* Allocate and populate configuration lines:
  1361. *
  1362. * Use the same format as the hard-coded directories in
  1363. * add_default_trusted_dir_authorities().
  1364. * Zeroing the structure has the same effect as initialising to:
  1365. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1366. */
  1367. config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
  1368. test_dir_authority->key = tor_strdup("DirAuthority");
  1369. test_dir_authority->value = tor_strdup(
  1370. "D0 orport=9000 "
  1371. "v3ident=0023456789012345678901234567890123456789 "
  1372. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1373. );
  1374. config_line_t *test_alt_bridge_authority = tor_malloc_zero(
  1375. sizeof(config_line_t));
  1376. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1377. test_alt_bridge_authority->value = tor_strdup(
  1378. "B1 orport=9001 bridge "
  1379. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1380. );
  1381. config_line_t *test_alt_dir_authority = tor_malloc_zero(
  1382. sizeof(config_line_t));
  1383. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1384. test_alt_dir_authority->value = tor_strdup(
  1385. "A2 orport=9002 "
  1386. "v3ident=0223456789012345678901234567890123456789 "
  1387. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1388. );
  1389. /* Use the format specified in the manual page */
  1390. config_line_t *test_fallback_directory = tor_malloc_zero(
  1391. sizeof(config_line_t));
  1392. test_fallback_directory->key = tor_strdup("FallbackDir");
  1393. test_fallback_directory->value = tor_strdup(
  1394. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1395. );
  1396. /* We need to know if add_default_fallback_dir_servers is called,
  1397. * whatever the size of the list in fallback_dirs.inc,
  1398. * so we use a version of add_default_fallback_dir_servers that adds
  1399. * one known default fallback directory. */
  1400. MOCK(add_default_fallback_dir_servers,
  1401. add_default_fallback_dir_servers_known_default);
  1402. /* There are 16 different cases, covering each combination of set/NULL for:
  1403. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1404. * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
  1405. * But validate_dir_servers() ensures that:
  1406. * "You cannot set both DirAuthority and Alternate*Authority."
  1407. * This reduces the number of cases to 10.
  1408. *
  1409. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1410. * So 1001 or case 9 is:
  1411. * DirAuthorities set,
  1412. * AlternateBridgeAuthority NULL,
  1413. * AlternateDirAuthority NULL
  1414. * FallbackDir set
  1415. * The valid cases are cases 0-9 counting using this method, as every case
  1416. * greater than or equal to 10 = 1010 is invalid.
  1417. *
  1418. * 1. Outcome: Use Set Directory Authorities
  1419. * - No Default Authorities
  1420. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1421. * if they are set
  1422. * Cases expected to yield this outcome:
  1423. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1424. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1425. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1426. *
  1427. * 2. Outcome: Use Set Bridge Authority
  1428. * - Use Default Non-Bridge Directory Authorities
  1429. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1430. * Cases expected to yield this outcome:
  1431. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1432. * AlternateBridgeAuthority is set, and
  1433. * AlternateDirAuthority is NULL)
  1434. *
  1435. * 3. Outcome: Use Set Alternate Directory Authority
  1436. * - Use Default Bridge Authorities
  1437. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1438. * Cases expected to yield this outcome:
  1439. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1440. * are both NULL, but AlternateDirAuthority is set)
  1441. *
  1442. * 4. Outcome: Use Set Custom Fallback Directory
  1443. * - Use Default Bridge & Directory Authorities
  1444. * Cases expected to yield this outcome:
  1445. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1446. * are all NULL, but FallbackDir is set)
  1447. *
  1448. * 5. Outcome: Use All Defaults
  1449. * - Use Default Bridge & Directory Authorities, and
  1450. * Default Fallback Directories
  1451. * Cases expected to yield this outcome:
  1452. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1453. * and FallbackDir are all NULL)
  1454. */
  1455. /*
  1456. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1457. * are hard-coded into this build.
  1458. * This code makes some assumptions about the implementation.
  1459. * If they are wrong, one or more of cases 0-5 could fail.
  1460. */
  1461. int n_default_alt_bridge_authority = 0;
  1462. int n_default_alt_dir_authority = 0;
  1463. int n_default_fallback_dir = 0;
  1464. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1465. + (n_default_alt_dir_authority))
  1466. /* Pre-Count Number of Authorities of Each Type
  1467. * Use 0000: No Directory Authorities or Fallback Directories Set
  1468. */
  1469. {
  1470. /* clear fallback dirs counter */
  1471. n_add_default_fallback_dir_servers_known_default = 0;
  1472. /* clear options*/
  1473. memset(options, 0, sizeof(or_options_t));
  1474. /* clear any previous dir servers:
  1475. consider_adding_dir_servers() should do this anyway */
  1476. clear_dir_servers();
  1477. /* assign options: 0000 */
  1478. options->DirAuthorities = NULL;
  1479. options->AlternateBridgeAuthority = NULL;
  1480. options->AlternateDirAuthority = NULL;
  1481. options->FallbackDir = NULL;
  1482. options->UseDefaultFallbackDirs = 1;
  1483. /* parse options - ensure we always update by passing NULL old_options */
  1484. consider_adding_dir_servers(options, NULL);
  1485. /* check outcome */
  1486. /* we must have added the default fallback dirs */
  1487. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  1488. /* we have more fallbacks than just the authorities */
  1489. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1490. {
  1491. /* fallback_dir_servers */
  1492. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1493. /* Count Bridge Authorities */
  1494. SMARTLIST_FOREACH(fallback_servers,
  1495. dir_server_t *,
  1496. ds,
  1497. /* increment the found counter if it's a bridge auth */
  1498. n_default_alt_bridge_authority +=
  1499. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1500. 1 : 0)
  1501. );
  1502. /* If we have no default bridge authority, something has gone wrong */
  1503. tt_assert(n_default_alt_bridge_authority >= 1);
  1504. /* Count v3 Authorities */
  1505. SMARTLIST_FOREACH(fallback_servers,
  1506. dir_server_t *,
  1507. ds,
  1508. /* increment found counter if it's a v3 auth */
  1509. n_default_alt_dir_authority +=
  1510. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1511. 1 : 0)
  1512. );
  1513. /* If we have no default authorities, something has gone really wrong */
  1514. tt_assert(n_default_alt_dir_authority >= 1);
  1515. /* Calculate Fallback Directory Count */
  1516. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1517. n_default_alt_bridge_authority -
  1518. n_default_alt_dir_authority);
  1519. /* If we have a negative count, something has gone really wrong,
  1520. * or some authorities aren't being added as fallback directories.
  1521. * (networkstatus_consensus_can_use_extra_fallbacks depends on all
  1522. * authorities being fallback directories.) */
  1523. tt_assert(n_default_fallback_dir >= 0);
  1524. }
  1525. }
  1526. /*
  1527. * 1. Outcome: Use Set Directory Authorities
  1528. * - No Default Authorities
  1529. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1530. * if they are set
  1531. * Cases expected to yield this outcome:
  1532. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1533. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1534. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1535. */
  1536. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1537. AlternateDirAuthority Not Set, FallbackDir Set */
  1538. {
  1539. /* clear fallback dirs counter */
  1540. n_add_default_fallback_dir_servers_known_default = 0;
  1541. /* clear options*/
  1542. memset(options, 0, sizeof(or_options_t));
  1543. /* clear any previous dir servers:
  1544. consider_adding_dir_servers() should do this anyway */
  1545. clear_dir_servers();
  1546. /* assign options: 1001 */
  1547. options->DirAuthorities = test_dir_authority;
  1548. options->AlternateBridgeAuthority = NULL;
  1549. options->AlternateDirAuthority = NULL;
  1550. options->FallbackDir = test_fallback_directory;
  1551. options->UseDefaultFallbackDirs = 1;
  1552. /* parse options - ensure we always update by passing NULL old_options */
  1553. consider_adding_dir_servers(options, NULL);
  1554. /* check outcome */
  1555. /* we must not have added the default fallback dirs */
  1556. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1557. /* we have more fallbacks than just the authorities */
  1558. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1559. {
  1560. /* trusted_dir_servers */
  1561. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1562. /* D0, (No B1), (No A2) */
  1563. tt_assert(smartlist_len(dir_servers) == 1);
  1564. /* DirAuthority - D0 - dir_port: 60090 */
  1565. int found_D0 = 0;
  1566. SMARTLIST_FOREACH(dir_servers,
  1567. dir_server_t *,
  1568. ds,
  1569. /* increment the found counter if dir_port matches */
  1570. found_D0 +=
  1571. (ds->dir_port == 60090 ?
  1572. 1 : 0)
  1573. );
  1574. tt_assert(found_D0 == 1);
  1575. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1576. int found_B1 = 0;
  1577. SMARTLIST_FOREACH(dir_servers,
  1578. dir_server_t *,
  1579. ds,
  1580. /* increment the found counter if dir_port matches */
  1581. found_B1 +=
  1582. (ds->dir_port == 60091 ?
  1583. 1 : 0)
  1584. );
  1585. tt_assert(found_B1 == 0);
  1586. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1587. int found_A2 = 0;
  1588. SMARTLIST_FOREACH(dir_servers,
  1589. dir_server_t *,
  1590. ds,
  1591. /* increment the found counter if dir_port matches */
  1592. found_A2 +=
  1593. (ds->dir_port == 60092 ?
  1594. 1 : 0)
  1595. );
  1596. tt_assert(found_A2 == 0);
  1597. }
  1598. {
  1599. /* fallback_dir_servers */
  1600. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1601. /* D0, (No B1), (No A2), Custom Fallback */
  1602. tt_assert(smartlist_len(fallback_servers) == 2);
  1603. /* DirAuthority - D0 - dir_port: 60090 */
  1604. int found_D0 = 0;
  1605. SMARTLIST_FOREACH(fallback_servers,
  1606. dir_server_t *,
  1607. ds,
  1608. /* increment the found counter if dir_port matches */
  1609. found_D0 +=
  1610. (ds->dir_port == 60090 ?
  1611. 1 : 0)
  1612. );
  1613. tt_assert(found_D0 == 1);
  1614. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1615. int found_B1 = 0;
  1616. SMARTLIST_FOREACH(fallback_servers,
  1617. dir_server_t *,
  1618. ds,
  1619. /* increment the found counter if dir_port matches */
  1620. found_B1 +=
  1621. (ds->dir_port == 60091 ?
  1622. 1 : 0)
  1623. );
  1624. tt_assert(found_B1 == 0);
  1625. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1626. int found_A2 = 0;
  1627. SMARTLIST_FOREACH(fallback_servers,
  1628. dir_server_t *,
  1629. ds,
  1630. /* increment the found counter if dir_port matches */
  1631. found_A2 +=
  1632. (ds->dir_port == 60092 ?
  1633. 1 : 0)
  1634. );
  1635. tt_assert(found_A2 == 0);
  1636. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1637. int found_non_default_fallback = 0;
  1638. SMARTLIST_FOREACH(fallback_servers,
  1639. dir_server_t *,
  1640. ds,
  1641. /* increment the found counter if dir_port matches */
  1642. found_non_default_fallback +=
  1643. (ds->dir_port == 60093 ?
  1644. 1 : 0)
  1645. );
  1646. tt_assert(found_non_default_fallback == 1);
  1647. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1648. int found_default_fallback = 0;
  1649. SMARTLIST_FOREACH(fallback_servers,
  1650. dir_server_t *,
  1651. ds,
  1652. /* increment the found counter if dir_port matches */
  1653. found_default_fallback +=
  1654. (ds->dir_port == 60099 ?
  1655. 1 : 0)
  1656. );
  1657. tt_assert(found_default_fallback == 0);
  1658. }
  1659. }
  1660. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1661. {
  1662. /* clear fallback dirs counter */
  1663. n_add_default_fallback_dir_servers_known_default = 0;
  1664. /* clear options*/
  1665. memset(options, 0, sizeof(or_options_t));
  1666. /* clear any previous dir servers:
  1667. consider_adding_dir_servers() should do this anyway */
  1668. clear_dir_servers();
  1669. /* assign options: 1000 */
  1670. options->DirAuthorities = test_dir_authority;
  1671. options->AlternateBridgeAuthority = NULL;
  1672. options->AlternateDirAuthority = NULL;
  1673. options->FallbackDir = NULL;
  1674. options->UseDefaultFallbackDirs = 1;
  1675. /* parse options - ensure we always update by passing NULL old_options */
  1676. consider_adding_dir_servers(options, NULL);
  1677. /* check outcome */
  1678. /* we must not have added the default fallback dirs */
  1679. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1680. /* we just have the authorities */
  1681. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1682. {
  1683. /* trusted_dir_servers */
  1684. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1685. /* D0, (No B1), (No A2) */
  1686. tt_assert(smartlist_len(dir_servers) == 1);
  1687. /* DirAuthority - D0 - dir_port: 60090 */
  1688. int found_D0 = 0;
  1689. SMARTLIST_FOREACH(dir_servers,
  1690. dir_server_t *,
  1691. ds,
  1692. /* increment the found counter if dir_port matches */
  1693. found_D0 +=
  1694. (ds->dir_port == 60090 ?
  1695. 1 : 0)
  1696. );
  1697. tt_assert(found_D0 == 1);
  1698. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1699. int found_B1 = 0;
  1700. SMARTLIST_FOREACH(dir_servers,
  1701. dir_server_t *,
  1702. ds,
  1703. /* increment the found counter if dir_port matches */
  1704. found_B1 +=
  1705. (ds->dir_port == 60091 ?
  1706. 1 : 0)
  1707. );
  1708. tt_assert(found_B1 == 0);
  1709. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1710. int found_A2 = 0;
  1711. SMARTLIST_FOREACH(dir_servers,
  1712. dir_server_t *,
  1713. ds,
  1714. /* increment the found counter if dir_port matches */
  1715. found_A2 +=
  1716. (ds->dir_port == 60092 ?
  1717. 1 : 0)
  1718. );
  1719. tt_assert(found_A2 == 0);
  1720. }
  1721. {
  1722. /* fallback_dir_servers */
  1723. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1724. /* D0, (No B1), (No A2), (No Fallback) */
  1725. tt_assert(smartlist_len(fallback_servers) == 1);
  1726. /* DirAuthority - D0 - dir_port: 60090 */
  1727. int found_D0 = 0;
  1728. SMARTLIST_FOREACH(fallback_servers,
  1729. dir_server_t *,
  1730. ds,
  1731. /* increment the found counter if dir_port matches */
  1732. found_D0 +=
  1733. (ds->dir_port == 60090 ?
  1734. 1 : 0)
  1735. );
  1736. tt_assert(found_D0 == 1);
  1737. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1738. int found_B1 = 0;
  1739. SMARTLIST_FOREACH(fallback_servers,
  1740. dir_server_t *,
  1741. ds,
  1742. /* increment the found counter if dir_port matches */
  1743. found_B1 +=
  1744. (ds->dir_port == 60091 ?
  1745. 1 : 0)
  1746. );
  1747. tt_assert(found_B1 == 0);
  1748. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1749. int found_A2 = 0;
  1750. SMARTLIST_FOREACH(fallback_servers,
  1751. dir_server_t *,
  1752. ds,
  1753. /* increment the found counter if dir_port matches */
  1754. found_A2 +=
  1755. (ds->dir_port == 60092 ?
  1756. 1 : 0)
  1757. );
  1758. tt_assert(found_A2 == 0);
  1759. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1760. int found_non_default_fallback = 0;
  1761. SMARTLIST_FOREACH(fallback_servers,
  1762. dir_server_t *,
  1763. ds,
  1764. /* increment the found counter if dir_port matches */
  1765. found_non_default_fallback +=
  1766. (ds->dir_port == 60093 ?
  1767. 1 : 0)
  1768. );
  1769. tt_assert(found_non_default_fallback == 0);
  1770. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1771. int found_default_fallback = 0;
  1772. SMARTLIST_FOREACH(fallback_servers,
  1773. dir_server_t *,
  1774. ds,
  1775. /* increment the found counter if dir_port matches */
  1776. found_default_fallback +=
  1777. (ds->dir_port == 60099 ?
  1778. 1 : 0)
  1779. );
  1780. tt_assert(found_default_fallback == 0);
  1781. }
  1782. }
  1783. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1784. {
  1785. /* clear fallback dirs counter */
  1786. n_add_default_fallback_dir_servers_known_default = 0;
  1787. /* clear options*/
  1788. memset(options, 0, sizeof(or_options_t));
  1789. /* clear any previous dir servers:
  1790. consider_adding_dir_servers() should do this anyway */
  1791. clear_dir_servers();
  1792. /* assign options: 0111 */
  1793. options->DirAuthorities = NULL;
  1794. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1795. options->AlternateDirAuthority = test_alt_dir_authority;
  1796. options->FallbackDir = test_fallback_directory;
  1797. options->UseDefaultFallbackDirs = 1;
  1798. /* parse options - ensure we always update by passing NULL old_options */
  1799. consider_adding_dir_servers(options, NULL);
  1800. /* check outcome */
  1801. /* we must not have added the default fallback dirs */
  1802. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1803. /* we have more fallbacks than just the authorities */
  1804. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1805. {
  1806. /* trusted_dir_servers */
  1807. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1808. /* (No D0), B1, A2 */
  1809. tt_assert(smartlist_len(dir_servers) == 2);
  1810. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1811. int found_D0 = 0;
  1812. SMARTLIST_FOREACH(dir_servers,
  1813. dir_server_t *,
  1814. ds,
  1815. /* increment the found counter if dir_port matches */
  1816. found_D0 +=
  1817. (ds->dir_port == 60090 ?
  1818. 1 : 0)
  1819. );
  1820. tt_assert(found_D0 == 0);
  1821. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1822. int found_B1 = 0;
  1823. SMARTLIST_FOREACH(dir_servers,
  1824. dir_server_t *,
  1825. ds,
  1826. /* increment the found counter if dir_port matches */
  1827. found_B1 +=
  1828. (ds->dir_port == 60091 ?
  1829. 1 : 0)
  1830. );
  1831. tt_assert(found_B1 == 1);
  1832. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1833. int found_A2 = 0;
  1834. SMARTLIST_FOREACH(dir_servers,
  1835. dir_server_t *,
  1836. ds,
  1837. /* increment the found counter if dir_port matches */
  1838. found_A2 +=
  1839. (ds->dir_port == 60092 ?
  1840. 1 : 0)
  1841. );
  1842. tt_assert(found_A2 == 1);
  1843. }
  1844. {
  1845. /* fallback_dir_servers */
  1846. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1847. /* (No D0), B1, A2, Custom Fallback */
  1848. tt_assert(smartlist_len(fallback_servers) == 3);
  1849. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1850. int found_D0 = 0;
  1851. SMARTLIST_FOREACH(fallback_servers,
  1852. dir_server_t *,
  1853. ds,
  1854. /* increment the found counter if dir_port matches */
  1855. found_D0 +=
  1856. (ds->dir_port == 60090 ?
  1857. 1 : 0)
  1858. );
  1859. tt_assert(found_D0 == 0);
  1860. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1861. int found_B1 = 0;
  1862. SMARTLIST_FOREACH(fallback_servers,
  1863. dir_server_t *,
  1864. ds,
  1865. /* increment the found counter if dir_port matches */
  1866. found_B1 +=
  1867. (ds->dir_port == 60091 ?
  1868. 1 : 0)
  1869. );
  1870. tt_assert(found_B1 == 1);
  1871. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1872. int found_A2 = 0;
  1873. SMARTLIST_FOREACH(fallback_servers,
  1874. dir_server_t *,
  1875. ds,
  1876. /* increment the found counter if dir_port matches */
  1877. found_A2 +=
  1878. (ds->dir_port == 60092 ?
  1879. 1 : 0)
  1880. );
  1881. tt_assert(found_A2 == 1);
  1882. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1883. int found_non_default_fallback = 0;
  1884. SMARTLIST_FOREACH(fallback_servers,
  1885. dir_server_t *,
  1886. ds,
  1887. /* increment the found counter if dir_port matches */
  1888. found_non_default_fallback +=
  1889. (ds->dir_port == 60093 ?
  1890. 1 : 0)
  1891. );
  1892. tt_assert(found_non_default_fallback == 1);
  1893. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1894. int found_default_fallback = 0;
  1895. SMARTLIST_FOREACH(fallback_servers,
  1896. dir_server_t *,
  1897. ds,
  1898. /* increment the found counter if dir_port matches */
  1899. found_default_fallback +=
  1900. (ds->dir_port == 60099 ?
  1901. 1 : 0)
  1902. );
  1903. tt_assert(found_default_fallback == 0);
  1904. }
  1905. }
  1906. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  1907. AlternateDirAuthority Set, FallbackDir Not Set */
  1908. {
  1909. /* clear fallback dirs counter */
  1910. n_add_default_fallback_dir_servers_known_default = 0;
  1911. /* clear options*/
  1912. memset(options, 0, sizeof(or_options_t));
  1913. /* clear any previous dir servers:
  1914. consider_adding_dir_servers() should do this anyway */
  1915. clear_dir_servers();
  1916. /* assign options: 0110 */
  1917. options->DirAuthorities = NULL;
  1918. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1919. options->AlternateDirAuthority = test_alt_dir_authority;
  1920. options->FallbackDir = NULL;
  1921. options->UseDefaultFallbackDirs = 1;
  1922. /* parse options - ensure we always update by passing NULL old_options */
  1923. consider_adding_dir_servers(options, NULL);
  1924. /* check outcome */
  1925. /* we must not have added the default fallback dirs */
  1926. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1927. /* we have more fallbacks than just the authorities */
  1928. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1929. {
  1930. /* trusted_dir_servers */
  1931. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1932. /* (No D0), B1, A2 */
  1933. tt_assert(smartlist_len(dir_servers) == 2);
  1934. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1935. int found_D0 = 0;
  1936. SMARTLIST_FOREACH(dir_servers,
  1937. dir_server_t *,
  1938. ds,
  1939. /* increment the found counter if dir_port matches */
  1940. found_D0 +=
  1941. (ds->dir_port == 60090 ?
  1942. 1 : 0)
  1943. );
  1944. tt_assert(found_D0 == 0);
  1945. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1946. int found_B1 = 0;
  1947. SMARTLIST_FOREACH(dir_servers,
  1948. dir_server_t *,
  1949. ds,
  1950. /* increment the found counter if dir_port matches */
  1951. found_B1 +=
  1952. (ds->dir_port == 60091 ?
  1953. 1 : 0)
  1954. );
  1955. tt_assert(found_B1 == 1);
  1956. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1957. int found_A2 = 0;
  1958. SMARTLIST_FOREACH(dir_servers,
  1959. dir_server_t *,
  1960. ds,
  1961. /* increment the found counter if dir_port matches */
  1962. found_A2 +=
  1963. (ds->dir_port == 60092 ?
  1964. 1 : 0)
  1965. );
  1966. tt_assert(found_A2 == 1);
  1967. }
  1968. {
  1969. /* fallback_dir_servers */
  1970. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1971. /* (No D0), B1, A2, (No Fallback) */
  1972. tt_assert(smartlist_len(fallback_servers) == 2);
  1973. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1974. int found_D0 = 0;
  1975. SMARTLIST_FOREACH(fallback_servers,
  1976. dir_server_t *,
  1977. ds,
  1978. /* increment the found counter if dir_port matches */
  1979. found_D0 +=
  1980. (ds->dir_port == 60090 ?
  1981. 1 : 0)
  1982. );
  1983. tt_assert(found_D0 == 0);
  1984. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1985. int found_B1 = 0;
  1986. SMARTLIST_FOREACH(fallback_servers,
  1987. dir_server_t *,
  1988. ds,
  1989. /* increment the found counter if dir_port matches */
  1990. found_B1 +=
  1991. (ds->dir_port == 60091 ?
  1992. 1 : 0)
  1993. );
  1994. tt_assert(found_B1 == 1);
  1995. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1996. int found_A2 = 0;
  1997. SMARTLIST_FOREACH(fallback_servers,
  1998. dir_server_t *,
  1999. ds,
  2000. /* increment the found counter if dir_port matches */
  2001. found_A2 +=
  2002. (ds->dir_port == 60092 ?
  2003. 1 : 0)
  2004. );
  2005. tt_assert(found_A2 == 1);
  2006. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2007. int found_non_default_fallback = 0;
  2008. SMARTLIST_FOREACH(fallback_servers,
  2009. dir_server_t *,
  2010. ds,
  2011. /* increment the found counter if dir_port matches */
  2012. found_non_default_fallback +=
  2013. (ds->dir_port == 60093 ?
  2014. 1 : 0)
  2015. );
  2016. tt_assert(found_non_default_fallback == 0);
  2017. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2018. int found_default_fallback = 0;
  2019. SMARTLIST_FOREACH(fallback_servers,
  2020. dir_server_t *,
  2021. ds,
  2022. /* increment the found counter if dir_port matches */
  2023. found_default_fallback +=
  2024. (ds->dir_port == 60099 ?
  2025. 1 : 0)
  2026. );
  2027. tt_assert(found_default_fallback == 0);
  2028. }
  2029. }
  2030. /*
  2031. 2. Outcome: Use Set Bridge Authority
  2032. - Use Default Non-Bridge Directory Authorities
  2033. - Use FallbackDir if it is set, otherwise use default FallbackDir
  2034. Cases expected to yield this outcome:
  2035. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  2036. AlternateBridgeAuthority is set, and
  2037. AlternateDirAuthority is NULL)
  2038. */
  2039. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2040. AlternateDirAuthority Not Set, FallbackDir Set */
  2041. {
  2042. /* clear fallback dirs counter */
  2043. n_add_default_fallback_dir_servers_known_default = 0;
  2044. /* clear options*/
  2045. memset(options, 0, sizeof(or_options_t));
  2046. /* clear any previous dir servers:
  2047. consider_adding_dir_servers() should do this anyway */
  2048. clear_dir_servers();
  2049. /* assign options: 0101 */
  2050. options->DirAuthorities = NULL;
  2051. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2052. options->AlternateDirAuthority = NULL;
  2053. options->FallbackDir = test_fallback_directory;
  2054. options->UseDefaultFallbackDirs = 1;
  2055. /* parse options - ensure we always update by passing NULL old_options */
  2056. consider_adding_dir_servers(options, NULL);
  2057. /* check outcome */
  2058. /* we must not have added the default fallback dirs */
  2059. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2060. /* we have more fallbacks than just the authorities */
  2061. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2062. {
  2063. /* trusted_dir_servers */
  2064. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2065. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2066. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2067. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2068. int found_D0 = 0;
  2069. SMARTLIST_FOREACH(dir_servers,
  2070. dir_server_t *,
  2071. ds,
  2072. /* increment the found counter if dir_port matches */
  2073. found_D0 +=
  2074. (ds->dir_port == 60090 ?
  2075. 1 : 0)
  2076. );
  2077. tt_assert(found_D0 == 0);
  2078. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2079. int found_B1 = 0;
  2080. SMARTLIST_FOREACH(dir_servers,
  2081. dir_server_t *,
  2082. ds,
  2083. /* increment the found counter if dir_port matches */
  2084. found_B1 +=
  2085. (ds->dir_port == 60091 ?
  2086. 1 : 0)
  2087. );
  2088. tt_assert(found_B1 == 1);
  2089. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2090. int found_A2 = 0;
  2091. SMARTLIST_FOREACH(dir_servers,
  2092. dir_server_t *,
  2093. ds,
  2094. /* increment the found counter if dir_port matches */
  2095. found_A2 +=
  2096. (ds->dir_port == 60092 ?
  2097. 1 : 0)
  2098. );
  2099. tt_assert(found_A2 == 0);
  2100. /* There's no easy way of checking that we have included all the
  2101. * default v3 non-Bridge directory authorities, so let's assume that
  2102. * if the total count above is correct, we have the right ones.
  2103. */
  2104. }
  2105. {
  2106. /* fallback_dir_servers */
  2107. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2108. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2109. * Custom Fallback */
  2110. tt_assert(smartlist_len(fallback_servers) ==
  2111. 2 + n_default_alt_dir_authority);
  2112. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2113. int found_D0 = 0;
  2114. SMARTLIST_FOREACH(fallback_servers,
  2115. dir_server_t *,
  2116. ds,
  2117. /* increment the found counter if dir_port matches */
  2118. found_D0 +=
  2119. (ds->dir_port == 60090 ?
  2120. 1 : 0)
  2121. );
  2122. tt_assert(found_D0 == 0);
  2123. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2124. int found_B1 = 0;
  2125. SMARTLIST_FOREACH(fallback_servers,
  2126. dir_server_t *,
  2127. ds,
  2128. /* increment the found counter if dir_port matches */
  2129. found_B1 +=
  2130. (ds->dir_port == 60091 ?
  2131. 1 : 0)
  2132. );
  2133. tt_assert(found_B1 == 1);
  2134. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2135. int found_A2 = 0;
  2136. SMARTLIST_FOREACH(fallback_servers,
  2137. dir_server_t *,
  2138. ds,
  2139. /* increment the found counter if dir_port matches */
  2140. found_A2 +=
  2141. (ds->dir_port == 60092 ?
  2142. 1 : 0)
  2143. );
  2144. tt_assert(found_A2 == 0);
  2145. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2146. int found_non_default_fallback = 0;
  2147. SMARTLIST_FOREACH(fallback_servers,
  2148. dir_server_t *,
  2149. ds,
  2150. /* increment the found counter if dir_port matches */
  2151. found_non_default_fallback +=
  2152. (ds->dir_port == 60093 ?
  2153. 1 : 0)
  2154. );
  2155. tt_assert(found_non_default_fallback == 1);
  2156. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2157. int found_default_fallback = 0;
  2158. SMARTLIST_FOREACH(fallback_servers,
  2159. dir_server_t *,
  2160. ds,
  2161. /* increment the found counter if dir_port matches */
  2162. found_default_fallback +=
  2163. (ds->dir_port == 60099 ?
  2164. 1 : 0)
  2165. );
  2166. tt_assert(found_default_fallback == 0);
  2167. /* There's no easy way of checking that we have included all the
  2168. * default v3 non-Bridge directory authorities, so let's assume that
  2169. * if the total count above is correct, we have the right ones.
  2170. */
  2171. }
  2172. }
  2173. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2174. AlternateDirAuthority & FallbackDir Not Set */
  2175. {
  2176. /* clear fallback dirs counter */
  2177. n_add_default_fallback_dir_servers_known_default = 0;
  2178. /* clear options*/
  2179. memset(options, 0, sizeof(or_options_t));
  2180. /* clear any previous dir servers:
  2181. consider_adding_dir_servers() should do this anyway */
  2182. clear_dir_servers();
  2183. /* assign options: 0100 */
  2184. options->DirAuthorities = NULL;
  2185. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2186. options->AlternateDirAuthority = NULL;
  2187. options->FallbackDir = NULL;
  2188. options->UseDefaultFallbackDirs = 1;
  2189. /* parse options - ensure we always update by passing NULL old_options */
  2190. consider_adding_dir_servers(options, NULL);
  2191. /* check outcome */
  2192. /* we must have added the default fallback dirs */
  2193. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2194. /* we have more fallbacks than just the authorities */
  2195. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2196. {
  2197. /* trusted_dir_servers */
  2198. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2199. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2200. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2201. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2202. int found_D0 = 0;
  2203. SMARTLIST_FOREACH(dir_servers,
  2204. dir_server_t *,
  2205. ds,
  2206. /* increment the found counter if dir_port matches */
  2207. found_D0 +=
  2208. (ds->dir_port == 60090 ?
  2209. 1 : 0)
  2210. );
  2211. tt_assert(found_D0 == 0);
  2212. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2213. int found_B1 = 0;
  2214. SMARTLIST_FOREACH(dir_servers,
  2215. dir_server_t *,
  2216. ds,
  2217. /* increment the found counter if dir_port matches */
  2218. found_B1 +=
  2219. (ds->dir_port == 60091 ?
  2220. 1 : 0)
  2221. );
  2222. tt_assert(found_B1 == 1);
  2223. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2224. int found_A2 = 0;
  2225. SMARTLIST_FOREACH(dir_servers,
  2226. dir_server_t *,
  2227. ds,
  2228. /* increment the found counter if dir_port matches */
  2229. found_A2 +=
  2230. (ds->dir_port == 60092 ?
  2231. 1 : 0)
  2232. );
  2233. tt_assert(found_A2 == 0);
  2234. /* There's no easy way of checking that we have included all the
  2235. * default v3 non-Bridge directory authorities, so let's assume that
  2236. * if the total count above is correct, we have the right ones.
  2237. */
  2238. }
  2239. {
  2240. /* fallback_dir_servers */
  2241. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2242. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2243. * Default Fallback */
  2244. tt_assert(smartlist_len(fallback_servers) ==
  2245. 2 + n_default_alt_dir_authority);
  2246. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2247. int found_D0 = 0;
  2248. SMARTLIST_FOREACH(fallback_servers,
  2249. dir_server_t *,
  2250. ds,
  2251. /* increment the found counter if dir_port matches */
  2252. found_D0 +=
  2253. (ds->dir_port == 60090 ?
  2254. 1 : 0)
  2255. );
  2256. tt_assert(found_D0 == 0);
  2257. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2258. int found_B1 = 0;
  2259. SMARTLIST_FOREACH(fallback_servers,
  2260. dir_server_t *,
  2261. ds,
  2262. /* increment the found counter if dir_port matches */
  2263. found_B1 +=
  2264. (ds->dir_port == 60091 ?
  2265. 1 : 0)
  2266. );
  2267. tt_assert(found_B1 == 1);
  2268. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2269. int found_A2 = 0;
  2270. SMARTLIST_FOREACH(fallback_servers,
  2271. dir_server_t *,
  2272. ds,
  2273. /* increment the found counter if dir_port matches */
  2274. found_A2 +=
  2275. (ds->dir_port == 60092 ?
  2276. 1 : 0)
  2277. );
  2278. tt_assert(found_A2 == 0);
  2279. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2280. int found_non_default_fallback = 0;
  2281. SMARTLIST_FOREACH(fallback_servers,
  2282. dir_server_t *,
  2283. ds,
  2284. /* increment the found counter if dir_port matches */
  2285. found_non_default_fallback +=
  2286. (ds->dir_port == 60093 ?
  2287. 1 : 0)
  2288. );
  2289. tt_assert(found_non_default_fallback == 0);
  2290. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2291. int found_default_fallback = 0;
  2292. SMARTLIST_FOREACH(fallback_servers,
  2293. dir_server_t *,
  2294. ds,
  2295. /* increment the found counter if dir_port matches */
  2296. found_default_fallback +=
  2297. (ds->dir_port == 60099 ?
  2298. 1 : 0)
  2299. );
  2300. tt_assert(found_default_fallback == 1);
  2301. /* There's no easy way of checking that we have included all the
  2302. * default v3 non-Bridge directory authorities, so let's assume that
  2303. * if the total count above is correct, we have the right ones.
  2304. */
  2305. }
  2306. }
  2307. /*
  2308. 3. Outcome: Use Set Alternate Directory Authority
  2309. - Use Default Bridge Authorities
  2310. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2311. Cases expected to yield this outcome:
  2312. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2313. are both NULL, but AlternateDirAuthority is set)
  2314. */
  2315. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2316. AlternateDirAuthority & FallbackDir Set */
  2317. {
  2318. /* clear fallback dirs counter */
  2319. n_add_default_fallback_dir_servers_known_default = 0;
  2320. /* clear options*/
  2321. memset(options, 0, sizeof(or_options_t));
  2322. /* clear any previous dir servers:
  2323. consider_adding_dir_servers() should do this anyway */
  2324. clear_dir_servers();
  2325. /* assign options: 0011 */
  2326. options->DirAuthorities = NULL;
  2327. options->AlternateBridgeAuthority = NULL;
  2328. options->AlternateDirAuthority = test_alt_dir_authority;
  2329. options->FallbackDir = test_fallback_directory;
  2330. options->UseDefaultFallbackDirs = 1;
  2331. /* parse options - ensure we always update by passing NULL old_options */
  2332. consider_adding_dir_servers(options, NULL);
  2333. /* check outcome */
  2334. /* we must not have added the default fallback dirs */
  2335. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2336. /* we have more fallbacks than just the authorities */
  2337. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2338. {
  2339. /* trusted_dir_servers */
  2340. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2341. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2342. tt_assert(smartlist_len(dir_servers) ==
  2343. 1 + n_default_alt_bridge_authority);
  2344. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2345. int found_D0 = 0;
  2346. SMARTLIST_FOREACH(dir_servers,
  2347. dir_server_t *,
  2348. ds,
  2349. /* increment the found counter if dir_port matches */
  2350. found_D0 +=
  2351. (ds->dir_port == 60090 ?
  2352. 1 : 0)
  2353. );
  2354. tt_assert(found_D0 == 0);
  2355. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2356. int found_B1 = 0;
  2357. SMARTLIST_FOREACH(dir_servers,
  2358. dir_server_t *,
  2359. ds,
  2360. /* increment the found counter if dir_port matches */
  2361. found_B1 +=
  2362. (ds->dir_port == 60091 ?
  2363. 1 : 0)
  2364. );
  2365. tt_assert(found_B1 == 0);
  2366. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2367. int found_A2 = 0;
  2368. SMARTLIST_FOREACH(dir_servers,
  2369. dir_server_t *,
  2370. ds,
  2371. /* increment the found counter if dir_port matches */
  2372. found_A2 +=
  2373. (ds->dir_port == 60092 ?
  2374. 1 : 0)
  2375. );
  2376. tt_assert(found_A2 == 1);
  2377. /* There's no easy way of checking that we have included all the
  2378. * default Bridge authorities (except for hard-coding tonga's details),
  2379. * so let's assume that if the total count above is correct,
  2380. * we have the right ones.
  2381. */
  2382. }
  2383. {
  2384. /* fallback_dir_servers */
  2385. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2386. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2387. * Custom Fallback Directory, (No Default Fallback Directories) */
  2388. tt_assert(smartlist_len(fallback_servers) ==
  2389. 2 + n_default_alt_bridge_authority);
  2390. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2391. int found_D0 = 0;
  2392. SMARTLIST_FOREACH(fallback_servers,
  2393. dir_server_t *,
  2394. ds,
  2395. /* increment the found counter if dir_port matches */
  2396. found_D0 +=
  2397. (ds->dir_port == 60090 ?
  2398. 1 : 0)
  2399. );
  2400. tt_assert(found_D0 == 0);
  2401. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2402. int found_B1 = 0;
  2403. SMARTLIST_FOREACH(fallback_servers,
  2404. dir_server_t *,
  2405. ds,
  2406. /* increment the found counter if dir_port matches */
  2407. found_B1 +=
  2408. (ds->dir_port == 60091 ?
  2409. 1 : 0)
  2410. );
  2411. tt_assert(found_B1 == 0);
  2412. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2413. int found_A2 = 0;
  2414. SMARTLIST_FOREACH(fallback_servers,
  2415. dir_server_t *,
  2416. ds,
  2417. /* increment the found counter if dir_port matches */
  2418. found_A2 +=
  2419. (ds->dir_port == 60092 ?
  2420. 1 : 0)
  2421. );
  2422. tt_assert(found_A2 == 1);
  2423. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2424. int found_non_default_fallback = 0;
  2425. SMARTLIST_FOREACH(fallback_servers,
  2426. dir_server_t *,
  2427. ds,
  2428. /* increment the found counter if dir_port matches */
  2429. found_non_default_fallback +=
  2430. (ds->dir_port == 60093 ?
  2431. 1 : 0)
  2432. );
  2433. tt_assert(found_non_default_fallback == 1);
  2434. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2435. int found_default_fallback = 0;
  2436. SMARTLIST_FOREACH(fallback_servers,
  2437. dir_server_t *,
  2438. ds,
  2439. /* increment the found counter if dir_port matches */
  2440. found_default_fallback +=
  2441. (ds->dir_port == 60099 ?
  2442. 1 : 0)
  2443. );
  2444. tt_assert(found_default_fallback == 0);
  2445. /* There's no easy way of checking that we have included all the
  2446. * default Bridge authorities (except for hard-coding tonga's details),
  2447. * so let's assume that if the total count above is correct,
  2448. * we have the right ones.
  2449. */
  2450. }
  2451. }
  2452. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2453. AlternateDirAuthority Set, FallbackDir Not Set */
  2454. {
  2455. /* clear fallback dirs counter */
  2456. n_add_default_fallback_dir_servers_known_default = 0;
  2457. /* clear options*/
  2458. memset(options, 0, sizeof(or_options_t));
  2459. /* clear any previous dir servers:
  2460. consider_adding_dir_servers() should do this anyway */
  2461. clear_dir_servers();
  2462. /* assign options: 0010 */
  2463. options->DirAuthorities = NULL;
  2464. options->AlternateBridgeAuthority = NULL;
  2465. options->AlternateDirAuthority = test_alt_dir_authority;
  2466. options->FallbackDir = NULL;
  2467. options->UseDefaultFallbackDirs = 1;
  2468. /* parse options - ensure we always update by passing NULL old_options */
  2469. consider_adding_dir_servers(options, NULL);
  2470. /* check outcome */
  2471. /* we must not have added the default fallback dirs */
  2472. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2473. /* we just have the authorities */
  2474. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2475. {
  2476. /* trusted_dir_servers */
  2477. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2478. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2479. * No Default or Custom Fallback Directories */
  2480. tt_assert(smartlist_len(dir_servers) ==
  2481. 1 + n_default_alt_bridge_authority);
  2482. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2483. int found_D0 = 0;
  2484. SMARTLIST_FOREACH(dir_servers,
  2485. dir_server_t *,
  2486. ds,
  2487. /* increment the found counter if dir_port matches */
  2488. found_D0 +=
  2489. (ds->dir_port == 60090 ?
  2490. 1 : 0)
  2491. );
  2492. tt_assert(found_D0 == 0);
  2493. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2494. int found_B1 = 0;
  2495. SMARTLIST_FOREACH(dir_servers,
  2496. dir_server_t *,
  2497. ds,
  2498. /* increment the found counter if dir_port matches */
  2499. found_B1 +=
  2500. (ds->dir_port == 60091 ?
  2501. 1 : 0)
  2502. );
  2503. tt_assert(found_B1 == 0);
  2504. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2505. int found_A2 = 0;
  2506. SMARTLIST_FOREACH(dir_servers,
  2507. dir_server_t *,
  2508. ds,
  2509. /* increment the found counter if dir_port matches */
  2510. found_A2 +=
  2511. (ds->dir_port == 60092 ?
  2512. 1 : 0)
  2513. );
  2514. tt_assert(found_A2 == 1);
  2515. /* There's no easy way of checking that we have included all the
  2516. * default Bridge authorities (except for hard-coding tonga's details),
  2517. * so let's assume that if the total count above is correct,
  2518. * we have the right ones.
  2519. */
  2520. }
  2521. {
  2522. /* fallback_dir_servers */
  2523. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2524. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2525. * No Custom or Default Fallback Directories */
  2526. tt_assert(smartlist_len(fallback_servers) ==
  2527. 1 + n_default_alt_bridge_authority);
  2528. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2529. int found_D0 = 0;
  2530. SMARTLIST_FOREACH(fallback_servers,
  2531. dir_server_t *,
  2532. ds,
  2533. /* increment the found counter if dir_port matches */
  2534. found_D0 +=
  2535. (ds->dir_port == 60090 ?
  2536. 1 : 0)
  2537. );
  2538. tt_assert(found_D0 == 0);
  2539. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2540. int found_B1 = 0;
  2541. SMARTLIST_FOREACH(fallback_servers,
  2542. dir_server_t *,
  2543. ds,
  2544. /* increment the found counter if dir_port matches */
  2545. found_B1 +=
  2546. (ds->dir_port == 60091 ?
  2547. 1 : 0)
  2548. );
  2549. tt_assert(found_B1 == 0);
  2550. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2551. int found_A2 = 0;
  2552. SMARTLIST_FOREACH(fallback_servers,
  2553. dir_server_t *,
  2554. ds,
  2555. /* increment the found counter if dir_port matches */
  2556. found_A2 +=
  2557. (ds->dir_port == 60092 ?
  2558. 1 : 0)
  2559. );
  2560. tt_assert(found_A2 == 1);
  2561. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2562. int found_non_default_fallback = 0;
  2563. SMARTLIST_FOREACH(fallback_servers,
  2564. dir_server_t *,
  2565. ds,
  2566. /* increment the found counter if dir_port matches */
  2567. found_non_default_fallback +=
  2568. (ds->dir_port == 60093 ?
  2569. 1 : 0)
  2570. );
  2571. tt_assert(found_non_default_fallback == 0);
  2572. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2573. int found_default_fallback = 0;
  2574. SMARTLIST_FOREACH(fallback_servers,
  2575. dir_server_t *,
  2576. ds,
  2577. /* increment the found counter if dir_port matches */
  2578. found_default_fallback +=
  2579. (ds->dir_port == 60099 ?
  2580. 1 : 0)
  2581. );
  2582. tt_assert(found_default_fallback == 0);
  2583. /* There's no easy way of checking that we have included all the
  2584. * default Bridge authorities (except for hard-coding tonga's details),
  2585. * so let's assume that if the total count above is correct,
  2586. * we have the right ones.
  2587. */
  2588. }
  2589. }
  2590. /*
  2591. 4. Outcome: Use Set Custom Fallback Directory
  2592. - Use Default Bridge & Directory Authorities
  2593. Cases expected to yield this outcome:
  2594. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2595. are all NULL, but FallbackDir is set)
  2596. */
  2597. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2598. & AlternateDirAuthority Not Set, FallbackDir Set */
  2599. {
  2600. /* clear fallback dirs counter */
  2601. n_add_default_fallback_dir_servers_known_default = 0;
  2602. /* clear options*/
  2603. memset(options, 0, sizeof(or_options_t));
  2604. /* clear any previous dir servers:
  2605. consider_adding_dir_servers() should do this anyway */
  2606. clear_dir_servers();
  2607. /* assign options: 0001 */
  2608. options->DirAuthorities = NULL;
  2609. options->AlternateBridgeAuthority = NULL;
  2610. options->AlternateDirAuthority = NULL;
  2611. options->FallbackDir = test_fallback_directory;
  2612. options->UseDefaultFallbackDirs = 1;
  2613. /* parse options - ensure we always update by passing NULL old_options */
  2614. consider_adding_dir_servers(options, NULL);
  2615. /* check outcome */
  2616. /* we must not have added the default fallback dirs */
  2617. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2618. /* we have more fallbacks than just the authorities */
  2619. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2620. {
  2621. /* trusted_dir_servers */
  2622. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2623. /* (No D0), (No B1), Default Bridge Authorities,
  2624. * (No A2), Default v3 Directory Authorities */
  2625. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2626. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2627. int found_D0 = 0;
  2628. SMARTLIST_FOREACH(dir_servers,
  2629. dir_server_t *,
  2630. ds,
  2631. /* increment the found counter if dir_port matches */
  2632. found_D0 +=
  2633. (ds->dir_port == 60090 ?
  2634. 1 : 0)
  2635. );
  2636. tt_assert(found_D0 == 0);
  2637. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2638. int found_B1 = 0;
  2639. SMARTLIST_FOREACH(dir_servers,
  2640. dir_server_t *,
  2641. ds,
  2642. /* increment the found counter if dir_port matches */
  2643. found_B1 +=
  2644. (ds->dir_port == 60091 ?
  2645. 1 : 0)
  2646. );
  2647. tt_assert(found_B1 == 0);
  2648. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2649. int found_A2 = 0;
  2650. SMARTLIST_FOREACH(dir_servers,
  2651. dir_server_t *,
  2652. ds,
  2653. /* increment the found counter if dir_port matches */
  2654. found_A2 +=
  2655. (ds->dir_port == 60092 ?
  2656. 1 : 0)
  2657. );
  2658. tt_assert(found_A2 == 0);
  2659. /* There's no easy way of checking that we have included all the
  2660. * default Bridge & V3 Directory authorities, so let's assume that
  2661. * if the total count above is correct, we have the right ones.
  2662. */
  2663. }
  2664. {
  2665. /* fallback_dir_servers */
  2666. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2667. /* (No D0), (No B1), Default Bridge Authorities,
  2668. * (No A2), Default v3 Directory Authorities,
  2669. * Custom Fallback Directory, (No Default Fallback Directories) */
  2670. tt_assert(smartlist_len(fallback_servers) ==
  2671. 1 + n_default_authorities);
  2672. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2673. int found_D0 = 0;
  2674. SMARTLIST_FOREACH(fallback_servers,
  2675. dir_server_t *,
  2676. ds,
  2677. /* increment the found counter if dir_port matches */
  2678. found_D0 +=
  2679. (ds->dir_port == 60090 ?
  2680. 1 : 0)
  2681. );
  2682. tt_assert(found_D0 == 0);
  2683. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2684. int found_B1 = 0;
  2685. SMARTLIST_FOREACH(fallback_servers,
  2686. dir_server_t *,
  2687. ds,
  2688. /* increment the found counter if dir_port matches */
  2689. found_B1 +=
  2690. (ds->dir_port == 60091 ?
  2691. 1 : 0)
  2692. );
  2693. tt_assert(found_B1 == 0);
  2694. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2695. int found_A2 = 0;
  2696. SMARTLIST_FOREACH(fallback_servers,
  2697. dir_server_t *,
  2698. ds,
  2699. /* increment the found counter if dir_port matches */
  2700. found_A2 +=
  2701. (ds->dir_port == 60092 ?
  2702. 1 : 0)
  2703. );
  2704. tt_assert(found_A2 == 0);
  2705. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2706. int found_non_default_fallback = 0;
  2707. SMARTLIST_FOREACH(fallback_servers,
  2708. dir_server_t *,
  2709. ds,
  2710. /* increment the found counter if dir_port matches */
  2711. found_non_default_fallback +=
  2712. (ds->dir_port == 60093 ?
  2713. 1 : 0)
  2714. );
  2715. tt_assert(found_non_default_fallback == 1);
  2716. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2717. int found_default_fallback = 0;
  2718. SMARTLIST_FOREACH(fallback_servers,
  2719. dir_server_t *,
  2720. ds,
  2721. /* increment the found counter if dir_port matches */
  2722. found_default_fallback +=
  2723. (ds->dir_port == 60099 ?
  2724. 1 : 0)
  2725. );
  2726. tt_assert(found_default_fallback == 0);
  2727. /* There's no easy way of checking that we have included all the
  2728. * default Bridge & V3 Directory authorities, so let's assume that
  2729. * if the total count above is correct, we have the right ones.
  2730. */
  2731. }
  2732. }
  2733. /*
  2734. 5. Outcome: Use All Defaults
  2735. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2736. Cases expected to yield this outcome:
  2737. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2738. and FallbackDir are all NULL)
  2739. */
  2740. /* Case 0: 0000 - All Not Set */
  2741. {
  2742. /* clear fallback dirs counter */
  2743. n_add_default_fallback_dir_servers_known_default = 0;
  2744. /* clear options*/
  2745. memset(options, 0, sizeof(or_options_t));
  2746. /* clear any previous dir servers:
  2747. consider_adding_dir_servers() should do this anyway */
  2748. clear_dir_servers();
  2749. /* assign options: 0001 */
  2750. options->DirAuthorities = NULL;
  2751. options->AlternateBridgeAuthority = NULL;
  2752. options->AlternateDirAuthority = NULL;
  2753. options->FallbackDir = NULL;
  2754. options->UseDefaultFallbackDirs = 1;
  2755. /* parse options - ensure we always update by passing NULL old_options */
  2756. consider_adding_dir_servers(options, NULL);
  2757. /* check outcome */
  2758. /* we must have added the default fallback dirs */
  2759. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2760. /* we have more fallbacks than just the authorities */
  2761. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2762. {
  2763. /* trusted_dir_servers */
  2764. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2765. /* (No D0), (No B1), Default Bridge Authorities,
  2766. * (No A2), Default v3 Directory Authorities */
  2767. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2768. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2769. int found_D0 = 0;
  2770. SMARTLIST_FOREACH(dir_servers,
  2771. dir_server_t *,
  2772. ds,
  2773. /* increment the found counter if dir_port matches */
  2774. found_D0 +=
  2775. (ds->dir_port == 60090 ?
  2776. 1 : 0)
  2777. );
  2778. tt_assert(found_D0 == 0);
  2779. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2780. int found_B1 = 0;
  2781. SMARTLIST_FOREACH(dir_servers,
  2782. dir_server_t *,
  2783. ds,
  2784. /* increment the found counter if dir_port matches */
  2785. found_B1 +=
  2786. (ds->dir_port == 60091 ?
  2787. 1 : 0)
  2788. );
  2789. tt_assert(found_B1 == 0);
  2790. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2791. int found_A2 = 0;
  2792. SMARTLIST_FOREACH(dir_servers,
  2793. dir_server_t *,
  2794. ds,
  2795. /* increment the found counter if dir_port matches */
  2796. found_A2 +=
  2797. (ds->dir_port == 60092 ?
  2798. 1 : 0)
  2799. );
  2800. tt_assert(found_A2 == 0);
  2801. /* There's no easy way of checking that we have included all the
  2802. * default Bridge & V3 Directory authorities, so let's assume that
  2803. * if the total count above is correct, we have the right ones.
  2804. */
  2805. }
  2806. {
  2807. /* fallback_dir_servers */
  2808. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2809. /* (No D0), (No B1), Default Bridge Authorities,
  2810. * (No A2), Default v3 Directory Authorities,
  2811. * (No Custom Fallback Directory), Default Fallback Directories */
  2812. tt_assert(smartlist_len(fallback_servers) ==
  2813. n_default_authorities + n_default_fallback_dir);
  2814. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2815. int found_D0 = 0;
  2816. SMARTLIST_FOREACH(fallback_servers,
  2817. dir_server_t *,
  2818. ds,
  2819. /* increment the found counter if dir_port matches */
  2820. found_D0 +=
  2821. (ds->dir_port == 60090 ?
  2822. 1 : 0)
  2823. );
  2824. tt_assert(found_D0 == 0);
  2825. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2826. int found_B1 = 0;
  2827. SMARTLIST_FOREACH(fallback_servers,
  2828. dir_server_t *,
  2829. ds,
  2830. /* increment the found counter if dir_port matches */
  2831. found_B1 +=
  2832. (ds->dir_port == 60091 ?
  2833. 1 : 0)
  2834. );
  2835. tt_assert(found_B1 == 0);
  2836. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2837. int found_A2 = 0;
  2838. SMARTLIST_FOREACH(fallback_servers,
  2839. dir_server_t *,
  2840. ds,
  2841. /* increment the found counter if dir_port matches */
  2842. found_A2 +=
  2843. (ds->dir_port == 60092 ?
  2844. 1 : 0)
  2845. );
  2846. tt_assert(found_A2 == 0);
  2847. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2848. int found_non_default_fallback = 0;
  2849. SMARTLIST_FOREACH(fallback_servers,
  2850. dir_server_t *,
  2851. ds,
  2852. /* increment the found counter if dir_port matches */
  2853. found_non_default_fallback +=
  2854. (ds->dir_port == 60093 ?
  2855. 1 : 0)
  2856. );
  2857. tt_assert(found_non_default_fallback == 0);
  2858. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2859. int found_default_fallback = 0;
  2860. SMARTLIST_FOREACH(fallback_servers,
  2861. dir_server_t *,
  2862. ds,
  2863. /* increment the found counter if dir_port matches */
  2864. found_default_fallback +=
  2865. (ds->dir_port == 60099 ?
  2866. 1 : 0)
  2867. );
  2868. tt_assert(found_default_fallback == 1);
  2869. /* There's no easy way of checking that we have included all the
  2870. * default Bridge & V3 Directory authorities, and the default
  2871. * Fallback Directories, so let's assume that if the total count
  2872. * above is correct, we have the right ones.
  2873. */
  2874. }
  2875. }
  2876. done:
  2877. clear_dir_servers();
  2878. tor_free(test_dir_authority->key);
  2879. tor_free(test_dir_authority->value);
  2880. tor_free(test_dir_authority);
  2881. tor_free(test_alt_dir_authority->key);
  2882. tor_free(test_alt_dir_authority->value);
  2883. tor_free(test_alt_dir_authority);
  2884. tor_free(test_alt_bridge_authority->key);
  2885. tor_free(test_alt_bridge_authority->value);
  2886. tor_free(test_alt_bridge_authority);
  2887. tor_free(test_fallback_directory->key);
  2888. tor_free(test_fallback_directory->value);
  2889. tor_free(test_fallback_directory);
  2890. options->DirAuthorities = NULL;
  2891. options->AlternateBridgeAuthority = NULL;
  2892. options->AlternateDirAuthority = NULL;
  2893. options->FallbackDir = NULL;
  2894. or_options_free(options);
  2895. UNMOCK(add_default_fallback_dir_servers);
  2896. }
  2897. static void
  2898. test_config_default_dir_servers(void *arg)
  2899. {
  2900. or_options_t *opts = NULL;
  2901. (void)arg;
  2902. int trusted_count = 0;
  2903. int fallback_count = 0;
  2904. opts = tor_malloc_zero(sizeof(or_options_t));
  2905. opts->UseDefaultFallbackDirs = 0;
  2906. consider_adding_dir_servers(opts, opts);
  2907. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2908. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2909. or_options_free(opts);
  2910. opts = NULL;
  2911. /* assume a release will never go out with less than 7 authorities */
  2912. tt_assert(trusted_count >= 7);
  2913. /* if we disable the default fallbacks, there must not be any extra */
  2914. tt_assert(fallback_count == trusted_count);
  2915. opts = tor_malloc_zero(sizeof(or_options_t));
  2916. opts->UseDefaultFallbackDirs = 1;
  2917. consider_adding_dir_servers(opts, opts);
  2918. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2919. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2920. or_options_free(opts);
  2921. opts = NULL;
  2922. /* assume a release will never go out with less than 7 authorities */
  2923. tt_assert(trusted_count >= 7);
  2924. /* XX/teor - allow for default fallbacks to be added without breaking
  2925. * the unit tests. Set a minimum fallback count once the list is stable. */
  2926. tt_assert(fallback_count >= trusted_count);
  2927. done:
  2928. or_options_free(opts);
  2929. }
  2930. static int mock_router_pick_published_address_result = 0;
  2931. static int
  2932. mock_router_pick_published_address(const or_options_t *options, uint32_t *addr)
  2933. {
  2934. (void)options;
  2935. (void)addr;
  2936. return mock_router_pick_published_address_result;
  2937. }
  2938. static int mock_router_my_exit_policy_is_reject_star_result = 0;
  2939. static int
  2940. mock_router_my_exit_policy_is_reject_star(void)
  2941. {
  2942. return mock_router_my_exit_policy_is_reject_star_result;
  2943. }
  2944. static int mock_advertised_server_mode_result = 0;
  2945. static int
  2946. mock_advertised_server_mode(void)
  2947. {
  2948. return mock_advertised_server_mode_result;
  2949. }
  2950. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  2951. static const routerinfo_t *
  2952. mock_router_get_my_routerinfo(void)
  2953. {
  2954. return mock_router_get_my_routerinfo_result;
  2955. }
  2956. static void
  2957. test_config_directory_fetch(void *arg)
  2958. {
  2959. (void)arg;
  2960. /* Test Setup */
  2961. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  2962. routerinfo_t routerinfo;
  2963. memset(&routerinfo, 0, sizeof(routerinfo));
  2964. mock_router_pick_published_address_result = -1;
  2965. mock_router_my_exit_policy_is_reject_star_result = 1;
  2966. mock_advertised_server_mode_result = 0;
  2967. mock_router_get_my_routerinfo_result = NULL;
  2968. MOCK(router_pick_published_address, mock_router_pick_published_address);
  2969. MOCK(router_my_exit_policy_is_reject_star,
  2970. mock_router_my_exit_policy_is_reject_star);
  2971. MOCK(advertised_server_mode, mock_advertised_server_mode);
  2972. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  2973. /* Clients can use multiple directory mirrors for bootstrap */
  2974. memset(options, 0, sizeof(or_options_t));
  2975. options->ClientOnly = 1;
  2976. tt_assert(server_mode(options) == 0);
  2977. tt_assert(public_server_mode(options) == 0);
  2978. tt_assert(directory_fetches_from_authorities(options) == 0);
  2979. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2980. == 1);
  2981. /* Bridge Clients can use multiple directory mirrors for bootstrap */
  2982. memset(options, 0, sizeof(or_options_t));
  2983. options->UseBridges = 1;
  2984. tt_assert(server_mode(options) == 0);
  2985. tt_assert(public_server_mode(options) == 0);
  2986. tt_assert(directory_fetches_from_authorities(options) == 0);
  2987. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2988. == 1);
  2989. /* Bridge Relays (Bridges) must act like clients, and use multiple
  2990. * directory mirrors for bootstrap */
  2991. memset(options, 0, sizeof(or_options_t));
  2992. options->BridgeRelay = 1;
  2993. options->ORPort_set = 1;
  2994. tt_assert(server_mode(options) == 1);
  2995. tt_assert(public_server_mode(options) == 0);
  2996. tt_assert(directory_fetches_from_authorities(options) == 0);
  2997. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2998. == 1);
  2999. /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
  3000. * but can use multiple authorities for bootstrap */
  3001. memset(options, 0, sizeof(or_options_t));
  3002. options->FetchDirInfoEarly = 1;
  3003. tt_assert(server_mode(options) == 0);
  3004. tt_assert(public_server_mode(options) == 0);
  3005. tt_assert(directory_fetches_from_authorities(options) == 1);
  3006. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3007. == 1);
  3008. /* OR servers only fetch the consensus from the authorities when they don't
  3009. * know their own address, but never use multiple directories for bootstrap
  3010. */
  3011. memset(options, 0, sizeof(or_options_t));
  3012. options->ORPort_set = 1;
  3013. mock_router_pick_published_address_result = -1;
  3014. tt_assert(server_mode(options) == 1);
  3015. tt_assert(public_server_mode(options) == 1);
  3016. tt_assert(directory_fetches_from_authorities(options) == 1);
  3017. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3018. == 0);
  3019. mock_router_pick_published_address_result = 0;
  3020. tt_assert(server_mode(options) == 1);
  3021. tt_assert(public_server_mode(options) == 1);
  3022. tt_assert(directory_fetches_from_authorities(options) == 0);
  3023. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3024. == 0);
  3025. /* Exit OR servers only fetch the consensus from the authorities when they
  3026. * refuse unknown exits, but never use multiple directories for bootstrap
  3027. */
  3028. memset(options, 0, sizeof(or_options_t));
  3029. options->ORPort_set = 1;
  3030. options->ExitRelay = 1;
  3031. mock_router_pick_published_address_result = 0;
  3032. mock_router_my_exit_policy_is_reject_star_result = 0;
  3033. mock_advertised_server_mode_result = 1;
  3034. mock_router_get_my_routerinfo_result = &routerinfo;
  3035. routerinfo.supports_tunnelled_dir_requests = 1;
  3036. options->RefuseUnknownExits = 1;
  3037. tt_assert(server_mode(options) == 1);
  3038. tt_assert(public_server_mode(options) == 1);
  3039. tt_assert(directory_fetches_from_authorities(options) == 1);
  3040. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3041. == 0);
  3042. options->RefuseUnknownExits = 0;
  3043. mock_router_pick_published_address_result = 0;
  3044. tt_assert(server_mode(options) == 1);
  3045. tt_assert(public_server_mode(options) == 1);
  3046. tt_assert(directory_fetches_from_authorities(options) == 0);
  3047. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3048. == 0);
  3049. /* Dir servers fetch the consensus from the authorities, unless they are not
  3050. * advertising themselves (hibernating) or have no routerinfo or are not
  3051. * advertising their dirport, and never use multiple directories for
  3052. * bootstrap. This only applies if they are also OR servers.
  3053. * (We don't care much about the behaviour of non-OR directory servers.) */
  3054. memset(options, 0, sizeof(or_options_t));
  3055. options->DirPort_set = 1;
  3056. options->ORPort_set = 1;
  3057. options->DirCache = 1;
  3058. mock_router_pick_published_address_result = 0;
  3059. mock_router_my_exit_policy_is_reject_star_result = 1;
  3060. mock_advertised_server_mode_result = 1;
  3061. routerinfo.dir_port = 1;
  3062. mock_router_get_my_routerinfo_result = &routerinfo;
  3063. tt_assert(server_mode(options) == 1);
  3064. tt_assert(public_server_mode(options) == 1);
  3065. tt_assert(directory_fetches_from_authorities(options) == 1);
  3066. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3067. == 0);
  3068. mock_advertised_server_mode_result = 0;
  3069. routerinfo.dir_port = 1;
  3070. mock_router_get_my_routerinfo_result = &routerinfo;
  3071. tt_assert(server_mode(options) == 1);
  3072. tt_assert(public_server_mode(options) == 1);
  3073. tt_assert(directory_fetches_from_authorities(options) == 0);
  3074. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3075. == 0);
  3076. mock_advertised_server_mode_result = 1;
  3077. mock_router_get_my_routerinfo_result = NULL;
  3078. tt_assert(server_mode(options) == 1);
  3079. tt_assert(public_server_mode(options) == 1);
  3080. tt_assert(directory_fetches_from_authorities(options) == 0);
  3081. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3082. == 0);
  3083. mock_advertised_server_mode_result = 1;
  3084. routerinfo.dir_port = 0;
  3085. routerinfo.supports_tunnelled_dir_requests = 0;
  3086. mock_router_get_my_routerinfo_result = &routerinfo;
  3087. tt_assert(server_mode(options) == 1);
  3088. tt_assert(public_server_mode(options) == 1);
  3089. tt_assert(directory_fetches_from_authorities(options) == 0);
  3090. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3091. == 0);
  3092. mock_advertised_server_mode_result = 1;
  3093. routerinfo.dir_port = 1;
  3094. routerinfo.supports_tunnelled_dir_requests = 1;
  3095. mock_router_get_my_routerinfo_result = &routerinfo;
  3096. tt_assert(server_mode(options) == 1);
  3097. tt_assert(public_server_mode(options) == 1);
  3098. tt_assert(directory_fetches_from_authorities(options) == 1);
  3099. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3100. == 0);
  3101. done:
  3102. tor_free(options);
  3103. UNMOCK(router_pick_published_address);
  3104. UNMOCK(router_get_my_routerinfo);
  3105. UNMOCK(advertised_server_mode);
  3106. UNMOCK(router_my_exit_policy_is_reject_star);
  3107. }
  3108. static void
  3109. test_config_default_fallback_dirs(void *arg)
  3110. {
  3111. const char *fallback[] = {
  3112. #include "../or/fallback_dirs.inc"
  3113. NULL
  3114. };
  3115. int n_included_fallback_dirs = 0;
  3116. int n_added_fallback_dirs = 0;
  3117. (void)arg;
  3118. clear_dir_servers();
  3119. while (fallback[n_included_fallback_dirs])
  3120. n_included_fallback_dirs++;
  3121. add_default_fallback_dir_servers();
  3122. n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
  3123. tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
  3124. done:
  3125. clear_dir_servers();
  3126. }
  3127. #define CONFIG_TEST(name, flags) \
  3128. { #name, test_config_ ## name, flags, NULL, NULL }
  3129. struct testcase_t config_tests[] = {
  3130. CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
  3131. CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
  3132. CONFIG_TEST(parsing_trusted_dir_server, 0),
  3133. CONFIG_TEST(parsing_fallback_dir_server, 0),
  3134. CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
  3135. CONFIG_TEST(adding_dir_servers, TT_FORK),
  3136. CONFIG_TEST(default_dir_servers, TT_FORK),
  3137. CONFIG_TEST(default_fallback_dirs, 0),
  3138. CONFIG_TEST(resolve_my_address, TT_FORK),
  3139. CONFIG_TEST(addressmap, 0),
  3140. CONFIG_TEST(parse_bridge_line, 0),
  3141. CONFIG_TEST(parse_transport_options_line, 0),
  3142. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  3143. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  3144. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  3145. CONFIG_TEST(fix_my_family, 0),
  3146. CONFIG_TEST(directory_fetch, 0),
  3147. END_OF_TESTCASES
  3148. };