test_config.c 178 KB

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