test_dir.c 84 KB

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