test_dir.c 249 KB

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