test_dir.c 103 KB

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