test_hs_common.c 7.8 KB

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