test_dir.c 116 KB

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