test_config.c 197 KB

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