test_hs_config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. /* Bad target addr:port separation. */
  128. {
  129. const char *conf =
  130. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  131. "HiddenServiceVersion 2\n"
  132. "HiddenServicePort 80 127.0.0.1 8000\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("HiddenServicePort parse error: "
  137. "invalid port mapping");
  138. teardown_capture_of_logs();
  139. }
  140. /* Out of order directives. */
  141. {
  142. const char *conf =
  143. "HiddenServiceVersion 2\n"
  144. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  145. "HiddenServicePort 80\n";
  146. setup_full_capture_of_logs(LOG_WARN);
  147. ret = helper_config_service(conf, 1);
  148. tt_int_op(ret, OP_EQ, -1);
  149. expect_log_msg_containing("HiddenServiceVersion with no preceding "
  150. "HiddenServiceDir directive");
  151. teardown_capture_of_logs();
  152. }
  153. done:
  154. ;
  155. }
  156. static void
  157. test_valid_service(void *arg)
  158. {
  159. int ret;
  160. (void) arg;
  161. /* Mix of v2 and v3. Still valid. */
  162. {
  163. const char *conf =
  164. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  165. "HiddenServiceVersion 2\n"
  166. "HiddenServicePort 80\n"
  167. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  168. "HiddenServiceVersion 3\n"
  169. "HiddenServicePort 81\n"
  170. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
  171. "HiddenServiceVersion 2\n"
  172. "HiddenServicePort 82\n";
  173. ret = helper_config_service(conf, 1);
  174. tt_int_op(ret, OP_EQ, 0);
  175. }
  176. done:
  177. ;
  178. }
  179. static void
  180. test_invalid_service_v2(void *arg)
  181. {
  182. int validate_only = 1, ret;
  183. (void) arg;
  184. /* Try with a missing port configuration. */
  185. {
  186. const char *conf =
  187. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  188. "HiddenServiceVersion 2\n";
  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("with no ports configured.");
  193. teardown_capture_of_logs();
  194. }
  195. /* Too many introduction points. */
  196. {
  197. const char *conf =
  198. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  199. "HiddenServiceVersion 2\n"
  200. "HiddenServicePort 80\n"
  201. "HiddenServiceNumIntroductionPoints 11\n"; /* One too many. */
  202. setup_full_capture_of_logs(LOG_WARN);
  203. ret = helper_config_service(conf, validate_only);
  204. tt_int_op(ret, OP_EQ, -1);
  205. expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
  206. "be between 0 and 10, not 11");
  207. teardown_capture_of_logs();
  208. }
  209. /* Too little introduction points. */
  210. {
  211. const char *conf =
  212. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  213. "HiddenServiceVersion 2\n"
  214. "HiddenServicePort 80\n"
  215. "HiddenServiceNumIntroductionPoints -1\n";
  216. setup_full_capture_of_logs(LOG_WARN);
  217. ret = helper_config_service(conf, validate_only);
  218. tt_int_op(ret, OP_EQ, -1);
  219. expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
  220. "be between 0 and 10, not -1");
  221. teardown_capture_of_logs();
  222. }
  223. /* Bad authorized client type. */
  224. {
  225. const char *conf =
  226. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  227. "HiddenServiceVersion 2\n"
  228. "HiddenServicePort 80\n"
  229. "HiddenServiceAuthorizeClient blah alice,bob\n"; /* blah is no good. */
  230. setup_full_capture_of_logs(LOG_WARN);
  231. ret = helper_config_service(conf, validate_only);
  232. tt_int_op(ret, OP_EQ, -1);
  233. expect_log_msg_containing("HiddenServiceAuthorizeClient contains "
  234. "unrecognized auth-type");
  235. teardown_capture_of_logs();
  236. }
  237. done:
  238. ;
  239. }
  240. static void
  241. test_valid_service_v2(void *arg)
  242. {
  243. int ret;
  244. (void) arg;
  245. /* Valid complex configuration. Basic client authorization. */
  246. {
  247. const char *conf =
  248. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  249. "HiddenServiceVersion 2\n"
  250. "HiddenServicePort 80\n"
  251. "HiddenServicePort 22 localhost:22\n"
  252. #ifdef HAVE_SYS_UN_H
  253. "HiddenServicePort 42 unix:/path/to/socket\n"
  254. #endif
  255. "HiddenServiceAuthorizeClient basic alice,bob,eve\n"
  256. "HiddenServiceAllowUnknownPorts 1\n"
  257. "HiddenServiceMaxStreams 42\n"
  258. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  259. "HiddenServiceDirGroupReadable 1\n"
  260. "HiddenServiceNumIntroductionPoints 7\n";
  261. ret = helper_config_service(conf, 1);
  262. tt_int_op(ret, OP_EQ, 0);
  263. }
  264. /* Valid complex configuration. Stealth client authorization. */
  265. {
  266. const char *conf =
  267. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  268. "HiddenServiceVersion 2\n"
  269. "HiddenServicePort 65535\n"
  270. "HiddenServicePort 22 1.1.1.1:22\n"
  271. #ifdef HAVE_SYS_UN_H
  272. "HiddenServicePort 9000 unix:/path/to/socket\n"
  273. #endif
  274. "HiddenServiceAuthorizeClient stealth charlie,romeo\n"
  275. "HiddenServiceAllowUnknownPorts 0\n"
  276. "HiddenServiceMaxStreams 42\n"
  277. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  278. "HiddenServiceDirGroupReadable 1\n"
  279. "HiddenServiceNumIntroductionPoints 8\n";
  280. ret = helper_config_service(conf, 1);
  281. tt_int_op(ret, OP_EQ, 0);
  282. }
  283. done:
  284. ;
  285. }
  286. static void
  287. test_invalid_service_v3(void *arg)
  288. {
  289. int validate_only = 1, ret;
  290. (void) arg;
  291. /* Try with a missing port configuration. */
  292. {
  293. const char *conf =
  294. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  295. "HiddenServiceVersion 3\n";
  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("with no ports configured.");
  300. teardown_capture_of_logs();
  301. }
  302. /* Too many introduction points. */
  303. {
  304. const char *conf =
  305. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  306. "HiddenServiceVersion 3\n"
  307. "HiddenServicePort 80\n"
  308. "HiddenServiceNumIntroductionPoints 21\n"; /* One too many. */
  309. setup_full_capture_of_logs(LOG_WARN);
  310. ret = helper_config_service(conf, validate_only);
  311. tt_int_op(ret, OP_EQ, -1);
  312. expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
  313. "be between 3 and 20, not 21.");
  314. teardown_capture_of_logs();
  315. }
  316. /* Too little introduction points. */
  317. {
  318. const char *conf =
  319. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  320. "HiddenServiceVersion 3\n"
  321. "HiddenServicePort 80\n"
  322. "HiddenServiceNumIntroductionPoints 1\n";
  323. setup_full_capture_of_logs(LOG_WARN);
  324. ret = helper_config_service(conf, validate_only);
  325. tt_int_op(ret, OP_EQ, -1);
  326. expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
  327. "be between 3 and 20, not 1.");
  328. teardown_capture_of_logs();
  329. }
  330. done:
  331. ;
  332. }
  333. static void
  334. test_valid_service_v3(void *arg)
  335. {
  336. int ret;
  337. (void) arg;
  338. /* Valid complex configuration. */
  339. {
  340. const char *conf =
  341. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  342. "HiddenServiceVersion 3\n"
  343. "HiddenServicePort 80\n"
  344. "HiddenServicePort 22 localhost:22\n"
  345. #ifdef HAVE_SYS_UN_H
  346. "HiddenServicePort 42 unix:/path/to/socket\n"
  347. #endif
  348. "HiddenServiceAllowUnknownPorts 1\n"
  349. "HiddenServiceMaxStreams 42\n"
  350. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  351. "HiddenServiceDirGroupReadable 1\n"
  352. "HiddenServiceNumIntroductionPoints 7\n";
  353. ret = helper_config_service(conf, 1);
  354. tt_int_op(ret, OP_EQ, 0);
  355. }
  356. /* Valid complex configuration. */
  357. {
  358. const char *conf =
  359. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  360. "HiddenServiceVersion 3\n"
  361. "HiddenServicePort 65535\n"
  362. "HiddenServicePort 22 1.1.1.1:22\n"
  363. #ifdef HAVE_SYS_UN_H
  364. "HiddenServicePort 9000 unix:/path/to/socket\n"
  365. #endif
  366. "HiddenServiceAllowUnknownPorts 0\n"
  367. "HiddenServiceMaxStreams 42\n"
  368. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  369. "HiddenServiceDirGroupReadable 1\n"
  370. "HiddenServiceNumIntroductionPoints 20\n";
  371. ret = helper_config_service(conf, 1);
  372. tt_int_op(ret, OP_EQ, 0);
  373. }
  374. /* Mix of v2 and v3. Still valid. */
  375. {
  376. const char *conf =
  377. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  378. "HiddenServiceVersion 2\n"
  379. "HiddenServicePort 80\n"
  380. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  381. "HiddenServiceVersion 3\n"
  382. "HiddenServicePort 81\n"
  383. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
  384. "HiddenServiceVersion 2\n"
  385. "HiddenServicePort 82\n";
  386. ret = helper_config_service(conf, 1);
  387. tt_int_op(ret, OP_EQ, 0);
  388. }
  389. done:
  390. ;
  391. }
  392. static void
  393. test_staging_service_v3(void *arg)
  394. {
  395. int ret;
  396. (void) arg;
  397. /* We don't validate a service object, this is the service test that are in
  398. * charge of doing so. We just check for the stable state after
  399. * registration. */
  400. hs_init();
  401. /* Time for a valid v3 service that should get staged. */
  402. const char *conf =
  403. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  404. "HiddenServiceVersion 3\n"
  405. "HiddenServicePort 65535\n"
  406. "HiddenServicePort 22 1.1.1.1:22\n"
  407. #ifdef HAVE_SYS_UN_H
  408. "HiddenServicePort 9000 unix:/path/to/socket\n"
  409. #endif
  410. "HiddenServiceAllowUnknownPorts 0\n"
  411. "HiddenServiceMaxStreams 42\n"
  412. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  413. "HiddenServiceDirGroupReadable 1\n"
  414. "HiddenServiceNumIntroductionPoints 20\n";
  415. ret = helper_config_service(conf, 0);
  416. tt_int_op(ret, OP_EQ, 0);
  417. /* Ok, we have a service in our map! Registration went well. */
  418. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  419. /* Make sure we don't have a magic v2 service out of this. */
  420. tt_int_op(rend_num_services(), OP_EQ, 0);
  421. done:
  422. hs_free_all();
  423. }
  424. struct testcase_t hs_config_tests[] = {
  425. /* Invalid service not specific to any version. */
  426. { "invalid_service", test_invalid_service, TT_FORK,
  427. NULL, NULL },
  428. { "valid_service", test_valid_service, TT_FORK,
  429. NULL, NULL },
  430. /* Test case only for version 2. */
  431. { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
  432. NULL, NULL },
  433. { "valid_service_v2", test_valid_service_v2, TT_FORK,
  434. NULL, NULL },
  435. /* Test case only for version 3. */
  436. { "invalid_service_v3", test_invalid_service_v3, TT_FORK,
  437. NULL, NULL },
  438. { "valid_service_v3", test_valid_service_v3, TT_FORK,
  439. NULL, NULL },
  440. /* Test service staging. */
  441. { "staging_service_v3", test_staging_service_v3, TT_FORK,
  442. NULL, NULL },
  443. END_OF_TESTCASES
  444. };