test_dir.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include <math.h>
  7. #define DIRSERV_PRIVATE
  8. #define DIRVOTE_PRIVATE
  9. #define ROUTER_PRIVATE
  10. #define ROUTERLIST_PRIVATE
  11. #define HIBERNATE_PRIVATE
  12. #define NETWORKSTATUS_PRIVATE
  13. #include "or.h"
  14. #include "config.h"
  15. #include "directory.h"
  16. #include "dirserv.h"
  17. #include "dirvote.h"
  18. #include "hibernate.h"
  19. #include "networkstatus.h"
  20. #include "router.h"
  21. #include "routerlist.h"
  22. #include "routerparse.h"
  23. #include "test.h"
  24. static void
  25. test_dir_nicknames(void *arg)
  26. {
  27. (void)arg;
  28. tt_assert( is_legal_nickname("a"));
  29. tt_assert(!is_legal_nickname(""));
  30. tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  31. tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
  32. tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  33. tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  34. /* valid */
  35. tt_assert( is_legal_nickname_or_hexdigest(
  36. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  37. tt_assert( is_legal_nickname_or_hexdigest(
  38. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  39. tt_assert( is_legal_nickname_or_hexdigest(
  40. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  41. /* too short */
  42. tt_assert(!is_legal_nickname_or_hexdigest(
  43. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  44. /* illegal char */
  45. tt_assert(!is_legal_nickname_or_hexdigest(
  46. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  47. /* hex part too long */
  48. tt_assert(!is_legal_nickname_or_hexdigest(
  49. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  50. tt_assert(!is_legal_nickname_or_hexdigest(
  51. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  52. /* Bad nickname */
  53. tt_assert(!is_legal_nickname_or_hexdigest(
  54. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  55. tt_assert(!is_legal_nickname_or_hexdigest(
  56. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  57. tt_assert(!is_legal_nickname_or_hexdigest(
  58. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  59. tt_assert(!is_legal_nickname_or_hexdigest(
  60. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  61. "abcdefghijklmnoppqrst"));
  62. /* Bad extra char. */
  63. tt_assert(!is_legal_nickname_or_hexdigest(
  64. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  65. tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  66. tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  67. tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  68. done:
  69. ;
  70. }
  71. /** Run unit tests for router descriptor generation logic. */
  72. static void
  73. test_dir_formats(void *arg)
  74. {
  75. char *buf = NULL;
  76. char buf2[8192];
  77. char platform[256];
  78. char fingerprint[FINGERPRINT_LEN+1];
  79. char *pk1_str = NULL, *pk2_str = NULL, *cp;
  80. size_t pk1_str_len, pk2_str_len;
  81. routerinfo_t *r1=NULL, *r2=NULL;
  82. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  83. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  84. addr_policy_t *ex1, *ex2;
  85. routerlist_t *dir1 = NULL, *dir2 = NULL;
  86. or_options_t *options = get_options_mutable();
  87. const addr_policy_t *p;
  88. (void)arg;
  89. pk1 = pk_generate(0);
  90. pk2 = pk_generate(1);
  91. tt_assert(pk1 && pk2);
  92. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  93. get_platform_str(platform, sizeof(platform));
  94. r1 = tor_malloc_zero(sizeof(routerinfo_t));
  95. r1->addr = 0xc0a80001u; /* 192.168.0.1 */
  96. r1->cache_info.published_on = 0;
  97. r1->or_port = 9000;
  98. r1->dir_port = 9003;
  99. tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
  100. r1->ipv6_orport = 9999;
  101. r1->onion_pkey = crypto_pk_dup_key(pk1);
  102. r1->identity_pkey = crypto_pk_dup_key(pk2);
  103. r1->bandwidthrate = 1000;
  104. r1->bandwidthburst = 5000;
  105. r1->bandwidthcapacity = 10000;
  106. r1->exit_policy = NULL;
  107. r1->nickname = tor_strdup("Magri");
  108. r1->platform = tor_strdup(platform);
  109. ex1 = tor_malloc_zero(sizeof(addr_policy_t));
  110. ex2 = tor_malloc_zero(sizeof(addr_policy_t));
  111. ex1->policy_type = ADDR_POLICY_ACCEPT;
  112. tor_addr_from_ipv4h(&ex1->addr, 0);
  113. ex1->maskbits = 0;
  114. ex1->prt_min = ex1->prt_max = 80;
  115. ex2->policy_type = ADDR_POLICY_REJECT;
  116. tor_addr_from_ipv4h(&ex2->addr, 18<<24);
  117. ex2->maskbits = 8;
  118. ex2->prt_min = ex2->prt_max = 24;
  119. r2 = tor_malloc_zero(sizeof(routerinfo_t));
  120. r2->addr = 0x0a030201u; /* 10.3.2.1 */
  121. r2->platform = tor_strdup(platform);
  122. r2->cache_info.published_on = 5;
  123. r2->or_port = 9005;
  124. r2->dir_port = 0;
  125. r2->onion_pkey = crypto_pk_dup_key(pk2);
  126. r2->onion_curve25519_pkey = tor_malloc_zero(sizeof(curve25519_public_key_t));
  127. curve25519_public_from_base64(r2->onion_curve25519_pkey,
  128. "skyinAnvardNostarsNomoonNowindormistsorsnow");
  129. r2->identity_pkey = crypto_pk_dup_key(pk1);
  130. r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
  131. r2->exit_policy = smartlist_new();
  132. smartlist_add(r2->exit_policy, ex1);
  133. smartlist_add(r2->exit_policy, ex2);
  134. r2->nickname = tor_strdup("Fred");
  135. tt_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  136. &pk1_str_len));
  137. tt_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  138. &pk2_str_len));
  139. /* XXXX025 router_dump_to_string should really take this from ri.*/
  140. options->ContactInfo = tor_strdup("Magri White "
  141. "<magri@elsewhere.example.com>");
  142. buf = router_dump_router_to_string(r1, pk2);
  143. tor_free(options->ContactInfo);
  144. tt_assert(buf);
  145. strlcpy(buf2, "router Magri 192.168.0.1 9000 0 9003\n"
  146. "or-address [1:2:3:4::]:9999\n"
  147. "platform Tor "VERSION" on ", sizeof(buf2));
  148. strlcat(buf2, get_uname(), sizeof(buf2));
  149. strlcat(buf2, "\n"
  150. "protocols Link 1 2 Circuit 1\n"
  151. "published 1970-01-01 00:00:00\n"
  152. "fingerprint ", sizeof(buf2));
  153. tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  154. strlcat(buf2, fingerprint, sizeof(buf2));
  155. strlcat(buf2, "\nuptime 0\n"
  156. /* XXX the "0" above is hard-coded, but even if we made it reflect
  157. * uptime, that still wouldn't make it right, because the two
  158. * descriptors might be made on different seconds... hm. */
  159. "bandwidth 1000 5000 10000\n"
  160. "onion-key\n", sizeof(buf2));
  161. strlcat(buf2, pk1_str, sizeof(buf2));
  162. strlcat(buf2, "signing-key\n", sizeof(buf2));
  163. strlcat(buf2, pk2_str, sizeof(buf2));
  164. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  165. strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
  166. sizeof(buf2));
  167. strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
  168. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  169. * twice */
  170. tt_str_op(buf,==, buf2);
  171. tor_free(buf);
  172. buf = router_dump_router_to_string(r1, pk2);
  173. tt_assert(buf);
  174. cp = buf;
  175. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
  176. tt_assert(rp1);
  177. tt_int_op(rp1->addr,==, r1->addr);
  178. tt_int_op(rp1->or_port,==, r1->or_port);
  179. //test_eq(rp1->dir_port, r1->dir_port);
  180. tt_int_op(rp1->bandwidthrate,==, r1->bandwidthrate);
  181. tt_int_op(rp1->bandwidthburst,==, r1->bandwidthburst);
  182. tt_int_op(rp1->bandwidthcapacity,==, r1->bandwidthcapacity);
  183. tt_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  184. tt_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  185. //tt_assert(rp1->exit_policy == NULL);
  186. tor_free(buf);
  187. strlcpy(buf2,
  188. "router Fred 10.3.2.1 9005 0 0\n"
  189. "platform Tor "VERSION" on ", sizeof(buf2));
  190. strlcat(buf2, get_uname(), sizeof(buf2));
  191. strlcat(buf2, "\n"
  192. "protocols Link 1 2 Circuit 1\n"
  193. "published 1970-01-01 00:00:05\n"
  194. "fingerprint ", sizeof(buf2));
  195. tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
  196. strlcat(buf2, fingerprint, sizeof(buf2));
  197. strlcat(buf2, "\nuptime 0\n"
  198. "bandwidth 3000 3000 3000\n", sizeof(buf2));
  199. strlcat(buf2, "onion-key\n", sizeof(buf2));
  200. strlcat(buf2, pk2_str, sizeof(buf2));
  201. strlcat(buf2, "signing-key\n", sizeof(buf2));
  202. strlcat(buf2, pk1_str, sizeof(buf2));
  203. strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
  204. #ifdef CURVE25519_ENABLED
  205. strlcat(buf2, "ntor-onion-key "
  206. "skyinAnvardNostarsNomoonNowindormistsorsnow=\n", sizeof(buf2));
  207. #endif
  208. strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
  209. strlcat(buf2, "router-signature\n", sizeof(buf2));
  210. buf = router_dump_router_to_string(r2, pk1);
  211. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  212. * twice */
  213. tt_str_op(buf,==, buf2);
  214. tor_free(buf);
  215. buf = router_dump_router_to_string(r2, pk1);
  216. cp = buf;
  217. rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
  218. tt_assert(rp2);
  219. tt_int_op(rp2->addr,==, r2->addr);
  220. tt_int_op(rp2->or_port,==, r2->or_port);
  221. tt_int_op(rp2->dir_port,==, r2->dir_port);
  222. tt_int_op(rp2->bandwidthrate,==, r2->bandwidthrate);
  223. tt_int_op(rp2->bandwidthburst,==, r2->bandwidthburst);
  224. tt_int_op(rp2->bandwidthcapacity,==, r2->bandwidthcapacity);
  225. #ifdef CURVE25519_ENABLED
  226. tt_mem_op(rp2->onion_curve25519_pkey->public_key,==,
  227. r2->onion_curve25519_pkey->public_key,
  228. CURVE25519_PUBKEY_LEN);
  229. #endif
  230. tt_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  231. tt_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  232. tt_int_op(smartlist_len(rp2->exit_policy),==, 2);
  233. p = smartlist_get(rp2->exit_policy, 0);
  234. tt_int_op(p->policy_type,==, ADDR_POLICY_ACCEPT);
  235. tt_assert(tor_addr_is_null(&p->addr));
  236. tt_int_op(p->maskbits,==, 0);
  237. tt_int_op(p->prt_min,==, 80);
  238. tt_int_op(p->prt_max,==, 80);
  239. p = smartlist_get(rp2->exit_policy, 1);
  240. tt_int_op(p->policy_type,==, ADDR_POLICY_REJECT);
  241. tt_assert(tor_addr_eq(&p->addr, &ex2->addr));
  242. tt_int_op(p->maskbits,==, 8);
  243. tt_int_op(p->prt_min,==, 24);
  244. tt_int_op(p->prt_max,==, 24);
  245. #if 0
  246. /* Okay, now for the directories. */
  247. {
  248. fingerprint_list = smartlist_new();
  249. crypto_pk_get_fingerprint(pk2, buf, 1);
  250. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  251. crypto_pk_get_fingerprint(pk1, buf, 1);
  252. add_fingerprint_to_dir(buf, fingerprint_list, 0);
  253. }
  254. #endif
  255. dirserv_free_fingerprint_list();
  256. done:
  257. if (r1)
  258. routerinfo_free(r1);
  259. if (r2)
  260. routerinfo_free(r2);
  261. if (rp2)
  262. routerinfo_free(rp2);
  263. tor_free(buf);
  264. tor_free(pk1_str);
  265. tor_free(pk2_str);
  266. if (pk1) crypto_pk_free(pk1);
  267. if (pk2) crypto_pk_free(pk2);
  268. if (rp1) routerinfo_free(rp1);
  269. tor_free(dir1); /* XXXX And more !*/
  270. tor_free(dir2); /* And more !*/
  271. }
  272. static void
  273. test_dir_versions(void *arg)
  274. {
  275. tor_version_t ver1;
  276. /* Try out version parsing functionality */
  277. (void)arg;
  278. tt_int_op(0,==, tor_version_parse("0.3.4pre2-cvs", &ver1));
  279. tt_int_op(0,==, ver1.major);
  280. tt_int_op(3,==, ver1.minor);
  281. tt_int_op(4,==, ver1.micro);
  282. tt_int_op(VER_PRE,==, ver1.status);
  283. tt_int_op(2,==, ver1.patchlevel);
  284. tt_int_op(0,==, tor_version_parse("0.3.4rc1", &ver1));
  285. tt_int_op(0,==, ver1.major);
  286. tt_int_op(3,==, ver1.minor);
  287. tt_int_op(4,==, ver1.micro);
  288. tt_int_op(VER_RC,==, ver1.status);
  289. tt_int_op(1,==, ver1.patchlevel);
  290. tt_int_op(0,==, tor_version_parse("1.3.4", &ver1));
  291. tt_int_op(1,==, ver1.major);
  292. tt_int_op(3,==, ver1.minor);
  293. tt_int_op(4,==, ver1.micro);
  294. tt_int_op(VER_RELEASE,==, ver1.status);
  295. tt_int_op(0,==, ver1.patchlevel);
  296. tt_int_op(0,==, tor_version_parse("1.3.4.999", &ver1));
  297. tt_int_op(1,==, ver1.major);
  298. tt_int_op(3,==, ver1.minor);
  299. tt_int_op(4,==, ver1.micro);
  300. tt_int_op(VER_RELEASE,==, ver1.status);
  301. tt_int_op(999,==, ver1.patchlevel);
  302. tt_int_op(0,==, tor_version_parse("0.1.2.4-alpha", &ver1));
  303. tt_int_op(0,==, ver1.major);
  304. tt_int_op(1,==, ver1.minor);
  305. tt_int_op(2,==, ver1.micro);
  306. tt_int_op(4,==, ver1.patchlevel);
  307. tt_int_op(VER_RELEASE,==, ver1.status);
  308. tt_str_op("alpha",==, ver1.status_tag);
  309. tt_int_op(0,==, tor_version_parse("0.1.2.4", &ver1));
  310. tt_int_op(0,==, ver1.major);
  311. tt_int_op(1,==, ver1.minor);
  312. tt_int_op(2,==, ver1.micro);
  313. tt_int_op(4,==, ver1.patchlevel);
  314. tt_int_op(VER_RELEASE,==, ver1.status);
  315. tt_str_op("",==, ver1.status_tag);
  316. #define tt_versionstatus_op(vs1, op, vs2) \
  317. tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
  318. (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
  319. #define test_v_i_o(val, ver, lst) \
  320. tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
  321. /* make sure tor_version_is_obsolete() works */
  322. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  323. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  324. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  325. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  326. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  327. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  328. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  329. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  330. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  331. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  332. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  333. /* Not on list, but newer than any in same series. */
  334. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  335. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  336. /* Series newer than any on list. */
  337. 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");
  338. /* Series older than any on list. */
  339. 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");
  340. /* Not on list, not newer than any on same series. */
  341. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  342. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  343. /* On list, not newer than any on same series. */
  344. test_v_i_o(VS_UNRECOMMENDED,
  345. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  346. tt_int_op(0,==, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  347. tt_int_op(1,==, tor_version_as_new_as(
  348. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  349. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  350. "0.0.8rc2"));
  351. tt_int_op(0,==, tor_version_as_new_as(
  352. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  353. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  354. /* Now try svn revisions. */
  355. tt_int_op(1,==, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  356. "Tor 0.2.1.0-dev (r99)"));
  357. tt_int_op(1,==, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
  358. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  359. tt_int_op(1,==, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  360. "Tor 0.2.1.0-dev on Colossus"));
  361. tt_int_op(0,==, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  362. "Tor 0.2.1.0-dev (r100)"));
  363. tt_int_op(0,==, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  364. "Tor 0.2.1.0-dev (r100) on AM"));
  365. tt_int_op(0,==, tor_version_as_new_as("Tor 0.2.1.0-dev",
  366. "Tor 0.2.1.0-dev (r99)"));
  367. tt_int_op(1,==, tor_version_as_new_as("Tor 0.2.1.1",
  368. "Tor 0.2.1.0-dev (r99)"));
  369. /* Now try git revisions */
  370. tt_int_op(0,==, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
  371. tt_int_op(0,==, ver1.major);
  372. tt_int_op(5,==, ver1.minor);
  373. tt_int_op(6,==, ver1.micro);
  374. tt_int_op(7,==, ver1.patchlevel);
  375. tt_int_op(3,==, ver1.git_tag_len);
  376. tt_mem_op(ver1.git_tag,==, "\xff\x00\xff", 3);
  377. tt_int_op(-1,==, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
  378. tt_int_op(-1,==, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
  379. tt_int_op(0,==, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
  380. done:
  381. ;
  382. }
  383. /** Run unit tests for directory fp_pair functions. */
  384. static void
  385. test_dir_fp_pairs(void *arg)
  386. {
  387. smartlist_t *sl = smartlist_new();
  388. fp_pair_t *pair;
  389. (void)arg;
  390. dir_split_resource_into_fingerprint_pairs(
  391. /* Two pairs, out of order, with one duplicate. */
  392. "73656372657420646174612E0000000000FFFFFF-"
  393. "557365204145532d32353620696e73746561642e+"
  394. "73656372657420646174612E0000000000FFFFFF-"
  395. "557365204145532d32353620696e73746561642e+"
  396. "48657861646563696d616c2069736e277420736f-"
  397. "676f6f6420666f7220686964696e6720796f7572.z", sl);
  398. tt_int_op(smartlist_len(sl),==, 2);
  399. pair = smartlist_get(sl, 0);
  400. tt_mem_op(pair->first,==, "Hexadecimal isn't so", DIGEST_LEN);
  401. tt_mem_op(pair->second,==, "good for hiding your", DIGEST_LEN);
  402. pair = smartlist_get(sl, 1);
  403. tt_mem_op(pair->first,==, "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN);
  404. tt_mem_op(pair->second,==, "Use AES-256 instead.", DIGEST_LEN);
  405. done:
  406. SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
  407. smartlist_free(sl);
  408. }
  409. static void
  410. test_dir_split_fps(void *testdata)
  411. {
  412. smartlist_t *sl = smartlist_new();
  413. char *mem_op_hex_tmp = NULL;
  414. (void)testdata;
  415. /* Some example hex fingerprints and their base64 equivalents */
  416. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  417. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  418. #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
  419. #define HEX256_1 \
  420. "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
  421. #define HEX256_2 \
  422. "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
  423. #define HEX256_3 \
  424. "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
  425. #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
  426. #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
  427. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  428. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  429. /* no flags set */
  430. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  431. tt_int_op(smartlist_len(sl), ==, 3);
  432. tt_str_op(smartlist_get(sl, 0), ==, "A");
  433. tt_str_op(smartlist_get(sl, 1), ==, "C");
  434. tt_str_op(smartlist_get(sl, 2), ==, "B");
  435. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  436. smartlist_clear(sl);
  437. /* uniq strings. */
  438. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  439. tt_int_op(smartlist_len(sl), ==, 3);
  440. tt_str_op(smartlist_get(sl, 0), ==, "A");
  441. tt_str_op(smartlist_get(sl, 1), ==, "B");
  442. tt_str_op(smartlist_get(sl, 2), ==, "C");
  443. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  444. smartlist_clear(sl);
  445. /* Decode hex. */
  446. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  447. tt_int_op(smartlist_len(sl), ==, 2);
  448. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  449. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  450. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  451. smartlist_clear(sl);
  452. /* decode hex and drop weirdness. */
  453. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  454. sl, NULL, DSR_HEX);
  455. tt_int_op(smartlist_len(sl), ==, 2);
  456. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  457. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  458. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  459. smartlist_clear(sl);
  460. /* Decode long hex */
  461. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  462. sl, NULL, DSR_HEX|DSR_DIGEST256);
  463. tt_int_op(smartlist_len(sl), ==, 3);
  464. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  465. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  466. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
  467. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  468. smartlist_clear(sl);
  469. /* Decode hex and sort. */
  470. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  471. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  472. tt_int_op(smartlist_len(sl), ==, 3);
  473. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
  474. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  475. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
  476. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  477. smartlist_clear(sl);
  478. /* Decode long hex and sort */
  479. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  480. "+"HEX256_1,
  481. sl, NULL,
  482. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  483. tt_int_op(smartlist_len(sl), ==, 3);
  484. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
  485. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  486. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
  487. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  488. smartlist_clear(sl);
  489. /* Decode base64 */
  490. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  491. tt_int_op(smartlist_len(sl), ==, 2);
  492. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  493. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  494. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  495. smartlist_clear(sl);
  496. /* Decode long base64 */
  497. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  498. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  499. tt_int_op(smartlist_len(sl), ==, 2);
  500. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  501. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  502. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  503. smartlist_clear(sl);
  504. dir_split_resource_into_fingerprints(B64_256_1,
  505. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  506. tt_int_op(smartlist_len(sl), ==, 1);
  507. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  508. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  509. smartlist_clear(sl);
  510. done:
  511. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  512. smartlist_free(sl);
  513. tor_free(mem_op_hex_tmp);
  514. }
  515. static void
  516. test_dir_measured_bw_kb(void *arg)
  517. {
  518. measured_bw_line_t mbwl;
  519. int i;
  520. const char *lines_pass[] = {
  521. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  522. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  523. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  524. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  525. "bw=1024 junk=007\n",
  526. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  527. "bw=1024 junk=007\n",
  528. "end"
  529. };
  530. const char *lines_fail[] = {
  531. /* Test possible python stupidity on input */
  532. "node_id=None bw=1024\n",
  533. "node_id=$None bw=1024\n",
  534. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  535. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  536. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  537. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  538. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  539. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  540. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  541. /* Test incomplete writes due to race conditions, partial copies, etc */
  542. "node_i",
  543. "node_i\n",
  544. "node_id=",
  545. "node_id=\n",
  546. "node_id=$557365204145532d32353620696e73746561642e bw=",
  547. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  548. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  549. "node_id=$557365204145532d32353620696e7374",
  550. "node_id=$557365204145532d32353620696e7374\n",
  551. "",
  552. "\n",
  553. " \n ",
  554. " \n\n",
  555. /* Test assorted noise */
  556. " node_id= ",
  557. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  558. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  559. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  560. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  561. "end"
  562. };
  563. (void)arg;
  564. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  565. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  566. tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
  567. }
  568. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  569. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  570. tt_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
  571. tt_assert(mbwl.bw_kb == 1024);
  572. tt_assert(strcmp(mbwl.node_hex,
  573. "557365204145532d32353620696e73746561642e") == 0);
  574. }
  575. done:
  576. return;
  577. }
  578. #define MBWC_INIT_TIME 1000
  579. /** Do the measured bandwidth cache unit test */
  580. static void
  581. test_dir_measured_bw_kb_cache(void *arg)
  582. {
  583. /* Initial fake time_t for testing */
  584. time_t curr = MBWC_INIT_TIME;
  585. /* Some measured_bw_line_ts */
  586. measured_bw_line_t mbwl[3];
  587. /* For receiving output on cache queries */
  588. long bw;
  589. time_t as_of;
  590. /* First, clear the cache and assert that it's empty */
  591. (void)arg;
  592. dirserv_clear_measured_bw_cache();
  593. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 0);
  594. /*
  595. * Set up test mbwls; none of the dirserv_cache_*() functions care about
  596. * the node_hex field.
  597. */
  598. memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
  599. mbwl[0].bw_kb = 20;
  600. memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
  601. mbwl[1].bw_kb = 40;
  602. memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
  603. mbwl[2].bw_kb = 80;
  604. /* Try caching something */
  605. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  606. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 1);
  607. /* Okay, let's see if we can retrieve it */
  608. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, &as_of));
  609. tt_int_op(bw,==, 20);
  610. tt_int_op(as_of,==, MBWC_INIT_TIME);
  611. /* Try retrieving it without some outputs */
  612. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL, NULL));
  613. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, NULL));
  614. tt_int_op(bw,==, 20);
  615. tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL,&as_of));
  616. tt_int_op(as_of,==, MBWC_INIT_TIME);
  617. /* Now expire it */
  618. curr += MAX_MEASUREMENT_AGE + 1;
  619. dirserv_expire_measured_bw_cache(curr);
  620. /* Check that the cache is empty */
  621. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 0);
  622. /* Check that we can't retrieve it */
  623. tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl[0].node_id, NULL,NULL));
  624. /* Try caching a few things now */
  625. dirserv_cache_measured_bw(&(mbwl[0]), curr);
  626. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 1);
  627. curr += MAX_MEASUREMENT_AGE / 4;
  628. dirserv_cache_measured_bw(&(mbwl[1]), curr);
  629. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 2);
  630. curr += MAX_MEASUREMENT_AGE / 4;
  631. dirserv_cache_measured_bw(&(mbwl[2]), curr);
  632. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 3);
  633. curr += MAX_MEASUREMENT_AGE / 4 + 1;
  634. /* Do an expire that's too soon to get any of them */
  635. dirserv_expire_measured_bw_cache(curr);
  636. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 3);
  637. /* Push the oldest one off the cliff */
  638. curr += MAX_MEASUREMENT_AGE / 4;
  639. dirserv_expire_measured_bw_cache(curr);
  640. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 2);
  641. /* And another... */
  642. curr += MAX_MEASUREMENT_AGE / 4;
  643. dirserv_expire_measured_bw_cache(curr);
  644. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 1);
  645. /* This should empty it out again */
  646. curr += MAX_MEASUREMENT_AGE / 4;
  647. dirserv_expire_measured_bw_cache(curr);
  648. tt_int_op(dirserv_get_measured_bw_cache_size(),==, 0);
  649. done:
  650. return;
  651. }
  652. static void
  653. test_dir_param_voting(void *arg)
  654. {
  655. networkstatus_t vote1, vote2, vote3, vote4;
  656. smartlist_t *votes = smartlist_new();
  657. char *res = NULL;
  658. /* dirvote_compute_params only looks at the net_params field of the votes,
  659. so that's all we need to set.
  660. */
  661. (void)arg;
  662. memset(&vote1, 0, sizeof(vote1));
  663. memset(&vote2, 0, sizeof(vote2));
  664. memset(&vote3, 0, sizeof(vote3));
  665. memset(&vote4, 0, sizeof(vote4));
  666. vote1.net_params = smartlist_new();
  667. vote2.net_params = smartlist_new();
  668. vote3.net_params = smartlist_new();
  669. vote4.net_params = smartlist_new();
  670. smartlist_split_string(vote1.net_params,
  671. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  672. smartlist_split_string(vote2.net_params,
  673. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  674. smartlist_split_string(vote3.net_params,
  675. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  676. smartlist_split_string(vote4.net_params,
  677. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  678. tt_int_op(100,==, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  679. tt_int_op(222,==, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  680. tt_int_op(80,==, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  681. tt_int_op(-8,==, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  682. tt_int_op(0,==, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  683. smartlist_add(votes, &vote1);
  684. /* Do the first tests without adding all the other votes, for
  685. * networks without many dirauths. */
  686. res = dirvote_compute_params(votes, 12, 2);
  687. tt_str_op(res,==, "");
  688. tor_free(res);
  689. res = dirvote_compute_params(votes, 12, 1);
  690. tt_str_op(res,==, "ab=90 abcd=20 cw=50 x-yz=-99");
  691. tor_free(res);
  692. smartlist_add(votes, &vote2);
  693. res = dirvote_compute_params(votes, 12, 2);
  694. tt_str_op(res,==, "ab=27 cw=5 x-yz=-99");
  695. tor_free(res);
  696. res = dirvote_compute_params(votes, 12, 3);
  697. tt_str_op(res,==, "ab=27 cw=5 x-yz=-99");
  698. tor_free(res);
  699. res = dirvote_compute_params(votes, 12, 6);
  700. tt_str_op(res,==, "");
  701. tor_free(res);
  702. smartlist_add(votes, &vote3);
  703. res = dirvote_compute_params(votes, 12, 3);
  704. tt_str_op(res,==, "ab=27 abcd=20 cw=50 x-yz=-9");
  705. tor_free(res);
  706. res = dirvote_compute_params(votes, 12, 5);
  707. tt_str_op(res,==, "cw=50 x-yz=-9");
  708. tor_free(res);
  709. res = dirvote_compute_params(votes, 12, 9);
  710. tt_str_op(res,==, "cw=50 x-yz=-9");
  711. tor_free(res);
  712. smartlist_add(votes, &vote4);
  713. res = dirvote_compute_params(votes, 12, 4);
  714. tt_str_op(res,==, "ab=90 abcd=20 cw=50 x-yz=-9");
  715. tor_free(res);
  716. res = dirvote_compute_params(votes, 12, 5);
  717. tt_str_op(res,==, "ab=90 abcd=20 cw=50 x-yz=-9");
  718. tor_free(res);
  719. /* Test that the special-cased "at least three dirauths voted for
  720. * this param" logic works as expected. */
  721. res = dirvote_compute_params(votes, 12, 6);
  722. tt_str_op(res,==, "ab=90 abcd=20 cw=50 x-yz=-9");
  723. tor_free(res);
  724. res = dirvote_compute_params(votes, 12, 10);
  725. tt_str_op(res,==, "ab=90 abcd=20 cw=50 x-yz=-9");
  726. tor_free(res);
  727. done:
  728. tor_free(res);
  729. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  730. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  731. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  732. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  733. smartlist_free(vote1.net_params);
  734. smartlist_free(vote2.net_params);
  735. smartlist_free(vote3.net_params);
  736. smartlist_free(vote4.net_params);
  737. smartlist_free(votes);
  738. return;
  739. }
  740. extern const char AUTHORITY_CERT_1[];
  741. extern const char AUTHORITY_SIGNKEY_1[];
  742. extern const char AUTHORITY_CERT_2[];
  743. extern const char AUTHORITY_SIGNKEY_2[];
  744. extern const char AUTHORITY_CERT_3[];
  745. extern const char AUTHORITY_SIGNKEY_3[];
  746. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  747. * same voting authority, and that they do in fact have all the same
  748. * information. */
  749. static void
  750. test_same_voter(networkstatus_voter_info_t *v1,
  751. networkstatus_voter_info_t *v2)
  752. {
  753. tt_str_op(v1->nickname,==, v2->nickname);
  754. tt_mem_op(v1->identity_digest,==, v2->identity_digest, DIGEST_LEN);
  755. tt_str_op(v1->address,==, v2->address);
  756. tt_int_op(v1->addr,==, v2->addr);
  757. tt_int_op(v1->dir_port,==, v2->dir_port);
  758. tt_int_op(v1->or_port,==, v2->or_port);
  759. tt_str_op(v1->contact,==, v2->contact);
  760. tt_mem_op(v1->vote_digest,==, v2->vote_digest, DIGEST_LEN);
  761. done:
  762. ;
  763. }
  764. /** Helper: Make a new routerinfo containing the right information for a
  765. * given vote_routerstatus_t. */
  766. static routerinfo_t *
  767. generate_ri_from_rs(const vote_routerstatus_t *vrs)
  768. {
  769. routerinfo_t *r;
  770. const routerstatus_t *rs = &vrs->status;
  771. static time_t published = 0;
  772. r = tor_malloc_zero(sizeof(routerinfo_t));
  773. memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
  774. memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
  775. DIGEST_LEN);
  776. r->cache_info.do_not_cache = 1;
  777. r->cache_info.routerlist_index = -1;
  778. r->cache_info.signed_descriptor_body =
  779. tor_strdup("123456789012345678901234567890123");
  780. r->cache_info.signed_descriptor_len =
  781. strlen(r->cache_info.signed_descriptor_body);
  782. r->exit_policy = smartlist_new();
  783. r->cache_info.published_on = ++published + time(NULL);
  784. if (rs->has_bandwidth) {
  785. /*
  786. * Multiply by 1000 because the routerinfo_t and the routerstatus_t
  787. * seem to use different units (*sigh*) and because we seem stuck on
  788. * icky and perverse decimal kilobytes (*double sigh*) - see
  789. * router_get_advertised_bandwidth_capped() of routerlist.c and
  790. * routerstatus_format_entry() of dirserv.c.
  791. */
  792. r->bandwidthrate = rs->bandwidth_kb * 1000;
  793. r->bandwidthcapacity = rs->bandwidth_kb * 1000;
  794. }
  795. return r;
  796. }
  797. /** Helper: get a detached signatures document for one or two
  798. * consensuses. */
  799. static char *
  800. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  801. {
  802. char *r;
  803. smartlist_t *sl;
  804. tor_assert(ns && ns->flavor == FLAV_NS);
  805. sl = smartlist_new();
  806. smartlist_add(sl,ns);
  807. if (ns2)
  808. smartlist_add(sl,ns2);
  809. r = networkstatus_get_detached_signatures(sl);
  810. smartlist_free(sl);
  811. return r;
  812. }
  813. /**
  814. * Generate a routerstatus for v3_networkstatus test
  815. */
  816. static vote_routerstatus_t *
  817. gen_routerstatus_for_v3ns(int idx, time_t now)
  818. {
  819. vote_routerstatus_t *vrs=NULL;
  820. routerstatus_t *rs;
  821. tor_addr_t addr_ipv6;
  822. switch (idx) {
  823. case 0:
  824. /* Generate the first routerstatus. */
  825. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  826. rs = &vrs->status;
  827. vrs->version = tor_strdup("0.1.2.14");
  828. rs->published_on = now-1500;
  829. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  830. memset(rs->identity_digest, 3, DIGEST_LEN);
  831. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  832. rs->addr = 0x99008801;
  833. rs->or_port = 443;
  834. rs->dir_port = 8000;
  835. /* all flags but running cleared */
  836. rs->is_flagged_running = 1;
  837. break;
  838. case 1:
  839. /* Generate the second routerstatus. */
  840. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  841. rs = &vrs->status;
  842. vrs->version = tor_strdup("0.2.0.5");
  843. rs->published_on = now-1000;
  844. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  845. memset(rs->identity_digest, 5, DIGEST_LEN);
  846. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  847. rs->addr = 0x99009901;
  848. rs->or_port = 443;
  849. rs->dir_port = 0;
  850. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  851. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  852. rs->ipv6_orport = 4711;
  853. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  854. rs->is_valid = rs->is_possible_guard = 1;
  855. break;
  856. case 2:
  857. /* Generate the third routerstatus. */
  858. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  859. rs = &vrs->status;
  860. vrs->version = tor_strdup("0.1.0.3");
  861. rs->published_on = now-1000;
  862. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  863. memset(rs->identity_digest, 33, DIGEST_LEN);
  864. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  865. rs->addr = 0xAA009901;
  866. rs->or_port = 400;
  867. rs->dir_port = 9999;
  868. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  869. rs->is_flagged_running = rs->is_valid =
  870. rs->is_possible_guard = 1;
  871. break;
  872. case 3:
  873. /* Generate a fourth routerstatus that is not running. */
  874. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  875. rs = &vrs->status;
  876. vrs->version = tor_strdup("0.1.6.3");
  877. rs->published_on = now-1000;
  878. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  879. memset(rs->identity_digest, 34, DIGEST_LEN);
  880. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  881. rs->addr = 0xC0000203;
  882. rs->or_port = 500;
  883. rs->dir_port = 1999;
  884. /* Running flag (and others) cleared */
  885. break;
  886. case 4:
  887. /* No more for this test; return NULL */
  888. vrs = NULL;
  889. break;
  890. default:
  891. /* Shouldn't happen */
  892. tt_assert(0);
  893. }
  894. if (vrs) {
  895. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  896. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  897. "m 9,10,11,12,13,14,15,16,17 "
  898. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  899. idx);
  900. }
  901. done:
  902. return vrs;
  903. }
  904. /** Apply tweaks to the vote list for each voter */
  905. static int
  906. vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
  907. {
  908. vote_routerstatus_t *vrs;
  909. const char *msg = NULL;
  910. tt_assert(v);
  911. (void)now;
  912. if (voter == 1) {
  913. measured_bw_line_t mbw;
  914. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  915. mbw.bw_kb = 1024;
  916. tt_assert(measured_bw_line_apply(&mbw,
  917. v->routerstatus_list) == 1);
  918. } else if (voter == 2 || voter == 3) {
  919. /* Monkey around with the list a bit */
  920. vrs = smartlist_get(v->routerstatus_list, 2);
  921. smartlist_del_keeporder(v->routerstatus_list, 2);
  922. vote_routerstatus_free(vrs);
  923. vrs = smartlist_get(v->routerstatus_list, 0);
  924. vrs->status.is_fast = 1;
  925. if (voter == 3) {
  926. vrs = smartlist_get(v->routerstatus_list, 0);
  927. smartlist_del_keeporder(v->routerstatus_list, 0);
  928. vote_routerstatus_free(vrs);
  929. vrs = smartlist_get(v->routerstatus_list, 0);
  930. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  931. tt_assert(router_add_to_routerlist(
  932. generate_ri_from_rs(vrs), &msg,0,0) >= 0);
  933. }
  934. }
  935. done:
  936. return 0;
  937. }
  938. /**
  939. * Test a parsed vote_routerstatus_t for v3_networkstatus test
  940. */
  941. static void
  942. test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
  943. {
  944. routerstatus_t *rs;
  945. tor_addr_t addr_ipv6;
  946. tt_assert(vrs);
  947. rs = &(vrs->status);
  948. tt_assert(rs);
  949. /* Split out by digests to test */
  950. if (tor_memeq(rs->identity_digest,
  951. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  952. "\x3\x3\x3\x3",
  953. DIGEST_LEN) &&
  954. (voter == 1)) {
  955. /* Check the first routerstatus. */
  956. tt_str_op(vrs->version,==, "0.1.2.14");
  957. tt_int_op(rs->published_on,==, now-1500);
  958. tt_str_op(rs->nickname,==, "router2");
  959. tt_mem_op(rs->identity_digest,==,
  960. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  961. "\x3\x3\x3\x3",
  962. DIGEST_LEN);
  963. tt_mem_op(rs->descriptor_digest,==, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  964. tt_int_op(rs->addr,==, 0x99008801);
  965. tt_int_op(rs->or_port,==, 443);
  966. tt_int_op(rs->dir_port,==, 8000);
  967. /* no flags except "running" (16) and "v2dir" (64) */
  968. tt_u64_op(vrs->flags, ==, U64_LITERAL(80));
  969. } else if (tor_memeq(rs->identity_digest,
  970. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  971. "\x5\x5\x5\x5",
  972. DIGEST_LEN) &&
  973. (voter == 1 || voter == 2)) {
  974. tt_mem_op(rs->identity_digest,==,
  975. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  976. "\x5\x5\x5\x5",
  977. DIGEST_LEN);
  978. if (voter == 1) {
  979. /* Check the second routerstatus. */
  980. tt_str_op(vrs->version,==, "0.2.0.5");
  981. tt_int_op(rs->published_on,==, now-1000);
  982. tt_str_op(rs->nickname,==, "router1");
  983. }
  984. tt_mem_op(rs->descriptor_digest,==, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  985. tt_int_op(rs->addr,==, 0x99009901);
  986. tt_int_op(rs->or_port,==, 443);
  987. tt_int_op(rs->dir_port,==, 0);
  988. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  989. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  990. tt_int_op(rs->ipv6_orport,==, 4711);
  991. if (voter == 1) {
  992. /* all except "authority" (1) and "v2dir" (64) */
  993. tt_u64_op(vrs->flags, ==, U64_LITERAL(190));
  994. } else {
  995. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) - v2dir(256) */
  996. tt_u64_op(vrs->flags, ==, U64_LITERAL(718));
  997. }
  998. } else if (tor_memeq(rs->identity_digest,
  999. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1000. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1001. DIGEST_LEN) &&
  1002. (voter == 1 || voter == 2)) {
  1003. /* Check the measured bandwidth bits */
  1004. tt_assert(vrs->has_measured_bw &&
  1005. vrs->measured_bw_kb == 1024);
  1006. } else {
  1007. /*
  1008. * Didn't expect this, but the old unit test only checked some of them,
  1009. * so don't assert.
  1010. */
  1011. /* tt_assert(0); */
  1012. }
  1013. done:
  1014. return;
  1015. }
  1016. /**
  1017. * Test a consensus for v3_networkstatus_test
  1018. */
  1019. static void
  1020. test_consensus_for_v3ns(networkstatus_t *con, time_t now)
  1021. {
  1022. (void)now;
  1023. tt_assert(con);
  1024. tt_assert(!con->cert);
  1025. tt_int_op(2,==, smartlist_len(con->routerstatus_list));
  1026. /* There should be two listed routers: one with identity 3, one with
  1027. * identity 5. */
  1028. done:
  1029. return;
  1030. }
  1031. /**
  1032. * Test a router list entry for v3_networkstatus test
  1033. */
  1034. static void
  1035. test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
  1036. {
  1037. tor_addr_t addr_ipv6;
  1038. tt_assert(rs);
  1039. /* There should be two listed routers: one with identity 3, one with
  1040. * identity 5. */
  1041. /* This one showed up in 2 digests. */
  1042. if (tor_memeq(rs->identity_digest,
  1043. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1044. "\x3\x3",
  1045. DIGEST_LEN)) {
  1046. tt_mem_op(rs->identity_digest,==,
  1047. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1048. DIGEST_LEN);
  1049. tt_mem_op(rs->descriptor_digest,==, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1050. tt_assert(!rs->is_authority);
  1051. tt_assert(!rs->is_exit);
  1052. tt_assert(!rs->is_fast);
  1053. tt_assert(!rs->is_possible_guard);
  1054. tt_assert(!rs->is_stable);
  1055. /* (If it wasn't running it wouldn't be here) */
  1056. tt_assert(rs->is_flagged_running);
  1057. tt_assert(!rs->is_valid);
  1058. tt_assert(!rs->is_named);
  1059. /* XXXX check version */
  1060. } else if (tor_memeq(rs->identity_digest,
  1061. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1062. "\x5\x5\x5\x5",
  1063. DIGEST_LEN)) {
  1064. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  1065. tt_mem_op(rs->identity_digest,==,
  1066. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1067. DIGEST_LEN);
  1068. tt_str_op(rs->nickname,==, "router1");
  1069. tt_mem_op(rs->descriptor_digest,==, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1070. tt_int_op(rs->published_on,==, now-1000);
  1071. tt_int_op(rs->addr,==, 0x99009901);
  1072. tt_int_op(rs->or_port,==, 443);
  1073. tt_int_op(rs->dir_port,==, 0);
  1074. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1075. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1076. tt_int_op(rs->ipv6_orport,==, 4711);
  1077. tt_assert(!rs->is_authority);
  1078. tt_assert(rs->is_exit);
  1079. tt_assert(rs->is_fast);
  1080. tt_assert(rs->is_possible_guard);
  1081. tt_assert(rs->is_stable);
  1082. tt_assert(rs->is_flagged_running);
  1083. tt_assert(rs->is_valid);
  1084. tt_assert(!rs->is_named);
  1085. /* XXXX check version */
  1086. } else {
  1087. /* Weren't expecting this... */
  1088. tt_assert(0);
  1089. }
  1090. done:
  1091. return;
  1092. }
  1093. /** Run a unit tests for generating and parsing networkstatuses, with
  1094. * the supply test fns. */
  1095. static void
  1096. test_a_networkstatus(
  1097. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  1098. int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  1099. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  1100. void (*consensus_test)(networkstatus_t *con, time_t now),
  1101. void (*rs_test)(routerstatus_t *rs, time_t now))
  1102. {
  1103. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  1104. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  1105. crypto_pk_t *sign_skey_leg1=NULL;
  1106. const char *msg=NULL;
  1107. /*
  1108. * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
  1109. * returns non-zero, it changed net_params and we should skip the tests for
  1110. * that later as they will fail.
  1111. */
  1112. int params_tweaked = 0;
  1113. time_t now = time(NULL);
  1114. networkstatus_voter_info_t *voter;
  1115. document_signature_t *sig;
  1116. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  1117. *con_md=NULL;
  1118. vote_routerstatus_t *vrs;
  1119. routerstatus_t *rs;
  1120. int idx, n_rs, n_vrs;
  1121. char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL,
  1122. *cp=NULL;
  1123. smartlist_t *votes = smartlist_new();
  1124. /* For generating the two other consensuses. */
  1125. char *detached_text1=NULL, *detached_text2=NULL;
  1126. char *consensus_text2=NULL, *consensus_text3=NULL;
  1127. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  1128. char *consensus_text_md=NULL;
  1129. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  1130. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  1131. tt_assert(vrs_gen);
  1132. tt_assert(rs_test);
  1133. tt_assert(vrs_test);
  1134. /* Parse certificates and keys. */
  1135. cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  1136. tt_assert(cert1);
  1137. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  1138. tt_assert(cert2);
  1139. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  1140. tt_assert(cert3);
  1141. sign_skey_1 = crypto_pk_new();
  1142. sign_skey_2 = crypto_pk_new();
  1143. sign_skey_3 = crypto_pk_new();
  1144. sign_skey_leg1 = pk_generate(4);
  1145. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  1146. AUTHORITY_SIGNKEY_1, -1));
  1147. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  1148. AUTHORITY_SIGNKEY_2, -1));
  1149. tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  1150. AUTHORITY_SIGNKEY_3, -1));
  1151. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  1152. tt_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  1153. /*
  1154. * Set up a vote; generate it; try to parse it.
  1155. */
  1156. vote = tor_malloc_zero(sizeof(networkstatus_t));
  1157. vote->type = NS_TYPE_VOTE;
  1158. vote->published = now;
  1159. vote->valid_after = now+1000;
  1160. vote->fresh_until = now+2000;
  1161. vote->valid_until = now+3000;
  1162. vote->vote_seconds = 100;
  1163. vote->dist_seconds = 200;
  1164. vote->supported_methods = smartlist_new();
  1165. smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
  1166. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  1167. vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  1168. vote->known_flags = smartlist_new();
  1169. smartlist_split_string(vote->known_flags,
  1170. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  1171. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1172. vote->voters = smartlist_new();
  1173. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  1174. voter->nickname = tor_strdup("Voter1");
  1175. voter->address = tor_strdup("1.2.3.4");
  1176. voter->addr = 0x01020304;
  1177. voter->dir_port = 80;
  1178. voter->or_port = 9000;
  1179. voter->contact = tor_strdup("voter@example.com");
  1180. crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
  1181. smartlist_add(vote->voters, voter);
  1182. vote->cert = authority_cert_dup(cert1);
  1183. vote->net_params = smartlist_new();
  1184. smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
  1185. NULL, 0, 0);
  1186. vote->routerstatus_list = smartlist_new();
  1187. /* add routerstatuses */
  1188. idx = 0;
  1189. do {
  1190. vrs = vrs_gen(idx, now);
  1191. if (vrs) {
  1192. smartlist_add(vote->routerstatus_list, vrs);
  1193. tt_assert(router_add_to_routerlist(generate_ri_from_rs(vrs),
  1194. &msg,0,0)>=0);
  1195. ++idx;
  1196. }
  1197. } while (vrs);
  1198. n_vrs = idx;
  1199. /* dump the vote and try to parse it. */
  1200. v1_text = format_networkstatus_vote(sign_skey_1, vote);
  1201. tt_assert(v1_text);
  1202. v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
  1203. tt_assert(v1);
  1204. /* Make sure the parsed thing was right. */
  1205. tt_int_op(v1->type,==, NS_TYPE_VOTE);
  1206. tt_int_op(v1->published,==, vote->published);
  1207. tt_int_op(v1->valid_after,==, vote->valid_after);
  1208. tt_int_op(v1->fresh_until,==, vote->fresh_until);
  1209. tt_int_op(v1->valid_until,==, vote->valid_until);
  1210. tt_int_op(v1->vote_seconds,==, vote->vote_seconds);
  1211. tt_int_op(v1->dist_seconds,==, vote->dist_seconds);
  1212. tt_str_op(v1->client_versions,==, vote->client_versions);
  1213. tt_str_op(v1->server_versions,==, vote->server_versions);
  1214. tt_assert(v1->voters && smartlist_len(v1->voters));
  1215. voter = smartlist_get(v1->voters, 0);
  1216. tt_str_op(voter->nickname,==, "Voter1");
  1217. tt_str_op(voter->address,==, "1.2.3.4");
  1218. tt_int_op(voter->addr,==, 0x01020304);
  1219. tt_int_op(voter->dir_port,==, 80);
  1220. tt_int_op(voter->or_port,==, 9000);
  1221. tt_str_op(voter->contact,==, "voter@example.com");
  1222. tt_assert(v1->cert);
  1223. tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  1224. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  1225. tt_str_op(cp,==, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  1226. tor_free(cp);
  1227. tt_int_op(smartlist_len(v1->routerstatus_list),==, n_vrs);
  1228. if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
  1229. /* Check the routerstatuses. */
  1230. for (idx = 0; idx < n_vrs; ++idx) {
  1231. vrs = smartlist_get(v1->routerstatus_list, idx);
  1232. tt_assert(vrs);
  1233. vrs_test(vrs, 1, now);
  1234. }
  1235. /* Generate second vote. It disagrees on some of the times,
  1236. * and doesn't list versions, and knows some crazy flags */
  1237. vote->published = now+1;
  1238. vote->fresh_until = now+3005;
  1239. vote->dist_seconds = 300;
  1240. authority_cert_free(vote->cert);
  1241. vote->cert = authority_cert_dup(cert2);
  1242. SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
  1243. smartlist_clear(vote->net_params);
  1244. smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
  1245. NULL, 0, 0);
  1246. tor_free(vote->client_versions);
  1247. tor_free(vote->server_versions);
  1248. voter = smartlist_get(vote->voters, 0);
  1249. tor_free(voter->nickname);
  1250. tor_free(voter->address);
  1251. voter->nickname = tor_strdup("Voter2");
  1252. voter->address = tor_strdup("2.3.4.5");
  1253. voter->addr = 0x02030405;
  1254. crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
  1255. smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
  1256. smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
  1257. smartlist_sort_strings(vote->known_flags);
  1258. /* generate and parse v2. */
  1259. v2_text = format_networkstatus_vote(sign_skey_2, vote);
  1260. tt_assert(v2_text);
  1261. v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
  1262. tt_assert(v2);
  1263. if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
  1264. /* Check that flags come out right.*/
  1265. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1266. tt_str_op(cp,==, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1267. "Running:Stable:V2Dir:Valid");
  1268. tor_free(cp);
  1269. /* Check the routerstatuses. */
  1270. n_vrs = smartlist_len(v2->routerstatus_list);
  1271. for (idx = 0; idx < n_vrs; ++idx) {
  1272. vrs = smartlist_get(v2->routerstatus_list, idx);
  1273. tt_assert(vrs);
  1274. vrs_test(vrs, 2, now);
  1275. }
  1276. /* Generate the third vote. */
  1277. vote->published = now;
  1278. vote->fresh_until = now+2003;
  1279. vote->dist_seconds = 250;
  1280. authority_cert_free(vote->cert);
  1281. vote->cert = authority_cert_dup(cert3);
  1282. SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
  1283. smartlist_clear(vote->net_params);
  1284. smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
  1285. NULL, 0, 0);
  1286. smartlist_add(vote->supported_methods, tor_strdup("4"));
  1287. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
  1288. vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
  1289. voter = smartlist_get(vote->voters, 0);
  1290. tor_free(voter->nickname);
  1291. tor_free(voter->address);
  1292. voter->nickname = tor_strdup("Voter3");
  1293. voter->address = tor_strdup("3.4.5.6");
  1294. voter->addr = 0x03040506;
  1295. crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
  1296. /* This one has a legacy id. */
  1297. memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
  1298. v3_text = format_networkstatus_vote(sign_skey_3, vote);
  1299. tt_assert(v3_text);
  1300. v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
  1301. tt_assert(v3);
  1302. if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
  1303. /* Compute a consensus as voter 3. */
  1304. smartlist_add(votes, v3);
  1305. smartlist_add(votes, v1);
  1306. smartlist_add(votes, v2);
  1307. consensus_text = networkstatus_compute_consensus(votes, 3,
  1308. cert3->identity_key,
  1309. sign_skey_3,
  1310. "AAAAAAAAAAAAAAAAAAAA",
  1311. sign_skey_leg1,
  1312. FLAV_NS);
  1313. tt_assert(consensus_text);
  1314. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  1315. NS_TYPE_CONSENSUS);
  1316. tt_assert(con);
  1317. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1318. // v1_text, v2_text, v3_text);
  1319. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1320. cert3->identity_key,
  1321. sign_skey_3,
  1322. "AAAAAAAAAAAAAAAAAAAA",
  1323. sign_skey_leg1,
  1324. FLAV_MICRODESC);
  1325. tt_assert(consensus_text_md);
  1326. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1327. NS_TYPE_CONSENSUS);
  1328. tt_assert(con_md);
  1329. tt_int_op(con_md->flavor,==, FLAV_MICRODESC);
  1330. /* Check consensus contents. */
  1331. tt_assert(con->type == NS_TYPE_CONSENSUS);
  1332. tt_int_op(con->published,==, 0); /* this field only appears in votes. */
  1333. tt_int_op(con->valid_after,==, now+1000);
  1334. tt_int_op(con->fresh_until,==, now+2003); /* median */
  1335. tt_int_op(con->valid_until,==, now+3000);
  1336. tt_int_op(con->vote_seconds,==, 100);
  1337. tt_int_op(con->dist_seconds,==, 250); /* median */
  1338. tt_str_op(con->client_versions,==, "0.1.2.14");
  1339. tt_str_op(con->server_versions,==, "0.1.2.15,0.1.2.16");
  1340. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1341. tt_str_op(cp,==, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1342. "Running:Stable:V2Dir:Valid");
  1343. tor_free(cp);
  1344. if (!params_tweaked) {
  1345. /* Skip this one if vote_tweaks() messed with the param lists */
  1346. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1347. tt_str_op(cp,==, "circuitwindow=80:foo=660");
  1348. tor_free(cp);
  1349. }
  1350. tt_int_op(4,==, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1351. /* The voter id digests should be in this order. */
  1352. tt_assert(memcmp(cert2->cache_info.identity_digest,
  1353. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1354. tt_assert(memcmp(cert1->cache_info.identity_digest,
  1355. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1356. test_same_voter(smartlist_get(con->voters, 1),
  1357. smartlist_get(v2->voters, 0));
  1358. test_same_voter(smartlist_get(con->voters, 2),
  1359. smartlist_get(v1->voters, 0));
  1360. test_same_voter(smartlist_get(con->voters, 3),
  1361. smartlist_get(v3->voters, 0));
  1362. consensus_test(con, now);
  1363. /* Check the routerstatuses. */
  1364. n_rs = smartlist_len(con->routerstatus_list);
  1365. for (idx = 0; idx < n_rs; ++idx) {
  1366. rs = smartlist_get(con->routerstatus_list, idx);
  1367. tt_assert(rs);
  1368. rs_test(rs, now);
  1369. }
  1370. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1371. * The second one hasn't signed. The fourth one has signed: validate it. */
  1372. voter = smartlist_get(con->voters, 1);
  1373. tt_int_op(smartlist_len(voter->sigs),==, 0);
  1374. voter = smartlist_get(con->voters, 3);
  1375. tt_int_op(smartlist_len(voter->sigs),==, 1);
  1376. sig = smartlist_get(voter->sigs, 0);
  1377. tt_assert(sig->signature);
  1378. tt_assert(!sig->good_signature);
  1379. tt_assert(!sig->bad_signature);
  1380. tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1381. tt_assert(sig->signature);
  1382. tt_assert(sig->good_signature);
  1383. tt_assert(!sig->bad_signature);
  1384. {
  1385. const char *msg=NULL;
  1386. /* Compute the other two signed consensuses. */
  1387. smartlist_shuffle(votes);
  1388. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1389. cert2->identity_key,
  1390. sign_skey_2, NULL,NULL,
  1391. FLAV_NS);
  1392. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1393. cert2->identity_key,
  1394. sign_skey_2, NULL,NULL,
  1395. FLAV_MICRODESC);
  1396. smartlist_shuffle(votes);
  1397. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1398. cert1->identity_key,
  1399. sign_skey_1, NULL,NULL,
  1400. FLAV_NS);
  1401. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1402. cert1->identity_key,
  1403. sign_skey_1, NULL,NULL,
  1404. FLAV_MICRODESC);
  1405. tt_assert(consensus_text2);
  1406. tt_assert(consensus_text3);
  1407. tt_assert(consensus_text_md2);
  1408. tt_assert(consensus_text_md3);
  1409. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1410. NS_TYPE_CONSENSUS);
  1411. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1412. NS_TYPE_CONSENSUS);
  1413. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1414. NS_TYPE_CONSENSUS);
  1415. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1416. NS_TYPE_CONSENSUS);
  1417. tt_assert(con2);
  1418. tt_assert(con3);
  1419. tt_assert(con_md2);
  1420. tt_assert(con_md3);
  1421. /* All three should have the same digest. */
  1422. tt_mem_op(&con->digests,==, &con2->digests, sizeof(digests_t));
  1423. tt_mem_op(&con->digests,==, &con3->digests, sizeof(digests_t));
  1424. tt_mem_op(&con_md->digests,==, &con_md2->digests, sizeof(digests_t));
  1425. tt_mem_op(&con_md->digests,==, &con_md3->digests, sizeof(digests_t));
  1426. /* Extract a detached signature from con3. */
  1427. detached_text1 = get_detached_sigs(con3, con_md3);
  1428. tt_assert(detached_text1);
  1429. /* Try to parse it. */
  1430. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1431. tt_assert(dsig1);
  1432. /* Are parsed values as expected? */
  1433. tt_int_op(dsig1->valid_after,==, con3->valid_after);
  1434. tt_int_op(dsig1->fresh_until,==, con3->fresh_until);
  1435. tt_int_op(dsig1->valid_until,==, con3->valid_until);
  1436. {
  1437. digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1438. tt_assert(dsig_digests);
  1439. tt_mem_op(dsig_digests->d[DIGEST_SHA1],==, con3->digests.d[DIGEST_SHA1],
  1440. DIGEST_LEN);
  1441. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1442. tt_assert(dsig_digests);
  1443. tt_mem_op(dsig_digests->d[DIGEST_SHA256],==,
  1444. con_md3->digests.d[DIGEST_SHA256],
  1445. DIGEST256_LEN);
  1446. }
  1447. {
  1448. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1449. tt_assert(dsig_signatures);
  1450. tt_int_op(1,==, smartlist_len(dsig_signatures));
  1451. sig = smartlist_get(dsig_signatures, 0);
  1452. tt_mem_op(sig->identity_digest,==, cert1->cache_info.identity_digest,
  1453. DIGEST_LEN);
  1454. tt_int_op(sig->alg,==, DIGEST_SHA1);
  1455. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1456. tt_assert(dsig_signatures);
  1457. tt_int_op(1,==, smartlist_len(dsig_signatures));
  1458. sig = smartlist_get(dsig_signatures, 0);
  1459. tt_mem_op(sig->identity_digest,==, cert1->cache_info.identity_digest,
  1460. DIGEST_LEN);
  1461. tt_int_op(sig->alg,==, DIGEST_SHA256);
  1462. }
  1463. /* Try adding it to con2. */
  1464. detached_text2 = get_detached_sigs(con2,con_md2);
  1465. tt_int_op(1,==, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1466. LOG_INFO, &msg));
  1467. tor_free(detached_text2);
  1468. tt_int_op(1,==,
  1469. networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1470. LOG_INFO, &msg));
  1471. tor_free(detached_text2);
  1472. detached_text2 = get_detached_sigs(con2,con_md2);
  1473. //printf("\n<%s>\n", detached_text2);
  1474. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1475. tt_assert(dsig2);
  1476. /*
  1477. printf("\n");
  1478. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1479. char hd[64];
  1480. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1481. printf("%s\n", hd);
  1482. });
  1483. */
  1484. tt_int_op(2,==,
  1485. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1486. tt_int_op(2,==,
  1487. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  1488. "microdesc")));
  1489. /* Try adding to con2 twice; verify that nothing changes. */
  1490. tt_int_op(0,==, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1491. LOG_INFO, &msg));
  1492. /* Add to con. */
  1493. tt_int_op(2,==, networkstatus_add_detached_signatures(con, dsig2, "test",
  1494. LOG_INFO, &msg));
  1495. /* Check signatures */
  1496. voter = smartlist_get(con->voters, 1);
  1497. sig = smartlist_get(voter->sigs, 0);
  1498. tt_assert(sig);
  1499. tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
  1500. voter = smartlist_get(con->voters, 2);
  1501. sig = smartlist_get(voter->sigs, 0);
  1502. tt_assert(sig);
  1503. tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
  1504. }
  1505. done:
  1506. tor_free(cp);
  1507. smartlist_free(votes);
  1508. tor_free(v1_text);
  1509. tor_free(v2_text);
  1510. tor_free(v3_text);
  1511. tor_free(consensus_text);
  1512. tor_free(consensus_text_md);
  1513. if (vote)
  1514. networkstatus_vote_free(vote);
  1515. if (v1)
  1516. networkstatus_vote_free(v1);
  1517. if (v2)
  1518. networkstatus_vote_free(v2);
  1519. if (v3)
  1520. networkstatus_vote_free(v3);
  1521. if (con)
  1522. networkstatus_vote_free(con);
  1523. if (con_md)
  1524. networkstatus_vote_free(con_md);
  1525. if (sign_skey_1)
  1526. crypto_pk_free(sign_skey_1);
  1527. if (sign_skey_2)
  1528. crypto_pk_free(sign_skey_2);
  1529. if (sign_skey_3)
  1530. crypto_pk_free(sign_skey_3);
  1531. if (sign_skey_leg1)
  1532. crypto_pk_free(sign_skey_leg1);
  1533. if (cert1)
  1534. authority_cert_free(cert1);
  1535. if (cert2)
  1536. authority_cert_free(cert2);
  1537. if (cert3)
  1538. authority_cert_free(cert3);
  1539. tor_free(consensus_text2);
  1540. tor_free(consensus_text3);
  1541. tor_free(consensus_text_md2);
  1542. tor_free(consensus_text_md3);
  1543. tor_free(detached_text1);
  1544. tor_free(detached_text2);
  1545. if (con2)
  1546. networkstatus_vote_free(con2);
  1547. if (con3)
  1548. networkstatus_vote_free(con3);
  1549. if (con_md2)
  1550. networkstatus_vote_free(con_md2);
  1551. if (con_md3)
  1552. networkstatus_vote_free(con_md3);
  1553. if (dsig1)
  1554. ns_detached_signatures_free(dsig1);
  1555. if (dsig2)
  1556. ns_detached_signatures_free(dsig2);
  1557. }
  1558. /** Run unit tests for generating and parsing V3 consensus networkstatus
  1559. * documents. */
  1560. static void
  1561. test_dir_v3_networkstatus(void *arg)
  1562. {
  1563. (void)arg;
  1564. test_a_networkstatus(gen_routerstatus_for_v3ns,
  1565. vote_tweaks_for_v3ns,
  1566. test_vrs_for_v3ns,
  1567. test_consensus_for_v3ns,
  1568. test_routerstatus_for_v3ns);
  1569. }
  1570. static void
  1571. test_dir_scale_bw(void *testdata)
  1572. {
  1573. double v[8] = { 2.0/3,
  1574. 7.0,
  1575. 1.0,
  1576. 3.0,
  1577. 1.0/5,
  1578. 1.0/7,
  1579. 12.0,
  1580. 24.0 };
  1581. u64_dbl_t vals[8];
  1582. uint64_t total;
  1583. int i;
  1584. (void) testdata;
  1585. for (i=0; i<8; ++i)
  1586. vals[i].dbl = v[i];
  1587. scale_array_elements_to_u64(vals, 8, &total);
  1588. tt_int_op((int)total, ==, 48);
  1589. total = 0;
  1590. for (i=0; i<8; ++i) {
  1591. total += vals[i].u64;
  1592. }
  1593. tt_assert(total >= (U64_LITERAL(1)<<60));
  1594. tt_assert(total <= (U64_LITERAL(1)<<62));
  1595. for (i=0; i<8; ++i) {
  1596. /* vals[2].u64 is the scaled value of 1.0 */
  1597. double ratio = ((double)vals[i].u64) / vals[2].u64;
  1598. tt_double_op(fabs(ratio - v[i]), <, .00001);
  1599. }
  1600. /* test handling of no entries */
  1601. total = 1;
  1602. scale_array_elements_to_u64(vals, 0, &total);
  1603. tt_assert(total == 0);
  1604. /* make sure we don't read the array when we have no entries
  1605. * may require compiler flags to catch NULL dereferences */
  1606. total = 1;
  1607. scale_array_elements_to_u64(NULL, 0, &total);
  1608. tt_assert(total == 0);
  1609. scale_array_elements_to_u64(NULL, 0, NULL);
  1610. /* test handling of zero totals */
  1611. total = 1;
  1612. vals[0].dbl = 0.0;
  1613. scale_array_elements_to_u64(vals, 1, &total);
  1614. tt_assert(total == 0);
  1615. tt_assert(vals[0].u64 == 0);
  1616. vals[0].dbl = 0.0;
  1617. vals[1].dbl = 0.0;
  1618. scale_array_elements_to_u64(vals, 2, NULL);
  1619. tt_assert(vals[0].u64 == 0);
  1620. tt_assert(vals[1].u64 == 0);
  1621. done:
  1622. ;
  1623. }
  1624. static void
  1625. test_dir_random_weighted(void *testdata)
  1626. {
  1627. int histogram[10];
  1628. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  1629. u64_dbl_t inp[10];
  1630. int i, choice;
  1631. const int n = 50000;
  1632. double max_sq_error;
  1633. (void) testdata;
  1634. /* Try a ten-element array with values from 0 through 10. The values are
  1635. * in a scrambled order to make sure we don't depend on order. */
  1636. memset(histogram,0,sizeof(histogram));
  1637. for (i=0; i<10; ++i) {
  1638. inp[i].u64 = vals[i];
  1639. total += vals[i];
  1640. }
  1641. tt_u64_op(total, ==, 45);
  1642. for (i=0; i<n; ++i) {
  1643. choice = choose_array_element_by_weight(inp, 10);
  1644. tt_int_op(choice, >=, 0);
  1645. tt_int_op(choice, <, 10);
  1646. histogram[choice]++;
  1647. }
  1648. /* Now see if we chose things about frequently enough. */
  1649. max_sq_error = 0;
  1650. for (i=0; i<10; ++i) {
  1651. int expected = (int)(n*vals[i]/total);
  1652. double frac_diff = 0, sq;
  1653. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1654. if (expected)
  1655. frac_diff = (histogram[i] - expected) / ((double)expected);
  1656. else
  1657. tt_int_op(histogram[i], ==, 0);
  1658. sq = frac_diff * frac_diff;
  1659. if (sq > max_sq_error)
  1660. max_sq_error = sq;
  1661. }
  1662. /* It should almost always be much much less than this. If you want to
  1663. * figure out the odds, please feel free. */
  1664. tt_double_op(max_sq_error, <, .05);
  1665. /* Now try a singleton; do we choose it? */
  1666. for (i = 0; i < 100; ++i) {
  1667. choice = choose_array_element_by_weight(inp, 1);
  1668. tt_int_op(choice, ==, 0);
  1669. }
  1670. /* Now try an array of zeros. We should choose randomly. */
  1671. memset(histogram,0,sizeof(histogram));
  1672. for (i = 0; i < 5; ++i)
  1673. inp[i].u64 = 0;
  1674. for (i = 0; i < n; ++i) {
  1675. choice = choose_array_element_by_weight(inp, 5);
  1676. tt_int_op(choice, >=, 0);
  1677. tt_int_op(choice, <, 5);
  1678. histogram[choice]++;
  1679. }
  1680. /* Now see if we chose things about frequently enough. */
  1681. max_sq_error = 0;
  1682. for (i=0; i<5; ++i) {
  1683. int expected = n/5;
  1684. double frac_diff = 0, sq;
  1685. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1686. frac_diff = (histogram[i] - expected) / ((double)expected);
  1687. sq = frac_diff * frac_diff;
  1688. if (sq > max_sq_error)
  1689. max_sq_error = sq;
  1690. }
  1691. /* It should almost always be much much less than this. If you want to
  1692. * figure out the odds, please feel free. */
  1693. tt_double_op(max_sq_error, <, .05);
  1694. done:
  1695. ;
  1696. }
  1697. /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
  1698. static uint32_t alternate_clip_bw = 0;
  1699. /**
  1700. * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
  1701. * v3_networkstatus ones.
  1702. */
  1703. static vote_routerstatus_t *
  1704. gen_routerstatus_for_umbw(int idx, time_t now)
  1705. {
  1706. vote_routerstatus_t *vrs = NULL;
  1707. routerstatus_t *rs;
  1708. tor_addr_t addr_ipv6;
  1709. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  1710. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  1711. switch (idx) {
  1712. case 0:
  1713. /* Generate the first routerstatus. */
  1714. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1715. rs = &vrs->status;
  1716. vrs->version = tor_strdup("0.1.2.14");
  1717. rs->published_on = now-1500;
  1718. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  1719. memset(rs->identity_digest, 3, DIGEST_LEN);
  1720. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  1721. rs->addr = 0x99008801;
  1722. rs->or_port = 443;
  1723. rs->dir_port = 8000;
  1724. /* all flags but running cleared */
  1725. rs->is_flagged_running = 1;
  1726. /*
  1727. * This one has measured bandwidth below the clip cutoff, and
  1728. * so shouldn't be clipped; we'll have to test that it isn't
  1729. * later.
  1730. */
  1731. vrs->has_measured_bw = 1;
  1732. rs->has_bandwidth = 1;
  1733. vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  1734. break;
  1735. case 1:
  1736. /* Generate the second routerstatus. */
  1737. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1738. rs = &vrs->status;
  1739. vrs->version = tor_strdup("0.2.0.5");
  1740. rs->published_on = now-1000;
  1741. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  1742. memset(rs->identity_digest, 5, DIGEST_LEN);
  1743. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  1744. rs->addr = 0x99009901;
  1745. rs->or_port = 443;
  1746. rs->dir_port = 0;
  1747. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1748. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  1749. rs->ipv6_orport = 4711;
  1750. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  1751. rs->is_valid = rs->is_possible_guard = 1;
  1752. /*
  1753. * This one has measured bandwidth above the clip cutoff, and
  1754. * so shouldn't be clipped; we'll have to test that it isn't
  1755. * later.
  1756. */
  1757. vrs->has_measured_bw = 1;
  1758. rs->has_bandwidth = 1;
  1759. vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  1760. break;
  1761. case 2:
  1762. /* Generate the third routerstatus. */
  1763. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1764. rs = &vrs->status;
  1765. vrs->version = tor_strdup("0.1.0.3");
  1766. rs->published_on = now-1000;
  1767. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  1768. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  1769. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  1770. rs->addr = 0xAA009901;
  1771. rs->or_port = 400;
  1772. rs->dir_port = 9999;
  1773. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  1774. rs->is_flagged_running = rs->is_valid =
  1775. rs->is_possible_guard = 1;
  1776. /*
  1777. * This one has unmeasured bandwidth above the clip cutoff, and
  1778. * so should be clipped; we'll have to test that it isn't
  1779. * later.
  1780. */
  1781. vrs->has_measured_bw = 0;
  1782. rs->has_bandwidth = 1;
  1783. vrs->measured_bw_kb = 0;
  1784. rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
  1785. break;
  1786. case 3:
  1787. /* Generate a fourth routerstatus that is not running. */
  1788. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1789. rs = &vrs->status;
  1790. vrs->version = tor_strdup("0.1.6.3");
  1791. rs->published_on = now-1000;
  1792. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  1793. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  1794. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  1795. rs->addr = 0xC0000203;
  1796. rs->or_port = 500;
  1797. rs->dir_port = 1999;
  1798. /* all flags but running cleared */
  1799. rs->is_flagged_running = 1;
  1800. /*
  1801. * This one has unmeasured bandwidth below the clip cutoff, and
  1802. * so shouldn't be clipped; we'll have to test that it isn't
  1803. * later.
  1804. */
  1805. vrs->has_measured_bw = 0;
  1806. rs->has_bandwidth = 1;
  1807. vrs->measured_bw_kb = 0;
  1808. rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
  1809. break;
  1810. case 4:
  1811. /* No more for this test; return NULL */
  1812. vrs = NULL;
  1813. break;
  1814. default:
  1815. /* Shouldn't happen */
  1816. tt_assert(0);
  1817. }
  1818. if (vrs) {
  1819. vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  1820. tor_asprintf(&vrs->microdesc->microdesc_hash_line,
  1821. "m 9,10,11,12,13,14,15,16,17 "
  1822. "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
  1823. idx);
  1824. }
  1825. done:
  1826. return vrs;
  1827. }
  1828. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  1829. * just adding the right consensus methods to let clipping happen */
  1830. static int
  1831. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  1832. {
  1833. char *maxbw_param = NULL;
  1834. int rv = 0;
  1835. tt_assert(v);
  1836. (void)voter;
  1837. (void)now;
  1838. tt_assert(v->supported_methods);
  1839. SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
  1840. smartlist_clear(v->supported_methods);
  1841. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
  1842. smartlist_split_string(v->supported_methods,
  1843. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
  1844. NULL, 0, -1);
  1845. /* If we're using a non-default clip bandwidth, add it to net_params */
  1846. if (alternate_clip_bw > 0) {
  1847. tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
  1848. tt_assert(maxbw_param);
  1849. if (maxbw_param) {
  1850. smartlist_add(v->net_params, maxbw_param);
  1851. rv = 1;
  1852. }
  1853. }
  1854. done:
  1855. return rv;
  1856. }
  1857. /**
  1858. * Test a parsed vote_routerstatus_t for umbw test.
  1859. */
  1860. static void
  1861. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  1862. {
  1863. routerstatus_t *rs;
  1864. tor_addr_t addr_ipv6;
  1865. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  1866. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  1867. (void)voter;
  1868. tt_assert(vrs);
  1869. rs = &(vrs->status);
  1870. tt_assert(rs);
  1871. /* Split out by digests to test */
  1872. if (tor_memeq(rs->identity_digest,
  1873. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1874. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1875. DIGEST_LEN)) {
  1876. /*
  1877. * Check the first routerstatus - measured bandwidth below the clip
  1878. * cutoff.
  1879. */
  1880. tt_str_op(vrs->version,==, "0.1.2.14");
  1881. tt_int_op(rs->published_on,==, now-1500);
  1882. tt_str_op(rs->nickname,==, "router2");
  1883. tt_mem_op(rs->identity_digest,==,
  1884. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1885. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1886. DIGEST_LEN);
  1887. tt_mem_op(rs->descriptor_digest,==, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1888. tt_int_op(rs->addr,==, 0x99008801);
  1889. tt_int_op(rs->or_port,==, 443);
  1890. tt_int_op(rs->dir_port,==, 8000);
  1891. tt_assert(rs->has_bandwidth);
  1892. tt_assert(vrs->has_measured_bw);
  1893. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb / 2);
  1894. tt_int_op(vrs->measured_bw_kb,==, max_unmeasured_bw_kb / 2);
  1895. } else if (tor_memeq(rs->identity_digest,
  1896. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1897. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1898. DIGEST_LEN)) {
  1899. /*
  1900. * Check the second routerstatus - measured bandwidth above the clip
  1901. * cutoff.
  1902. */
  1903. tt_str_op(vrs->version,==, "0.2.0.5");
  1904. tt_int_op(rs->published_on,==, now-1000);
  1905. tt_str_op(rs->nickname,==, "router1");
  1906. tt_mem_op(rs->identity_digest,==,
  1907. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1908. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1909. DIGEST_LEN);
  1910. tt_mem_op(rs->descriptor_digest,==, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1911. tt_int_op(rs->addr,==, 0x99009901);
  1912. tt_int_op(rs->or_port,==, 443);
  1913. tt_int_op(rs->dir_port,==, 0);
  1914. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1915. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1916. tt_int_op(rs->ipv6_orport,==, 4711);
  1917. tt_assert(rs->has_bandwidth);
  1918. tt_assert(vrs->has_measured_bw);
  1919. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb * 2);
  1920. tt_int_op(vrs->measured_bw_kb,==, max_unmeasured_bw_kb * 2);
  1921. } else if (tor_memeq(rs->identity_digest,
  1922. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1923. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1924. DIGEST_LEN)) {
  1925. /*
  1926. * Check the third routerstatus - unmeasured bandwidth above the clip
  1927. * cutoff; this one should be clipped later on in the consensus, but
  1928. * appears unclipped in the vote.
  1929. */
  1930. tt_assert(rs->has_bandwidth);
  1931. tt_assert(!(vrs->has_measured_bw));
  1932. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb * 2);
  1933. tt_int_op(vrs->measured_bw_kb,==, 0);
  1934. } else if (tor_memeq(rs->identity_digest,
  1935. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  1936. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  1937. DIGEST_LEN)) {
  1938. /*
  1939. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  1940. * cutoff; this one should not be clipped.
  1941. */
  1942. tt_assert(rs->has_bandwidth);
  1943. tt_assert(!(vrs->has_measured_bw));
  1944. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb / 2);
  1945. tt_int_op(vrs->measured_bw_kb,==, 0);
  1946. } else {
  1947. tt_assert(0);
  1948. }
  1949. done:
  1950. return;
  1951. }
  1952. /**
  1953. * Test a consensus for v3_networkstatus_test
  1954. */
  1955. static void
  1956. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  1957. {
  1958. (void)now;
  1959. tt_assert(con);
  1960. tt_assert(!con->cert);
  1961. // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
  1962. tt_assert(con->consensus_method >= 16);
  1963. tt_int_op(4,==, smartlist_len(con->routerstatus_list));
  1964. /* There should be four listed routers; all voters saw the same in this */
  1965. done:
  1966. return;
  1967. }
  1968. /**
  1969. * Test a router list entry for umbw test
  1970. */
  1971. static void
  1972. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  1973. {
  1974. tor_addr_t addr_ipv6;
  1975. uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
  1976. alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
  1977. tt_assert(rs);
  1978. /* There should be four listed routers, as constructed above */
  1979. if (tor_memeq(rs->identity_digest,
  1980. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1981. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1982. DIGEST_LEN)) {
  1983. tt_mem_op(rs->identity_digest,==,
  1984. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1985. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1986. DIGEST_LEN);
  1987. tt_mem_op(rs->descriptor_digest,==, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1988. tt_assert(!rs->is_authority);
  1989. tt_assert(!rs->is_exit);
  1990. tt_assert(!rs->is_fast);
  1991. tt_assert(!rs->is_possible_guard);
  1992. tt_assert(!rs->is_stable);
  1993. /* (If it wasn't running it wouldn't be here) */
  1994. tt_assert(rs->is_flagged_running);
  1995. tt_assert(!rs->is_valid);
  1996. tt_assert(!rs->is_named);
  1997. /* This one should have measured bandwidth below the clip cutoff */
  1998. tt_assert(rs->has_bandwidth);
  1999. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb / 2);
  2000. tt_assert(!(rs->bw_is_unmeasured));
  2001. } else if (tor_memeq(rs->identity_digest,
  2002. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2003. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2004. DIGEST_LEN)) {
  2005. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  2006. tt_mem_op(rs->identity_digest,==,
  2007. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  2008. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  2009. DIGEST_LEN);
  2010. tt_str_op(rs->nickname,==, "router1");
  2011. tt_mem_op(rs->descriptor_digest,==, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  2012. tt_int_op(rs->published_on,==, now-1000);
  2013. tt_int_op(rs->addr,==, 0x99009901);
  2014. tt_int_op(rs->or_port,==, 443);
  2015. tt_int_op(rs->dir_port,==, 0);
  2016. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  2017. tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  2018. tt_int_op(rs->ipv6_orport,==, 4711);
  2019. tt_assert(!rs->is_authority);
  2020. tt_assert(rs->is_exit);
  2021. tt_assert(rs->is_fast);
  2022. tt_assert(rs->is_possible_guard);
  2023. tt_assert(rs->is_stable);
  2024. tt_assert(rs->is_flagged_running);
  2025. tt_assert(rs->is_valid);
  2026. tt_assert(!rs->is_named);
  2027. /* This one should have measured bandwidth above the clip cutoff */
  2028. tt_assert(rs->has_bandwidth);
  2029. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb * 2);
  2030. tt_assert(!(rs->bw_is_unmeasured));
  2031. } else if (tor_memeq(rs->identity_digest,
  2032. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  2033. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  2034. DIGEST_LEN)) {
  2035. /*
  2036. * This one should have unmeasured bandwidth above the clip cutoff,
  2037. * and so should be clipped
  2038. */
  2039. tt_assert(rs->has_bandwidth);
  2040. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb);
  2041. tt_assert(rs->bw_is_unmeasured);
  2042. } else if (tor_memeq(rs->identity_digest,
  2043. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  2044. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  2045. DIGEST_LEN)) {
  2046. /*
  2047. * This one should have unmeasured bandwidth below the clip cutoff,
  2048. * and so should not be clipped
  2049. */
  2050. tt_assert(rs->has_bandwidth);
  2051. tt_int_op(rs->bandwidth_kb,==, max_unmeasured_bw_kb / 2);
  2052. tt_assert(rs->bw_is_unmeasured);
  2053. } else {
  2054. /* Weren't expecting this... */
  2055. tt_assert(0);
  2056. }
  2057. done:
  2058. return;
  2059. }
  2060. /**
  2061. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  2062. * method 17; this uses the same test_a_networkstatus() function that the
  2063. * v3_networkstatus test uses.
  2064. */
  2065. static void
  2066. test_dir_clip_unmeasured_bw_kb(void *arg)
  2067. {
  2068. /* Run the test with the default clip bandwidth */
  2069. (void)arg;
  2070. alternate_clip_bw = 0;
  2071. test_a_networkstatus(gen_routerstatus_for_umbw,
  2072. vote_tweaks_for_umbw,
  2073. test_vrs_for_umbw,
  2074. test_consensus_for_umbw,
  2075. test_routerstatus_for_umbw);
  2076. }
  2077. /**
  2078. * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
  2079. * of clip bandwidth.
  2080. */
  2081. static void
  2082. test_dir_clip_unmeasured_bw_kb_alt(void *arg)
  2083. {
  2084. /*
  2085. * Try a different one; this value is chosen so that the below-the-cutoff
  2086. * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
  2087. * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
  2088. * cutoff it will fail the test.
  2089. */
  2090. (void)arg;
  2091. alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
  2092. test_a_networkstatus(gen_routerstatus_for_umbw,
  2093. vote_tweaks_for_umbw,
  2094. test_vrs_for_umbw,
  2095. test_consensus_for_umbw,
  2096. test_routerstatus_for_umbw);
  2097. }
  2098. static void
  2099. test_dir_fmt_control_ns(void *arg)
  2100. {
  2101. char *s = NULL;
  2102. routerstatus_t rs;
  2103. (void)arg;
  2104. memset(&rs, 0, sizeof(rs));
  2105. rs.published_on = 1364925198;
  2106. strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
  2107. memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
  2108. memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
  2109. rs.addr = 0x20304050;
  2110. rs.or_port = 9001;
  2111. rs.dir_port = 9002;
  2112. rs.is_exit = 1;
  2113. rs.is_fast = 1;
  2114. rs.is_flagged_running = 1;
  2115. rs.has_bandwidth = 1;
  2116. rs.bandwidth_kb = 1000;
  2117. s = networkstatus_getinfo_helper_single(&rs);
  2118. tt_assert(s);
  2119. tt_str_op(s, ==,
  2120. "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
  2121. "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
  2122. "32.48.64.80 9001 9002\n"
  2123. "s Exit Fast Running V2Dir\n"
  2124. "w Bandwidth=1000\n");
  2125. done:
  2126. tor_free(s);
  2127. }
  2128. static void
  2129. test_dir_http_handling(void *args)
  2130. {
  2131. char *url = NULL;
  2132. (void)args;
  2133. /* Parse http url tests: */
  2134. /* Good headers */
  2135. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
  2136. "Host: example.com\r\n"
  2137. "User-Agent: Mozilla/5.0 (Windows;"
  2138. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2139. &url),==, 0);
  2140. tt_str_op(url,==, "/tor/a/b/c.txt");
  2141. tor_free(url);
  2142. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),==, 0);
  2143. tt_str_op(url,==, "/tor/a/b/c.txt");
  2144. tor_free(url);
  2145. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),==, 0);
  2146. tt_str_op(url,==, "/tor/a/b/c.txt");
  2147. tor_free(url);
  2148. /* Should prepend '/tor/' to url if required */
  2149. tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
  2150. "Host: example.com\r\n"
  2151. "User-Agent: Mozilla/5.0 (Windows;"
  2152. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2153. &url),==, 0);
  2154. tt_str_op(url,==, "/tor/a/b/c.txt");
  2155. tor_free(url);
  2156. /* Bad headers -- no HTTP/1.x*/
  2157. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2158. "Host: example.com\r\n"
  2159. "User-Agent: Mozilla/5.0 (Windows;"
  2160. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2161. &url),==, -1);
  2162. tt_assert(!url);
  2163. /* Bad headers */
  2164. tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
  2165. "Host: example.com\r\n"
  2166. "User-Agent: Mozilla/5.0 (Windows;"
  2167. " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
  2168. &url),==, -1);
  2169. tt_assert(!url);
  2170. tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),==, -1);
  2171. tt_assert(!url);
  2172. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),==, -1);
  2173. tt_assert(!url);
  2174. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),==, -1);
  2175. tt_assert(!url);
  2176. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),==, -1);
  2177. tt_assert(!url);
  2178. tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),==, -1);
  2179. tt_assert(!url);
  2180. done:
  2181. tor_free(url);
  2182. }
  2183. #define DIR_LEGACY(name) \
  2184. { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
  2185. #define DIR(name,flags) \
  2186. { #name, test_dir_##name, (flags), NULL, NULL }
  2187. struct testcase_t dir_tests[] = {
  2188. DIR_LEGACY(nicknames),
  2189. DIR_LEGACY(formats),
  2190. DIR_LEGACY(versions),
  2191. DIR_LEGACY(fp_pairs),
  2192. DIR(split_fps, 0),
  2193. DIR_LEGACY(measured_bw_kb),
  2194. DIR_LEGACY(measured_bw_kb_cache),
  2195. DIR_LEGACY(param_voting),
  2196. DIR_LEGACY(v3_networkstatus),
  2197. DIR(random_weighted, 0),
  2198. DIR(scale_bw, 0),
  2199. DIR_LEGACY(clip_unmeasured_bw_kb),
  2200. DIR_LEGACY(clip_unmeasured_bw_kb_alt),
  2201. DIR(fmt_control_ns, 0),
  2202. DIR(http_handling, 0),
  2203. END_OF_TESTCASES
  2204. };