test_config.c 204 KB

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