test_dir.c 223 KB

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