test_dir.c 78 KB

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