test_hs_config.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_config.c
  5. * \brief Test hidden service configuration functionality.
  6. */
  7. #define CONFIG_PRIVATE
  8. #include "test.h"
  9. #include "test_helpers.h"
  10. #include "log_test_helpers.h"
  11. #include "hs_config.h"
  12. #include "config.h"
  13. static int
  14. helper_config_service_v2(const char *conf, int validate_only)
  15. {
  16. int ret = 0;
  17. or_options_t *options = NULL;
  18. tt_assert(conf);
  19. options = helper_parse_options(conf);
  20. tt_assert(options);
  21. ret = hs_config_service_all(options, validate_only);
  22. done:
  23. or_options_free(options);
  24. return ret;
  25. }
  26. static void
  27. test_invalid_service_v2(void *arg)
  28. {
  29. int validate_only = 1, ret;
  30. (void) arg;
  31. /* Try with a missing port configuration. */
  32. {
  33. const char *conf =
  34. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  35. "HiddenServiceVersion 2\n";
  36. setup_full_capture_of_logs(LOG_WARN);
  37. ret = helper_config_service_v2(conf, validate_only);
  38. tt_int_op(ret, OP_EQ, -1);
  39. expect_log_msg_containing("with no ports configured.");
  40. teardown_capture_of_logs();
  41. }
  42. /* Out of order directives. */
  43. {
  44. const char *conf =
  45. "HiddenServiceVersion 2\n"
  46. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  47. "HiddenServicePort 80\n";
  48. setup_full_capture_of_logs(LOG_WARN);
  49. ret = helper_config_service_v2(conf, validate_only);
  50. tt_int_op(ret, OP_EQ, -1);
  51. expect_log_msg_containing("HiddenServiceVersion with no preceding "
  52. "HiddenServiceDir directive");
  53. teardown_capture_of_logs();
  54. }
  55. /* Bad port. */
  56. {
  57. const char *conf =
  58. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  59. "HiddenServiceVersion 2\n"
  60. "HiddenServicePort 65536\n";
  61. setup_full_capture_of_logs(LOG_WARN);
  62. ret = helper_config_service_v2(conf, validate_only);
  63. tt_int_op(ret, OP_EQ, -1);
  64. expect_log_msg_containing("Missing or invalid port");
  65. teardown_capture_of_logs();
  66. }
  67. /* Too many introduction points. */
  68. {
  69. const char *conf =
  70. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  71. "HiddenServiceVersion 2\n"
  72. "HiddenServicePort 80\n"
  73. "HiddenServiceNumIntroductionPoints 11\n"; /* One too many. */
  74. setup_full_capture_of_logs(LOG_WARN);
  75. ret = helper_config_service_v2(conf, validate_only);
  76. tt_int_op(ret, OP_EQ, -1);
  77. expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
  78. "be between 0 and 10, not 11");
  79. teardown_capture_of_logs();
  80. }
  81. /* Too much max streams. */
  82. {
  83. const char *conf =
  84. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  85. "HiddenServiceVersion 2\n"
  86. "HiddenServicePort 80\n"
  87. "HiddenServiceMaxStreams 65536\n"; /* One too many. */
  88. setup_full_capture_of_logs(LOG_WARN);
  89. ret = helper_config_service_v2(conf, validate_only);
  90. tt_int_op(ret, OP_EQ, -1);
  91. expect_log_msg_containing("HiddenServiceMaxStreams should be between "
  92. "0 and 65535, not 65536");
  93. teardown_capture_of_logs();
  94. }
  95. /* Bad authorized client type. */
  96. {
  97. const char *conf =
  98. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  99. "HiddenServiceVersion 2\n"
  100. "HiddenServicePort 80\n"
  101. "HiddenServiceAuthorizeClient blah alice,bob\n"; /* blah is no good. */
  102. setup_full_capture_of_logs(LOG_WARN);
  103. ret = helper_config_service_v2(conf, validate_only);
  104. tt_int_op(ret, OP_EQ, -1);
  105. expect_log_msg_containing("HiddenServiceAuthorizeClient contains "
  106. "unrecognized auth-type");
  107. teardown_capture_of_logs();
  108. }
  109. /* Duplicate directory directive. */
  110. {
  111. const char *conf =
  112. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  113. "HiddenServiceVersion 2\n"
  114. "HiddenServicePort 80\n"
  115. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  116. "HiddenServiceVersion 2\n"
  117. "HiddenServicePort 81\n";
  118. setup_full_capture_of_logs(LOG_WARN);
  119. ret = helper_config_service_v2(conf, validate_only);
  120. tt_int_op(ret, OP_EQ, -1);
  121. expect_log_msg_containing("Another hidden service is already "
  122. "configured for directory");
  123. teardown_capture_of_logs();
  124. }
  125. done:
  126. ;
  127. }
  128. static void
  129. test_valid_service_v2(void *arg)
  130. {
  131. int ret;
  132. (void) arg;
  133. /* Valid complex configuration. Basic client authorization. */
  134. {
  135. const char *conf =
  136. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  137. "HiddenServiceVersion 2\n"
  138. "HiddenServicePort 80\n"
  139. "HiddenServicePort 22 localhost:22\n"
  140. "HiddenServicePort 42 unix:/path/to/socket\n"
  141. "HiddenServiceAuthorizeClient basic alice,bob,eve\n"
  142. "HiddenServiceAllowUnknownPorts 1\n"
  143. "HiddenServiceMaxStreams 42\n"
  144. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  145. "HiddenServiceDirGroupReadable 1\n"
  146. "HiddenServiceNumIntroductionPoints 7\n";
  147. ret = helper_config_service_v2(conf, 1);
  148. tt_int_op(ret, OP_EQ, 0);
  149. }
  150. /* Valid complex configuration. Stealth client authorization. */
  151. {
  152. const char *conf =
  153. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  154. "HiddenServiceVersion 2\n"
  155. "HiddenServicePort 65535\n"
  156. "HiddenServicePort 22 1.1.1.1:22\n"
  157. "HiddenServicePort 9000 unix:/path/to/socket\n"
  158. "HiddenServiceAuthorizeClient stealth charlie,romeo\n"
  159. "HiddenServiceAllowUnknownPorts 0\n"
  160. "HiddenServiceMaxStreams 42\n"
  161. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  162. "HiddenServiceDirGroupReadable 1\n"
  163. "HiddenServiceNumIntroductionPoints 8\n";
  164. ret = helper_config_service_v2(conf, 1);
  165. tt_int_op(ret, OP_EQ, 0);
  166. }
  167. done:
  168. ;
  169. }
  170. struct testcase_t hs_config_tests[] = {
  171. { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
  172. NULL, NULL },
  173. { "valid_service_v2", test_valid_service_v2, TT_FORK,
  174. NULL, NULL },
  175. END_OF_TESTCASES
  176. };