test_dir.c 117 KB

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