test_dir.c 113 KB

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