test_hs_common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. static void
  14. test_validate_address(void *arg)
  15. {
  16. int ret;
  17. (void) arg;
  18. /* Address too short and too long. */
  19. setup_full_capture_of_logs(LOG_WARN);
  20. ret = hs_address_is_valid("blah");
  21. tt_int_op(ret, OP_EQ, 0);
  22. expect_log_msg_containing("has an invalid length");
  23. teardown_capture_of_logs();
  24. setup_full_capture_of_logs(LOG_WARN);
  25. ret = hs_address_is_valid(
  26. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  27. tt_int_op(ret, OP_EQ, 0);
  28. expect_log_msg_containing("has an invalid length");
  29. teardown_capture_of_logs();
  30. /* Invalid checksum (taken from prop224) */
  31. setup_full_capture_of_logs(LOG_WARN);
  32. ret = hs_address_is_valid(
  33. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  34. tt_int_op(ret, OP_EQ, 0);
  35. expect_log_msg_containing("invalid checksum");
  36. teardown_capture_of_logs();
  37. setup_full_capture_of_logs(LOG_WARN);
  38. ret = hs_address_is_valid(
  39. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  40. tt_int_op(ret, OP_EQ, 0);
  41. expect_log_msg_containing("invalid checksum");
  42. teardown_capture_of_logs();
  43. /* Non base32 decodable string. */
  44. setup_full_capture_of_logs(LOG_WARN);
  45. ret = hs_address_is_valid(
  46. "????????????????????????????????????????????????????????");
  47. tt_int_op(ret, OP_EQ, 0);
  48. expect_log_msg_containing("can't be decoded");
  49. teardown_capture_of_logs();
  50. /* Valid address. */
  51. ret = hs_address_is_valid(
  52. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  53. tt_int_op(ret, OP_EQ, 1);
  54. done:
  55. ;
  56. }
  57. static void
  58. test_build_address(void *arg)
  59. {
  60. int ret;
  61. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  62. ed25519_public_key_t pubkey;
  63. (void) arg;
  64. /* The following has been created with hs_build_address.py script that
  65. * follows proposal 224 specification to build an onion address. */
  66. static const char *test_addr =
  67. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid";
  68. /* Let's try to build the same onion address that the script can do. Key is
  69. * a long set of very random \x42 :). */
  70. memset(&pubkey, '\x42', sizeof(pubkey));
  71. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  72. tt_str_op(test_addr, OP_EQ, onion_addr);
  73. /* Validate that address. */
  74. ret = hs_address_is_valid(onion_addr);
  75. tt_int_op(ret, OP_EQ, 1);
  76. done:
  77. ;
  78. }
  79. /** Test that our HS time period calculation functions work properly */
  80. static void
  81. test_time_period(void *arg)
  82. {
  83. (void) arg;
  84. uint64_t tn;
  85. int retval;
  86. time_t fake_time;
  87. /* Let's do the example in prop224 section [TIME-PERIODS] */
  88. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  89. &fake_time);
  90. tt_int_op(retval, ==, 0);
  91. /* Check that the time period number is right */
  92. tn = hs_get_time_period_num(fake_time);
  93. tt_u64_op(tn, ==, 16903);
  94. /* Increase current time to 11:59:59 UTC and check that the time period
  95. number is still the same */
  96. fake_time += 3599;
  97. tn = hs_get_time_period_num(fake_time);
  98. tt_u64_op(tn, ==, 16903);
  99. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  100. fake_time += 1;
  101. tn = hs_get_time_period_num(fake_time);
  102. tt_u64_op(tn, ==, 16904);
  103. /* Now also check our hs_get_next_time_period_num() function */
  104. tn = hs_get_next_time_period_num(fake_time);
  105. tt_u64_op(tn, ==, 16905);
  106. done:
  107. ;
  108. }
  109. /** Test that our HS overlap period functions work properly. */
  110. static void
  111. test_desc_overlap_period(void *arg)
  112. {
  113. (void) arg;
  114. int retval;
  115. time_t now = time(NULL);
  116. networkstatus_t *dummy_consensus = NULL;
  117. /* First try with a consensus inside the overlap period */
  118. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  119. retval = parse_rfc1123_time("Wed, 13 Apr 2016 10:00:00 UTC",
  120. &dummy_consensus->valid_after);
  121. tt_int_op(retval, ==, 0);
  122. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  123. tt_int_op(retval, ==, 1);
  124. /* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
  125. period is still active. */
  126. dummy_consensus->valid_after += 3600;
  127. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  128. tt_int_op(retval, ==, 1);
  129. /* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
  130. period is still active. */
  131. dummy_consensus->valid_after += 3599;
  132. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  133. tt_int_op(retval, ==, 1);
  134. /* Now increase the valid_after so that it drifts to noon, and check that
  135. overlap mode is not active anymore. */
  136. dummy_consensus->valid_after += 1;
  137. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  138. tt_int_op(retval, ==, 0);
  139. /* Check that overlap mode is also inactive at 23:59:59 UTC */
  140. retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 UTC",
  141. &dummy_consensus->valid_after);
  142. tt_int_op(retval, ==, 0);
  143. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  144. tt_int_op(retval, ==, 0);
  145. done:
  146. tor_free(dummy_consensus);
  147. }
  148. struct testcase_t hs_common_tests[] = {
  149. { "build_address", test_build_address, TT_FORK,
  150. NULL, NULL },
  151. { "validate_address", test_validate_address, TT_FORK,
  152. NULL, NULL },
  153. { "time_period", test_time_period, TT_FORK,
  154. NULL, NULL },
  155. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  156. NULL, NULL },
  157. END_OF_TESTCASES
  158. };