test_dir.c 102 KB

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