test_config.c 199 KB

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