test_dir.c 83 KB

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