test_config.c 201 KB

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