test_config.c 190 KB

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