test_config.c 199 KB

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