test_dir.c 83 KB

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