test_config.c 199 KB

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