test_dir.c 117 KB

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