test_dir.c 103 KB

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