test_hs_common.c 65 KB

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