test_dir.c 148 KB

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