test_config.c 179 KB

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