test_config.c 179 KB

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