test_hs_common.c 67 KB

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