test_hs_common.c 60 KB

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