test_hs_config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* Copyright (c) 2016-2018, 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. #define HS_SERVICE_PRIVATE
  9. #include "test/test.h"
  10. #include "test/test_helpers.h"
  11. #include "test/log_test_helpers.h"
  12. #include "app/config/config.h"
  13. #include "feature/hs/hs_common.h"
  14. #include "feature/hs/hs_config.h"
  15. #include "feature/hs/hs_service.h"
  16. #include "feature/rend/rendservice.h"
  17. static int
  18. helper_config_service(const char *conf, int validate_only)
  19. {
  20. int ret = 0;
  21. or_options_t *options = NULL;
  22. tt_assert(conf);
  23. options = helper_parse_options(conf);
  24. tt_assert(options);
  25. ret = hs_config_service_all(options, validate_only);
  26. done:
  27. or_options_free(options);
  28. return ret;
  29. }
  30. static void
  31. test_invalid_service(void *arg)
  32. {
  33. int ret;
  34. (void) arg;
  35. /* Try with a missing port configuration. */
  36. {
  37. const char *conf =
  38. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  39. "HiddenServiceVersion 1\n"; /* Wrong not supported version. */
  40. setup_full_capture_of_logs(LOG_WARN);
  41. ret = helper_config_service(conf, 1);
  42. tt_int_op(ret, OP_EQ, -1);
  43. expect_log_msg_containing("HiddenServiceVersion must be between 2 and 3");
  44. teardown_capture_of_logs();
  45. }
  46. /* Bad value of HiddenServiceAllowUnknownPorts. */
  47. {
  48. const char *conf =
  49. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  50. "HiddenServiceVersion 2\n"
  51. "HiddenServiceAllowUnknownPorts 2\n"; /* Should be 0 or 1. */
  52. setup_full_capture_of_logs(LOG_WARN);
  53. ret = helper_config_service(conf, 1);
  54. tt_int_op(ret, OP_EQ, -1);
  55. expect_log_msg_containing("HiddenServiceAllowUnknownPorts must be "
  56. "between 0 and 1, not 2");
  57. teardown_capture_of_logs();
  58. }
  59. /* Bad value of HiddenServiceDirGroupReadable */
  60. {
  61. const char *conf =
  62. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  63. "HiddenServiceVersion 2\n"
  64. "HiddenServiceDirGroupReadable 2\n"; /* Should be 0 or 1. */
  65. setup_full_capture_of_logs(LOG_WARN);
  66. ret = helper_config_service(conf, 1);
  67. tt_int_op(ret, OP_EQ, -1);
  68. expect_log_msg_containing("HiddenServiceDirGroupReadable must be "
  69. "between 0 and 1, not 2");
  70. teardown_capture_of_logs();
  71. }
  72. /* Bad value of HiddenServiceMaxStreamsCloseCircuit */
  73. {
  74. const char *conf =
  75. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  76. "HiddenServiceVersion 2\n"
  77. "HiddenServiceMaxStreamsCloseCircuit 2\n"; /* Should be 0 or 1. */
  78. setup_full_capture_of_logs(LOG_WARN);
  79. ret = helper_config_service(conf, 1);
  80. tt_int_op(ret, OP_EQ, -1);
  81. expect_log_msg_containing("HiddenServiceMaxStreamsCloseCircuit must "
  82. "be between 0 and 1, not 2");
  83. teardown_capture_of_logs();
  84. }
  85. /* Too much max streams. */
  86. {
  87. const char *conf =
  88. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  89. "HiddenServiceVersion 2\n"
  90. "HiddenServicePort 80\n"
  91. "HiddenServiceMaxStreams 65536\n"; /* One too many. */
  92. setup_full_capture_of_logs(LOG_WARN);
  93. ret = helper_config_service(conf, 1);
  94. tt_int_op(ret, OP_EQ, -1);
  95. expect_log_msg_containing("HiddenServiceMaxStreams must be between "
  96. "0 and 65535, not 65536");
  97. teardown_capture_of_logs();
  98. }
  99. /* Duplicate directory directive. */
  100. {
  101. const char *conf =
  102. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  103. "HiddenServiceVersion 2\n"
  104. "HiddenServicePort 80\n"
  105. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  106. "HiddenServiceVersion 2\n"
  107. "HiddenServicePort 81\n";
  108. setup_full_capture_of_logs(LOG_WARN);
  109. ret = helper_config_service(conf, 1);
  110. tt_int_op(ret, OP_EQ, -1);
  111. expect_log_msg_containing("Another hidden service is already "
  112. "configured for directory");
  113. teardown_capture_of_logs();
  114. }
  115. /* Bad port. */
  116. {
  117. const char *conf =
  118. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  119. "HiddenServiceVersion 2\n"
  120. "HiddenServicePort 65536\n";
  121. setup_full_capture_of_logs(LOG_WARN);
  122. ret = helper_config_service(conf, 1);
  123. tt_int_op(ret, OP_EQ, -1);
  124. expect_log_msg_containing("Missing or invalid port");
  125. teardown_capture_of_logs();
  126. }
  127. /* Out of order directives. */
  128. {
  129. const char *conf =
  130. "HiddenServiceVersion 2\n"
  131. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  132. "HiddenServicePort 80\n";
  133. setup_full_capture_of_logs(LOG_WARN);
  134. ret = helper_config_service(conf, 1);
  135. tt_int_op(ret, OP_EQ, -1);
  136. expect_log_msg_containing("HiddenServiceVersion with no preceding "
  137. "HiddenServiceDir directive");
  138. teardown_capture_of_logs();
  139. }
  140. done:
  141. ;
  142. }
  143. static void
  144. test_valid_service(void *arg)
  145. {
  146. int ret;
  147. (void) arg;
  148. /* Mix of v2 and v3. Still valid. */
  149. {
  150. const char *conf =
  151. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  152. "HiddenServiceVersion 2\n"
  153. "HiddenServicePort 80\n"
  154. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  155. "HiddenServiceVersion 3\n"
  156. "HiddenServicePort 81\n"
  157. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
  158. "HiddenServiceVersion 2\n"
  159. "HiddenServicePort 82\n";
  160. ret = helper_config_service(conf, 1);
  161. tt_int_op(ret, OP_EQ, 0);
  162. }
  163. done:
  164. ;
  165. }
  166. static void
  167. test_invalid_service_v2(void *arg)
  168. {
  169. int validate_only = 1, ret;
  170. (void) arg;
  171. /* Try with a missing port configuration. */
  172. {
  173. const char *conf =
  174. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  175. "HiddenServiceVersion 2\n";
  176. setup_full_capture_of_logs(LOG_WARN);
  177. ret = helper_config_service(conf, validate_only);
  178. tt_int_op(ret, OP_EQ, -1);
  179. expect_log_msg_containing("with no ports configured.");
  180. teardown_capture_of_logs();
  181. }
  182. /* Too many introduction points. */
  183. {
  184. const char *conf =
  185. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  186. "HiddenServiceVersion 2\n"
  187. "HiddenServicePort 80\n"
  188. "HiddenServiceNumIntroductionPoints 11\n"; /* One too many. */
  189. setup_full_capture_of_logs(LOG_WARN);
  190. ret = helper_config_service(conf, validate_only);
  191. tt_int_op(ret, OP_EQ, -1);
  192. expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
  193. "be between 0 and 10, not 11");
  194. teardown_capture_of_logs();
  195. }
  196. /* Too little introduction points. */
  197. {
  198. const char *conf =
  199. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  200. "HiddenServiceVersion 2\n"
  201. "HiddenServicePort 80\n"
  202. "HiddenServiceNumIntroductionPoints -1\n";
  203. setup_full_capture_of_logs(LOG_WARN);
  204. ret = helper_config_service(conf, validate_only);
  205. tt_int_op(ret, OP_EQ, -1);
  206. expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
  207. "be between 0 and 10, not -1");
  208. teardown_capture_of_logs();
  209. }
  210. /* Bad authorized client type. */
  211. {
  212. const char *conf =
  213. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  214. "HiddenServiceVersion 2\n"
  215. "HiddenServicePort 80\n"
  216. "HiddenServiceAuthorizeClient blah alice,bob\n"; /* blah is no good. */
  217. setup_full_capture_of_logs(LOG_WARN);
  218. ret = helper_config_service(conf, validate_only);
  219. tt_int_op(ret, OP_EQ, -1);
  220. expect_log_msg_containing("HiddenServiceAuthorizeClient contains "
  221. "unrecognized auth-type");
  222. teardown_capture_of_logs();
  223. }
  224. done:
  225. ;
  226. }
  227. static void
  228. test_valid_service_v2(void *arg)
  229. {
  230. int ret;
  231. (void) arg;
  232. /* Valid complex configuration. Basic client authorization. */
  233. {
  234. const char *conf =
  235. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  236. "HiddenServiceVersion 2\n"
  237. "HiddenServicePort 80\n"
  238. "HiddenServicePort 22 localhost:22\n"
  239. #ifdef HAVE_SYS_UN_H
  240. "HiddenServicePort 42 unix:/path/to/socket\n"
  241. #endif
  242. "HiddenServiceAuthorizeClient basic alice,bob,eve\n"
  243. "HiddenServiceAllowUnknownPorts 1\n"
  244. "HiddenServiceMaxStreams 42\n"
  245. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  246. "HiddenServiceDirGroupReadable 1\n"
  247. "HiddenServiceNumIntroductionPoints 7\n";
  248. ret = helper_config_service(conf, 1);
  249. tt_int_op(ret, OP_EQ, 0);
  250. }
  251. /* Valid complex configuration. Stealth client authorization. */
  252. {
  253. const char *conf =
  254. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  255. "HiddenServiceVersion 2\n"
  256. "HiddenServicePort 65535\n"
  257. "HiddenServicePort 22 1.1.1.1:22\n"
  258. #ifdef HAVE_SYS_UN_H
  259. "HiddenServicePort 9000 unix:/path/to/socket\n"
  260. #endif
  261. "HiddenServiceAuthorizeClient stealth charlie,romeo\n"
  262. "HiddenServiceAllowUnknownPorts 0\n"
  263. "HiddenServiceMaxStreams 42\n"
  264. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  265. "HiddenServiceDirGroupReadable 1\n"
  266. "HiddenServiceNumIntroductionPoints 8\n";
  267. ret = helper_config_service(conf, 1);
  268. tt_int_op(ret, OP_EQ, 0);
  269. }
  270. done:
  271. ;
  272. }
  273. static void
  274. test_invalid_service_v3(void *arg)
  275. {
  276. int validate_only = 1, ret;
  277. (void) arg;
  278. /* Try with a missing port configuration. */
  279. {
  280. const char *conf =
  281. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  282. "HiddenServiceVersion 3\n";
  283. setup_full_capture_of_logs(LOG_WARN);
  284. ret = helper_config_service(conf, validate_only);
  285. tt_int_op(ret, OP_EQ, -1);
  286. expect_log_msg_containing("with no ports configured.");
  287. teardown_capture_of_logs();
  288. }
  289. /* Too many introduction points. */
  290. {
  291. const char *conf =
  292. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  293. "HiddenServiceVersion 3\n"
  294. "HiddenServicePort 80\n"
  295. "HiddenServiceNumIntroductionPoints 21\n"; /* One too many. */
  296. setup_full_capture_of_logs(LOG_WARN);
  297. ret = helper_config_service(conf, validate_only);
  298. tt_int_op(ret, OP_EQ, -1);
  299. expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
  300. "be between 3 and 20, not 21.");
  301. teardown_capture_of_logs();
  302. }
  303. /* Too little introduction points. */
  304. {
  305. const char *conf =
  306. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  307. "HiddenServiceVersion 3\n"
  308. "HiddenServicePort 80\n"
  309. "HiddenServiceNumIntroductionPoints 1\n";
  310. setup_full_capture_of_logs(LOG_WARN);
  311. ret = helper_config_service(conf, validate_only);
  312. tt_int_op(ret, OP_EQ, -1);
  313. expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
  314. "be between 3 and 20, not 1.");
  315. teardown_capture_of_logs();
  316. }
  317. done:
  318. ;
  319. }
  320. static void
  321. test_valid_service_v3(void *arg)
  322. {
  323. int ret;
  324. (void) arg;
  325. /* Valid complex configuration. */
  326. {
  327. const char *conf =
  328. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  329. "HiddenServiceVersion 3\n"
  330. "HiddenServicePort 80\n"
  331. "HiddenServicePort 22 localhost:22\n"
  332. #ifdef HAVE_SYS_UN_H
  333. "HiddenServicePort 42 unix:/path/to/socket\n"
  334. #endif
  335. "HiddenServiceAllowUnknownPorts 1\n"
  336. "HiddenServiceMaxStreams 42\n"
  337. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  338. "HiddenServiceDirGroupReadable 1\n"
  339. "HiddenServiceNumIntroductionPoints 7\n";
  340. ret = helper_config_service(conf, 1);
  341. tt_int_op(ret, OP_EQ, 0);
  342. }
  343. /* Valid complex configuration. */
  344. {
  345. const char *conf =
  346. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  347. "HiddenServiceVersion 3\n"
  348. "HiddenServicePort 65535\n"
  349. "HiddenServicePort 22 1.1.1.1:22\n"
  350. #ifdef HAVE_SYS_UN_H
  351. "HiddenServicePort 9000 unix:/path/to/socket\n"
  352. #endif
  353. "HiddenServiceAllowUnknownPorts 0\n"
  354. "HiddenServiceMaxStreams 42\n"
  355. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  356. "HiddenServiceDirGroupReadable 1\n"
  357. "HiddenServiceNumIntroductionPoints 20\n";
  358. ret = helper_config_service(conf, 1);
  359. tt_int_op(ret, OP_EQ, 0);
  360. }
  361. /* Mix of v2 and v3. Still valid. */
  362. {
  363. const char *conf =
  364. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  365. "HiddenServiceVersion 2\n"
  366. "HiddenServicePort 80\n"
  367. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  368. "HiddenServiceVersion 3\n"
  369. "HiddenServicePort 81\n"
  370. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
  371. "HiddenServiceVersion 2\n"
  372. "HiddenServicePort 82\n";
  373. ret = helper_config_service(conf, 1);
  374. tt_int_op(ret, OP_EQ, 0);
  375. }
  376. done:
  377. ;
  378. }
  379. static void
  380. test_staging_service_v3(void *arg)
  381. {
  382. int ret;
  383. (void) arg;
  384. /* We don't validate a service object, this is the service test that are in
  385. * charge of doing so. We just check for the stable state after
  386. * registration. */
  387. hs_init();
  388. /* Time for a valid v3 service that should get staged. */
  389. const char *conf =
  390. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  391. "HiddenServiceVersion 3\n"
  392. "HiddenServicePort 65535\n"
  393. "HiddenServicePort 22 1.1.1.1:22\n"
  394. #ifdef HAVE_SYS_UN_H
  395. "HiddenServicePort 9000 unix:/path/to/socket\n"
  396. #endif
  397. "HiddenServiceAllowUnknownPorts 0\n"
  398. "HiddenServiceMaxStreams 42\n"
  399. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  400. "HiddenServiceDirGroupReadable 1\n"
  401. "HiddenServiceNumIntroductionPoints 20\n";
  402. ret = helper_config_service(conf, 0);
  403. tt_int_op(ret, OP_EQ, 0);
  404. /* Ok, we have a service in our map! Registration went well. */
  405. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  406. /* Make sure we don't have a magic v2 service out of this. */
  407. tt_int_op(rend_num_services(), OP_EQ, 0);
  408. done:
  409. hs_free_all();
  410. }
  411. struct testcase_t hs_config_tests[] = {
  412. /* Invalid service not specific to any version. */
  413. { "invalid_service", test_invalid_service, TT_FORK,
  414. NULL, NULL },
  415. { "valid_service", test_valid_service, TT_FORK,
  416. NULL, NULL },
  417. /* Test case only for version 2. */
  418. { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
  419. NULL, NULL },
  420. { "valid_service_v2", test_valid_service_v2, TT_FORK,
  421. NULL, NULL },
  422. /* Test case only for version 3. */
  423. { "invalid_service_v3", test_invalid_service_v3, TT_FORK,
  424. NULL, NULL },
  425. { "valid_service_v3", test_valid_service_v3, TT_FORK,
  426. NULL, NULL },
  427. /* Test service staging. */
  428. { "staging_service_v3", test_staging_service_v3, TT_FORK,
  429. NULL, NULL },
  430. END_OF_TESTCASES
  431. };