test_dir.c 222 KB

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