test_dir.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
  379. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  380. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  381. #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
  382. /* no flags set */
  383. dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
  384. tt_int_op(smartlist_len(sl), ==, 3);
  385. tt_str_op(smartlist_get(sl, 0), ==, "A");
  386. tt_str_op(smartlist_get(sl, 1), ==, "C");
  387. tt_str_op(smartlist_get(sl, 2), ==, "B");
  388. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  389. smartlist_clear(sl);
  390. /* uniq strings. */
  391. dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
  392. tt_int_op(smartlist_len(sl), ==, 3);
  393. tt_str_op(smartlist_get(sl, 0), ==, "A");
  394. tt_str_op(smartlist_get(sl, 1), ==, "B");
  395. tt_str_op(smartlist_get(sl, 2), ==, "C");
  396. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  397. smartlist_clear(sl);
  398. /* Decode hex. */
  399. dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
  400. tt_int_op(smartlist_len(sl), ==, 2);
  401. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  402. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  403. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  404. smartlist_clear(sl);
  405. /* decode hex and drop weirdness. */
  406. dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
  407. sl, NULL, DSR_HEX);
  408. tt_int_op(smartlist_len(sl), ==, 2);
  409. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  410. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  411. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  412. smartlist_clear(sl);
  413. /* Decode long hex */
  414. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
  415. sl, NULL, DSR_HEX|DSR_DIGEST256);
  416. tt_int_op(smartlist_len(sl), ==, 3);
  417. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  418. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  419. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
  420. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  421. smartlist_clear(sl);
  422. /* Decode hex and sort. */
  423. dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
  424. sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
  425. tt_int_op(smartlist_len(sl), ==, 3);
  426. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
  427. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  428. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
  429. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  430. smartlist_clear(sl);
  431. /* Decode long hex and sort */
  432. dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
  433. "+"HEX256_1,
  434. sl, NULL,
  435. DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
  436. tt_int_op(smartlist_len(sl), ==, 3);
  437. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
  438. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  439. test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
  440. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  441. smartlist_clear(sl);
  442. /* Decode base64 */
  443. dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
  444. tt_int_op(smartlist_len(sl), ==, 2);
  445. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
  446. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
  447. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  448. smartlist_clear(sl);
  449. /* Decode long base64 */
  450. dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
  451. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  452. tt_int_op(smartlist_len(sl), ==, 2);
  453. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  454. test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
  455. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  456. smartlist_clear(sl);
  457. dir_split_resource_into_fingerprints(B64_256_1,
  458. sl, NULL, DSR_BASE64|DSR_DIGEST256);
  459. tt_int_op(smartlist_len(sl), ==, 1);
  460. test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
  461. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  462. smartlist_clear(sl);
  463. done:
  464. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  465. smartlist_free(sl);
  466. tor_free(mem_op_hex_tmp);
  467. }
  468. static void
  469. test_dir_measured_bw(void)
  470. {
  471. measured_bw_line_t mbwl;
  472. int i;
  473. const char *lines_pass[] = {
  474. "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  475. "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
  476. " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
  477. "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
  478. "bw=1024 junk=007\n",
  479. "misc=junk node_id=$557365204145532d32353620696e73746561642e "
  480. "bw=1024 junk=007\n",
  481. "end"
  482. };
  483. const char *lines_fail[] = {
  484. /* Test possible python stupidity on input */
  485. "node_id=None bw=1024\n",
  486. "node_id=$None bw=1024\n",
  487. "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
  488. "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
  489. "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
  490. "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
  491. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
  492. "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
  493. "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
  494. /* Test incomplete writes due to race conditions, partial copies, etc */
  495. "node_i",
  496. "node_i\n",
  497. "node_id=",
  498. "node_id=\n",
  499. "node_id=$557365204145532d32353620696e73746561642e bw=",
  500. "node_id=$557365204145532d32353620696e73746561642e bw=1024",
  501. "node_id=$557365204145532d32353620696e73746561642e bw=\n",
  502. "node_id=$557365204145532d32353620696e7374",
  503. "node_id=$557365204145532d32353620696e7374\n",
  504. "",
  505. "\n",
  506. " \n ",
  507. " \n\n",
  508. /* Test assorted noise */
  509. " node_id= ",
  510. "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
  511. "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
  512. "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
  513. "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
  514. "end"
  515. };
  516. for (i = 0; strcmp(lines_fail[i], "end"); i++) {
  517. //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
  518. test_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
  519. }
  520. for (i = 0; strcmp(lines_pass[i], "end"); i++) {
  521. //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
  522. test_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
  523. test_assert(mbwl.bw == 1024);
  524. test_assert(strcmp(mbwl.node_hex,
  525. "557365204145532d32353620696e73746561642e") == 0);
  526. }
  527. done:
  528. return;
  529. }
  530. static void
  531. test_dir_param_voting(void)
  532. {
  533. networkstatus_t vote1, vote2, vote3, vote4;
  534. smartlist_t *votes = smartlist_new();
  535. char *res = NULL;
  536. /* dirvote_compute_params only looks at the net_params field of the votes,
  537. so that's all we need to set.
  538. */
  539. memset(&vote1, 0, sizeof(vote1));
  540. memset(&vote2, 0, sizeof(vote2));
  541. memset(&vote3, 0, sizeof(vote3));
  542. memset(&vote4, 0, sizeof(vote4));
  543. vote1.net_params = smartlist_new();
  544. vote2.net_params = smartlist_new();
  545. vote3.net_params = smartlist_new();
  546. vote4.net_params = smartlist_new();
  547. smartlist_split_string(vote1.net_params,
  548. "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
  549. smartlist_split_string(vote2.net_params,
  550. "ab=27 cw=5 x-yz=88", NULL, 0, 0);
  551. smartlist_split_string(vote3.net_params,
  552. "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
  553. smartlist_split_string(vote4.net_params,
  554. "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
  555. test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
  556. test_eq(222, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
  557. test_eq(80, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
  558. test_eq(-8, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
  559. test_eq(0, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
  560. smartlist_add(votes, &vote1);
  561. /* Do the first tests without adding all the other votes, for
  562. * networks without many dirauths. */
  563. res = dirvote_compute_params(votes, 11, 6);
  564. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  565. tor_free(res);
  566. res = dirvote_compute_params(votes, 12, 2);
  567. test_streq(res, "");
  568. tor_free(res);
  569. res = dirvote_compute_params(votes, 12, 1);
  570. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
  571. tor_free(res);
  572. smartlist_add(votes, &vote2);
  573. res = dirvote_compute_params(votes, 11, 2);
  574. test_streq(res, "ab=27 abcd=20 cw=5 x-yz=-99");
  575. tor_free(res);
  576. res = dirvote_compute_params(votes, 12, 2);
  577. test_streq(res, "ab=27 cw=5 x-yz=-99");
  578. tor_free(res);
  579. res = dirvote_compute_params(votes, 12, 3);
  580. test_streq(res, "ab=27 cw=5 x-yz=-99");
  581. tor_free(res);
  582. res = dirvote_compute_params(votes, 12, 6);
  583. test_streq(res, "");
  584. tor_free(res);
  585. smartlist_add(votes, &vote3);
  586. res = dirvote_compute_params(votes, 11, 3);
  587. test_streq(res, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
  588. tor_free(res);
  589. res = dirvote_compute_params(votes, 12, 3);
  590. test_streq(res, "ab=27 abcd=20 cw=50 x-yz=-9");
  591. tor_free(res);
  592. res = dirvote_compute_params(votes, 12, 5);
  593. test_streq(res, "cw=50 x-yz=-9");
  594. tor_free(res);
  595. res = dirvote_compute_params(votes, 12, 9);
  596. test_streq(res, "cw=50 x-yz=-9");
  597. tor_free(res);
  598. smartlist_add(votes, &vote4);
  599. res = dirvote_compute_params(votes, 11, 4);
  600. test_streq(res, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
  601. tor_free(res);
  602. res = dirvote_compute_params(votes, 12, 4);
  603. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  604. tor_free(res);
  605. res = dirvote_compute_params(votes, 12, 5);
  606. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  607. tor_free(res);
  608. /* Test that the special-cased "at least three dirauths voted for
  609. * this param" logic works as expected. */
  610. res = dirvote_compute_params(votes, 12, 6);
  611. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  612. tor_free(res);
  613. res = dirvote_compute_params(votes, 12, 10);
  614. test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
  615. tor_free(res);
  616. done:
  617. tor_free(res);
  618. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  619. SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
  620. SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
  621. SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
  622. smartlist_free(vote1.net_params);
  623. smartlist_free(vote2.net_params);
  624. smartlist_free(vote3.net_params);
  625. smartlist_free(vote4.net_params);
  626. smartlist_free(votes);
  627. return;
  628. }
  629. extern const char AUTHORITY_CERT_1[];
  630. extern const char AUTHORITY_SIGNKEY_1[];
  631. extern const char AUTHORITY_CERT_2[];
  632. extern const char AUTHORITY_SIGNKEY_2[];
  633. extern const char AUTHORITY_CERT_3[];
  634. extern const char AUTHORITY_SIGNKEY_3[];
  635. /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
  636. * same voting authority, and that they do in fact have all the same
  637. * information. */
  638. static void
  639. test_same_voter(networkstatus_voter_info_t *v1,
  640. networkstatus_voter_info_t *v2)
  641. {
  642. test_streq(v1->nickname, v2->nickname);
  643. test_memeq(v1->identity_digest, v2->identity_digest, DIGEST_LEN);
  644. test_streq(v1->address, v2->address);
  645. test_eq(v1->addr, v2->addr);
  646. test_eq(v1->dir_port, v2->dir_port);
  647. test_eq(v1->or_port, v2->or_port);
  648. test_streq(v1->contact, v2->contact);
  649. test_memeq(v1->vote_digest, v2->vote_digest, DIGEST_LEN);
  650. done:
  651. ;
  652. }
  653. /** Helper: Make a new routerinfo containing the right information for a
  654. * given vote_routerstatus_t. */
  655. static routerinfo_t *
  656. generate_ri_from_rs(const vote_routerstatus_t *vrs)
  657. {
  658. routerinfo_t *r;
  659. const routerstatus_t *rs = &vrs->status;
  660. static time_t published = 0;
  661. r = tor_malloc_zero(sizeof(routerinfo_t));
  662. memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
  663. memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
  664. DIGEST_LEN);
  665. r->cache_info.do_not_cache = 1;
  666. r->cache_info.routerlist_index = -1;
  667. r->cache_info.signed_descriptor_body =
  668. tor_strdup("123456789012345678901234567890123");
  669. r->cache_info.signed_descriptor_len =
  670. strlen(r->cache_info.signed_descriptor_body);
  671. r->exit_policy = smartlist_new();
  672. r->cache_info.published_on = ++published + time(NULL);
  673. if (rs->has_bandwidth) {
  674. /*
  675. * Multiply by 1000 because the routerinfo_t and the routerstatus_t
  676. * seem to use different units (*sigh*) and because we seem stuck on
  677. * icky and perverse decimal kilobytes (*double sigh*) - see
  678. * router_get_advertised_bandwidth_capped() of routerlist.c and
  679. * routerstatus_format_entry() of dirserv.c.
  680. */
  681. r->bandwidthrate = rs->bandwidth * 1000;
  682. r->bandwidthcapacity = rs->bandwidth * 1000;
  683. }
  684. return r;
  685. }
  686. /** Helper: get a detached signatures document for one or two
  687. * consensuses. */
  688. static char *
  689. get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
  690. {
  691. char *r;
  692. smartlist_t *sl;
  693. tor_assert(ns && ns->flavor == FLAV_NS);
  694. sl = smartlist_new();
  695. smartlist_add(sl,ns);
  696. if (ns2)
  697. smartlist_add(sl,ns2);
  698. r = networkstatus_get_detached_signatures(sl);
  699. smartlist_free(sl);
  700. return r;
  701. }
  702. /**
  703. * Generate a routerstatus for v3_networkstatus test
  704. */
  705. static vote_routerstatus_t *
  706. gen_routerstatus_for_v3ns(int idx, time_t now)
  707. {
  708. vote_routerstatus_t *vrs;
  709. routerstatus_t *rs;
  710. tor_addr_t addr_ipv6;
  711. switch (idx) {
  712. case 0:
  713. /* Generate the first routerstatus. */
  714. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  715. rs = &vrs->status;
  716. vrs->version = tor_strdup("0.1.2.14");
  717. rs->published_on = now-1500;
  718. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  719. memset(rs->identity_digest, 3, DIGEST_LEN);
  720. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  721. rs->addr = 0x99008801;
  722. rs->or_port = 443;
  723. rs->dir_port = 8000;
  724. /* all flags but running cleared */
  725. rs->is_flagged_running = 1;
  726. break;
  727. case 1:
  728. /* Generate the second routerstatus. */
  729. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  730. rs = &vrs->status;
  731. vrs->version = tor_strdup("0.2.0.5");
  732. rs->published_on = now-1000;
  733. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  734. memset(rs->identity_digest, 5, DIGEST_LEN);
  735. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  736. rs->addr = 0x99009901;
  737. rs->or_port = 443;
  738. rs->dir_port = 0;
  739. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  740. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  741. rs->ipv6_orport = 4711;
  742. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  743. rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
  744. break;
  745. case 2:
  746. /* Generate the third routerstatus. */
  747. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  748. rs = &vrs->status;
  749. vrs->version = tor_strdup("0.1.0.3");
  750. rs->published_on = now-1000;
  751. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  752. memset(rs->identity_digest, 33, DIGEST_LEN);
  753. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  754. rs->addr = 0xAA009901;
  755. rs->or_port = 400;
  756. rs->dir_port = 9999;
  757. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  758. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  759. rs->is_possible_guard = 1;
  760. break;
  761. case 3:
  762. /* Generate a fourth routerstatus that is not running. */
  763. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  764. rs = &vrs->status;
  765. vrs->version = tor_strdup("0.1.6.3");
  766. rs->published_on = now-1000;
  767. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  768. memset(rs->identity_digest, 34, DIGEST_LEN);
  769. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  770. rs->addr = 0xC0000203;
  771. rs->or_port = 500;
  772. rs->dir_port = 1999;
  773. /* Running flag (and others) cleared */
  774. break;
  775. case 4:
  776. /* No more for this test; return NULL */
  777. vrs = NULL;
  778. break;
  779. default:
  780. /* Shouldn't happen */
  781. test_assert(0);
  782. }
  783. done:
  784. return vrs;
  785. }
  786. /** Apply tweaks to the vote list for each voter */
  787. static void
  788. vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
  789. {
  790. vote_routerstatus_t *vrs;
  791. const char *msg = NULL;
  792. test_assert(v);
  793. (void)now;
  794. if (voter == 1) {
  795. measured_bw_line_t mbw;
  796. memset(mbw.node_id, 33, sizeof(mbw.node_id));
  797. mbw.bw = 1024;
  798. test_assert(measured_bw_line_apply(&mbw,
  799. v->routerstatus_list) == 1);
  800. } else if (voter == 2 || voter == 3) {
  801. /* Monkey around with the list a bit */
  802. vrs = smartlist_get(v->routerstatus_list, 2);
  803. smartlist_del_keeporder(v->routerstatus_list, 2);
  804. tor_free(vrs->version);
  805. tor_free(vrs);
  806. vrs = smartlist_get(v->routerstatus_list, 0);
  807. vrs->status.is_fast = 1;
  808. if (voter == 3) {
  809. vrs = smartlist_get(v->routerstatus_list, 0);
  810. smartlist_del_keeporder(v->routerstatus_list, 0);
  811. tor_free(vrs->version);
  812. tor_free(vrs);
  813. vrs = smartlist_get(v->routerstatus_list, 0);
  814. memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
  815. test_assert(router_add_to_routerlist(
  816. generate_ri_from_rs(vrs), &msg,0,0) >= 0);
  817. }
  818. }
  819. done:
  820. return;
  821. }
  822. /**
  823. * Test a parsed vote_routerstatus_t for v3_networkstatus test
  824. */
  825. static void
  826. test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
  827. {
  828. routerstatus_t *rs;
  829. tor_addr_t addr_ipv6;
  830. test_assert(vrs);
  831. rs = &(vrs->status);
  832. test_assert(rs);
  833. /* Split out by digests to test */
  834. if (tor_memeq(rs->identity_digest,
  835. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  836. "\x3\x3\x3\x3",
  837. DIGEST_LEN) &&
  838. (voter == 1)) {
  839. /* Check the first routerstatus. */
  840. test_streq(vrs->version, "0.1.2.14");
  841. test_eq(rs->published_on, now-1500);
  842. test_streq(rs->nickname, "router2");
  843. test_memeq(rs->identity_digest,
  844. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  845. "\x3\x3\x3\x3",
  846. DIGEST_LEN);
  847. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  848. test_eq(rs->addr, 0x99008801);
  849. test_eq(rs->or_port, 443);
  850. test_eq(rs->dir_port, 8000);
  851. test_eq(vrs->flags, U64_LITERAL(16)); // no flags except "running"
  852. } else if (tor_memeq(rs->identity_digest,
  853. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  854. "\x5\x5\x5\x5",
  855. DIGEST_LEN) &&
  856. (voter == 1 || voter == 2)) {
  857. test_memeq(rs->identity_digest,
  858. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  859. "\x5\x5\x5\x5",
  860. DIGEST_LEN);
  861. if (voter == 1) {
  862. /* Check the second routerstatus. */
  863. test_streq(vrs->version, "0.2.0.5");
  864. test_eq(rs->published_on, now-1000);
  865. test_streq(rs->nickname, "router1");
  866. }
  867. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  868. test_eq(rs->addr, 0x99009901);
  869. test_eq(rs->or_port, 443);
  870. test_eq(rs->dir_port, 0);
  871. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  872. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  873. test_eq(rs->ipv6_orport, 4711);
  874. if (voter == 1) {
  875. test_eq(vrs->flags, U64_LITERAL(254)); // all flags except "authority."
  876. } else {
  877. /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
  878. test_eq(vrs->flags, U64_LITERAL(974));
  879. }
  880. } else if (tor_memeq(rs->identity_digest,
  881. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  882. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  883. DIGEST_LEN) &&
  884. (voter == 1 || voter == 2)) {
  885. /* Check the measured bandwidth bits */
  886. test_assert(vrs->status.has_measured_bw &&
  887. vrs->status.measured_bw == 1024);
  888. } else {
  889. /*
  890. * Didn't expect this, but the old unit test only checked some of them,
  891. * so don't assert.
  892. */
  893. /* test_assert(0); */
  894. }
  895. done:
  896. return;
  897. }
  898. /**
  899. * Test a consensus for v3_networkstatus_test
  900. */
  901. static void
  902. test_consensus_for_v3ns(networkstatus_t *con, time_t now)
  903. {
  904. (void)now;
  905. test_assert(con);
  906. test_assert(!con->cert);
  907. test_eq(2, smartlist_len(con->routerstatus_list));
  908. /* There should be two listed routers: one with identity 3, one with
  909. * identity 5. */
  910. done:
  911. return;
  912. }
  913. /**
  914. * Test a router list entry for v3_networkstatus test
  915. */
  916. static void
  917. test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
  918. {
  919. tor_addr_t addr_ipv6;
  920. test_assert(rs);
  921. /* There should be two listed routers: one with identity 3, one with
  922. * identity 5. */
  923. /* This one showed up in 2 digests. */
  924. if (tor_memeq(rs->identity_digest,
  925. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  926. "\x3\x3",
  927. DIGEST_LEN)) {
  928. test_memeq(rs->identity_digest,
  929. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  930. DIGEST_LEN);
  931. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  932. test_assert(!rs->is_authority);
  933. test_assert(!rs->is_exit);
  934. test_assert(!rs->is_fast);
  935. test_assert(!rs->is_possible_guard);
  936. test_assert(!rs->is_stable);
  937. /* (If it wasn't running it wouldn't be here) */
  938. test_assert(rs->is_flagged_running);
  939. test_assert(!rs->is_v2_dir);
  940. test_assert(!rs->is_valid);
  941. test_assert(!rs->is_named);
  942. /* XXXX check version */
  943. } else if (tor_memeq(rs->identity_digest,
  944. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  945. "\x5\x5\x5\x5",
  946. DIGEST_LEN)) {
  947. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  948. test_memeq(rs->identity_digest,
  949. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  950. DIGEST_LEN);
  951. test_streq(rs->nickname, "router1");
  952. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  953. test_eq(rs->published_on, now-1000);
  954. test_eq(rs->addr, 0x99009901);
  955. test_eq(rs->or_port, 443);
  956. test_eq(rs->dir_port, 0);
  957. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  958. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  959. test_eq(rs->ipv6_orport, 4711);
  960. test_assert(!rs->is_authority);
  961. test_assert(rs->is_exit);
  962. test_assert(rs->is_fast);
  963. test_assert(rs->is_possible_guard);
  964. test_assert(rs->is_stable);
  965. test_assert(rs->is_flagged_running);
  966. test_assert(rs->is_v2_dir);
  967. test_assert(rs->is_valid);
  968. test_assert(!rs->is_named);
  969. /* XXXX check version */
  970. } else {
  971. /* Weren't expecting this... */
  972. test_assert(0);
  973. }
  974. done:
  975. return;
  976. }
  977. /** Run a unit tests for generating and parsing networkstatuses, with
  978. * the supply test fns. */
  979. static void
  980. test_a_networkstatus(
  981. vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
  982. void (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
  983. void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
  984. void (*consensus_test)(networkstatus_t *con, time_t now),
  985. void (*rs_test)(routerstatus_t *rs, time_t now))
  986. {
  987. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  988. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  989. crypto_pk_t *sign_skey_leg1=NULL;
  990. const char *msg=NULL;
  991. time_t now = time(NULL);
  992. networkstatus_voter_info_t *voter;
  993. document_signature_t *sig;
  994. networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
  995. *con_md=NULL;
  996. vote_routerstatus_t *vrs;
  997. routerstatus_t *rs;
  998. int idx, n_rs, n_vrs;
  999. char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL, *cp;
  1000. smartlist_t *votes = smartlist_new();
  1001. /* For generating the two other consensuses. */
  1002. char *detached_text1=NULL, *detached_text2=NULL;
  1003. char *consensus_text2=NULL, *consensus_text3=NULL;
  1004. char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
  1005. char *consensus_text_md=NULL;
  1006. networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
  1007. ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
  1008. test_assert(vrs_gen);
  1009. test_assert(rs_test);
  1010. test_assert(vrs_test);
  1011. /* Parse certificates and keys. */
  1012. cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  1013. test_assert(cert1);
  1014. test_assert(cert1->is_cross_certified);
  1015. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  1016. test_assert(cert2);
  1017. cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
  1018. test_assert(cert3);
  1019. sign_skey_1 = crypto_pk_new();
  1020. sign_skey_2 = crypto_pk_new();
  1021. sign_skey_3 = crypto_pk_new();
  1022. sign_skey_leg1 = pk_generate(4);
  1023. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  1024. AUTHORITY_SIGNKEY_1, -1));
  1025. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  1026. AUTHORITY_SIGNKEY_2, -1));
  1027. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
  1028. AUTHORITY_SIGNKEY_3, -1));
  1029. test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  1030. test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  1031. /*
  1032. * Set up a vote; generate it; try to parse it.
  1033. */
  1034. vote = tor_malloc_zero(sizeof(networkstatus_t));
  1035. vote->type = NS_TYPE_VOTE;
  1036. vote->published = now;
  1037. vote->valid_after = now+1000;
  1038. vote->fresh_until = now+2000;
  1039. vote->valid_until = now+3000;
  1040. vote->vote_seconds = 100;
  1041. vote->dist_seconds = 200;
  1042. vote->supported_methods = smartlist_new();
  1043. smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
  1044. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  1045. vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  1046. vote->known_flags = smartlist_new();
  1047. smartlist_split_string(vote->known_flags,
  1048. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  1049. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1050. vote->voters = smartlist_new();
  1051. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  1052. voter->nickname = tor_strdup("Voter1");
  1053. voter->address = tor_strdup("1.2.3.4");
  1054. voter->addr = 0x01020304;
  1055. voter->dir_port = 80;
  1056. voter->or_port = 9000;
  1057. voter->contact = tor_strdup("voter@example.com");
  1058. crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
  1059. smartlist_add(vote->voters, voter);
  1060. vote->cert = authority_cert_dup(cert1);
  1061. vote->net_params = smartlist_new();
  1062. smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
  1063. NULL, 0, 0);
  1064. vote->routerstatus_list = smartlist_new();
  1065. /* add routerstatuses */
  1066. idx = 0;
  1067. do {
  1068. vrs = vrs_gen(idx, now);
  1069. if (vrs) {
  1070. smartlist_add(vote->routerstatus_list, vrs);
  1071. test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs),
  1072. &msg,0,0)>=0);
  1073. ++idx;
  1074. }
  1075. } while (vrs);
  1076. n_vrs = idx;
  1077. /* dump the vote and try to parse it. */
  1078. v1_text = format_networkstatus_vote(sign_skey_1, vote);
  1079. test_assert(v1_text);
  1080. v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
  1081. test_assert(v1);
  1082. /* Make sure the parsed thing was right. */
  1083. test_eq(v1->type, NS_TYPE_VOTE);
  1084. test_eq(v1->published, vote->published);
  1085. test_eq(v1->valid_after, vote->valid_after);
  1086. test_eq(v1->fresh_until, vote->fresh_until);
  1087. test_eq(v1->valid_until, vote->valid_until);
  1088. test_eq(v1->vote_seconds, vote->vote_seconds);
  1089. test_eq(v1->dist_seconds, vote->dist_seconds);
  1090. test_streq(v1->client_versions, vote->client_versions);
  1091. test_streq(v1->server_versions, vote->server_versions);
  1092. test_assert(v1->voters && smartlist_len(v1->voters));
  1093. voter = smartlist_get(v1->voters, 0);
  1094. test_streq(voter->nickname, "Voter1");
  1095. test_streq(voter->address, "1.2.3.4");
  1096. test_eq(voter->addr, 0x01020304);
  1097. test_eq(voter->dir_port, 80);
  1098. test_eq(voter->or_port, 9000);
  1099. test_streq(voter->contact, "voter@example.com");
  1100. test_assert(v1->cert);
  1101. test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  1102. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  1103. test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  1104. tor_free(cp);
  1105. test_eq(smartlist_len(v1->routerstatus_list), n_vrs);
  1106. if (vote_tweaks) vote_tweaks(v1, 1, now);
  1107. /* Check the routerstatuses. */
  1108. for (idx = 0; idx < n_vrs; ++idx) {
  1109. vrs = smartlist_get(v1->routerstatus_list, idx);
  1110. test_assert(vrs);
  1111. vrs_test(vrs, 1, now);
  1112. }
  1113. /* Generate second vote. It disagrees on some of the times,
  1114. * and doesn't list versions, and knows some crazy flags */
  1115. vote->published = now+1;
  1116. vote->fresh_until = now+3005;
  1117. vote->dist_seconds = 300;
  1118. authority_cert_free(vote->cert);
  1119. vote->cert = authority_cert_dup(cert2);
  1120. vote->net_params = smartlist_new();
  1121. smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
  1122. NULL, 0, 0);
  1123. tor_free(vote->client_versions);
  1124. tor_free(vote->server_versions);
  1125. voter = smartlist_get(vote->voters, 0);
  1126. tor_free(voter->nickname);
  1127. tor_free(voter->address);
  1128. voter->nickname = tor_strdup("Voter2");
  1129. voter->address = tor_strdup("2.3.4.5");
  1130. voter->addr = 0x02030405;
  1131. crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
  1132. smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
  1133. smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
  1134. smartlist_sort_strings(vote->known_flags);
  1135. /* generate and parse v2. */
  1136. v2_text = format_networkstatus_vote(sign_skey_2, vote);
  1137. test_assert(v2_text);
  1138. v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
  1139. test_assert(v2);
  1140. if (vote_tweaks) vote_tweaks(v2, 2, now);
  1141. /* Check that flags come out right.*/
  1142. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1143. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1144. "Running:Stable:V2Dir:Valid");
  1145. tor_free(cp);
  1146. /* Check the routerstatuses. */
  1147. n_vrs = smartlist_len(v2->routerstatus_list);
  1148. for (idx = 0; idx < n_vrs; ++idx) {
  1149. vrs = smartlist_get(v2->routerstatus_list, idx);
  1150. test_assert(vrs);
  1151. vrs_test(vrs, 2, now);
  1152. }
  1153. /* Generate the third vote. */
  1154. vote->published = now;
  1155. vote->fresh_until = now+2003;
  1156. vote->dist_seconds = 250;
  1157. authority_cert_free(vote->cert);
  1158. vote->cert = authority_cert_dup(cert3);
  1159. vote->net_params = smartlist_new();
  1160. smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
  1161. NULL, 0, 0);
  1162. smartlist_add(vote->supported_methods, tor_strdup("4"));
  1163. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
  1164. vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
  1165. voter = smartlist_get(vote->voters, 0);
  1166. tor_free(voter->nickname);
  1167. tor_free(voter->address);
  1168. voter->nickname = tor_strdup("Voter3");
  1169. voter->address = tor_strdup("3.4.5.6");
  1170. voter->addr = 0x03040506;
  1171. crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
  1172. /* This one has a legacy id. */
  1173. memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
  1174. v3_text = format_networkstatus_vote(sign_skey_3, vote);
  1175. test_assert(v3_text);
  1176. v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
  1177. test_assert(v3);
  1178. if (vote_tweaks) vote_tweaks(v3, 3, now);
  1179. /* Compute a consensus as voter 3. */
  1180. smartlist_add(votes, v3);
  1181. smartlist_add(votes, v1);
  1182. smartlist_add(votes, v2);
  1183. consensus_text = networkstatus_compute_consensus(votes, 3,
  1184. cert3->identity_key,
  1185. sign_skey_3,
  1186. "AAAAAAAAAAAAAAAAAAAA",
  1187. sign_skey_leg1,
  1188. FLAV_NS);
  1189. test_assert(consensus_text);
  1190. con = networkstatus_parse_vote_from_string(consensus_text, NULL,
  1191. NS_TYPE_CONSENSUS);
  1192. test_assert(con);
  1193. //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
  1194. // v1_text, v2_text, v3_text);
  1195. consensus_text_md = networkstatus_compute_consensus(votes, 3,
  1196. cert3->identity_key,
  1197. sign_skey_3,
  1198. "AAAAAAAAAAAAAAAAAAAA",
  1199. sign_skey_leg1,
  1200. FLAV_MICRODESC);
  1201. test_assert(consensus_text_md);
  1202. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  1203. NS_TYPE_CONSENSUS);
  1204. test_assert(con_md);
  1205. test_eq(con_md->flavor, FLAV_MICRODESC);
  1206. /* Check consensus contents. */
  1207. test_assert(con->type == NS_TYPE_CONSENSUS);
  1208. test_eq(con->published, 0); /* this field only appears in votes. */
  1209. test_eq(con->valid_after, now+1000);
  1210. test_eq(con->fresh_until, now+2003); /* median */
  1211. test_eq(con->valid_until, now+3000);
  1212. test_eq(con->vote_seconds, 100);
  1213. test_eq(con->dist_seconds, 250); /* median */
  1214. test_streq(con->client_versions, "0.1.2.14");
  1215. test_streq(con->server_versions, "0.1.2.15,0.1.2.16");
  1216. cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
  1217. test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
  1218. "Running:Stable:V2Dir:Valid");
  1219. tor_free(cp);
  1220. cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
  1221. test_streq(cp, "circuitwindow=80:foo=660");
  1222. tor_free(cp);
  1223. test_eq(4, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
  1224. /* The voter id digests should be in this order. */
  1225. test_assert(memcmp(cert2->cache_info.identity_digest,
  1226. cert1->cache_info.identity_digest,DIGEST_LEN)<0);
  1227. test_assert(memcmp(cert1->cache_info.identity_digest,
  1228. cert3->cache_info.identity_digest,DIGEST_LEN)<0);
  1229. test_same_voter(smartlist_get(con->voters, 1),
  1230. smartlist_get(v2->voters, 0));
  1231. test_same_voter(smartlist_get(con->voters, 2),
  1232. smartlist_get(v1->voters, 0));
  1233. test_same_voter(smartlist_get(con->voters, 3),
  1234. smartlist_get(v3->voters, 0));
  1235. consensus_test(con, now);
  1236. /* Check the routerstatuses. */
  1237. n_rs = smartlist_len(con->routerstatus_list);
  1238. for (idx = 0; idx < n_rs; ++idx) {
  1239. rs = smartlist_get(con->routerstatus_list, idx);
  1240. test_assert(rs);
  1241. rs_test(rs, now);
  1242. }
  1243. /* Check signatures. the first voter is a pseudo-entry with a legacy key.
  1244. * The second one hasn't signed. The fourth one has signed: validate it. */
  1245. voter = smartlist_get(con->voters, 1);
  1246. test_eq(smartlist_len(voter->sigs), 0);
  1247. voter = smartlist_get(con->voters, 3);
  1248. test_eq(smartlist_len(voter->sigs), 1);
  1249. sig = smartlist_get(voter->sigs, 0);
  1250. test_assert(sig->signature);
  1251. test_assert(!sig->good_signature);
  1252. test_assert(!sig->bad_signature);
  1253. test_assert(!networkstatus_check_document_signature(con, sig, cert3));
  1254. test_assert(sig->signature);
  1255. test_assert(sig->good_signature);
  1256. test_assert(!sig->bad_signature);
  1257. {
  1258. const char *msg=NULL;
  1259. /* Compute the other two signed consensuses. */
  1260. smartlist_shuffle(votes);
  1261. consensus_text2 = networkstatus_compute_consensus(votes, 3,
  1262. cert2->identity_key,
  1263. sign_skey_2, NULL,NULL,
  1264. FLAV_NS);
  1265. consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
  1266. cert2->identity_key,
  1267. sign_skey_2, NULL,NULL,
  1268. FLAV_MICRODESC);
  1269. smartlist_shuffle(votes);
  1270. consensus_text3 = networkstatus_compute_consensus(votes, 3,
  1271. cert1->identity_key,
  1272. sign_skey_1, NULL,NULL,
  1273. FLAV_NS);
  1274. consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
  1275. cert1->identity_key,
  1276. sign_skey_1, NULL,NULL,
  1277. FLAV_MICRODESC);
  1278. test_assert(consensus_text2);
  1279. test_assert(consensus_text3);
  1280. test_assert(consensus_text_md2);
  1281. test_assert(consensus_text_md3);
  1282. con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
  1283. NS_TYPE_CONSENSUS);
  1284. con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
  1285. NS_TYPE_CONSENSUS);
  1286. con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
  1287. NS_TYPE_CONSENSUS);
  1288. con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
  1289. NS_TYPE_CONSENSUS);
  1290. test_assert(con2);
  1291. test_assert(con3);
  1292. test_assert(con_md2);
  1293. test_assert(con_md3);
  1294. /* All three should have the same digest. */
  1295. test_memeq(&con->digests, &con2->digests, sizeof(digests_t));
  1296. test_memeq(&con->digests, &con3->digests, sizeof(digests_t));
  1297. test_memeq(&con_md->digests, &con_md2->digests, sizeof(digests_t));
  1298. test_memeq(&con_md->digests, &con_md3->digests, sizeof(digests_t));
  1299. /* Extract a detached signature from con3. */
  1300. detached_text1 = get_detached_sigs(con3, con_md3);
  1301. tt_assert(detached_text1);
  1302. /* Try to parse it. */
  1303. dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
  1304. tt_assert(dsig1);
  1305. /* Are parsed values as expected? */
  1306. test_eq(dsig1->valid_after, con3->valid_after);
  1307. test_eq(dsig1->fresh_until, con3->fresh_until);
  1308. test_eq(dsig1->valid_until, con3->valid_until);
  1309. {
  1310. digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
  1311. test_assert(dsig_digests);
  1312. test_memeq(dsig_digests->d[DIGEST_SHA1], con3->digests.d[DIGEST_SHA1],
  1313. DIGEST_LEN);
  1314. dsig_digests = strmap_get(dsig1->digests, "microdesc");
  1315. test_assert(dsig_digests);
  1316. test_memeq(dsig_digests->d[DIGEST_SHA256],
  1317. con_md3->digests.d[DIGEST_SHA256],
  1318. DIGEST256_LEN);
  1319. }
  1320. {
  1321. smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
  1322. test_assert(dsig_signatures);
  1323. test_eq(1, smartlist_len(dsig_signatures));
  1324. sig = smartlist_get(dsig_signatures, 0);
  1325. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1326. DIGEST_LEN);
  1327. test_eq(sig->alg, DIGEST_SHA1);
  1328. dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
  1329. test_assert(dsig_signatures);
  1330. test_eq(1, smartlist_len(dsig_signatures));
  1331. sig = smartlist_get(dsig_signatures, 0);
  1332. test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
  1333. DIGEST_LEN);
  1334. test_eq(sig->alg, DIGEST_SHA256);
  1335. }
  1336. /* Try adding it to con2. */
  1337. detached_text2 = get_detached_sigs(con2,con_md2);
  1338. test_eq(1, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1339. LOG_INFO, &msg));
  1340. tor_free(detached_text2);
  1341. test_eq(1, networkstatus_add_detached_signatures(con_md2, dsig1, "test",
  1342. LOG_INFO, &msg));
  1343. tor_free(detached_text2);
  1344. detached_text2 = get_detached_sigs(con2,con_md2);
  1345. //printf("\n<%s>\n", detached_text2);
  1346. dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
  1347. test_assert(dsig2);
  1348. /*
  1349. printf("\n");
  1350. SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
  1351. char hd[64];
  1352. base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
  1353. printf("%s\n", hd);
  1354. });
  1355. */
  1356. test_eq(2,
  1357. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
  1358. test_eq(2,
  1359. smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
  1360. "microdesc")));
  1361. /* Try adding to con2 twice; verify that nothing changes. */
  1362. test_eq(0, networkstatus_add_detached_signatures(con2, dsig1, "test",
  1363. LOG_INFO, &msg));
  1364. /* Add to con. */
  1365. test_eq(2, networkstatus_add_detached_signatures(con, dsig2, "test",
  1366. LOG_INFO, &msg));
  1367. /* Check signatures */
  1368. voter = smartlist_get(con->voters, 1);
  1369. sig = smartlist_get(voter->sigs, 0);
  1370. test_assert(sig);
  1371. test_assert(!networkstatus_check_document_signature(con, sig, cert2));
  1372. voter = smartlist_get(con->voters, 2);
  1373. sig = smartlist_get(voter->sigs, 0);
  1374. test_assert(sig);
  1375. test_assert(!networkstatus_check_document_signature(con, sig, cert1));
  1376. }
  1377. done:
  1378. smartlist_free(votes);
  1379. tor_free(v1_text);
  1380. tor_free(v2_text);
  1381. tor_free(v3_text);
  1382. tor_free(consensus_text);
  1383. tor_free(consensus_text_md);
  1384. if (vote)
  1385. networkstatus_vote_free(vote);
  1386. if (v1)
  1387. networkstatus_vote_free(v1);
  1388. if (v2)
  1389. networkstatus_vote_free(v2);
  1390. if (v3)
  1391. networkstatus_vote_free(v3);
  1392. if (con)
  1393. networkstatus_vote_free(con);
  1394. if (con_md)
  1395. networkstatus_vote_free(con_md);
  1396. if (sign_skey_1)
  1397. crypto_pk_free(sign_skey_1);
  1398. if (sign_skey_2)
  1399. crypto_pk_free(sign_skey_2);
  1400. if (sign_skey_3)
  1401. crypto_pk_free(sign_skey_3);
  1402. if (sign_skey_leg1)
  1403. crypto_pk_free(sign_skey_leg1);
  1404. if (cert1)
  1405. authority_cert_free(cert1);
  1406. if (cert2)
  1407. authority_cert_free(cert2);
  1408. if (cert3)
  1409. authority_cert_free(cert3);
  1410. tor_free(consensus_text2);
  1411. tor_free(consensus_text3);
  1412. tor_free(consensus_text_md2);
  1413. tor_free(consensus_text_md3);
  1414. tor_free(detached_text1);
  1415. tor_free(detached_text2);
  1416. if (con2)
  1417. networkstatus_vote_free(con2);
  1418. if (con3)
  1419. networkstatus_vote_free(con3);
  1420. if (con_md2)
  1421. networkstatus_vote_free(con_md2);
  1422. if (con_md3)
  1423. networkstatus_vote_free(con_md3);
  1424. if (dsig1)
  1425. ns_detached_signatures_free(dsig1);
  1426. if (dsig2)
  1427. ns_detached_signatures_free(dsig2);
  1428. }
  1429. /** Run unit tests for generating and parsing V3 consensus networkstatus
  1430. * documents. */
  1431. static void
  1432. test_dir_v3_networkstatus(void)
  1433. {
  1434. test_a_networkstatus(gen_routerstatus_for_v3ns,
  1435. vote_tweaks_for_v3ns,
  1436. test_vrs_for_v3ns,
  1437. test_consensus_for_v3ns,
  1438. test_routerstatus_for_v3ns);
  1439. }
  1440. static void
  1441. test_dir_scale_bw(void *testdata)
  1442. {
  1443. double v[8] = { 2.0/3,
  1444. 7.0,
  1445. 1.0,
  1446. 3.0,
  1447. 1.0/5,
  1448. 1.0/7,
  1449. 12.0,
  1450. 24.0 };
  1451. u64_dbl_t vals[8];
  1452. uint64_t total;
  1453. int i;
  1454. (void) testdata;
  1455. for (i=0; i<8; ++i)
  1456. vals[i].dbl = v[i];
  1457. scale_array_elements_to_u64(vals, 8, &total);
  1458. tt_int_op((int)total, ==, 48);
  1459. total = 0;
  1460. for (i=0; i<8; ++i) {
  1461. total += vals[i].u64;
  1462. }
  1463. tt_assert(total >= (U64_LITERAL(1)<<60));
  1464. tt_assert(total <= (U64_LITERAL(1)<<62));
  1465. for (i=0; i<8; ++i) {
  1466. double ratio = ((double)vals[i].u64) / vals[2].u64;
  1467. tt_double_op(fabs(ratio - v[i]), <, .00001);
  1468. }
  1469. done:
  1470. ;
  1471. }
  1472. static void
  1473. test_dir_random_weighted(void *testdata)
  1474. {
  1475. int histogram[10];
  1476. uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
  1477. u64_dbl_t inp[10];
  1478. int i, choice;
  1479. const int n = 50000;
  1480. double max_sq_error;
  1481. (void) testdata;
  1482. /* Try a ten-element array with values from 0 through 10. The values are
  1483. * in a scrambled order to make sure we don't depend on order. */
  1484. memset(histogram,0,sizeof(histogram));
  1485. for (i=0; i<10; ++i) {
  1486. inp[i].u64 = vals[i];
  1487. total += vals[i];
  1488. }
  1489. tt_int_op(total, ==, 45);
  1490. for (i=0; i<n; ++i) {
  1491. choice = choose_array_element_by_weight(inp, 10);
  1492. tt_int_op(choice, >=, 0);
  1493. tt_int_op(choice, <, 10);
  1494. histogram[choice]++;
  1495. }
  1496. /* Now see if we chose things about frequently enough. */
  1497. max_sq_error = 0;
  1498. for (i=0; i<10; ++i) {
  1499. int expected = (int)(n*vals[i]/total);
  1500. double frac_diff = 0, sq;
  1501. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1502. if (expected)
  1503. frac_diff = (histogram[i] - expected) / ((double)expected);
  1504. else
  1505. tt_int_op(histogram[i], ==, 0);
  1506. sq = frac_diff * frac_diff;
  1507. if (sq > max_sq_error)
  1508. max_sq_error = sq;
  1509. }
  1510. /* It should almost always be much much less than this. If you want to
  1511. * figure out the odds, please feel free. */
  1512. tt_double_op(max_sq_error, <, .05);
  1513. /* Now try a singleton; do we choose it? */
  1514. for (i = 0; i < 100; ++i) {
  1515. choice = choose_array_element_by_weight(inp, 1);
  1516. tt_int_op(choice, ==, 0);
  1517. }
  1518. /* Now try an array of zeros. We should choose randomly. */
  1519. memset(histogram,0,sizeof(histogram));
  1520. for (i = 0; i < 5; ++i)
  1521. inp[i].u64 = 0;
  1522. for (i = 0; i < n; ++i) {
  1523. choice = choose_array_element_by_weight(inp, 5);
  1524. tt_int_op(choice, >=, 0);
  1525. tt_int_op(choice, <, 5);
  1526. histogram[choice]++;
  1527. }
  1528. /* Now see if we chose things about frequently enough. */
  1529. max_sq_error = 0;
  1530. for (i=0; i<5; ++i) {
  1531. int expected = n/5;
  1532. double frac_diff = 0, sq;
  1533. TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
  1534. frac_diff = (histogram[i] - expected) / ((double)expected);
  1535. sq = frac_diff * frac_diff;
  1536. if (sq > max_sq_error)
  1537. max_sq_error = sq;
  1538. }
  1539. /* It should almost always be much much less than this. If you want to
  1540. * figure out the odds, please feel free. */
  1541. tt_double_op(max_sq_error, <, .05);
  1542. done:
  1543. ;
  1544. }
  1545. /* Function pointers for test_dir_clip_unmeasured_bw() */
  1546. /**
  1547. * Generate a routerstatus for clip_unmeasured_bw test; based on the
  1548. * v3_networkstatus ones.
  1549. */
  1550. static vote_routerstatus_t *
  1551. gen_routerstatus_for_umbw(int idx, time_t now)
  1552. {
  1553. vote_routerstatus_t *vrs;
  1554. routerstatus_t *rs;
  1555. tor_addr_t addr_ipv6;
  1556. switch (idx) {
  1557. case 0:
  1558. /* Generate the first routerstatus. */
  1559. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1560. rs = &vrs->status;
  1561. vrs->version = tor_strdup("0.1.2.14");
  1562. rs->published_on = now-1500;
  1563. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  1564. memset(rs->identity_digest, 3, DIGEST_LEN);
  1565. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  1566. rs->addr = 0x99008801;
  1567. rs->or_port = 443;
  1568. rs->dir_port = 8000;
  1569. /* all flags but running cleared */
  1570. rs->is_flagged_running = 1;
  1571. /*
  1572. * This one has measured bandwidth below the clip cutoff, and
  1573. * so shouldn't be clipped; we'll have to test that it isn't
  1574. * later.
  1575. */
  1576. rs->has_measured_bw = 1;
  1577. rs->has_bandwidth = 1;
  1578. rs->measured_bw = rs->bandwidth = DEFAULT_MAX_UNMEASURED_BW / 2;
  1579. break;
  1580. case 1:
  1581. /* Generate the second routerstatus. */
  1582. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1583. rs = &vrs->status;
  1584. vrs->version = tor_strdup("0.2.0.5");
  1585. rs->published_on = now-1000;
  1586. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  1587. memset(rs->identity_digest, 5, DIGEST_LEN);
  1588. memset(rs->descriptor_digest, 77, DIGEST_LEN);
  1589. rs->addr = 0x99009901;
  1590. rs->or_port = 443;
  1591. rs->dir_port = 0;
  1592. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1593. tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
  1594. rs->ipv6_orport = 4711;
  1595. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
  1596. rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
  1597. /*
  1598. * This one has measured bandwidth above the clip cutoff, and
  1599. * so shouldn't be clipped; we'll have to test that it isn't
  1600. * later.
  1601. */
  1602. rs->has_measured_bw = 1;
  1603. rs->has_bandwidth = 1;
  1604. rs->measured_bw = rs->bandwidth = 2 * DEFAULT_MAX_UNMEASURED_BW;
  1605. break;
  1606. case 2:
  1607. /* Generate the third routerstatus. */
  1608. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1609. rs = &vrs->status;
  1610. vrs->version = tor_strdup("0.1.0.3");
  1611. rs->published_on = now-1000;
  1612. strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
  1613. memset(rs->identity_digest, 0x33, DIGEST_LEN);
  1614. memset(rs->descriptor_digest, 79, DIGEST_LEN);
  1615. rs->addr = 0xAA009901;
  1616. rs->or_port = 400;
  1617. rs->dir_port = 9999;
  1618. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  1619. rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
  1620. rs->is_possible_guard = 1;
  1621. /*
  1622. * This one has unmeasured bandwidth above the clip cutoff, and
  1623. * so should be clipped; we'll have to test that it isn't
  1624. * later.
  1625. */
  1626. rs->has_measured_bw = 0;
  1627. rs->has_bandwidth = 1;
  1628. rs->measured_bw = 0;
  1629. rs->bandwidth = 2 * DEFAULT_MAX_UNMEASURED_BW;
  1630. break;
  1631. case 3:
  1632. /* Generate a fourth routerstatus that is not running. */
  1633. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1634. rs = &vrs->status;
  1635. vrs->version = tor_strdup("0.1.6.3");
  1636. rs->published_on = now-1000;
  1637. strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
  1638. memset(rs->identity_digest, 0x34, DIGEST_LEN);
  1639. memset(rs->descriptor_digest, 47, DIGEST_LEN);
  1640. rs->addr = 0xC0000203;
  1641. rs->or_port = 500;
  1642. rs->dir_port = 1999;
  1643. /* all flags but running cleared */
  1644. rs->is_flagged_running = 1;
  1645. /*
  1646. * This one has unmeasured bandwidth below the clip cutoff, and
  1647. * so shouldn't be clipped; we'll have to test that it isn't
  1648. * later.
  1649. */
  1650. rs->has_measured_bw = 0;
  1651. rs->has_bandwidth = 1;
  1652. rs->measured_bw = 0;
  1653. rs->bandwidth = DEFAULT_MAX_UNMEASURED_BW / 2;
  1654. break;
  1655. case 4:
  1656. /* No more for this test; return NULL */
  1657. vrs = NULL;
  1658. break;
  1659. default:
  1660. /* Shouldn't happen */
  1661. test_assert(0);
  1662. }
  1663. done:
  1664. return vrs;
  1665. }
  1666. /** Apply tweaks to the vote list for each voter; for the umbw test this is
  1667. * just adding the right consensus methods to let clipping happen */
  1668. static void
  1669. vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
  1670. {
  1671. test_assert(v);
  1672. (void)voter;
  1673. (void)now;
  1674. test_assert(v->supported_methods);
  1675. smartlist_clear(v->supported_methods);
  1676. /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW */
  1677. smartlist_split_string(v->supported_methods,
  1678. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
  1679. NULL, 0, -1);
  1680. done:
  1681. return;
  1682. }
  1683. /**
  1684. * Test a parsed vote_routerstatus_t for umbw test.
  1685. */
  1686. static void
  1687. test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
  1688. {
  1689. routerstatus_t *rs;
  1690. tor_addr_t addr_ipv6;
  1691. (void)voter;
  1692. test_assert(vrs);
  1693. rs = &(vrs->status);
  1694. test_assert(rs);
  1695. /* Split out by digests to test */
  1696. if (tor_memeq(rs->identity_digest,
  1697. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1698. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1699. DIGEST_LEN)) {
  1700. /*
  1701. * Check the first routerstatus - measured bandwidth below the clip
  1702. * cutoff.
  1703. */
  1704. test_streq(vrs->version, "0.1.2.14");
  1705. test_eq(rs->published_on, now-1500);
  1706. test_streq(rs->nickname, "router2");
  1707. test_memeq(rs->identity_digest,
  1708. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1709. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1710. DIGEST_LEN);
  1711. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1712. test_eq(rs->addr, 0x99008801);
  1713. test_eq(rs->or_port, 443);
  1714. test_eq(rs->dir_port, 8000);
  1715. test_assert(rs->has_bandwidth);
  1716. test_assert(rs->has_measured_bw);
  1717. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
  1718. test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW / 2);
  1719. } else if (tor_memeq(rs->identity_digest,
  1720. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1721. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1722. DIGEST_LEN)) {
  1723. /*
  1724. * Check the second routerstatus - measured bandwidth above the clip
  1725. * cutoff.
  1726. */
  1727. test_streq(vrs->version, "0.2.0.5");
  1728. test_eq(rs->published_on, now-1000);
  1729. test_streq(rs->nickname, "router1");
  1730. test_memeq(rs->identity_digest,
  1731. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1732. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1733. DIGEST_LEN);
  1734. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1735. test_eq(rs->addr, 0x99009901);
  1736. test_eq(rs->or_port, 443);
  1737. test_eq(rs->dir_port, 0);
  1738. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1739. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1740. test_eq(rs->ipv6_orport, 4711);
  1741. test_assert(rs->has_bandwidth);
  1742. test_assert(rs->has_measured_bw);
  1743. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
  1744. test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW * 2);
  1745. } else if (tor_memeq(rs->identity_digest,
  1746. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1747. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1748. DIGEST_LEN)) {
  1749. /*
  1750. * Check the third routerstatus - unmeasured bandwidth above the clip
  1751. * cutoff; this one should be clipped later on in the consensus, but
  1752. * appears unclipped in the vote.
  1753. */
  1754. test_assert(rs->has_bandwidth);
  1755. test_assert(!(rs->has_measured_bw));
  1756. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
  1757. test_eq(rs->measured_bw, 0);
  1758. } else if (tor_memeq(rs->identity_digest,
  1759. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  1760. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  1761. DIGEST_LEN)) {
  1762. /*
  1763. * Check the fourth routerstatus - unmeasured bandwidth below the clip
  1764. * cutoff; this one should not be clipped.
  1765. */
  1766. test_assert(rs->has_bandwidth);
  1767. test_assert(!(rs->has_measured_bw));
  1768. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
  1769. test_eq(rs->measured_bw, 0);
  1770. } else {
  1771. test_assert(0);
  1772. }
  1773. done:
  1774. return;
  1775. }
  1776. /**
  1777. * Test a consensus for v3_networkstatus_test
  1778. */
  1779. static void
  1780. test_consensus_for_umbw(networkstatus_t *con, time_t now)
  1781. {
  1782. (void)now;
  1783. test_assert(con);
  1784. test_assert(!con->cert);
  1785. /* test_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW); */
  1786. test_assert(con->consensus_method >= 16);
  1787. test_eq(4, smartlist_len(con->routerstatus_list));
  1788. /* There should be four listed routers; all voters saw the same in this */
  1789. done:
  1790. return;
  1791. }
  1792. /**
  1793. * Test a router list entry for umbw test
  1794. */
  1795. static void
  1796. test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
  1797. {
  1798. tor_addr_t addr_ipv6;
  1799. test_assert(rs);
  1800. /* There should be four listed routers, as constructed above */
  1801. if (tor_memeq(rs->identity_digest,
  1802. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1803. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1804. DIGEST_LEN)) {
  1805. test_memeq(rs->identity_digest,
  1806. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
  1807. "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
  1808. DIGEST_LEN);
  1809. test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
  1810. test_assert(!rs->is_authority);
  1811. test_assert(!rs->is_exit);
  1812. test_assert(!rs->is_fast);
  1813. test_assert(!rs->is_possible_guard);
  1814. test_assert(!rs->is_stable);
  1815. /* (If it wasn't running it wouldn't be here) */
  1816. test_assert(rs->is_flagged_running);
  1817. test_assert(!rs->is_v2_dir);
  1818. test_assert(!rs->is_valid);
  1819. test_assert(!rs->is_named);
  1820. /* This one should have measured bandwidth below the clip cutoff */
  1821. test_assert(rs->has_bandwidth);
  1822. test_assert(rs->has_measured_bw);
  1823. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
  1824. test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW / 2);
  1825. } else if (tor_memeq(rs->identity_digest,
  1826. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1827. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1828. DIGEST_LEN)) {
  1829. /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
  1830. test_memeq(rs->identity_digest,
  1831. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
  1832. "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
  1833. DIGEST_LEN);
  1834. test_streq(rs->nickname, "router1");
  1835. test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
  1836. test_eq(rs->published_on, now-1000);
  1837. test_eq(rs->addr, 0x99009901);
  1838. test_eq(rs->or_port, 443);
  1839. test_eq(rs->dir_port, 0);
  1840. tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
  1841. test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
  1842. test_eq(rs->ipv6_orport, 4711);
  1843. test_assert(!rs->is_authority);
  1844. test_assert(rs->is_exit);
  1845. test_assert(rs->is_fast);
  1846. test_assert(rs->is_possible_guard);
  1847. test_assert(rs->is_stable);
  1848. test_assert(rs->is_flagged_running);
  1849. test_assert(rs->is_v2_dir);
  1850. test_assert(rs->is_valid);
  1851. test_assert(!rs->is_named);
  1852. /* This one should have measured bandwidth above the clip cutoff */
  1853. test_assert(rs->has_bandwidth);
  1854. test_assert(rs->has_measured_bw);
  1855. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
  1856. test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW * 2);
  1857. } else if (tor_memeq(rs->identity_digest,
  1858. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
  1859. "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
  1860. DIGEST_LEN)) {
  1861. /*
  1862. * This one should have unmeasured bandwidth above the clip cutoff,
  1863. * and so should be clipped
  1864. */
  1865. test_assert(rs->has_bandwidth);
  1866. test_assert(!(rs->has_measured_bw));
  1867. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW);
  1868. test_eq(rs->measured_bw, 0);
  1869. } else if (tor_memeq(rs->identity_digest,
  1870. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
  1871. "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
  1872. DIGEST_LEN)) {
  1873. /*
  1874. * This one should have unmeasured bandwidth below the clip cutoff,
  1875. * and so should not be clipped
  1876. */
  1877. test_assert(rs->has_bandwidth);
  1878. test_assert(!(rs->has_measured_bw));
  1879. test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
  1880. test_eq(rs->measured_bw, 0);
  1881. } else {
  1882. /* Weren't expecting this... */
  1883. test_assert(0);
  1884. }
  1885. done:
  1886. return;
  1887. }
  1888. /**
  1889. * Compute a consensus involving clipping unmeasured bandwidth with consensus
  1890. * method 17; this uses the same test_a_networkstatus() function that the
  1891. * v3_networkstatus test uses.
  1892. */
  1893. static void
  1894. test_dir_clip_unmeasured_bw(void)
  1895. {
  1896. test_a_networkstatus(gen_routerstatus_for_umbw,
  1897. vote_tweaks_for_umbw,
  1898. test_vrs_for_umbw,
  1899. test_consensus_for_umbw,
  1900. test_routerstatus_for_umbw);
  1901. }
  1902. #define DIR_LEGACY(name) \
  1903. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
  1904. #define DIR(name) \
  1905. { #name, test_dir_##name, 0, NULL, NULL }
  1906. struct testcase_t dir_tests[] = {
  1907. DIR_LEGACY(nicknames),
  1908. DIR_LEGACY(formats),
  1909. DIR_LEGACY(versions),
  1910. DIR_LEGACY(fp_pairs),
  1911. DIR(split_fps),
  1912. DIR_LEGACY(measured_bw),
  1913. DIR_LEGACY(param_voting),
  1914. DIR_LEGACY(v3_networkstatus),
  1915. DIR(random_weighted),
  1916. DIR(scale_bw),
  1917. DIR_LEGACY(clip_unmeasured_bw),
  1918. END_OF_TESTCASES
  1919. };