test_dir.c 212 KB

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