test_dir.c 204 KB

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