test_dir.c 145 KB

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