test_dir.c 153 KB

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