test_dir.c 138 KB

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