test_dir.c 94 KB

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