test_config.c 200 KB

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