test_dir.c 144 KB

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