test_dir.c 202 KB

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