test_hs_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "test.h"
  9. #include "test_helpers.h"
  10. #include "log_test_helpers.h"
  11. #include "hs_test_helpers.h"
  12. #include "hs_common.h"
  13. #include "config.h"
  14. /** Test the validation of HS v3 addresses */
  15. static void
  16. test_validate_address(void *arg)
  17. {
  18. int ret;
  19. (void) arg;
  20. /* Address too short and too long. */
  21. setup_full_capture_of_logs(LOG_WARN);
  22. ret = hs_address_is_valid("blah");
  23. tt_int_op(ret, OP_EQ, 0);
  24. expect_log_msg_containing("has an invalid length");
  25. teardown_capture_of_logs();
  26. setup_full_capture_of_logs(LOG_WARN);
  27. ret = hs_address_is_valid(
  28. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  29. tt_int_op(ret, OP_EQ, 0);
  30. expect_log_msg_containing("has an invalid length");
  31. teardown_capture_of_logs();
  32. /* Invalid checksum (taken from prop224) */
  33. setup_full_capture_of_logs(LOG_WARN);
  34. ret = hs_address_is_valid(
  35. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  36. tt_int_op(ret, OP_EQ, 0);
  37. expect_log_msg_containing("invalid checksum");
  38. teardown_capture_of_logs();
  39. setup_full_capture_of_logs(LOG_WARN);
  40. ret = hs_address_is_valid(
  41. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  42. tt_int_op(ret, OP_EQ, 0);
  43. expect_log_msg_containing("invalid checksum");
  44. teardown_capture_of_logs();
  45. /* Non base32 decodable string. */
  46. setup_full_capture_of_logs(LOG_WARN);
  47. ret = hs_address_is_valid(
  48. "????????????????????????????????????????????????????????");
  49. tt_int_op(ret, OP_EQ, 0);
  50. expect_log_msg_containing("can't be decoded");
  51. teardown_capture_of_logs();
  52. /* Valid address. */
  53. ret = hs_address_is_valid(
  54. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  55. tt_int_op(ret, OP_EQ, 1);
  56. done:
  57. ;
  58. }
  59. /** Test building HS v3 onion addresses */
  60. static void
  61. test_build_address(void *arg)
  62. {
  63. int ret;
  64. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  65. ed25519_public_key_t pubkey;
  66. (void) arg;
  67. /* The following has been created with hs_build_address.py script that
  68. * follows proposal 224 specification to build an onion address. */
  69. static const char *test_addr =
  70. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid";
  71. /* Let's try to build the same onion address that the script can do. Key is
  72. * a long set of very random \x42 :). */
  73. memset(&pubkey, '\x42', sizeof(pubkey));
  74. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  75. tt_str_op(test_addr, OP_EQ, onion_addr);
  76. /* Validate that address. */
  77. ret = hs_address_is_valid(onion_addr);
  78. tt_int_op(ret, OP_EQ, 1);
  79. done:
  80. ;
  81. }
  82. /** Test that our HS time period calculation functions work properly */
  83. static void
  84. test_time_period(void *arg)
  85. {
  86. (void) arg;
  87. uint64_t tn;
  88. int retval;
  89. time_t fake_time;
  90. /* Let's do the example in prop224 section [TIME-PERIODS] */
  91. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  92. &fake_time);
  93. tt_int_op(retval, ==, 0);
  94. /* Check that the time period number is right */
  95. tn = hs_get_time_period_num(fake_time);
  96. tt_u64_op(tn, ==, 16903);
  97. /* Increase current time to 11:59:59 UTC and check that the time period
  98. number is still the same */
  99. fake_time += 3599;
  100. tn = hs_get_time_period_num(fake_time);
  101. tt_u64_op(tn, ==, 16903);
  102. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  103. fake_time += 1;
  104. tn = hs_get_time_period_num(fake_time);
  105. tt_u64_op(tn, ==, 16904);
  106. /* Now also check our hs_get_next_time_period_num() function */
  107. tn = hs_get_next_time_period_num(fake_time);
  108. tt_u64_op(tn, ==, 16905);
  109. done:
  110. ;
  111. }
  112. /** Test that we can correctly find the start time of the next time period */
  113. static void
  114. test_start_time_of_next_time_period(void *arg)
  115. {
  116. (void) arg;
  117. int retval;
  118. time_t fake_time;
  119. char tbuf[ISO_TIME_LEN + 1];
  120. time_t next_tp_start_time;
  121. /* Do some basic tests */
  122. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  123. &fake_time);
  124. tt_int_op(retval, ==, 0);
  125. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  126. /* Compare it with the correct result */
  127. format_iso_time(tbuf, next_tp_start_time);
  128. tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
  129. /* Another test with an edge-case time (start of TP) */
  130. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  131. &fake_time);
  132. tt_int_op(retval, ==, 0);
  133. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  134. format_iso_time(tbuf, next_tp_start_time);
  135. tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
  136. {
  137. /* Now pretend we are on a testing network and alter the voting schedule to
  138. be every 10 seconds. This means that a time period has length 10*24
  139. seconds (4 minutes). It also means that we apply a rotational offset of
  140. 120 seconds to the time period, so that it starts at 00:02:00 instead of
  141. 00:00:00. */
  142. or_options_t *options = get_options_mutable();
  143. options->TestingTorNetwork = 1;
  144. options->V3AuthVotingInterval = 10;
  145. options->TestingV3AuthInitialVotingInterval = 10;
  146. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  147. &fake_time);
  148. tt_int_op(retval, ==, 0);
  149. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  150. /* Compare it with the correct result */
  151. format_iso_time(tbuf, next_tp_start_time);
  152. tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);
  153. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  154. &fake_time);
  155. tt_int_op(retval, ==, 0);
  156. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  157. /* Compare it with the correct result */
  158. format_iso_time(tbuf, next_tp_start_time);
  159. tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  160. }
  161. done:
  162. ;
  163. }
  164. /** Test that our HS overlap period functions work properly. */
  165. static void
  166. test_desc_overlap_period(void *arg)
  167. {
  168. (void) arg;
  169. int retval;
  170. time_t now = time(NULL);
  171. networkstatus_t *dummy_consensus = NULL;
  172. /* First try with a consensus just inside the overlap period */
  173. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  174. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  175. &dummy_consensus->valid_after);
  176. tt_int_op(retval, ==, 0);
  177. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  178. tt_int_op(retval, ==, 1);
  179. /* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
  180. period is still active. */
  181. dummy_consensus->valid_after += 3600*11;
  182. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  183. tt_int_op(retval, ==, 1);
  184. /* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
  185. period is still active. */
  186. dummy_consensus->valid_after += 3599;
  187. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  188. tt_int_op(retval, ==, 1);
  189. /* Now increase the valid_after so that it drifts to noon, and check that
  190. overlap mode is not active anymore. */
  191. dummy_consensus->valid_after += 1;
  192. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  193. tt_int_op(retval, ==, 0);
  194. /* Check that overlap mode is also inactive at 23:59:59 UTC */
  195. retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 UTC",
  196. &dummy_consensus->valid_after);
  197. tt_int_op(retval, ==, 0);
  198. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  199. tt_int_op(retval, ==, 0);
  200. done:
  201. tor_free(dummy_consensus);
  202. }
  203. /* Test the overlap period functions on a testnet with altered voting
  204. * schedule */
  205. static void
  206. test_desc_overlap_period_testnet(void *arg)
  207. {
  208. int retval;
  209. time_t now = approx_time();
  210. networkstatus_t *dummy_consensus = NULL;
  211. or_options_t *options = get_options_mutable();
  212. (void) arg;
  213. /* Set the testnet option and a 10-second voting interval */
  214. options->TestingTorNetwork = 1;
  215. options->V3AuthVotingInterval = 10;
  216. options->TestingV3AuthInitialVotingInterval = 10;
  217. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  218. /* A 10-second voting interval means that the lengths of an SRV run and of a
  219. * time period are both 10*24 seconds (4 minutes). The SRV gets published at
  220. * 00:00:00 and the TP starts at 00:02:00 (rotation offset: 2 mins). Those
  221. * two minutes between SRV publish and TP start is the overlap period
  222. * window. Let's test it: */
  223. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  224. &dummy_consensus->valid_after);
  225. tt_int_op(retval, ==, 0);
  226. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  227. tt_int_op(retval, ==, 1);
  228. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:01:59 UTC",
  229. &dummy_consensus->valid_after);
  230. tt_int_op(retval, ==, 0);
  231. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  232. tt_int_op(retval, ==, 1);
  233. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  234. &dummy_consensus->valid_after);
  235. tt_int_op(retval, ==, 0);
  236. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  237. tt_int_op(retval, ==, 0);
  238. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:04:00 UTC",
  239. &dummy_consensus->valid_after);
  240. tt_int_op(retval, ==, 0);
  241. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  242. tt_int_op(retval, ==, 1);
  243. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:05:59 UTC",
  244. &dummy_consensus->valid_after);
  245. tt_int_op(retval, ==, 0);
  246. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  247. tt_int_op(retval, ==, 1);
  248. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:06:00 UTC",
  249. &dummy_consensus->valid_after);
  250. tt_int_op(retval, ==, 0);
  251. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  252. tt_int_op(retval, ==, 0);
  253. done:
  254. tor_free(dummy_consensus);
  255. }
  256. struct testcase_t hs_common_tests[] = {
  257. { "build_address", test_build_address, TT_FORK,
  258. NULL, NULL },
  259. { "validate_address", test_validate_address, TT_FORK,
  260. NULL, NULL },
  261. { "time_period", test_time_period, TT_FORK,
  262. NULL, NULL },
  263. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  264. TT_FORK, NULL, NULL },
  265. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  266. NULL, NULL },
  267. { "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK,
  268. NULL, NULL },
  269. END_OF_TESTCASES
  270. };