test_dir.c 136 KB

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