test_dir.c 208 KB

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