test_dir.c 83 KB

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