test_dir.c 106 KB

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