test_config.c 189 KB

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