test_dir.c 115 KB

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