test_dir.c 223 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include <math.h>
  7. #define BWAUTH_PRIVATE
  8. #define CONFIG_PRIVATE
  9. #define CONTROL_PRIVATE
  10. #define DIRCACHE_PRIVATE
  11. #define DIRCLIENT_PRIVATE
  12. #define DIRSERV_PRIVATE
  13. #define DIRVOTE_PRIVATE
  14. #define DLSTATUS_PRIVATE
  15. #define HIBERNATE_PRIVATE
  16. #define NETWORKSTATUS_PRIVATE
  17. #define NS_PARSE_PRIVATE
  18. #define NODE_SELECT_PRIVATE
  19. #define RELAY_PRIVATE
  20. #define ROUTERLIST_PRIVATE
  21. #define ROUTER_PRIVATE
  22. #define UNPARSEABLE_PRIVATE
  23. #define VOTEFLAGS_PRIVATE
  24. #include "core/or/or.h"
  25. #include "app/config/config.h"
  26. #include "app/config/confparse.h"
  27. #include "core/mainloop/connection.h"
  28. #include "core/or/relay.h"
  29. #include "core/or/versions.h"
  30. #include "feature/client/bridges.h"
  31. #include "feature/client/entrynodes.h"
  32. #include "feature/control/control.h"
  33. #include "feature/dirauth/bwauth.h"
  34. #include "feature/dirauth/dirvote.h"
  35. #include "feature/dirauth/dsigs_parse.h"
  36. #include "feature/dirauth/process_descs.h"
  37. #include "feature/dirauth/recommend_pkg.h"
  38. #include "feature/dirauth/shared_random_state.h"
  39. #include "feature/dirauth/voteflags.h"
  40. #include "feature/dircache/dircache.h"
  41. #include "feature/dircache/dirserv.h"
  42. #include "feature/dirclient/dirclient.h"
  43. #include "feature/dirclient/dlstatus.h"
  44. #include "feature/dircommon/directory.h"
  45. #include "feature/dircommon/fp_pair.h"
  46. #include "feature/dircommon/voting_schedule.h"
  47. #include "feature/hibernate/hibernate.h"
  48. #include "feature/nodelist/authcert.h"
  49. #include "feature/nodelist/dirlist.h"
  50. #include "feature/nodelist/networkstatus.h"
  51. #include "feature/nodelist/nickname.h"
  52. #include "feature/nodelist/node_select.h"
  53. #include "feature/nodelist/routerlist.h"
  54. #include "feature/dirparse/authcert_parse.h"
  55. #include "feature/dirparse/ns_parse.h"
  56. #include "feature/dirparse/routerparse.h"
  57. #include "feature/dirparse/unparseable.h"
  58. #include "feature/nodelist/routerset.h"
  59. #include "feature/nodelist/torcert.h"
  60. #include "feature/relay/router.h"
  61. #include "feature/relay/routerkeys.h"
  62. #include "feature/relay/routermode.h"
  63. #include "lib/compress/compress.h"
  64. #include "lib/crypt_ops/crypto_ed25519.h"
  65. #include "lib/crypt_ops/crypto_format.h"
  66. #include "lib/crypt_ops/crypto_rand.h"
  67. #include "lib/encoding/confline.h"
  68. #include "lib/memarea/memarea.h"
  69. #include "lib/osinfo/uname.h"
  70. #include "test/log_test_helpers.h"
  71. #include "test/test.h"
  72. #include "test/test_dir_common.h"
  73. #include "core/or/addr_policy_st.h"
  74. #include "feature/nodelist/authority_cert_st.h"
  75. #include "feature/nodelist/document_signature_st.h"
  76. #include "feature/nodelist/extrainfo_st.h"
  77. #include "feature/nodelist/networkstatus_st.h"
  78. #include "feature/nodelist/networkstatus_voter_info_st.h"
  79. #include "feature/dirauth/ns_detached_signatures_st.h"
  80. #include "core/or/port_cfg_st.h"
  81. #include "feature/nodelist/routerinfo_st.h"
  82. #include "feature/nodelist/routerlist_st.h"
  83. #include "core/or/tor_version_st.h"
  84. #include "feature/dirauth/vote_microdesc_hash_st.h"
  85. #include "feature/nodelist/vote_routerstatus_st.h"
  86. #ifdef HAVE_SYS_STAT_H
  87. #include <sys/stat.h>
  88. #endif
  89. #define NS_MODULE dir
  90. static void
  91. test_dir_nicknames(void *arg)
  92. {
  93. (void)arg;
  94. tt_assert( is_legal_nickname("a"));
  95. tt_assert(!is_legal_nickname(""));
  96. tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  97. tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
  98. tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  99. tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  100. /* valid */
  101. tt_assert( is_legal_nickname_or_hexdigest(
  102. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  103. tt_assert( is_legal_nickname_or_hexdigest(
  104. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  105. tt_assert( is_legal_nickname_or_hexdigest(
  106. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  107. /* too short */
  108. tt_assert(!is_legal_nickname_or_hexdigest(
  109. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  110. /* illegal char */
  111. tt_assert(!is_legal_nickname_or_hexdigest(
  112. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  113. /* hex part too long */
  114. tt_assert(!is_legal_nickname_or_hexdigest(
  115. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  116. tt_assert(!is_legal_nickname_or_hexdigest(
  117. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  118. /* Bad nickname */
  119. tt_assert(!is_legal_nickname_or_hexdigest(
  120. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  121. tt_assert(!is_legal_nickname_or_hexdigest(
  122. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  123. tt_assert(!is_legal_nickname_or_hexdigest(
  124. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  125. tt_assert(!is_legal_nickname_or_hexdigest(
  126. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  127. "abcdefghijklmnoppqrst"));
  128. /* Bad extra char. */
  129. tt_assert(!is_legal_nickname_or_hexdigest(
  130. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  131. tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  132. tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  133. tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  134. done:
  135. ;
  136. }
  137. static smartlist_t *mocked_configured_ports = NULL;
  138. /** Returns mocked_configured_ports */
  139. static const smartlist_t *
  140. mock_get_configured_ports(void)
  141. {
  142. return mocked_configured_ports;
  143. }
  144. /** Run unit tests for router descriptor generation logic. */
  145. static void
  146. test_dir_formats(void *arg)
  147. {
  148. char *buf = NULL;
  149. char buf2[8192];
  150. char platform[256];
  151. char fingerprint[FINGERPRINT_LEN+1];
  152. char *pk1_str = NULL, *pk2_str = NULL, *cp;
  153. size_t pk1_str_len, pk2_str_len;
  154. routerinfo_t *r1=NULL, *r2=NULL;
  155. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  156. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  157. addr_policy_t *ex1, *ex2;
  158. routerlist_t *dir1 = NULL, *dir2 = NULL;
  159. uint8_t *rsa_cc = NULL;
  160. or_options_t *options = get_options_mutable();
  161. const addr_policy_t *p;
  162. time_t now = time(NULL);
  163. port_cfg_t orport, dirport;
  164. char cert_buf[256];
  165. (void)arg;
  166. pk1 = pk_generate(0);
  167. pk2 = pk_generate(1);
  168. tt_assert(pk1 && pk2);
  169. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  170. get_platform_str(platform, sizeof(platform));
  171. r1 = tor_malloc_zero(sizeof(routerinfo_t));
  172. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  173. r1->cache_info.published_on = 0;
  174. r1->or_port = 9000;
  175. r1->dir_port = 9003;
  176. r1->supports_tunnelled_dir_requests = 1;
  177. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  178. r1->ipv6_orport = 9999;
  179. router_set_rsa_onion_pkey(pk1, &r1->onion_pkey, &r1->onion_pkey_len);
  180. /* Fake just enough of an ntor key to get by */
  181. curve25519_keypair_t r1_onion_keypair;
  182. curve25519_keypair_generate(&r1_onion_keypair, 0);
  183. r1->onion_curve25519_pkey = tor_memdup(&r1_onion_keypair.pubkey,
  184. sizeof(curve25519_public_key_t));
  185. r1->identity_pkey = crypto_pk_dup_key(pk2);
  186. r1->bandwidthrate = 1000;
  187. r1->bandwidthburst = 5000;
  188. r1->bandwidthcapacity = 10000;
  189. r1->exit_policy = NULL;
  190. r1->nickname = tor_strdup("Magri");
  191. r1->platform = tor_strdup(platform);
  192. ex1 = tor_malloc_zero(sizeof(addr_policy_t));
  193. ex2 = tor_malloc_zero(sizeof(addr_policy_t));
  194. ex1->policy_type = ADDR_POLICY_ACCEPT;
  195. tor_addr_from_ipv4h(&ex1->addr, 0);
  196. ex1->maskbits = 0;
  197. ex1->prt_min = ex1->prt_max = 80;
  198. ex2->policy_type = ADDR_POLICY_REJECT;
  199. tor_addr_from_ipv4h(&ex2->addr, 18<<24);
  200. ex2->maskbits = 8;
  201. ex2->prt_min = ex2->prt_max = 24;
  202. r2 = tor_malloc_zero(sizeof(routerinfo_t));
  203. r2->addr = 0x0a030201u; /* 10.3.2.1 */
  204. ed25519_keypair_t kp1, kp2;
  205. ed25519_secret_key_from_seed(&kp1.seckey,
  206. (const uint8_t*)"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
  207. ed25519_public_key_generate(&kp1.pubkey, &kp1.seckey);
  208. ed25519_secret_key_from_seed(&kp2.seckey,
  209. (const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  210. ed25519_public_key_generate(&kp2.pubkey, &kp2.seckey);
  211. r2->cache_info.signing_key_cert = tor_cert_create(&kp1,
  212. CERT_TYPE_ID_SIGNING,
  213. &kp2.pubkey,
  214. now, 86400,
  215. CERT_FLAG_INCLUDE_SIGNING_KEY);
  216. r2->platform = tor_strdup(platform);
  217. r2->cache_info.published_on = 5;
  218. r2->or_port = 9005;
  219. r2->dir_port = 0;
  220. r2->supports_tunnelled_dir_requests = 1;
  221. router_set_rsa_onion_pkey(pk2, &r2->onion_pkey, &r2->onion_pkey_len);
  222. curve25519_keypair_t r2_onion_keypair;
  223. curve25519_keypair_generate(&r2_onion_keypair, 0);
  224. r2->onion_curve25519_pkey = tor_memdup(&r2_onion_keypair.pubkey,
  225. sizeof(curve25519_public_key_t));
  226. r2->identity_pkey = crypto_pk_dup_key(pk1);
  227. r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
  228. r2->exit_policy = smartlist_new();
  229. smartlist_add(r2->exit_policy, ex1);
  230. smartlist_add(r2->exit_policy, ex2);
  231. r2->nickname = tor_strdup("Fred");
  232. tt_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  233. &pk1_str_len));
  234. tt_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  235. &pk2_str_len));
  236. /* XXXX+++ router_dump_to_string should really take this from ri.*/
  237. options->ContactInfo = tor_strdup("Magri White "
  238. "<magri@elsewhere.example.com>");
  239. /* Skip reachability checks for DirPort and tunnelled-dir-server */
  240. options->AssumeReachable = 1;
  241. /* Fake just enough of an ORPort and DirPort to get by */
  242. MOCK(get_configured_ports, mock_get_configured_ports);
  243. mocked_configured_ports = smartlist_new();
  244. memset(&orport, 0, sizeof(orport));
  245. orport.type = CONN_TYPE_OR_LISTENER;
  246. orport.addr.family = AF_INET;
  247. orport.port = 9000;
  248. smartlist_add(mocked_configured_ports, &orport);
  249. memset(&dirport, 0, sizeof(dirport));
  250. dirport.type = CONN_TYPE_DIR_LISTENER;
  251. dirport.addr.family = AF_INET;
  252. dirport.port = 9003;
  253. smartlist_add(mocked_configured_ports, &dirport);
  254. buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
  255. UNMOCK(get_configured_ports);
  256. smartlist_free(mocked_configured_ports);
  257. mocked_configured_ports = NULL;
  258. tor_free(options->ContactInfo);
  259. tt_assert(buf);
  260. strlcpy(buf2, "router Magri 192.168.0.1 9000 0 9003\n"
  261. "or-address [1:2:3:4::]:9999\n"
  262. "platform Tor "VERSION" on ", sizeof(buf2));
  263. strlcat(buf2, get_uname(), sizeof(buf2));
  264. strlcat(buf2, "\n"
  265. "published 1970-01-01 00:00:00\n"
  266. "fingerprint ", sizeof(buf2));
  267. tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  268. strlcat(buf2, fingerprint, sizeof(buf2));
  269. strlcat(buf2, "\nuptime 0\n"
  270. /* XXX the "0" above is hard-coded, but even if we made it reflect
  271. * uptime, that still wouldn't make it right, because the two
  272. * descriptors might be made on different seconds... hm. */
  273. "bandwidth 1000 5000 10000\n"
  274. "onion-key\n", sizeof(buf2));
  275. strlcat(buf2, pk1_str, sizeof(buf2));
  276. strlcat(buf2, "signing-key\n", sizeof(buf2));
  277. strlcat(buf2, pk2_str, sizeof(buf2));
  278. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  279. strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
  280. sizeof(buf2));
  281. strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
  282. base64_encode(cert_buf, sizeof(cert_buf),
  283. (const char*)r1_onion_keypair.pubkey.public_key, 32,
  284. BASE64_ENCODE_MULTILINE);
  285. strlcat(buf2, cert_buf, sizeof(buf2));
  286. strlcat(buf2, "reject *:*\n", sizeof(buf2));
  287. strlcat(buf2, "tunnelled-dir-server\nrouter-signature\n", sizeof(buf2));
  288. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  289. * twice */
  290. tt_str_op(buf,OP_EQ, buf2);
  291. tor_free(buf);
  292. buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
  293. tt_assert(buf);
  294. cp = buf;
  295. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
  296. tt_assert(rp1);
  297. tt_int_op(rp1->addr,OP_EQ, r1->addr);
  298. tt_int_op(rp1->or_port,OP_EQ, r1->or_port);
  299. tt_int_op(rp1->dir_port,OP_EQ, r1->dir_port);
  300. tt_int_op(rp1->bandwidthrate,OP_EQ, r1->bandwidthrate);
  301. tt_int_op(rp1->bandwidthburst,OP_EQ, r1->bandwidthburst);
  302. tt_int_op(rp1->bandwidthcapacity,OP_EQ, r1->bandwidthcapacity);
  303. crypto_pk_t *onion_pkey = router_get_rsa_onion_pkey(rp1->onion_pkey,
  304. rp1->onion_pkey_len);
  305. tt_int_op(crypto_pk_cmp_keys(onion_pkey, pk1), OP_EQ, 0);
  306. crypto_pk_free(onion_pkey);
  307. tt_int_op(crypto_pk_cmp_keys(rp1->identity_pkey, pk2), OP_EQ, 0);
  308. tt_assert(rp1->supports_tunnelled_dir_requests);
  309. //tt_assert(rp1->exit_policy == NULL);
  310. tor_free(buf);
  311. strlcpy(buf2,
  312. "router Fred 10.3.2.1 9005 0 0\n"
  313. "identity-ed25519\n"
  314. "-----BEGIN ED25519 CERT-----\n", sizeof(buf2));
  315. base64_encode(cert_buf, sizeof(cert_buf),
  316. (const char*)r2->cache_info.signing_key_cert->encoded,
  317. r2->cache_info.signing_key_cert->encoded_len,
  318. BASE64_ENCODE_MULTILINE);
  319. strlcat(buf2, cert_buf, sizeof(buf2));
  320. strlcat(buf2, "-----END ED25519 CERT-----\n", sizeof(buf2));
  321. strlcat(buf2, "master-key-ed25519 ", sizeof(buf2));
  322. {
  323. char k[ED25519_BASE64_LEN+1];
  324. tt_int_op(ed25519_public_to_base64(k,
  325. &r2->cache_info.signing_key_cert->signing_key),
  326. OP_GE, 0);
  327. strlcat(buf2, k, sizeof(buf2));
  328. strlcat(buf2, "\n", sizeof(buf2));
  329. }
  330. strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2));
  331. strlcat(buf2, get_uname(), sizeof(buf2));
  332. strlcat(buf2, "\n"
  333. "published 1970-01-01 00:00:05\n"
  334. "fingerprint ", sizeof(buf2));
  335. tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
  336. strlcat(buf2, fingerprint, sizeof(buf2));
  337. strlcat(buf2, "\nuptime 0\n"
  338. "bandwidth 3000 3000 3000\n", sizeof(buf2));
  339. strlcat(buf2, "onion-key\n", sizeof(buf2));
  340. strlcat(buf2, pk2_str, sizeof(buf2));
  341. strlcat(buf2, "signing-key\n", sizeof(buf2));
  342. strlcat(buf2, pk1_str, sizeof(buf2));
  343. int rsa_cc_len;
  344. rsa_cc = make_tap_onion_key_crosscert(pk2,
  345. &kp1.pubkey,
  346. pk1,
  347. &rsa_cc_len);
  348. tt_assert(rsa_cc);
  349. base64_encode(cert_buf, sizeof(cert_buf), (char*)rsa_cc, rsa_cc_len,
  350. BASE64_ENCODE_MULTILINE);
  351. strlcat(buf2, "onion-key-crosscert\n"
  352. "-----BEGIN CROSSCERT-----\n", sizeof(buf2));
  353. strlcat(buf2, cert_buf, sizeof(buf2));
  354. strlcat(buf2, "-----END CROSSCERT-----\n", sizeof(buf2));
  355. int ntor_cc_sign;
  356. {
  357. tor_cert_t *ntor_cc = NULL;
  358. ntor_cc = make_ntor_onion_key_crosscert(&r2_onion_keypair,
  359. &kp1.pubkey,
  360. r2->cache_info.published_on,
  361. get_onion_key_lifetime(),
  362. &ntor_cc_sign);
  363. tt_assert(ntor_cc);
  364. base64_encode(cert_buf, sizeof(cert_buf),
  365. (char*)ntor_cc->encoded, ntor_cc->encoded_len,
  366. BASE64_ENCODE_MULTILINE);
  367. tor_cert_free(ntor_cc);
  368. }
  369. tor_snprintf(buf2+strlen(buf2), sizeof(buf2)-strlen(buf2),
  370. "ntor-onion-key-crosscert %d\n"
  371. "-----BEGIN ED25519 CERT-----\n"
  372. "%s"
  373. "-----END ED25519 CERT-----\n", ntor_cc_sign, cert_buf);
  374. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  375. strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
  376. base64_encode(cert_buf, sizeof(cert_buf),
  377. (const char*)r2_onion_keypair.pubkey.public_key, 32,
  378. BASE64_ENCODE_MULTILINE);
  379. strlcat(buf2, cert_buf, sizeof(buf2));
  380. strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
  381. strlcat(buf2, "tunnelled-dir-server\n", sizeof(buf2));
  382. strlcat(buf2, "router-sig-ed25519 ", sizeof(buf2));
  383. /* Fake just enough of an ORPort to get by */
  384. MOCK(get_configured_ports, mock_get_configured_ports);
  385. mocked_configured_ports = smartlist_new();
  386. memset(&orport, 0, sizeof(orport));
  387. orport.type = CONN_TYPE_OR_LISTENER;
  388. orport.addr.family = AF_INET;
  389. orport.port = 9005;
  390. smartlist_add(mocked_configured_ports, &orport);
  391. buf = router_dump_router_to_string(r2, pk1, pk2, &r2_onion_keypair, &kp2);
  392. tt_assert(buf);
  393. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  394. * twice */
  395. tt_str_op(buf, OP_EQ, buf2);
  396. tor_free(buf);
  397. buf = router_dump_router_to_string(r2, pk1, NULL, NULL, NULL);
  398. UNMOCK(get_configured_ports);
  399. smartlist_free(mocked_configured_ports);
  400. mocked_configured_ports = NULL;
  401. /* Reset for later */
  402. cp = buf;
  403. rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
  404. tt_assert(rp2);
  405. tt_int_op(rp2->addr,OP_EQ, r2->addr);
  406. tt_int_op(rp2->or_port,OP_EQ, r2->or_port);
  407. tt_int_op(rp2->dir_port,OP_EQ, r2->dir_port);
  408. tt_int_op(rp2->bandwidthrate,OP_EQ, r2->bandwidthrate);
  409. tt_int_op(rp2->bandwidthburst,OP_EQ, r2->bandwidthburst);
  410. tt_int_op(rp2->bandwidthcapacity,OP_EQ, r2->bandwidthcapacity);
  411. tt_mem_op(rp2->onion_curve25519_pkey->public_key,OP_EQ,
  412. r2->onion_curve25519_pkey->public_key,
  413. CURVE25519_PUBKEY_LEN);
  414. onion_pkey = router_get_rsa_onion_pkey(rp2->onion_pkey,
  415. rp2->onion_pkey_len);
  416. tt_int_op(crypto_pk_cmp_keys(onion_pkey, pk2), OP_EQ, 0);
  417. crypto_pk_free(onion_pkey);
  418. tt_int_op(crypto_pk_cmp_keys(rp2->identity_pkey, pk1), OP_EQ, 0);
  419. tt_assert(rp2->supports_tunnelled_dir_requests);
  420. tt_int_op(smartlist_len(rp2->exit_policy),OP_EQ, 2);
  421. p = smartlist_get(rp2->exit_policy, 0);
  422. tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_ACCEPT);
  423. tt_assert(tor_addr_is_null(&p->addr));
  424. tt_int_op(p->maskbits,OP_EQ, 0);
  425. tt_int_op(p->prt_min,OP_EQ, 80);
  426. tt_int_op(p->prt_max,OP_EQ, 80);
  427. p = smartlist_get(rp2->exit_policy, 1);
  428. tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_REJECT);
  429. tt_assert(tor_addr_eq(&p->addr, &ex2->addr));
  430. tt_int_op(p->maskbits,OP_EQ, 8);
  431. tt_int_op(p->prt_min,OP_EQ, 24);
  432. tt_int_op(p->prt_max,OP_EQ, 24);
  433. #if 0
  434. /* Okay, now for the directories. */
  435. {
  436. fingerprint_list = smartlist_new();
  437. crypto_pk_get_fingerprint(pk2, buf, 1);
  438. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  439. crypto_pk_get_fingerprint(pk1, buf, 1);
  440. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  441. }
  442. #endif /* 0 */
  443. dirserv_free_fingerprint_list();
  444. done:
  445. if (r1)
  446. routerinfo_free(r1);
  447. if (r2)
  448. routerinfo_free(r2);
  449. if (rp2)
  450. routerinfo_free(rp2);
  451. tor_free(rsa_cc);
  452. tor_free(buf);
  453. tor_free(pk1_str);
  454. tor_free(pk2_str);
  455. if (pk1) crypto_pk_free(pk1);
  456. if (pk2) crypto_pk_free(pk2);
  457. if (rp1) routerinfo_free(rp1);
  458. tor_free(dir1); /* XXXX And more !*/
  459. tor_free(dir2); /* And more !*/
  460. }
  461. #include "failing_routerdescs.inc"
  462. static void
  463. test_dir_routerinfo_parsing(void *arg)
  464. {
  465. (void) arg;
  466. int again;
  467. routerinfo_t *ri = NULL;
  468. #define CHECK_OK(s) \
  469. do { \
  470. routerinfo_free(ri); \
  471. ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
  472. tt_assert(ri); \
  473. } while (0)
  474. #define CHECK_FAIL(s, againval) \
  475. do { \
  476. routerinfo_free(ri); \
  477. again = 999; \
  478. ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
  479. tt_assert(ri == NULL); \
  480. tt_int_op(again, OP_EQ, (againval)); \
  481. } while (0)
  482. CHECK_OK(EX_RI_MINIMAL);
  483. CHECK_OK(EX_RI_MAXIMAL);
  484. CHECK_OK(EX_RI_MINIMAL_ED);
  485. /* good annotations prepended */
  486. routerinfo_free(ri);
  487. ri = router_parse_entry_from_string(EX_RI_MINIMAL, NULL, 0, 0,
  488. "@purpose bridge\n", NULL);
  489. tt_ptr_op(ri, OP_NE, NULL);
  490. tt_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  491. routerinfo_free(ri);
  492. /* bad annotations prepended. */
  493. ri = router_parse_entry_from_string(EX_RI_MINIMAL,
  494. NULL, 0, 0, "@purpose\n", NULL);
  495. tt_ptr_op(ri, OP_EQ, NULL);
  496. /* bad annotations on router. */
  497. ri = router_parse_entry_from_string("@purpose\nrouter x\n", NULL, 0, 1,
  498. NULL, NULL);
  499. tt_ptr_op(ri, OP_EQ, NULL);
  500. /* unwanted annotations on router. */
  501. ri = router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL, 0, 0,
  502. NULL, NULL);
  503. tt_ptr_op(ri, OP_EQ, NULL);
  504. /* No signature. */
  505. ri = router_parse_entry_from_string("router x\n", NULL, 0, 0,
  506. NULL, NULL);
  507. tt_ptr_op(ri, OP_EQ, NULL);
  508. /* Not a router */
  509. routerinfo_free(ri);
  510. ri = router_parse_entry_from_string("hello\n", NULL, 0, 0, NULL, NULL);
  511. tt_ptr_op(ri, OP_EQ, NULL);
  512. CHECK_FAIL(EX_RI_BAD_SIG1, 1);
  513. CHECK_FAIL(EX_RI_BAD_SIG2, 1);
  514. CHECK_FAIL(EX_RI_BAD_TOKENS, 0);
  515. CHECK_FAIL(EX_RI_BAD_PUBLISHED, 0);
  516. CHECK_FAIL(EX_RI_NEG_BANDWIDTH, 0);
  517. CHECK_FAIL(EX_RI_BAD_BANDWIDTH, 0);
  518. CHECK_FAIL(EX_RI_BAD_BANDWIDTH2, 0);
  519. CHECK_FAIL(EX_RI_BAD_ONIONKEY1, 0);
  520. CHECK_FAIL(EX_RI_BAD_ONIONKEY2, 0);
  521. CHECK_FAIL(EX_RI_BAD_PORTS, 0);
  522. CHECK_FAIL(EX_RI_BAD_IP, 0);
  523. CHECK_FAIL(EX_RI_BAD_DIRPORT, 0);
  524. CHECK_FAIL(EX_RI_BAD_NAME2, 0);
  525. CHECK_FAIL(EX_RI_BAD_UPTIME, 0);
  526. CHECK_FAIL(EX_RI_BAD_BANDWIDTH3, 0);
  527. CHECK_FAIL(EX_RI_BAD_NTOR_KEY, 0);
  528. CHECK_FAIL(EX_RI_BAD_FINGERPRINT, 0);
  529. CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT, 0);
  530. CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6, 0);
  531. CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY, 0);
  532. CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY, 0);
  533. CHECK_FAIL(EX_RI_BAD_FAMILY, 0);
  534. CHECK_FAIL(EX_RI_ZERO_ORPORT, 0);
  535. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT, 0);
  536. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2, 0);
  537. CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN, 0);
  538. CHECK_FAIL(EX_RI_ED_BAD_SIG1, 0);
  539. CHECK_FAIL(EX_RI_ED_BAD_SIG2, 0);
  540. CHECK_FAIL(EX_RI_ED_BAD_SIG3, 0);
  541. CHECK_FAIL(EX_RI_ED_BAD_SIG4, 0);
  542. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1, 0);
  543. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3, 0);
  544. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4, 0);
  545. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5, 0);
  546. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6, 0);
  547. CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7, 0);
  548. CHECK_FAIL(EX_RI_ED_MISPLACED1, 0);
  549. CHECK_FAIL(EX_RI_ED_MISPLACED2, 0);
  550. CHECK_FAIL(EX_RI_ED_BAD_CERT1, 0);
  551. CHECK_FAIL(EX_RI_ED_BAD_CERT2, 0);
  552. CHECK_FAIL(EX_RI_ED_BAD_CERT3, 0);
  553. /* This is allowed; we just ignore it. */
  554. CHECK_OK(EX_RI_BAD_EI_DIGEST);
  555. CHECK_OK(EX_RI_BAD_EI_DIGEST2);
  556. #undef CHECK_FAIL
  557. #undef CHECK_OK
  558. done:
  559. routerinfo_free(ri);
  560. }
  561. #include "example_extrainfo.inc"
  562. static void
  563. routerinfo_free_wrapper_(void *arg)
  564. {
  565. routerinfo_free_(arg);
  566. }
  567. static void
  568. test_dir_extrainfo_parsing(void *arg)
  569. {
  570. (void) arg;
  571. #define CHECK_OK(s) \
  572. do { \
  573. extrainfo_free(ei); \
  574. ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
  575. tt_assert(ei); \
  576. } while (0)
  577. #define CHECK_FAIL(s, againval) \
  578. do { \
  579. extrainfo_free(ei); \
  580. again = 999; \
  581. ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
  582. tt_assert(ei == NULL); \
  583. tt_int_op(again, OP_EQ, (againval)); \
  584. } while (0)
  585. #define ADD(name) \
  586. do { \
  587. ri = tor_malloc_zero(sizeof(routerinfo_t)); \
  588. crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
  589. tt_assert(! crypto_pk_read_public_key_from_string(pk, \
  590. name##_KEY, strlen(name##_KEY))); \
  591. tt_int_op(20,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
  592. digestmap_set((digestmap_t*)map, d, ri); \
  593. ri = NULL; \
  594. } while (0)
  595. routerinfo_t *ri = NULL;
  596. char d[20];
  597. struct digest_ri_map_t *map = NULL;
  598. extrainfo_t *ei = NULL;
  599. int again;
  600. CHECK_OK(EX_EI_MINIMAL);
  601. tt_assert(ei->pending_sig);
  602. CHECK_OK(EX_EI_MAXIMAL);
  603. tt_assert(ei->pending_sig);
  604. CHECK_OK(EX_EI_GOOD_ED_EI);
  605. tt_assert(ei->pending_sig);
  606. map = (struct digest_ri_map_t *)digestmap_new();
  607. ADD(EX_EI_MINIMAL);
  608. ADD(EX_EI_MAXIMAL);
  609. ADD(EX_EI_GOOD_ED_EI);
  610. ADD(EX_EI_BAD_FP);
  611. ADD(EX_EI_BAD_NICKNAME);
  612. ADD(EX_EI_BAD_TOKENS);
  613. ADD(EX_EI_BAD_START);
  614. ADD(EX_EI_BAD_PUBLISHED);
  615. ADD(EX_EI_ED_MISSING_SIG);
  616. ADD(EX_EI_ED_MISSING_CERT);
  617. ADD(EX_EI_ED_BAD_CERT1);
  618. ADD(EX_EI_ED_BAD_CERT2);
  619. ADD(EX_EI_ED_BAD_SIG1);
  620. ADD(EX_EI_ED_BAD_SIG2);
  621. ADD(EX_EI_ED_MISPLACED_CERT);
  622. ADD(EX_EI_ED_MISPLACED_SIG);
  623. CHECK_OK(EX_EI_MINIMAL);
  624. tt_ptr_op(ei->pending_sig, OP_EQ, NULL);
  625. CHECK_OK(EX_EI_MAXIMAL);
  626. tt_ptr_op(ei->pending_sig, OP_EQ, NULL);
  627. CHECK_OK(EX_EI_GOOD_ED_EI);
  628. tt_ptr_op(ei->pending_sig, OP_EQ, NULL);
  629. CHECK_FAIL(EX_EI_BAD_SIG1,1);
  630. CHECK_FAIL(EX_EI_BAD_SIG2,1);
  631. CHECK_FAIL(EX_EI_BAD_SIG3,1);
  632. CHECK_FAIL(EX_EI_BAD_FP,0);
  633. CHECK_FAIL(EX_EI_BAD_NICKNAME,0);
  634. CHECK_FAIL(EX_EI_BAD_TOKENS,0);
  635. CHECK_FAIL(EX_EI_BAD_START,0);
  636. CHECK_FAIL(EX_EI_BAD_PUBLISHED,0);
  637. CHECK_FAIL(EX_EI_ED_MISSING_SIG,0);
  638. CHECK_FAIL(EX_EI_ED_MISSING_CERT,0);
  639. CHECK_FAIL(EX_EI_ED_BAD_CERT1,0);
  640. CHECK_FAIL(EX_EI_ED_BAD_CERT2,0);
  641. CHECK_FAIL(EX_EI_ED_BAD_SIG1,0);
  642. CHECK_FAIL(EX_EI_ED_BAD_SIG2,0);
  643. CHECK_FAIL(EX_EI_ED_MISPLACED_CERT,0);
  644. CHECK_FAIL(EX_EI_ED_MISPLACED_SIG,0);
  645. #undef CHECK_OK
  646. #undef CHECK_FAIL
  647. done:
  648. escaped(NULL);
  649. extrainfo_free(ei);
  650. routerinfo_free(ri);
  651. digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
  652. }
  653. static void
  654. test_dir_parse_router_list(void *arg)
  655. {
  656. (void) arg;
  657. smartlist_t *invalid = smartlist_new();
  658. smartlist_t *dest = smartlist_new();
  659. smartlist_t *chunks = smartlist_new();
  660. int dest_has_ri = 1;
  661. char *list = NULL;
  662. const char *cp;
  663. digestmap_t *map = NULL;
  664. char *mem_op_hex_tmp = NULL;
  665. routerinfo_t *ri = NULL;
  666. char d[DIGEST_LEN];
  667. smartlist_add_strdup(chunks, EX_RI_MINIMAL); // ri 0
  668. smartlist_add_strdup(chunks, EX_RI_BAD_PORTS); // bad ri 0
  669. smartlist_add_strdup(chunks, EX_EI_MAXIMAL); // ei 0
  670. smartlist_add_strdup(chunks, EX_EI_BAD_SIG2); // bad ei --
  671. smartlist_add_strdup(chunks, EX_EI_BAD_NICKNAME);// bad ei 0
  672. smartlist_add_strdup(chunks, EX_RI_BAD_SIG1); // bad ri --
  673. smartlist_add_strdup(chunks, EX_EI_BAD_PUBLISHED); // bad ei 1
  674. smartlist_add_strdup(chunks, EX_RI_MAXIMAL); // ri 1
  675. smartlist_add_strdup(chunks, EX_RI_BAD_FAMILY); // bad ri 1
  676. smartlist_add_strdup(chunks, EX_EI_MINIMAL); // ei 1
  677. list = smartlist_join_strings(chunks, "", 0, NULL);
  678. /* First, parse the routers. */
  679. cp = list;
  680. tt_int_op(0,OP_EQ,
  681. router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
  682. 0, 0, NULL, invalid));
  683. tt_int_op(2, OP_EQ, smartlist_len(dest));
  684. tt_ptr_op(cp, OP_EQ, list + strlen(list));
  685. routerinfo_t *r = smartlist_get(dest, 0);
  686. tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
  687. EX_RI_MINIMAL, strlen(EX_RI_MINIMAL));
  688. r = smartlist_get(dest, 1);
  689. tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
  690. EX_RI_MAXIMAL, strlen(EX_RI_MAXIMAL));
  691. tt_int_op(2, OP_EQ, smartlist_len(invalid));
  692. test_memeq_hex(smartlist_get(invalid, 0),
  693. "ab9eeaa95e7d45740185b4e519c76ead756277a9");
  694. test_memeq_hex(smartlist_get(invalid, 1),
  695. "9a651ee03b64325959e8f1b46f2b689b30750b4c");
  696. /* Now tidy up */
  697. SMARTLIST_FOREACH(dest, routerinfo_t *, rinfo, routerinfo_free(rinfo));
  698. SMARTLIST_FOREACH(invalid, uint8_t *, dig, tor_free(dig));
  699. smartlist_clear(dest);
  700. smartlist_clear(invalid);
  701. /* And check extrainfos. */
  702. dest_has_ri = 0;
  703. map = (digestmap_t*)router_get_routerlist()->identity_map;
  704. ADD(EX_EI_MINIMAL);
  705. ADD(EX_EI_MAXIMAL);
  706. ADD(EX_EI_BAD_NICKNAME);
  707. ADD(EX_EI_BAD_PUBLISHED);
  708. cp = list;
  709. tt_int_op(0,OP_EQ,
  710. router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
  711. 1, 0, NULL, invalid));
  712. tt_int_op(2, OP_EQ, smartlist_len(dest));
  713. extrainfo_t *e = smartlist_get(dest, 0);
  714. tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
  715. EX_EI_MAXIMAL, strlen(EX_EI_MAXIMAL));
  716. e = smartlist_get(dest, 1);
  717. tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
  718. EX_EI_MINIMAL, strlen(EX_EI_MINIMAL));
  719. tt_int_op(2, OP_EQ, smartlist_len(invalid));
  720. test_memeq_hex(smartlist_get(invalid, 0),
  721. "d5df4aa62ee9ffc9543d41150c9864908e0390af");
  722. test_memeq_hex(smartlist_get(invalid, 1),
  723. "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
  724. done:
  725. tor_free(list);
  726. if (dest_has_ri)
  727. SMARTLIST_FOREACH(dest, routerinfo_t *, rt, routerinfo_free(rt));
  728. else
  729. SMARTLIST_FOREACH(dest, extrainfo_t *, ei, extrainfo_free(ei));
  730. smartlist_free(dest);
  731. SMARTLIST_FOREACH(invalid, uint8_t *, dig, tor_free(dig));
  732. smartlist_free(invalid);
  733. SMARTLIST_FOREACH(chunks, char *, chunk, tor_free(chunk));
  734. smartlist_free(chunks);
  735. routerinfo_free(ri);
  736. if (map) {
  737. digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
  738. router_get_routerlist()->identity_map =
  739. (struct digest_ri_map_t*)digestmap_new();
  740. }
  741. tor_free(mem_op_hex_tmp);
  742. #undef ADD
  743. }
  744. static download_status_t dls_minimal;
  745. static download_status_t dls_maximal;
  746. static download_status_t dls_bad_fingerprint;
  747. static download_status_t dls_bad_sig2;
  748. static download_status_t dls_bad_ports;
  749. static download_status_t dls_bad_tokens;
  750. static int mock_router_get_dl_status_unrecognized = 0;
  751. static int mock_router_get_dl_status_calls = 0;
  752. static download_status_t *
  753. mock_router_get_dl_status(const char *d)
  754. {
  755. ++mock_router_get_dl_status_calls;
  756. char hex[HEX_DIGEST_LEN+1];
  757. base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
  758. if (!strcmp(hex, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
  759. return &dls_minimal;
  760. } else if (!strcmp(hex, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
  761. return &dls_maximal;
  762. } else if (!strcmp(hex, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
  763. return &dls_bad_fingerprint;
  764. } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
  765. return &dls_bad_sig2;
  766. } else if (!strcmp(hex, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
  767. return &dls_bad_ports;
  768. } else if (!strcmp(hex, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
  769. return &dls_bad_tokens;
  770. } else {
  771. ++mock_router_get_dl_status_unrecognized;
  772. return NULL;
  773. }
  774. }
  775. static void
  776. test_dir_load_routers(void *arg)
  777. {
  778. (void) arg;
  779. smartlist_t *chunks = smartlist_new();
  780. smartlist_t *wanted = smartlist_new();
  781. char buf[DIGEST_LEN];
  782. char *mem_op_hex_tmp = NULL;
  783. char *list = NULL;
  784. #define ADD(str) \
  785. do { \
  786. tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
  787. smartlist_add_strdup(wanted, hex_str(buf, DIGEST_LEN)); \
  788. } while (0)
  789. MOCK(router_get_dl_status_by_descriptor_digest, mock_router_get_dl_status);
  790. update_approx_time(1412510400);
  791. smartlist_add_strdup(chunks, EX_RI_MINIMAL);
  792. smartlist_add_strdup(chunks, EX_RI_BAD_FINGERPRINT);
  793. smartlist_add_strdup(chunks, EX_RI_BAD_SIG2);
  794. smartlist_add_strdup(chunks, EX_RI_MAXIMAL);
  795. smartlist_add_strdup(chunks, EX_RI_BAD_PORTS);
  796. smartlist_add_strdup(chunks, EX_RI_BAD_TOKENS);
  797. /* not ADDing MINIMIAL */
  798. ADD(EX_RI_MAXIMAL);
  799. ADD(EX_RI_BAD_FINGERPRINT);
  800. ADD(EX_RI_BAD_SIG2);
  801. /* Not ADDing BAD_PORTS */
  802. ADD(EX_RI_BAD_TOKENS);
  803. list = smartlist_join_strings(chunks, "", 0, NULL);
  804. tt_int_op(1, OP_EQ,
  805. router_load_routers_from_string(list, NULL, SAVED_IN_JOURNAL,
  806. wanted, 1, NULL));
  807. /* The "maximal" router was added. */
  808. /* "minimal" was not. */
  809. tt_int_op(smartlist_len(router_get_routerlist()->routers),OP_EQ,1);
  810. routerinfo_t *r = smartlist_get(router_get_routerlist()->routers, 0);
  811. test_memeq_hex(r->cache_info.signed_descriptor_digest,
  812. "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
  813. tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
  814. tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
  815. /* "Bad fingerprint" and "Bad tokens" should have gotten marked
  816. * non-retriable. */
  817. tt_want_int_op(mock_router_get_dl_status_calls, OP_EQ, 2);
  818. tt_want_int_op(mock_router_get_dl_status_unrecognized, OP_EQ, 0);
  819. tt_int_op(dls_bad_fingerprint.n_download_failures, OP_EQ, 255);
  820. tt_int_op(dls_bad_tokens.n_download_failures, OP_EQ, 255);
  821. /* bad_sig2 and bad ports" are retriable -- one since only the signature
  822. * was bad, and one because we didn't ask for it. */
  823. tt_int_op(dls_bad_sig2.n_download_failures, OP_EQ, 0);
  824. tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
  825. /* Wanted still contains "BAD_SIG2" */
  826. tt_int_op(smartlist_len(wanted), OP_EQ, 1);
  827. tt_str_op(smartlist_get(wanted, 0), OP_EQ,
  828. "E0A3753CEFD54128EAB239F294954121DB23D2EF");
  829. #undef ADD
  830. done:
  831. tor_free(mem_op_hex_tmp);
  832. UNMOCK(router_get_dl_status_by_descriptor_digest);
  833. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  834. smartlist_free(chunks);
  835. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  836. smartlist_free(wanted);
  837. tor_free(list);
  838. }
  839. static int mock_get_by_ei_dd_calls = 0;
  840. static int mock_get_by_ei_dd_unrecognized = 0;
  841. static signed_descriptor_t sd_ei_minimal;
  842. static signed_descriptor_t sd_ei_bad_nickname;
  843. static signed_descriptor_t sd_ei_maximal;
  844. static signed_descriptor_t sd_ei_bad_tokens;
  845. static signed_descriptor_t sd_ei_bad_sig2;
  846. static signed_descriptor_t *
  847. mock_get_by_ei_desc_digest(const char *d)
  848. {
  849. ++mock_get_by_ei_dd_calls;
  850. char hex[HEX_DIGEST_LEN+1];
  851. base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
  852. if (!strcmp(hex, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
  853. return &sd_ei_minimal;
  854. } else if (!strcmp(hex, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
  855. return &sd_ei_maximal;
  856. } else if (!strcmp(hex, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
  857. return &sd_ei_bad_nickname;
  858. } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
  859. return &sd_ei_bad_sig2;
  860. } else if (!strcmp(hex, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
  861. return &sd_ei_bad_tokens;
  862. } else {
  863. ++mock_get_by_ei_dd_unrecognized;
  864. return NULL;
  865. }
  866. }
  867. static signed_descriptor_t *
  868. mock_ei_get_by_ei_digest(const char *d)
  869. {
  870. char hex[HEX_DIGEST_LEN+1];
  871. base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
  872. signed_descriptor_t *sd = &sd_ei_minimal;
  873. if (!strcmp(hex, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
  874. sd->signed_descriptor_body = (char *)EX_EI_MINIMAL;
  875. sd->signed_descriptor_len = sizeof(EX_EI_MINIMAL);
  876. sd->annotations_len = 0;
  877. sd->saved_location = SAVED_NOWHERE;
  878. return sd;
  879. }
  880. return NULL;
  881. }
  882. static smartlist_t *mock_ei_insert_list = NULL;
  883. static was_router_added_t
  884. mock_ei_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
  885. {
  886. (void) rl;
  887. (void) warn_if_incompatible;
  888. smartlist_add(mock_ei_insert_list, ei);
  889. return ROUTER_ADDED_SUCCESSFULLY;
  890. }
  891. static void
  892. test_dir_load_extrainfo(void *arg)
  893. {
  894. (void) arg;
  895. smartlist_t *chunks = smartlist_new();
  896. smartlist_t *wanted = smartlist_new();
  897. char buf[DIGEST_LEN];
  898. char *mem_op_hex_tmp = NULL;
  899. char *list = NULL;
  900. #define ADD(str) \
  901. do { \
  902. tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
  903. smartlist_add_strdup(wanted, hex_str(buf, DIGEST_LEN)); \
  904. } while (0)
  905. mock_ei_insert_list = smartlist_new();
  906. MOCK(router_get_by_extrainfo_digest, mock_get_by_ei_desc_digest);
  907. MOCK(extrainfo_insert, mock_ei_insert);
  908. smartlist_add_strdup(chunks, EX_EI_MINIMAL);
  909. smartlist_add_strdup(chunks, EX_EI_BAD_NICKNAME);
  910. smartlist_add_strdup(chunks, EX_EI_MAXIMAL);
  911. smartlist_add_strdup(chunks, EX_EI_BAD_PUBLISHED);
  912. smartlist_add_strdup(chunks, EX_EI_BAD_TOKENS);
  913. /* not ADDing MINIMIAL */
  914. ADD(EX_EI_MAXIMAL);
  915. ADD(EX_EI_BAD_NICKNAME);
  916. /* Not ADDing BAD_PUBLISHED */
  917. ADD(EX_EI_BAD_TOKENS);
  918. ADD(EX_EI_BAD_SIG2);
  919. list = smartlist_join_strings(chunks, "", 0, NULL);
  920. router_load_extrainfo_from_string(list, NULL, SAVED_IN_JOURNAL, wanted, 1);
  921. /* The "maximal" router was added. */
  922. /* "minimal" was also added, even though we didn't ask for it, since
  923. * that's what we do with extrainfos. */
  924. tt_int_op(smartlist_len(mock_ei_insert_list),OP_EQ,2);
  925. extrainfo_t *e = smartlist_get(mock_ei_insert_list, 0);
  926. test_memeq_hex(e->cache_info.signed_descriptor_digest,
  927. "11E0EDF526950739F7769810FCACAB8C882FAEEE");
  928. e = smartlist_get(mock_ei_insert_list, 1);
  929. test_memeq_hex(e->cache_info.signed_descriptor_digest,
  930. "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
  931. tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
  932. tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
  933. /* "Bad nickname" and "Bad tokens" should have gotten marked
  934. * non-retriable. */
  935. tt_want_int_op(mock_get_by_ei_dd_calls, OP_EQ, 2);
  936. tt_want_int_op(mock_get_by_ei_dd_unrecognized, OP_EQ, 0);
  937. tt_int_op(sd_ei_bad_nickname.ei_dl_status.n_download_failures, OP_EQ, 255);
  938. tt_int_op(sd_ei_bad_tokens.ei_dl_status.n_download_failures, OP_EQ, 255);
  939. /* bad_ports is retriable -- because we didn't ask for it. */
  940. tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
  941. /* Wanted still contains "BAD_SIG2" */
  942. tt_int_op(smartlist_len(wanted), OP_EQ, 1);
  943. tt_str_op(smartlist_get(wanted, 0), OP_EQ,
  944. "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
  945. #undef ADD
  946. done:
  947. tor_free(mem_op_hex_tmp);
  948. UNMOCK(router_get_by_extrainfo_digest);
  949. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  950. smartlist_free(chunks);
  951. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  952. smartlist_free(wanted);
  953. tor_free(list);
  954. }
  955. static void
  956. test_dir_getinfo_extra(void *arg)
  957. {
  958. int r;
  959. char *answer = NULL;
  960. const char *errmsg = NULL;
  961. (void)arg;
  962. MOCK(extrainfo_get_by_descriptor_digest, mock_ei_get_by_ei_digest);
  963. r = getinfo_helper_dir(NULL, "extra-info/digest/"
  964. "11E0EDF526950739F7769810FCACAB8C882FAEEE", &answer,
  965. &errmsg);
  966. tt_int_op(0, OP_EQ, r);
  967. tt_ptr_op(NULL, OP_EQ, errmsg);
  968. tt_str_op(answer, OP_EQ, EX_EI_MINIMAL);
  969. tor_free(answer);
  970. answer = NULL;
  971. r = getinfo_helper_dir(NULL, "extra-info/digest/"
  972. "NOTAVALIDHEXSTRINGNOTAVALIDHEXSTRINGNOTA", &answer,
  973. &errmsg);
  974. tt_int_op(0, OP_EQ, r);
  975. /* getinfo_helper_dir() should maybe return an error here but doesn't */
  976. tt_ptr_op(NULL, OP_EQ, errmsg);
  977. /* In any case, there should be no answer for an invalid hex string. */
  978. tt_ptr_op(NULL, OP_EQ, answer);
  979. done:
  980. UNMOCK(extrainfo_get_by_descriptor_digest);
  981. }
  982. static void
  983. test_dir_versions(void *arg)
  984. {
  985. tor_version_t ver1;
  986. /* Try out version parsing functionality */
  987. (void)arg;
  988. tt_int_op(0,OP_EQ, tor_version_parse("0.3.4pre2-cvs", &ver1));
  989. tt_int_op(0,OP_EQ, ver1.major);
  990. tt_int_op(3,OP_EQ, ver1.minor);
  991. tt_int_op(4,OP_EQ, ver1.micro);
  992. tt_int_op(VER_PRE,OP_EQ, ver1.status);
  993. tt_int_op(2,OP_EQ, ver1.patchlevel);
  994. tt_int_op(0,OP_EQ, tor_version_parse("0.3.4rc1", &ver1));
  995. tt_int_op(0,OP_EQ, ver1.major);
  996. tt_int_op(3,OP_EQ, ver1.minor);
  997. tt_int_op(4,OP_EQ, ver1.micro);
  998. tt_int_op(VER_RC,OP_EQ, ver1.status);
  999. tt_int_op(1,OP_EQ, ver1.patchlevel);
  1000. tt_int_op(0,OP_EQ, tor_version_parse("1.3.4", &ver1));
  1001. tt_int_op(1,OP_EQ, ver1.major);
  1002. tt_int_op(3,OP_EQ, ver1.minor);
  1003. tt_int_op(4,OP_EQ, ver1.micro);
  1004. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  1005. tt_int_op(0,OP_EQ, ver1.patchlevel);
  1006. tt_int_op(0,OP_EQ, tor_version_parse("1.3.4.999", &ver1));
  1007. tt_int_op(1,OP_EQ, ver1.major);
  1008. tt_int_op(3,OP_EQ, ver1.minor);
  1009. tt_int_op(4,OP_EQ, ver1.micro);
  1010. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  1011. tt_int_op(999,OP_EQ, ver1.patchlevel);
  1012. tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4-alpha", &ver1));
  1013. tt_int_op(0,OP_EQ, ver1.major);
  1014. tt_int_op(1,OP_EQ, ver1.minor);
  1015. tt_int_op(2,OP_EQ, ver1.micro);
  1016. tt_int_op(4,OP_EQ, ver1.patchlevel);
  1017. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  1018. tt_str_op("alpha",OP_EQ, ver1.status_tag);
  1019. tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4", &ver1));
  1020. tt_int_op(0,OP_EQ, ver1.major);
  1021. tt_int_op(1,OP_EQ, ver1.minor);
  1022. tt_int_op(2,OP_EQ, ver1.micro);
  1023. tt_int_op(4,OP_EQ, ver1.patchlevel);
  1024. tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
  1025. tt_str_op("",OP_EQ, ver1.status_tag);
  1026. tt_int_op(0, OP_EQ, tor_version_parse("10.1", &ver1));
  1027. tt_int_op(10, OP_EQ, ver1.major);
  1028. tt_int_op(1, OP_EQ, ver1.minor);
  1029. tt_int_op(0, OP_EQ, ver1.micro);
  1030. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1031. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1032. tt_str_op("", OP_EQ, ver1.status_tag);
  1033. tt_int_op(0, OP_EQ, tor_version_parse("5.99.999", &ver1));
  1034. tt_int_op(5, OP_EQ, ver1.major);
  1035. tt_int_op(99, OP_EQ, ver1.minor);
  1036. tt_int_op(999, OP_EQ, ver1.micro);
  1037. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1038. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1039. tt_str_op("", OP_EQ, ver1.status_tag);
  1040. tt_int_op(0, OP_EQ, tor_version_parse("10.1-alpha", &ver1));
  1041. tt_int_op(10, OP_EQ, ver1.major);
  1042. tt_int_op(1, OP_EQ, ver1.minor);
  1043. tt_int_op(0, OP_EQ, ver1.micro);
  1044. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1045. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1046. tt_str_op("alpha", OP_EQ, ver1.status_tag);
  1047. /* Go through the full set of status tags */
  1048. tt_int_op(0, OP_EQ, tor_version_parse("2.1.700-alpha", &ver1));
  1049. tt_int_op(2, OP_EQ, ver1.major);
  1050. tt_int_op(1, OP_EQ, ver1.minor);
  1051. tt_int_op(700, OP_EQ, ver1.micro);
  1052. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1053. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1054. tt_str_op("alpha", OP_EQ, ver1.status_tag);
  1055. tt_int_op(0, OP_EQ, tor_version_parse("1.6.8-alpha-dev", &ver1));
  1056. tt_int_op(1, OP_EQ, ver1.major);
  1057. tt_int_op(6, OP_EQ, ver1.minor);
  1058. tt_int_op(8, OP_EQ, ver1.micro);
  1059. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1060. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1061. tt_str_op("alpha-dev", OP_EQ, ver1.status_tag);
  1062. tt_int_op(0, OP_EQ, tor_version_parse("0.2.9.5-rc", &ver1));
  1063. tt_int_op(0, OP_EQ, ver1.major);
  1064. tt_int_op(2, OP_EQ, ver1.minor);
  1065. tt_int_op(9, OP_EQ, ver1.micro);
  1066. tt_int_op(5, OP_EQ, ver1.patchlevel);
  1067. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1068. tt_str_op("rc", OP_EQ, ver1.status_tag);
  1069. tt_int_op(0, OP_EQ, tor_version_parse("0.2.9.6-rc-dev", &ver1));
  1070. tt_int_op(0, OP_EQ, ver1.major);
  1071. tt_int_op(2, OP_EQ, ver1.minor);
  1072. tt_int_op(9, OP_EQ, ver1.micro);
  1073. tt_int_op(6, OP_EQ, ver1.patchlevel);
  1074. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1075. tt_str_op("rc-dev", OP_EQ, ver1.status_tag);
  1076. tt_int_op(0, OP_EQ, tor_version_parse("0.2.9.8", &ver1));
  1077. tt_int_op(0, OP_EQ, ver1.major);
  1078. tt_int_op(2, OP_EQ, ver1.minor);
  1079. tt_int_op(9, OP_EQ, ver1.micro);
  1080. tt_int_op(8, OP_EQ, ver1.patchlevel);
  1081. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1082. tt_str_op("", OP_EQ, ver1.status_tag);
  1083. tt_int_op(0, OP_EQ, tor_version_parse("0.2.9.9-dev", &ver1));
  1084. tt_int_op(0, OP_EQ, ver1.major);
  1085. tt_int_op(2, OP_EQ, ver1.minor);
  1086. tt_int_op(9, OP_EQ, ver1.micro);
  1087. tt_int_op(9, OP_EQ, ver1.patchlevel);
  1088. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1089. tt_str_op("dev", OP_EQ, ver1.status_tag);
  1090. /* In #21450, we fixed an inconsistency in parsing versions > INT32_MAX
  1091. * between i386 and x86_64, as we used tor_parse_long, and then cast to int
  1092. */
  1093. tt_int_op(0, OP_EQ, tor_version_parse("0.2147483647.0", &ver1));
  1094. tt_int_op(0, OP_EQ, ver1.major);
  1095. tt_int_op(2147483647, OP_EQ, ver1.minor);
  1096. tt_int_op(0, OP_EQ, ver1.micro);
  1097. tt_int_op(0, OP_EQ, ver1.patchlevel);
  1098. tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
  1099. tt_str_op("", OP_EQ, ver1.status_tag);
  1100. tt_int_op(-1, OP_EQ, tor_version_parse("0.2147483648.0", &ver1));
  1101. tt_int_op(-1, OP_EQ, tor_version_parse("0.4294967295.0", &ver1));
  1102. /* In #21278, we reject negative version components */
  1103. tt_int_op(-1, OP_EQ, tor_version_parse("0.-1.0", &ver1));
  1104. tt_int_op(-1, OP_EQ, tor_version_parse("0.-2147483648.0", &ver1));
  1105. tt_int_op(-1, OP_EQ, tor_version_parse("0.-4294967295.0", &ver1));
  1106. /* In #21507, we reject version components with non-numeric prefixes */
  1107. tt_int_op(-1, OP_EQ, tor_version_parse("0.-0.0", &ver1));
  1108. tt_int_op(-1, OP_EQ, tor_version_parse("+1.0.0", &ver1));
  1109. /* use the list in isspace() */
  1110. tt_int_op(-1, OP_EQ, tor_version_parse("0.\t0.0", &ver1));
  1111. tt_int_op(-1, OP_EQ, tor_version_parse("0.\n0.0", &ver1));
  1112. tt_int_op(-1, OP_EQ, tor_version_parse("0.\v0.0", &ver1));
  1113. tt_int_op(-1, OP_EQ, tor_version_parse("0.\f0.0", &ver1));
  1114. tt_int_op(-1, OP_EQ, tor_version_parse("0.\r0.0", &ver1));
  1115. tt_int_op(-1, OP_EQ, tor_version_parse("0. 0.0", &ver1));
  1116. #define tt_versionstatus_op(vs1, op, vs2) \
  1117. tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
  1118. (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
  1119. #define test_v_i_o(val, ver, lst) \
  1120. tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
  1121. /* make sure tor_version_is_obsolete() works */
  1122. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  1123. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  1124. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  1125. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  1126. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  1127. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  1128. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  1129. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  1130. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  1131. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  1132. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  1133. test_v_i_o(VS_NEW, "0.2.9.9-dev", "0.2.9.9");
  1134. /* Not on list, but newer than any in same series. */
  1135. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  1136. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1137. /* Series newer than any on list. */
  1138. test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1139. /* Series older than any on list. */
  1140. test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1141. /* Not on list, not newer than any on same series. */
  1142. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  1143. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1144. /* On list, not newer than any on same series. */
  1145. test_v_i_o(VS_UNRECOMMENDED,
  1146. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1147. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1148. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1149. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1150. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  1151. "0.0.8rc2"));
  1152. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1153. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1154. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1155. /* Now try svn revisions. */
  1156. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  1157. "Tor 0.2.1.0-dev (r99)"));
  1158. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1159. "Tor 0.2.1.0-dev (r100) on Banana Jr",
  1160. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  1161. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  1162. "Tor 0.2.1.0-dev on Colossus"));
  1163. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  1164. "Tor 0.2.1.0-dev (r100)"));
  1165. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  1166. "Tor 0.2.1.0-dev (r100) on AM"));
  1167. tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev",
  1168. "Tor 0.2.1.0-dev (r99)"));
  1169. tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.1",
  1170. "Tor 0.2.1.0-dev (r99)"));
  1171. /* And git revisions */
  1172. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1173. "Tor 0.2.9.9 (git-56788a2489127072)",
  1174. "Tor 0.2.9.9 (git-56788a2489127072)"));
  1175. /* a git revision is newer than no git revision */
  1176. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1177. "Tor 0.2.9.9 (git-56788a2489127072)",
  1178. "Tor 0.2.9.9"));
  1179. /* a longer git revision is newer than a shorter git revision
  1180. * this should be true if they prefix-match, but if they don't, they are
  1181. * incomparable, because hashes aren't ordered (but we compare their bytes
  1182. * anyway) */
  1183. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1184. "Tor 0.2.9.9 (git-56788a2489127072d513cf4baf35a8ff475f3c7b)",
  1185. "Tor 0.2.9.9 (git-56788a2489127072)"));
  1186. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1187. "Tor 0.2.9.9 (git-0102)",
  1188. "Tor 0.2.9.9 (git-03)"));
  1189. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1190. "Tor 0.2.9.9 (git-0102)",
  1191. "Tor 0.2.9.9 (git-00)"));
  1192. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1193. "Tor 0.2.9.9 (git-01)",
  1194. "Tor 0.2.9.9 (git-00)"));
  1195. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1196. "Tor 0.2.9.9 (git-00)",
  1197. "Tor 0.2.9.9 (git-01)"));
  1198. /* In #21278, we compare without integer overflows.
  1199. * But since #21450 limits version components to [0, INT32_MAX], it is no
  1200. * longer possible to cause an integer overflow in tor_version_compare() */
  1201. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1202. "Tor 0.0.0.0",
  1203. "Tor 2147483647.0.0.0"));
  1204. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1205. "Tor 2147483647.0.0.0",
  1206. "Tor 0.0.0.0"));
  1207. /* These versions used to cause an overflow, now they don't parse
  1208. * (and authorities reject their descriptors), and log a BUG message */
  1209. setup_full_capture_of_logs(LOG_WARN);
  1210. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1211. "Tor 0.0.0.0",
  1212. "Tor 0.-2147483648.0.0"));
  1213. expect_single_log_msg_containing("unparseable");
  1214. mock_clean_saved_logs();
  1215. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1216. "Tor 0.2147483647.0.0",
  1217. "Tor 0.-1.0.0"));
  1218. expect_single_log_msg_containing("unparseable");
  1219. mock_clean_saved_logs();
  1220. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1221. "Tor 0.2147483647.0.0",
  1222. "Tor 0.-2147483648.0.0"));
  1223. expect_single_log_msg_containing("unparseable");
  1224. mock_clean_saved_logs();
  1225. tt_int_op(1,OP_EQ, tor_version_as_new_as(
  1226. "Tor 4294967295.0.0.0",
  1227. "Tor 0.0.0.0"));
  1228. expect_no_log_entry();
  1229. tt_int_op(0,OP_EQ, tor_version_as_new_as(
  1230. "Tor 0.4294967295.0.0",
  1231. "Tor 0.-4294967295.0.0"));
  1232. expect_single_log_msg_containing("unparseable");
  1233. mock_clean_saved_logs();
  1234. teardown_capture_of_logs();
  1235. /* Now try git revisions */
  1236. tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
  1237. tt_int_op(0,OP_EQ, ver1.major);
  1238. tt_int_op(5,OP_EQ, ver1.minor);
  1239. tt_int_op(6,OP_EQ, ver1.micro);
  1240. tt_int_op(7,OP_EQ, ver1.patchlevel);
  1241. tt_int_op(3,OP_EQ, ver1.git_tag_len);
  1242. tt_mem_op(ver1.git_tag,OP_EQ, "\xff\x00\xff", 3);
  1243. /* reject bad hex digits */
  1244. tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
  1245. /* reject odd hex digit count */
  1246. tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
  1247. /* ignore "git " */
  1248. tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
  1249. /* standard length is 16 hex digits */
  1250. tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git-0010203040506070)",
  1251. &ver1));
  1252. /* length limit is 40 hex digits */
  1253. tt_int_op(0,OP_EQ, tor_version_parse(
  1254. "0.5.6.7 (git-000102030405060708090a0b0c0d0e0f10111213)",
  1255. &ver1));
  1256. tt_int_op(-1,OP_EQ, tor_version_parse(
  1257. "0.5.6.7 (git-000102030405060708090a0b0c0d0e0f1011121314)",
  1258. &ver1));
  1259. done:
  1260. teardown_capture_of_logs();
  1261. }
  1262. /** Run unit tests for directory fp_pair functions. */
  1263. static void
  1264. test_dir_fp_pairs(void *arg)
  1265. {
  1266. smartlist_t *sl = smartlist_new();
  1267. fp_pair_t *pair;
  1268. (void)arg;
  1269. dir_split_resource_into_fingerprint_pairs(
  1270. /* Two pairs, out of order, with one duplicate. */
  1271. "73656372657420646174612E0000000000FFFFFF-"
  1272. "557365204145532d32353620696e73746561642e+"
  1273. "73656372657420646174612E0000000000FFFFFF-"
  1274. "557365204145532d32353620696e73746561642e+"
  1275. "48657861646563696d616c2069736e277420736f-"
  1276. "676f6f6420666f7220686964696e6720796f7572.z", sl);
  1277. tt_int_op(smartlist_len(sl),OP_EQ, 2);
  1278. pair = smartlist_get(sl, 0);
  1279. tt_mem_op(pair->first,OP_EQ, "Hexadecimal isn't so", DIGEST_LEN);
  1280. tt_mem_op(pair->second,OP_EQ, "good for hiding your", DIGEST_LEN);
  1281. pair = smartlist_get(sl, 1);
  1282. tt_mem_op(pair->first,OP_EQ, "secret data.\0\0\0\0\0\xff\xff\xff",
  1283. DIGEST_LEN);
  1284. tt_mem_op(pair->second,OP_EQ, "Use AES-256 instead.", DIGEST_LEN);
  1285. done:
  1286. SMARTLIST_FOREACH(sl, fp_pair_t *, pair_to_free, tor_free(pair_to_free));
  1287. smartlist_free(sl);
  1288. }
  1289. static void
  1290. test_dir_split_fps(void *testdata)
  1291. {
  1292. smartlist_t *sl = smartlist_new();
  1293. char *mem_op_hex_tmp = NULL;
  1294. (void)testdata;
  1295. /* Some example hex fingerprints and their base64 equivalents */
  1296. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  1297. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  1298. #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
  1299. #define HEX256_1 \
  1300. "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
  1301. #define HEX256_2 \
  1302. "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
  1303. #define HEX256_3 \
  1304. "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
  1305. #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
  1306. #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
  1307. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  1308. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  1309. /* no flags set */
  1310. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  1311. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1312. tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
  1313. tt_str_op(smartlist_get(sl, 1), OP_EQ, "C");
  1314. tt_str_op(smartlist_get(sl, 2), OP_EQ, "B");
  1315. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1316. smartlist_clear(sl);
  1317. /* uniq strings. */
  1318. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  1319. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1320. tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
  1321. tt_str_op(smartlist_get(sl, 1), OP_EQ, "B");
  1322. tt_str_op(smartlist_get(sl, 2), OP_EQ, "C");
  1323. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1324. smartlist_clear(sl);
  1325. /* Decode hex. */
  1326. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  1327. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1328. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1329. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1330. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1331. smartlist_clear(sl);
  1332. /* decode hex and drop weirdness. */
  1333. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  1334. sl, NULL, DSR_HEX);
  1335. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1336. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1337. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1338. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1339. smartlist_clear(sl);
  1340. /* Decode long hex */
  1341. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  1342. sl, NULL, DSR_HEX|DSR_DIGEST256);
  1343. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1344. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1345. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1346. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_3);
  1347. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1348. smartlist_clear(sl);
  1349. /* Decode hex and sort. */
  1350. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  1351. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  1352. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1353. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX3);
  1354. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1355. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX1);
  1356. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1357. smartlist_clear(sl);
  1358. /* Decode long hex and sort */
  1359. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  1360. "+"HEX256_1,
  1361. sl, NULL,
  1362. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  1363. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  1364. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_3);
  1365. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1366. test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_1);
  1367. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1368. smartlist_clear(sl);
  1369. /* Decode base64 */
  1370. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  1371. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1372. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
  1373. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
  1374. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1375. smartlist_clear(sl);
  1376. /* Decode long base64 */
  1377. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  1378. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  1379. tt_int_op(smartlist_len(sl), OP_EQ, 2);
  1380. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1381. test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
  1382. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1383. smartlist_clear(sl);
  1384. dir_split_resource_into_fingerprints(B64_256_1,
  1385. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  1386. tt_int_op(smartlist_len(sl), OP_EQ, 1);
  1387. test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
  1388. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1389. smartlist_clear(sl);
  1390. done:
  1391. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1392. smartlist_free(sl);
  1393. tor_free(mem_op_hex_tmp);
  1394. }
  1395. static void
  1396. test_dir_measured_bw_kb(void *arg)
  1397. {
  1398. measured_bw_line_t mbwl;
  1399. int i;
  1400. const char *lines_pass[] = {
  1401. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  1402. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  1403. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  1404. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  1405. "bw=1024 junk=007\n",
  1406. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  1407. "bw=1024 junk=007\n",
  1408. /* check whether node_id can be at the end */
  1409. "bw=1024 node_id=$557365204145532d32353620696e73746561642e\n",
  1410. /* check whether node_id can be at the end and bw has something in front*/
  1411. "foo=bar bw=1024 node_id=$557365204145532d32353620696e73746561642e\n",
  1412. /* check whether node_id can be at the end and something in the
  1413. * in the middle of bw and node_id */
  1414. "bw=1024 foo=bar node_id=$557365204145532d32353620696e73746561642e\n",
  1415. "end"
  1416. };
  1417. const char *lines_fail[] = {
  1418. /* Test possible python stupidity on input */
  1419. "node_id=None bw=1024\n",
  1420. "node_id=$None bw=1024\n",
  1421. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  1422. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  1423. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  1424. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  1425. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  1426. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  1427. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  1428. /* Test incomplete writes due to race conditions, partial copies, etc */
  1429. "node_i",
  1430. "node_i\n",
  1431. "node_id=",
  1432. "node_id=\n",
  1433. "node_id=$557365204145532d32353620696e73746561642e bw=",
  1434. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  1435. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  1436. "node_id=$557365204145532d32353620696e7374",
  1437. "node_id=$557365204145532d32353620696e7374\n",
  1438. "",
  1439. "\n",
  1440. " \n ",
  1441. " \n\n",
  1442. /* Test assorted noise */
  1443. " node_id= ",
  1444. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  1445. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  1446. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  1447. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  1448. "end"
  1449. };
  1450. (void)arg;
  1451. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  1452. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  1453. /* Testing only with line_is_after_headers = 1. Tests with
  1454. * line_is_after_headers = 0 in
  1455. * test_dir_measured_bw_kb_line_is_after_headers */
  1456. tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i], 1) == -1);
  1457. }
  1458. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  1459. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  1460. /* Testing only with line_is_after_headers = 1. Tests with
  1461. * line_is_after_headers = 0 in
  1462. * test_dir_measured_bw_kb_line_is_after_headers */
  1463. tt_assert(measured_bw_line_parse(&mbwl, lines_pass[i], 1) == 0);
  1464. tt_assert(mbwl.bw_kb == 1024);
  1465. tt_assert(strcmp(mbwl.node_hex,
  1466. "557365204145532d32353620696e73746561642e") == 0);
  1467. }
  1468. done:
  1469. return;
  1470. }
  1471. /* Unit tests for measured_bw_line_parse using line_is_after_headers flag.
  1472. * When the end of the header is detected (a first complete bw line is parsed),
  1473. * incomplete lines fail and give warnings, but do not give warnings if
  1474. * the header is not ended, allowing to ignore additional header lines. */
  1475. static void
  1476. test_dir_measured_bw_kb_line_is_after_headers(void *arg)
  1477. {
  1478. (void)arg;
  1479. measured_bw_line_t mbwl;
  1480. const char *line_pass = \
  1481. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n";
  1482. int i;
  1483. const char *lines_fail[] = {
  1484. "node_id=$557365204145532d32353620696e73746561642e \n",
  1485. "bw=1024\n",
  1486. "rtt=300\n",
  1487. "end"
  1488. };
  1489. setup_capture_of_logs(LOG_DEBUG);
  1490. /* Test bw lines when header has ended */
  1491. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  1492. tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i], 1) == -1);
  1493. expect_log_msg_containing("Incomplete line in bandwidth file:");
  1494. mock_clean_saved_logs();
  1495. }
  1496. tt_assert(measured_bw_line_parse(&mbwl, line_pass, 1) == 0);
  1497. /* Test bw lines when header has not ended */
  1498. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  1499. tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i], 0) == -1);
  1500. expect_log_msg_containing("Missing bw or node_id in bandwidth file line:");
  1501. mock_clean_saved_logs();
  1502. }
  1503. tt_assert(measured_bw_line_parse(&mbwl, line_pass, 0) == 0);
  1504. done:
  1505. teardown_capture_of_logs();
  1506. }
  1507. /* Test dirserv_read_measured_bandwidths with headers and complete files. */
  1508. static void
  1509. test_dir_dirserv_read_measured_bandwidths(void *arg)
  1510. {
  1511. (void)arg;
  1512. char *content = NULL;
  1513. time_t timestamp = time(NULL);
  1514. char *fname = tor_strdup(get_fname("V3BandwidthsFile"));
  1515. smartlist_t *bw_file_headers = smartlist_new();
  1516. /* bw file strings in vote */
  1517. char *bw_file_headers_str = NULL;
  1518. char *bw_file_headers_str_v100 = NULL;
  1519. char *bw_file_headers_str_v110 = NULL;
  1520. char *bw_file_headers_str_bad = NULL;
  1521. char *bw_file_headers_str_extra = NULL;
  1522. char bw_file_headers_str_long[MAX_BW_FILE_HEADER_COUNT_IN_VOTE * 8 + 1] = "";
  1523. /* string header lines in bw file */
  1524. char *header_lines_v100 = NULL;
  1525. char *header_lines_v110_no_terminator = NULL;
  1526. char *header_lines_v110 = NULL;
  1527. char header_lines_long[MAX_BW_FILE_HEADER_COUNT_IN_VOTE * 8 + 1] = "";
  1528. int i;
  1529. const char *header_lines_v110_no_terminator_no_timestamp =
  1530. "version=1.1.0\n"
  1531. "software=sbws\n"
  1532. "software_version=0.1.0\n"
  1533. "earliest_bandwidth=2018-05-08T16:13:26\n"
  1534. "file_created=2018-04-16T21:49:18\n"
  1535. "generator_started=2018-05-08T16:13:25\n"
  1536. "latest_bandwidth=2018-04-16T20:49:18\n";
  1537. const char *bw_file_headers_str_v110_no_timestamp =
  1538. "version=1.1.0 software=sbws "
  1539. "software_version=0.1.0 "
  1540. "earliest_bandwidth=2018-05-08T16:13:26 "
  1541. "file_created=2018-04-16T21:49:18 "
  1542. "generator_started=2018-05-08T16:13:25 "
  1543. "latest_bandwidth=2018-04-16T20:49:18";
  1544. const char *relay_lines_v100 =
  1545. "node_id=$557365204145532d32353620696e73746561642e bw=1024 "
  1546. "nick=Test measured_at=1523911725 updated_at=1523911725 "
  1547. "pid_error=4.11374090719 pid_error_sum=4.11374090719 "
  1548. "pid_bw=57136645 pid_delta=2.12168374577 circ_fail=0.2 "
  1549. "scanner=/filepath\n";
  1550. const char *relay_lines_v110 =
  1551. "node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 "
  1552. "master_key_ed25519=YaqV4vbvPYKucElk297eVdNArDz9HtIwUoIeo0+cVIpQ "
  1553. "bw=760 nick=Test rtt=380 time=2018-05-08T16:13:26\n";
  1554. const char *relay_lines_bad =
  1555. "node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A\n";
  1556. tor_asprintf(&header_lines_v100, "%ld\n", (long)timestamp);
  1557. tor_asprintf(&header_lines_v110_no_terminator, "%ld\n%s", (long)timestamp,
  1558. header_lines_v110_no_terminator_no_timestamp);
  1559. tor_asprintf(&header_lines_v110, "%s%s",
  1560. header_lines_v110_no_terminator, BW_FILE_HEADERS_TERMINATOR);
  1561. tor_asprintf(&bw_file_headers_str_v100, "timestamp=%ld",(long)timestamp);
  1562. tor_asprintf(&bw_file_headers_str_v110, "timestamp=%ld %s",
  1563. (long)timestamp, bw_file_headers_str_v110_no_timestamp);
  1564. tor_asprintf(&bw_file_headers_str_bad, "%s "
  1565. "node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A",
  1566. bw_file_headers_str_v110);
  1567. for (i=0; i<MAX_BW_FILE_HEADER_COUNT_IN_VOTE; i++) {
  1568. strlcat(header_lines_long, "foo=bar\n",
  1569. sizeof(header_lines_long));
  1570. }
  1571. /* 8 is the number of v110 lines in header_lines_v110 */
  1572. for (i=0; i<MAX_BW_FILE_HEADER_COUNT_IN_VOTE - 8 - 1; i++) {
  1573. strlcat(bw_file_headers_str_long, "foo=bar ",
  1574. sizeof(bw_file_headers_str_long));
  1575. }
  1576. strlcat(bw_file_headers_str_long, "foo=bar",
  1577. sizeof(bw_file_headers_str_long));
  1578. tor_asprintf(&bw_file_headers_str_extra,
  1579. "%s %s",
  1580. bw_file_headers_str_v110,
  1581. bw_file_headers_str_long);
  1582. /* Test an empty bandwidth file. bw_file_headers will be empty string */
  1583. write_str_to_file(fname, "", 0);
  1584. setup_capture_of_logs(LOG_WARN);
  1585. tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1586. bw_file_headers));
  1587. expect_log_msg("Empty bandwidth file\n");
  1588. teardown_capture_of_logs();
  1589. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1590. tt_str_op("", OP_EQ, bw_file_headers_str);
  1591. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1592. smartlist_free(bw_file_headers);
  1593. tor_free(bw_file_headers_str);
  1594. /* Test bandwidth file with only timestamp.
  1595. * bw_file_headers will be empty string */
  1596. bw_file_headers = smartlist_new();
  1597. tor_asprintf(&content, "%ld", (long)timestamp);
  1598. write_str_to_file(fname, content, 0);
  1599. tor_free(content);
  1600. tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1601. bw_file_headers));
  1602. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1603. tt_str_op("", OP_EQ, bw_file_headers_str);
  1604. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1605. smartlist_free(bw_file_headers);
  1606. tor_free(bw_file_headers_str);
  1607. /* Test v1.0.0 bandwidth file headers */
  1608. write_str_to_file(fname, header_lines_v100, 0);
  1609. bw_file_headers = smartlist_new();
  1610. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1611. bw_file_headers));
  1612. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1613. tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
  1614. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1615. smartlist_free(bw_file_headers);
  1616. tor_free(bw_file_headers_str);
  1617. /* Test v1.0.0 complete bandwidth file */
  1618. bw_file_headers = smartlist_new();
  1619. tor_asprintf(&content, "%s%s", header_lines_v100, relay_lines_v100);
  1620. write_str_to_file(fname, content, 0);
  1621. tor_free(content);
  1622. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1623. bw_file_headers));
  1624. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1625. tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
  1626. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1627. smartlist_free(bw_file_headers);
  1628. tor_free(bw_file_headers_str);
  1629. /* Test v1.0.0 complete bandwidth file with NULL bw_file_headers. */
  1630. tor_asprintf(&content, "%s%s", header_lines_v100, relay_lines_v100);
  1631. write_str_to_file(fname, content, 0);
  1632. tor_free(content);
  1633. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL, NULL));
  1634. /* Test bandwidth file including v1.1.0 bandwidth headers and
  1635. * v1.0.0 relay lines. bw_file_headers will contain the v1.1.0 headers. */
  1636. bw_file_headers = smartlist_new();
  1637. tor_asprintf(&content, "%s%s%s", header_lines_v100, header_lines_v110,
  1638. relay_lines_v100);
  1639. write_str_to_file(fname, content, 0);
  1640. tor_free(content);
  1641. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1642. bw_file_headers));
  1643. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1644. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1645. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1646. smartlist_free(bw_file_headers);
  1647. tor_free(bw_file_headers_str);
  1648. /* Test v1.0.0 complete bandwidth file with v1.1.0 headers at the end.
  1649. * bw_file_headers will contain only v1.0.0 headers and the additional
  1650. * headers will be interpreted as malformed relay lines. */
  1651. bw_file_headers = smartlist_new();
  1652. tor_asprintf(&content, "%s%s%s", header_lines_v100, relay_lines_v100,
  1653. header_lines_v110);
  1654. write_str_to_file(fname, content, 0);
  1655. tor_free(content);
  1656. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1657. bw_file_headers));
  1658. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1659. tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
  1660. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1661. smartlist_free(bw_file_headers);
  1662. tor_free(bw_file_headers_str);
  1663. /* Test v1.0.0 complete bandwidth file, the v1.1.0 headers and more relay
  1664. * lines. bw_file_headers will contain only v1.0.0 headers, the additional
  1665. * headers will be interpreted as malformed relay lines and the last relay
  1666. * lines will be correctly interpreted as relay lines. */
  1667. bw_file_headers = smartlist_new();
  1668. tor_asprintf(&content, "%s%s%s%s", header_lines_v100, relay_lines_v100,
  1669. header_lines_v110, relay_lines_v100);
  1670. write_str_to_file(fname, content, 0);
  1671. tor_free(content);
  1672. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1673. bw_file_headers));
  1674. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1675. tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
  1676. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1677. smartlist_free(bw_file_headers);
  1678. tor_free(bw_file_headers_str);
  1679. /* Test v1.1.0 bandwidth headers without terminator */
  1680. bw_file_headers = smartlist_new();
  1681. write_str_to_file(fname, header_lines_v110_no_terminator, 0);
  1682. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1683. bw_file_headers));
  1684. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1685. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1686. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1687. smartlist_free(bw_file_headers);
  1688. tor_free(bw_file_headers_str);
  1689. /* Test v1.1.0 bandwidth headers with terminator */
  1690. bw_file_headers = smartlist_new();
  1691. write_str_to_file(fname, header_lines_v110, 0);
  1692. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1693. bw_file_headers));
  1694. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1695. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1696. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1697. smartlist_free(bw_file_headers);
  1698. tor_free(bw_file_headers_str);
  1699. /* Test v1.1.0 bandwidth file without terminator, then relay lines.
  1700. * bw_file_headers will contain the v1.1.0 headers. */
  1701. bw_file_headers = smartlist_new();
  1702. tor_asprintf(&content, "%s%s",
  1703. header_lines_v110_no_terminator, relay_lines_v110);
  1704. write_str_to_file(fname, content, 0);
  1705. tor_free(content);
  1706. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1707. bw_file_headers));
  1708. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1709. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1710. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1711. smartlist_free(bw_file_headers);
  1712. tor_free(bw_file_headers_str);
  1713. /* Test v1.1.0 bandwidth headers with terminator, then relay lines
  1714. * bw_file_headers will contain the v1.1.0 headers. */
  1715. bw_file_headers = smartlist_new();
  1716. tor_asprintf(&content, "%s%s",
  1717. header_lines_v110, relay_lines_v110);
  1718. write_str_to_file(fname, content, 0);
  1719. tor_free(content);
  1720. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1721. bw_file_headers));
  1722. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1723. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1724. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1725. smartlist_free(bw_file_headers);
  1726. tor_free(bw_file_headers_str);
  1727. /* Test v1.1.0 bandwidth headers with terminator, then bad relay lines,
  1728. * then terminator, then relay_lines_bad.
  1729. * bw_file_headers will contain the v1.1.0 headers. */
  1730. bw_file_headers = smartlist_new();
  1731. tor_asprintf(&content, "%s%s%s%s", header_lines_v110, relay_lines_bad,
  1732. BW_FILE_HEADERS_TERMINATOR, relay_lines_bad);
  1733. write_str_to_file(fname, content, 0);
  1734. tor_free(content);
  1735. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1736. bw_file_headers));
  1737. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1738. tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
  1739. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1740. smartlist_free(bw_file_headers);
  1741. tor_free(bw_file_headers_str);
  1742. /* Test v1.1.0 bandwidth headers without terminator, then bad relay lines,
  1743. * then relay lines. bw_file_headers will contain the v1.1.0 headers and
  1744. * the bad relay lines. */
  1745. bw_file_headers = smartlist_new();
  1746. tor_asprintf(&content, "%s%s%s",
  1747. header_lines_v110_no_terminator, relay_lines_bad,
  1748. relay_lines_v110);
  1749. write_str_to_file(fname, content, 0);
  1750. tor_free(content);
  1751. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1752. bw_file_headers));
  1753. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1754. tt_str_op(bw_file_headers_str_bad, OP_EQ, bw_file_headers_str);
  1755. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1756. smartlist_free(bw_file_headers);
  1757. tor_free(bw_file_headers_str);
  1758. /* Test v1.1.0 bandwidth headers without terminator,
  1759. * then many bad relay lines, then relay lines.
  1760. * bw_file_headers will contain the v1.1.0 headers and the bad relay lines
  1761. * to a maximum of MAX_BW_FILE_HEADER_COUNT_IN_VOTE header lines. */
  1762. bw_file_headers = smartlist_new();
  1763. tor_asprintf(&content, "%s%s%s",
  1764. header_lines_v110_no_terminator, header_lines_long,
  1765. relay_lines_v110);
  1766. write_str_to_file(fname, content, 0);
  1767. tor_free(content);
  1768. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1769. bw_file_headers));
  1770. tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_EQ,
  1771. smartlist_len(bw_file_headers));
  1772. bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
  1773. tt_str_op(bw_file_headers_str_extra, OP_EQ, bw_file_headers_str);
  1774. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1775. smartlist_free(bw_file_headers);
  1776. tor_free(bw_file_headers_str);
  1777. /* Test v1.1.0 bandwidth headers without terminator,
  1778. * then many bad relay lines, then relay lines.
  1779. * bw_file_headers will contain the v1.1.0 headers and the bad relay lines.
  1780. * Force bw_file_headers to have more than MAX_BW_FILE_HEADER_COUNT_IN_VOTE
  1781. * This test is needed while there is not dirvote test. */
  1782. bw_file_headers = smartlist_new();
  1783. tor_asprintf(&content, "%s%s%s",
  1784. header_lines_v110_no_terminator, header_lines_long,
  1785. relay_lines_v110);
  1786. write_str_to_file(fname, content, 0);
  1787. tor_free(content);
  1788. tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
  1789. bw_file_headers));
  1790. tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_EQ,
  1791. smartlist_len(bw_file_headers));
  1792. /* force bw_file_headers to be bigger than
  1793. * MAX_BW_FILE_HEADER_COUNT_IN_VOTE */
  1794. char line[8] = "foo=bar\0";
  1795. smartlist_add_strdup(bw_file_headers, line);
  1796. tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_LT,
  1797. smartlist_len(bw_file_headers));
  1798. SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
  1799. smartlist_free(bw_file_headers);
  1800. tor_free(bw_file_headers_str);
  1801. done:
  1802. tor_free(fname);
  1803. tor_free(header_lines_v100);
  1804. tor_free(header_lines_v110_no_terminator);
  1805. tor_free(header_lines_v110);
  1806. tor_free(bw_file_headers_str_v100);
  1807. tor_free(bw_file_headers_str_v110);
  1808. tor_free(bw_file_headers_str_bad);
  1809. tor_free(bw_file_headers_str_extra);
  1810. }
  1811. #define MBWC_INIT_TIME 1000
  1812. /** Do the measured bandwidth cache unit test */
  1813. static void
  1814. test_dir_measured_bw_kb_cache(void *arg)
  1815. {
  1816. /* Initial fake time_t for testing */
  1817. time_t curr = MBWC_INIT_TIME;
  1818. /* Some measured_bw_line_ts */
  1819. measured_bw_line_t mbwl[3];
  1820. /* For receiving output on cache queries */
  1821. long bw;
  1822. time_t as_of;
  1823. /* First, clear the cache and assert that it's empty */
  1824. (void)arg;
  1825. dirserv_clear_measured_bw_cache();
  1826. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1827. /*
  1828. * Set up test mbwls; none of the dirserv_cache_*() functions care about
  1829. * the node_hex field.
  1830. */
  1831. memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
  1832. mbwl[0].bw_kb = 20;
  1833. memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
  1834. mbwl[1].bw_kb = 40;
  1835. memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
  1836. mbwl[2].bw_kb = 80;
  1837. /* Try caching something */
  1838. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  1839. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1840. /* Okay, let's see if we can retrieve it */
  1841. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, &as_of));
  1842. tt_int_op(bw,OP_EQ, 20);
  1843. tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
  1844. /* Try retrieving it without some outputs */
  1845. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL, NULL));
  1846. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, NULL));
  1847. tt_int_op(bw,OP_EQ, 20);
  1848. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL,&as_of));
  1849. tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
  1850. /* Now expire it */
  1851. curr += MAX_MEASUREMENT_AGE + 1;
  1852. dirserv_expire_measured_bw_cache(curr);
  1853. /* Check that the cache is empty */
  1854. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1855. /* Check that we can't retrieve it */
  1856. tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl[0].node_id, NULL,NULL));
  1857. /* Try caching a few things now */
  1858. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  1859. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1860. curr += MAX_MEASUREMENT_AGE / 4;
  1861. dirserv_cache_measured_bw(&(mbwl[1]), curr);
  1862. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
  1863. curr += MAX_MEASUREMENT_AGE / 4;
  1864. dirserv_cache_measured_bw(&(mbwl[2]), curr);
  1865. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
  1866. curr += MAX_MEASUREMENT_AGE / 4 + 1;
  1867. /* Do an expire that's too soon to get any of them */
  1868. dirserv_expire_measured_bw_cache(curr);
  1869. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
  1870. /* Push the oldest one off the cliff */
  1871. curr += MAX_MEASUREMENT_AGE / 4;
  1872. dirserv_expire_measured_bw_cache(curr);
  1873. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
  1874. /* And another... */
  1875. curr += MAX_MEASUREMENT_AGE / 4;
  1876. dirserv_expire_measured_bw_cache(curr);
  1877. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
  1878. /* This should empty it out again */
  1879. curr += MAX_MEASUREMENT_AGE / 4;
  1880. dirserv_expire_measured_bw_cache(curr);
  1881. tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
  1882. done:
  1883. return;
  1884. }
  1885. static char *
  1886. my_dirvote_compute_params(smartlist_t *votes, int method,
  1887. int total_authorities)
  1888. {
  1889. smartlist_t *s = dirvote_compute_params(votes, method, total_authorities);
  1890. tor_assert(s);
  1891. char *res = smartlist_join_strings(s, " ", 0, NULL);
  1892. SMARTLIST_FOREACH(s, char *, cp, tor_free(cp));
  1893. smartlist_free(s);
  1894. return res;
  1895. }
  1896. #define dirvote_compute_params my_dirvote_compute_params
  1897. static void
  1898. test_dir_param_voting(void *arg)
  1899. {
  1900. networkstatus_t vote1, vote2, vote3, vote4;
  1901. smartlist_t *votes = smartlist_new();
  1902. char *res = NULL;
  1903. /* dirvote_compute_params only looks at the net_params field of the votes,
  1904. so that's all we need to set.
  1905. */
  1906. (void)arg;
  1907. memset(&vote1, 0, sizeof(vote1));
  1908. memset(&vote2, 0, sizeof(vote2));
  1909. memset(&vote3, 0, sizeof(vote3));
  1910. memset(&vote4, 0, sizeof(vote4));
  1911. vote1.net_params = smartlist_new();
  1912. vote2.net_params = smartlist_new();
  1913. vote3.net_params = smartlist_new();
  1914. vote4.net_params = smartlist_new();
  1915. smartlist_split_string(vote1.net_params,
  1916. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  1917. smartlist_split_string(vote2.net_params,
  1918. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  1919. smartlist_split_string(vote3.net_params,
  1920. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  1921. smartlist_split_string(vote4.net_params,
  1922. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  1923. tt_int_op(100,OP_EQ, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  1924. tt_int_op(222,OP_EQ, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  1925. tt_int_op(80,OP_EQ, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  1926. tt_int_op(-8,OP_EQ, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  1927. tt_int_op(0,OP_EQ, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  1928. tt_int_op(100,OP_EQ, networkstatus_get_overridable_param(
  1929. &vote4, -1, "x-yz", 50, 0, 300));
  1930. tt_int_op(30,OP_EQ, networkstatus_get_overridable_param(
  1931. &vote4, 30, "x-yz", 50, 0, 300));
  1932. tt_int_op(0,OP_EQ, networkstatus_get_overridable_param(
  1933. &vote4, -101, "foobar", 0, -100, 8));
  1934. tt_int_op(-99,OP_EQ, networkstatus_get_overridable_param(
  1935. &vote4, -99, "foobar", 0, -100, 8));
  1936. smartlist_add(votes, &vote1);
  1937. /* Do the first tests without adding all the other votes, for
  1938. * networks without many dirauths. */
  1939. res = dirvote_compute_params(votes, 12, 2);
  1940. tt_str_op(res,OP_EQ, "");
  1941. tor_free(res);
  1942. res = dirvote_compute_params(votes, 12, 1);
  1943. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-99");
  1944. tor_free(res);
  1945. smartlist_add(votes, &vote2);
  1946. res = dirvote_compute_params(votes, 12, 2);
  1947. tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
  1948. tor_free(res);
  1949. res = dirvote_compute_params(votes, 12, 3);
  1950. tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
  1951. tor_free(res);
  1952. res = dirvote_compute_params(votes, 12, 6);
  1953. tt_str_op(res,OP_EQ, "");
  1954. tor_free(res);
  1955. smartlist_add(votes, &vote3);
  1956. res = dirvote_compute_params(votes, 12, 3);
  1957. tt_str_op(res,OP_EQ, "ab=27 abcd=20 cw=50 x-yz=-9");
  1958. tor_free(res);
  1959. res = dirvote_compute_params(votes, 12, 5);
  1960. tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
  1961. tor_free(res);
  1962. res = dirvote_compute_params(votes, 12, 9);
  1963. tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
  1964. tor_free(res);
  1965. smartlist_add(votes, &vote4);
  1966. res = dirvote_compute_params(votes, 12, 4);
  1967. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1968. tor_free(res);
  1969. res = dirvote_compute_params(votes, 12, 5);
  1970. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1971. tor_free(res);
  1972. /* Test that the special-cased "at least three dirauths voted for
  1973. * this param" logic works as expected. */
  1974. res = dirvote_compute_params(votes, 12, 6);
  1975. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1976. tor_free(res);
  1977. res = dirvote_compute_params(votes, 12, 10);
  1978. tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
  1979. tor_free(res);
  1980. done:
  1981. tor_free(res);
  1982. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  1983. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  1984. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  1985. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  1986. smartlist_free(vote1.net_params);
  1987. smartlist_free(vote2.net_params);
  1988. smartlist_free(vote3.net_params);
  1989. smartlist_free(vote4.net_params);
  1990. smartlist_free(votes);
  1991. return;
  1992. }
  1993. static void
  1994. test_dir_param_voting_lookup(void *arg)
  1995. {
  1996. (void)arg;
  1997. smartlist_t *lst = smartlist_new();
  1998. smartlist_split_string(lst,
  1999. "moomin=9 moomin=10 moomintroll=5 fred "
  2000. "jack= electricity=sdk opa=6z abc=9 abcd=99",
  2001. NULL, 0, 0);
  2002. tt_int_op(1000,
  2003. OP_EQ, dirvote_get_intermediate_param_value(lst, "ab", 1000));
  2004. tt_int_op(9, OP_EQ, dirvote_get_intermediate_param_value(lst, "abc", 1000));
  2005. tt_int_op(99, OP_EQ,
  2006. dirvote_get_intermediate_param_value(lst, "abcd", 1000));
  2007. /* moomin appears twice. That's a bug. */
  2008. tor_capture_bugs_(1);
  2009. tt_int_op(-100, OP_EQ,
  2010. dirvote_get_intermediate_param_value(lst, "moomin", -100));
  2011. tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
  2012. tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
  2013. "n_found == 0");
  2014. tor_end_capture_bugs_();
  2015. /* There is no 'fred=', so that is treated as not existing. */
  2016. tt_int_op(-100, OP_EQ,
  2017. dirvote_get_intermediate_param_value(lst, "fred", -100));
  2018. /* jack is truncated */
  2019. tor_capture_bugs_(1);
  2020. tt_int_op(-100, OP_EQ,
  2021. dirvote_get_intermediate_param_value(lst, "jack", -100));
  2022. tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
  2023. tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
  2024. "!(! ok)");
  2025. tor_end_capture_bugs_();
  2026. /* electricity and opa aren't integers. */
  2027. tor_capture_bugs_(1);
  2028. tt_int_op(-100, OP_EQ,
  2029. dirvote_get_intermediate_param_value(lst, "electricity", -100));
  2030. tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
  2031. tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
  2032. "!(! ok)");
  2033. tor_end_capture_bugs_();
  2034. tor_capture_bugs_(1);
  2035. tt_int_op(-100, OP_EQ,
  2036. dirvote_get_intermediate_param_value(lst, "opa", -100));
  2037. tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
  2038. tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
  2039. "!(! ok)");
  2040. tor_end_capture_bugs_();
  2041. done:
  2042. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  2043. smartlist_free(lst);
  2044. tor_end_capture_bugs_();
  2045. }
  2046. #undef dirvote_compute_params
  2047. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  2048. * same voting authority, and that they do in fact have all the same
  2049. * information. */
  2050. static void
  2051. test_same_voter(networkstatus_voter_info_t *v1,
  2052. networkstatus_voter_info_t *v2)
  2053. {
  2054. tt_str_op(v1->nickname,OP_EQ, v2->nickname);
  2055. tt_mem_op(v1->identity_digest,OP_EQ, v2->identity_digest, DIGEST_LEN);
  2056. tt_str_op(v1->address,OP_EQ, v2->address);
  2057. tt_int_op(v1->addr,OP_EQ, v2->addr);
  2058. tt_int_op(v1->dir_port,OP_EQ, v2->dir_port);
  2059. tt_int_op(v1->or_port,OP_EQ, v2->or_port);
  2060. tt_str_op(v1->contact,OP_EQ, v2->contact);
  2061. tt_mem_op(v1->vote_digest,OP_EQ, v2->vote_digest, DIGEST_LEN);
  2062. done:
  2063. ;
  2064. }
  2065. /** Helper: get a detached signatures document for one or two
  2066. * consensuses. */
  2067. static char *
  2068. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  2069. {
  2070. char *r;
  2071. smartlist_t *sl;
  2072. tor_assert(ns && ns->flavor == FLAV_NS);
  2073. sl = smartlist_new();
  2074. smartlist_add(sl,ns);
  2075. if (ns2)
  2076. smartlist_add(sl,ns2);
  2077. r = networkstatus_get_detached_signatures(sl);
  2078. smartlist_free(sl);
  2079. return r;
  2080. }
  2081. /** Apply tweaks to the vote list for each voter */
  2082. static int
  2083. vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
  2084. {
  2085. vote_routerstatus_t *vrs;
  2086. const char *msg = NULL;
  2087. tt_assert(v);
  2088. (void)now;
  2089. if (voter == 1) {
  2090. measured_bw_line_t mbw;
  2091. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  2092. mbw.bw_kb = 1024;
  2093. tt_int_op(measured_bw_line_apply(&mbw, v->routerstatus_list), OP_EQ, 1);
  2094. } else if (voter == 2 || voter == 3) {
  2095. /* Monkey around with the list a bit */
  2096. vrs = smartlist_get(v->routerstatus_list, 2);
  2097. smartlist_del_keeporder(v->routerstatus_list, 2);
  2098. vote_routerstatus_free(vrs);
  2099. vrs = smartlist_get(v->routerstatus_list, 0);
  2100. vrs->status.is_fast = 1;
  2101. if (voter == 3) {
  2102. vrs = smartlist_get(v->routerstatus_list, 0);
  2103. smartlist_del_keeporder(v->routerstatus_list, 0);
  2104. vote_routerstatus_free(vrs);
  2105. vrs = smartlist_get(v->routerstatus_list, 0);
  2106. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  2107. tt_assert(router_add_to_routerlist(
  2108. dir_common_generate_ri_from_rs(vrs), &msg,0,0) >= 0);
  2109. }
  2110. }
  2111. done:
  2112. return 0;
  2113. }
  2114. /**
  2115. * Test a parsed vote_routerstatus_t for v3_networkstatus test
  2116. */
  2117. static void
  2118. test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
  2119. {
  2120. routerstatus_t *rs;
  2121. tor_addr_t addr_ipv6;
  2122. tt_assert(vrs);
  2123. rs = &(vrs->status);
  2124. tt_assert(rs);
  2125. /* Split out by digests to test */
  2126. if (tor_memeq(rs->identity_digest,
  2127. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2128. "\x3\x3\x3\x3",
  2129. DIGEST_LEN) &&
  2130. (voter == 1)) {
  2131. /* Check the first routerstatus. */
  2132. tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
  2133. tt_int_op(rs->published_on,OP_EQ, now-1500);
  2134. tt_str_op(rs->nickname,OP_EQ, "router2");
  2135. tt_mem_op(rs->identity_digest,OP_EQ,
  2136. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2137. "\x3\x3\x3\x3",
  2138. DIGEST_LEN);
  2139. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2140. tt_int_op(rs->addr,OP_EQ, 0x99008801);
  2141. tt_int_op(rs->or_port,OP_EQ, 443);
  2142. tt_int_op(rs->dir_port,OP_EQ, 8000);
  2143. /* no flags except "running" (16) and "v2dir" (64) and "valid" (128) */
  2144. tt_u64_op(vrs->flags, OP_EQ, UINT64_C(0xd0));
  2145. } else if (tor_memeq(rs->identity_digest,
  2146. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2147. "\x5\x5\x5\x5",
  2148. DIGEST_LEN) &&
  2149. (voter == 1 || voter == 2)) {
  2150. tt_mem_op(rs->identity_digest,OP_EQ,
  2151. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2152. "\x5\x5\x5\x5",
  2153. DIGEST_LEN);
  2154. if (voter == 1) {
  2155. /* Check the second routerstatus. */
  2156. tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
  2157. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2158. tt_str_op(rs->nickname,OP_EQ, "router1");
  2159. }
  2160. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2161. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2162. tt_int_op(rs->or_port,OP_EQ, 443);
  2163. tt_int_op(rs->dir_port,OP_EQ, 0);
  2164. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2165. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2166. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2167. if (voter == 1) {
  2168. /* all except "authority" (1) */
  2169. tt_u64_op(vrs->flags, OP_EQ, UINT64_C(254));
  2170. } else {
  2171. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
  2172. tt_u64_op(vrs->flags, OP_EQ, UINT64_C(974));
  2173. }
  2174. } else if (tor_memeq(rs->identity_digest,
  2175. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2176. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2177. DIGEST_LEN) &&
  2178. (voter == 1 || voter == 2)) {
  2179. /* Check the measured bandwidth bits */
  2180. tt_assert(vrs->has_measured_bw &&
  2181. vrs->measured_bw_kb == 1024);
  2182. } else {
  2183. /*
  2184. * Didn't expect this, but the old unit test only checked some of them,
  2185. * so don't assert.
  2186. */
  2187. /* tt_assert(0); */
  2188. }
  2189. done:
  2190. return;
  2191. }
  2192. /**
  2193. * Test a consensus for v3_networkstatus_test
  2194. */
  2195. static void
  2196. test_consensus_for_v3ns(networkstatus_t *con, time_t now)
  2197. {
  2198. (void)now;
  2199. tt_assert(con);
  2200. tt_ptr_op(con->cert, OP_EQ, NULL);
  2201. tt_int_op(2,OP_EQ, smartlist_len(con->routerstatus_list));
  2202. /* There should be two listed routers: one with identity 3, one with
  2203. * identity 5. */
  2204. done:
  2205. return;
  2206. }
  2207. /**
  2208. * Test a router list entry for v3_networkstatus test
  2209. */
  2210. static void
  2211. test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
  2212. {
  2213. tor_addr_t addr_ipv6;
  2214. tt_assert(rs);
  2215. /* There should be two listed routers: one with identity 3, one with
  2216. * identity 5. */
  2217. /* This one showed up in 2 digests. */
  2218. if (tor_memeq(rs->identity_digest,
  2219. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2220. "\x3\x3",
  2221. DIGEST_LEN)) {
  2222. tt_mem_op(rs->identity_digest,OP_EQ,
  2223. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2224. DIGEST_LEN);
  2225. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2226. tt_assert(!rs->is_authority);
  2227. tt_assert(!rs->is_exit);
  2228. tt_assert(!rs->is_fast);
  2229. tt_assert(!rs->is_possible_guard);
  2230. tt_assert(!rs->is_stable);
  2231. /* (If it wasn't running it wouldn't be here) */
  2232. tt_assert(rs->is_flagged_running);
  2233. tt_assert(rs->is_valid);
  2234. tt_assert(!rs->is_named);
  2235. tt_assert(rs->is_v2_dir);
  2236. /* XXXX check version */
  2237. } else if (tor_memeq(rs->identity_digest,
  2238. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2239. "\x5\x5\x5\x5",
  2240. DIGEST_LEN)) {
  2241. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  2242. tt_mem_op(rs->identity_digest,OP_EQ,
  2243. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2244. DIGEST_LEN);
  2245. tt_str_op(rs->nickname,OP_EQ, "router1");
  2246. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2247. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2248. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2249. tt_int_op(rs->or_port,OP_EQ, 443);
  2250. tt_int_op(rs->dir_port,OP_EQ, 0);
  2251. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2252. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2253. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2254. tt_assert(!rs->is_authority);
  2255. tt_assert(rs->is_exit);
  2256. tt_assert(rs->is_fast);
  2257. tt_assert(rs->is_possible_guard);
  2258. tt_assert(rs->is_stable);
  2259. tt_assert(rs->is_flagged_running);
  2260. tt_assert(rs->is_valid);
  2261. tt_assert(rs->is_v2_dir);
  2262. tt_assert(!rs->is_named);
  2263. /* XXXX check version */
  2264. } else {
  2265. /* Weren't expecting this... */
  2266. tt_abort();
  2267. }
  2268. done:
  2269. return;
  2270. }
  2271. static void
  2272. test_dir_networkstatus_compute_bw_weights_v10(void *arg)
  2273. {
  2274. (void) arg;
  2275. smartlist_t *chunks = smartlist_new();
  2276. int64_t G, M, E, D, T, weight_scale;
  2277. int ret;
  2278. weight_scale = 10000;
  2279. /* no case. one or more of the values is 0 */
  2280. G = M = E = D = 0;
  2281. T = G + M + E + D;
  2282. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2283. weight_scale);
  2284. tt_int_op(ret, OP_EQ, 0);
  2285. tt_int_op(smartlist_len(chunks), OP_EQ, 0);
  2286. /* case 1 */
  2287. /* XXX dir-spec not followed? See #20272. If it isn't closed, then this is
  2288. * testing current behavior, not spec. */
  2289. G = E = 10;
  2290. M = D = 1;
  2291. T = G + M + E + D;
  2292. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2293. weight_scale);
  2294. tt_int_op(ret, OP_EQ, 1);
  2295. tt_int_op(smartlist_len(chunks), OP_EQ, 1);
  2296. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=3333 "
  2297. "Wbe=3000 Wbg=3000 Wbm=10000 Wdb=10000 Web=10000 Wed=3333 Wee=7000 "
  2298. "Weg=3333 Wem=7000 Wgb=10000 Wgd=3333 Wgg=7000 Wgm=7000 Wmb=10000 "
  2299. "Wmd=3333 Wme=3000 Wmg=3000 Wmm=10000\n");
  2300. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2301. smartlist_clear(chunks);
  2302. /* case 2a E scarce */
  2303. M = 100;
  2304. G = 20;
  2305. E = D = 5;
  2306. T = G + M + E + D;
  2307. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2308. weight_scale);
  2309. tt_int_op(ret, OP_EQ, 1);
  2310. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 Wbe=0 "
  2311. "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
  2312. "Wem=10000 Wgb=10000 Wgd=0 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 Wme=0 "
  2313. "Wmg=0 Wmm=10000\n");
  2314. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2315. smartlist_clear(chunks);
  2316. /* case 2a G scarce */
  2317. M = 100;
  2318. E = 20;
  2319. G = D = 5;
  2320. T = G + M + E + D;
  2321. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2322. weight_scale);
  2323. tt_int_op(ret, OP_EQ, 1);
  2324. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 Wbe=0 "
  2325. "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=10000 Weg=0 Wem=10000 "
  2326. "Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 Wme=0 Wmg=0 "
  2327. "Wmm=10000\n");
  2328. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2329. smartlist_clear(chunks);
  2330. /* case 2b1 (Wgg=1, Wmd=Wgd) */
  2331. M = 10;
  2332. E = 30;
  2333. G = 10;
  2334. D = 100;
  2335. T = G + M + E + D;
  2336. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2337. weight_scale);
  2338. tt_int_op(ret, OP_EQ, 1);
  2339. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=4000 "
  2340. "Wbe=0 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=2000 Wee=10000 Weg=2000 "
  2341. "Wem=10000 Wgb=10000 Wgd=4000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=4000 "
  2342. "Wme=0 Wmg=0 Wmm=10000\n");
  2343. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2344. smartlist_clear(chunks);
  2345. /* case 2b2 */
  2346. M = 60;
  2347. E = 30;
  2348. G = 10;
  2349. D = 100;
  2350. T = G + M + E + D;
  2351. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2352. weight_scale);
  2353. tt_int_op(ret, OP_EQ, 1);
  2354. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=666 Wbe=0 "
  2355. "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=3666 Wee=10000 Weg=3666 "
  2356. "Wem=10000 Wgb=10000 Wgd=5668 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=666 "
  2357. "Wme=0 Wmg=0 Wmm=10000\n");
  2358. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2359. smartlist_clear(chunks);
  2360. /* case 2b3 */
  2361. /* XXX I can't get a combination of values that hits this case without error,
  2362. * so this just tests that it fails. See #20285. Also see #20284 as 2b3 does
  2363. * not follow dir-spec. */
  2364. /* (E < T/3 && G < T/3) && (E+D>=G || G+D>=E) && (M > T/3) */
  2365. M = 80;
  2366. E = 30;
  2367. G = 30;
  2368. D = 30;
  2369. T = G + M + E + D;
  2370. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2371. weight_scale);
  2372. tt_int_op(ret, OP_EQ, 0);
  2373. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2374. smartlist_clear(chunks);
  2375. /* case 3a G scarce */
  2376. M = 10;
  2377. E = 30;
  2378. G = 10;
  2379. D = 5;
  2380. T = G + M + E + D;
  2381. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2382. weight_scale);
  2383. tt_int_op(ret, OP_EQ, 1);
  2384. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 "
  2385. "Wbe=3333 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=6667 Weg=0 "
  2386. "Wem=6667 Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 "
  2387. "Wme=3333 Wmg=0 Wmm=10000\n");
  2388. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2389. smartlist_clear(chunks);
  2390. /* case 3a E scarce */
  2391. M = 10;
  2392. E = 10;
  2393. G = 30;
  2394. D = 5;
  2395. T = G + M + E + D;
  2396. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2397. weight_scale);
  2398. tt_int_op(ret, OP_EQ, 1);
  2399. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 Wbe=0 "
  2400. "Wbg=3333 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
  2401. "Wem=10000 Wgb=10000 Wgd=0 Wgg=6667 Wgm=6667 Wmb=10000 Wmd=0 Wme=0 "
  2402. "Wmg=3333 Wmm=10000\n");
  2403. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2404. smartlist_clear(chunks);
  2405. /* case 3bg */
  2406. M = 10;
  2407. E = 30;
  2408. G = 10;
  2409. D = 10;
  2410. T = G + M + E + D;
  2411. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2412. weight_scale);
  2413. tt_int_op(ret, OP_EQ, 1);
  2414. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 "
  2415. "Wbe=3334 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=6666 Weg=0 "
  2416. "Wem=6666 Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 "
  2417. "Wme=3334 Wmg=0 Wmm=10000\n");
  2418. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2419. smartlist_clear(chunks);
  2420. /* case 3be */
  2421. M = 10;
  2422. E = 10;
  2423. G = 30;
  2424. D = 10;
  2425. T = G + M + E + D;
  2426. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2427. weight_scale);
  2428. tt_int_op(ret, OP_EQ, 1);
  2429. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 Wbe=0 "
  2430. "Wbg=3334 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
  2431. "Wem=10000 Wgb=10000 Wgd=0 Wgg=6666 Wgm=6666 Wmb=10000 Wmd=0 Wme=0 "
  2432. "Wmg=3334 Wmm=10000\n");
  2433. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2434. smartlist_clear(chunks);
  2435. /* case from 21 Jul 2013 (3be) */
  2436. G = 5483409;
  2437. M = 1455379;
  2438. E = 980834;
  2439. D = 3385803;
  2440. T = 11305425;
  2441. tt_i64_op(G+M+E+D, OP_EQ, T);
  2442. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2443. weight_scale);
  2444. tt_assert(ret);
  2445. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=883 Wbe=0 "
  2446. "Wbg=3673 Wbm=10000 Wdb=10000 Web=10000 Wed=8233 Wee=10000 Weg=8233 "
  2447. "Wem=10000 Wgb=10000 Wgd=883 Wgg=6327 Wgm=6327 Wmb=10000 Wmd=883 Wme=0 "
  2448. "Wmg=3673 Wmm=10000\n");
  2449. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2450. smartlist_clear(chunks);
  2451. /* case from 04 Oct 2016 (3a E scarce) */
  2452. G=29322240;
  2453. M=4721546;
  2454. E=1522058;
  2455. D=9273571;
  2456. T=44839415;
  2457. tt_i64_op(G+M+E+D, OP_EQ, T);
  2458. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2459. weight_scale);
  2460. tt_assert(ret);
  2461. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=0 Wbe=0 "
  2462. "Wbg=4194 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
  2463. "Wem=10000 Wgb=10000 Wgd=0 Wgg=5806 Wgm=5806 Wmb=10000 Wmd=0 Wme=0 "
  2464. "Wmg=4194 Wmm=10000\n");
  2465. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2466. smartlist_clear(chunks);
  2467. /* case from 04 Sep 2013 (2b1) */
  2468. G=3091352;
  2469. M=1838837;
  2470. E=2109300;
  2471. D=2469369;
  2472. T=9508858;
  2473. tt_i64_op(G+M+E+D, OP_EQ, T);
  2474. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2475. weight_scale);
  2476. tt_assert(ret);
  2477. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=317 "
  2478. "Wbe=5938 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=9366 Wee=4061 "
  2479. "Weg=9366 Wem=4061 Wgb=10000 Wgd=317 Wgg=10000 Wgm=10000 Wmb=10000 "
  2480. "Wmd=317 Wme=5938 Wmg=0 Wmm=10000\n");
  2481. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2482. smartlist_clear(chunks);
  2483. /* explicitly test initializing weights to 1*/
  2484. G=1;
  2485. M=1;
  2486. E=1;
  2487. D=1;
  2488. T=4;
  2489. tt_i64_op(G+M+E+D, OP_EQ, T);
  2490. ret = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D, T,
  2491. weight_scale);
  2492. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "bandwidth-weights Wbd=3333 "
  2493. "Wbe=0 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=3333 Wee=10000 Weg=3333 "
  2494. "Wem=10000 Wgb=10000 Wgd=3333 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=3333 "
  2495. "Wme=0 Wmg=0 Wmm=10000\n");
  2496. tt_assert(ret);
  2497. done:
  2498. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2499. smartlist_free(chunks);
  2500. }
  2501. static authority_cert_t *mock_cert;
  2502. static authority_cert_t *
  2503. get_my_v3_authority_cert_m(void)
  2504. {
  2505. tor_assert(mock_cert);
  2506. return mock_cert;
  2507. }
  2508. /** Run a unit tests for generating and parsing networkstatuses, with
  2509. * the supply test fns. */
  2510. static void
  2511. test_a_networkstatus(
  2512. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  2513. int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  2514. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  2515. void (*consensus_test)(networkstatus_t *con, time_t now),
  2516. void (*rs_test)(routerstatus_t *rs, time_t now))
  2517. {
  2518. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  2519. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  2520. crypto_pk_t *sign_skey_leg1=NULL;
  2521. /*
  2522. * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
  2523. * returns non-zero, it changed net_params and we should skip the tests for
  2524. * that later as they will fail.
  2525. */
  2526. int params_tweaked = 0;
  2527. time_t now = time(NULL);
  2528. networkstatus_voter_info_t *voter;
  2529. document_signature_t *sig;
  2530. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  2531. *con_md=NULL;
  2532. vote_routerstatus_t *vrs;
  2533. routerstatus_t *rs;
  2534. int idx, n_rs, n_vrs;
  2535. char *consensus_text=NULL, *cp=NULL;
  2536. smartlist_t *votes = smartlist_new();
  2537. /* For generating the two other consensuses. */
  2538. char *detached_text1=NULL, *detached_text2=NULL;
  2539. char *consensus_text2=NULL, *consensus_text3=NULL;
  2540. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  2541. char *consensus_text_md=NULL;
  2542. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  2543. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  2544. tt_assert(vrs_gen);
  2545. tt_assert(rs_test);
  2546. tt_assert(vrs_test);
  2547. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  2548. /* Parse certificates and keys. */
  2549. cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  2550. tt_assert(cert1);
  2551. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  2552. tt_assert(cert2);
  2553. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  2554. tt_assert(cert3);
  2555. sign_skey_1 = crypto_pk_new();
  2556. sign_skey_2 = crypto_pk_new();
  2557. sign_skey_3 = crypto_pk_new();
  2558. sign_skey_leg1 = pk_generate(4);
  2559. voting_schedule_recalculate_timing(get_options(), now);
  2560. sr_state_init(0, 0);
  2561. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  2562. AUTHORITY_SIGNKEY_1, -1));
  2563. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  2564. AUTHORITY_SIGNKEY_2, -1));
  2565. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  2566. AUTHORITY_SIGNKEY_3, -1));
  2567. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  2568. tt_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  2569. tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
  2570. &v1, &n_vrs, now, 1));
  2571. tt_assert(v1);
  2572. /* Make sure the parsed thing was right. */
  2573. tt_int_op(v1->type,OP_EQ, NS_TYPE_VOTE);
  2574. tt_int_op(v1->published,OP_EQ, vote->published);
  2575. tt_int_op(v1->valid_after,OP_EQ, vote->valid_after);
  2576. tt_int_op(v1->fresh_until,OP_EQ, vote->fresh_until);
  2577. tt_int_op(v1->valid_until,OP_EQ, vote->valid_until);
  2578. tt_int_op(v1->vote_seconds,OP_EQ, vote->vote_seconds);
  2579. tt_int_op(v1->dist_seconds,OP_EQ, vote->dist_seconds);
  2580. tt_str_op(v1->client_versions,OP_EQ, vote->client_versions);
  2581. tt_str_op(v1->server_versions,OP_EQ, vote->server_versions);
  2582. tt_assert(v1->voters && smartlist_len(v1->voters));
  2583. voter = smartlist_get(v1->voters, 0);
  2584. tt_str_op(voter->nickname,OP_EQ, "Voter1");
  2585. tt_str_op(voter->address,OP_EQ, "1.2.3.4");
  2586. tt_int_op(voter->addr,OP_EQ, 0x01020304);
  2587. tt_int_op(voter->dir_port,OP_EQ, 80);
  2588. tt_int_op(voter->or_port,OP_EQ, 9000);
  2589. tt_str_op(voter->contact,OP_EQ, "voter@example.com");
  2590. tt_assert(v1->cert);
  2591. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  2592. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  2593. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  2594. tor_free(cp);
  2595. tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
  2596. networkstatus_vote_free(vote);
  2597. vote = NULL;
  2598. if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
  2599. /* Check the routerstatuses. */
  2600. for (idx = 0; idx < n_vrs; ++idx) {
  2601. vrs = smartlist_get(v1->routerstatus_list, idx);
  2602. tt_assert(vrs);
  2603. vrs_test(vrs, 1, now);
  2604. }
  2605. /* Generate second vote. It disagrees on some of the times,
  2606. * and doesn't list versions, and knows some crazy flags.
  2607. * Generate and parse v2. */
  2608. tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
  2609. &v2, &n_vrs, now, 1));
  2610. tt_assert(v2);
  2611. if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
  2612. /* Check that flags come out right.*/
  2613. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  2614. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  2615. "Running:Stable:V2Dir:Valid");
  2616. tor_free(cp);
  2617. /* Check the routerstatuses. */
  2618. n_vrs = smartlist_len(v2->routerstatus_list);
  2619. for (idx = 0; idx < n_vrs; ++idx) {
  2620. vrs = smartlist_get(v2->routerstatus_list, idx);
  2621. tt_assert(vrs);
  2622. vrs_test(vrs, 2, now);
  2623. }
  2624. networkstatus_vote_free(vote);
  2625. vote = NULL;
  2626. /* Generate the third vote with a legacy id. */
  2627. tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
  2628. &v3, &n_vrs, now, 1));
  2629. tt_assert(v3);
  2630. if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
  2631. /* Compute a consensus as voter 3. */
  2632. smartlist_add(votes, v3);
  2633. smartlist_add(votes, v1);
  2634. smartlist_add(votes, v2);
  2635. consensus_text = networkstatus_compute_consensus(votes, 3,
  2636. cert3->identity_key,
  2637. sign_skey_3,
  2638. "AAAAAAAAAAAAAAAAAAAA",
  2639. sign_skey_leg1,
  2640. FLAV_NS);
  2641. tt_assert(consensus_text);
  2642. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  2643. NS_TYPE_CONSENSUS);
  2644. tt_assert(con);
  2645. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  2646. // v1_text, v2_text, v3_text);
  2647. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  2648. cert3->identity_key,
  2649. sign_skey_3,
  2650. "AAAAAAAAAAAAAAAAAAAA",
  2651. sign_skey_leg1,
  2652. FLAV_MICRODESC);
  2653. tt_assert(consensus_text_md);
  2654. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  2655. NS_TYPE_CONSENSUS);
  2656. tt_assert(con_md);
  2657. tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
  2658. /* Check consensus contents. */
  2659. tt_assert(con->type == NS_TYPE_CONSENSUS);
  2660. tt_int_op(con->published,OP_EQ, 0); /* this field only appears in votes. */
  2661. tt_int_op(con->valid_after,OP_EQ, now+1000);
  2662. tt_int_op(con->fresh_until,OP_EQ, now+2003); /* median */
  2663. tt_int_op(con->valid_until,OP_EQ, now+3000);
  2664. tt_int_op(con->vote_seconds,OP_EQ, 100);
  2665. tt_int_op(con->dist_seconds,OP_EQ, 250); /* median */
  2666. tt_str_op(con->client_versions,OP_EQ, "0.1.2.14");
  2667. tt_str_op(con->server_versions,OP_EQ, "0.1.2.15,0.1.2.16");
  2668. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  2669. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  2670. "Running:Stable:V2Dir:Valid");
  2671. tor_free(cp);
  2672. if (!params_tweaked) {
  2673. /* Skip this one if vote_tweaks() messed with the param lists */
  2674. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  2675. tt_str_op(cp,OP_EQ, "circuitwindow=80:foo=660");
  2676. tor_free(cp);
  2677. }
  2678. tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  2679. /* The voter id digests should be in this order. */
  2680. tt_assert(fast_memcmp(cert2->cache_info.identity_digest,
  2681. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  2682. tt_assert(fast_memcmp(cert1->cache_info.identity_digest,
  2683. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  2684. test_same_voter(smartlist_get(con->voters, 1),
  2685. smartlist_get(v2->voters, 0));
  2686. test_same_voter(smartlist_get(con->voters, 2),
  2687. smartlist_get(v1->voters, 0));
  2688. test_same_voter(smartlist_get(con->voters, 3),
  2689. smartlist_get(v3->voters, 0));
  2690. consensus_test(con, now);
  2691. /* Check the routerstatuses. */
  2692. n_rs = smartlist_len(con->routerstatus_list);
  2693. tt_assert(n_rs);
  2694. for (idx = 0; idx < n_rs; ++idx) {
  2695. rs = smartlist_get(con->routerstatus_list, idx);
  2696. tt_assert(rs);
  2697. rs_test(rs, now);
  2698. }
  2699. n_rs = smartlist_len(con_md->routerstatus_list);
  2700. tt_assert(n_rs);
  2701. for (idx = 0; idx < n_rs; ++idx) {
  2702. rs = smartlist_get(con_md->routerstatus_list, idx);
  2703. tt_assert(rs);
  2704. }
  2705. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  2706. * The second one hasn't signed. The fourth one has signed: validate it. */
  2707. voter = smartlist_get(con->voters, 1);
  2708. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 0);
  2709. voter = smartlist_get(con->voters, 3);
  2710. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 1);
  2711. sig = smartlist_get(voter->sigs, 0);
  2712. tt_assert(sig->signature);
  2713. tt_assert(!sig->good_signature);
  2714. tt_assert(!sig->bad_signature);
  2715. tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
  2716. tt_assert(sig->signature);
  2717. tt_assert(sig->good_signature);
  2718. tt_assert(!sig->bad_signature);
  2719. {
  2720. const char *msg=NULL;
  2721. /* Compute the other two signed consensuses. */
  2722. smartlist_shuffle(votes);
  2723. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  2724. cert2->identity_key,
  2725. sign_skey_2, NULL,NULL,
  2726. FLAV_NS);
  2727. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  2728. cert2->identity_key,
  2729. sign_skey_2, NULL,NULL,
  2730. FLAV_MICRODESC);
  2731. smartlist_shuffle(votes);
  2732. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  2733. cert1->identity_key,
  2734. sign_skey_1, NULL,NULL,
  2735. FLAV_NS);
  2736. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  2737. cert1->identity_key,
  2738. sign_skey_1, NULL,NULL,
  2739. FLAV_MICRODESC);
  2740. tt_assert(consensus_text2);
  2741. tt_assert(consensus_text3);
  2742. tt_assert(consensus_text_md2);
  2743. tt_assert(consensus_text_md3);
  2744. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  2745. NS_TYPE_CONSENSUS);
  2746. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  2747. NS_TYPE_CONSENSUS);
  2748. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  2749. NS_TYPE_CONSENSUS);
  2750. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  2751. NS_TYPE_CONSENSUS);
  2752. tt_assert(con2);
  2753. tt_assert(con3);
  2754. tt_assert(con_md2);
  2755. tt_assert(con_md3);
  2756. /* All three should have the same digest. */
  2757. tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(common_digests_t));
  2758. tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(common_digests_t));
  2759. tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests,
  2760. sizeof(common_digests_t));
  2761. tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests,
  2762. sizeof(common_digests_t));
  2763. /* Extract a detached signature from con3. */
  2764. detached_text1 = get_detached_sigs(con3, con_md3);
  2765. tt_assert(detached_text1);
  2766. /* Try to parse it. */
  2767. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  2768. tt_assert(dsig1);
  2769. /* Are parsed values as expected? */
  2770. tt_int_op(dsig1->valid_after,OP_EQ, con3->valid_after);
  2771. tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
  2772. tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
  2773. {
  2774. common_digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  2775. tt_assert(dsig_digests);
  2776. tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
  2777. con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
  2778. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  2779. tt_assert(dsig_digests);
  2780. tt_mem_op(dsig_digests->d[DIGEST_SHA256],OP_EQ,
  2781. con_md3->digests.d[DIGEST_SHA256],
  2782. DIGEST256_LEN);
  2783. }
  2784. {
  2785. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  2786. tt_assert(dsig_signatures);
  2787. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  2788. sig = smartlist_get(dsig_signatures, 0);
  2789. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  2790. DIGEST_LEN);
  2791. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA1);
  2792. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  2793. tt_assert(dsig_signatures);
  2794. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  2795. sig = smartlist_get(dsig_signatures, 0);
  2796. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  2797. DIGEST_LEN);
  2798. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA256);
  2799. }
  2800. /* Try adding it to con2. */
  2801. detached_text2 = get_detached_sigs(con2,con_md2);
  2802. tt_int_op(1,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  2803. "test", LOG_INFO, &msg));
  2804. tor_free(detached_text2);
  2805. tt_int_op(1,OP_EQ,
  2806. networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  2807. LOG_INFO, &msg));
  2808. tor_free(detached_text2);
  2809. detached_text2 = get_detached_sigs(con2,con_md2);
  2810. //printf("\n<%s>\n", detached_text2);
  2811. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  2812. tt_assert(dsig2);
  2813. /*
  2814. printf("\n");
  2815. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  2816. char hd[64];
  2817. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  2818. printf("%s\n", hd);
  2819. });
  2820. */
  2821. tt_int_op(2,OP_EQ,
  2822. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  2823. tt_int_op(2,OP_EQ,
  2824. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  2825. "microdesc")));
  2826. /* Try adding to con2 twice; verify that nothing changes. */
  2827. tt_int_op(0,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  2828. "test", LOG_INFO, &msg));
  2829. /* Add to con. */
  2830. tt_int_op(2,OP_EQ, networkstatus_add_detached_signatures(con, dsig2,
  2831. "test", LOG_INFO, &msg));
  2832. /* Check signatures */
  2833. voter = smartlist_get(con->voters, 1);
  2834. sig = smartlist_get(voter->sigs, 0);
  2835. tt_assert(sig);
  2836. tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
  2837. voter = smartlist_get(con->voters, 2);
  2838. sig = smartlist_get(voter->sigs, 0);
  2839. tt_assert(sig);
  2840. tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
  2841. }
  2842. done:
  2843. tor_free(cp);
  2844. smartlist_free(votes);
  2845. tor_free(consensus_text);
  2846. tor_free(consensus_text_md);
  2847. networkstatus_vote_free(vote);
  2848. networkstatus_vote_free(v1);
  2849. networkstatus_vote_free(v2);
  2850. networkstatus_vote_free(v3);
  2851. networkstatus_vote_free(con);
  2852. networkstatus_vote_free(con_md);
  2853. crypto_pk_free(sign_skey_1);
  2854. crypto_pk_free(sign_skey_2);
  2855. crypto_pk_free(sign_skey_3);
  2856. crypto_pk_free(sign_skey_leg1);
  2857. authority_cert_free(cert1);
  2858. authority_cert_free(cert2);
  2859. authority_cert_free(cert3);
  2860. tor_free(consensus_text2);
  2861. tor_free(consensus_text3);
  2862. tor_free(consensus_text_md2);
  2863. tor_free(consensus_text_md3);
  2864. tor_free(detached_text1);
  2865. tor_free(detached_text2);
  2866. networkstatus_vote_free(con2);
  2867. networkstatus_vote_free(con3);
  2868. networkstatus_vote_free(con_md2);
  2869. networkstatus_vote_free(con_md3);
  2870. ns_detached_signatures_free(dsig1);
  2871. ns_detached_signatures_free(dsig2);
  2872. }
  2873. /** Run unit tests for generating and parsing V3 consensus networkstatus
  2874. * documents. */
  2875. static void
  2876. test_dir_v3_networkstatus(void *arg)
  2877. {
  2878. (void)arg;
  2879. test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
  2880. vote_tweaks_for_v3ns,
  2881. test_vrs_for_v3ns,
  2882. test_consensus_for_v3ns,
  2883. test_routerstatus_for_v3ns);
  2884. }
  2885. static void
  2886. test_dir_scale_bw(void *testdata)
  2887. {
  2888. double v[8] = { 2.0/3,
  2889. 7.0,
  2890. 1.0,
  2891. 3.0,
  2892. 1.0/5,
  2893. 1.0/7,
  2894. 12.0,
  2895. 24.0 };
  2896. double vals_dbl[8];
  2897. uint64_t vals_u64[8];
  2898. uint64_t total;
  2899. int i;
  2900. (void) testdata;
  2901. for (i=0; i<8; ++i)
  2902. vals_dbl[i] = v[i];
  2903. scale_array_elements_to_u64(vals_u64, vals_dbl, 8, &total);
  2904. tt_int_op((int)total, OP_EQ, 48);
  2905. total = 0;
  2906. for (i=0; i<8; ++i) {
  2907. total += vals_u64[i];
  2908. }
  2909. tt_assert(total >= (UINT64_C(1)<<60));
  2910. tt_assert(total <= (UINT64_C(1)<<62));
  2911. for (i=0; i<8; ++i) {
  2912. /* vals[2].u64 is the scaled value of 1.0 */
  2913. double ratio = ((double)vals_u64[i]) / vals_u64[2];
  2914. tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
  2915. }
  2916. /* test handling of no entries */
  2917. total = 1;
  2918. scale_array_elements_to_u64(vals_u64, vals_dbl, 0, &total);
  2919. tt_assert(total == 0);
  2920. /* make sure we don't read the array when we have no entries
  2921. * may require compiler flags to catch NULL dereferences */
  2922. total = 1;
  2923. scale_array_elements_to_u64(NULL, NULL, 0, &total);
  2924. tt_assert(total == 0);
  2925. scale_array_elements_to_u64(NULL, NULL, 0, NULL);
  2926. /* test handling of zero totals */
  2927. total = 1;
  2928. vals_dbl[0] = 0.0;
  2929. scale_array_elements_to_u64(vals_u64, vals_dbl, 1, &total);
  2930. tt_assert(total == 0);
  2931. tt_assert(vals_u64[0] == 0);
  2932. vals_dbl[0] = 0.0;
  2933. vals_dbl[1] = 0.0;
  2934. scale_array_elements_to_u64(vals_u64, vals_dbl, 2, NULL);
  2935. tt_assert(vals_u64[0] == 0);
  2936. tt_assert(vals_u64[1] == 0);
  2937. done:
  2938. ;
  2939. }
  2940. static void
  2941. test_dir_random_weighted(void *testdata)
  2942. {
  2943. int histogram[10];
  2944. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  2945. uint64_t inp_u64[10];
  2946. int i, choice;
  2947. const int n = 50000;
  2948. double max_sq_error;
  2949. (void) testdata;
  2950. /* Try a ten-element array with values from 0 through 10. The values are
  2951. * in a scrambled order to make sure we don't depend on order. */
  2952. memset(histogram,0,sizeof(histogram));
  2953. for (i=0; i<10; ++i) {
  2954. inp_u64[i] = vals[i];
  2955. total += vals[i];
  2956. }
  2957. tt_u64_op(total, OP_EQ, 45);
  2958. for (i=0; i<n; ++i) {
  2959. choice = choose_array_element_by_weight(inp_u64, 10);
  2960. tt_int_op(choice, OP_GE, 0);
  2961. tt_int_op(choice, OP_LT, 10);
  2962. histogram[choice]++;
  2963. }
  2964. /* Now see if we chose things about frequently enough. */
  2965. max_sq_error = 0;
  2966. for (i=0; i<10; ++i) {
  2967. int expected = (int)(n*vals[i]/total);
  2968. double frac_diff = 0, sq;
  2969. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  2970. if (expected)
  2971. frac_diff = (histogram[i] - expected) / ((double)expected);
  2972. else
  2973. tt_int_op(histogram[i], OP_EQ, 0);
  2974. sq = frac_diff * frac_diff;
  2975. if (sq > max_sq_error)
  2976. max_sq_error = sq;
  2977. }
  2978. /* It should almost always be much much less than this. If you want to
  2979. * figure out the odds, please feel free. */
  2980. tt_double_op(max_sq_error, OP_LT, .05);
  2981. /* Now try a singleton; do we choose it? */
  2982. for (i = 0; i < 100; ++i) {
  2983. choice = choose_array_element_by_weight(inp_u64, 1);
  2984. tt_int_op(choice, OP_EQ, 0);
  2985. }
  2986. /* Now try an array of zeros. We should choose randomly. */
  2987. memset(histogram,0,sizeof(histogram));
  2988. for (i = 0; i < 5; ++i)
  2989. inp_u64[i] = 0;
  2990. for (i = 0; i < n; ++i) {
  2991. choice = choose_array_element_by_weight(inp_u64, 5);
  2992. tt_int_op(choice, OP_GE, 0);
  2993. tt_int_op(choice, OP_LT, 5);
  2994. histogram[choice]++;
  2995. }
  2996. /* Now see if we chose things about frequently enough. */
  2997. max_sq_error = 0;
  2998. for (i=0; i<5; ++i) {
  2999. int expected = n/5;
  3000. double frac_diff = 0, sq;
  3001. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  3002. frac_diff = (histogram[i] - expected) / ((double)expected);
  3003. sq = frac_diff * frac_diff;
  3004. if (sq > max_sq_error)
  3005. max_sq_error = sq;
  3006. }
  3007. /* It should almost always be much much less than this. If you want to
  3008. * figure out the odds, please feel free. */
  3009. tt_double_op(max_sq_error, OP_LT, .05);
  3010. done:
  3011. ;
  3012. }
  3013. /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
  3014. static uint32_t alternate_clip_bw = 0;
  3015. /**
  3016. * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
  3017. * v3_networkstatus ones.
  3018. */
  3019. static vote_routerstatus_t *
  3020. gen_routerstatus_for_umbw(int idx, time_t now)
  3021. {
  3022. vote_routerstatus_t *vrs = NULL;
  3023. routerstatus_t *rs;
  3024. tor_addr_t addr_ipv6;
  3025. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  3026. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  3027. switch (idx) {
  3028. case 0:
  3029. /* Generate the first routerstatus. */
  3030. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  3031. rs = &vrs->status;
  3032. vrs->version = tor_strdup("0.1.2.14");
  3033. rs->published_on = now-1500;
  3034. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  3035. memset(rs->identity_digest, 3, DIGEST_LEN);
  3036. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  3037. rs->addr = 0x99008801;
  3038. rs->or_port = 443;
  3039. rs->dir_port = 8000;
  3040. /* all flags but running and valid cleared */
  3041. rs->is_flagged_running = 1;
  3042. rs->is_valid = 1;
  3043. /*
  3044. * This one has measured bandwidth below the clip cutoff, and
  3045. * so shouldn't be clipped; we'll have to test that it isn't
  3046. * later.
  3047. */
  3048. vrs->has_measured_bw = 1;
  3049. rs->has_bandwidth = 1;
  3050. vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  3051. break;
  3052. case 1:
  3053. /* Generate the second routerstatus. */
  3054. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  3055. rs = &vrs->status;
  3056. vrs->version = tor_strdup("0.2.0.5");
  3057. rs->published_on = now-1000;
  3058. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  3059. memset(rs->identity_digest, 5, DIGEST_LEN);
  3060. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  3061. rs->addr = 0x99009901;
  3062. rs->or_port = 443;
  3063. rs->dir_port = 0;
  3064. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  3065. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  3066. rs->ipv6_orport = 4711;
  3067. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  3068. rs->is_valid = rs->is_possible_guard = 1;
  3069. /*
  3070. * This one has measured bandwidth above the clip cutoff, and
  3071. * so shouldn't be clipped; we'll have to test that it isn't
  3072. * later.
  3073. */
  3074. vrs->has_measured_bw = 1;
  3075. rs->has_bandwidth = 1;
  3076. vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  3077. break;
  3078. case 2:
  3079. /* Generate the third routerstatus. */
  3080. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  3081. rs = &vrs->status;
  3082. vrs->version = tor_strdup("0.1.0.3");
  3083. rs->published_on = now-1000;
  3084. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  3085. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  3086. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  3087. rs->addr = 0xAA009901;
  3088. rs->or_port = 400;
  3089. rs->dir_port = 9999;
  3090. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  3091. rs->is_flagged_running = rs->is_valid =
  3092. rs->is_possible_guard = 1;
  3093. /*
  3094. * This one has unmeasured bandwidth above the clip cutoff, and
  3095. * so should be clipped; we'll have to test that it isn't
  3096. * later.
  3097. */
  3098. vrs->has_measured_bw = 0;
  3099. rs->has_bandwidth = 1;
  3100. vrs->measured_bw_kb = 0;
  3101. rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  3102. break;
  3103. case 3:
  3104. /* Generate a fourth routerstatus that is not running. */
  3105. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  3106. rs = &vrs->status;
  3107. vrs->version = tor_strdup("0.1.6.3");
  3108. rs->published_on = now-1000;
  3109. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  3110. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  3111. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  3112. rs->addr = 0xC0000203;
  3113. rs->or_port = 500;
  3114. rs->dir_port = 1999;
  3115. /* all flags but running and valid cleared */
  3116. rs->is_flagged_running = 1;
  3117. rs->is_valid = 1;
  3118. /*
  3119. * This one has unmeasured bandwidth below the clip cutoff, and
  3120. * so shouldn't be clipped; we'll have to test that it isn't
  3121. * later.
  3122. */
  3123. vrs->has_measured_bw = 0;
  3124. rs->has_bandwidth = 1;
  3125. vrs->measured_bw_kb = 0;
  3126. rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  3127. break;
  3128. case 4:
  3129. /* No more for this test; return NULL */
  3130. vrs = NULL;
  3131. break;
  3132. default:
  3133. /* Shouldn't happen */
  3134. tt_abort();
  3135. }
  3136. if (vrs) {
  3137. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  3138. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  3139. "m 25,26,27,28 "
  3140. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  3141. idx);
  3142. }
  3143. done:
  3144. return vrs;
  3145. }
  3146. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  3147. * just adding the right consensus methods to let clipping happen */
  3148. static int
  3149. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  3150. {
  3151. char *maxbw_param = NULL;
  3152. int rv = 0;
  3153. tt_assert(v);
  3154. (void)voter;
  3155. (void)now;
  3156. tt_assert(v->supported_methods);
  3157. SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
  3158. smartlist_clear(v->supported_methods);
  3159. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
  3160. smartlist_split_string(v->supported_methods,
  3161. "25 26 27 28",
  3162. NULL, 0, -1);
  3163. /* If we're using a non-default clip bandwidth, add it to net_params */
  3164. if (alternate_clip_bw > 0) {
  3165. tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
  3166. tt_assert(maxbw_param);
  3167. if (maxbw_param) {
  3168. smartlist_add(v->net_params, maxbw_param);
  3169. rv = 1;
  3170. }
  3171. }
  3172. done:
  3173. return rv;
  3174. }
  3175. /**
  3176. * Test a parsed vote_routerstatus_t for umbw test.
  3177. */
  3178. static void
  3179. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  3180. {
  3181. routerstatus_t *rs;
  3182. tor_addr_t addr_ipv6;
  3183. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  3184. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  3185. (void)voter;
  3186. tt_assert(vrs);
  3187. rs = &(vrs->status);
  3188. tt_assert(rs);
  3189. /* Split out by digests to test */
  3190. if (tor_memeq(rs->identity_digest,
  3191. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  3192. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  3193. DIGEST_LEN)) {
  3194. /*
  3195. * Check the first routerstatus - measured bandwidth below the clip
  3196. * cutoff.
  3197. */
  3198. tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
  3199. tt_int_op(rs->published_on,OP_EQ, now-1500);
  3200. tt_str_op(rs->nickname,OP_EQ, "router2");
  3201. tt_mem_op(rs->identity_digest,OP_EQ,
  3202. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  3203. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  3204. DIGEST_LEN);
  3205. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  3206. tt_int_op(rs->addr,OP_EQ, 0x99008801);
  3207. tt_int_op(rs->or_port,OP_EQ, 443);
  3208. tt_int_op(rs->dir_port,OP_EQ, 8000);
  3209. tt_assert(rs->has_bandwidth);
  3210. tt_assert(vrs->has_measured_bw);
  3211. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  3212. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  3213. } else if (tor_memeq(rs->identity_digest,
  3214. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  3215. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  3216. DIGEST_LEN)) {
  3217. /*
  3218. * Check the second routerstatus - measured bandwidth above the clip
  3219. * cutoff.
  3220. */
  3221. tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
  3222. tt_int_op(rs->published_on,OP_EQ, now-1000);
  3223. tt_str_op(rs->nickname,OP_EQ, "router1");
  3224. tt_mem_op(rs->identity_digest,OP_EQ,
  3225. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  3226. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  3227. DIGEST_LEN);
  3228. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  3229. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  3230. tt_int_op(rs->or_port,OP_EQ, 443);
  3231. tt_int_op(rs->dir_port,OP_EQ, 0);
  3232. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  3233. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  3234. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  3235. tt_assert(rs->has_bandwidth);
  3236. tt_assert(vrs->has_measured_bw);
  3237. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  3238. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  3239. } else if (tor_memeq(rs->identity_digest,
  3240. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  3241. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  3242. DIGEST_LEN)) {
  3243. /*
  3244. * Check the third routerstatus - unmeasured bandwidth above the clip
  3245. * cutoff; this one should be clipped later on in the consensus, but
  3246. * appears unclipped in the vote.
  3247. */
  3248. tt_assert(rs->has_bandwidth);
  3249. tt_assert(!(vrs->has_measured_bw));
  3250. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  3251. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  3252. } else if (tor_memeq(rs->identity_digest,
  3253. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  3254. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  3255. DIGEST_LEN)) {
  3256. /*
  3257. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  3258. * cutoff; this one should not be clipped.
  3259. */
  3260. tt_assert(rs->has_bandwidth);
  3261. tt_assert(!(vrs->has_measured_bw));
  3262. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  3263. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  3264. } else {
  3265. tt_abort();
  3266. }
  3267. done:
  3268. return;
  3269. }
  3270. /**
  3271. * Test a consensus for v3_networkstatus_test
  3272. */
  3273. static void
  3274. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  3275. {
  3276. (void)now;
  3277. tt_assert(con);
  3278. tt_ptr_op(con->cert, OP_EQ, NULL);
  3279. // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
  3280. tt_int_op(con->consensus_method, OP_GE, 16);
  3281. tt_int_op(4,OP_EQ, smartlist_len(con->routerstatus_list));
  3282. /* There should be four listed routers; all voters saw the same in this */
  3283. done:
  3284. return;
  3285. }
  3286. /**
  3287. * Test a router list entry for umbw test
  3288. */
  3289. static void
  3290. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  3291. {
  3292. tor_addr_t addr_ipv6;
  3293. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  3294. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  3295. tt_assert(rs);
  3296. /* There should be four listed routers, as constructed above */
  3297. if (tor_memeq(rs->identity_digest,
  3298. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  3299. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  3300. DIGEST_LEN)) {
  3301. tt_mem_op(rs->identity_digest,OP_EQ,
  3302. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  3303. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  3304. DIGEST_LEN);
  3305. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  3306. tt_assert(!rs->is_authority);
  3307. tt_assert(!rs->is_exit);
  3308. tt_assert(!rs->is_fast);
  3309. tt_assert(!rs->is_possible_guard);
  3310. tt_assert(!rs->is_stable);
  3311. /* (If it wasn't running and valid it wouldn't be here) */
  3312. tt_assert(rs->is_flagged_running);
  3313. tt_assert(rs->is_valid);
  3314. tt_assert(!rs->is_named);
  3315. /* This one should have measured bandwidth below the clip cutoff */
  3316. tt_assert(rs->has_bandwidth);
  3317. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  3318. tt_assert(!(rs->bw_is_unmeasured));
  3319. } else if (tor_memeq(rs->identity_digest,
  3320. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  3321. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  3322. DIGEST_LEN)) {
  3323. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  3324. tt_mem_op(rs->identity_digest,OP_EQ,
  3325. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  3326. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  3327. DIGEST_LEN);
  3328. tt_str_op(rs->nickname,OP_EQ, "router1");
  3329. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  3330. tt_int_op(rs->published_on,OP_EQ, now-1000);
  3331. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  3332. tt_int_op(rs->or_port,OP_EQ, 443);
  3333. tt_int_op(rs->dir_port,OP_EQ, 0);
  3334. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  3335. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  3336. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  3337. tt_assert(!rs->is_authority);
  3338. tt_assert(rs->is_exit);
  3339. tt_assert(rs->is_fast);
  3340. tt_assert(rs->is_possible_guard);
  3341. tt_assert(rs->is_stable);
  3342. tt_assert(rs->is_flagged_running);
  3343. tt_assert(rs->is_valid);
  3344. tt_assert(!rs->is_named);
  3345. /* This one should have measured bandwidth above the clip cutoff */
  3346. tt_assert(rs->has_bandwidth);
  3347. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  3348. tt_assert(!(rs->bw_is_unmeasured));
  3349. } else if (tor_memeq(rs->identity_digest,
  3350. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  3351. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  3352. DIGEST_LEN)) {
  3353. /*
  3354. * This one should have unmeasured bandwidth above the clip cutoff,
  3355. * and so should be clipped
  3356. */
  3357. tt_assert(rs->has_bandwidth);
  3358. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb);
  3359. tt_assert(rs->bw_is_unmeasured);
  3360. } else if (tor_memeq(rs->identity_digest,
  3361. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  3362. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  3363. DIGEST_LEN)) {
  3364. /*
  3365. * This one should have unmeasured bandwidth below the clip cutoff,
  3366. * and so should not be clipped
  3367. */
  3368. tt_assert(rs->has_bandwidth);
  3369. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  3370. tt_assert(rs->bw_is_unmeasured);
  3371. } else {
  3372. /* Weren't expecting this... */
  3373. tt_abort();
  3374. }
  3375. done:
  3376. return;
  3377. }
  3378. /**
  3379. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  3380. * method 17; this uses the same test_a_networkstatus() function that the
  3381. * v3_networkstatus test uses.
  3382. */
  3383. static void
  3384. test_dir_clip_unmeasured_bw_kb(void *arg)
  3385. {
  3386. /* Run the test with the default clip bandwidth */
  3387. (void)arg;
  3388. alternate_clip_bw = 0;
  3389. test_a_networkstatus(gen_routerstatus_for_umbw,
  3390. vote_tweaks_for_umbw,
  3391. test_vrs_for_umbw,
  3392. test_consensus_for_umbw,
  3393. test_routerstatus_for_umbw);
  3394. }
  3395. /**
  3396. * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
  3397. * of clip bandwidth.
  3398. */
  3399. static void
  3400. test_dir_clip_unmeasured_bw_kb_alt(void *arg)
  3401. {
  3402. /*
  3403. * Try a different one; this value is chosen so that the below-the-cutoff
  3404. * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
  3405. * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
  3406. * cutoff it will fail the test.
  3407. */
  3408. (void)arg;
  3409. alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
  3410. test_a_networkstatus(gen_routerstatus_for_umbw,
  3411. vote_tweaks_for_umbw,
  3412. test_vrs_for_umbw,
  3413. test_consensus_for_umbw,
  3414. test_routerstatus_for_umbw);
  3415. }
  3416. static void
  3417. test_dir_fmt_control_ns(void *arg)
  3418. {
  3419. char *s = NULL;
  3420. routerstatus_t rs;
  3421. (void)arg;
  3422. memset(&rs, 0, sizeof(rs));
  3423. rs.published_on = 1364925198;
  3424. strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
  3425. memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
  3426. memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
  3427. rs.addr = 0x20304050;
  3428. rs.or_port = 9001;
  3429. rs.dir_port = 9002;
  3430. rs.is_exit = 1;
  3431. rs.is_fast = 1;
  3432. rs.is_flagged_running = 1;
  3433. rs.has_bandwidth = 1;
  3434. rs.is_v2_dir = 1;
  3435. rs.bandwidth_kb = 1000;
  3436. s = networkstatus_getinfo_helper_single(&rs);
  3437. tt_assert(s);
  3438. tt_str_op(s, OP_EQ,
  3439. "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
  3440. "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
  3441. "32.48.64.80 9001 9002\n"
  3442. "s Exit Fast Running V2Dir\n"
  3443. "w Bandwidth=1000\n");
  3444. done:
  3445. tor_free(s);
  3446. }
  3447. static int mock_get_options_calls = 0;
  3448. static or_options_t *mock_options = NULL;
  3449. static void
  3450. reset_options(or_options_t *options, int *get_options_calls)
  3451. {
  3452. memset(options, 0, sizeof(or_options_t));
  3453. options->TestingTorNetwork = 1;
  3454. *get_options_calls = 0;
  3455. }
  3456. static const or_options_t *
  3457. mock_get_options(void)
  3458. {
  3459. ++mock_get_options_calls;
  3460. tor_assert(mock_options);
  3461. return mock_options;
  3462. }
  3463. static void
  3464. reset_routerstatus(routerstatus_t *rs,
  3465. const char *hex_identity_digest,
  3466. uint32_t ipv4_addr)
  3467. {
  3468. memset(rs, 0, sizeof(routerstatus_t));
  3469. base16_decode(rs->identity_digest, sizeof(rs->identity_digest),
  3470. hex_identity_digest, HEX_DIGEST_LEN);
  3471. /* A zero address matches everything, so the address needs to be set.
  3472. * But the specific value is irrelevant. */
  3473. rs->addr = ipv4_addr;
  3474. }
  3475. #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  3476. #define ROUTER_A_IPV4 0xAA008801
  3477. #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  3478. #define ROUTER_B_IPV4 0xBB008801
  3479. #define ROUTERSET_ALL_STR "*"
  3480. #define ROUTERSET_A_STR ROUTER_A_ID_STR
  3481. #define ROUTERSET_NONE_STR ""
  3482. /*
  3483. * Test that dirserv_set_routerstatus_testing sets router flags correctly
  3484. * Using "*" sets flags on A and B
  3485. * Using "A" sets flags on A
  3486. * Using "" sets flags on Neither
  3487. * If the router is not included:
  3488. * - if *Strict is set, the flag is set to 0,
  3489. * - otherwise, the flag is not modified. */
  3490. static void
  3491. test_dir_dirserv_set_routerstatus_testing(void *arg)
  3492. {
  3493. (void)arg;
  3494. /* Init options */
  3495. mock_options = tor_malloc(sizeof(or_options_t));
  3496. reset_options(mock_options, &mock_get_options_calls);
  3497. MOCK(get_options, mock_get_options);
  3498. /* Init routersets */
  3499. routerset_t *routerset_all = routerset_new();
  3500. routerset_parse(routerset_all, ROUTERSET_ALL_STR, "All routers");
  3501. routerset_t *routerset_a = routerset_new();
  3502. routerset_parse(routerset_a, ROUTERSET_A_STR, "Router A only");
  3503. routerset_t *routerset_none = routerset_new();
  3504. /* Routersets are empty when provided by routerset_new(),
  3505. * so this is not strictly necessary */
  3506. routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
  3507. /* Init routerstatuses */
  3508. routerstatus_t *rs_a = tor_malloc(sizeof(routerstatus_t));
  3509. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  3510. routerstatus_t *rs_b = tor_malloc(sizeof(routerstatus_t));
  3511. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3512. /* Sanity check that routersets correspond to routerstatuses.
  3513. * Return values are {2, 3, 4} */
  3514. /* We want 3 ("*" means match all addresses) */
  3515. tt_int_op(routerset_contains_routerstatus(routerset_all, rs_a, 0), OP_EQ, 3);
  3516. tt_int_op(routerset_contains_routerstatus(routerset_all, rs_b, 0), OP_EQ, 3);
  3517. /* We want 4 (match id_digest [or nickname]) */
  3518. tt_int_op(routerset_contains_routerstatus(routerset_a, rs_a, 0), OP_EQ, 4);
  3519. tt_int_op(routerset_contains_routerstatus(routerset_a, rs_b, 0), OP_EQ, 0);
  3520. tt_int_op(routerset_contains_routerstatus(routerset_none, rs_a, 0), OP_EQ,
  3521. 0);
  3522. tt_int_op(routerset_contains_routerstatus(routerset_none, rs_b, 0), OP_EQ,
  3523. 0);
  3524. /* Check that "*" sets flags on all routers: Exit
  3525. * Check the flags aren't being confused with each other */
  3526. reset_options(mock_options, &mock_get_options_calls);
  3527. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  3528. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3529. mock_options->TestingDirAuthVoteExit = routerset_all;
  3530. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  3531. dirserv_set_routerstatus_testing(rs_a);
  3532. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3533. dirserv_set_routerstatus_testing(rs_b);
  3534. tt_int_op(mock_get_options_calls, OP_EQ, 2);
  3535. tt_uint_op(rs_a->is_exit, OP_EQ, 1);
  3536. tt_uint_op(rs_b->is_exit, OP_EQ, 1);
  3537. /* Be paranoid - check no other flags are set */
  3538. tt_uint_op(rs_a->is_possible_guard, OP_EQ, 0);
  3539. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 0);
  3540. tt_uint_op(rs_a->is_hs_dir, OP_EQ, 0);
  3541. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 0);
  3542. /* Check that "*" sets flags on all routers: Guard & HSDir
  3543. * Cover the remaining flags in one test */
  3544. reset_options(mock_options, &mock_get_options_calls);
  3545. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  3546. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3547. mock_options->TestingDirAuthVoteGuard = routerset_all;
  3548. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  3549. mock_options->TestingDirAuthVoteHSDir = routerset_all;
  3550. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  3551. dirserv_set_routerstatus_testing(rs_a);
  3552. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3553. dirserv_set_routerstatus_testing(rs_b);
  3554. tt_int_op(mock_get_options_calls, OP_EQ, 2);
  3555. tt_uint_op(rs_a->is_possible_guard, OP_EQ, 1);
  3556. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 1);
  3557. tt_uint_op(rs_a->is_hs_dir, OP_EQ, 1);
  3558. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 1);
  3559. /* Be paranoid - check exit isn't set */
  3560. tt_uint_op(rs_a->is_exit, OP_EQ, 0);
  3561. tt_uint_op(rs_b->is_exit, OP_EQ, 0);
  3562. /* Check routerset A sets all flags on router A,
  3563. * but leaves router B unmodified */
  3564. reset_options(mock_options, &mock_get_options_calls);
  3565. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  3566. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3567. mock_options->TestingDirAuthVoteExit = routerset_a;
  3568. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  3569. mock_options->TestingDirAuthVoteGuard = routerset_a;
  3570. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  3571. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  3572. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  3573. dirserv_set_routerstatus_testing(rs_a);
  3574. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3575. dirserv_set_routerstatus_testing(rs_b);
  3576. tt_int_op(mock_get_options_calls, OP_EQ, 2);
  3577. tt_uint_op(rs_a->is_exit, OP_EQ, 1);
  3578. tt_uint_op(rs_b->is_exit, OP_EQ, 0);
  3579. tt_uint_op(rs_a->is_possible_guard, OP_EQ, 1);
  3580. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 0);
  3581. tt_uint_op(rs_a->is_hs_dir, OP_EQ, 1);
  3582. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 0);
  3583. /* Check routerset A unsets all flags on router B when Strict is set */
  3584. reset_options(mock_options, &mock_get_options_calls);
  3585. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3586. mock_options->TestingDirAuthVoteExit = routerset_a;
  3587. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  3588. mock_options->TestingDirAuthVoteGuard = routerset_a;
  3589. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  3590. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  3591. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  3592. rs_b->is_exit = 1;
  3593. rs_b->is_possible_guard = 1;
  3594. rs_b->is_hs_dir = 1;
  3595. dirserv_set_routerstatus_testing(rs_b);
  3596. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3597. tt_uint_op(rs_b->is_exit, OP_EQ, 0);
  3598. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 0);
  3599. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 0);
  3600. /* Check routerset A doesn't modify flags on router B without Strict set */
  3601. reset_options(mock_options, &mock_get_options_calls);
  3602. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3603. mock_options->TestingDirAuthVoteExit = routerset_a;
  3604. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  3605. mock_options->TestingDirAuthVoteGuard = routerset_a;
  3606. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  3607. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  3608. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  3609. rs_b->is_exit = 1;
  3610. rs_b->is_possible_guard = 1;
  3611. rs_b->is_hs_dir = 1;
  3612. dirserv_set_routerstatus_testing(rs_b);
  3613. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3614. tt_uint_op(rs_b->is_exit, OP_EQ, 1);
  3615. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 1);
  3616. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 1);
  3617. /* Check the empty routerset zeroes all flags
  3618. * on routers A & B with Strict set */
  3619. reset_options(mock_options, &mock_get_options_calls);
  3620. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3621. mock_options->TestingDirAuthVoteExit = routerset_none;
  3622. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  3623. mock_options->TestingDirAuthVoteGuard = routerset_none;
  3624. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  3625. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  3626. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  3627. rs_b->is_exit = 1;
  3628. rs_b->is_possible_guard = 1;
  3629. rs_b->is_hs_dir = 1;
  3630. dirserv_set_routerstatus_testing(rs_b);
  3631. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3632. tt_uint_op(rs_b->is_exit, OP_EQ, 0);
  3633. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 0);
  3634. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 0);
  3635. /* Check the empty routerset doesn't modify any flags
  3636. * on A or B without Strict set */
  3637. reset_options(mock_options, &mock_get_options_calls);
  3638. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  3639. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  3640. mock_options->TestingDirAuthVoteExit = routerset_none;
  3641. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  3642. mock_options->TestingDirAuthVoteGuard = routerset_none;
  3643. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  3644. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  3645. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  3646. rs_b->is_exit = 1;
  3647. rs_b->is_possible_guard = 1;
  3648. rs_b->is_hs_dir = 1;
  3649. dirserv_set_routerstatus_testing(rs_a);
  3650. tt_int_op(mock_get_options_calls, OP_EQ, 1);
  3651. dirserv_set_routerstatus_testing(rs_b);
  3652. tt_int_op(mock_get_options_calls, OP_EQ, 2);
  3653. tt_uint_op(rs_a->is_exit, OP_EQ, 0);
  3654. tt_uint_op(rs_a->is_possible_guard, OP_EQ, 0);
  3655. tt_uint_op(rs_a->is_hs_dir, OP_EQ, 0);
  3656. tt_uint_op(rs_b->is_exit, OP_EQ, 1);
  3657. tt_uint_op(rs_b->is_possible_guard, OP_EQ, 1);
  3658. tt_uint_op(rs_b->is_hs_dir, OP_EQ, 1);
  3659. done:
  3660. tor_free(mock_options);
  3661. mock_options = NULL;
  3662. UNMOCK(get_options);
  3663. routerset_free(routerset_all);
  3664. routerset_free(routerset_a);
  3665. routerset_free(routerset_none);
  3666. tor_free(rs_a);
  3667. tor_free(rs_b);
  3668. }
  3669. static void
  3670. test_dir_http_handling(void *args)
  3671. {
  3672. char *url = NULL;
  3673. (void)args;
  3674. /* Parse http url tests: */
  3675. /* Good headers */
  3676. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
  3677. "Host: example.com\r\n"
  3678. "User-Agent: Mozilla/5.0 (Windows;"
  3679. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  3680. &url),OP_EQ, 0);
  3681. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  3682. tor_free(url);
  3683. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),OP_EQ, 0);
  3684. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  3685. tor_free(url);
  3686. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),
  3687. OP_EQ, 0);
  3688. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  3689. tor_free(url);
  3690. /* Should prepend '/tor/' to url if required */
  3691. tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
  3692. "Host: example.com\r\n"
  3693. "User-Agent: Mozilla/5.0 (Windows;"
  3694. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  3695. &url),OP_EQ, 0);
  3696. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  3697. tor_free(url);
  3698. /* Bad headers -- no HTTP/1.x*/
  3699. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  3700. "Host: example.com\r\n"
  3701. "User-Agent: Mozilla/5.0 (Windows;"
  3702. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  3703. &url),OP_EQ, -1);
  3704. tt_ptr_op(url, OP_EQ, NULL);
  3705. /* Bad headers */
  3706. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  3707. "Host: example.com\r\n"
  3708. "User-Agent: Mozilla/5.0 (Windows;"
  3709. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  3710. &url),OP_EQ, -1);
  3711. tt_ptr_op(url, OP_EQ, NULL);
  3712. tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),OP_EQ, -1);
  3713. tt_ptr_op(url, OP_EQ, NULL);
  3714. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),OP_EQ, -1);
  3715. tt_ptr_op(url, OP_EQ, NULL);
  3716. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),
  3717. OP_EQ, -1);
  3718. tt_ptr_op(url, OP_EQ, NULL);
  3719. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),OP_EQ, -1);
  3720. tt_ptr_op(url, OP_EQ, NULL);
  3721. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),OP_EQ, -1);
  3722. tt_ptr_op(url, OP_EQ, NULL);
  3723. done:
  3724. tor_free(url);
  3725. }
  3726. static void
  3727. test_dir_purpose_needs_anonymity_returns_true_by_default(void *arg)
  3728. {
  3729. (void)arg;
  3730. tor_capture_bugs_(1);
  3731. setup_full_capture_of_logs(LOG_WARN);
  3732. tt_int_op(1, OP_EQ, purpose_needs_anonymity(0, 0, NULL));
  3733. tt_int_op(1, OP_EQ, smartlist_len(tor_get_captured_bug_log_()));
  3734. expect_single_log_msg_containing("Called with dir_purpose=0");
  3735. tor_end_capture_bugs_();
  3736. done:
  3737. tor_end_capture_bugs_();
  3738. teardown_capture_of_logs();
  3739. }
  3740. static void
  3741. test_dir_purpose_needs_anonymity_returns_true_for_bridges(void *arg)
  3742. {
  3743. (void)arg;
  3744. tt_int_op(1, OP_EQ, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE, NULL));
  3745. tt_int_op(1, OP_EQ, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE,
  3746. "foobar"));
  3747. tt_int_op(1, OP_EQ,
  3748. purpose_needs_anonymity(DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2,
  3749. ROUTER_PURPOSE_BRIDGE, NULL));
  3750. done: ;
  3751. }
  3752. static void
  3753. test_dir_purpose_needs_anonymity_returns_false_for_own_bridge_desc(void *arg)
  3754. {
  3755. (void)arg;
  3756. tt_int_op(0, OP_EQ, purpose_needs_anonymity(DIR_PURPOSE_FETCH_SERVERDESC,
  3757. ROUTER_PURPOSE_BRIDGE,
  3758. "authority.z"));
  3759. done: ;
  3760. }
  3761. static void
  3762. test_dir_purpose_needs_anonymity_returns_true_for_sensitive_purpose(void *arg)
  3763. {
  3764. (void)arg;
  3765. tt_int_op(1, OP_EQ, purpose_needs_anonymity(
  3766. DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2,
  3767. ROUTER_PURPOSE_GENERAL, NULL));
  3768. tt_int_op(1, OP_EQ, purpose_needs_anonymity(
  3769. DIR_PURPOSE_UPLOAD_RENDDESC_V2, 0, NULL));
  3770. tt_int_op(1, OP_EQ, purpose_needs_anonymity(
  3771. DIR_PURPOSE_FETCH_RENDDESC_V2, 0, NULL));
  3772. done: ;
  3773. }
  3774. static void
  3775. test_dir_purpose_needs_anonymity_ret_false_for_non_sensitive_conn(void *arg)
  3776. {
  3777. (void)arg;
  3778. tt_int_op(0, OP_EQ, purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_DIR,
  3779. ROUTER_PURPOSE_GENERAL, NULL));
  3780. tt_int_op(0, OP_EQ,
  3781. purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_VOTE, 0, NULL));
  3782. tt_int_op(0, OP_EQ,
  3783. purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_SIGNATURES, 0, NULL));
  3784. tt_int_op(0, OP_EQ,
  3785. purpose_needs_anonymity(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL));
  3786. tt_int_op(0, OP_EQ, purpose_needs_anonymity(
  3787. DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0, NULL));
  3788. tt_int_op(0, OP_EQ,
  3789. purpose_needs_anonymity(DIR_PURPOSE_FETCH_CONSENSUS, 0, NULL));
  3790. tt_int_op(0, OP_EQ,
  3791. purpose_needs_anonymity(DIR_PURPOSE_FETCH_CERTIFICATE, 0, NULL));
  3792. tt_int_op(0, OP_EQ,
  3793. purpose_needs_anonymity(DIR_PURPOSE_FETCH_SERVERDESC, 0, NULL));
  3794. tt_int_op(0, OP_EQ,
  3795. purpose_needs_anonymity(DIR_PURPOSE_FETCH_EXTRAINFO, 0, NULL));
  3796. tt_int_op(0, OP_EQ,
  3797. purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC, 0, NULL));
  3798. done: ;
  3799. }
  3800. static void
  3801. test_dir_fetch_type(void *arg)
  3802. {
  3803. (void)arg;
  3804. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
  3805. NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
  3806. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
  3807. NULL), OP_EQ, EXTRAINFO_DIRINFO | V3_DIRINFO);
  3808. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
  3809. NULL), OP_EQ, BRIDGE_DIRINFO);
  3810. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC,
  3811. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  3812. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE,
  3813. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  3814. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  3815. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  3816. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE,
  3817. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  3818. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
  3819. "microdesc"), OP_EQ, V3_DIRINFO|MICRODESC_DIRINFO);
  3820. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
  3821. NULL), OP_EQ, V3_DIRINFO);
  3822. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
  3823. NULL), OP_EQ, MICRODESC_DIRINFO);
  3824. /* This will give a warning, because this function isn't supposed to be
  3825. * used for HS descriptors. */
  3826. setup_full_capture_of_logs(LOG_WARN);
  3827. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
  3828. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
  3829. expect_single_log_msg_containing("Unexpected purpose");
  3830. done:
  3831. teardown_capture_of_logs();
  3832. }
  3833. static void
  3834. test_dir_packages(void *arg)
  3835. {
  3836. smartlist_t *votes = smartlist_new();
  3837. char *res = NULL;
  3838. (void)arg;
  3839. #define BAD(s) \
  3840. tt_int_op(0, OP_EQ, validate_recommended_package_line(s));
  3841. #define GOOD(s) \
  3842. tt_int_op(1, OP_EQ, validate_recommended_package_line(s));
  3843. GOOD("tor 0.2.6.3-alpha "
  3844. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  3845. "sha256=sssdlkfjdsklfjdskfljasdklfj");
  3846. GOOD("tor 0.2.6.3-alpha "
  3847. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  3848. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
  3849. BAD("tor 0.2.6.3-alpha "
  3850. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  3851. "sha256=sssdlkfjdsklfjdskfljasdklfj=");
  3852. BAD("tor 0.2.6.3-alpha "
  3853. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  3854. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
  3855. BAD("tor 0.2.6.3-alpha "
  3856. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
  3857. BAD("tor 0.2.6.3-alpha "
  3858. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
  3859. BAD("tor 0.2.6.3-alpha ");
  3860. BAD("tor 0.2.6.3-alpha");
  3861. BAD("tor ");
  3862. BAD("tor");
  3863. BAD("");
  3864. BAD("=foobar sha256="
  3865. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  3866. BAD("= = sha256="
  3867. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  3868. BAD("sha512= sha256="
  3869. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  3870. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3871. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3872. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3873. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3874. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3875. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  3876. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  3877. ns->package_lines = smartlist_new());
  3878. #define ADD(i, s) \
  3879. smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
  3880. (void*)(s));
  3881. /* Only one vote for this one. */
  3882. ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
  3883. /* Only two matching entries for this one, but 3 voters */
  3884. ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
  3885. ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  3886. ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  3887. /* Only two matching entries for this one, but at least 4 voters */
  3888. ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
  3889. ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  3890. ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  3891. ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
  3892. /* This one has only invalid votes. */
  3893. ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
  3894. ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
  3895. ADD(2, "haffenreffer 1.2 ");
  3896. ADD(3, "haffenreffer ");
  3897. ADD(4, "haffenreffer");
  3898. /* Three matching votes for this; it should actually go in! */
  3899. ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3900. ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3901. ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3902. ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
  3903. ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
  3904. /* Three votes for A, three votes for B */
  3905. ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3906. ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3907. ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3908. ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3909. ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3910. ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3911. /* Three votes for A, two votes for B */
  3912. ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  3913. ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  3914. ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3915. ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3916. ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3917. /* Four votes for A, two for B. */
  3918. ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  3919. ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  3920. ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3921. ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3922. ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3923. ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3924. /* Five votes for A ... all from the same authority. Three for B. */
  3925. ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3926. ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3927. ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3928. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3929. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3930. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3931. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3932. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3933. /* As above but new replaces old: no two match. */
  3934. ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3935. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3936. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
  3937. ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3938. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3939. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3940. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3941. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3942. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3943. res = compute_consensus_package_lines(votes);
  3944. tt_assert(res);
  3945. tt_str_op(res, OP_EQ,
  3946. "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
  3947. "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
  3948. "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
  3949. "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
  3950. "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
  3951. );
  3952. #undef ADD
  3953. #undef BAD
  3954. #undef GOOD
  3955. done:
  3956. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  3957. { smartlist_free(ns->package_lines); tor_free(ns); });
  3958. smartlist_free(votes);
  3959. tor_free(res);
  3960. }
  3961. static void
  3962. download_status_random_backoff_helper(int min_delay)
  3963. {
  3964. download_status_t dls_random =
  3965. { 0, 0, 0, DL_SCHED_GENERIC, DL_WANT_AUTHORITY,
  3966. DL_SCHED_INCREMENT_FAILURE, 0, 0 };
  3967. int increment = -1;
  3968. int old_increment = -1;
  3969. time_t current_time = time(NULL);
  3970. /* Check the random backoff cases */
  3971. int n_attempts = 0;
  3972. do {
  3973. increment = download_status_schedule_get_delay(&dls_random,
  3974. min_delay,
  3975. current_time);
  3976. log_debug(LD_DIR, "Min: %d, Inc: %d, Old Inc: %d",
  3977. min_delay, increment, old_increment);
  3978. /* Regression test for 20534 and friends
  3979. * increment must always increase after the first */
  3980. if (dls_random.last_backoff_position > 0) {
  3981. /* Always increment the exponential backoff */
  3982. tt_int_op(increment, OP_GE, 1);
  3983. }
  3984. /* Test */
  3985. tt_int_op(increment, OP_GE, min_delay);
  3986. /* Advance */
  3987. if (dls_random.n_download_attempts < IMPOSSIBLE_TO_DOWNLOAD - 1) {
  3988. ++(dls_random.n_download_attempts);
  3989. ++(dls_random.n_download_failures);
  3990. }
  3991. /* Try another maybe */
  3992. old_increment = increment;
  3993. } while (++n_attempts < 1000);
  3994. done:
  3995. return;
  3996. }
  3997. static void
  3998. test_dir_download_status_random_backoff(void *arg)
  3999. {
  4000. (void)arg;
  4001. /* Do a standard test */
  4002. download_status_random_backoff_helper(0);
  4003. /* regression tests for 17750: initial delay */
  4004. download_status_random_backoff_helper(10);
  4005. download_status_random_backoff_helper(20);
  4006. /* Pathological cases */
  4007. download_status_random_backoff_helper(INT_MAX/2);
  4008. }
  4009. static void
  4010. test_dir_download_status_random_backoff_ranges(void *arg)
  4011. {
  4012. (void)arg;
  4013. int lo, hi;
  4014. next_random_exponential_delay_range(&lo, &hi, 0, 10);
  4015. tt_int_op(lo, OP_EQ, 10);
  4016. tt_int_op(hi, OP_EQ, 11);
  4017. next_random_exponential_delay_range(&lo, &hi, 6, 10);
  4018. tt_int_op(lo, OP_EQ, 10);
  4019. tt_int_op(hi, OP_EQ, 6*3);
  4020. next_random_exponential_delay_range(&lo, &hi, 13, 10);
  4021. tt_int_op(lo, OP_EQ, 10);
  4022. tt_int_op(hi, OP_EQ, 13 * 3);
  4023. next_random_exponential_delay_range(&lo, &hi, 37, 10);
  4024. tt_int_op(lo, OP_EQ, 10);
  4025. tt_int_op(hi, OP_EQ, 111);
  4026. next_random_exponential_delay_range(&lo, &hi, 123, 10);
  4027. tt_int_op(lo, OP_EQ, 10);
  4028. tt_int_op(hi, OP_EQ, 369);
  4029. next_random_exponential_delay_range(&lo, &hi, INT_MAX-5, 10);
  4030. tt_int_op(lo, OP_EQ, 10);
  4031. tt_int_op(hi, OP_EQ, INT_MAX);
  4032. done:
  4033. ;
  4034. }
  4035. static void
  4036. test_dir_download_status_increment(void *arg)
  4037. {
  4038. (void)arg;
  4039. download_status_t dls_exp = { 0, 0, 0, DL_SCHED_GENERIC,
  4040. DL_WANT_ANY_DIRSERVER,
  4041. DL_SCHED_INCREMENT_ATTEMPT,
  4042. 0, 0 };
  4043. or_options_t test_options;
  4044. time_t current_time = time(NULL);
  4045. const int delay0 = 10;
  4046. const int no_delay = 0;
  4047. const int schedule = 10;
  4048. const int schedule_no_initial_delay = 0;
  4049. /* Put it in the options */
  4050. mock_options = &test_options;
  4051. reset_options(mock_options, &mock_get_options_calls);
  4052. mock_options->TestingBridgeBootstrapDownloadInitialDelay = schedule;
  4053. mock_options->TestingClientDownloadInitialDelay = schedule;
  4054. MOCK(get_options, mock_get_options);
  4055. /* Check that the initial value of the schedule is the first value used,
  4056. * whether or not it was reset before being used */
  4057. /* regression test for 17750: no initial delay */
  4058. mock_options->TestingClientDownloadInitialDelay = schedule_no_initial_delay;
  4059. mock_get_options_calls = 0;
  4060. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  4061. * an unrealiable test, because time(NULL) might change. */
  4062. /* regression test for 17750: exponential, no initial delay */
  4063. mock_options->TestingClientDownloadInitialDelay = schedule_no_initial_delay;
  4064. mock_get_options_calls = 0;
  4065. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  4066. * an unrealiable test, because time(NULL) might change. */
  4067. tt_assert(download_status_get_next_attempt_at(&dls_exp)
  4068. >= current_time + no_delay);
  4069. tt_assert(download_status_get_next_attempt_at(&dls_exp)
  4070. != TIME_MAX);
  4071. tt_int_op(download_status_get_n_failures(&dls_exp), OP_EQ, 0);
  4072. tt_int_op(download_status_get_n_attempts(&dls_exp), OP_EQ, 0);
  4073. tt_int_op(mock_get_options_calls, OP_GE, 1);
  4074. /* regression test for 17750: exponential, initial delay */
  4075. mock_options->TestingClientDownloadInitialDelay = schedule;
  4076. mock_get_options_calls = 0;
  4077. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  4078. * an unrealiable test, because time(NULL) might change. */
  4079. tt_assert(download_status_get_next_attempt_at(&dls_exp)
  4080. >= current_time + delay0);
  4081. tt_assert(download_status_get_next_attempt_at(&dls_exp)
  4082. != TIME_MAX);
  4083. tt_int_op(download_status_get_n_failures(&dls_exp), OP_EQ, 0);
  4084. tt_int_op(download_status_get_n_attempts(&dls_exp), OP_EQ, 0);
  4085. tt_int_op(mock_get_options_calls, OP_GE, 1);
  4086. done:
  4087. UNMOCK(get_options);
  4088. mock_options = NULL;
  4089. mock_get_options_calls = 0;
  4090. teardown_capture_of_logs();
  4091. }
  4092. static void
  4093. test_dir_authdir_type_to_string(void *data)
  4094. {
  4095. (void)data;
  4096. char *res;
  4097. tt_str_op(res = authdir_type_to_string(NO_DIRINFO), OP_EQ,
  4098. "[Not an authority]");
  4099. tor_free(res);
  4100. tt_str_op(res = authdir_type_to_string(EXTRAINFO_DIRINFO), OP_EQ,
  4101. "[Not an authority]");
  4102. tor_free(res);
  4103. tt_str_op(res = authdir_type_to_string(MICRODESC_DIRINFO), OP_EQ,
  4104. "[Not an authority]");
  4105. tor_free(res);
  4106. tt_str_op(res = authdir_type_to_string(V3_DIRINFO), OP_EQ, "V3");
  4107. tor_free(res);
  4108. tt_str_op(res = authdir_type_to_string(BRIDGE_DIRINFO), OP_EQ, "Bridge");
  4109. tor_free(res);
  4110. tt_str_op(res = authdir_type_to_string(
  4111. V3_DIRINFO | BRIDGE_DIRINFO | EXTRAINFO_DIRINFO), OP_EQ,
  4112. "V3, Bridge");
  4113. done:
  4114. tor_free(res);
  4115. }
  4116. static void
  4117. test_dir_conn_purpose_to_string(void *data)
  4118. {
  4119. (void)data;
  4120. #define EXPECT_CONN_PURPOSE(purpose, expected) \
  4121. tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
  4122. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR, "server descriptor upload");
  4123. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE, "server vote upload");
  4124. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES,
  4125. "consensus signature upload");
  4126. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC, "server descriptor fetch");
  4127. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO, "extra-info fetch");
  4128. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS,
  4129. "consensus network-status fetch");
  4130. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE, "authority cert fetch");
  4131. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE, "status vote fetch");
  4132. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  4133. "consensus signature fetch");
  4134. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2,
  4135. "hidden-service v2 descriptor fetch");
  4136. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
  4137. "hidden-service v2 descriptor upload");
  4138. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
  4139. /* This will give a warning, because there is no purpose 1024. */
  4140. setup_full_capture_of_logs(LOG_WARN);
  4141. EXPECT_CONN_PURPOSE(1024, "(unknown)");
  4142. expect_single_log_msg_containing("Called with unknown purpose 1024");
  4143. done:
  4144. teardown_capture_of_logs();
  4145. }
  4146. NS_DECL(int,
  4147. public_server_mode, (const or_options_t *options));
  4148. static int
  4149. NS(public_server_mode)(const or_options_t *options)
  4150. {
  4151. (void)options;
  4152. if (CALLED(public_server_mode)++ == 0) {
  4153. return 1;
  4154. }
  4155. return 0;
  4156. }
  4157. static void
  4158. test_dir_should_use_directory_guards(void *data)
  4159. {
  4160. or_options_t *options;
  4161. char *errmsg = NULL;
  4162. (void)data;
  4163. NS_MOCK(public_server_mode);
  4164. options = options_new();
  4165. options_init(options);
  4166. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4167. tt_int_op(CALLED(public_server_mode), OP_EQ, 1);
  4168. options->UseEntryGuards = 1;
  4169. options->DownloadExtraInfo = 0;
  4170. options->FetchDirInfoEarly = 0;
  4171. options->FetchDirInfoExtraEarly = 0;
  4172. options->FetchUselessDescriptors = 0;
  4173. tt_int_op(should_use_directory_guards(options), OP_EQ, 1);
  4174. tt_int_op(CALLED(public_server_mode), OP_EQ, 2);
  4175. options->UseEntryGuards = 0;
  4176. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4177. tt_int_op(CALLED(public_server_mode), OP_EQ, 3);
  4178. options->UseEntryGuards = 1;
  4179. options->DownloadExtraInfo = 1;
  4180. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4181. tt_int_op(CALLED(public_server_mode), OP_EQ, 4);
  4182. options->DownloadExtraInfo = 0;
  4183. options->FetchDirInfoEarly = 1;
  4184. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4185. tt_int_op(CALLED(public_server_mode), OP_EQ, 5);
  4186. options->FetchDirInfoEarly = 0;
  4187. options->FetchDirInfoExtraEarly = 1;
  4188. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4189. tt_int_op(CALLED(public_server_mode), OP_EQ, 6);
  4190. options->FetchDirInfoExtraEarly = 0;
  4191. options->FetchUselessDescriptors = 1;
  4192. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  4193. tt_int_op(CALLED(public_server_mode), OP_EQ, 7);
  4194. options->FetchUselessDescriptors = 0;
  4195. done:
  4196. NS_UNMOCK(public_server_mode);
  4197. or_options_free(options);
  4198. tor_free(errmsg);
  4199. }
  4200. NS_DECL(void,
  4201. directory_initiate_request, (directory_request_t *req));
  4202. static void
  4203. test_dir_should_not_init_request_to_ourselves(void *data)
  4204. {
  4205. char digest[DIGEST_LEN];
  4206. dir_server_t *ourself = NULL;
  4207. crypto_pk_t *key = pk_generate(2);
  4208. (void) data;
  4209. NS_MOCK(directory_initiate_request);
  4210. clear_dir_servers();
  4211. routerlist_free_all();
  4212. set_server_identity_key(key);
  4213. crypto_pk_get_digest(key, (char*) &digest);
  4214. ourself = trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
  4215. NULL, digest,
  4216. NULL, V3_DIRINFO, 1.0);
  4217. tt_assert(ourself);
  4218. dir_server_add(ourself);
  4219. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  4220. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 0);
  4221. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  4222. NULL);
  4223. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 0);
  4224. done:
  4225. NS_UNMOCK(directory_initiate_request);
  4226. clear_dir_servers();
  4227. routerlist_free_all();
  4228. crypto_pk_free(key);
  4229. }
  4230. static void
  4231. test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data)
  4232. {
  4233. dir_server_t *ds = NULL;
  4234. dirinfo_type_t dirinfo_type = BRIDGE_DIRINFO | EXTRAINFO_DIRINFO \
  4235. | MICRODESC_DIRINFO;
  4236. (void) data;
  4237. NS_MOCK(directory_initiate_request);
  4238. clear_dir_servers();
  4239. routerlist_free_all();
  4240. ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
  4241. "12345678901234567890", NULL, dirinfo_type, 1.0);
  4242. tt_assert(ds);
  4243. dir_server_add(ds);
  4244. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  4245. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 0);
  4246. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  4247. NULL);
  4248. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 0);
  4249. done:
  4250. NS_UNMOCK(directory_initiate_request);
  4251. clear_dir_servers();
  4252. routerlist_free_all();
  4253. }
  4254. static void
  4255. test_dir_should_init_request_to_dir_auths(void *data)
  4256. {
  4257. dir_server_t *ds = NULL;
  4258. (void) data;
  4259. NS_MOCK(directory_initiate_request);
  4260. clear_dir_servers();
  4261. routerlist_free_all();
  4262. ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
  4263. "12345678901234567890", NULL, V3_DIRINFO, 1.0);
  4264. tt_assert(ds);
  4265. dir_server_add(ds);
  4266. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  4267. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 1);
  4268. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  4269. NULL);
  4270. tt_int_op(CALLED(directory_initiate_request), OP_EQ, 2);
  4271. done:
  4272. NS_UNMOCK(directory_initiate_request);
  4273. clear_dir_servers();
  4274. routerlist_free_all();
  4275. }
  4276. void
  4277. NS(directory_initiate_request)(directory_request_t *req)
  4278. {
  4279. (void)req;
  4280. CALLED(directory_initiate_request)++;
  4281. }
  4282. static void
  4283. test_dir_choose_compression_level(void* data)
  4284. {
  4285. (void)data;
  4286. /* It starts under_memory_pressure */
  4287. tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);
  4288. tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  4289. tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
  4290. tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
  4291. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
  4292. /* Reset under_memory_pressure timer */
  4293. cell_queues_check_size();
  4294. tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);
  4295. tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  4296. tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
  4297. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
  4298. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
  4299. done: ;
  4300. }
  4301. /*
  4302. * Mock check_private_dir(), and always succeed - no need to actually
  4303. * look at or create anything on the filesystem.
  4304. */
  4305. static int
  4306. mock_check_private_dir(const char *dirname, cpd_check_t check,
  4307. const char *effective_user)
  4308. {
  4309. (void)dirname;
  4310. (void)check;
  4311. (void)effective_user;
  4312. return 0;
  4313. }
  4314. /*
  4315. * This really mocks options_get_datadir_fname2_suffix(), but for testing
  4316. * dump_desc(), we only care about get_datadir_fname(sub1), which is defined
  4317. * in config.h as:
  4318. *
  4319. * options_get_datadir_fname2_suffix(get_options(), sub1, NULL, NULL)
  4320. */
  4321. static char *
  4322. mock_get_datadir_fname(const or_options_t *options,
  4323. directory_root_t roottype,
  4324. const char *sub1, const char *sub2,
  4325. const char *suffix)
  4326. {
  4327. (void) roottype;
  4328. char *rv = NULL;
  4329. /*
  4330. * Assert we were called like get_datadir_fname2() or get_datadir_fname(),
  4331. * since that's all we implement here.
  4332. */
  4333. tt_ptr_op(options, OP_NE, NULL);
  4334. tt_ptr_op(sub1, OP_NE, NULL);
  4335. /*
  4336. * No particular assertions about sub2, since we could be in the
  4337. * get_datadir_fname() or get_datadir_fname2() case.
  4338. */
  4339. tt_ptr_op(suffix, OP_EQ, NULL);
  4340. /* Just duplicate the basename and return it for this mock */
  4341. if (sub2) {
  4342. /* If we have sub2, it's the basename, otherwise sub1 */
  4343. rv = tor_strdup(sub2);
  4344. } else {
  4345. rv = tor_strdup(sub1);
  4346. }
  4347. done:
  4348. return rv;
  4349. }
  4350. static char *last_unlinked_path = NULL;
  4351. static int unlinked_count = 0;
  4352. static void
  4353. mock_unlink_reset(void)
  4354. {
  4355. tor_free(last_unlinked_path);
  4356. unlinked_count = 0;
  4357. }
  4358. static int
  4359. mock_unlink(const char *path)
  4360. {
  4361. tt_ptr_op(path, OP_NE, NULL);
  4362. tor_free(last_unlinked_path);
  4363. last_unlinked_path = tor_strdup(path);
  4364. ++unlinked_count;
  4365. done:
  4366. return 0;
  4367. }
  4368. static char *last_write_str_path = NULL;
  4369. static uint8_t last_write_str_hash[DIGEST256_LEN];
  4370. static int write_str_count = 0;
  4371. static void
  4372. mock_write_str_to_file_reset(void)
  4373. {
  4374. tor_free(last_write_str_path);
  4375. write_str_count = 0;
  4376. }
  4377. static int
  4378. mock_write_str_to_file(const char *path, const char *str, int bin)
  4379. {
  4380. size_t len;
  4381. uint8_t hash[DIGEST256_LEN];
  4382. (void)bin;
  4383. tt_ptr_op(path, OP_NE, NULL);
  4384. tt_ptr_op(str, OP_NE, NULL);
  4385. len = strlen(str);
  4386. crypto_digest256((char *)hash, str, len, DIGEST_SHA256);
  4387. tor_free(last_write_str_path);
  4388. last_write_str_path = tor_strdup(path);
  4389. memcpy(last_write_str_hash, hash, sizeof(last_write_str_hash));
  4390. ++write_str_count;
  4391. done:
  4392. return 0;
  4393. }
  4394. static void
  4395. test_dir_dump_unparseable_descriptors(void *data)
  4396. {
  4397. /*
  4398. * These bogus descriptors look nothing at all like real bogus descriptors
  4399. * we might see, but we're only testing dump_desc() here, not the parser.
  4400. */
  4401. const char *test_desc_type = "squamous";
  4402. /* strlen(test_desc_1) = 583 bytes */
  4403. const char *test_desc_1 =
  4404. "The most merciful thing in the world, I think, is the inability of the "
  4405. "human mind to correlate all its contents. We live on a placid island of"
  4406. " ignorance in the midst of black seas of infinity, and it was not meant"
  4407. " that we should voyage far. The sciences, each straining in its own dir"
  4408. "ection, have hitherto harmed us little; but some day the piecing togeth"
  4409. "er of dissociated knowledge will open up such terrifying vistas of real"
  4410. "ity, and of our frightful position therein, that we shall either go mad"
  4411. "from the revelation or flee from the light into the peace and safety of"
  4412. "a new dark age.";
  4413. uint8_t test_desc_1_hash[DIGEST256_LEN];
  4414. char test_desc_1_hash_str[HEX_DIGEST256_LEN+1];
  4415. /* strlen(test_desc_2) = 650 bytes */
  4416. const char *test_desc_2 =
  4417. "I think their predominant colour was a greyish-green, though they had w"
  4418. "hite bellies. They were mostly shiny and slippery, but the ridges of th"
  4419. "eir backs were scaly. Their forms vaguely suggested the anthropoid, whi"
  4420. "le their heads were the heads of fish, with prodigious bulging eyes tha"
  4421. "t never closed. At the sides of their necks were palpitating gills, and"
  4422. "their long paws were webbed. They hopped irregularly, sometimes on two "
  4423. "legs and sometimes on four. I was somehow glad that they had no more th"
  4424. "an four limbs. Their croaking, baying voices, clearly wed tar articulat"
  4425. "e speech, held all the dark shades of expression which their staring fa"
  4426. "ces lacked.";
  4427. uint8_t test_desc_2_hash[DIGEST256_LEN];
  4428. char test_desc_2_hash_str[HEX_DIGEST256_LEN+1];
  4429. /* strlen(test_desc_3) = 700 bytes */
  4430. const char *test_desc_3 =
  4431. "Without knowing what futurism is like, Johansen achieved something very"
  4432. "close to it when he spoke of the city; for instead of describing any de"
  4433. "finite structure or building, he dwells only on broad impressions of va"
  4434. "st angles and stone surfaces - surfaces too great to belong to anything"
  4435. "right or proper for this earth, and impious with horrible images and hi"
  4436. "eroglyphs. I mention his talk about angles because it suggests somethin"
  4437. "g Wilcox had told me of his awful dreams. He said that the geometry of "
  4438. "the dream-place he saw was abnormal, non-Euclidean, and loathsomely red"
  4439. "olent of spheres and dimensions apart from ours. Now an unlettered seam"
  4440. "an felt the same thing whilst gazing at the terrible reality.";
  4441. uint8_t test_desc_3_hash[DIGEST256_LEN];
  4442. char test_desc_3_hash_str[HEX_DIGEST256_LEN+1];
  4443. /* strlen(test_desc_3) = 604 bytes */
  4444. const char *test_desc_4 =
  4445. "So we glanced back simultaneously, it would appear; though no doubt the"
  4446. "incipient motion of one prompted the imitation of the other. As we did "
  4447. "so we flashed both torches full strength at the momentarily thinned mis"
  4448. "t; either from sheer primitive anxiety to see all we could, or in a les"
  4449. "s primitive but equally unconscious effort to dazzle the entity before "
  4450. "we dimmed our light and dodged among the penguins of the labyrinth cent"
  4451. "er ahead. Unhappy act! Not Orpheus himself, or Lot's wife, paid much mo"
  4452. "re dearly for a backward glance. And again came that shocking, wide-ran"
  4453. "ged piping - \"Tekeli-li! Tekeli-li!\"";
  4454. uint8_t test_desc_4_hash[DIGEST256_LEN];
  4455. char test_desc_4_hash_str[HEX_DIGEST256_LEN+1];
  4456. (void)data;
  4457. /*
  4458. * Set up options mock so we can force a tiny FIFO size and generate
  4459. * cleanups.
  4460. */
  4461. mock_options = tor_malloc(sizeof(or_options_t));
  4462. reset_options(mock_options, &mock_get_options_calls);
  4463. mock_options->MaxUnparseableDescSizeToLog = 1536;
  4464. MOCK(get_options, mock_get_options);
  4465. MOCK(check_private_dir, mock_check_private_dir);
  4466. MOCK(options_get_dir_fname2_suffix,
  4467. mock_get_datadir_fname);
  4468. /*
  4469. * Set up unlink and write mocks
  4470. */
  4471. MOCK(tor_unlink, mock_unlink);
  4472. mock_unlink_reset();
  4473. MOCK(write_str_to_file, mock_write_str_to_file);
  4474. mock_write_str_to_file_reset();
  4475. /*
  4476. * Compute hashes we'll need to recognize which descriptor is which
  4477. */
  4478. crypto_digest256((char *)test_desc_1_hash, test_desc_1,
  4479. strlen(test_desc_1), DIGEST_SHA256);
  4480. base16_encode(test_desc_1_hash_str, sizeof(test_desc_1_hash_str),
  4481. (const char *)test_desc_1_hash,
  4482. sizeof(test_desc_1_hash));
  4483. crypto_digest256((char *)test_desc_2_hash, test_desc_2,
  4484. strlen(test_desc_2), DIGEST_SHA256);
  4485. base16_encode(test_desc_2_hash_str, sizeof(test_desc_2_hash_str),
  4486. (const char *)test_desc_2_hash,
  4487. sizeof(test_desc_2_hash));
  4488. crypto_digest256((char *)test_desc_3_hash, test_desc_3,
  4489. strlen(test_desc_3), DIGEST_SHA256);
  4490. base16_encode(test_desc_3_hash_str, sizeof(test_desc_3_hash_str),
  4491. (const char *)test_desc_3_hash,
  4492. sizeof(test_desc_3_hash));
  4493. crypto_digest256((char *)test_desc_4_hash, test_desc_4,
  4494. strlen(test_desc_4), DIGEST_SHA256);
  4495. base16_encode(test_desc_4_hash_str, sizeof(test_desc_4_hash_str),
  4496. (const char *)test_desc_4_hash,
  4497. sizeof(test_desc_4_hash));
  4498. /*
  4499. * Reset the FIFO and check its state
  4500. */
  4501. dump_desc_fifo_cleanup();
  4502. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4503. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4504. /*
  4505. * (1) Fire off dump_desc() once; these descriptors should all be safely
  4506. * smaller than configured FIFO size.
  4507. */
  4508. dump_desc(test_desc_1, test_desc_type);
  4509. /*
  4510. * Assert things about the FIFO state
  4511. */
  4512. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_1));
  4513. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4514. /*
  4515. * Assert things about the mocks
  4516. */
  4517. tt_int_op(unlinked_count, OP_EQ, 0);
  4518. tt_int_op(write_str_count, OP_EQ, 1);
  4519. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4520. /*
  4521. * Reset the FIFO and check its state
  4522. */
  4523. dump_desc_fifo_cleanup();
  4524. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4525. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4526. /*
  4527. * Reset the mocks and check their state
  4528. */
  4529. mock_unlink_reset();
  4530. mock_write_str_to_file_reset();
  4531. tt_int_op(unlinked_count, OP_EQ, 0);
  4532. tt_int_op(write_str_count, OP_EQ, 0);
  4533. /*
  4534. * (2) Fire off dump_desc() twice; this still should trigger no cleanup.
  4535. */
  4536. /* First time */
  4537. dump_desc(test_desc_2, test_desc_type);
  4538. /*
  4539. * Assert things about the FIFO state
  4540. */
  4541. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_2));
  4542. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4543. /*
  4544. * Assert things about the mocks
  4545. */
  4546. tt_int_op(unlinked_count, OP_EQ, 0);
  4547. tt_int_op(write_str_count, OP_EQ, 1);
  4548. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4549. /* Second time */
  4550. dump_desc(test_desc_3, test_desc_type);
  4551. /*
  4552. * Assert things about the FIFO state
  4553. */
  4554. tt_u64_op(len_descs_dumped, OP_EQ,
  4555. strlen(test_desc_2) + strlen(test_desc_3));
  4556. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4557. /*
  4558. * Assert things about the mocks
  4559. */
  4560. tt_int_op(unlinked_count, OP_EQ, 0);
  4561. tt_int_op(write_str_count, OP_EQ, 2);
  4562. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4563. /*
  4564. * Reset the FIFO and check its state
  4565. */
  4566. dump_desc_fifo_cleanup();
  4567. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4568. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4569. /*
  4570. * Reset the mocks and check their state
  4571. */
  4572. mock_unlink_reset();
  4573. mock_write_str_to_file_reset();
  4574. tt_int_op(unlinked_count, OP_EQ, 0);
  4575. tt_int_op(write_str_count, OP_EQ, 0);
  4576. /*
  4577. * (3) Three calls to dump_desc cause a FIFO cleanup
  4578. */
  4579. /* First time */
  4580. dump_desc(test_desc_4, test_desc_type);
  4581. /*
  4582. * Assert things about the FIFO state
  4583. */
  4584. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_4));
  4585. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4586. /*
  4587. * Assert things about the mocks
  4588. */
  4589. tt_int_op(unlinked_count, OP_EQ, 0);
  4590. tt_int_op(write_str_count, OP_EQ, 1);
  4591. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4592. /* Second time */
  4593. dump_desc(test_desc_1, test_desc_type);
  4594. /*
  4595. * Assert things about the FIFO state
  4596. */
  4597. tt_u64_op(len_descs_dumped, OP_EQ,
  4598. strlen(test_desc_4) + strlen(test_desc_1));
  4599. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4600. /*
  4601. * Assert things about the mocks
  4602. */
  4603. tt_int_op(unlinked_count, OP_EQ, 0);
  4604. tt_int_op(write_str_count, OP_EQ, 2);
  4605. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4606. /* Third time - we should unlink the dump of test_desc_4 here */
  4607. dump_desc(test_desc_2, test_desc_type);
  4608. /*
  4609. * Assert things about the FIFO state
  4610. */
  4611. tt_u64_op(len_descs_dumped, OP_EQ,
  4612. strlen(test_desc_1) + strlen(test_desc_2));
  4613. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4614. /*
  4615. * Assert things about the mocks
  4616. */
  4617. tt_int_op(unlinked_count, OP_EQ, 1);
  4618. tt_int_op(write_str_count, OP_EQ, 3);
  4619. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4620. /*
  4621. * Reset the FIFO and check its state
  4622. */
  4623. dump_desc_fifo_cleanup();
  4624. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4625. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4626. /*
  4627. * Reset the mocks and check their state
  4628. */
  4629. mock_unlink_reset();
  4630. mock_write_str_to_file_reset();
  4631. tt_int_op(unlinked_count, OP_EQ, 0);
  4632. tt_int_op(write_str_count, OP_EQ, 0);
  4633. /*
  4634. * (4) But repeating one (A B B) doesn't overflow and cleanup
  4635. */
  4636. /* First time */
  4637. dump_desc(test_desc_3, test_desc_type);
  4638. /*
  4639. * Assert things about the FIFO state
  4640. */
  4641. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_3));
  4642. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4643. /*
  4644. * Assert things about the mocks
  4645. */
  4646. tt_int_op(unlinked_count, OP_EQ, 0);
  4647. tt_int_op(write_str_count, OP_EQ, 1);
  4648. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4649. /* Second time */
  4650. dump_desc(test_desc_4, test_desc_type);
  4651. /*
  4652. * Assert things about the FIFO state
  4653. */
  4654. tt_u64_op(len_descs_dumped, OP_EQ,
  4655. strlen(test_desc_3) + strlen(test_desc_4));
  4656. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4657. /*
  4658. * Assert things about the mocks
  4659. */
  4660. tt_int_op(unlinked_count, OP_EQ, 0);
  4661. tt_int_op(write_str_count, OP_EQ, 2);
  4662. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4663. /* Third time */
  4664. dump_desc(test_desc_4, test_desc_type);
  4665. /*
  4666. * Assert things about the FIFO state
  4667. */
  4668. tt_u64_op(len_descs_dumped, OP_EQ,
  4669. strlen(test_desc_3) + strlen(test_desc_4));
  4670. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4671. /*
  4672. * Assert things about the mocks
  4673. */
  4674. tt_int_op(unlinked_count, OP_EQ, 0);
  4675. tt_int_op(write_str_count, OP_EQ, 2);
  4676. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4677. /*
  4678. * Reset the FIFO and check its state
  4679. */
  4680. dump_desc_fifo_cleanup();
  4681. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4682. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4683. /*
  4684. * Reset the mocks and check their state
  4685. */
  4686. mock_unlink_reset();
  4687. mock_write_str_to_file_reset();
  4688. tt_int_op(unlinked_count, OP_EQ, 0);
  4689. tt_int_op(write_str_count, OP_EQ, 0);
  4690. /*
  4691. * (5) Same for the (A B A) repetition
  4692. */
  4693. /* First time */
  4694. dump_desc(test_desc_1, test_desc_type);
  4695. /*
  4696. * Assert things about the FIFO state
  4697. */
  4698. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_1));
  4699. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4700. /*
  4701. * Assert things about the mocks
  4702. */
  4703. tt_int_op(unlinked_count, OP_EQ, 0);
  4704. tt_int_op(write_str_count, OP_EQ, 1);
  4705. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4706. /* Second time */
  4707. dump_desc(test_desc_2, test_desc_type);
  4708. /*
  4709. * Assert things about the FIFO state
  4710. */
  4711. tt_u64_op(len_descs_dumped, OP_EQ,
  4712. strlen(test_desc_1) + strlen(test_desc_2));
  4713. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4714. /*
  4715. * Assert things about the mocks
  4716. */
  4717. tt_int_op(unlinked_count, OP_EQ, 0);
  4718. tt_int_op(write_str_count, OP_EQ, 2);
  4719. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4720. /* Third time */
  4721. dump_desc(test_desc_1, test_desc_type);
  4722. /*
  4723. * Assert things about the FIFO state
  4724. */
  4725. tt_u64_op(len_descs_dumped, OP_EQ,
  4726. strlen(test_desc_1) + strlen(test_desc_2));
  4727. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4728. /*
  4729. * Assert things about the mocks
  4730. */
  4731. tt_int_op(unlinked_count, OP_EQ, 0);
  4732. tt_int_op(write_str_count, OP_EQ, 2);
  4733. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4734. /*
  4735. * Reset the FIFO and check its state
  4736. */
  4737. dump_desc_fifo_cleanup();
  4738. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4739. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4740. /*
  4741. * Reset the mocks and check their state
  4742. */
  4743. mock_unlink_reset();
  4744. mock_write_str_to_file_reset();
  4745. tt_int_op(unlinked_count, OP_EQ, 0);
  4746. tt_int_op(write_str_count, OP_EQ, 0);
  4747. /*
  4748. * (6) (A B B C) triggering overflow on C causes A, not B to be unlinked
  4749. */
  4750. /* First time */
  4751. dump_desc(test_desc_3, test_desc_type);
  4752. /*
  4753. * Assert things about the FIFO state
  4754. */
  4755. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_3));
  4756. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4757. /*
  4758. * Assert things about the mocks
  4759. */
  4760. tt_int_op(unlinked_count, OP_EQ, 0);
  4761. tt_int_op(write_str_count, OP_EQ, 1);
  4762. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4763. /* Second time */
  4764. dump_desc(test_desc_4, test_desc_type);
  4765. /*
  4766. * Assert things about the FIFO state
  4767. */
  4768. tt_u64_op(len_descs_dumped, OP_EQ,
  4769. strlen(test_desc_3) + strlen(test_desc_4));
  4770. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4771. /*
  4772. * Assert things about the mocks
  4773. */
  4774. tt_int_op(unlinked_count, OP_EQ, 0);
  4775. tt_int_op(write_str_count, OP_EQ, 2);
  4776. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4777. /* Third time */
  4778. dump_desc(test_desc_4, test_desc_type);
  4779. /*
  4780. * Assert things about the FIFO state
  4781. */
  4782. tt_u64_op(len_descs_dumped, OP_EQ,
  4783. strlen(test_desc_3) + strlen(test_desc_4));
  4784. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4785. /*
  4786. * Assert things about the mocks
  4787. */
  4788. tt_int_op(unlinked_count, OP_EQ, 0);
  4789. tt_int_op(write_str_count, OP_EQ, 2);
  4790. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4791. /* Fourth time - we should unlink the dump of test_desc_3 here */
  4792. dump_desc(test_desc_1, test_desc_type);
  4793. /*
  4794. * Assert things about the FIFO state
  4795. */
  4796. tt_u64_op(len_descs_dumped, OP_EQ,
  4797. strlen(test_desc_4) + strlen(test_desc_1));
  4798. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4799. /*
  4800. * Assert things about the mocks
  4801. */
  4802. tt_int_op(unlinked_count, OP_EQ, 1);
  4803. tt_int_op(write_str_count, OP_EQ, 3);
  4804. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4805. /*
  4806. * Reset the FIFO and check its state
  4807. */
  4808. dump_desc_fifo_cleanup();
  4809. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4810. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4811. /*
  4812. * Reset the mocks and check their state
  4813. */
  4814. mock_unlink_reset();
  4815. mock_write_str_to_file_reset();
  4816. tt_int_op(unlinked_count, OP_EQ, 0);
  4817. tt_int_op(write_str_count, OP_EQ, 0);
  4818. /*
  4819. * (7) (A B A C) triggering overflow on C causes B, not A to be unlinked
  4820. */
  4821. /* First time */
  4822. dump_desc(test_desc_2, test_desc_type);
  4823. /*
  4824. * Assert things about the FIFO state
  4825. */
  4826. tt_u64_op(len_descs_dumped, OP_EQ, strlen(test_desc_2));
  4827. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4828. /*
  4829. * Assert things about the mocks
  4830. */
  4831. tt_int_op(unlinked_count, OP_EQ, 0);
  4832. tt_int_op(write_str_count, OP_EQ, 1);
  4833. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4834. /* Second time */
  4835. dump_desc(test_desc_3, test_desc_type);
  4836. /*
  4837. * Assert things about the FIFO state
  4838. */
  4839. tt_u64_op(len_descs_dumped, OP_EQ,
  4840. strlen(test_desc_2) + strlen(test_desc_3));
  4841. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4842. /*
  4843. * Assert things about the mocks
  4844. */
  4845. tt_int_op(unlinked_count, OP_EQ, 0);
  4846. tt_int_op(write_str_count, OP_EQ, 2);
  4847. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4848. /* Third time */
  4849. dump_desc(test_desc_2, test_desc_type);
  4850. /*
  4851. * Assert things about the FIFO state
  4852. */
  4853. tt_u64_op(len_descs_dumped, OP_EQ,
  4854. strlen(test_desc_2) + strlen(test_desc_3));
  4855. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4856. /*
  4857. * Assert things about the mocks
  4858. */
  4859. tt_int_op(unlinked_count, OP_EQ, 0);
  4860. tt_int_op(write_str_count, OP_EQ, 2);
  4861. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4862. /* Fourth time - we should unlink the dump of test_desc_3 here */
  4863. dump_desc(test_desc_4, test_desc_type);
  4864. /*
  4865. * Assert things about the FIFO state
  4866. */
  4867. tt_u64_op(len_descs_dumped, OP_EQ,
  4868. strlen(test_desc_2) + strlen(test_desc_4));
  4869. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4870. /*
  4871. * Assert things about the mocks
  4872. */
  4873. tt_int_op(unlinked_count, OP_EQ, 1);
  4874. tt_int_op(write_str_count, OP_EQ, 3);
  4875. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4876. /*
  4877. * Reset the FIFO and check its state
  4878. */
  4879. dump_desc_fifo_cleanup();
  4880. tt_u64_op(len_descs_dumped, OP_EQ, 0);
  4881. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4882. /*
  4883. * Reset the mocks and check their state
  4884. */
  4885. mock_unlink_reset();
  4886. mock_write_str_to_file_reset();
  4887. tt_int_op(unlinked_count, OP_EQ, 0);
  4888. tt_int_op(write_str_count, OP_EQ, 0);
  4889. done:
  4890. /* Clean up the fifo */
  4891. dump_desc_fifo_cleanup();
  4892. /* Remove mocks */
  4893. UNMOCK(tor_unlink);
  4894. mock_unlink_reset();
  4895. UNMOCK(write_str_to_file);
  4896. mock_write_str_to_file_reset();
  4897. UNMOCK(options_get_dir_fname2_suffix);
  4898. UNMOCK(check_private_dir);
  4899. UNMOCK(get_options);
  4900. tor_free(mock_options);
  4901. mock_options = NULL;
  4902. return;
  4903. }
  4904. /* Variables for reset_read_file_to_str_mock() */
  4905. static int enforce_expected_filename = 0;
  4906. static char *expected_filename = NULL;
  4907. static char *file_content = NULL;
  4908. static size_t file_content_len = 0;
  4909. static struct stat file_stat;
  4910. static int read_count = 0, read_call_count = 0;
  4911. static void
  4912. reset_read_file_to_str_mock(void)
  4913. {
  4914. tor_free(expected_filename);
  4915. tor_free(file_content);
  4916. file_content_len = 0;
  4917. memset(&file_stat, 0, sizeof(file_stat));
  4918. read_count = 0;
  4919. read_call_count = 0;
  4920. }
  4921. static char *
  4922. read_file_to_str_mock(const char *filename, int flags,
  4923. struct stat *stat_out) {
  4924. char *result = NULL;
  4925. /* Insist we got a filename */
  4926. tt_ptr_op(filename, OP_NE, NULL);
  4927. /* We ignore flags */
  4928. (void)flags;
  4929. /* Bump the call count */
  4930. ++read_call_count;
  4931. if (enforce_expected_filename) {
  4932. tt_assert(expected_filename);
  4933. tt_str_op(filename, OP_EQ, expected_filename);
  4934. }
  4935. if (expected_filename != NULL &&
  4936. file_content != NULL &&
  4937. strcmp(filename, expected_filename) == 0) {
  4938. /* You asked for it, you got it */
  4939. /*
  4940. * This is the same behavior as the real read_file_to_str();
  4941. * if there's a NUL, the real size ends up in stat_out.
  4942. */
  4943. result = tor_malloc(file_content_len + 1);
  4944. if (file_content_len > 0) {
  4945. memcpy(result, file_content, file_content_len);
  4946. }
  4947. result[file_content_len] = '\0';
  4948. /* Do we need to set up stat_out? */
  4949. if (stat_out != NULL) {
  4950. memcpy(stat_out, &file_stat, sizeof(file_stat));
  4951. /* We always return the correct length here */
  4952. stat_out->st_size = file_content_len;
  4953. }
  4954. /* Wooo, we have a return value - bump the counter */
  4955. ++read_count;
  4956. }
  4957. /* else no match, return NULL */
  4958. done:
  4959. return result;
  4960. }
  4961. /* This one tests dump_desc_populate_one_file() */
  4962. static void
  4963. test_dir_populate_dump_desc_fifo(void *data)
  4964. {
  4965. const char *dirname = "foo";
  4966. const char *fname = NULL;
  4967. dumped_desc_t *ent;
  4968. (void)data;
  4969. /*
  4970. * Set up unlink and read_file_to_str mocks
  4971. */
  4972. MOCK(tor_unlink, mock_unlink);
  4973. mock_unlink_reset();
  4974. MOCK(read_file_to_str, read_file_to_str_mock);
  4975. reset_read_file_to_str_mock();
  4976. /* Check state of unlink mock */
  4977. tt_int_op(unlinked_count, OP_EQ, 0);
  4978. /* Some cases that should fail before trying to read the file */
  4979. ent = dump_desc_populate_one_file(dirname, "bar");
  4980. tt_ptr_op(ent, OP_EQ, NULL);
  4981. tt_int_op(unlinked_count, OP_EQ, 1);
  4982. tt_int_op(read_count, OP_EQ, 0);
  4983. tt_int_op(read_call_count, OP_EQ, 0);
  4984. ent = dump_desc_populate_one_file(dirname, "unparseable-desc");
  4985. tt_ptr_op(ent, OP_EQ, NULL);
  4986. tt_int_op(unlinked_count, OP_EQ, 2);
  4987. tt_int_op(read_count, OP_EQ, 0);
  4988. tt_int_op(read_call_count, OP_EQ, 0);
  4989. ent = dump_desc_populate_one_file(dirname, "unparseable-desc.baz");
  4990. tt_ptr_op(ent, OP_EQ, NULL);
  4991. tt_int_op(unlinked_count, OP_EQ, 3);
  4992. tt_int_op(read_count, OP_EQ, 0);
  4993. tt_int_op(read_call_count, OP_EQ, 0);
  4994. ent = dump_desc_populate_one_file(
  4995. dirname,
  4996. "unparseable-desc.08AE85E90461F59E");
  4997. tt_ptr_op(ent, OP_EQ, NULL);
  4998. tt_int_op(unlinked_count, OP_EQ, 4);
  4999. tt_int_op(read_count, OP_EQ, 0);
  5000. tt_int_op(read_call_count, OP_EQ, 0);
  5001. ent = dump_desc_populate_one_file(
  5002. dirname,
  5003. "unparseable-desc.08AE85E90461F59EDF0981323F3A70D02B55AB54B44B04F"
  5004. "287D72F7B72F242E85C8CB0EDA8854A99");
  5005. tt_ptr_op(ent, OP_EQ, NULL);
  5006. tt_int_op(unlinked_count, OP_EQ, 5);
  5007. tt_int_op(read_count, OP_EQ, 0);
  5008. tt_int_op(read_call_count, OP_EQ, 0);
  5009. /* This is a correct-length digest but base16_decode() will fail */
  5010. ent = dump_desc_populate_one_file(
  5011. dirname,
  5012. "unparseable-desc.68219B8BGE64B705A6FFC728C069DC596216D60A7D7520C"
  5013. "D5ECE250D912E686B");
  5014. tt_ptr_op(ent, OP_EQ, NULL);
  5015. tt_int_op(unlinked_count, OP_EQ, 6);
  5016. tt_int_op(read_count, OP_EQ, 0);
  5017. tt_int_op(read_call_count, OP_EQ, 0);
  5018. /* This one has a correctly formed filename and should try reading */
  5019. /* Read fails */
  5020. ent = dump_desc_populate_one_file(
  5021. dirname,
  5022. "unparseable-desc.DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E"
  5023. "85C8CB0EDA8854A99");
  5024. tt_ptr_op(ent, OP_EQ, NULL);
  5025. tt_int_op(unlinked_count, OP_EQ, 7);
  5026. tt_int_op(read_count, OP_EQ, 0);
  5027. tt_int_op(read_call_count, OP_EQ, 1);
  5028. /* This read will succeed but the digest won't match the file content */
  5029. fname =
  5030. "unparseable-desc."
  5031. "DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E85C8CB0EDA8854A99";
  5032. enforce_expected_filename = 1;
  5033. tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
  5034. file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
  5035. file_content_len = strlen(file_content);
  5036. file_stat.st_mtime = 123456;
  5037. ent = dump_desc_populate_one_file(dirname, fname);
  5038. enforce_expected_filename = 0;
  5039. tt_ptr_op(ent, OP_EQ, NULL);
  5040. tt_int_op(unlinked_count, OP_EQ, 8);
  5041. tt_int_op(read_count, OP_EQ, 1);
  5042. tt_int_op(read_call_count, OP_EQ, 2);
  5043. tor_free(expected_filename);
  5044. tor_free(file_content);
  5045. /* This one will match */
  5046. fname =
  5047. "unparseable-desc."
  5048. "0786C7173447B7FB033FFCA2FC47C3CF71C30DD47CA8236D3FC7FF35853271C6";
  5049. tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
  5050. file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
  5051. file_content_len = strlen(file_content);
  5052. file_stat.st_mtime = 789012;
  5053. ent = dump_desc_populate_one_file(dirname, fname);
  5054. tt_ptr_op(ent, OP_NE, NULL);
  5055. tt_int_op(unlinked_count, OP_EQ, 8);
  5056. tt_int_op(read_count, OP_EQ, 2);
  5057. tt_int_op(read_call_count, OP_EQ, 3);
  5058. tt_str_op(ent->filename, OP_EQ, expected_filename);
  5059. tt_int_op(ent->len, OP_EQ, file_content_len);
  5060. tt_int_op(ent->when, OP_EQ, file_stat.st_mtime);
  5061. tor_free(ent->filename);
  5062. tor_free(ent);
  5063. tor_free(expected_filename);
  5064. /*
  5065. * Reset the mocks and check their state
  5066. */
  5067. mock_unlink_reset();
  5068. tt_int_op(unlinked_count, OP_EQ, 0);
  5069. reset_read_file_to_str_mock();
  5070. tt_int_op(read_count, OP_EQ, 0);
  5071. done:
  5072. UNMOCK(tor_unlink);
  5073. mock_unlink_reset();
  5074. UNMOCK(read_file_to_str);
  5075. reset_read_file_to_str_mock();
  5076. tor_free(file_content);
  5077. return;
  5078. }
  5079. static smartlist_t *
  5080. listdir_mock(const char *dname)
  5081. {
  5082. smartlist_t *l;
  5083. /* Ignore the name, always return this list */
  5084. (void)dname;
  5085. l = smartlist_new();
  5086. smartlist_add_strdup(l, "foo");
  5087. smartlist_add_strdup(l, "bar");
  5088. smartlist_add_strdup(l, "baz");
  5089. return l;
  5090. }
  5091. static dumped_desc_t *
  5092. pop_one_mock(const char *dirname, const char *f)
  5093. {
  5094. dumped_desc_t *ent = NULL;
  5095. if (dirname != NULL && strcmp(dirname, "d") == 0) {
  5096. if (f != NULL && strcmp(f, "foo") == 0) {
  5097. ent = tor_malloc_zero(sizeof(*ent));
  5098. ent->filename = tor_strdup("d/foo");
  5099. ent->len = 123;
  5100. ent->digest_sha256[0] = 1;
  5101. ent->when = 1024;
  5102. } else if (f != NULL && strcmp(f, "bar") == 0) {
  5103. ent = tor_malloc_zero(sizeof(*ent));
  5104. ent->filename = tor_strdup("d/bar");
  5105. ent->len = 456;
  5106. ent->digest_sha256[0] = 2;
  5107. /*
  5108. * Note that the timestamps are in a different order than
  5109. * listdir_mock() returns; we're testing the sort order.
  5110. */
  5111. ent->when = 512;
  5112. } else if (f != NULL && strcmp(f, "baz") == 0) {
  5113. ent = tor_malloc_zero(sizeof(*ent));
  5114. ent->filename = tor_strdup("d/baz");
  5115. ent->len = 789;
  5116. ent->digest_sha256[0] = 3;
  5117. ent->when = 768;
  5118. }
  5119. }
  5120. return ent;
  5121. }
  5122. /* This one tests dump_desc_populate_fifo_from_directory() */
  5123. static void
  5124. test_dir_populate_dump_desc_fifo_2(void *data)
  5125. {
  5126. dumped_desc_t *ent = NULL;
  5127. (void)data;
  5128. /* Set up the mocks */
  5129. MOCK(tor_listdir, listdir_mock);
  5130. MOCK(dump_desc_populate_one_file, pop_one_mock);
  5131. /* Run dump_desc_populate_fifo_from_directory() */
  5132. descs_dumped = NULL;
  5133. len_descs_dumped = 0;
  5134. dump_desc_populate_fifo_from_directory("d");
  5135. tt_assert(descs_dumped != NULL);
  5136. tt_int_op(smartlist_len(descs_dumped), OP_EQ, 3);
  5137. tt_u64_op(len_descs_dumped, OP_EQ, 1368);
  5138. ent = smartlist_get(descs_dumped, 0);
  5139. tt_str_op(ent->filename, OP_EQ, "d/bar");
  5140. tt_int_op(ent->len, OP_EQ, 456);
  5141. tt_int_op(ent->when, OP_EQ, 512);
  5142. ent = smartlist_get(descs_dumped, 1);
  5143. tt_str_op(ent->filename, OP_EQ, "d/baz");
  5144. tt_int_op(ent->len, OP_EQ, 789);
  5145. tt_int_op(ent->when, OP_EQ, 768);
  5146. ent = smartlist_get(descs_dumped, 2);
  5147. tt_str_op(ent->filename, OP_EQ, "d/foo");
  5148. tt_int_op(ent->len, OP_EQ, 123);
  5149. tt_int_op(ent->when, OP_EQ, 1024);
  5150. done:
  5151. dump_desc_fifo_cleanup();
  5152. UNMOCK(dump_desc_populate_one_file);
  5153. UNMOCK(tor_listdir);
  5154. return;
  5155. }
  5156. static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
  5157. static int
  5158. mock_networkstatus_consensus_is_bootstrapping(time_t now)
  5159. {
  5160. (void)now;
  5161. return mock_networkstatus_consensus_is_bootstrapping_value;
  5162. }
  5163. static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
  5164. static int
  5165. mock_networkstatus_consensus_can_use_extra_fallbacks(
  5166. const or_options_t *options)
  5167. {
  5168. (void)options;
  5169. return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
  5170. }
  5171. static int mock_num_bridges_usable_value = 0;
  5172. static int
  5173. mock_num_bridges_usable(int use_maybe_reachable)
  5174. {
  5175. (void)use_maybe_reachable;
  5176. return mock_num_bridges_usable_value;
  5177. }
  5178. /* data is a 3 character nul-terminated string.
  5179. * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
  5180. * If data[1] is 'f', set extra fallbacks, anything else means no extra
  5181. * If data[2] is 'f', set running bridges, anything else means no extra
  5182. * fallbacks.
  5183. */
  5184. static void
  5185. test_dir_find_dl_min_delay(void* data)
  5186. {
  5187. const char *str = (const char *)data;
  5188. tt_assert(strlen(data) == 3);
  5189. if (str[0] == 'b') {
  5190. mock_networkstatus_consensus_is_bootstrapping_value = 1;
  5191. } else {
  5192. mock_networkstatus_consensus_is_bootstrapping_value = 0;
  5193. }
  5194. if (str[1] == 'f') {
  5195. mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
  5196. } else {
  5197. mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
  5198. }
  5199. if (str[2] == 'r') {
  5200. /* Any positive, non-zero value should work */
  5201. mock_num_bridges_usable_value = 2;
  5202. } else {
  5203. mock_num_bridges_usable_value = 0;
  5204. }
  5205. MOCK(networkstatus_consensus_is_bootstrapping,
  5206. mock_networkstatus_consensus_is_bootstrapping);
  5207. MOCK(networkstatus_consensus_can_use_extra_fallbacks,
  5208. mock_networkstatus_consensus_can_use_extra_fallbacks);
  5209. MOCK(num_bridges_usable,
  5210. mock_num_bridges_usable);
  5211. download_status_t dls;
  5212. const int server=10, client=20, server_cons=30, client_cons=40;
  5213. const int client_boot_auth_only_cons=50, client_boot_auth_cons=60;
  5214. const int client_boot_fallback_cons=70, bridge=80, bridge_bootstrap=90;
  5215. mock_options = tor_malloc(sizeof(or_options_t));
  5216. reset_options(mock_options, &mock_get_options_calls);
  5217. MOCK(get_options, mock_get_options);
  5218. mock_options->TestingServerDownloadInitialDelay = server;
  5219. mock_options->TestingClientDownloadInitialDelay = client;
  5220. mock_options->TestingServerConsensusDownloadInitialDelay = server_cons;
  5221. mock_options->TestingClientConsensusDownloadInitialDelay = client_cons;
  5222. mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadInitialDelay =
  5223. client_boot_auth_only_cons;
  5224. mock_options->ClientBootstrapConsensusAuthorityDownloadInitialDelay =
  5225. client_boot_auth_cons;
  5226. mock_options->ClientBootstrapConsensusFallbackDownloadInitialDelay =
  5227. client_boot_fallback_cons;
  5228. mock_options->TestingBridgeDownloadInitialDelay = bridge;
  5229. mock_options->TestingBridgeBootstrapDownloadInitialDelay = bridge_bootstrap;
  5230. dls.schedule = DL_SCHED_GENERIC;
  5231. /* client */
  5232. mock_options->ClientOnly = 1;
  5233. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ, client);
  5234. mock_options->ClientOnly = 0;
  5235. /* dir mode */
  5236. mock_options->DirPort_set = 1;
  5237. mock_options->DirCache = 1;
  5238. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ, server);
  5239. mock_options->DirPort_set = 0;
  5240. mock_options->DirCache = 0;
  5241. dls.schedule = DL_SCHED_CONSENSUS;
  5242. /* public server mode */
  5243. mock_options->ORPort_set = 1;
  5244. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ, server_cons);
  5245. mock_options->ORPort_set = 0;
  5246. /* client and bridge modes */
  5247. if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
  5248. if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
  5249. dls.want_authority = 1;
  5250. /* client */
  5251. mock_options->ClientOnly = 1;
  5252. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5253. client_boot_auth_cons);
  5254. mock_options->ClientOnly = 0;
  5255. /* bridge relay */
  5256. mock_options->ORPort_set = 1;
  5257. mock_options->BridgeRelay = 1;
  5258. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5259. client_boot_auth_cons);
  5260. mock_options->ORPort_set = 0;
  5261. mock_options->BridgeRelay = 0;
  5262. dls.want_authority = 0;
  5263. /* client */
  5264. mock_options->ClientOnly = 1;
  5265. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5266. client_boot_fallback_cons);
  5267. mock_options->ClientOnly = 0;
  5268. /* bridge relay */
  5269. mock_options->ORPort_set = 1;
  5270. mock_options->BridgeRelay = 1;
  5271. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5272. client_boot_fallback_cons);
  5273. mock_options->ORPort_set = 0;
  5274. mock_options->BridgeRelay = 0;
  5275. } else {
  5276. /* dls.want_authority is ignored */
  5277. /* client */
  5278. mock_options->ClientOnly = 1;
  5279. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5280. client_boot_auth_only_cons);
  5281. mock_options->ClientOnly = 0;
  5282. /* bridge relay */
  5283. mock_options->ORPort_set = 1;
  5284. mock_options->BridgeRelay = 1;
  5285. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5286. client_boot_auth_only_cons);
  5287. mock_options->ORPort_set = 0;
  5288. mock_options->BridgeRelay = 0;
  5289. }
  5290. } else {
  5291. /* client */
  5292. mock_options->ClientOnly = 1;
  5293. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5294. client_cons);
  5295. mock_options->ClientOnly = 0;
  5296. /* bridge relay */
  5297. mock_options->ORPort_set = 1;
  5298. mock_options->BridgeRelay = 1;
  5299. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ,
  5300. client_cons);
  5301. mock_options->ORPort_set = 0;
  5302. mock_options->BridgeRelay = 0;
  5303. }
  5304. dls.schedule = DL_SCHED_BRIDGE;
  5305. /* client */
  5306. mock_options->ClientOnly = 1;
  5307. mock_options->UseBridges = 1;
  5308. if (num_bridges_usable(0) > 0) {
  5309. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ, bridge);
  5310. } else {
  5311. tt_int_op(find_dl_min_delay(&dls, mock_options), OP_EQ, bridge_bootstrap);
  5312. }
  5313. done:
  5314. UNMOCK(networkstatus_consensus_is_bootstrapping);
  5315. UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
  5316. UNMOCK(num_bridges_usable);
  5317. UNMOCK(get_options);
  5318. tor_free(mock_options);
  5319. mock_options = NULL;
  5320. }
  5321. static void
  5322. test_dir_assumed_flags(void *arg)
  5323. {
  5324. (void)arg;
  5325. smartlist_t *tokens = smartlist_new();
  5326. memarea_t *area = memarea_new();
  5327. routerstatus_t *rs = NULL;
  5328. /* We can assume that consensus method is higher than 24, so Running and
  5329. * Valid are always implicitly set */
  5330. const char *str1 =
  5331. "r example hereiswhereyouridentitygoes 2015-08-30 12:00:00 "
  5332. "192.168.0.1 9001 0\n"
  5333. "m thisoneislongerbecauseitisa256bitmddigest33\n"
  5334. "s Fast Guard Stable\n";
  5335. const char *cp = str1;
  5336. rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL,
  5337. 24, FLAV_MICRODESC);
  5338. tt_assert(rs);
  5339. tt_assert(rs->is_flagged_running);
  5340. tt_assert(rs->is_valid);
  5341. tt_assert(! rs->is_exit);
  5342. tt_assert(rs->is_fast);
  5343. done:
  5344. smartlist_free(tokens);
  5345. memarea_drop_all(area);
  5346. routerstatus_free(rs);
  5347. }
  5348. static void
  5349. test_dir_post_parsing(void *arg)
  5350. {
  5351. (void) arg;
  5352. /* Test the version parsing from an HS descriptor publish request. */
  5353. {
  5354. const char *end;
  5355. const char *prefix = "/tor/hs/";
  5356. int version = parse_hs_version_from_post("/tor/hs//publish", prefix, &end);
  5357. tt_int_op(version, OP_EQ, -1);
  5358. tt_ptr_op(end, OP_EQ, NULL);
  5359. version = parse_hs_version_from_post("/tor/hs/a/publish", prefix, &end);
  5360. tt_int_op(version, OP_EQ, -1);
  5361. tt_ptr_op(end, OP_EQ, NULL);
  5362. version = parse_hs_version_from_post("/tor/hs/3/publish", prefix, &end);
  5363. tt_int_op(version, OP_EQ, 3);
  5364. tt_str_op(end, OP_EQ, "/publish");
  5365. version = parse_hs_version_from_post("/tor/hs/42/publish", prefix, &end);
  5366. tt_int_op(version, OP_EQ, 42);
  5367. tt_str_op(end, OP_EQ, "/publish");
  5368. version = parse_hs_version_from_post("/tor/hs/18163/publish",prefix, &end);
  5369. tt_int_op(version, OP_EQ, 18163);
  5370. tt_str_op(end, OP_EQ, "/publish");
  5371. version = parse_hs_version_from_post("JUNKJUNKJUNK", prefix, &end);
  5372. tt_int_op(version, OP_EQ, -1);
  5373. tt_ptr_op(end, OP_EQ, NULL);
  5374. version = parse_hs_version_from_post("/tor/hs/3/publish", "blah", &end);
  5375. tt_int_op(version, OP_EQ, -1);
  5376. tt_ptr_op(end, OP_EQ, NULL);
  5377. /* Missing the '/' at the end of the prefix. */
  5378. version = parse_hs_version_from_post("/tor/hs/3/publish", "/tor/hs", &end);
  5379. tt_int_op(version, OP_EQ, -1);
  5380. tt_ptr_op(end, OP_EQ, NULL);
  5381. version = parse_hs_version_from_post("/random/blah/tor/hs/3/publish",
  5382. prefix, &end);
  5383. tt_int_op(version, OP_EQ, -1);
  5384. tt_ptr_op(end, OP_EQ, NULL);
  5385. version = parse_hs_version_from_post("/tor/hs/3/publish/random/junk",
  5386. prefix, &end);
  5387. tt_int_op(version, OP_EQ, 3);
  5388. tt_str_op(end, OP_EQ, "/publish/random/junk");
  5389. version = parse_hs_version_from_post("/tor/hs/-1/publish", prefix, &end);
  5390. tt_int_op(version, OP_EQ, -1);
  5391. tt_ptr_op(end, OP_EQ, NULL);
  5392. /* INT_MAX */
  5393. version = parse_hs_version_from_post("/tor/hs/2147483647/publish",
  5394. prefix, &end);
  5395. tt_int_op(version, OP_EQ, INT_MAX);
  5396. tt_str_op(end, OP_EQ, "/publish");
  5397. /* INT_MAX + 1*/
  5398. version = parse_hs_version_from_post("/tor/hs/2147483648/publish",
  5399. prefix, &end);
  5400. tt_int_op(version, OP_EQ, -1);
  5401. tt_ptr_op(end, OP_EQ, NULL);
  5402. }
  5403. done:
  5404. ;
  5405. }
  5406. static void
  5407. test_dir_platform_str(void *arg)
  5408. {
  5409. char platform[256];
  5410. (void)arg;
  5411. platform[0] = 0;
  5412. get_platform_str(platform, sizeof(platform));
  5413. tt_int_op((int)strlen(platform), OP_GT, 0);
  5414. tt_assert(!strcmpstart(platform, "Tor "));
  5415. tor_version_t ver;
  5416. // make sure this is a tor version, a real actual tor version.
  5417. tt_int_op(tor_version_parse_platform(platform, &ver, 1), OP_EQ, 1);
  5418. TT_BLATHER(("%d.%d.%d.%d", ver.major, ver.minor, ver.micro, ver.patchlevel));
  5419. // Handle an example version.
  5420. tt_int_op(tor_version_parse_platform(
  5421. "Tor 0.3.3.3 (foo) (git-xyzzy) on a potato", &ver, 1), OP_EQ, 1);
  5422. done:
  5423. ;
  5424. }
  5425. static networkstatus_t *mock_networkstatus;
  5426. static networkstatus_t *
  5427. mock_networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  5428. {
  5429. (void)f;
  5430. return mock_networkstatus;
  5431. }
  5432. static void
  5433. test_dir_networkstatus_consensus_has_ipv6(void *arg)
  5434. {
  5435. (void)arg;
  5436. int has_ipv6 = 0;
  5437. /* Init options and networkstatus */
  5438. or_options_t our_options;
  5439. mock_options = &our_options;
  5440. reset_options(mock_options, &mock_get_options_calls);
  5441. MOCK(get_options, mock_get_options);
  5442. networkstatus_t our_networkstatus;
  5443. mock_networkstatus = &our_networkstatus;
  5444. memset(mock_networkstatus, 0, sizeof(*mock_networkstatus));
  5445. MOCK(networkstatus_get_latest_consensus_by_flavor,
  5446. mock_networkstatus_get_latest_consensus_by_flavor);
  5447. /* A live consensus */
  5448. mock_networkstatus->valid_after = time(NULL) - 3600;
  5449. mock_networkstatus->valid_until = time(NULL) + 3600;
  5450. /* Test the bounds for A lines in the NS consensus */
  5451. mock_options->UseMicrodescriptors = 0;
  5452. mock_networkstatus->consensus_method = MIN_SUPPORTED_CONSENSUS_METHOD;
  5453. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5454. tt_assert(has_ipv6);
  5455. /* Test the bounds for A lines in the microdesc consensus */
  5456. mock_options->UseMicrodescriptors = 1;
  5457. mock_networkstatus->consensus_method =
  5458. MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS;
  5459. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5460. tt_assert(has_ipv6);
  5461. mock_networkstatus->consensus_method = MAX_SUPPORTED_CONSENSUS_METHOD + 20;
  5462. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5463. tt_assert(has_ipv6);
  5464. mock_networkstatus->consensus_method =
  5465. MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS + 1;
  5466. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5467. tt_assert(has_ipv6);
  5468. mock_networkstatus->consensus_method =
  5469. MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS + 20;
  5470. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5471. tt_assert(has_ipv6);
  5472. mock_networkstatus->consensus_method =
  5473. MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS - 1;
  5474. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5475. tt_assert(!has_ipv6);
  5476. /* Test the edge cases */
  5477. mock_options->UseMicrodescriptors = 1;
  5478. mock_networkstatus->consensus_method =
  5479. MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS;
  5480. /* Reasonably live */
  5481. mock_networkstatus->valid_until = approx_time() - 60;
  5482. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5483. tt_assert(has_ipv6);
  5484. /* Not reasonably live */
  5485. mock_networkstatus->valid_after = approx_time() - 24*60*60 - 3600;
  5486. mock_networkstatus->valid_until = approx_time() - 24*60*60 - 60;
  5487. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5488. tt_assert(!has_ipv6);
  5489. /* NULL consensus */
  5490. mock_networkstatus = NULL;
  5491. has_ipv6 = networkstatus_consensus_has_ipv6(get_options());
  5492. tt_assert(!has_ipv6);
  5493. done:
  5494. UNMOCK(get_options);
  5495. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  5496. }
  5497. static void
  5498. test_dir_format_versions_list(void *arg)
  5499. {
  5500. (void)arg;
  5501. char *s = NULL;
  5502. config_line_t *lines = NULL;
  5503. setup_capture_of_logs(LOG_WARN);
  5504. s = format_recommended_version_list(lines, 1);
  5505. tt_str_op(s, OP_EQ, "");
  5506. tor_free(s);
  5507. config_line_append(&lines, "ignored", "0.3.4.1, 0.2.9.111-alpha, 4.4.4-rc");
  5508. s = format_recommended_version_list(lines, 1);
  5509. tt_str_op(s, OP_EQ, "0.2.9.111-alpha,0.3.4.1,4.4.4-rc");
  5510. tor_free(s);
  5511. config_line_append(&lines, "ignored", "0.1.2.3,0.2.9.10 ");
  5512. s = format_recommended_version_list(lines, 1);
  5513. tt_str_op(s, OP_EQ, "0.1.2.3,0.2.9.10,0.2.9.111-alpha,0.3.4.1,4.4.4-rc");
  5514. /* There should be no warnings so far. */
  5515. expect_no_log_entry();
  5516. /* Now try a line with a space in it. */
  5517. tor_free(s);
  5518. config_line_append(&lines, "ignored", "1.3.3.8 1.3.3.7");
  5519. s = format_recommended_version_list(lines, 1);
  5520. tt_str_op(s, OP_EQ, "0.1.2.3,0.2.9.10,0.2.9.111-alpha,0.3.4.1,"
  5521. "1.3.3.7,1.3.3.8,4.4.4-rc");
  5522. expect_single_log_msg_containing(
  5523. "Unexpected space in versions list member \"1.3.3.8 1.3.3.7\"." );
  5524. /* Start over, with a line containing a bogus version */
  5525. config_free_lines(lines);
  5526. lines = NULL;
  5527. tor_free(s);
  5528. mock_clean_saved_logs();
  5529. config_line_append(&lines, "ignored", "0.1.2.3, alpha-complex, 0.1.1.8-rc");
  5530. s = format_recommended_version_list(lines,1);
  5531. tt_str_op(s, OP_EQ, "0.1.1.8-rc,0.1.2.3,alpha-complex");
  5532. expect_single_log_msg_containing(
  5533. "Recommended version \"alpha-complex\" does not look valid.");
  5534. done:
  5535. tor_free(s);
  5536. config_free_lines(lines);
  5537. teardown_capture_of_logs();
  5538. }
  5539. #define DIR_LEGACY(name) \
  5540. { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
  5541. #define DIR(name,flags) \
  5542. { #name, test_dir_##name, (flags), NULL, NULL }
  5543. /* where arg is a string constant */
  5544. #define DIR_ARG(name,flags,arg) \
  5545. { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
  5546. struct testcase_t dir_tests[] = {
  5547. DIR_LEGACY(nicknames),
  5548. DIR_LEGACY(formats),
  5549. DIR(routerinfo_parsing, 0),
  5550. DIR(extrainfo_parsing, 0),
  5551. DIR(parse_router_list, TT_FORK),
  5552. DIR(load_routers, TT_FORK),
  5553. DIR(load_extrainfo, TT_FORK),
  5554. DIR(getinfo_extra, 0),
  5555. DIR_LEGACY(versions),
  5556. DIR_LEGACY(fp_pairs),
  5557. DIR(split_fps, 0),
  5558. DIR_LEGACY(measured_bw_kb),
  5559. DIR_LEGACY(measured_bw_kb_line_is_after_headers),
  5560. DIR_LEGACY(measured_bw_kb_cache),
  5561. DIR_LEGACY(dirserv_read_measured_bandwidths),
  5562. DIR_LEGACY(param_voting),
  5563. DIR(param_voting_lookup, 0),
  5564. DIR_LEGACY(v3_networkstatus),
  5565. DIR(random_weighted, 0),
  5566. DIR(scale_bw, 0),
  5567. DIR_LEGACY(clip_unmeasured_bw_kb),
  5568. DIR_LEGACY(clip_unmeasured_bw_kb_alt),
  5569. DIR(fmt_control_ns, 0),
  5570. DIR(dirserv_set_routerstatus_testing, 0),
  5571. DIR(http_handling, 0),
  5572. DIR(purpose_needs_anonymity_returns_true_for_bridges, 0),
  5573. DIR(purpose_needs_anonymity_returns_false_for_own_bridge_desc, 0),
  5574. DIR(purpose_needs_anonymity_returns_true_by_default, 0),
  5575. DIR(purpose_needs_anonymity_returns_true_for_sensitive_purpose, 0),
  5576. DIR(purpose_needs_anonymity_ret_false_for_non_sensitive_conn, 0),
  5577. DIR(post_parsing, 0),
  5578. DIR(fetch_type, 0),
  5579. DIR(packages, 0),
  5580. DIR(download_status_random_backoff, 0),
  5581. DIR(download_status_random_backoff_ranges, 0),
  5582. DIR(download_status_increment, TT_FORK),
  5583. DIR(authdir_type_to_string, 0),
  5584. DIR(conn_purpose_to_string, 0),
  5585. DIR(should_use_directory_guards, 0),
  5586. DIR(should_not_init_request_to_ourselves, TT_FORK),
  5587. DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
  5588. DIR(should_init_request_to_dir_auths, 0),
  5589. DIR(choose_compression_level, 0),
  5590. DIR(dump_unparseable_descriptors, 0),
  5591. DIR(populate_dump_desc_fifo, 0),
  5592. DIR(populate_dump_desc_fifo_2, 0),
  5593. DIR_ARG(find_dl_min_delay, TT_FORK, "bfd"),
  5594. DIR_ARG(find_dl_min_delay, TT_FORK, "bad"),
  5595. DIR_ARG(find_dl_min_delay, TT_FORK, "cfd"),
  5596. DIR_ARG(find_dl_min_delay, TT_FORK, "cad"),
  5597. DIR_ARG(find_dl_min_delay, TT_FORK, "bfr"),
  5598. DIR_ARG(find_dl_min_delay, TT_FORK, "bar"),
  5599. DIR_ARG(find_dl_min_delay, TT_FORK, "cfr"),
  5600. DIR_ARG(find_dl_min_delay, TT_FORK, "car"),
  5601. DIR(assumed_flags, 0),
  5602. DIR(networkstatus_compute_bw_weights_v10, 0),
  5603. DIR(platform_str, 0),
  5604. DIR(networkstatus_consensus_has_ipv6, TT_FORK),
  5605. DIR(format_versions_list, TT_FORK),
  5606. END_OF_TESTCASES
  5607. };