test_dir.c 126 KB

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