test_dir.c 241 KB

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