test_dir.c 148 KB

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