test_config.c 199 KB

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