test_dir.c 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530
  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(chunks, tor_strdup(EX_RI_MINIMAL)); // ri 0
  610. smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS)); // bad ri 0
  611. smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL)); // ei 0
  612. smartlist_add(chunks, tor_strdup(EX_EI_BAD_SIG2)); // bad ei --
  613. smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));// bad ei 0
  614. smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG1)); // bad ri --
  615. smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED)); // bad ei 1
  616. smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL)); // ri 1
  617. smartlist_add(chunks, tor_strdup(EX_RI_BAD_FAMILY)); // bad ri 1
  618. smartlist_add(chunks, tor_strdup(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(wanted, tor_strdup(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(chunks, tor_strdup(EX_RI_MINIMAL));
  734. smartlist_add(chunks, tor_strdup(EX_RI_BAD_FINGERPRINT));
  735. smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG2));
  736. smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL));
  737. smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS));
  738. smartlist_add(chunks, tor_strdup(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(wanted, tor_strdup(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(chunks, tor_strdup(EX_EI_MINIMAL));
  836. smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));
  837. smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL));
  838. smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED));
  839. smartlist_add(chunks, tor_strdup(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 authority_cert_t *mock_cert;
  1679. static authority_cert_t *
  1680. get_my_v3_authority_cert_m(void)
  1681. {
  1682. tor_assert(mock_cert);
  1683. return mock_cert;
  1684. }
  1685. /** Run a unit tests for generating and parsing networkstatuses, with
  1686. * the supply test fns. */
  1687. static void
  1688. test_a_networkstatus(
  1689. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  1690. int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  1691. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  1692. void (*consensus_test)(networkstatus_t *con, time_t now),
  1693. void (*rs_test)(routerstatus_t *rs, time_t now))
  1694. {
  1695. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  1696. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  1697. crypto_pk_t *sign_skey_leg1=NULL;
  1698. /*
  1699. * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
  1700. * returns non-zero, it changed net_params and we should skip the tests for
  1701. * that later as they will fail.
  1702. */
  1703. int params_tweaked = 0;
  1704. time_t now = time(NULL);
  1705. networkstatus_voter_info_t *voter;
  1706. document_signature_t *sig;
  1707. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  1708. *con_md=NULL;
  1709. vote_routerstatus_t *vrs;
  1710. routerstatus_t *rs;
  1711. int idx, n_rs, n_vrs;
  1712. char *consensus_text=NULL, *cp=NULL;
  1713. smartlist_t *votes = smartlist_new();
  1714. /* For generating the two other consensuses. */
  1715. char *detached_text1=NULL, *detached_text2=NULL;
  1716. char *consensus_text2=NULL, *consensus_text3=NULL;
  1717. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  1718. char *consensus_text_md=NULL;
  1719. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  1720. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  1721. tt_assert(vrs_gen);
  1722. tt_assert(rs_test);
  1723. tt_assert(vrs_test);
  1724. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1725. /* Parse certificates and keys. */
  1726. cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  1727. tt_assert(cert1);
  1728. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  1729. tt_assert(cert2);
  1730. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  1731. tt_assert(cert3);
  1732. sign_skey_1 = crypto_pk_new();
  1733. sign_skey_2 = crypto_pk_new();
  1734. sign_skey_3 = crypto_pk_new();
  1735. sign_skey_leg1 = pk_generate(4);
  1736. sr_state_init(0, 0);
  1737. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  1738. AUTHORITY_SIGNKEY_1, -1));
  1739. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  1740. AUTHORITY_SIGNKEY_2, -1));
  1741. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  1742. AUTHORITY_SIGNKEY_3, -1));
  1743. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  1744. tt_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  1745. tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
  1746. &v1, &n_vrs, now, 1));
  1747. tt_assert(v1);
  1748. /* Make sure the parsed thing was right. */
  1749. tt_int_op(v1->type,OP_EQ, NS_TYPE_VOTE);
  1750. tt_int_op(v1->published,OP_EQ, vote->published);
  1751. tt_int_op(v1->valid_after,OP_EQ, vote->valid_after);
  1752. tt_int_op(v1->fresh_until,OP_EQ, vote->fresh_until);
  1753. tt_int_op(v1->valid_until,OP_EQ, vote->valid_until);
  1754. tt_int_op(v1->vote_seconds,OP_EQ, vote->vote_seconds);
  1755. tt_int_op(v1->dist_seconds,OP_EQ, vote->dist_seconds);
  1756. tt_str_op(v1->client_versions,OP_EQ, vote->client_versions);
  1757. tt_str_op(v1->server_versions,OP_EQ, vote->server_versions);
  1758. tt_assert(v1->voters && smartlist_len(v1->voters));
  1759. voter = smartlist_get(v1->voters, 0);
  1760. tt_str_op(voter->nickname,OP_EQ, "Voter1");
  1761. tt_str_op(voter->address,OP_EQ, "1.2.3.4");
  1762. tt_int_op(voter->addr,OP_EQ, 0x01020304);
  1763. tt_int_op(voter->dir_port,OP_EQ, 80);
  1764. tt_int_op(voter->or_port,OP_EQ, 9000);
  1765. tt_str_op(voter->contact,OP_EQ, "voter@example.com");
  1766. tt_assert(v1->cert);
  1767. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  1768. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  1769. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  1770. tor_free(cp);
  1771. tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
  1772. networkstatus_vote_free(vote);
  1773. vote = NULL;
  1774. if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
  1775. /* Check the routerstatuses. */
  1776. for (idx = 0; idx < n_vrs; ++idx) {
  1777. vrs = smartlist_get(v1->routerstatus_list, idx);
  1778. tt_assert(vrs);
  1779. vrs_test(vrs, 1, now);
  1780. }
  1781. /* Generate second vote. It disagrees on some of the times,
  1782. * and doesn't list versions, and knows some crazy flags.
  1783. * Generate and parse v2. */
  1784. tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
  1785. &v2, &n_vrs, now, 1));
  1786. tt_assert(v2);
  1787. if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
  1788. /* Check that flags come out right.*/
  1789. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1790. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1791. "Running:Stable:V2Dir:Valid");
  1792. tor_free(cp);
  1793. /* Check the routerstatuses. */
  1794. n_vrs = smartlist_len(v2->routerstatus_list);
  1795. for (idx = 0; idx < n_vrs; ++idx) {
  1796. vrs = smartlist_get(v2->routerstatus_list, idx);
  1797. tt_assert(vrs);
  1798. vrs_test(vrs, 2, now);
  1799. }
  1800. networkstatus_vote_free(vote);
  1801. vote = NULL;
  1802. /* Generate the third vote with a legacy id. */
  1803. tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
  1804. &v3, &n_vrs, now, 1));
  1805. tt_assert(v3);
  1806. if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
  1807. /* Compute a consensus as voter 3. */
  1808. smartlist_add(votes, v3);
  1809. smartlist_add(votes, v1);
  1810. smartlist_add(votes, v2);
  1811. consensus_text = networkstatus_compute_consensus(votes, 3,
  1812. cert3->identity_key,
  1813. sign_skey_3,
  1814. "AAAAAAAAAAAAAAAAAAAA",
  1815. sign_skey_leg1,
  1816. FLAV_NS);
  1817. tt_assert(consensus_text);
  1818. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  1819. NS_TYPE_CONSENSUS);
  1820. tt_assert(con);
  1821. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1822. // v1_text, v2_text, v3_text);
  1823. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1824. cert3->identity_key,
  1825. sign_skey_3,
  1826. "AAAAAAAAAAAAAAAAAAAA",
  1827. sign_skey_leg1,
  1828. FLAV_MICRODESC);
  1829. tt_assert(consensus_text_md);
  1830. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1831. NS_TYPE_CONSENSUS);
  1832. tt_assert(con_md);
  1833. tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
  1834. /* Check consensus contents. */
  1835. tt_assert(con->type == NS_TYPE_CONSENSUS);
  1836. tt_int_op(con->published,OP_EQ, 0); /* this field only appears in votes. */
  1837. tt_int_op(con->valid_after,OP_EQ, now+1000);
  1838. tt_int_op(con->fresh_until,OP_EQ, now+2003); /* median */
  1839. tt_int_op(con->valid_until,OP_EQ, now+3000);
  1840. tt_int_op(con->vote_seconds,OP_EQ, 100);
  1841. tt_int_op(con->dist_seconds,OP_EQ, 250); /* median */
  1842. tt_str_op(con->client_versions,OP_EQ, "0.1.2.14");
  1843. tt_str_op(con->server_versions,OP_EQ, "0.1.2.15,0.1.2.16");
  1844. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1845. tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1846. "Running:Stable:V2Dir:Valid");
  1847. tor_free(cp);
  1848. if (!params_tweaked) {
  1849. /* Skip this one if vote_tweaks() messed with the param lists */
  1850. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1851. tt_str_op(cp,OP_EQ, "circuitwindow=80:foo=660");
  1852. tor_free(cp);
  1853. }
  1854. tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1855. /* The voter id digests should be in this order. */
  1856. tt_assert(fast_memcmp(cert2->cache_info.identity_digest,
  1857. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1858. tt_assert(fast_memcmp(cert1->cache_info.identity_digest,
  1859. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1860. test_same_voter(smartlist_get(con->voters, 1),
  1861. smartlist_get(v2->voters, 0));
  1862. test_same_voter(smartlist_get(con->voters, 2),
  1863. smartlist_get(v1->voters, 0));
  1864. test_same_voter(smartlist_get(con->voters, 3),
  1865. smartlist_get(v3->voters, 0));
  1866. consensus_test(con, now);
  1867. /* Check the routerstatuses. */
  1868. n_rs = smartlist_len(con->routerstatus_list);
  1869. tt_assert(n_rs);
  1870. for (idx = 0; idx < n_rs; ++idx) {
  1871. rs = smartlist_get(con->routerstatus_list, idx);
  1872. tt_assert(rs);
  1873. rs_test(rs, now);
  1874. }
  1875. n_rs = smartlist_len(con_md->routerstatus_list);
  1876. tt_assert(n_rs);
  1877. for (idx = 0; idx < n_rs; ++idx) {
  1878. rs = smartlist_get(con_md->routerstatus_list, idx);
  1879. tt_assert(rs);
  1880. }
  1881. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1882. * The second one hasn't signed. The fourth one has signed: validate it. */
  1883. voter = smartlist_get(con->voters, 1);
  1884. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 0);
  1885. voter = smartlist_get(con->voters, 3);
  1886. tt_int_op(smartlist_len(voter->sigs),OP_EQ, 1);
  1887. sig = smartlist_get(voter->sigs, 0);
  1888. tt_assert(sig->signature);
  1889. tt_assert(!sig->good_signature);
  1890. tt_assert(!sig->bad_signature);
  1891. tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1892. tt_assert(sig->signature);
  1893. tt_assert(sig->good_signature);
  1894. tt_assert(!sig->bad_signature);
  1895. {
  1896. const char *msg=NULL;
  1897. /* Compute the other two signed consensuses. */
  1898. smartlist_shuffle(votes);
  1899. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1900. cert2->identity_key,
  1901. sign_skey_2, NULL,NULL,
  1902. FLAV_NS);
  1903. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1904. cert2->identity_key,
  1905. sign_skey_2, NULL,NULL,
  1906. FLAV_MICRODESC);
  1907. smartlist_shuffle(votes);
  1908. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1909. cert1->identity_key,
  1910. sign_skey_1, NULL,NULL,
  1911. FLAV_NS);
  1912. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1913. cert1->identity_key,
  1914. sign_skey_1, NULL,NULL,
  1915. FLAV_MICRODESC);
  1916. tt_assert(consensus_text2);
  1917. tt_assert(consensus_text3);
  1918. tt_assert(consensus_text_md2);
  1919. tt_assert(consensus_text_md3);
  1920. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1921. NS_TYPE_CONSENSUS);
  1922. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1923. NS_TYPE_CONSENSUS);
  1924. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1925. NS_TYPE_CONSENSUS);
  1926. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1927. NS_TYPE_CONSENSUS);
  1928. tt_assert(con2);
  1929. tt_assert(con3);
  1930. tt_assert(con_md2);
  1931. tt_assert(con_md3);
  1932. /* All three should have the same digest. */
  1933. tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(common_digests_t));
  1934. tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(common_digests_t));
  1935. tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests,
  1936. sizeof(common_digests_t));
  1937. tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests,
  1938. sizeof(common_digests_t));
  1939. /* Extract a detached signature from con3. */
  1940. detached_text1 = get_detached_sigs(con3, con_md3);
  1941. tt_assert(detached_text1);
  1942. /* Try to parse it. */
  1943. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1944. tt_assert(dsig1);
  1945. /* Are parsed values as expected? */
  1946. tt_int_op(dsig1->valid_after,OP_EQ, con3->valid_after);
  1947. tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
  1948. tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
  1949. {
  1950. common_digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1951. tt_assert(dsig_digests);
  1952. tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
  1953. con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
  1954. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1955. tt_assert(dsig_digests);
  1956. tt_mem_op(dsig_digests->d[DIGEST_SHA256],OP_EQ,
  1957. con_md3->digests.d[DIGEST_SHA256],
  1958. DIGEST256_LEN);
  1959. }
  1960. {
  1961. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1962. tt_assert(dsig_signatures);
  1963. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  1964. sig = smartlist_get(dsig_signatures, 0);
  1965. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  1966. DIGEST_LEN);
  1967. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA1);
  1968. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1969. tt_assert(dsig_signatures);
  1970. tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
  1971. sig = smartlist_get(dsig_signatures, 0);
  1972. tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
  1973. DIGEST_LEN);
  1974. tt_int_op(sig->alg,OP_EQ, DIGEST_SHA256);
  1975. }
  1976. /* Try adding it to con2. */
  1977. detached_text2 = get_detached_sigs(con2,con_md2);
  1978. tt_int_op(1,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  1979. "test", LOG_INFO, &msg));
  1980. tor_free(detached_text2);
  1981. tt_int_op(1,OP_EQ,
  1982. networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1983. LOG_INFO, &msg));
  1984. tor_free(detached_text2);
  1985. detached_text2 = get_detached_sigs(con2,con_md2);
  1986. //printf("\n<%s>\n", detached_text2);
  1987. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1988. tt_assert(dsig2);
  1989. /*
  1990. printf("\n");
  1991. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1992. char hd[64];
  1993. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1994. printf("%s\n", hd);
  1995. });
  1996. */
  1997. tt_int_op(2,OP_EQ,
  1998. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1999. tt_int_op(2,OP_EQ,
  2000. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  2001. "microdesc")));
  2002. /* Try adding to con2 twice; verify that nothing changes. */
  2003. tt_int_op(0,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
  2004. "test", LOG_INFO, &msg));
  2005. /* Add to con. */
  2006. tt_int_op(2,OP_EQ, networkstatus_add_detached_signatures(con, dsig2,
  2007. "test", LOG_INFO, &msg));
  2008. /* Check signatures */
  2009. voter = smartlist_get(con->voters, 1);
  2010. sig = smartlist_get(voter->sigs, 0);
  2011. tt_assert(sig);
  2012. tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
  2013. voter = smartlist_get(con->voters, 2);
  2014. sig = smartlist_get(voter->sigs, 0);
  2015. tt_assert(sig);
  2016. tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
  2017. }
  2018. done:
  2019. tor_free(cp);
  2020. smartlist_free(votes);
  2021. tor_free(consensus_text);
  2022. tor_free(consensus_text_md);
  2023. networkstatus_vote_free(vote);
  2024. networkstatus_vote_free(v1);
  2025. networkstatus_vote_free(v2);
  2026. networkstatus_vote_free(v3);
  2027. networkstatus_vote_free(con);
  2028. networkstatus_vote_free(con_md);
  2029. crypto_pk_free(sign_skey_1);
  2030. crypto_pk_free(sign_skey_2);
  2031. crypto_pk_free(sign_skey_3);
  2032. crypto_pk_free(sign_skey_leg1);
  2033. authority_cert_free(cert1);
  2034. authority_cert_free(cert2);
  2035. authority_cert_free(cert3);
  2036. tor_free(consensus_text2);
  2037. tor_free(consensus_text3);
  2038. tor_free(consensus_text_md2);
  2039. tor_free(consensus_text_md3);
  2040. tor_free(detached_text1);
  2041. tor_free(detached_text2);
  2042. networkstatus_vote_free(con2);
  2043. networkstatus_vote_free(con3);
  2044. networkstatus_vote_free(con_md2);
  2045. networkstatus_vote_free(con_md3);
  2046. ns_detached_signatures_free(dsig1);
  2047. ns_detached_signatures_free(dsig2);
  2048. }
  2049. /** Run unit tests for generating and parsing V3 consensus networkstatus
  2050. * documents. */
  2051. static void
  2052. test_dir_v3_networkstatus(void *arg)
  2053. {
  2054. (void)arg;
  2055. test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
  2056. vote_tweaks_for_v3ns,
  2057. test_vrs_for_v3ns,
  2058. test_consensus_for_v3ns,
  2059. test_routerstatus_for_v3ns);
  2060. }
  2061. static void
  2062. test_dir_scale_bw(void *testdata)
  2063. {
  2064. double v[8] = { 2.0/3,
  2065. 7.0,
  2066. 1.0,
  2067. 3.0,
  2068. 1.0/5,
  2069. 1.0/7,
  2070. 12.0,
  2071. 24.0 };
  2072. double vals_dbl[8];
  2073. uint64_t vals_u64[8];
  2074. uint64_t total;
  2075. int i;
  2076. (void) testdata;
  2077. for (i=0; i<8; ++i)
  2078. vals_dbl[i] = v[i];
  2079. scale_array_elements_to_u64(vals_u64, vals_dbl, 8, &total);
  2080. tt_int_op((int)total, OP_EQ, 48);
  2081. total = 0;
  2082. for (i=0; i<8; ++i) {
  2083. total += vals_u64[i];
  2084. }
  2085. tt_assert(total >= (U64_LITERAL(1)<<60));
  2086. tt_assert(total <= (U64_LITERAL(1)<<62));
  2087. for (i=0; i<8; ++i) {
  2088. /* vals[2].u64 is the scaled value of 1.0 */
  2089. double ratio = ((double)vals_u64[i]) / vals_u64[2];
  2090. tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
  2091. }
  2092. /* test handling of no entries */
  2093. total = 1;
  2094. scale_array_elements_to_u64(vals_u64, vals_dbl, 0, &total);
  2095. tt_assert(total == 0);
  2096. /* make sure we don't read the array when we have no entries
  2097. * may require compiler flags to catch NULL dereferences */
  2098. total = 1;
  2099. scale_array_elements_to_u64(NULL, NULL, 0, &total);
  2100. tt_assert(total == 0);
  2101. scale_array_elements_to_u64(NULL, NULL, 0, NULL);
  2102. /* test handling of zero totals */
  2103. total = 1;
  2104. vals_dbl[0] = 0.0;
  2105. scale_array_elements_to_u64(vals_u64, vals_dbl, 1, &total);
  2106. tt_assert(total == 0);
  2107. tt_assert(vals_u64[0] == 0);
  2108. vals_dbl[0] = 0.0;
  2109. vals_dbl[1] = 0.0;
  2110. scale_array_elements_to_u64(vals_u64, vals_dbl, 2, NULL);
  2111. tt_assert(vals_u64[0] == 0);
  2112. tt_assert(vals_u64[1] == 0);
  2113. done:
  2114. ;
  2115. }
  2116. static void
  2117. test_dir_random_weighted(void *testdata)
  2118. {
  2119. int histogram[10];
  2120. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  2121. uint64_t inp_u64[10];
  2122. int i, choice;
  2123. const int n = 50000;
  2124. double max_sq_error;
  2125. (void) testdata;
  2126. /* Try a ten-element array with values from 0 through 10. The values are
  2127. * in a scrambled order to make sure we don't depend on order. */
  2128. memset(histogram,0,sizeof(histogram));
  2129. for (i=0; i<10; ++i) {
  2130. inp_u64[i] = vals[i];
  2131. total += vals[i];
  2132. }
  2133. tt_u64_op(total, OP_EQ, 45);
  2134. for (i=0; i<n; ++i) {
  2135. choice = choose_array_element_by_weight(inp_u64, 10);
  2136. tt_int_op(choice, OP_GE, 0);
  2137. tt_int_op(choice, OP_LT, 10);
  2138. histogram[choice]++;
  2139. }
  2140. /* Now see if we chose things about frequently enough. */
  2141. max_sq_error = 0;
  2142. for (i=0; i<10; ++i) {
  2143. int expected = (int)(n*vals[i]/total);
  2144. double frac_diff = 0, sq;
  2145. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  2146. if (expected)
  2147. frac_diff = (histogram[i] - expected) / ((double)expected);
  2148. else
  2149. tt_int_op(histogram[i], OP_EQ, 0);
  2150. sq = frac_diff * frac_diff;
  2151. if (sq > max_sq_error)
  2152. max_sq_error = sq;
  2153. }
  2154. /* It should almost always be much much less than this. If you want to
  2155. * figure out the odds, please feel free. */
  2156. tt_double_op(max_sq_error, OP_LT, .05);
  2157. /* Now try a singleton; do we choose it? */
  2158. for (i = 0; i < 100; ++i) {
  2159. choice = choose_array_element_by_weight(inp_u64, 1);
  2160. tt_int_op(choice, OP_EQ, 0);
  2161. }
  2162. /* Now try an array of zeros. We should choose randomly. */
  2163. memset(histogram,0,sizeof(histogram));
  2164. for (i = 0; i < 5; ++i)
  2165. inp_u64[i] = 0;
  2166. for (i = 0; i < n; ++i) {
  2167. choice = choose_array_element_by_weight(inp_u64, 5);
  2168. tt_int_op(choice, OP_GE, 0);
  2169. tt_int_op(choice, OP_LT, 5);
  2170. histogram[choice]++;
  2171. }
  2172. /* Now see if we chose things about frequently enough. */
  2173. max_sq_error = 0;
  2174. for (i=0; i<5; ++i) {
  2175. int expected = n/5;
  2176. double frac_diff = 0, sq;
  2177. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  2178. frac_diff = (histogram[i] - expected) / ((double)expected);
  2179. sq = frac_diff * frac_diff;
  2180. if (sq > max_sq_error)
  2181. max_sq_error = sq;
  2182. }
  2183. /* It should almost always be much much less than this. If you want to
  2184. * figure out the odds, please feel free. */
  2185. tt_double_op(max_sq_error, OP_LT, .05);
  2186. done:
  2187. ;
  2188. }
  2189. /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
  2190. static uint32_t alternate_clip_bw = 0;
  2191. /**
  2192. * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
  2193. * v3_networkstatus ones.
  2194. */
  2195. static vote_routerstatus_t *
  2196. gen_routerstatus_for_umbw(int idx, time_t now)
  2197. {
  2198. vote_routerstatus_t *vrs = NULL;
  2199. routerstatus_t *rs;
  2200. tor_addr_t addr_ipv6;
  2201. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2202. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2203. switch (idx) {
  2204. case 0:
  2205. /* Generate the first routerstatus. */
  2206. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2207. rs = &vrs->status;
  2208. vrs->version = tor_strdup("0.1.2.14");
  2209. rs->published_on = now-1500;
  2210. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  2211. memset(rs->identity_digest, 3, DIGEST_LEN);
  2212. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  2213. rs->addr = 0x99008801;
  2214. rs->or_port = 443;
  2215. rs->dir_port = 8000;
  2216. /* all flags but running cleared */
  2217. rs->is_flagged_running = 1;
  2218. /*
  2219. * This one has measured bandwidth below the clip cutoff, and
  2220. * so shouldn't be clipped; we'll have to test that it isn't
  2221. * later.
  2222. */
  2223. vrs->has_measured_bw = 1;
  2224. rs->has_bandwidth = 1;
  2225. vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  2226. break;
  2227. case 1:
  2228. /* Generate the second routerstatus. */
  2229. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2230. rs = &vrs->status;
  2231. vrs->version = tor_strdup("0.2.0.5");
  2232. rs->published_on = now-1000;
  2233. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  2234. memset(rs->identity_digest, 5, DIGEST_LEN);
  2235. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  2236. rs->addr = 0x99009901;
  2237. rs->or_port = 443;
  2238. rs->dir_port = 0;
  2239. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2240. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  2241. rs->ipv6_orport = 4711;
  2242. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  2243. rs->is_valid = rs->is_possible_guard = 1;
  2244. /*
  2245. * This one has measured bandwidth above the clip cutoff, and
  2246. * so shouldn't be clipped; we'll have to test that it isn't
  2247. * later.
  2248. */
  2249. vrs->has_measured_bw = 1;
  2250. rs->has_bandwidth = 1;
  2251. vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  2252. break;
  2253. case 2:
  2254. /* Generate the third routerstatus. */
  2255. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2256. rs = &vrs->status;
  2257. vrs->version = tor_strdup("0.1.0.3");
  2258. rs->published_on = now-1000;
  2259. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  2260. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  2261. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  2262. rs->addr = 0xAA009901;
  2263. rs->or_port = 400;
  2264. rs->dir_port = 9999;
  2265. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  2266. rs->is_flagged_running = rs->is_valid =
  2267. rs->is_possible_guard = 1;
  2268. /*
  2269. * This one has unmeasured bandwidth above the clip cutoff, and
  2270. * so should be clipped; we'll have to test that it isn't
  2271. * later.
  2272. */
  2273. vrs->has_measured_bw = 0;
  2274. rs->has_bandwidth = 1;
  2275. vrs->measured_bw_kb = 0;
  2276. rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  2277. break;
  2278. case 3:
  2279. /* Generate a fourth routerstatus that is not running. */
  2280. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  2281. rs = &vrs->status;
  2282. vrs->version = tor_strdup("0.1.6.3");
  2283. rs->published_on = now-1000;
  2284. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  2285. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  2286. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  2287. rs->addr = 0xC0000203;
  2288. rs->or_port = 500;
  2289. rs->dir_port = 1999;
  2290. /* all flags but running cleared */
  2291. rs->is_flagged_running = 1;
  2292. /*
  2293. * This one has unmeasured bandwidth below the clip cutoff, and
  2294. * so shouldn't be clipped; we'll have to test that it isn't
  2295. * later.
  2296. */
  2297. vrs->has_measured_bw = 0;
  2298. rs->has_bandwidth = 1;
  2299. vrs->measured_bw_kb = 0;
  2300. rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  2301. break;
  2302. case 4:
  2303. /* No more for this test; return NULL */
  2304. vrs = NULL;
  2305. break;
  2306. default:
  2307. /* Shouldn't happen */
  2308. tt_assert(0);
  2309. }
  2310. if (vrs) {
  2311. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  2312. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  2313. "m 9,10,11,12,13,14,15,16,17 "
  2314. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  2315. idx);
  2316. }
  2317. done:
  2318. return vrs;
  2319. }
  2320. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  2321. * just adding the right consensus methods to let clipping happen */
  2322. static int
  2323. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  2324. {
  2325. char *maxbw_param = NULL;
  2326. int rv = 0;
  2327. tt_assert(v);
  2328. (void)voter;
  2329. (void)now;
  2330. tt_assert(v->supported_methods);
  2331. SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
  2332. smartlist_clear(v->supported_methods);
  2333. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
  2334. smartlist_split_string(v->supported_methods,
  2335. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
  2336. NULL, 0, -1);
  2337. /* If we're using a non-default clip bandwidth, add it to net_params */
  2338. if (alternate_clip_bw > 0) {
  2339. tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
  2340. tt_assert(maxbw_param);
  2341. if (maxbw_param) {
  2342. smartlist_add(v->net_params, maxbw_param);
  2343. rv = 1;
  2344. }
  2345. }
  2346. done:
  2347. return rv;
  2348. }
  2349. /**
  2350. * Test a parsed vote_routerstatus_t for umbw test.
  2351. */
  2352. static void
  2353. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  2354. {
  2355. routerstatus_t *rs;
  2356. tor_addr_t addr_ipv6;
  2357. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2358. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2359. (void)voter;
  2360. tt_assert(vrs);
  2361. rs = &(vrs->status);
  2362. tt_assert(rs);
  2363. /* Split out by digests to test */
  2364. if (tor_memeq(rs->identity_digest,
  2365. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2366. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2367. DIGEST_LEN)) {
  2368. /*
  2369. * Check the first routerstatus - measured bandwidth below the clip
  2370. * cutoff.
  2371. */
  2372. tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
  2373. tt_int_op(rs->published_on,OP_EQ, now-1500);
  2374. tt_str_op(rs->nickname,OP_EQ, "router2");
  2375. tt_mem_op(rs->identity_digest,OP_EQ,
  2376. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2377. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2378. DIGEST_LEN);
  2379. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2380. tt_int_op(rs->addr,OP_EQ, 0x99008801);
  2381. tt_int_op(rs->or_port,OP_EQ, 443);
  2382. tt_int_op(rs->dir_port,OP_EQ, 8000);
  2383. tt_assert(rs->has_bandwidth);
  2384. tt_assert(vrs->has_measured_bw);
  2385. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2386. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2387. } else if (tor_memeq(rs->identity_digest,
  2388. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2389. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2390. DIGEST_LEN)) {
  2391. /*
  2392. * Check the second routerstatus - measured bandwidth above the clip
  2393. * cutoff.
  2394. */
  2395. tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
  2396. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2397. tt_str_op(rs->nickname,OP_EQ, "router1");
  2398. tt_mem_op(rs->identity_digest,OP_EQ,
  2399. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2400. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2401. DIGEST_LEN);
  2402. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2403. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2404. tt_int_op(rs->or_port,OP_EQ, 443);
  2405. tt_int_op(rs->dir_port,OP_EQ, 0);
  2406. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2407. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2408. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2409. tt_assert(rs->has_bandwidth);
  2410. tt_assert(vrs->has_measured_bw);
  2411. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2412. tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2413. } else if (tor_memeq(rs->identity_digest,
  2414. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2415. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2416. DIGEST_LEN)) {
  2417. /*
  2418. * Check the third routerstatus - unmeasured bandwidth above the clip
  2419. * cutoff; this one should be clipped later on in the consensus, but
  2420. * appears unclipped in the vote.
  2421. */
  2422. tt_assert(rs->has_bandwidth);
  2423. tt_assert(!(vrs->has_measured_bw));
  2424. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2425. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  2426. } else if (tor_memeq(rs->identity_digest,
  2427. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  2428. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  2429. DIGEST_LEN)) {
  2430. /*
  2431. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  2432. * cutoff; this one should not be clipped.
  2433. */
  2434. tt_assert(rs->has_bandwidth);
  2435. tt_assert(!(vrs->has_measured_bw));
  2436. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2437. tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
  2438. } else {
  2439. tt_assert(0);
  2440. }
  2441. done:
  2442. return;
  2443. }
  2444. /**
  2445. * Test a consensus for v3_networkstatus_test
  2446. */
  2447. static void
  2448. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  2449. {
  2450. (void)now;
  2451. tt_assert(con);
  2452. tt_assert(!con->cert);
  2453. // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
  2454. tt_assert(con->consensus_method >= 16);
  2455. tt_int_op(4,OP_EQ, smartlist_len(con->routerstatus_list));
  2456. /* There should be four listed routers; all voters saw the same in this */
  2457. done:
  2458. return;
  2459. }
  2460. /**
  2461. * Test a router list entry for umbw test
  2462. */
  2463. static void
  2464. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  2465. {
  2466. tor_addr_t addr_ipv6;
  2467. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  2468. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  2469. tt_assert(rs);
  2470. /* There should be four listed routers, as constructed above */
  2471. if (tor_memeq(rs->identity_digest,
  2472. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2473. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2474. DIGEST_LEN)) {
  2475. tt_mem_op(rs->identity_digest,OP_EQ,
  2476. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  2477. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  2478. DIGEST_LEN);
  2479. tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  2480. tt_assert(!rs->is_authority);
  2481. tt_assert(!rs->is_exit);
  2482. tt_assert(!rs->is_fast);
  2483. tt_assert(!rs->is_possible_guard);
  2484. tt_assert(!rs->is_stable);
  2485. /* (If it wasn't running it wouldn't be here) */
  2486. tt_assert(rs->is_flagged_running);
  2487. tt_assert(!rs->is_valid);
  2488. tt_assert(!rs->is_named);
  2489. /* This one should have measured bandwidth below the clip cutoff */
  2490. tt_assert(rs->has_bandwidth);
  2491. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2492. tt_assert(!(rs->bw_is_unmeasured));
  2493. } else if (tor_memeq(rs->identity_digest,
  2494. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2495. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2496. DIGEST_LEN)) {
  2497. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  2498. tt_mem_op(rs->identity_digest,OP_EQ,
  2499. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2500. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2501. DIGEST_LEN);
  2502. tt_str_op(rs->nickname,OP_EQ, "router1");
  2503. tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2504. tt_int_op(rs->published_on,OP_EQ, now-1000);
  2505. tt_int_op(rs->addr,OP_EQ, 0x99009901);
  2506. tt_int_op(rs->or_port,OP_EQ, 443);
  2507. tt_int_op(rs->dir_port,OP_EQ, 0);
  2508. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2509. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2510. tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
  2511. tt_assert(!rs->is_authority);
  2512. tt_assert(rs->is_exit);
  2513. tt_assert(rs->is_fast);
  2514. tt_assert(rs->is_possible_guard);
  2515. tt_assert(rs->is_stable);
  2516. tt_assert(rs->is_flagged_running);
  2517. tt_assert(rs->is_valid);
  2518. tt_assert(!rs->is_named);
  2519. /* This one should have measured bandwidth above the clip cutoff */
  2520. tt_assert(rs->has_bandwidth);
  2521. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
  2522. tt_assert(!(rs->bw_is_unmeasured));
  2523. } else if (tor_memeq(rs->identity_digest,
  2524. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2525. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2526. DIGEST_LEN)) {
  2527. /*
  2528. * This one should have unmeasured bandwidth above the clip cutoff,
  2529. * and so should be clipped
  2530. */
  2531. tt_assert(rs->has_bandwidth);
  2532. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb);
  2533. tt_assert(rs->bw_is_unmeasured);
  2534. } else if (tor_memeq(rs->identity_digest,
  2535. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  2536. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  2537. DIGEST_LEN)) {
  2538. /*
  2539. * This one should have unmeasured bandwidth below the clip cutoff,
  2540. * and so should not be clipped
  2541. */
  2542. tt_assert(rs->has_bandwidth);
  2543. tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
  2544. tt_assert(rs->bw_is_unmeasured);
  2545. } else {
  2546. /* Weren't expecting this... */
  2547. tt_assert(0);
  2548. }
  2549. done:
  2550. return;
  2551. }
  2552. /**
  2553. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  2554. * method 17; this uses the same test_a_networkstatus() function that the
  2555. * v3_networkstatus test uses.
  2556. */
  2557. static void
  2558. test_dir_clip_unmeasured_bw_kb(void *arg)
  2559. {
  2560. /* Run the test with the default clip bandwidth */
  2561. (void)arg;
  2562. alternate_clip_bw = 0;
  2563. test_a_networkstatus(gen_routerstatus_for_umbw,
  2564. vote_tweaks_for_umbw,
  2565. test_vrs_for_umbw,
  2566. test_consensus_for_umbw,
  2567. test_routerstatus_for_umbw);
  2568. }
  2569. /**
  2570. * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
  2571. * of clip bandwidth.
  2572. */
  2573. static void
  2574. test_dir_clip_unmeasured_bw_kb_alt(void *arg)
  2575. {
  2576. /*
  2577. * Try a different one; this value is chosen so that the below-the-cutoff
  2578. * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
  2579. * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
  2580. * cutoff it will fail the test.
  2581. */
  2582. (void)arg;
  2583. alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
  2584. test_a_networkstatus(gen_routerstatus_for_umbw,
  2585. vote_tweaks_for_umbw,
  2586. test_vrs_for_umbw,
  2587. test_consensus_for_umbw,
  2588. test_routerstatus_for_umbw);
  2589. }
  2590. static void
  2591. test_dir_fmt_control_ns(void *arg)
  2592. {
  2593. char *s = NULL;
  2594. routerstatus_t rs;
  2595. (void)arg;
  2596. memset(&rs, 0, sizeof(rs));
  2597. rs.published_on = 1364925198;
  2598. strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
  2599. memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
  2600. memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
  2601. rs.addr = 0x20304050;
  2602. rs.or_port = 9001;
  2603. rs.dir_port = 9002;
  2604. rs.is_exit = 1;
  2605. rs.is_fast = 1;
  2606. rs.is_flagged_running = 1;
  2607. rs.has_bandwidth = 1;
  2608. rs.is_v2_dir = 1;
  2609. rs.bandwidth_kb = 1000;
  2610. s = networkstatus_getinfo_helper_single(&rs);
  2611. tt_assert(s);
  2612. tt_str_op(s, OP_EQ,
  2613. "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
  2614. "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
  2615. "32.48.64.80 9001 9002\n"
  2616. "s Exit Fast Running V2Dir\n"
  2617. "w Bandwidth=1000\n");
  2618. done:
  2619. tor_free(s);
  2620. }
  2621. static int mock_get_options_calls = 0;
  2622. static or_options_t *mock_options = NULL;
  2623. static void
  2624. reset_options(or_options_t *options, int *get_options_calls)
  2625. {
  2626. memset(options, 0, sizeof(or_options_t));
  2627. options->TestingTorNetwork = 1;
  2628. *get_options_calls = 0;
  2629. }
  2630. static const or_options_t *
  2631. mock_get_options(void)
  2632. {
  2633. ++mock_get_options_calls;
  2634. tor_assert(mock_options);
  2635. return mock_options;
  2636. }
  2637. static void
  2638. reset_routerstatus(routerstatus_t *rs,
  2639. const char *hex_identity_digest,
  2640. int32_t ipv4_addr)
  2641. {
  2642. memset(rs, 0, sizeof(routerstatus_t));
  2643. base16_decode(rs->identity_digest, sizeof(rs->identity_digest),
  2644. hex_identity_digest, HEX_DIGEST_LEN);
  2645. /* A zero address matches everything, so the address needs to be set.
  2646. * But the specific value is irrelevant. */
  2647. rs->addr = ipv4_addr;
  2648. }
  2649. #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  2650. #define ROUTER_A_IPV4 0xAA008801
  2651. #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  2652. #define ROUTER_B_IPV4 0xBB008801
  2653. #define ROUTERSET_ALL_STR "*"
  2654. #define ROUTERSET_A_STR ROUTER_A_ID_STR
  2655. #define ROUTERSET_NONE_STR ""
  2656. /*
  2657. * Test that dirserv_set_routerstatus_testing sets router flags correctly
  2658. * Using "*" sets flags on A and B
  2659. * Using "A" sets flags on A
  2660. * Using "" sets flags on Neither
  2661. * If the router is not included:
  2662. * - if *Strict is set, the flag is set to 0,
  2663. * - otherwise, the flag is not modified. */
  2664. static void
  2665. test_dir_dirserv_set_routerstatus_testing(void *arg)
  2666. {
  2667. (void)arg;
  2668. /* Init options */
  2669. mock_options = tor_malloc(sizeof(or_options_t));
  2670. reset_options(mock_options, &mock_get_options_calls);
  2671. MOCK(get_options, mock_get_options);
  2672. /* Init routersets */
  2673. routerset_t *routerset_all = routerset_new();
  2674. routerset_parse(routerset_all, ROUTERSET_ALL_STR, "All routers");
  2675. routerset_t *routerset_a = routerset_new();
  2676. routerset_parse(routerset_a, ROUTERSET_A_STR, "Router A only");
  2677. routerset_t *routerset_none = routerset_new();
  2678. /* Routersets are empty when provided by routerset_new(),
  2679. * so this is not strictly necessary */
  2680. routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
  2681. /* Init routerstatuses */
  2682. routerstatus_t *rs_a = tor_malloc(sizeof(routerstatus_t));
  2683. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2684. routerstatus_t *rs_b = tor_malloc(sizeof(routerstatus_t));
  2685. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2686. /* Sanity check that routersets correspond to routerstatuses.
  2687. * Return values are {2, 3, 4} */
  2688. /* We want 3 ("*" means match all addresses) */
  2689. tt_assert(routerset_contains_routerstatus(routerset_all, rs_a, 0) == 3);
  2690. tt_assert(routerset_contains_routerstatus(routerset_all, rs_b, 0) == 3);
  2691. /* We want 4 (match id_digest [or nickname]) */
  2692. tt_assert(routerset_contains_routerstatus(routerset_a, rs_a, 0) == 4);
  2693. tt_assert(routerset_contains_routerstatus(routerset_a, rs_b, 0) == 0);
  2694. tt_assert(routerset_contains_routerstatus(routerset_none, rs_a, 0) == 0);
  2695. tt_assert(routerset_contains_routerstatus(routerset_none, rs_b, 0) == 0);
  2696. /* Check that "*" sets flags on all routers: Exit
  2697. * Check the flags aren't being confused with each other */
  2698. reset_options(mock_options, &mock_get_options_calls);
  2699. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2700. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2701. mock_options->TestingDirAuthVoteExit = routerset_all;
  2702. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2703. dirserv_set_routerstatus_testing(rs_a);
  2704. tt_assert(mock_get_options_calls == 1);
  2705. dirserv_set_routerstatus_testing(rs_b);
  2706. tt_assert(mock_get_options_calls == 2);
  2707. tt_assert(rs_a->is_exit == 1);
  2708. tt_assert(rs_b->is_exit == 1);
  2709. /* Be paranoid - check no other flags are set */
  2710. tt_assert(rs_a->is_possible_guard == 0);
  2711. tt_assert(rs_b->is_possible_guard == 0);
  2712. tt_assert(rs_a->is_hs_dir == 0);
  2713. tt_assert(rs_b->is_hs_dir == 0);
  2714. /* Check that "*" sets flags on all routers: Guard & HSDir
  2715. * Cover the remaining flags in one test */
  2716. reset_options(mock_options, &mock_get_options_calls);
  2717. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2718. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2719. mock_options->TestingDirAuthVoteGuard = routerset_all;
  2720. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2721. mock_options->TestingDirAuthVoteHSDir = routerset_all;
  2722. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2723. dirserv_set_routerstatus_testing(rs_a);
  2724. tt_assert(mock_get_options_calls == 1);
  2725. dirserv_set_routerstatus_testing(rs_b);
  2726. tt_assert(mock_get_options_calls == 2);
  2727. tt_assert(rs_a->is_possible_guard == 1);
  2728. tt_assert(rs_b->is_possible_guard == 1);
  2729. tt_assert(rs_a->is_hs_dir == 1);
  2730. tt_assert(rs_b->is_hs_dir == 1);
  2731. /* Be paranoid - check exit isn't set */
  2732. tt_assert(rs_a->is_exit == 0);
  2733. tt_assert(rs_b->is_exit == 0);
  2734. /* Check routerset A sets all flags on router A,
  2735. * but leaves router B unmodified */
  2736. reset_options(mock_options, &mock_get_options_calls);
  2737. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2738. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2739. mock_options->TestingDirAuthVoteExit = routerset_a;
  2740. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2741. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2742. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2743. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2744. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2745. dirserv_set_routerstatus_testing(rs_a);
  2746. tt_assert(mock_get_options_calls == 1);
  2747. dirserv_set_routerstatus_testing(rs_b);
  2748. tt_assert(mock_get_options_calls == 2);
  2749. tt_assert(rs_a->is_exit == 1);
  2750. tt_assert(rs_b->is_exit == 0);
  2751. tt_assert(rs_a->is_possible_guard == 1);
  2752. tt_assert(rs_b->is_possible_guard == 0);
  2753. tt_assert(rs_a->is_hs_dir == 1);
  2754. tt_assert(rs_b->is_hs_dir == 0);
  2755. /* Check routerset A unsets all flags on router B when Strict is set */
  2756. reset_options(mock_options, &mock_get_options_calls);
  2757. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2758. mock_options->TestingDirAuthVoteExit = routerset_a;
  2759. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  2760. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2761. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  2762. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2763. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  2764. rs_b->is_exit = 1;
  2765. rs_b->is_possible_guard = 1;
  2766. rs_b->is_hs_dir = 1;
  2767. dirserv_set_routerstatus_testing(rs_b);
  2768. tt_assert(mock_get_options_calls == 1);
  2769. tt_assert(rs_b->is_exit == 0);
  2770. tt_assert(rs_b->is_possible_guard == 0);
  2771. tt_assert(rs_b->is_hs_dir == 0);
  2772. /* Check routerset A doesn't modify flags on router B without Strict set */
  2773. reset_options(mock_options, &mock_get_options_calls);
  2774. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2775. mock_options->TestingDirAuthVoteExit = routerset_a;
  2776. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2777. mock_options->TestingDirAuthVoteGuard = routerset_a;
  2778. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2779. mock_options->TestingDirAuthVoteHSDir = routerset_a;
  2780. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2781. rs_b->is_exit = 1;
  2782. rs_b->is_possible_guard = 1;
  2783. rs_b->is_hs_dir = 1;
  2784. dirserv_set_routerstatus_testing(rs_b);
  2785. tt_assert(mock_get_options_calls == 1);
  2786. tt_assert(rs_b->is_exit == 1);
  2787. tt_assert(rs_b->is_possible_guard == 1);
  2788. tt_assert(rs_b->is_hs_dir == 1);
  2789. /* Check the empty routerset zeroes all flags
  2790. * on routers A & B with Strict set */
  2791. reset_options(mock_options, &mock_get_options_calls);
  2792. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2793. mock_options->TestingDirAuthVoteExit = routerset_none;
  2794. mock_options->TestingDirAuthVoteExitIsStrict = 1;
  2795. mock_options->TestingDirAuthVoteGuard = routerset_none;
  2796. mock_options->TestingDirAuthVoteGuardIsStrict = 1;
  2797. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  2798. mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
  2799. rs_b->is_exit = 1;
  2800. rs_b->is_possible_guard = 1;
  2801. rs_b->is_hs_dir = 1;
  2802. dirserv_set_routerstatus_testing(rs_b);
  2803. tt_assert(mock_get_options_calls == 1);
  2804. tt_assert(rs_b->is_exit == 0);
  2805. tt_assert(rs_b->is_possible_guard == 0);
  2806. tt_assert(rs_b->is_hs_dir == 0);
  2807. /* Check the empty routerset doesn't modify any flags
  2808. * on A or B without Strict set */
  2809. reset_options(mock_options, &mock_get_options_calls);
  2810. reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
  2811. reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
  2812. mock_options->TestingDirAuthVoteExit = routerset_none;
  2813. mock_options->TestingDirAuthVoteExitIsStrict = 0;
  2814. mock_options->TestingDirAuthVoteGuard = routerset_none;
  2815. mock_options->TestingDirAuthVoteGuardIsStrict = 0;
  2816. mock_options->TestingDirAuthVoteHSDir = routerset_none;
  2817. mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
  2818. rs_b->is_exit = 1;
  2819. rs_b->is_possible_guard = 1;
  2820. rs_b->is_hs_dir = 1;
  2821. dirserv_set_routerstatus_testing(rs_a);
  2822. tt_assert(mock_get_options_calls == 1);
  2823. dirserv_set_routerstatus_testing(rs_b);
  2824. tt_assert(mock_get_options_calls == 2);
  2825. tt_assert(rs_a->is_exit == 0);
  2826. tt_assert(rs_a->is_possible_guard == 0);
  2827. tt_assert(rs_a->is_hs_dir == 0);
  2828. tt_assert(rs_b->is_exit == 1);
  2829. tt_assert(rs_b->is_possible_guard == 1);
  2830. tt_assert(rs_b->is_hs_dir == 1);
  2831. done:
  2832. tor_free(mock_options);
  2833. mock_options = NULL;
  2834. UNMOCK(get_options);
  2835. routerset_free(routerset_all);
  2836. routerset_free(routerset_a);
  2837. routerset_free(routerset_none);
  2838. tor_free(rs_a);
  2839. tor_free(rs_b);
  2840. }
  2841. static void
  2842. test_dir_http_handling(void *args)
  2843. {
  2844. char *url = NULL;
  2845. (void)args;
  2846. /* Parse http url tests: */
  2847. /* Good headers */
  2848. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
  2849. "Host: example.com\r\n"
  2850. "User-Agent: Mozilla/5.0 (Windows;"
  2851. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2852. &url),OP_EQ, 0);
  2853. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2854. tor_free(url);
  2855. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),OP_EQ, 0);
  2856. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2857. tor_free(url);
  2858. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),
  2859. OP_EQ, 0);
  2860. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2861. tor_free(url);
  2862. /* Should prepend '/tor/' to url if required */
  2863. tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
  2864. "Host: example.com\r\n"
  2865. "User-Agent: Mozilla/5.0 (Windows;"
  2866. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2867. &url),OP_EQ, 0);
  2868. tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
  2869. tor_free(url);
  2870. /* Bad headers -- no HTTP/1.x*/
  2871. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2872. "Host: example.com\r\n"
  2873. "User-Agent: Mozilla/5.0 (Windows;"
  2874. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2875. &url),OP_EQ, -1);
  2876. tt_assert(!url);
  2877. /* Bad headers */
  2878. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2879. "Host: example.com\r\n"
  2880. "User-Agent: Mozilla/5.0 (Windows;"
  2881. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2882. &url),OP_EQ, -1);
  2883. tt_assert(!url);
  2884. tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),OP_EQ, -1);
  2885. tt_assert(!url);
  2886. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),OP_EQ, -1);
  2887. tt_assert(!url);
  2888. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),
  2889. OP_EQ, -1);
  2890. tt_assert(!url);
  2891. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),OP_EQ, -1);
  2892. tt_assert(!url);
  2893. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),OP_EQ, -1);
  2894. tt_assert(!url);
  2895. done:
  2896. tor_free(url);
  2897. }
  2898. static void
  2899. test_dir_purpose_needs_anonymity(void *arg)
  2900. {
  2901. (void)arg;
  2902. tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE));
  2903. tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_GENERAL));
  2904. tt_int_op(0, ==, purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC,
  2905. ROUTER_PURPOSE_GENERAL));
  2906. done: ;
  2907. }
  2908. static void
  2909. test_dir_fetch_type(void *arg)
  2910. {
  2911. (void)arg;
  2912. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
  2913. NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
  2914. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
  2915. NULL), OP_EQ, EXTRAINFO_DIRINFO | V3_DIRINFO);
  2916. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
  2917. NULL), OP_EQ, BRIDGE_DIRINFO);
  2918. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC,
  2919. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  2920. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE,
  2921. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  2922. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  2923. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  2924. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE,
  2925. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
  2926. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
  2927. "microdesc"), OP_EQ, V3_DIRINFO|MICRODESC_DIRINFO);
  2928. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
  2929. NULL), OP_EQ, V3_DIRINFO);
  2930. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
  2931. NULL), OP_EQ, MICRODESC_DIRINFO);
  2932. /* This will give a warning, because this function isn't supposed to be
  2933. * used for HS descriptors. */
  2934. setup_full_capture_of_logs(LOG_WARN);
  2935. tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
  2936. ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
  2937. expect_single_log_msg_containing("Unexpected purpose");
  2938. done:
  2939. teardown_capture_of_logs();
  2940. }
  2941. static void
  2942. test_dir_packages(void *arg)
  2943. {
  2944. smartlist_t *votes = smartlist_new();
  2945. char *res = NULL;
  2946. (void)arg;
  2947. #define BAD(s) \
  2948. tt_int_op(0, ==, validate_recommended_package_line(s));
  2949. #define GOOD(s) \
  2950. tt_int_op(1, ==, validate_recommended_package_line(s));
  2951. GOOD("tor 0.2.6.3-alpha "
  2952. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2953. "sha256=sssdlkfjdsklfjdskfljasdklfj");
  2954. GOOD("tor 0.2.6.3-alpha "
  2955. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2956. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
  2957. BAD("tor 0.2.6.3-alpha "
  2958. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2959. "sha256=sssdlkfjdsklfjdskfljasdklfj=");
  2960. BAD("tor 0.2.6.3-alpha "
  2961. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
  2962. "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
  2963. BAD("tor 0.2.6.3-alpha "
  2964. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
  2965. BAD("tor 0.2.6.3-alpha "
  2966. "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
  2967. BAD("tor 0.2.6.3-alpha ");
  2968. BAD("tor 0.2.6.3-alpha");
  2969. BAD("tor ");
  2970. BAD("tor");
  2971. BAD("");
  2972. BAD("=foobar sha256="
  2973. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2974. BAD("= = sha256="
  2975. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2976. BAD("sha512= sha256="
  2977. "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
  2978. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2979. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2980. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2981. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2982. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2983. smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
  2984. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  2985. ns->package_lines = smartlist_new());
  2986. #define ADD(i, s) \
  2987. smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
  2988. (void*)(s));
  2989. /* Only one vote for this one. */
  2990. ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
  2991. /* Only two matching entries for this one, but 3 voters */
  2992. ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
  2993. ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  2994. ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
  2995. /* Only two matching entries for this one, but at least 4 voters */
  2996. ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
  2997. ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  2998. ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
  2999. ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
  3000. /* This one has only invalid votes. */
  3001. ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
  3002. ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
  3003. ADD(2, "haffenreffer 1.2 ");
  3004. ADD(3, "haffenreffer ");
  3005. ADD(4, "haffenreffer");
  3006. /* Three matching votes for this; it should actually go in! */
  3007. ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3008. ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3009. ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
  3010. ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
  3011. ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
  3012. /* Three votes for A, three votes for B */
  3013. ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3014. ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3015. ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
  3016. ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3017. ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3018. ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
  3019. /* Three votes for A, two votes for B */
  3020. ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  3021. ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
  3022. ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3023. ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3024. ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
  3025. /* Four votes for A, two for B. */
  3026. ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  3027. ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
  3028. ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3029. ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3030. ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3031. ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
  3032. /* Five votes for A ... all from the same authority. Three for B. */
  3033. ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3034. ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3035. ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3036. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3037. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3038. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3039. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3040. ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
  3041. /* As above but new replaces old: no two match. */
  3042. ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3043. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3044. ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
  3045. ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
  3046. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3047. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3048. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3049. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3050. ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
  3051. res = compute_consensus_package_lines(votes);
  3052. tt_assert(res);
  3053. tt_str_op(res, ==,
  3054. "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
  3055. "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
  3056. "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
  3057. "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
  3058. "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
  3059. );
  3060. #undef ADD
  3061. #undef BAD
  3062. #undef GOOD
  3063. done:
  3064. SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
  3065. { smartlist_free(ns->package_lines); tor_free(ns); });
  3066. smartlist_free(votes);
  3067. tor_free(res);
  3068. }
  3069. static void
  3070. test_dir_download_status_schedule(void *arg)
  3071. {
  3072. (void)arg;
  3073. download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
  3074. DL_WANT_AUTHORITY,
  3075. DL_SCHED_INCREMENT_FAILURE,
  3076. DL_SCHED_DETERMINISTIC, 0, 0 };
  3077. download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_CONSENSUS,
  3078. DL_WANT_ANY_DIRSERVER,
  3079. DL_SCHED_INCREMENT_ATTEMPT,
  3080. DL_SCHED_DETERMINISTIC, 0, 0 };
  3081. download_status_t dls_bridge = { 0, 0, 0, DL_SCHED_BRIDGE,
  3082. DL_WANT_AUTHORITY,
  3083. DL_SCHED_INCREMENT_FAILURE,
  3084. DL_SCHED_DETERMINISTIC, 0, 0 };
  3085. int increment = -1;
  3086. int expected_increment = -1;
  3087. time_t current_time = time(NULL);
  3088. int delay1 = -1;
  3089. int delay2 = -1;
  3090. smartlist_t *schedule = smartlist_new();
  3091. /* Make a dummy schedule */
  3092. smartlist_add(schedule, (void *)&delay1);
  3093. smartlist_add(schedule, (void *)&delay2);
  3094. /* check a range of values */
  3095. delay1 = 1000;
  3096. increment = download_status_schedule_get_delay(&dls_failure,
  3097. schedule,
  3098. 0, INT_MAX,
  3099. TIME_MIN);
  3100. expected_increment = delay1;
  3101. tt_assert(increment == expected_increment);
  3102. tt_assert(dls_failure.next_attempt_at == TIME_MIN + expected_increment);
  3103. delay1 = INT_MAX;
  3104. increment = download_status_schedule_get_delay(&dls_failure,
  3105. schedule,
  3106. 0, INT_MAX,
  3107. -1);
  3108. expected_increment = delay1;
  3109. tt_assert(increment == expected_increment);
  3110. tt_assert(dls_failure.next_attempt_at == TIME_MAX);
  3111. delay1 = 0;
  3112. increment = download_status_schedule_get_delay(&dls_attempt,
  3113. schedule,
  3114. 0, INT_MAX,
  3115. 0);
  3116. expected_increment = delay1;
  3117. tt_assert(increment == expected_increment);
  3118. tt_assert(dls_attempt.next_attempt_at == 0 + expected_increment);
  3119. delay1 = 1000;
  3120. increment = download_status_schedule_get_delay(&dls_attempt,
  3121. schedule,
  3122. 0, INT_MAX,
  3123. 1);
  3124. expected_increment = delay1;
  3125. tt_assert(increment == expected_increment);
  3126. tt_assert(dls_attempt.next_attempt_at == 1 + expected_increment);
  3127. delay1 = INT_MAX;
  3128. increment = download_status_schedule_get_delay(&dls_bridge,
  3129. schedule,
  3130. 0, INT_MAX,
  3131. current_time);
  3132. expected_increment = delay1;
  3133. tt_assert(increment == expected_increment);
  3134. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  3135. delay1 = 1;
  3136. increment = download_status_schedule_get_delay(&dls_bridge,
  3137. schedule,
  3138. 0, INT_MAX,
  3139. TIME_MAX);
  3140. expected_increment = delay1;
  3141. tt_assert(increment == expected_increment);
  3142. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  3143. /* see what happens when we reach the end */
  3144. dls_attempt.n_download_attempts++;
  3145. dls_bridge.n_download_failures++;
  3146. delay2 = 100;
  3147. increment = download_status_schedule_get_delay(&dls_attempt,
  3148. schedule,
  3149. 0, INT_MAX,
  3150. current_time);
  3151. expected_increment = delay2;
  3152. tt_assert(increment == expected_increment);
  3153. tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
  3154. delay2 = 1;
  3155. increment = download_status_schedule_get_delay(&dls_bridge,
  3156. schedule,
  3157. 0, INT_MAX,
  3158. current_time);
  3159. expected_increment = delay2;
  3160. tt_assert(increment == expected_increment);
  3161. tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
  3162. /* see what happens when we try to go off the end */
  3163. dls_attempt.n_download_attempts++;
  3164. dls_bridge.n_download_failures++;
  3165. delay2 = 5;
  3166. increment = download_status_schedule_get_delay(&dls_attempt,
  3167. schedule,
  3168. 0, INT_MAX,
  3169. current_time);
  3170. expected_increment = delay2;
  3171. tt_assert(increment == expected_increment);
  3172. tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
  3173. delay2 = 17;
  3174. increment = download_status_schedule_get_delay(&dls_bridge,
  3175. schedule,
  3176. 0, INT_MAX,
  3177. current_time);
  3178. expected_increment = delay2;
  3179. tt_assert(increment == expected_increment);
  3180. tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
  3181. /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
  3182. dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  3183. dls_bridge.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  3184. delay2 = 35;
  3185. increment = download_status_schedule_get_delay(&dls_attempt,
  3186. schedule,
  3187. 0, INT_MAX,
  3188. current_time);
  3189. expected_increment = INT_MAX;
  3190. tt_assert(increment == expected_increment);
  3191. tt_assert(dls_attempt.next_attempt_at == TIME_MAX);
  3192. delay2 = 99;
  3193. increment = download_status_schedule_get_delay(&dls_bridge,
  3194. schedule,
  3195. 0, INT_MAX,
  3196. current_time);
  3197. expected_increment = INT_MAX;
  3198. tt_assert(increment == expected_increment);
  3199. tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
  3200. done:
  3201. /* the pointers in schedule are allocated on the stack */
  3202. smartlist_free(schedule);
  3203. }
  3204. static void
  3205. test_dir_download_status_random_backoff(void *arg)
  3206. {
  3207. download_status_t dls_random =
  3208. { 0, 0, 0, DL_SCHED_GENERIC, DL_WANT_AUTHORITY,
  3209. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
  3210. int increment = -1;
  3211. int old_increment;
  3212. time_t current_time = time(NULL);
  3213. const int min_delay = 0;
  3214. const int max_delay = 1000000;
  3215. (void)arg;
  3216. /* Check the random backoff cases */
  3217. old_increment = 0;
  3218. int n_attempts = 0;
  3219. do {
  3220. increment = download_status_schedule_get_delay(&dls_random,
  3221. NULL,
  3222. min_delay, max_delay,
  3223. current_time);
  3224. /* Test */
  3225. tt_int_op(increment, OP_GE, min_delay);
  3226. tt_int_op(increment, OP_LE, max_delay);
  3227. if (old_increment)
  3228. tt_int_op(increment, OP_LE, old_increment * 3);
  3229. /* Advance */
  3230. current_time += increment;
  3231. ++(dls_random.n_download_attempts);
  3232. ++(dls_random.n_download_failures);
  3233. /* Try another maybe */
  3234. old_increment = increment;
  3235. } while (increment < max_delay && ++n_attempts < 1000);
  3236. done:
  3237. return;
  3238. }
  3239. static void
  3240. test_dir_download_status_random_backoff_ranges(void *arg)
  3241. {
  3242. (void)arg;
  3243. int lo, hi;
  3244. next_random_exponential_delay_range(&lo, &hi, 0, 10);
  3245. tt_int_op(lo, OP_EQ, 10);
  3246. tt_int_op(hi, OP_EQ, 11);
  3247. next_random_exponential_delay_range(&lo, &hi, 6, 10);
  3248. tt_int_op(lo, OP_EQ, 10);
  3249. tt_int_op(hi, OP_EQ, 6*3);
  3250. next_random_exponential_delay_range(&lo, &hi, 13, 10);
  3251. tt_int_op(lo, OP_EQ, 10);
  3252. tt_int_op(hi, OP_EQ, 13 * 3);
  3253. next_random_exponential_delay_range(&lo, &hi, 37, 10);
  3254. tt_int_op(lo, OP_EQ, 10);
  3255. tt_int_op(hi, OP_EQ, 111);
  3256. next_random_exponential_delay_range(&lo, &hi, 123, 10);
  3257. tt_int_op(lo, OP_EQ, 10);
  3258. tt_int_op(hi, OP_EQ, 369);
  3259. next_random_exponential_delay_range(&lo, &hi, INT_MAX-5, 10);
  3260. tt_int_op(lo, OP_EQ, 10);
  3261. tt_int_op(hi, OP_EQ, INT_MAX);
  3262. done:
  3263. ;
  3264. }
  3265. static void
  3266. test_dir_download_status_increment(void *arg)
  3267. {
  3268. (void)arg;
  3269. download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
  3270. DL_WANT_AUTHORITY,
  3271. DL_SCHED_INCREMENT_FAILURE,
  3272. DL_SCHED_DETERMINISTIC, 0, 0 };
  3273. download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_BRIDGE,
  3274. DL_WANT_ANY_DIRSERVER,
  3275. DL_SCHED_INCREMENT_ATTEMPT,
  3276. DL_SCHED_DETERMINISTIC, 0, 0 };
  3277. int delay0 = -1;
  3278. int delay1 = -1;
  3279. int delay2 = -1;
  3280. smartlist_t *schedule = smartlist_new();
  3281. or_options_t test_options;
  3282. time_t next_at = TIME_MAX;
  3283. time_t current_time = time(NULL);
  3284. /* Provide some values for the schedule */
  3285. delay0 = 10;
  3286. delay1 = 99;
  3287. delay2 = 20;
  3288. /* Make the schedule */
  3289. smartlist_add(schedule, (void *)&delay0);
  3290. smartlist_add(schedule, (void *)&delay1);
  3291. smartlist_add(schedule, (void *)&delay2);
  3292. /* Put it in the options */
  3293. mock_options = &test_options;
  3294. reset_options(mock_options, &mock_get_options_calls);
  3295. mock_options->TestingClientDownloadSchedule = schedule;
  3296. mock_options->TestingBridgeDownloadSchedule = schedule;
  3297. MOCK(get_options, mock_get_options);
  3298. /* Check that a failure reset works */
  3299. mock_get_options_calls = 0;
  3300. download_status_reset(&dls_failure);
  3301. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3302. * an unrealiable test, because time(NULL) might change. */
  3303. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3304. >= current_time + delay0);
  3305. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3306. != TIME_MAX);
  3307. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3308. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3309. tt_assert(mock_get_options_calls >= 1);
  3310. /* avoid timing inconsistencies */
  3311. dls_failure.next_attempt_at = current_time + delay0;
  3312. /* check that a reset schedule becomes ready at the right time */
  3313. tt_assert(download_status_is_ready(&dls_failure,
  3314. current_time + delay0 - 1,
  3315. 1) == 0);
  3316. tt_assert(download_status_is_ready(&dls_failure,
  3317. current_time + delay0,
  3318. 1) == 1);
  3319. tt_assert(download_status_is_ready(&dls_failure,
  3320. current_time + delay0 + 1,
  3321. 1) == 1);
  3322. /* Check that a failure increment works */
  3323. mock_get_options_calls = 0;
  3324. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3325. current_time);
  3326. tt_assert(next_at == current_time + delay1);
  3327. tt_assert(download_status_get_n_failures(&dls_failure) == 1);
  3328. tt_assert(download_status_get_n_attempts(&dls_failure) == 1);
  3329. tt_assert(mock_get_options_calls >= 1);
  3330. /* check that an incremented schedule becomes ready at the right time */
  3331. tt_assert(download_status_is_ready(&dls_failure,
  3332. current_time + delay1 - 1,
  3333. 1) == 0);
  3334. tt_assert(download_status_is_ready(&dls_failure,
  3335. current_time + delay1,
  3336. 1) == 1);
  3337. tt_assert(download_status_is_ready(&dls_failure,
  3338. current_time + delay1 + 1,
  3339. 1) == 1);
  3340. /* check that a schedule isn't ready if it's had too many failures */
  3341. tt_assert(download_status_is_ready(&dls_failure,
  3342. current_time + delay1 + 10,
  3343. 0) == 0);
  3344. /* Check that failure increments do happen on 503 for clients, and
  3345. * attempt increments do too. */
  3346. mock_get_options_calls = 0;
  3347. next_at = download_status_increment_failure(&dls_failure, 503, "test", 0,
  3348. current_time);
  3349. tt_i64_op(next_at, ==, current_time + delay2);
  3350. tt_int_op(download_status_get_n_failures(&dls_failure), ==, 2);
  3351. tt_int_op(download_status_get_n_attempts(&dls_failure), ==, 2);
  3352. tt_assert(mock_get_options_calls >= 1);
  3353. /* Check that failure increments do happen on 503 for servers */
  3354. mock_get_options_calls = 0;
  3355. next_at = download_status_increment_failure(&dls_failure, 503, "test", 1,
  3356. current_time);
  3357. tt_assert(next_at == current_time + delay2);
  3358. tt_assert(download_status_get_n_failures(&dls_failure) == 3);
  3359. tt_assert(download_status_get_n_attempts(&dls_failure) == 3);
  3360. tt_assert(mock_get_options_calls >= 1);
  3361. /* Check what happens when we run off the end of the schedule */
  3362. mock_get_options_calls = 0;
  3363. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3364. current_time);
  3365. tt_assert(next_at == current_time + delay2);
  3366. tt_assert(download_status_get_n_failures(&dls_failure) == 4);
  3367. tt_assert(download_status_get_n_attempts(&dls_failure) == 4);
  3368. tt_assert(mock_get_options_calls >= 1);
  3369. /* Check what happens when we hit the failure limit */
  3370. mock_get_options_calls = 0;
  3371. download_status_mark_impossible(&dls_failure);
  3372. next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
  3373. current_time);
  3374. tt_assert(next_at == TIME_MAX);
  3375. tt_assert(download_status_get_n_failures(&dls_failure)
  3376. == IMPOSSIBLE_TO_DOWNLOAD);
  3377. tt_assert(download_status_get_n_attempts(&dls_failure)
  3378. == IMPOSSIBLE_TO_DOWNLOAD);
  3379. tt_assert(mock_get_options_calls >= 1);
  3380. /* Check that a failure reset doesn't reset at the limit */
  3381. mock_get_options_calls = 0;
  3382. download_status_reset(&dls_failure);
  3383. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3384. == TIME_MAX);
  3385. tt_assert(download_status_get_n_failures(&dls_failure)
  3386. == IMPOSSIBLE_TO_DOWNLOAD);
  3387. tt_assert(download_status_get_n_attempts(&dls_failure)
  3388. == IMPOSSIBLE_TO_DOWNLOAD);
  3389. tt_assert(mock_get_options_calls == 0);
  3390. /* Check that a failure reset resets just before the limit */
  3391. mock_get_options_calls = 0;
  3392. dls_failure.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3393. dls_failure.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3394. download_status_reset(&dls_failure);
  3395. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3396. * an unrealiable test, because time(NULL) might change. */
  3397. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3398. >= current_time + delay0);
  3399. tt_assert(download_status_get_next_attempt_at(&dls_failure)
  3400. != TIME_MAX);
  3401. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3402. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3403. tt_assert(mock_get_options_calls >= 1);
  3404. /* Check that failure increments do happen on attempt-based schedules,
  3405. * but that the retry is set at the end of time */
  3406. mock_get_options_calls = 0;
  3407. next_at = download_status_increment_failure(&dls_attempt, 404, "test", 0,
  3408. current_time);
  3409. tt_assert(next_at == TIME_MAX);
  3410. tt_assert(download_status_get_n_failures(&dls_attempt) == 1);
  3411. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3412. tt_assert(mock_get_options_calls == 0);
  3413. /* Check that an attempt reset works */
  3414. mock_get_options_calls = 0;
  3415. download_status_reset(&dls_attempt);
  3416. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3417. * an unrealiable test, because time(NULL) might change. */
  3418. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3419. >= current_time + delay0);
  3420. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3421. != TIME_MAX);
  3422. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3423. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3424. tt_assert(mock_get_options_calls >= 1);
  3425. /* avoid timing inconsistencies */
  3426. dls_attempt.next_attempt_at = current_time + delay0;
  3427. /* check that a reset schedule becomes ready at the right time */
  3428. tt_assert(download_status_is_ready(&dls_attempt,
  3429. current_time + delay0 - 1,
  3430. 1) == 0);
  3431. tt_assert(download_status_is_ready(&dls_attempt,
  3432. current_time + delay0,
  3433. 1) == 1);
  3434. tt_assert(download_status_is_ready(&dls_attempt,
  3435. current_time + delay0 + 1,
  3436. 1) == 1);
  3437. /* Check that an attempt increment works */
  3438. mock_get_options_calls = 0;
  3439. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3440. current_time);
  3441. tt_assert(next_at == current_time + delay1);
  3442. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3443. tt_assert(download_status_get_n_attempts(&dls_attempt) == 1);
  3444. tt_assert(mock_get_options_calls >= 1);
  3445. /* check that an incremented schedule becomes ready at the right time */
  3446. tt_assert(download_status_is_ready(&dls_attempt,
  3447. current_time + delay1 - 1,
  3448. 1) == 0);
  3449. tt_assert(download_status_is_ready(&dls_attempt,
  3450. current_time + delay1,
  3451. 1) == 1);
  3452. tt_assert(download_status_is_ready(&dls_attempt,
  3453. current_time + delay1 + 1,
  3454. 1) == 1);
  3455. /* check that a schedule isn't ready if it's had too many attempts */
  3456. tt_assert(download_status_is_ready(&dls_attempt,
  3457. current_time + delay1 + 10,
  3458. 0) == 0);
  3459. /* Check what happens when we reach then run off the end of the schedule */
  3460. mock_get_options_calls = 0;
  3461. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3462. current_time);
  3463. tt_assert(next_at == current_time + delay2);
  3464. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3465. tt_assert(download_status_get_n_attempts(&dls_attempt) == 2);
  3466. tt_assert(mock_get_options_calls >= 1);
  3467. mock_get_options_calls = 0;
  3468. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3469. current_time);
  3470. tt_assert(next_at == current_time + delay2);
  3471. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3472. tt_assert(download_status_get_n_attempts(&dls_attempt) == 3);
  3473. tt_assert(mock_get_options_calls >= 1);
  3474. /* Check what happens when we hit the attempt limit */
  3475. mock_get_options_calls = 0;
  3476. download_status_mark_impossible(&dls_attempt);
  3477. next_at = download_status_increment_attempt(&dls_attempt, "test",
  3478. current_time);
  3479. tt_assert(next_at == TIME_MAX);
  3480. tt_assert(download_status_get_n_failures(&dls_attempt)
  3481. == IMPOSSIBLE_TO_DOWNLOAD);
  3482. tt_assert(download_status_get_n_attempts(&dls_attempt)
  3483. == IMPOSSIBLE_TO_DOWNLOAD);
  3484. tt_assert(mock_get_options_calls >= 1);
  3485. /* Check that an attempt reset doesn't reset at the limit */
  3486. mock_get_options_calls = 0;
  3487. download_status_reset(&dls_attempt);
  3488. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3489. == TIME_MAX);
  3490. tt_assert(download_status_get_n_failures(&dls_attempt)
  3491. == IMPOSSIBLE_TO_DOWNLOAD);
  3492. tt_assert(download_status_get_n_attempts(&dls_attempt)
  3493. == IMPOSSIBLE_TO_DOWNLOAD);
  3494. tt_assert(mock_get_options_calls == 0);
  3495. /* Check that an attempt reset resets just before the limit */
  3496. mock_get_options_calls = 0;
  3497. dls_attempt.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3498. dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
  3499. download_status_reset(&dls_attempt);
  3500. /* we really want to test that it's equal to time(NULL) + delay0, but that's
  3501. * an unrealiable test, because time(NULL) might change. */
  3502. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3503. >= current_time + delay0);
  3504. tt_assert(download_status_get_next_attempt_at(&dls_attempt)
  3505. != TIME_MAX);
  3506. tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
  3507. tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
  3508. tt_assert(mock_get_options_calls >= 1);
  3509. /* Check that attempt increments don't happen on failure-based schedules,
  3510. * and that the attempt is set at the end of time */
  3511. mock_get_options_calls = 0;
  3512. setup_full_capture_of_logs(LOG_WARN);
  3513. next_at = download_status_increment_attempt(&dls_failure, "test",
  3514. current_time);
  3515. expect_single_log_msg_containing(
  3516. "Tried to launch an attempt-based connection on a failure-based "
  3517. "schedule.");
  3518. teardown_capture_of_logs();
  3519. tt_assert(next_at == TIME_MAX);
  3520. tt_assert(download_status_get_n_failures(&dls_failure) == 0);
  3521. tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
  3522. tt_assert(mock_get_options_calls == 0);
  3523. done:
  3524. /* the pointers in schedule are allocated on the stack */
  3525. smartlist_free(schedule);
  3526. UNMOCK(get_options);
  3527. mock_options = NULL;
  3528. mock_get_options_calls = 0;
  3529. teardown_capture_of_logs();
  3530. }
  3531. static void
  3532. test_dir_authdir_type_to_string(void *data)
  3533. {
  3534. (void)data;
  3535. char *res;
  3536. tt_str_op(res = authdir_type_to_string(NO_DIRINFO), OP_EQ,
  3537. "[Not an authority]");
  3538. tor_free(res);
  3539. tt_str_op(res = authdir_type_to_string(EXTRAINFO_DIRINFO), OP_EQ,
  3540. "[Not an authority]");
  3541. tor_free(res);
  3542. tt_str_op(res = authdir_type_to_string(MICRODESC_DIRINFO), OP_EQ,
  3543. "[Not an authority]");
  3544. tor_free(res);
  3545. tt_str_op(res = authdir_type_to_string(V3_DIRINFO), OP_EQ, "V3");
  3546. tor_free(res);
  3547. tt_str_op(res = authdir_type_to_string(BRIDGE_DIRINFO), OP_EQ, "Bridge");
  3548. tor_free(res);
  3549. tt_str_op(res = authdir_type_to_string(
  3550. V3_DIRINFO | BRIDGE_DIRINFO | EXTRAINFO_DIRINFO), OP_EQ,
  3551. "V3, Bridge");
  3552. done:
  3553. tor_free(res);
  3554. }
  3555. static void
  3556. test_dir_conn_purpose_to_string(void *data)
  3557. {
  3558. (void)data;
  3559. #define EXPECT_CONN_PURPOSE(purpose, expected) \
  3560. tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
  3561. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR, "server descriptor upload");
  3562. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE, "server vote upload");
  3563. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES,
  3564. "consensus signature upload");
  3565. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC, "server descriptor fetch");
  3566. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO, "extra-info fetch");
  3567. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS,
  3568. "consensus network-status fetch");
  3569. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE, "authority cert fetch");
  3570. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE, "status vote fetch");
  3571. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  3572. "consensus signature fetch");
  3573. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2,
  3574. "hidden-service v2 descriptor fetch");
  3575. EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
  3576. "hidden-service v2 descriptor upload");
  3577. EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
  3578. /* This will give a warning, because there is no purpose 1024. */
  3579. setup_full_capture_of_logs(LOG_WARN);
  3580. EXPECT_CONN_PURPOSE(1024, "(unknown)");
  3581. expect_single_log_msg_containing("Called with unknown purpose 1024");
  3582. done:
  3583. teardown_capture_of_logs();
  3584. }
  3585. NS_DECL(int,
  3586. public_server_mode, (const or_options_t *options));
  3587. static int
  3588. NS(public_server_mode)(const or_options_t *options)
  3589. {
  3590. (void)options;
  3591. if (CALLED(public_server_mode)++ == 0) {
  3592. return 1;
  3593. }
  3594. return 0;
  3595. }
  3596. static void
  3597. test_dir_should_use_directory_guards(void *data)
  3598. {
  3599. or_options_t *options;
  3600. char *errmsg = NULL;
  3601. (void)data;
  3602. NS_MOCK(public_server_mode);
  3603. options = options_new();
  3604. options_init(options);
  3605. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3606. tt_int_op(CALLED(public_server_mode), OP_EQ, 1);
  3607. options->UseEntryGuardsAsDirGuards = 1;
  3608. options->UseEntryGuards = 1;
  3609. options->DownloadExtraInfo = 0;
  3610. options->FetchDirInfoEarly = 0;
  3611. options->FetchDirInfoExtraEarly = 0;
  3612. options->FetchUselessDescriptors = 0;
  3613. tt_int_op(should_use_directory_guards(options), OP_EQ, 1);
  3614. tt_int_op(CALLED(public_server_mode), OP_EQ, 2);
  3615. options->UseEntryGuards = 0;
  3616. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3617. tt_int_op(CALLED(public_server_mode), OP_EQ, 3);
  3618. options->UseEntryGuards = 1;
  3619. options->UseEntryGuardsAsDirGuards = 0;
  3620. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3621. tt_int_op(CALLED(public_server_mode), OP_EQ, 4);
  3622. options->UseEntryGuardsAsDirGuards = 1;
  3623. options->DownloadExtraInfo = 1;
  3624. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3625. tt_int_op(CALLED(public_server_mode), OP_EQ, 5);
  3626. options->DownloadExtraInfo = 0;
  3627. options->FetchDirInfoEarly = 1;
  3628. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3629. tt_int_op(CALLED(public_server_mode), OP_EQ, 6);
  3630. options->FetchDirInfoEarly = 0;
  3631. options->FetchDirInfoExtraEarly = 1;
  3632. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3633. tt_int_op(CALLED(public_server_mode), OP_EQ, 7);
  3634. options->FetchDirInfoExtraEarly = 0;
  3635. options->FetchUselessDescriptors = 1;
  3636. tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
  3637. tt_int_op(CALLED(public_server_mode), OP_EQ, 8);
  3638. options->FetchUselessDescriptors = 0;
  3639. done:
  3640. NS_UNMOCK(public_server_mode);
  3641. or_options_free(options);
  3642. tor_free(errmsg);
  3643. }
  3644. NS_DECL(void,
  3645. directory_initiate_command_routerstatus, (const routerstatus_t *status,
  3646. uint8_t dir_purpose,
  3647. uint8_t router_purpose,
  3648. dir_indirection_t indirection,
  3649. const char *resource,
  3650. const char *payload,
  3651. size_t payload_len,
  3652. time_t if_modified_since));
  3653. static void
  3654. test_dir_should_not_init_request_to_ourselves(void *data)
  3655. {
  3656. char digest[DIGEST_LEN];
  3657. dir_server_t *ourself = NULL;
  3658. crypto_pk_t *key = pk_generate(2);
  3659. (void) data;
  3660. NS_MOCK(directory_initiate_command_routerstatus);
  3661. clear_dir_servers();
  3662. routerlist_free_all();
  3663. set_server_identity_key(key);
  3664. crypto_pk_get_digest(key, (char*) &digest);
  3665. ourself = trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
  3666. NULL, digest,
  3667. NULL, V3_DIRINFO, 1.0);
  3668. tt_assert(ourself);
  3669. dir_server_add(ourself);
  3670. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  3671. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
  3672. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  3673. NULL);
  3674. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
  3675. done:
  3676. NS_UNMOCK(directory_initiate_command_routerstatus);
  3677. clear_dir_servers();
  3678. routerlist_free_all();
  3679. crypto_pk_free(key);
  3680. }
  3681. static void
  3682. test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data)
  3683. {
  3684. dir_server_t *ds = NULL;
  3685. dirinfo_type_t dirinfo_type = BRIDGE_DIRINFO | EXTRAINFO_DIRINFO \
  3686. | MICRODESC_DIRINFO;
  3687. (void) data;
  3688. NS_MOCK(directory_initiate_command_routerstatus);
  3689. clear_dir_servers();
  3690. routerlist_free_all();
  3691. ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
  3692. "12345678901234567890", NULL, dirinfo_type, 1.0);
  3693. tt_assert(ds);
  3694. dir_server_add(ds);
  3695. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  3696. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
  3697. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  3698. NULL);
  3699. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
  3700. done:
  3701. NS_UNMOCK(directory_initiate_command_routerstatus);
  3702. clear_dir_servers();
  3703. routerlist_free_all();
  3704. }
  3705. static void
  3706. test_dir_should_init_request_to_dir_auths(void *data)
  3707. {
  3708. dir_server_t *ds = NULL;
  3709. (void) data;
  3710. NS_MOCK(directory_initiate_command_routerstatus);
  3711. clear_dir_servers();
  3712. routerlist_free_all();
  3713. ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
  3714. "12345678901234567890", NULL, V3_DIRINFO, 1.0);
  3715. tt_assert(ds);
  3716. dir_server_add(ds);
  3717. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
  3718. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 1);
  3719. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
  3720. NULL);
  3721. tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 2);
  3722. done:
  3723. NS_UNMOCK(directory_initiate_command_routerstatus);
  3724. clear_dir_servers();
  3725. routerlist_free_all();
  3726. }
  3727. void
  3728. NS(directory_initiate_command_routerstatus)(const routerstatus_t *status,
  3729. uint8_t dir_purpose,
  3730. uint8_t router_purpose,
  3731. dir_indirection_t indirection,
  3732. const char *resource,
  3733. const char *payload,
  3734. size_t payload_len,
  3735. time_t if_modified_since)
  3736. {
  3737. (void)status;
  3738. (void)dir_purpose;
  3739. (void)router_purpose;
  3740. (void)indirection;
  3741. (void)resource;
  3742. (void)payload;
  3743. (void)payload_len;
  3744. (void)if_modified_since;
  3745. CALLED(directory_initiate_command_routerstatus)++;
  3746. }
  3747. static void
  3748. test_dir_choose_compression_level(void* data)
  3749. {
  3750. (void)data;
  3751. /* It starts under_memory_pressure */
  3752. tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);
  3753. tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  3754. tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
  3755. tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
  3756. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
  3757. /* Reset under_memory_pressure timer */
  3758. cell_queues_check_size();
  3759. tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);
  3760. tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  3761. tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
  3762. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
  3763. tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
  3764. done: ;
  3765. }
  3766. /*
  3767. * Mock check_private_dir(), and always succeed - no need to actually
  3768. * look at or create anything on the filesystem.
  3769. */
  3770. static int
  3771. mock_check_private_dir(const char *dirname, cpd_check_t check,
  3772. const char *effective_user)
  3773. {
  3774. (void)dirname;
  3775. (void)check;
  3776. (void)effective_user;
  3777. return 0;
  3778. }
  3779. /*
  3780. * This really mocks options_get_datadir_fname2_suffix(), but for testing
  3781. * dump_desc(), we only care about get_datadir_fname(sub1), which is defined
  3782. * in config.h as:
  3783. *
  3784. * options_get_datadir_fname2_suffix(get_options(), sub1, NULL, NULL)
  3785. */
  3786. static char *
  3787. mock_get_datadir_fname(const or_options_t *options,
  3788. const char *sub1, const char *sub2,
  3789. const char *suffix)
  3790. {
  3791. char *rv = NULL;
  3792. /*
  3793. * Assert we were called like get_datadir_fname2() or get_datadir_fname(),
  3794. * since that's all we implement here.
  3795. */
  3796. tt_assert(options != NULL);
  3797. tt_assert(sub1 != NULL);
  3798. /*
  3799. * No particular assertions about sub2, since we could be in the
  3800. * get_datadir_fname() or get_datadir_fname2() case.
  3801. */
  3802. tt_assert(suffix == NULL);
  3803. /* Just duplicate the basename and return it for this mock */
  3804. if (sub2) {
  3805. /* If we have sub2, it's the basename, otherwise sub1 */
  3806. rv = tor_strdup(sub2);
  3807. } else {
  3808. rv = tor_strdup(sub1);
  3809. }
  3810. done:
  3811. return rv;
  3812. }
  3813. static char *last_unlinked_path = NULL;
  3814. static int unlinked_count = 0;
  3815. static void
  3816. mock_unlink_reset(void)
  3817. {
  3818. tor_free(last_unlinked_path);
  3819. unlinked_count = 0;
  3820. }
  3821. static int
  3822. mock_unlink(const char *path)
  3823. {
  3824. tt_assert(path != NULL);
  3825. tor_free(last_unlinked_path);
  3826. last_unlinked_path = tor_strdup(path);
  3827. ++unlinked_count;
  3828. done:
  3829. return 0;
  3830. }
  3831. static char *last_write_str_path = NULL;
  3832. static uint8_t last_write_str_hash[DIGEST256_LEN];
  3833. static int write_str_count = 0;
  3834. static void
  3835. mock_write_str_to_file_reset(void)
  3836. {
  3837. tor_free(last_write_str_path);
  3838. write_str_count = 0;
  3839. }
  3840. static int
  3841. mock_write_str_to_file(const char *path, const char *str, int bin)
  3842. {
  3843. size_t len;
  3844. uint8_t hash[DIGEST256_LEN];
  3845. (void)bin;
  3846. tt_assert(path != NULL);
  3847. tt_assert(str != NULL);
  3848. len = strlen(str);
  3849. crypto_digest256((char *)hash, str, len, DIGEST_SHA256);
  3850. tor_free(last_write_str_path);
  3851. last_write_str_path = tor_strdup(path);
  3852. memcpy(last_write_str_hash, hash, sizeof(last_write_str_hash));
  3853. ++write_str_count;
  3854. done:
  3855. return 0;
  3856. }
  3857. static void
  3858. test_dir_dump_unparseable_descriptors(void *data)
  3859. {
  3860. /*
  3861. * These bogus descriptors look nothing at all like real bogus descriptors
  3862. * we might see, but we're only testing dump_desc() here, not the parser.
  3863. */
  3864. const char *test_desc_type = "squamous";
  3865. /* strlen(test_desc_1) = 583 bytes */
  3866. const char *test_desc_1 =
  3867. "The most merciful thing in the world, I think, is the inability of the "
  3868. "human mind to correlate all its contents. We live on a placid island of"
  3869. " ignorance in the midst of black seas of infinity, and it was not meant"
  3870. " that we should voyage far. The sciences, each straining in its own dir"
  3871. "ection, have hitherto harmed us little; but some day the piecing togeth"
  3872. "er of dissociated knowledge will open up such terrifying vistas of real"
  3873. "ity, and of our frightful position therein, that we shall either go mad"
  3874. "from the revelation or flee from the light into the peace and safety of"
  3875. "a new dark age.";
  3876. uint8_t test_desc_1_hash[DIGEST256_LEN];
  3877. char test_desc_1_hash_str[HEX_DIGEST256_LEN+1];
  3878. /* strlen(test_desc_2) = 650 bytes */
  3879. const char *test_desc_2 =
  3880. "I think their predominant colour was a greyish-green, though they had w"
  3881. "hite bellies. They were mostly shiny and slippery, but the ridges of th"
  3882. "eir backs were scaly. Their forms vaguely suggested the anthropoid, whi"
  3883. "le their heads were the heads of fish, with prodigious bulging eyes tha"
  3884. "t never closed. At the sides of their necks were palpitating gills, and"
  3885. "their long paws were webbed. They hopped irregularly, sometimes on two "
  3886. "legs and sometimes on four. I was somehow glad that they had no more th"
  3887. "an four limbs. Their croaking, baying voices, clearly wed tar articulat"
  3888. "e speech, held all the dark shades of expression which their staring fa"
  3889. "ces lacked.";
  3890. uint8_t test_desc_2_hash[DIGEST256_LEN];
  3891. char test_desc_2_hash_str[HEX_DIGEST256_LEN+1];
  3892. /* strlen(test_desc_3) = 700 bytes */
  3893. const char *test_desc_3 =
  3894. "Without knowing what futurism is like, Johansen achieved something very"
  3895. "close to it when he spoke of the city; for instead of describing any de"
  3896. "finite structure or building, he dwells only on broad impressions of va"
  3897. "st angles and stone surfaces - surfaces too great to belong to anything"
  3898. "right or proper for this earth, and impious with horrible images and hi"
  3899. "eroglyphs. I mention his talk about angles because it suggests somethin"
  3900. "g Wilcox had told me of his awful dreams. He said that the geometry of "
  3901. "the dream-place he saw was abnormal, non-Euclidean, and loathsomely red"
  3902. "olent of spheres and dimensions apart from ours. Now an unlettered seam"
  3903. "an felt the same thing whilst gazing at the terrible reality.";
  3904. uint8_t test_desc_3_hash[DIGEST256_LEN];
  3905. char test_desc_3_hash_str[HEX_DIGEST256_LEN+1];
  3906. /* strlen(test_desc_3) = 604 bytes */
  3907. const char *test_desc_4 =
  3908. "So we glanced back simultaneously, it would appear; though no doubt the"
  3909. "incipient motion of one prompted the imitation of the other. As we did "
  3910. "so we flashed both torches full strength at the momentarily thinned mis"
  3911. "t; either from sheer primitive anxiety to see all we could, or in a les"
  3912. "s primitive but equally unconscious effort to dazzle the entity before "
  3913. "we dimmed our light and dodged among the penguins of the labyrinth cent"
  3914. "er ahead. Unhappy act! Not Orpheus himself, or Lot's wife, paid much mo"
  3915. "re dearly for a backward glance. And again came that shocking, wide-ran"
  3916. "ged piping - \"Tekeli-li! Tekeli-li!\"";
  3917. uint8_t test_desc_4_hash[DIGEST256_LEN];
  3918. char test_desc_4_hash_str[HEX_DIGEST256_LEN+1];
  3919. (void)data;
  3920. /*
  3921. * Set up options mock so we can force a tiny FIFO size and generate
  3922. * cleanups.
  3923. */
  3924. mock_options = tor_malloc(sizeof(or_options_t));
  3925. reset_options(mock_options, &mock_get_options_calls);
  3926. mock_options->MaxUnparseableDescSizeToLog = 1536;
  3927. MOCK(get_options, mock_get_options);
  3928. MOCK(check_private_dir, mock_check_private_dir);
  3929. MOCK(options_get_datadir_fname2_suffix,
  3930. mock_get_datadir_fname);
  3931. /*
  3932. * Set up unlink and write mocks
  3933. */
  3934. MOCK(tor_unlink, mock_unlink);
  3935. mock_unlink_reset();
  3936. MOCK(write_str_to_file, mock_write_str_to_file);
  3937. mock_write_str_to_file_reset();
  3938. /*
  3939. * Compute hashes we'll need to recognize which descriptor is which
  3940. */
  3941. crypto_digest256((char *)test_desc_1_hash, test_desc_1,
  3942. strlen(test_desc_1), DIGEST_SHA256);
  3943. base16_encode(test_desc_1_hash_str, sizeof(test_desc_1_hash_str),
  3944. (const char *)test_desc_1_hash,
  3945. sizeof(test_desc_1_hash));
  3946. crypto_digest256((char *)test_desc_2_hash, test_desc_2,
  3947. strlen(test_desc_2), DIGEST_SHA256);
  3948. base16_encode(test_desc_2_hash_str, sizeof(test_desc_2_hash_str),
  3949. (const char *)test_desc_2_hash,
  3950. sizeof(test_desc_2_hash));
  3951. crypto_digest256((char *)test_desc_3_hash, test_desc_3,
  3952. strlen(test_desc_3), DIGEST_SHA256);
  3953. base16_encode(test_desc_3_hash_str, sizeof(test_desc_3_hash_str),
  3954. (const char *)test_desc_3_hash,
  3955. sizeof(test_desc_3_hash));
  3956. crypto_digest256((char *)test_desc_4_hash, test_desc_4,
  3957. strlen(test_desc_4), DIGEST_SHA256);
  3958. base16_encode(test_desc_4_hash_str, sizeof(test_desc_4_hash_str),
  3959. (const char *)test_desc_4_hash,
  3960. sizeof(test_desc_4_hash));
  3961. /*
  3962. * Reset the FIFO and check its state
  3963. */
  3964. dump_desc_fifo_cleanup();
  3965. tt_u64_op(len_descs_dumped, ==, 0);
  3966. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  3967. /*
  3968. * (1) Fire off dump_desc() once; these descriptors should all be safely
  3969. * smaller than configured FIFO size.
  3970. */
  3971. dump_desc(test_desc_1, test_desc_type);
  3972. /*
  3973. * Assert things about the FIFO state
  3974. */
  3975. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1));
  3976. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  3977. /*
  3978. * Assert things about the mocks
  3979. */
  3980. tt_int_op(unlinked_count, ==, 0);
  3981. tt_int_op(write_str_count, ==, 1);
  3982. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  3983. /*
  3984. * Reset the FIFO and check its state
  3985. */
  3986. dump_desc_fifo_cleanup();
  3987. tt_u64_op(len_descs_dumped, ==, 0);
  3988. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  3989. /*
  3990. * Reset the mocks and check their state
  3991. */
  3992. mock_unlink_reset();
  3993. mock_write_str_to_file_reset();
  3994. tt_int_op(unlinked_count, ==, 0);
  3995. tt_int_op(write_str_count, ==, 0);
  3996. /*
  3997. * (2) Fire off dump_desc() twice; this still should trigger no cleanup.
  3998. */
  3999. /* First time */
  4000. dump_desc(test_desc_2, test_desc_type);
  4001. /*
  4002. * Assert things about the FIFO state
  4003. */
  4004. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2));
  4005. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4006. /*
  4007. * Assert things about the mocks
  4008. */
  4009. tt_int_op(unlinked_count, ==, 0);
  4010. tt_int_op(write_str_count, ==, 1);
  4011. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4012. /* Second time */
  4013. dump_desc(test_desc_3, test_desc_type);
  4014. /*
  4015. * Assert things about the FIFO state
  4016. */
  4017. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
  4018. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4019. /*
  4020. * Assert things about the mocks
  4021. */
  4022. tt_int_op(unlinked_count, ==, 0);
  4023. tt_int_op(write_str_count, ==, 2);
  4024. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4025. /*
  4026. * Reset the FIFO and check its state
  4027. */
  4028. dump_desc_fifo_cleanup();
  4029. tt_u64_op(len_descs_dumped, ==, 0);
  4030. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4031. /*
  4032. * Reset the mocks and check their state
  4033. */
  4034. mock_unlink_reset();
  4035. mock_write_str_to_file_reset();
  4036. tt_int_op(unlinked_count, ==, 0);
  4037. tt_int_op(write_str_count, ==, 0);
  4038. /*
  4039. * (3) Three calls to dump_desc cause a FIFO cleanup
  4040. */
  4041. /* First time */
  4042. dump_desc(test_desc_4, test_desc_type);
  4043. /*
  4044. * Assert things about the FIFO state
  4045. */
  4046. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4));
  4047. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4048. /*
  4049. * Assert things about the mocks
  4050. */
  4051. tt_int_op(unlinked_count, ==, 0);
  4052. tt_int_op(write_str_count, ==, 1);
  4053. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4054. /* Second time */
  4055. dump_desc(test_desc_1, test_desc_type);
  4056. /*
  4057. * Assert things about the FIFO state
  4058. */
  4059. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4) + strlen(test_desc_1));
  4060. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4061. /*
  4062. * Assert things about the mocks
  4063. */
  4064. tt_int_op(unlinked_count, ==, 0);
  4065. tt_int_op(write_str_count, ==, 2);
  4066. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4067. /* Third time - we should unlink the dump of test_desc_4 here */
  4068. dump_desc(test_desc_2, test_desc_type);
  4069. /*
  4070. * Assert things about the FIFO state
  4071. */
  4072. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
  4073. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4074. /*
  4075. * Assert things about the mocks
  4076. */
  4077. tt_int_op(unlinked_count, ==, 1);
  4078. tt_int_op(write_str_count, ==, 3);
  4079. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4080. /*
  4081. * Reset the FIFO and check its state
  4082. */
  4083. dump_desc_fifo_cleanup();
  4084. tt_u64_op(len_descs_dumped, ==, 0);
  4085. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4086. /*
  4087. * Reset the mocks and check their state
  4088. */
  4089. mock_unlink_reset();
  4090. mock_write_str_to_file_reset();
  4091. tt_int_op(unlinked_count, ==, 0);
  4092. tt_int_op(write_str_count, ==, 0);
  4093. /*
  4094. * (4) But repeating one (A B B) doesn't overflow and cleanup
  4095. */
  4096. /* First time */
  4097. dump_desc(test_desc_3, test_desc_type);
  4098. /*
  4099. * Assert things about the FIFO state
  4100. */
  4101. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3));
  4102. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4103. /*
  4104. * Assert things about the mocks
  4105. */
  4106. tt_int_op(unlinked_count, ==, 0);
  4107. tt_int_op(write_str_count, ==, 1);
  4108. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4109. /* Second time */
  4110. dump_desc(test_desc_4, test_desc_type);
  4111. /*
  4112. * Assert things about the FIFO state
  4113. */
  4114. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
  4115. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4116. /*
  4117. * Assert things about the mocks
  4118. */
  4119. tt_int_op(unlinked_count, ==, 0);
  4120. tt_int_op(write_str_count, ==, 2);
  4121. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4122. /* Third time */
  4123. dump_desc(test_desc_4, test_desc_type);
  4124. /*
  4125. * Assert things about the FIFO state
  4126. */
  4127. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
  4128. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4129. /*
  4130. * Assert things about the mocks
  4131. */
  4132. tt_int_op(unlinked_count, ==, 0);
  4133. tt_int_op(write_str_count, ==, 2);
  4134. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4135. /*
  4136. * Reset the FIFO and check its state
  4137. */
  4138. dump_desc_fifo_cleanup();
  4139. tt_u64_op(len_descs_dumped, ==, 0);
  4140. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4141. /*
  4142. * Reset the mocks and check their state
  4143. */
  4144. mock_unlink_reset();
  4145. mock_write_str_to_file_reset();
  4146. tt_int_op(unlinked_count, ==, 0);
  4147. tt_int_op(write_str_count, ==, 0);
  4148. /*
  4149. * (5) Same for the (A B A) repetition
  4150. */
  4151. /* First time */
  4152. dump_desc(test_desc_1, test_desc_type);
  4153. /*
  4154. * Assert things about the FIFO state
  4155. */
  4156. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1));
  4157. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4158. /*
  4159. * Assert things about the mocks
  4160. */
  4161. tt_int_op(unlinked_count, ==, 0);
  4162. tt_int_op(write_str_count, ==, 1);
  4163. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4164. /* Second time */
  4165. dump_desc(test_desc_2, test_desc_type);
  4166. /*
  4167. * Assert things about the FIFO state
  4168. */
  4169. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
  4170. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4171. /*
  4172. * Assert things about the mocks
  4173. */
  4174. tt_int_op(unlinked_count, ==, 0);
  4175. tt_int_op(write_str_count, ==, 2);
  4176. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4177. /* Third time */
  4178. dump_desc(test_desc_1, test_desc_type);
  4179. /*
  4180. * Assert things about the FIFO state
  4181. */
  4182. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
  4183. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4184. /*
  4185. * Assert things about the mocks
  4186. */
  4187. tt_int_op(unlinked_count, ==, 0);
  4188. tt_int_op(write_str_count, ==, 2);
  4189. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4190. /*
  4191. * Reset the FIFO and check its state
  4192. */
  4193. dump_desc_fifo_cleanup();
  4194. tt_u64_op(len_descs_dumped, ==, 0);
  4195. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4196. /*
  4197. * Reset the mocks and check their state
  4198. */
  4199. mock_unlink_reset();
  4200. mock_write_str_to_file_reset();
  4201. tt_int_op(unlinked_count, ==, 0);
  4202. tt_int_op(write_str_count, ==, 0);
  4203. /*
  4204. * (6) (A B B C) triggering overflow on C causes A, not B to be unlinked
  4205. */
  4206. /* First time */
  4207. dump_desc(test_desc_3, test_desc_type);
  4208. /*
  4209. * Assert things about the FIFO state
  4210. */
  4211. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3));
  4212. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4213. /*
  4214. * Assert things about the mocks
  4215. */
  4216. tt_int_op(unlinked_count, ==, 0);
  4217. tt_int_op(write_str_count, ==, 1);
  4218. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4219. /* Second time */
  4220. dump_desc(test_desc_4, test_desc_type);
  4221. /*
  4222. * Assert things about the FIFO state
  4223. */
  4224. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
  4225. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4226. /*
  4227. * Assert things about the mocks
  4228. */
  4229. tt_int_op(unlinked_count, ==, 0);
  4230. tt_int_op(write_str_count, ==, 2);
  4231. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4232. /* Third time */
  4233. dump_desc(test_desc_4, test_desc_type);
  4234. /*
  4235. * Assert things about the FIFO state
  4236. */
  4237. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
  4238. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4239. /*
  4240. * Assert things about the mocks
  4241. */
  4242. tt_int_op(unlinked_count, ==, 0);
  4243. tt_int_op(write_str_count, ==, 2);
  4244. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4245. /* Fourth time - we should unlink the dump of test_desc_3 here */
  4246. dump_desc(test_desc_1, test_desc_type);
  4247. /*
  4248. * Assert things about the FIFO state
  4249. */
  4250. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4) + strlen(test_desc_1));
  4251. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4252. /*
  4253. * Assert things about the mocks
  4254. */
  4255. tt_int_op(unlinked_count, ==, 1);
  4256. tt_int_op(write_str_count, ==, 3);
  4257. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
  4258. /*
  4259. * Reset the FIFO and check its state
  4260. */
  4261. dump_desc_fifo_cleanup();
  4262. tt_u64_op(len_descs_dumped, ==, 0);
  4263. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4264. /*
  4265. * Reset the mocks and check their state
  4266. */
  4267. mock_unlink_reset();
  4268. mock_write_str_to_file_reset();
  4269. tt_int_op(unlinked_count, ==, 0);
  4270. tt_int_op(write_str_count, ==, 0);
  4271. /*
  4272. * (7) (A B A C) triggering overflow on C causes B, not A to be unlinked
  4273. */
  4274. /* First time */
  4275. dump_desc(test_desc_2, test_desc_type);
  4276. /*
  4277. * Assert things about the FIFO state
  4278. */
  4279. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2));
  4280. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
  4281. /*
  4282. * Assert things about the mocks
  4283. */
  4284. tt_int_op(unlinked_count, ==, 0);
  4285. tt_int_op(write_str_count, ==, 1);
  4286. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
  4287. /* Second time */
  4288. dump_desc(test_desc_3, test_desc_type);
  4289. /*
  4290. * Assert things about the FIFO state
  4291. */
  4292. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
  4293. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4294. /*
  4295. * Assert things about the mocks
  4296. */
  4297. tt_int_op(unlinked_count, ==, 0);
  4298. tt_int_op(write_str_count, ==, 2);
  4299. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4300. /* Third time */
  4301. dump_desc(test_desc_2, test_desc_type);
  4302. /*
  4303. * Assert things about the FIFO state
  4304. */
  4305. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
  4306. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4307. /*
  4308. * Assert things about the mocks
  4309. */
  4310. tt_int_op(unlinked_count, ==, 0);
  4311. tt_int_op(write_str_count, ==, 2);
  4312. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
  4313. /* Fourth time - we should unlink the dump of test_desc_3 here */
  4314. dump_desc(test_desc_4, test_desc_type);
  4315. /*
  4316. * Assert things about the FIFO state
  4317. */
  4318. tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_4));
  4319. tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
  4320. /*
  4321. * Assert things about the mocks
  4322. */
  4323. tt_int_op(unlinked_count, ==, 1);
  4324. tt_int_op(write_str_count, ==, 3);
  4325. tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
  4326. /*
  4327. * Reset the FIFO and check its state
  4328. */
  4329. dump_desc_fifo_cleanup();
  4330. tt_u64_op(len_descs_dumped, ==, 0);
  4331. tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
  4332. /*
  4333. * Reset the mocks and check their state
  4334. */
  4335. mock_unlink_reset();
  4336. mock_write_str_to_file_reset();
  4337. tt_int_op(unlinked_count, ==, 0);
  4338. tt_int_op(write_str_count, ==, 0);
  4339. done:
  4340. /* Clean up the fifo */
  4341. dump_desc_fifo_cleanup();
  4342. /* Remove mocks */
  4343. UNMOCK(tor_unlink);
  4344. mock_unlink_reset();
  4345. UNMOCK(write_str_to_file);
  4346. mock_write_str_to_file_reset();
  4347. UNMOCK(options_get_datadir_fname2_suffix);
  4348. UNMOCK(check_private_dir);
  4349. UNMOCK(get_options);
  4350. tor_free(mock_options);
  4351. mock_options = NULL;
  4352. return;
  4353. }
  4354. /* Variables for reset_read_file_to_str_mock() */
  4355. static int enforce_expected_filename = 0;
  4356. static char *expected_filename = NULL;
  4357. static char *file_content = NULL;
  4358. static size_t file_content_len = 0;
  4359. static struct stat file_stat;
  4360. static int read_count = 0, read_call_count = 0;
  4361. static void
  4362. reset_read_file_to_str_mock(void)
  4363. {
  4364. tor_free(expected_filename);
  4365. tor_free(file_content);
  4366. file_content_len = 0;
  4367. memset(&file_stat, 0, sizeof(file_stat));
  4368. read_count = 0;
  4369. read_call_count = 0;
  4370. }
  4371. static char *
  4372. read_file_to_str_mock(const char *filename, int flags,
  4373. struct stat *stat_out) {
  4374. char *result = NULL;
  4375. /* Insist we got a filename */
  4376. tt_assert(filename != NULL);
  4377. /* We ignore flags */
  4378. (void)flags;
  4379. /* Bump the call count */
  4380. ++read_call_count;
  4381. if (enforce_expected_filename) {
  4382. tt_assert(expected_filename);
  4383. tt_str_op(filename, OP_EQ, expected_filename);
  4384. }
  4385. if (expected_filename != NULL &&
  4386. file_content != NULL &&
  4387. strcmp(filename, expected_filename) == 0) {
  4388. /* You asked for it, you got it */
  4389. /*
  4390. * This is the same behavior as the real read_file_to_str();
  4391. * if there's a NUL, the real size ends up in stat_out.
  4392. */
  4393. result = tor_malloc(file_content_len + 1);
  4394. if (file_content_len > 0) {
  4395. memcpy(result, file_content, file_content_len);
  4396. }
  4397. result[file_content_len] = '\0';
  4398. /* Do we need to set up stat_out? */
  4399. if (stat_out != NULL) {
  4400. memcpy(stat_out, &file_stat, sizeof(file_stat));
  4401. /* We always return the correct length here */
  4402. stat_out->st_size = file_content_len;
  4403. }
  4404. /* Wooo, we have a return value - bump the counter */
  4405. ++read_count;
  4406. }
  4407. /* else no match, return NULL */
  4408. done:
  4409. return result;
  4410. }
  4411. /* This one tests dump_desc_populate_one_file() */
  4412. static void
  4413. test_dir_populate_dump_desc_fifo(void *data)
  4414. {
  4415. const char *dirname = "foo";
  4416. const char *fname = NULL;
  4417. dumped_desc_t *ent;
  4418. (void)data;
  4419. /*
  4420. * Set up unlink and read_file_to_str mocks
  4421. */
  4422. MOCK(tor_unlink, mock_unlink);
  4423. mock_unlink_reset();
  4424. MOCK(read_file_to_str, read_file_to_str_mock);
  4425. reset_read_file_to_str_mock();
  4426. /* Check state of unlink mock */
  4427. tt_int_op(unlinked_count, ==, 0);
  4428. /* Some cases that should fail before trying to read the file */
  4429. ent = dump_desc_populate_one_file(dirname, "bar");
  4430. tt_assert(ent == NULL);
  4431. tt_int_op(unlinked_count, ==, 1);
  4432. tt_int_op(read_count, ==, 0);
  4433. tt_int_op(read_call_count, ==, 0);
  4434. ent = dump_desc_populate_one_file(dirname, "unparseable-desc");
  4435. tt_assert(ent == NULL);
  4436. tt_int_op(unlinked_count, ==, 2);
  4437. tt_int_op(read_count, ==, 0);
  4438. tt_int_op(read_call_count, ==, 0);
  4439. ent = dump_desc_populate_one_file(dirname, "unparseable-desc.baz");
  4440. tt_assert(ent == NULL);
  4441. tt_int_op(unlinked_count, ==, 3);
  4442. tt_int_op(read_count, ==, 0);
  4443. tt_int_op(read_call_count, ==, 0);
  4444. ent = dump_desc_populate_one_file(
  4445. dirname,
  4446. "unparseable-desc.08AE85E90461F59E");
  4447. tt_assert(ent == NULL);
  4448. tt_int_op(unlinked_count, ==, 4);
  4449. tt_int_op(read_count, ==, 0);
  4450. tt_int_op(read_call_count, ==, 0);
  4451. ent = dump_desc_populate_one_file(
  4452. dirname,
  4453. "unparseable-desc.08AE85E90461F59EDF0981323F3A70D02B55AB54B44B04F"
  4454. "287D72F7B72F242E85C8CB0EDA8854A99");
  4455. tt_assert(ent == NULL);
  4456. tt_int_op(unlinked_count, ==, 5);
  4457. tt_int_op(read_count, ==, 0);
  4458. tt_int_op(read_call_count, ==, 0);
  4459. /* This is a correct-length digest but base16_decode() will fail */
  4460. ent = dump_desc_populate_one_file(
  4461. dirname,
  4462. "unparseable-desc.68219B8BGE64B705A6FFC728C069DC596216D60A7D7520C"
  4463. "D5ECE250D912E686B");
  4464. tt_assert(ent == NULL);
  4465. tt_int_op(unlinked_count, ==, 6);
  4466. tt_int_op(read_count, ==, 0);
  4467. tt_int_op(read_call_count, ==, 0);
  4468. /* This one has a correctly formed filename and should try reading */
  4469. /* Read fails */
  4470. ent = dump_desc_populate_one_file(
  4471. dirname,
  4472. "unparseable-desc.DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E"
  4473. "85C8CB0EDA8854A99");
  4474. tt_assert(ent == NULL);
  4475. tt_int_op(unlinked_count, ==, 7);
  4476. tt_int_op(read_count, ==, 0);
  4477. tt_int_op(read_call_count, ==, 1);
  4478. /* This read will succeed but the digest won't match the file content */
  4479. fname =
  4480. "unparseable-desc."
  4481. "DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E85C8CB0EDA8854A99";
  4482. enforce_expected_filename = 1;
  4483. tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
  4484. file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
  4485. file_content_len = strlen(file_content);
  4486. file_stat.st_mtime = 123456;
  4487. ent = dump_desc_populate_one_file(dirname, fname);
  4488. enforce_expected_filename = 0;
  4489. tt_assert(ent == NULL);
  4490. tt_int_op(unlinked_count, ==, 8);
  4491. tt_int_op(read_count, ==, 1);
  4492. tt_int_op(read_call_count, ==, 2);
  4493. tor_free(expected_filename);
  4494. tor_free(file_content);
  4495. /* This one will match */
  4496. fname =
  4497. "unparseable-desc."
  4498. "0786C7173447B7FB033FFCA2FC47C3CF71C30DD47CA8236D3FC7FF35853271C6";
  4499. tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
  4500. file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
  4501. file_content_len = strlen(file_content);
  4502. file_stat.st_mtime = 789012;
  4503. ent = dump_desc_populate_one_file(dirname, fname);
  4504. tt_assert(ent != NULL);
  4505. tt_int_op(unlinked_count, ==, 8);
  4506. tt_int_op(read_count, ==, 2);
  4507. tt_int_op(read_call_count, ==, 3);
  4508. tt_str_op(ent->filename, OP_EQ, expected_filename);
  4509. tt_int_op(ent->len, ==, file_content_len);
  4510. tt_int_op(ent->when, ==, file_stat.st_mtime);
  4511. tor_free(ent->filename);
  4512. tor_free(ent);
  4513. tor_free(expected_filename);
  4514. /*
  4515. * Reset the mocks and check their state
  4516. */
  4517. mock_unlink_reset();
  4518. tt_int_op(unlinked_count, ==, 0);
  4519. reset_read_file_to_str_mock();
  4520. tt_int_op(read_count, ==, 0);
  4521. done:
  4522. UNMOCK(tor_unlink);
  4523. mock_unlink_reset();
  4524. UNMOCK(read_file_to_str);
  4525. reset_read_file_to_str_mock();
  4526. tor_free(file_content);
  4527. return;
  4528. }
  4529. static smartlist_t *
  4530. listdir_mock(const char *dname)
  4531. {
  4532. smartlist_t *l;
  4533. /* Ignore the name, always return this list */
  4534. (void)dname;
  4535. l = smartlist_new();
  4536. smartlist_add(l, tor_strdup("foo"));
  4537. smartlist_add(l, tor_strdup("bar"));
  4538. smartlist_add(l, tor_strdup("baz"));
  4539. return l;
  4540. }
  4541. static dumped_desc_t *
  4542. pop_one_mock(const char *dirname, const char *f)
  4543. {
  4544. dumped_desc_t *ent = NULL;
  4545. if (dirname != NULL && strcmp(dirname, "d") == 0) {
  4546. if (f != NULL && strcmp(f, "foo") == 0) {
  4547. ent = tor_malloc_zero(sizeof(*ent));
  4548. ent->filename = tor_strdup("d/foo");
  4549. ent->len = 123;
  4550. ent->digest_sha256[0] = 1;
  4551. ent->when = 1024;
  4552. } else if (f != NULL && strcmp(f, "bar") == 0) {
  4553. ent = tor_malloc_zero(sizeof(*ent));
  4554. ent->filename = tor_strdup("d/bar");
  4555. ent->len = 456;
  4556. ent->digest_sha256[0] = 2;
  4557. /*
  4558. * Note that the timestamps are in a different order than
  4559. * listdir_mock() returns; we're testing the sort order.
  4560. */
  4561. ent->when = 512;
  4562. } else if (f != NULL && strcmp(f, "baz") == 0) {
  4563. ent = tor_malloc_zero(sizeof(*ent));
  4564. ent->filename = tor_strdup("d/baz");
  4565. ent->len = 789;
  4566. ent->digest_sha256[0] = 3;
  4567. ent->when = 768;
  4568. }
  4569. }
  4570. return ent;
  4571. }
  4572. /* This one tests dump_desc_populate_fifo_from_directory() */
  4573. static void
  4574. test_dir_populate_dump_desc_fifo_2(void *data)
  4575. {
  4576. dumped_desc_t *ent = NULL;
  4577. (void)data;
  4578. /* Set up the mocks */
  4579. MOCK(tor_listdir, listdir_mock);
  4580. MOCK(dump_desc_populate_one_file, pop_one_mock);
  4581. /* Run dump_desc_populate_fifo_from_directory() */
  4582. descs_dumped = NULL;
  4583. len_descs_dumped = 0;
  4584. dump_desc_populate_fifo_from_directory("d");
  4585. tt_assert(descs_dumped != NULL);
  4586. tt_int_op(smartlist_len(descs_dumped), OP_EQ, 3);
  4587. tt_u64_op(len_descs_dumped, OP_EQ, 1368);
  4588. ent = smartlist_get(descs_dumped, 0);
  4589. tt_str_op(ent->filename, OP_EQ, "d/bar");
  4590. tt_int_op(ent->len, OP_EQ, 456);
  4591. tt_int_op(ent->when, OP_EQ, 512);
  4592. ent = smartlist_get(descs_dumped, 1);
  4593. tt_str_op(ent->filename, OP_EQ, "d/baz");
  4594. tt_int_op(ent->len, OP_EQ, 789);
  4595. tt_int_op(ent->when, OP_EQ, 768);
  4596. ent = smartlist_get(descs_dumped, 2);
  4597. tt_str_op(ent->filename, OP_EQ, "d/foo");
  4598. tt_int_op(ent->len, OP_EQ, 123);
  4599. tt_int_op(ent->when, OP_EQ, 1024);
  4600. done:
  4601. dump_desc_fifo_cleanup();
  4602. UNMOCK(dump_desc_populate_one_file);
  4603. UNMOCK(tor_listdir);
  4604. return;
  4605. }
  4606. static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
  4607. static int
  4608. mock_networkstatus_consensus_is_bootstrapping(time_t now)
  4609. {
  4610. (void)now;
  4611. return mock_networkstatus_consensus_is_bootstrapping_value;
  4612. }
  4613. static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
  4614. static int
  4615. mock_networkstatus_consensus_can_use_extra_fallbacks(
  4616. const or_options_t *options)
  4617. {
  4618. (void)options;
  4619. return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
  4620. }
  4621. /* data is a 2 character nul-terminated string.
  4622. * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
  4623. * If data[1] is 'f', set extra fallbacks, anything else means no extra
  4624. * fallbacks.
  4625. */
  4626. static void
  4627. test_dir_find_dl_schedule(void* data)
  4628. {
  4629. const char *str = (const char *)data;
  4630. tt_assert(strlen(data) == 2);
  4631. if (str[0] == 'b') {
  4632. mock_networkstatus_consensus_is_bootstrapping_value = 1;
  4633. } else {
  4634. mock_networkstatus_consensus_is_bootstrapping_value = 0;
  4635. }
  4636. if (str[1] == 'f') {
  4637. mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
  4638. } else {
  4639. mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
  4640. }
  4641. MOCK(networkstatus_consensus_is_bootstrapping,
  4642. mock_networkstatus_consensus_is_bootstrapping);
  4643. MOCK(networkstatus_consensus_can_use_extra_fallbacks,
  4644. mock_networkstatus_consensus_can_use_extra_fallbacks);
  4645. download_status_t dls;
  4646. smartlist_t server, client, server_cons, client_cons;
  4647. smartlist_t client_boot_auth_only_cons, client_boot_auth_cons;
  4648. smartlist_t client_boot_fallback_cons, bridge;
  4649. mock_options = tor_malloc(sizeof(or_options_t));
  4650. reset_options(mock_options, &mock_get_options_calls);
  4651. MOCK(get_options, mock_get_options);
  4652. mock_options->TestingServerDownloadSchedule = &server;
  4653. mock_options->TestingClientDownloadSchedule = &client;
  4654. mock_options->TestingServerConsensusDownloadSchedule = &server_cons;
  4655. mock_options->TestingClientConsensusDownloadSchedule = &client_cons;
  4656. mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule =
  4657. &client_boot_auth_only_cons;
  4658. mock_options->ClientBootstrapConsensusAuthorityDownloadSchedule =
  4659. &client_boot_auth_cons;
  4660. mock_options->ClientBootstrapConsensusFallbackDownloadSchedule =
  4661. &client_boot_fallback_cons;
  4662. mock_options->TestingBridgeDownloadSchedule = &bridge;
  4663. dls.schedule = DL_SCHED_GENERIC;
  4664. /* client */
  4665. mock_options->ClientOnly = 1;
  4666. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client);
  4667. mock_options->ClientOnly = 0;
  4668. /* dir mode */
  4669. mock_options->DirPort_set = 1;
  4670. mock_options->DirCache = 1;
  4671. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server);
  4672. mock_options->DirPort_set = 0;
  4673. mock_options->DirCache = 0;
  4674. dls.schedule = DL_SCHED_CONSENSUS;
  4675. /* public server mode */
  4676. mock_options->ORPort_set = 1;
  4677. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server_cons);
  4678. mock_options->ORPort_set = 0;
  4679. /* client and bridge modes */
  4680. if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
  4681. if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
  4682. dls.want_authority = 1;
  4683. /* client */
  4684. mock_options->ClientOnly = 1;
  4685. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4686. &client_boot_auth_cons);
  4687. mock_options->ClientOnly = 0;
  4688. /* bridge relay */
  4689. mock_options->ORPort_set = 1;
  4690. mock_options->BridgeRelay = 1;
  4691. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4692. &client_boot_auth_cons);
  4693. mock_options->ORPort_set = 0;
  4694. mock_options->BridgeRelay = 0;
  4695. dls.want_authority = 0;
  4696. /* client */
  4697. mock_options->ClientOnly = 1;
  4698. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4699. &client_boot_fallback_cons);
  4700. mock_options->ClientOnly = 0;
  4701. /* bridge relay */
  4702. mock_options->ORPort_set = 1;
  4703. mock_options->BridgeRelay = 1;
  4704. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4705. &client_boot_fallback_cons);
  4706. mock_options->ORPort_set = 0;
  4707. mock_options->BridgeRelay = 0;
  4708. } else {
  4709. /* dls.want_authority is ignored */
  4710. /* client */
  4711. mock_options->ClientOnly = 1;
  4712. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4713. &client_boot_auth_only_cons);
  4714. mock_options->ClientOnly = 0;
  4715. /* bridge relay */
  4716. mock_options->ORPort_set = 1;
  4717. mock_options->BridgeRelay = 1;
  4718. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4719. &client_boot_auth_only_cons);
  4720. mock_options->ORPort_set = 0;
  4721. mock_options->BridgeRelay = 0;
  4722. }
  4723. } else {
  4724. /* client */
  4725. mock_options->ClientOnly = 1;
  4726. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4727. &client_cons);
  4728. mock_options->ClientOnly = 0;
  4729. /* bridge relay */
  4730. mock_options->ORPort_set = 1;
  4731. mock_options->BridgeRelay = 1;
  4732. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
  4733. &client_cons);
  4734. mock_options->ORPort_set = 0;
  4735. mock_options->BridgeRelay = 0;
  4736. }
  4737. dls.schedule = DL_SCHED_BRIDGE;
  4738. /* client */
  4739. mock_options->ClientOnly = 1;
  4740. tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
  4741. done:
  4742. UNMOCK(networkstatus_consensus_is_bootstrapping);
  4743. UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
  4744. UNMOCK(get_options);
  4745. tor_free(mock_options);
  4746. mock_options = NULL;
  4747. }
  4748. static void
  4749. test_dir_assumed_flags(void *arg)
  4750. {
  4751. (void)arg;
  4752. smartlist_t *tokens = smartlist_new();
  4753. memarea_t *area = memarea_new();
  4754. routerstatus_t *rs = NULL;
  4755. /* First, we should always assume that the Running flag is set, even
  4756. * when it isn't listed, since the consensus method is always
  4757. * higher than 4. */
  4758. const char *str1 =
  4759. "r example hereiswhereyouridentitygoes 2015-08-30 12:00:00 "
  4760. "192.168.0.1 9001 0\n"
  4761. "m thisoneislongerbecauseitisa256bitmddigest33\n"
  4762. "s Fast Guard Stable\n";
  4763. const char *cp = str1;
  4764. rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL,
  4765. 23, FLAV_MICRODESC);
  4766. tt_assert(rs);
  4767. tt_assert(rs->is_flagged_running);
  4768. tt_assert(! rs->is_valid);
  4769. tt_assert(! rs->is_exit);
  4770. tt_assert(rs->is_fast);
  4771. routerstatus_free(rs);
  4772. /* With method 24 or later, we can assume "valid" is set. */
  4773. cp = str1;
  4774. rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL,
  4775. 24, FLAV_MICRODESC);
  4776. tt_assert(rs);
  4777. tt_assert(rs->is_flagged_running);
  4778. tt_assert(rs->is_valid);
  4779. tt_assert(! rs->is_exit);
  4780. tt_assert(rs->is_fast);
  4781. done:
  4782. smartlist_free(tokens);
  4783. memarea_drop_all(area);
  4784. routerstatus_free(rs);
  4785. }
  4786. #define DIR_LEGACY(name) \
  4787. { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
  4788. #define DIR(name,flags) \
  4789. { #name, test_dir_##name, (flags), NULL, NULL }
  4790. /* where arg is a string constant */
  4791. #define DIR_ARG(name,flags,arg) \
  4792. { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
  4793. struct testcase_t dir_tests[] = {
  4794. DIR_LEGACY(nicknames),
  4795. DIR_LEGACY(formats),
  4796. DIR(routerinfo_parsing, 0),
  4797. DIR(extrainfo_parsing, 0),
  4798. DIR(parse_router_list, TT_FORK),
  4799. DIR(load_routers, TT_FORK),
  4800. DIR(load_extrainfo, TT_FORK),
  4801. DIR_LEGACY(versions),
  4802. DIR_LEGACY(fp_pairs),
  4803. DIR(split_fps, 0),
  4804. DIR_LEGACY(measured_bw_kb),
  4805. DIR_LEGACY(measured_bw_kb_cache),
  4806. DIR_LEGACY(param_voting),
  4807. DIR(param_voting_lookup, 0),
  4808. DIR_LEGACY(v3_networkstatus),
  4809. DIR(random_weighted, 0),
  4810. DIR(scale_bw, 0),
  4811. DIR_LEGACY(clip_unmeasured_bw_kb),
  4812. DIR_LEGACY(clip_unmeasured_bw_kb_alt),
  4813. DIR(fmt_control_ns, 0),
  4814. DIR(dirserv_set_routerstatus_testing, 0),
  4815. DIR(http_handling, 0),
  4816. DIR(purpose_needs_anonymity, 0),
  4817. DIR(fetch_type, 0),
  4818. DIR(packages, 0),
  4819. DIR(download_status_schedule, 0),
  4820. DIR(download_status_random_backoff, 0),
  4821. DIR(download_status_random_backoff_ranges, 0),
  4822. DIR(download_status_increment, 0),
  4823. DIR(authdir_type_to_string, 0),
  4824. DIR(conn_purpose_to_string, 0),
  4825. DIR(should_use_directory_guards, 0),
  4826. DIR(should_not_init_request_to_ourselves, TT_FORK),
  4827. DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
  4828. DIR(should_init_request_to_dir_auths, 0),
  4829. DIR(choose_compression_level, 0),
  4830. DIR(dump_unparseable_descriptors, 0),
  4831. DIR(populate_dump_desc_fifo, 0),
  4832. DIR(populate_dump_desc_fifo_2, 0),
  4833. DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
  4834. DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
  4835. DIR_ARG(find_dl_schedule, TT_FORK, "cf"),
  4836. DIR_ARG(find_dl_schedule, TT_FORK, "ca"),
  4837. DIR(assumed_flags, 0),
  4838. END_OF_TESTCASES
  4839. };