test_dir.c 103 KB

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