test_dir.c 184 KB

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