test_dir.c 117 KB

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