test_dir.c 86 KB

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