test_config.c 199 KB

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