test_hs_common.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_common.c
  5. * \brief Test hidden service common functionalities.
  6. */
  7. #define HS_COMMON_PRIVATE
  8. #define HS_CLIENT_PRIVATE
  9. #define HS_SERVICE_PRIVATE
  10. #define NODELIST_PRIVATE
  11. #include "test/test.h"
  12. #include "test/test_helpers.h"
  13. #include "test/log_test_helpers.h"
  14. #include "test/hs_test_helpers.h"
  15. #include "core/or/connection_edge.h"
  16. #include "lib/crypt_ops/crypto_format.h"
  17. #include "lib/crypt_ops/crypto_rand.h"
  18. #include "feature/hs/hs_common.h"
  19. #include "feature/hs/hs_client.h"
  20. #include "feature/hs/hs_service.h"
  21. #include "app/config/config.h"
  22. #include "feature/nodelist/networkstatus.h"
  23. #include "feature/dirclient/dirclient.h"
  24. #include "feature/dirauth/dirvote.h"
  25. #include "feature/nodelist/nodelist.h"
  26. #include "feature/nodelist/routerlist.h"
  27. #include "app/config/statefile.h"
  28. #include "core/or/circuitlist.h"
  29. #include "feature/dirauth/shared_random.h"
  30. #include "feature/dircommon/voting_schedule.h"
  31. #include "feature/nodelist/microdesc_st.h"
  32. #include "feature/nodelist/networkstatus_st.h"
  33. #include "feature/nodelist/node_st.h"
  34. #include "app/config/or_state_st.h"
  35. #include "feature/nodelist/routerinfo_st.h"
  36. #include "feature/nodelist/routerstatus_st.h"
  37. /** Test the validation of HS v3 addresses */
  38. static void
  39. test_validate_address(void *arg)
  40. {
  41. int ret;
  42. (void) arg;
  43. /* Address too short and too long. */
  44. setup_full_capture_of_logs(LOG_WARN);
  45. ret = hs_address_is_valid("blah");
  46. tt_int_op(ret, OP_EQ, 0);
  47. expect_log_msg_containing("has an invalid length");
  48. teardown_capture_of_logs();
  49. setup_full_capture_of_logs(LOG_WARN);
  50. ret = hs_address_is_valid(
  51. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  52. tt_int_op(ret, OP_EQ, 0);
  53. expect_log_msg_containing("has an invalid length");
  54. teardown_capture_of_logs();
  55. /* Invalid checksum (taken from prop224) */
  56. setup_full_capture_of_logs(LOG_WARN);
  57. ret = hs_address_is_valid(
  58. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  59. tt_int_op(ret, OP_EQ, 0);
  60. expect_log_msg_containing("invalid checksum");
  61. teardown_capture_of_logs();
  62. setup_full_capture_of_logs(LOG_WARN);
  63. ret = hs_address_is_valid(
  64. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  65. tt_int_op(ret, OP_EQ, 0);
  66. expect_log_msg_containing("invalid checksum");
  67. teardown_capture_of_logs();
  68. /* Non base32 decodable string. */
  69. setup_full_capture_of_logs(LOG_WARN);
  70. ret = hs_address_is_valid(
  71. "????????????????????????????????????????????????????????");
  72. tt_int_op(ret, OP_EQ, 0);
  73. expect_log_msg_containing("can't be decoded");
  74. teardown_capture_of_logs();
  75. /* Valid address. */
  76. ret = hs_address_is_valid(
  77. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
  78. tt_int_op(ret, OP_EQ, 1);
  79. done:
  80. ;
  81. }
  82. static int
  83. mock_write_str_to_file(const char *path, const char *str, int bin)
  84. {
  85. (void)bin;
  86. tt_str_op(path, OP_EQ, "/double/five"PATH_SEPARATOR"squared");
  87. tt_str_op(str, OP_EQ,
  88. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion\n");
  89. done:
  90. return 0;
  91. }
  92. /** Test building HS v3 onion addresses. Uses test vectors from the
  93. * ./hs_build_address.py script. */
  94. static void
  95. test_build_address(void *arg)
  96. {
  97. int ret;
  98. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  99. ed25519_public_key_t pubkey;
  100. /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  101. char pubkey_hex[] =
  102. "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
  103. hs_service_t *service = NULL;
  104. (void) arg;
  105. MOCK(write_str_to_file, mock_write_str_to_file);
  106. /* The following has been created with hs_build_address.py script that
  107. * follows proposal 224 specification to build an onion address. */
  108. static const char *test_addr =
  109. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid";
  110. /* Let's try to build the same onion address as the script */
  111. base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
  112. pubkey_hex, strlen(pubkey_hex));
  113. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  114. tt_str_op(test_addr, OP_EQ, onion_addr);
  115. /* Validate that address. */
  116. ret = hs_address_is_valid(onion_addr);
  117. tt_int_op(ret, OP_EQ, 1);
  118. service = tor_malloc_zero(sizeof(hs_service_t));
  119. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  120. tor_asprintf(&service->config.directory_path, "/double/five");
  121. ret = write_address_to_file(service, "squared");
  122. tt_int_op(ret, OP_EQ, 0);
  123. done:
  124. hs_service_free(service);
  125. }
  126. /** Test that our HS time period calculation functions work properly */
  127. static void
  128. test_time_period(void *arg)
  129. {
  130. (void) arg;
  131. uint64_t tn;
  132. int retval;
  133. time_t fake_time, correct_time, start_time;
  134. /* Let's do the example in prop224 section [TIME-PERIODS] */
  135. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  136. &fake_time);
  137. tt_int_op(retval, OP_EQ, 0);
  138. /* Check that the time period number is right */
  139. tn = hs_get_time_period_num(fake_time);
  140. tt_u64_op(tn, OP_EQ, 16903);
  141. /* Increase current time to 11:59:59 UTC and check that the time period
  142. number is still the same */
  143. fake_time += 3599;
  144. tn = hs_get_time_period_num(fake_time);
  145. tt_u64_op(tn, OP_EQ, 16903);
  146. { /* Check start time of next time period */
  147. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  148. &correct_time);
  149. tt_int_op(retval, OP_EQ, 0);
  150. start_time = hs_get_start_time_of_next_time_period(fake_time);
  151. tt_int_op(start_time, OP_EQ, correct_time);
  152. }
  153. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  154. fake_time += 1;
  155. tn = hs_get_time_period_num(fake_time);
  156. tt_u64_op(tn, OP_EQ, 16904);
  157. /* Now also check our hs_get_next_time_period_num() function */
  158. tn = hs_get_next_time_period_num(fake_time);
  159. tt_u64_op(tn, OP_EQ, 16905);
  160. { /* Check start time of next time period again */
  161. retval = parse_rfc1123_time("Wed, 14 Apr 2016 12:00:00 UTC",
  162. &correct_time);
  163. tt_int_op(retval, OP_EQ, 0);
  164. start_time = hs_get_start_time_of_next_time_period(fake_time);
  165. tt_int_op(start_time, OP_EQ, correct_time);
  166. }
  167. /* Now do another sanity check: The time period number at the start of the
  168. * next time period, must be the same time period number as the one returned
  169. * from hs_get_next_time_period_num() */
  170. {
  171. time_t next_tp_start = hs_get_start_time_of_next_time_period(fake_time);
  172. tt_u64_op(hs_get_time_period_num(next_tp_start), OP_EQ,
  173. hs_get_next_time_period_num(fake_time));
  174. }
  175. done:
  176. ;
  177. }
  178. /** Test that we can correctly find the start time of the next time period */
  179. static void
  180. test_start_time_of_next_time_period(void *arg)
  181. {
  182. (void) arg;
  183. int retval;
  184. time_t fake_time;
  185. char tbuf[ISO_TIME_LEN + 1];
  186. time_t next_tp_start_time;
  187. /* Do some basic tests */
  188. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  189. &fake_time);
  190. tt_int_op(retval, OP_EQ, 0);
  191. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  192. /* Compare it with the correct result */
  193. format_iso_time(tbuf, next_tp_start_time);
  194. tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
  195. /* Another test with an edge-case time (start of TP) */
  196. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  197. &fake_time);
  198. tt_int_op(retval, OP_EQ, 0);
  199. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  200. format_iso_time(tbuf, next_tp_start_time);
  201. tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
  202. {
  203. /* Now pretend we are on a testing network and alter the voting schedule to
  204. be every 10 seconds. This means that a time period has length 10*24
  205. seconds (4 minutes). It also means that we apply a rotational offset of
  206. 120 seconds to the time period, so that it starts at 00:02:00 instead of
  207. 00:00:00. */
  208. or_options_t *options = get_options_mutable();
  209. options->TestingTorNetwork = 1;
  210. options->V3AuthVotingInterval = 10;
  211. options->TestingV3AuthInitialVotingInterval = 10;
  212. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  213. &fake_time);
  214. tt_int_op(retval, OP_EQ, 0);
  215. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  216. /* Compare it with the correct result */
  217. format_iso_time(tbuf, next_tp_start_time);
  218. tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);
  219. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  220. &fake_time);
  221. tt_int_op(retval, OP_EQ, 0);
  222. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  223. /* Compare it with the correct result */
  224. format_iso_time(tbuf, next_tp_start_time);
  225. tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  226. }
  227. done:
  228. ;
  229. }
  230. /* Cleanup the global nodelist. It also frees the "md" in the node_t because
  231. * we allocate the memory in helper_add_hsdir_to_networkstatus(). */
  232. static void
  233. cleanup_nodelist(void)
  234. {
  235. const smartlist_t *nodelist = nodelist_get_list();
  236. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  237. tor_free(node->md);
  238. node->md = NULL;
  239. } SMARTLIST_FOREACH_END(node);
  240. nodelist_free_all();
  241. }
  242. static void
  243. helper_add_hsdir_to_networkstatus(networkstatus_t *ns,
  244. int identity_idx,
  245. const char *nickname,
  246. int is_hsdir)
  247. {
  248. routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t));
  249. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  250. uint8_t identity[DIGEST_LEN];
  251. tor_addr_t ipv4_addr;
  252. node_t *node = NULL;
  253. memset(identity, identity_idx, sizeof(identity));
  254. memcpy(rs->identity_digest, identity, DIGEST_LEN);
  255. rs->is_hs_dir = is_hsdir;
  256. rs->pv.supports_v3_hsdir = 1;
  257. strlcpy(rs->nickname, nickname, sizeof(rs->nickname));
  258. tor_addr_parse(&ipv4_addr, "1.2.3.4");
  259. ri->addr = tor_addr_to_ipv4h(&ipv4_addr);
  260. rs->addr = tor_addr_to_ipv4h(&ipv4_addr);
  261. ri->nickname = tor_strdup(nickname);
  262. ri->protocol_list = tor_strdup("HSDir=1-2 LinkAuth=3");
  263. memcpy(ri->cache_info.identity_digest, identity, DIGEST_LEN);
  264. ri->cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  265. /* Needed for the HSDir index computation. */
  266. memset(&ri->cache_info.signing_key_cert->signing_key,
  267. identity_idx, ED25519_PUBKEY_LEN);
  268. tt_assert(nodelist_set_routerinfo(ri, NULL));
  269. node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  270. tt_assert(node);
  271. node->rs = rs;
  272. /* We need this to exist for node_has_preferred_descriptor() to return
  273. * true. */
  274. node->md = tor_malloc_zero(sizeof(microdesc_t));
  275. /* Do this now the nodelist_set_routerinfo() function needs a "rs" to set
  276. * the indexes which it doesn't have when it is called. */
  277. node_set_hsdir_index(node, ns);
  278. node->ri = NULL;
  279. smartlist_add(ns->routerstatus_list, rs);
  280. done:
  281. if (node == NULL)
  282. routerstatus_free(rs);
  283. routerinfo_free(ri);
  284. }
  285. static networkstatus_t *mock_ns = NULL;
  286. static networkstatus_t *
  287. mock_networkstatus_get_latest_consensus(void)
  288. {
  289. time_t now = approx_time();
  290. /* If initialized, return it */
  291. if (mock_ns) {
  292. return mock_ns;
  293. }
  294. /* Initialize fake consensus */
  295. mock_ns = tor_malloc_zero(sizeof(networkstatus_t));
  296. /* This consensus is live */
  297. mock_ns->valid_after = now-1;
  298. mock_ns->fresh_until = now+1;
  299. mock_ns->valid_until = now+2;
  300. /* Create routerstatus list */
  301. mock_ns->routerstatus_list = smartlist_new();
  302. mock_ns->type = NS_TYPE_CONSENSUS;
  303. return mock_ns;
  304. }
  305. static networkstatus_t *
  306. mock_networkstatus_get_live_consensus(time_t now)
  307. {
  308. (void) now;
  309. tt_assert(mock_ns);
  310. done:
  311. return mock_ns;
  312. }
  313. /** Test the responsible HSDirs calculation function */
  314. static void
  315. test_responsible_hsdirs(void *arg)
  316. {
  317. smartlist_t *responsible_dirs = smartlist_new();
  318. networkstatus_t *ns = NULL;
  319. (void) arg;
  320. hs_init();
  321. MOCK(networkstatus_get_latest_consensus,
  322. mock_networkstatus_get_latest_consensus);
  323. ns = networkstatus_get_latest_consensus();
  324. { /* First router: HSdir */
  325. helper_add_hsdir_to_networkstatus(ns, 1, "igor", 1);
  326. }
  327. { /* Second HSDir */
  328. helper_add_hsdir_to_networkstatus(ns, 2, "victor", 1);
  329. }
  330. { /* Third relay but not HSDir */
  331. helper_add_hsdir_to_networkstatus(ns, 3, "spyro", 0);
  332. }
  333. /* Use a fixed time period and pub key so we always take the same path */
  334. ed25519_public_key_t pubkey;
  335. uint64_t time_period_num = 17653; // 2 May, 2018, 14:00.
  336. memset(&pubkey, 42, sizeof(pubkey));
  337. hs_get_responsible_hsdirs(&pubkey, time_period_num,
  338. 0, 0, responsible_dirs);
  339. /* Make sure that we only found 2 responsible HSDirs.
  340. * The third relay was not an hsdir! */
  341. tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 2);
  342. /** TODO: Build a bigger network and do more tests here */
  343. done:
  344. SMARTLIST_FOREACH(ns->routerstatus_list,
  345. routerstatus_t *, rs, routerstatus_free(rs));
  346. smartlist_free(responsible_dirs);
  347. smartlist_clear(ns->routerstatus_list);
  348. networkstatus_vote_free(mock_ns);
  349. cleanup_nodelist();
  350. }
  351. static void
  352. mock_directory_initiate_request(directory_request_t *req)
  353. {
  354. (void)req;
  355. return;
  356. }
  357. static int
  358. mock_hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  359. const ed25519_keypair_t *signing_kp,
  360. const uint8_t *descriptor_cookie,
  361. char **encoded_out)
  362. {
  363. (void)desc;
  364. (void)signing_kp;
  365. (void)descriptor_cookie;
  366. tor_asprintf(encoded_out, "lulu");
  367. return 0;
  368. }
  369. static or_state_t dummy_state;
  370. /* Mock function to get fake or state (used for rev counters) */
  371. static or_state_t *
  372. get_or_state_replacement(void)
  373. {
  374. return &dummy_state;
  375. }
  376. static int
  377. mock_router_have_minimum_dir_info(void)
  378. {
  379. return 1;
  380. }
  381. /** Test that we correctly detect when the HSDir hash ring changes so that we
  382. * reupload our descriptor. */
  383. static void
  384. test_desc_reupload_logic(void *arg)
  385. {
  386. networkstatus_t *ns = NULL;
  387. (void) arg;
  388. hs_init();
  389. MOCK(router_have_minimum_dir_info,
  390. mock_router_have_minimum_dir_info);
  391. MOCK(get_or_state,
  392. get_or_state_replacement);
  393. MOCK(networkstatus_get_latest_consensus,
  394. mock_networkstatus_get_latest_consensus);
  395. MOCK(directory_initiate_request,
  396. mock_directory_initiate_request);
  397. MOCK(hs_desc_encode_descriptor,
  398. mock_hs_desc_encode_descriptor);
  399. ns = networkstatus_get_latest_consensus();
  400. /** Test logic:
  401. * 1) Upload descriptor to HSDirs
  402. * CHECK that previous_hsdirs list was populated.
  403. * 2) Then call router_dir_info_changed() without an HSDir set change.
  404. * CHECK that no reuplod occurs.
  405. * 3) Now change the HSDir set, and call dir_info_changed() again.
  406. * CHECK that reupload occurs.
  407. * 4) Finally call service_desc_schedule_upload().
  408. * CHECK that previous_hsdirs list was cleared.
  409. **/
  410. /* Let's start by building our descriptor and service */
  411. hs_service_descriptor_t *desc = service_descriptor_new();
  412. hs_service_t *service = NULL;
  413. /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  414. char pubkey_hex[] =
  415. "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
  416. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  417. ed25519_public_key_t pubkey;
  418. base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
  419. pubkey_hex, strlen(pubkey_hex));
  420. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  421. service = tor_malloc_zero(sizeof(hs_service_t));
  422. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  423. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  424. ed25519_public_key_generate(&service->keys.identity_pk,
  425. &service->keys.identity_sk);
  426. service->desc_current = desc;
  427. /* Also add service to service map */
  428. hs_service_ht *service_map = get_hs_service_map();
  429. tt_assert(service_map);
  430. tt_int_op(hs_service_get_num_services(), OP_EQ, 0);
  431. register_service(service_map, service);
  432. tt_int_op(hs_service_get_num_services(), OP_EQ, 1);
  433. /* Now let's create our hash ring: */
  434. {
  435. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  436. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  437. helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
  438. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  439. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  440. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  441. }
  442. /* Now let's upload our desc to all hsdirs */
  443. upload_descriptor_to_all(service, desc);
  444. /* Check that previous hsdirs were populated */
  445. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  446. /* Poison next upload time so that we can see if it was changed by
  447. * router_dir_info_changed(). No changes in hash ring so far, so the upload
  448. * time should stay as is. */
  449. desc->next_upload_time = 42;
  450. router_dir_info_changed();
  451. tt_int_op(desc->next_upload_time, OP_EQ, 42);
  452. /* Now change the HSDir hash ring by swapping nora for aaron.
  453. * Start by clearing the hash ring */
  454. {
  455. SMARTLIST_FOREACH(ns->routerstatus_list,
  456. routerstatus_t *, rs, routerstatus_free(rs));
  457. smartlist_clear(ns->routerstatus_list);
  458. cleanup_nodelist();
  459. routerlist_free_all();
  460. }
  461. { /* Now add back all the nodes */
  462. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  463. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  464. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  465. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  466. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  467. helper_add_hsdir_to_networkstatus(ns, 7, "nora", 1);
  468. }
  469. /* Now call service_desc_hsdirs_changed() and see that it detected the hash
  470. ring change */
  471. time_t now = approx_time();
  472. tt_assert(now);
  473. tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
  474. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  475. /* Now order another upload and see that we keep having 6 prev hsdirs */
  476. upload_descriptor_to_all(service, desc);
  477. /* Check that previous hsdirs were populated */
  478. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  479. /* Now restore the HSDir hash ring to its original state by swapping back
  480. aaron for nora */
  481. /* First clear up the hash ring */
  482. {
  483. SMARTLIST_FOREACH(ns->routerstatus_list,
  484. routerstatus_t *, rs, routerstatus_free(rs));
  485. smartlist_clear(ns->routerstatus_list);
  486. cleanup_nodelist();
  487. routerlist_free_all();
  488. }
  489. { /* Now populate the hash ring again */
  490. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  491. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  492. helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
  493. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  494. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  495. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  496. }
  497. /* Check that our algorithm catches this change of hsdirs */
  498. tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
  499. /* Now pretend that the descriptor changed, and order a reupload to all
  500. HSDirs. Make sure that the set of previous HSDirs was cleared. */
  501. service_desc_schedule_upload(desc, now, 1);
  502. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 0);
  503. /* Now reupload again: see that the prev hsdir set got populated again. */
  504. upload_descriptor_to_all(service, desc);
  505. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  506. done:
  507. SMARTLIST_FOREACH(ns->routerstatus_list,
  508. routerstatus_t *, rs, routerstatus_free(rs));
  509. smartlist_clear(ns->routerstatus_list);
  510. if (service) {
  511. remove_service(get_hs_service_map(), service);
  512. hs_service_free(service);
  513. }
  514. networkstatus_vote_free(ns);
  515. cleanup_nodelist();
  516. hs_free_all();
  517. }
  518. /** Test disaster SRV computation and caching */
  519. static void
  520. test_disaster_srv(void *arg)
  521. {
  522. uint8_t *cached_disaster_srv_one = NULL;
  523. uint8_t *cached_disaster_srv_two = NULL;
  524. uint8_t srv_one[DIGEST256_LEN] = {0};
  525. uint8_t srv_two[DIGEST256_LEN] = {0};
  526. uint8_t srv_three[DIGEST256_LEN] = {0};
  527. uint8_t srv_four[DIGEST256_LEN] = {0};
  528. uint8_t srv_five[DIGEST256_LEN] = {0};
  529. (void) arg;
  530. /* Get the cached SRVs: we gonna use them later for verification */
  531. cached_disaster_srv_one = get_first_cached_disaster_srv();
  532. cached_disaster_srv_two = get_second_cached_disaster_srv();
  533. /* Compute some srvs */
  534. get_disaster_srv(1, srv_one);
  535. get_disaster_srv(2, srv_two);
  536. /* Check that the cached ones were updated */
  537. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  538. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  539. /* Ask for an SRV that has already been computed */
  540. get_disaster_srv(2, srv_two);
  541. /* and check that the cache entries have not changed */
  542. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  543. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  544. /* Ask for a new SRV */
  545. get_disaster_srv(3, srv_three);
  546. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  547. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  548. /* Ask for another SRV: none of the original SRVs should now be cached */
  549. get_disaster_srv(4, srv_four);
  550. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  551. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  552. /* Ask for yet another SRV */
  553. get_disaster_srv(5, srv_five);
  554. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  555. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  556. done:
  557. ;
  558. }
  559. /** Test our HS descriptor request tracker by making various requests and
  560. * checking whether they get tracked properly. */
  561. static void
  562. test_hid_serv_request_tracker(void *arg)
  563. {
  564. (void) arg;
  565. time_t retval;
  566. routerstatus_t *hsdir = NULL, *hsdir2 = NULL, *hsdir3 = NULL;
  567. time_t now = approx_time();
  568. const char *req_key_str_first =
  569. "vd4zb6zesaubtrjvdqcr2w7x7lhw2up4Xnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  570. const char *req_key_str_second =
  571. "g53o7iavcd62oihswhr24u6czmqws5kpXnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  572. const char *req_key_str_small = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
  573. /*************************** basic test *******************************/
  574. /* Get request tracker and make sure it's empty */
  575. strmap_t *request_tracker = get_last_hid_serv_requests();
  576. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  577. /* Let's register a hid serv request */
  578. hsdir = tor_malloc_zero(sizeof(routerstatus_t));
  579. memset(hsdir->identity_digest, 'Z', DIGEST_LEN);
  580. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  581. now, 1);
  582. tt_int_op(retval, OP_EQ, now);
  583. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  584. /* Let's lookup a non-existent hidserv request */
  585. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_second,
  586. now+1, 0);
  587. tt_int_op(retval, OP_EQ, 0);
  588. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  589. /* Let's lookup a real hidserv request */
  590. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  591. now+2, 0);
  592. tt_int_op(retval, OP_EQ, now); /* we got it */
  593. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  594. /**********************************************************************/
  595. /* Let's add another request for the same HS but on a different HSDir. */
  596. hsdir2 = tor_malloc_zero(sizeof(routerstatus_t));
  597. memset(hsdir2->identity_digest, 2, DIGEST_LEN);
  598. retval = hs_lookup_last_hid_serv_request(hsdir2, req_key_str_first,
  599. now+3, 1);
  600. tt_int_op(retval, OP_EQ, now+3);
  601. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  602. /* Check that we can clean the first request based on time */
  603. hs_clean_last_hid_serv_requests(now+3+REND_HID_SERV_DIR_REQUERY_PERIOD);
  604. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  605. /* Check that it doesn't exist anymore */
  606. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  607. now+2, 0);
  608. tt_int_op(retval, OP_EQ, 0);
  609. /* Now let's add a smaller req key str */
  610. hsdir3 = tor_malloc_zero(sizeof(routerstatus_t));
  611. memset(hsdir3->identity_digest, 3, DIGEST_LEN);
  612. retval = hs_lookup_last_hid_serv_request(hsdir3, req_key_str_small,
  613. now+4, 1);
  614. tt_int_op(retval, OP_EQ, now+4);
  615. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  616. /*************************** deleting entries **************************/
  617. /* Add another request with very short key */
  618. retval = hs_lookup_last_hid_serv_request(hsdir, "l", now, 1);
  619. tt_int_op(retval, OP_EQ, now);
  620. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  621. /* Try deleting entries with a dummy key. Check that our previous requests
  622. * are still there */
  623. tor_capture_bugs_(1);
  624. hs_purge_hid_serv_from_last_hid_serv_requests("a");
  625. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  626. tor_end_capture_bugs_();
  627. /* Try another dummy key. Check that requests are still there */
  628. {
  629. char dummy[2000];
  630. memset(dummy, 'Z', 2000);
  631. dummy[1999] = '\x00';
  632. hs_purge_hid_serv_from_last_hid_serv_requests(dummy);
  633. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  634. }
  635. /* Another dummy key! */
  636. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_second);
  637. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  638. /* Now actually delete a request! */
  639. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_first);
  640. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  641. /* Purge it all! */
  642. hs_purge_last_hid_serv_requests();
  643. request_tracker = get_last_hid_serv_requests();
  644. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  645. done:
  646. tor_free(hsdir);
  647. tor_free(hsdir2);
  648. tor_free(hsdir3);
  649. }
  650. static void
  651. test_parse_extended_hostname(void *arg)
  652. {
  653. (void) arg;
  654. char address1[] = "fooaddress.onion";
  655. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  656. char address3[] = "fooaddress.exit";
  657. char address4[] = "www.torproject.org";
  658. char address5[] = "foo.abcdefghijklmnop.onion";
  659. char address6[] = "foo.bar.abcdefghijklmnop.onion";
  660. char address7[] = ".abcdefghijklmnop.onion";
  661. char address8[] =
  662. "www.25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion";
  663. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  664. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address2));
  665. tt_str_op(address2,OP_EQ, "aaaaaaaaaaaaaaaa");
  666. tt_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  667. tt_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  668. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address5));
  669. tt_str_op(address5,OP_EQ, "abcdefghijklmnop");
  670. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address6));
  671. tt_str_op(address6,OP_EQ, "abcdefghijklmnop");
  672. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
  673. tt_assert(ONION_V3_HOSTNAME == parse_extended_hostname(address8));
  674. tt_str_op(address8, OP_EQ,
  675. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
  676. done: ;
  677. }
  678. static void
  679. test_time_between_tp_and_srv(void *arg)
  680. {
  681. int ret;
  682. networkstatus_t ns;
  683. (void) arg;
  684. /* This function should be returning true where "^" are:
  685. *
  686. * +------------------------------------------------------------------+
  687. * | |
  688. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  689. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  690. * | |
  691. * | $==========|-----------$===========|-----------$===========| |
  692. * | ^^^^^^^^^^^^ ^^^^^^^^^^^^ |
  693. * | |
  694. * +------------------------------------------------------------------+
  695. */
  696. ret = parse_rfc1123_time("Sat, 26 Oct 1985 00:00:00 UTC", &ns.valid_after);
  697. tt_int_op(ret, OP_EQ, 0);
  698. ret = parse_rfc1123_time("Sat, 26 Oct 1985 01:00:00 UTC", &ns.fresh_until);
  699. tt_int_op(ret, OP_EQ, 0);
  700. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  701. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  702. tt_int_op(ret, OP_EQ, 0);
  703. ret = parse_rfc1123_time("Sat, 26 Oct 1985 11:00:00 UTC", &ns.valid_after);
  704. tt_int_op(ret, OP_EQ, 0);
  705. ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.fresh_until);
  706. tt_int_op(ret, OP_EQ, 0);
  707. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  708. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  709. tt_int_op(ret, OP_EQ, 0);
  710. ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.valid_after);
  711. tt_int_op(ret, OP_EQ, 0);
  712. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC", &ns.fresh_until);
  713. tt_int_op(ret, OP_EQ, 0);
  714. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  715. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  716. tt_int_op(ret, OP_EQ, 1);
  717. ret = parse_rfc1123_time("Sat, 26 Oct 1985 23:00:00 UTC", &ns.valid_after);
  718. tt_int_op(ret, OP_EQ, 0);
  719. ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.fresh_until);
  720. tt_int_op(ret, OP_EQ, 0);
  721. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  722. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  723. tt_int_op(ret, OP_EQ, 1);
  724. ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.valid_after);
  725. tt_int_op(ret, OP_EQ, 0);
  726. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC", &ns.fresh_until);
  727. tt_int_op(ret, OP_EQ, 0);
  728. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  729. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  730. tt_int_op(ret, OP_EQ, 0);
  731. done:
  732. ;
  733. }
  734. /************ Reachability Test (it is huge) ****************/
  735. /* Simulate different consensus for client and service. Used by the
  736. * reachability test. The SRV and responsible HSDir list are used by all
  737. * reachability tests so make them common to simplify setup and teardown. */
  738. static networkstatus_t *mock_service_ns = NULL;
  739. static networkstatus_t *mock_client_ns = NULL;
  740. static sr_srv_t current_srv, previous_srv;
  741. static smartlist_t *service_responsible_hsdirs = NULL;
  742. static smartlist_t *client_responsible_hsdirs = NULL;
  743. static networkstatus_t *
  744. mock_networkstatus_get_live_consensus_service(time_t now)
  745. {
  746. (void) now;
  747. if (mock_service_ns) {
  748. return mock_service_ns;
  749. }
  750. mock_service_ns = tor_malloc_zero(sizeof(networkstatus_t));
  751. mock_service_ns->routerstatus_list = smartlist_new();
  752. mock_service_ns->type = NS_TYPE_CONSENSUS;
  753. return mock_service_ns;
  754. }
  755. static networkstatus_t *
  756. mock_networkstatus_get_latest_consensus_service(void)
  757. {
  758. return mock_networkstatus_get_live_consensus_service(0);
  759. }
  760. static networkstatus_t *
  761. mock_networkstatus_get_live_consensus_client(time_t now)
  762. {
  763. (void) now;
  764. if (mock_client_ns) {
  765. return mock_client_ns;
  766. }
  767. mock_client_ns = tor_malloc_zero(sizeof(networkstatus_t));
  768. mock_client_ns->routerstatus_list = smartlist_new();
  769. mock_client_ns->type = NS_TYPE_CONSENSUS;
  770. return mock_client_ns;
  771. }
  772. static networkstatus_t *
  773. mock_networkstatus_get_latest_consensus_client(void)
  774. {
  775. return mock_networkstatus_get_live_consensus_client(0);
  776. }
  777. /* Mock function because we are not trying to test the close circuit that does
  778. * an awful lot of checks on the circuit object. */
  779. static void
  780. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  781. const char *file)
  782. {
  783. (void) circ;
  784. (void) reason;
  785. (void) line;
  786. (void) file;
  787. return;
  788. }
  789. /* Initialize a big HSDir V3 hash ring. */
  790. static void
  791. helper_initialize_big_hash_ring(networkstatus_t *ns)
  792. {
  793. int ret;
  794. /* Generate 250 hsdirs! :) */
  795. for (int counter = 1 ; counter < 251 ; counter++) {
  796. /* Let's generate random nickname for each hsdir... */
  797. char nickname_binary[8];
  798. char nickname_str[13] = {0};
  799. crypto_rand(nickname_binary, sizeof(nickname_binary));
  800. ret = base64_encode(nickname_str, sizeof(nickname_str),
  801. nickname_binary, sizeof(nickname_binary), 0);
  802. tt_int_op(ret, OP_EQ, 12);
  803. helper_add_hsdir_to_networkstatus(ns, counter, nickname_str, 1);
  804. }
  805. /* Make sure we have 200 hsdirs in our list */
  806. tt_int_op(smartlist_len(ns->routerstatus_list), OP_EQ, 250);
  807. done:
  808. ;
  809. }
  810. /** Initialize service and publish its descriptor as needed. Return the newly
  811. * allocated service object to the caller. */
  812. static hs_service_t *
  813. helper_init_service(time_t now)
  814. {
  815. int retval;
  816. hs_service_t *service = hs_service_new(get_options());
  817. tt_assert(service);
  818. service->config.version = HS_VERSION_THREE;
  819. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  820. ed25519_public_key_generate(&service->keys.identity_pk,
  821. &service->keys.identity_sk);
  822. /* Register service to global map. */
  823. retval = register_service(get_hs_service_map(), service);
  824. tt_int_op(retval, OP_EQ, 0);
  825. /* Initialize service descriptor */
  826. build_all_descriptors(now);
  827. tt_assert(service->desc_current);
  828. tt_assert(service->desc_next);
  829. done:
  830. return service;
  831. }
  832. /* Helper function to set the RFC 1123 time string into t. */
  833. static void
  834. set_consensus_times(const char *timestr, time_t *t)
  835. {
  836. tt_assert(timestr);
  837. tt_assert(t);
  838. int ret = parse_rfc1123_time(timestr, t);
  839. tt_int_op(ret, OP_EQ, 0);
  840. done:
  841. return;
  842. }
  843. /* Helper function to cleanup the mock consensus (client and service) */
  844. static void
  845. cleanup_mock_ns(void)
  846. {
  847. if (mock_service_ns) {
  848. SMARTLIST_FOREACH(mock_service_ns->routerstatus_list,
  849. routerstatus_t *, rs, routerstatus_free(rs));
  850. smartlist_clear(mock_service_ns->routerstatus_list);
  851. mock_service_ns->sr_info.current_srv = NULL;
  852. mock_service_ns->sr_info.previous_srv = NULL;
  853. networkstatus_vote_free(mock_service_ns);
  854. mock_service_ns = NULL;
  855. }
  856. if (mock_client_ns) {
  857. SMARTLIST_FOREACH(mock_client_ns->routerstatus_list,
  858. routerstatus_t *, rs, routerstatus_free(rs));
  859. smartlist_clear(mock_client_ns->routerstatus_list);
  860. mock_client_ns->sr_info.current_srv = NULL;
  861. mock_client_ns->sr_info.previous_srv = NULL;
  862. networkstatus_vote_free(mock_client_ns);
  863. mock_client_ns = NULL;
  864. }
  865. }
  866. /* Helper function to setup a reachability test. Once called, the
  867. * cleanup_reachability_test MUST be called at the end. */
  868. static void
  869. setup_reachability_test(void)
  870. {
  871. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  872. MOCK(get_or_state, get_or_state_replacement);
  873. hs_init();
  874. /* Baseline to start with. */
  875. memset(&current_srv, 0, sizeof(current_srv));
  876. memset(&previous_srv, 1, sizeof(previous_srv));
  877. /* Initialize the consensuses. */
  878. mock_networkstatus_get_latest_consensus_service();
  879. mock_networkstatus_get_latest_consensus_client();
  880. service_responsible_hsdirs = smartlist_new();
  881. client_responsible_hsdirs = smartlist_new();
  882. }
  883. /* Helper function to cleanup a reachability test initial setup. */
  884. static void
  885. cleanup_reachability_test(void)
  886. {
  887. smartlist_free(service_responsible_hsdirs);
  888. service_responsible_hsdirs = NULL;
  889. smartlist_free(client_responsible_hsdirs);
  890. client_responsible_hsdirs = NULL;
  891. hs_free_all();
  892. cleanup_mock_ns();
  893. UNMOCK(get_or_state);
  894. UNMOCK(circuit_mark_for_close_);
  895. }
  896. /* A reachability test always check if the resulting service and client
  897. * responsible HSDir for the given parameters are equal.
  898. *
  899. * Return true iff the same exact nodes are in both list. */
  900. static int
  901. are_responsible_hsdirs_equal(void)
  902. {
  903. int count = 0;
  904. tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);
  905. tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
  906. SMARTLIST_FOREACH_BEGIN(client_responsible_hsdirs,
  907. const routerstatus_t *, c_rs) {
  908. SMARTLIST_FOREACH_BEGIN(service_responsible_hsdirs,
  909. const routerstatus_t *, s_rs) {
  910. if (tor_memeq(c_rs->identity_digest, s_rs->identity_digest,
  911. DIGEST_LEN)) {
  912. count++;
  913. break;
  914. }
  915. } SMARTLIST_FOREACH_END(s_rs);
  916. } SMARTLIST_FOREACH_END(c_rs);
  917. done:
  918. return (count == 6);
  919. }
  920. /* Tor doesn't use such a function to get the previous HSDir, it is only used
  921. * in node_set_hsdir_index(). We need it here so we can test the reachability
  922. * scenario 6 that requires the previous time period to compute the list of
  923. * responsible HSDir because of the client state timing. */
  924. static uint64_t
  925. get_previous_time_period(time_t now)
  926. {
  927. return hs_get_time_period_num(now) - 1;
  928. }
  929. /* Configuration of a reachability test scenario. */
  930. typedef struct reachability_cfg_t {
  931. /* Consensus timings to be set. They have to be compliant with
  932. * RFC 1123 time format. */
  933. const char *service_valid_after;
  934. const char *service_valid_until;
  935. const char *client_valid_after;
  936. const char *client_valid_until;
  937. /* SRVs that the service and client should use. */
  938. sr_srv_t *service_current_srv;
  939. sr_srv_t *service_previous_srv;
  940. sr_srv_t *client_current_srv;
  941. sr_srv_t *client_previous_srv;
  942. /* A time period function for the service to use for this scenario. For a
  943. * successful reachability test, the client always use the current time
  944. * period thus why no client function. */
  945. uint64_t (*service_time_period_fn)(time_t);
  946. /* Is the client and service expected to be in a new time period. After
  947. * setting the consensus time, the reachability test checks
  948. * hs_in_period_between_tp_and_srv() and test the returned value against
  949. * this. */
  950. unsigned int service_in_new_tp;
  951. unsigned int client_in_new_tp;
  952. /* Some scenario requires a hint that the client, because of its consensus
  953. * time, will request the "next" service descriptor so this indicates if it
  954. * is the case or not. */
  955. unsigned int client_fetch_next_desc;
  956. } reachability_cfg_t;
  957. /* Some defines to help with semantic while reading a configuration below. */
  958. #define NOT_IN_NEW_TP 0
  959. #define IN_NEW_TP 1
  960. #define DONT_NEED_NEXT_DESC 0
  961. #define NEED_NEXT_DESC 1
  962. static reachability_cfg_t reachability_scenarios[] = {
  963. /* Scenario 1
  964. *
  965. * +------------------------------------------------------------------+
  966. * | |
  967. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  968. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  969. * | |
  970. * | $==========|-----------$===========|-----------$===========| |
  971. * | ^ ^ |
  972. * | S C |
  973. * +------------------------------------------------------------------+
  974. *
  975. * S: Service, C: Client
  976. *
  977. * Service consensus valid_after time is set to 13:00 and client to 15:00,
  978. * both are after TP#1 thus have access to SRV#1. Service and client should
  979. * be using TP#1.
  980. */
  981. { "Sat, 26 Oct 1985 13:00:00 UTC", /* Service valid_after */
  982. "Sat, 26 Oct 1985 14:00:00 UTC", /* Service valid_until */
  983. "Sat, 26 Oct 1985 15:00:00 UTC", /* Client valid_after */
  984. "Sat, 26 Oct 1985 16:00:00 UTC", /* Client valid_until. */
  985. &current_srv, NULL, /* Service current and previous SRV */
  986. &current_srv, NULL, /* Client current and previous SRV */
  987. hs_get_time_period_num, /* Service time period function. */
  988. IN_NEW_TP, /* Is service in new TP? */
  989. IN_NEW_TP, /* Is client in new TP? */
  990. NEED_NEXT_DESC },
  991. /* Scenario 2
  992. *
  993. * +------------------------------------------------------------------+
  994. * | |
  995. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  996. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  997. * | |
  998. * | $==========|-----------$===========|-----------$===========| |
  999. * | ^ ^ |
  1000. * | S C |
  1001. * +------------------------------------------------------------------+
  1002. *
  1003. * S: Service, C: Client
  1004. *
  1005. * Service consensus valid_after time is set to 23:00 and client to 01:00,
  1006. * which makes the client after the SRV#2 and the service just before. The
  1007. * service should only be using TP#1. The client should be using TP#1.
  1008. */
  1009. { "Sat, 26 Oct 1985 23:00:00 UTC", /* Service valid_after */
  1010. "Sat, 27 Oct 1985 00:00:00 UTC", /* Service valid_until */
  1011. "Sat, 27 Oct 1985 01:00:00 UTC", /* Client valid_after */
  1012. "Sat, 27 Oct 1985 02:00:00 UTC", /* Client valid_until. */
  1013. &previous_srv, NULL, /* Service current and previous SRV */
  1014. &current_srv, &previous_srv, /* Client current and previous SRV */
  1015. hs_get_time_period_num, /* Service time period function. */
  1016. IN_NEW_TP, /* Is service in new TP? */
  1017. NOT_IN_NEW_TP, /* Is client in new TP? */
  1018. NEED_NEXT_DESC },
  1019. /* Scenario 3
  1020. *
  1021. * +------------------------------------------------------------------+
  1022. * | |
  1023. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1024. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1025. * | |
  1026. * | $==========|-----------$===========|----------$===========| |
  1027. * | ^ ^ |
  1028. * | S C |
  1029. * +------------------------------------------------------------------+
  1030. *
  1031. * S: Service, C: Client
  1032. *
  1033. * Service consensus valid_after time is set to 03:00 and client to 05:00,
  1034. * which makes both after SRV#2. The service should be using TP#1 as its
  1035. * current time period. The client should be using TP#1.
  1036. */
  1037. { "Sat, 27 Oct 1985 03:00:00 UTC", /* Service valid_after */
  1038. "Sat, 27 Oct 1985 04:00:00 UTC", /* Service valid_until */
  1039. "Sat, 27 Oct 1985 05:00:00 UTC", /* Client valid_after */
  1040. "Sat, 27 Oct 1985 06:00:00 UTC", /* Client valid_until. */
  1041. &current_srv, &previous_srv, /* Service current and previous SRV */
  1042. &current_srv, &previous_srv, /* Client current and previous SRV */
  1043. hs_get_time_period_num, /* Service time period function. */
  1044. NOT_IN_NEW_TP, /* Is service in new TP? */
  1045. NOT_IN_NEW_TP, /* Is client in new TP? */
  1046. DONT_NEED_NEXT_DESC },
  1047. /* Scenario 4
  1048. *
  1049. * +------------------------------------------------------------------+
  1050. * | |
  1051. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1052. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1053. * | |
  1054. * | $==========|-----------$===========|-----------$===========| |
  1055. * | ^ ^ |
  1056. * | S C |
  1057. * +------------------------------------------------------------------+
  1058. *
  1059. * S: Service, C: Client
  1060. *
  1061. * Service consensus valid_after time is set to 11:00 and client to 13:00,
  1062. * which makes the service before TP#2 and the client just after. The
  1063. * service should be using TP#1 as its current time period and TP#2 as the
  1064. * next. The client should be using TP#2 time period.
  1065. */
  1066. { "Sat, 27 Oct 1985 11:00:00 UTC", /* Service valid_after */
  1067. "Sat, 27 Oct 1985 12:00:00 UTC", /* Service valid_until */
  1068. "Sat, 27 Oct 1985 13:00:00 UTC", /* Client valid_after */
  1069. "Sat, 27 Oct 1985 14:00:00 UTC", /* Client valid_until. */
  1070. &current_srv, &previous_srv, /* Service current and previous SRV */
  1071. &current_srv, &previous_srv, /* Client current and previous SRV */
  1072. hs_get_next_time_period_num, /* Service time period function. */
  1073. NOT_IN_NEW_TP, /* Is service in new TP? */
  1074. IN_NEW_TP, /* Is client in new TP? */
  1075. NEED_NEXT_DESC },
  1076. /* Scenario 5
  1077. *
  1078. * +------------------------------------------------------------------+
  1079. * | |
  1080. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1081. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1082. * | |
  1083. * | $==========|-----------$===========|-----------$===========| |
  1084. * | ^ ^ |
  1085. * | C S |
  1086. * +------------------------------------------------------------------+
  1087. *
  1088. * S: Service, C: Client
  1089. *
  1090. * Service consensus valid_after time is set to 01:00 and client to 23:00,
  1091. * which makes the service after SRV#2 and the client just before. The
  1092. * service should be using TP#1 as its current time period and TP#2 as the
  1093. * next. The client should be using TP#1 time period.
  1094. */
  1095. { "Sat, 27 Oct 1985 01:00:00 UTC", /* Service valid_after */
  1096. "Sat, 27 Oct 1985 02:00:00 UTC", /* Service valid_until */
  1097. "Sat, 26 Oct 1985 23:00:00 UTC", /* Client valid_after */
  1098. "Sat, 27 Oct 1985 00:00:00 UTC", /* Client valid_until. */
  1099. &current_srv, &previous_srv, /* Service current and previous SRV */
  1100. &previous_srv, NULL, /* Client current and previous SRV */
  1101. hs_get_time_period_num, /* Service time period function. */
  1102. NOT_IN_NEW_TP, /* Is service in new TP? */
  1103. IN_NEW_TP, /* Is client in new TP? */
  1104. DONT_NEED_NEXT_DESC },
  1105. /* Scenario 6
  1106. *
  1107. * +------------------------------------------------------------------+
  1108. * | |
  1109. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1110. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1111. * | |
  1112. * | $==========|-----------$===========|-----------$===========| |
  1113. * | ^ ^ |
  1114. * | C S |
  1115. * +------------------------------------------------------------------+
  1116. *
  1117. * S: Service, C: Client
  1118. *
  1119. * Service consensus valid_after time is set to 13:00 and client to 11:00,
  1120. * which makes the service outside after TP#2 and the client just before.
  1121. * The service should be using TP#1 as its current time period and TP#2 as
  1122. * its next. The client should be using TP#1 time period.
  1123. */
  1124. { "Sat, 27 Oct 1985 13:00:00 UTC", /* Service valid_after */
  1125. "Sat, 27 Oct 1985 14:00:00 UTC", /* Service valid_until */
  1126. "Sat, 27 Oct 1985 11:00:00 UTC", /* Client valid_after */
  1127. "Sat, 27 Oct 1985 12:00:00 UTC", /* Client valid_until. */
  1128. &current_srv, &previous_srv, /* Service current and previous SRV */
  1129. &current_srv, &previous_srv, /* Client current and previous SRV */
  1130. get_previous_time_period, /* Service time period function. */
  1131. IN_NEW_TP, /* Is service in new TP? */
  1132. NOT_IN_NEW_TP, /* Is client in new TP? */
  1133. DONT_NEED_NEXT_DESC },
  1134. /* End marker. */
  1135. { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0}
  1136. };
  1137. /* Run a single reachability scenario. num_scenario is the corresponding
  1138. * scenario number from the documentation. It is used to log it in case of
  1139. * failure so we know which scenario fails. */
  1140. static int
  1141. run_reachability_scenario(const reachability_cfg_t *cfg, int num_scenario)
  1142. {
  1143. int ret = -1;
  1144. hs_service_t *service;
  1145. uint64_t service_tp, client_tp;
  1146. ed25519_public_key_t service_blinded_pk, client_blinded_pk;
  1147. setup_reachability_test();
  1148. tt_assert(cfg);
  1149. /* Set service consensus time. */
  1150. set_consensus_times(cfg->service_valid_after,
  1151. &mock_service_ns->valid_after);
  1152. set_consensus_times(cfg->service_valid_until,
  1153. &mock_service_ns->valid_until);
  1154. set_consensus_times(cfg->service_valid_until,
  1155. &mock_service_ns->fresh_until);
  1156. voting_schedule_recalculate_timing(get_options(),
  1157. mock_service_ns->valid_after);
  1158. /* Check that service is in the right time period point */
  1159. tt_int_op(hs_in_period_between_tp_and_srv(mock_service_ns, 0), OP_EQ,
  1160. cfg->service_in_new_tp);
  1161. /* Set client consensus time. */
  1162. set_consensus_times(cfg->client_valid_after,
  1163. &mock_client_ns->valid_after);
  1164. set_consensus_times(cfg->client_valid_until,
  1165. &mock_client_ns->valid_until);
  1166. set_consensus_times(cfg->client_valid_until,
  1167. &mock_client_ns->fresh_until);
  1168. voting_schedule_recalculate_timing(get_options(),
  1169. mock_client_ns->valid_after);
  1170. /* Check that client is in the right time period point */
  1171. tt_int_op(hs_in_period_between_tp_and_srv(mock_client_ns, 0), OP_EQ,
  1172. cfg->client_in_new_tp);
  1173. /* Set the SRVs for this scenario. */
  1174. mock_client_ns->sr_info.current_srv = cfg->client_current_srv;
  1175. mock_client_ns->sr_info.previous_srv = cfg->client_previous_srv;
  1176. mock_service_ns->sr_info.current_srv = cfg->service_current_srv;
  1177. mock_service_ns->sr_info.previous_srv = cfg->service_previous_srv;
  1178. /* Initialize a service to get keys. */
  1179. update_approx_time(mock_service_ns->valid_after);
  1180. service = helper_init_service(mock_service_ns->valid_after+1);
  1181. /*
  1182. * === Client setup ===
  1183. */
  1184. MOCK(networkstatus_get_live_consensus,
  1185. mock_networkstatus_get_live_consensus_client);
  1186. MOCK(networkstatus_get_latest_consensus,
  1187. mock_networkstatus_get_latest_consensus_client);
  1188. /* Make networkstatus_is_live() happy. */
  1189. update_approx_time(mock_client_ns->valid_after);
  1190. /* Initialize a big hashring for this consensus with the hsdir index set. */
  1191. helper_initialize_big_hash_ring(mock_client_ns);
  1192. /* Client ONLY use the current time period. This is the whole point of these
  1193. * reachability test that is to make sure the client can always reach the
  1194. * service using only its current time period. */
  1195. client_tp = hs_get_time_period_num(0);
  1196. hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
  1197. client_tp, &client_blinded_pk);
  1198. hs_get_responsible_hsdirs(&client_blinded_pk, client_tp, 0, 1,
  1199. client_responsible_hsdirs);
  1200. /* Cleanup the nodelist so we can let the service computes its own set of
  1201. * node with its own hashring. */
  1202. cleanup_nodelist();
  1203. tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);
  1204. UNMOCK(networkstatus_get_latest_consensus);
  1205. UNMOCK(networkstatus_get_live_consensus);
  1206. /*
  1207. * === Service setup ===
  1208. */
  1209. MOCK(networkstatus_get_live_consensus,
  1210. mock_networkstatus_get_live_consensus_service);
  1211. MOCK(networkstatus_get_latest_consensus,
  1212. mock_networkstatus_get_latest_consensus_service);
  1213. /* Make networkstatus_is_live() happy. */
  1214. update_approx_time(mock_service_ns->valid_after);
  1215. /* Initialize a big hashring for this consensus with the hsdir index set. */
  1216. helper_initialize_big_hash_ring(mock_service_ns);
  1217. service_tp = cfg->service_time_period_fn(0);
  1218. hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
  1219. service_tp, &service_blinded_pk);
  1220. /* A service builds two lists of responsible HSDir, for the current and the
  1221. * next descriptor. Depending on the scenario, the client timing indicate if
  1222. * it is fetching the current or the next descriptor so we use the
  1223. * "client_fetch_next_desc" to know which one the client is trying to get to
  1224. * confirm that the service computes the same hashring for the same blinded
  1225. * key and service time period function. */
  1226. hs_get_responsible_hsdirs(&service_blinded_pk, service_tp,
  1227. cfg->client_fetch_next_desc, 0,
  1228. service_responsible_hsdirs);
  1229. cleanup_nodelist();
  1230. tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
  1231. UNMOCK(networkstatus_get_latest_consensus);
  1232. UNMOCK(networkstatus_get_live_consensus);
  1233. /* Some testing of the values we just got from the client and service. */
  1234. tt_mem_op(&client_blinded_pk, OP_EQ, &service_blinded_pk,
  1235. ED25519_PUBKEY_LEN);
  1236. tt_int_op(are_responsible_hsdirs_equal(), OP_EQ, 1);
  1237. /* Everything went well. */
  1238. ret = 0;
  1239. done:
  1240. cleanup_reachability_test();
  1241. if (ret == -1) {
  1242. /* Do this so we can know which scenario failed. */
  1243. char msg[32];
  1244. tor_snprintf(msg, sizeof(msg), "Scenario %d failed", num_scenario);
  1245. tt_fail_msg(msg);
  1246. }
  1247. return ret;
  1248. }
  1249. static void
  1250. test_reachability(void *arg)
  1251. {
  1252. (void) arg;
  1253. /* NOTE: An important axiom to understand here is that SRV#N must only be
  1254. * used with TP#N value. For example, SRV#2 with TP#1 should NEVER be used
  1255. * together. The HSDir index computation is based on this axiom.*/
  1256. for (int i = 0; reachability_scenarios[i].service_valid_after; ++i) {
  1257. int ret = run_reachability_scenario(&reachability_scenarios[i], i + 1);
  1258. if (ret < 0) {
  1259. return;
  1260. }
  1261. }
  1262. }
  1263. /** Pick an HSDir for service with <b>onion_identity_pk</b> as a client. Put
  1264. * its identity digest in <b>hsdir_digest_out</b>. */
  1265. static void
  1266. helper_client_pick_hsdir(const ed25519_public_key_t *onion_identity_pk,
  1267. char *hsdir_digest_out)
  1268. {
  1269. tt_assert(onion_identity_pk);
  1270. routerstatus_t *client_hsdir = pick_hsdir_v3(onion_identity_pk);
  1271. tt_assert(client_hsdir);
  1272. digest_to_base64(hsdir_digest_out, client_hsdir->identity_digest);
  1273. done:
  1274. ;
  1275. }
  1276. static void
  1277. test_hs_indexes(void *arg)
  1278. {
  1279. int ret;
  1280. uint64_t period_num = 42;
  1281. ed25519_public_key_t pubkey;
  1282. (void) arg;
  1283. /* Build the hs_index */
  1284. {
  1285. uint8_t hs_index[DIGEST256_LEN];
  1286. const char *b32_test_vector =
  1287. "37e5cbbd56a22823714f18f1623ece5983a0d64c78495a8cfab854245e5f9a8a";
  1288. char test_vector[DIGEST256_LEN];
  1289. ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
  1290. strlen(b32_test_vector));
  1291. tt_int_op(ret, OP_EQ, sizeof(test_vector));
  1292. /* Our test vector uses a public key set to 32 bytes of \x42. */
  1293. memset(&pubkey, '\x42', sizeof(pubkey));
  1294. hs_build_hs_index(1, &pubkey, period_num, hs_index);
  1295. tt_mem_op(hs_index, OP_EQ, test_vector, sizeof(hs_index));
  1296. }
  1297. /* Build the hsdir_index */
  1298. {
  1299. uint8_t srv[DIGEST256_LEN];
  1300. uint8_t hsdir_index[DIGEST256_LEN];
  1301. const char *b32_test_vector =
  1302. "db475361014a09965e7e5e4d4a25b8f8d4b8f16cb1d8a7e95eed50249cc1a2d5";
  1303. char test_vector[DIGEST256_LEN];
  1304. ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
  1305. strlen(b32_test_vector));
  1306. tt_int_op(ret, OP_EQ, sizeof(test_vector));
  1307. /* Our test vector uses a public key set to 32 bytes of \x42. */
  1308. memset(&pubkey, '\x42', sizeof(pubkey));
  1309. memset(srv, '\x43', sizeof(srv));
  1310. hs_build_hsdir_index(&pubkey, srv, period_num, hsdir_index);
  1311. tt_mem_op(hsdir_index, OP_EQ, test_vector, sizeof(hsdir_index));
  1312. }
  1313. done:
  1314. ;
  1315. }
  1316. #define EARLY_IN_SRV_TO_TP 0
  1317. #define LATE_IN_SRV_TO_TP 1
  1318. #define EARLY_IN_TP_TO_SRV 2
  1319. #define LATE_IN_TP_TO_SRV 3
  1320. /** Set the consensus and system time based on <b>position</b>. See the
  1321. * following diagram for details:
  1322. *
  1323. * +------------------------------------------------------------------+
  1324. * | |
  1325. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1326. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1327. * | |
  1328. * | $==========|-----------$===========|----------$===========| |
  1329. * | |
  1330. * | |
  1331. * +------------------------------------------------------------------+
  1332. */
  1333. static time_t
  1334. helper_set_consensus_and_system_time(networkstatus_t *ns, int position)
  1335. {
  1336. time_t real_time = 0;
  1337. /* The period between SRV#N and TP#N is from 00:00 to 12:00 UTC. Consensus
  1338. * valid_after is what matters here, the rest is just to specify the voting
  1339. * period correctly. */
  1340. if (position == LATE_IN_SRV_TO_TP) {
  1341. parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC", &ns->valid_after);
  1342. parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC", &ns->fresh_until);
  1343. parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->valid_until);
  1344. } else if (position == EARLY_IN_TP_TO_SRV) {
  1345. parse_rfc1123_time("Wed, 13 Apr 2016 13:00:00 UTC", &ns->valid_after);
  1346. parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->fresh_until);
  1347. parse_rfc1123_time("Wed, 13 Apr 2016 16:00:00 UTC", &ns->valid_until);
  1348. } else if (position == LATE_IN_TP_TO_SRV) {
  1349. parse_rfc1123_time("Wed, 13 Apr 2016 23:00:00 UTC", &ns->valid_after);
  1350. parse_rfc1123_time("Wed, 14 Apr 2016 00:00:00 UTC", &ns->fresh_until);
  1351. parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->valid_until);
  1352. } else if (position == EARLY_IN_SRV_TO_TP) {
  1353. parse_rfc1123_time("Wed, 14 Apr 2016 01:00:00 UTC", &ns->valid_after);
  1354. parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->fresh_until);
  1355. parse_rfc1123_time("Wed, 14 Apr 2016 04:00:00 UTC", &ns->valid_until);
  1356. } else {
  1357. tt_assert(0);
  1358. }
  1359. voting_schedule_recalculate_timing(get_options(), ns->valid_after);
  1360. /* Set system time: pretend to be just 2 minutes before consensus expiry */
  1361. real_time = ns->valid_until - 120;
  1362. update_approx_time(real_time);
  1363. done:
  1364. return real_time;
  1365. }
  1366. /** Helper function that carries out the actual test for
  1367. * test_client_service_sync() */
  1368. static void
  1369. helper_test_hsdir_sync(networkstatus_t *ns,
  1370. int service_position, int client_position,
  1371. int client_fetches_next_desc)
  1372. {
  1373. hs_service_descriptor_t *desc;
  1374. int retval;
  1375. /** Test logic:
  1376. * 1) Initialize service time: consensus and system time.
  1377. * 1.1) Initialize service hash ring
  1378. * 2) Initialize service and publish descriptors.
  1379. * 3) Initialize client time: consensus and system time.
  1380. * 3.1) Initialize client hash ring
  1381. * 4) Try to fetch descriptor as client, and CHECK that the HSDir picked by
  1382. * the client was also picked by service.
  1383. */
  1384. /* 1) Initialize service time: consensus and real time */
  1385. time_t now = helper_set_consensus_and_system_time(ns, service_position);
  1386. helper_initialize_big_hash_ring(ns);
  1387. /* 2) Initialize service */
  1388. hs_service_t *service = helper_init_service(now);
  1389. desc = client_fetches_next_desc ? service->desc_next : service->desc_current;
  1390. /* Now let's upload our desc to all hsdirs */
  1391. upload_descriptor_to_all(service, desc);
  1392. /* Cleanup right now so we don't memleak on error. */
  1393. cleanup_nodelist();
  1394. /* Check that previous hsdirs were populated */
  1395. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 8);
  1396. /* 3) Initialize client time */
  1397. helper_set_consensus_and_system_time(ns, client_position);
  1398. cleanup_nodelist();
  1399. SMARTLIST_FOREACH(ns->routerstatus_list,
  1400. routerstatus_t *, rs, routerstatus_free(rs));
  1401. smartlist_clear(ns->routerstatus_list);
  1402. helper_initialize_big_hash_ring(ns);
  1403. /* 4) Pick 6 HSDirs as a client and check that they were also chosen by the
  1404. service. */
  1405. for (int y = 0 ; y < 6 ; y++) {
  1406. char client_hsdir_b64_digest[BASE64_DIGEST_LEN+1] = {0};
  1407. helper_client_pick_hsdir(&service->keys.identity_pk,
  1408. client_hsdir_b64_digest);
  1409. /* CHECK: Go through the hsdirs chosen by the service and make sure that it
  1410. * contains the one picked by the client! */
  1411. retval = smartlist_contains_string(desc->previous_hsdirs,
  1412. client_hsdir_b64_digest);
  1413. tt_int_op(retval, OP_EQ, 1);
  1414. }
  1415. /* Finally, try to pick a 7th hsdir and see that NULL is returned since we
  1416. * exhausted all of them: */
  1417. tt_assert(!pick_hsdir_v3(&service->keys.identity_pk));
  1418. done:
  1419. /* At the end: free all services and initialize the subsystem again, we will
  1420. * need it for next scenario. */
  1421. cleanup_nodelist();
  1422. hs_service_free_all();
  1423. hs_service_init();
  1424. SMARTLIST_FOREACH(ns->routerstatus_list,
  1425. routerstatus_t *, rs, routerstatus_free(rs));
  1426. smartlist_clear(ns->routerstatus_list);
  1427. }
  1428. /** This test ensures that client and service will pick the same HSDirs, under
  1429. * various timing scenarios:
  1430. * a) Scenario where both client and service are in the time segment between
  1431. * SRV#N and TP#N:
  1432. * b) Scenario where both client and service are in the time segment between
  1433. * TP#N and SRV#N+1.
  1434. * c) Scenario where service is between SRV#N and TP#N, but client is between
  1435. * TP#N and SRV#N+1.
  1436. * d) Scenario where service is between TP#N and SRV#N+1, but client is
  1437. * between SRV#N and TP#N.
  1438. *
  1439. * This test is important because it tests that upload_descriptor_to_all() is
  1440. * in synch with pick_hsdir_v3(). That's not the case for the
  1441. * test_reachability() test which only compares the responsible hsdir sets.
  1442. */
  1443. static void
  1444. test_client_service_hsdir_set_sync(void *arg)
  1445. {
  1446. networkstatus_t *ns = NULL;
  1447. (void) arg;
  1448. MOCK(networkstatus_get_latest_consensus,
  1449. mock_networkstatus_get_latest_consensus);
  1450. MOCK(networkstatus_get_live_consensus,
  1451. mock_networkstatus_get_live_consensus);
  1452. MOCK(get_or_state,
  1453. get_or_state_replacement);
  1454. MOCK(hs_desc_encode_descriptor,
  1455. mock_hs_desc_encode_descriptor);
  1456. MOCK(directory_initiate_request,
  1457. mock_directory_initiate_request);
  1458. hs_init();
  1459. /* Initialize a big hash ring: we want it to be big so that client and
  1460. * service cannot accidentally select the same HSDirs */
  1461. ns = networkstatus_get_latest_consensus();
  1462. tt_assert(ns);
  1463. /** Now test the various synch scenarios. See the helper function for more
  1464. details: */
  1465. /* a) Scenario where both client and service are in the time segment between
  1466. * SRV#N and TP#N. At this time the client fetches the first HS desc:
  1467. *
  1468. * +------------------------------------------------------------------+
  1469. * | |
  1470. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1471. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1472. * | |
  1473. * | $==========|-----------$===========|----------$===========| |
  1474. * | ^ ^ |
  1475. * | S C |
  1476. * +------------------------------------------------------------------+
  1477. */
  1478. helper_test_hsdir_sync(ns, LATE_IN_SRV_TO_TP, LATE_IN_SRV_TO_TP, 0);
  1479. /* b) Scenario where both client and service are in the time segment between
  1480. * TP#N and SRV#N+1. At this time the client fetches the second HS
  1481. * desc:
  1482. *
  1483. * +------------------------------------------------------------------+
  1484. * | |
  1485. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1486. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1487. * | |
  1488. * | $==========|-----------$===========|-----------$===========| |
  1489. * | ^ ^ |
  1490. * | S C |
  1491. * +------------------------------------------------------------------+
  1492. */
  1493. helper_test_hsdir_sync(ns, LATE_IN_TP_TO_SRV, LATE_IN_TP_TO_SRV, 1);
  1494. /* c) Scenario where service is between SRV#N and TP#N, but client is
  1495. * between TP#N and SRV#N+1. Client is forward in time so it fetches the
  1496. * second HS desc.
  1497. *
  1498. * +------------------------------------------------------------------+
  1499. * | |
  1500. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1501. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1502. * | |
  1503. * | $==========|-----------$===========|-----------$===========| |
  1504. * | ^ ^ |
  1505. * | S C |
  1506. * +------------------------------------------------------------------+
  1507. */
  1508. helper_test_hsdir_sync(ns, LATE_IN_SRV_TO_TP, EARLY_IN_TP_TO_SRV, 1);
  1509. /* d) Scenario where service is between TP#N and SRV#N+1, but client is
  1510. * between SRV#N and TP#N. Client is backwards in time so it fetches the
  1511. * first HS desc.
  1512. *
  1513. * +------------------------------------------------------------------+
  1514. * | |
  1515. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1516. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1517. * | |
  1518. * | $==========|-----------$===========|-----------$===========| |
  1519. * | ^ ^ |
  1520. * | C S |
  1521. * +------------------------------------------------------------------+
  1522. */
  1523. helper_test_hsdir_sync(ns, EARLY_IN_TP_TO_SRV, LATE_IN_SRV_TO_TP, 0);
  1524. /* e) Scenario where service is between SRV#N and TP#N, but client is
  1525. * between TP#N-1 and SRV#3. Client is backwards in time so it fetches
  1526. * the first HS desc.
  1527. *
  1528. * +------------------------------------------------------------------+
  1529. * | |
  1530. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1531. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1532. * | |
  1533. * | $==========|-----------$===========|-----------$===========| |
  1534. * | ^ ^ |
  1535. * | C S |
  1536. * +------------------------------------------------------------------+
  1537. */
  1538. helper_test_hsdir_sync(ns, EARLY_IN_SRV_TO_TP, LATE_IN_TP_TO_SRV, 0);
  1539. /* f) Scenario where service is between TP#N and SRV#N+1, but client is
  1540. * between SRV#N+1 and TP#N+1. Client is forward in time so it fetches
  1541. * the second HS desc.
  1542. *
  1543. * +------------------------------------------------------------------+
  1544. * | |
  1545. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1546. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1547. * | |
  1548. * | $==========|-----------$===========|-----------$===========| |
  1549. * | ^ ^ |
  1550. * | S C |
  1551. * +------------------------------------------------------------------+
  1552. */
  1553. helper_test_hsdir_sync(ns, LATE_IN_TP_TO_SRV, EARLY_IN_SRV_TO_TP, 1);
  1554. done:
  1555. networkstatus_vote_free(ns);
  1556. nodelist_free_all();
  1557. hs_free_all();
  1558. }
  1559. struct testcase_t hs_common_tests[] = {
  1560. { "build_address", test_build_address, TT_FORK,
  1561. NULL, NULL },
  1562. { "validate_address", test_validate_address, TT_FORK,
  1563. NULL, NULL },
  1564. { "time_period", test_time_period, TT_FORK,
  1565. NULL, NULL },
  1566. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  1567. TT_FORK, NULL, NULL },
  1568. { "responsible_hsdirs", test_responsible_hsdirs, TT_FORK,
  1569. NULL, NULL },
  1570. { "desc_reupload_logic", test_desc_reupload_logic, TT_FORK,
  1571. NULL, NULL },
  1572. { "disaster_srv", test_disaster_srv, TT_FORK,
  1573. NULL, NULL },
  1574. { "hid_serv_request_tracker", test_hid_serv_request_tracker, TT_FORK,
  1575. NULL, NULL },
  1576. { "parse_extended_hostname", test_parse_extended_hostname, TT_FORK,
  1577. NULL, NULL },
  1578. { "time_between_tp_and_srv", test_time_between_tp_and_srv, TT_FORK,
  1579. NULL, NULL },
  1580. { "reachability", test_reachability, TT_FORK,
  1581. NULL, NULL },
  1582. { "client_service_hsdir_set_sync", test_client_service_hsdir_set_sync,
  1583. TT_FORK, NULL, NULL },
  1584. { "hs_indexes", test_hs_indexes, TT_FORK,
  1585. NULL, NULL },
  1586. END_OF_TESTCASES
  1587. };