test_dir.c 102 KB

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