test_dir.c 111 KB

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