test_hs_common.c 67 KB

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