test_dir.c 203 KB

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