test_dir.c 156 KB

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