test_config.c 145 KB

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