test_hs_config.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. /* v2-specific HiddenServiceAuthorizeClient set. */
  331. {
  332. const char *conf =
  333. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  334. "HiddenServiceVersion 3\n"
  335. "HiddenServiceAuthorizeClient stealth client1\n";
  336. setup_full_capture_of_logs(LOG_WARN);
  337. ret = helper_config_service(conf, validate_only);
  338. tt_int_op(ret, OP_EQ, -1);
  339. expect_log_msg_containing("Hidden service option "
  340. "HiddenServiceAuthorizeClient is incompatible "
  341. "with version 3 of service in "
  342. "/tmp/tor-test-hs-RANDOM/hs1");
  343. teardown_capture_of_logs();
  344. }
  345. done:
  346. ;
  347. }
  348. static void
  349. test_valid_service_v3(void *arg)
  350. {
  351. int ret;
  352. (void) arg;
  353. /* Valid complex configuration. */
  354. {
  355. const char *conf =
  356. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  357. "HiddenServiceVersion 3\n"
  358. "HiddenServicePort 80\n"
  359. "HiddenServicePort 22 localhost:22\n"
  360. #ifdef HAVE_SYS_UN_H
  361. "HiddenServicePort 42 unix:/path/to/socket\n"
  362. #endif
  363. "HiddenServiceAllowUnknownPorts 1\n"
  364. "HiddenServiceMaxStreams 42\n"
  365. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  366. "HiddenServiceDirGroupReadable 1\n"
  367. "HiddenServiceNumIntroductionPoints 7\n";
  368. ret = helper_config_service(conf, 1);
  369. tt_int_op(ret, OP_EQ, 0);
  370. }
  371. /* Valid complex configuration. */
  372. {
  373. const char *conf =
  374. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  375. "HiddenServiceVersion 3\n"
  376. "HiddenServicePort 65535\n"
  377. "HiddenServicePort 22 1.1.1.1:22\n"
  378. #ifdef HAVE_SYS_UN_H
  379. "HiddenServicePort 9000 unix:/path/to/socket\n"
  380. #endif
  381. "HiddenServiceAllowUnknownPorts 0\n"
  382. "HiddenServiceMaxStreams 42\n"
  383. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  384. "HiddenServiceDirGroupReadable 1\n"
  385. "HiddenServiceNumIntroductionPoints 20\n";
  386. ret = helper_config_service(conf, 1);
  387. tt_int_op(ret, OP_EQ, 0);
  388. }
  389. /* Mix of v2 and v3. Still valid. */
  390. {
  391. const char *conf =
  392. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
  393. "HiddenServiceVersion 2\n"
  394. "HiddenServicePort 80\n"
  395. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  396. "HiddenServiceVersion 3\n"
  397. "HiddenServicePort 81\n"
  398. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
  399. "HiddenServiceVersion 2\n"
  400. "HiddenServicePort 82\n";
  401. ret = helper_config_service(conf, 1);
  402. tt_int_op(ret, OP_EQ, 0);
  403. }
  404. done:
  405. ;
  406. }
  407. static void
  408. test_staging_service_v3(void *arg)
  409. {
  410. int ret;
  411. (void) arg;
  412. /* We don't validate a service object, this is the service test that are in
  413. * charge of doing so. We just check for the stable state after
  414. * registration. */
  415. hs_init();
  416. /* Time for a valid v3 service that should get staged. */
  417. const char *conf =
  418. "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
  419. "HiddenServiceVersion 3\n"
  420. "HiddenServicePort 65535\n"
  421. "HiddenServicePort 22 1.1.1.1:22\n"
  422. #ifdef HAVE_SYS_UN_H
  423. "HiddenServicePort 9000 unix:/path/to/socket\n"
  424. #endif
  425. "HiddenServiceAllowUnknownPorts 0\n"
  426. "HiddenServiceMaxStreams 42\n"
  427. "HiddenServiceMaxStreamsCloseCircuit 0\n"
  428. "HiddenServiceDirGroupReadable 1\n"
  429. "HiddenServiceNumIntroductionPoints 20\n";
  430. ret = helper_config_service(conf, 0);
  431. tt_int_op(ret, OP_EQ, 0);
  432. /* Ok, we have a service in our map! Registration went well. */
  433. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  434. /* Make sure we don't have a magic v2 service out of this. */
  435. tt_int_op(rend_num_services(), OP_EQ, 0);
  436. done:
  437. hs_free_all();
  438. }
  439. struct testcase_t hs_config_tests[] = {
  440. /* Invalid service not specific to any version. */
  441. { "invalid_service", test_invalid_service, TT_FORK,
  442. NULL, NULL },
  443. { "valid_service", test_valid_service, TT_FORK,
  444. NULL, NULL },
  445. /* Test case only for version 2. */
  446. { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
  447. NULL, NULL },
  448. { "valid_service_v2", test_valid_service_v2, TT_FORK,
  449. NULL, NULL },
  450. /* Test case only for version 3. */
  451. { "invalid_service_v3", test_invalid_service_v3, TT_FORK,
  452. NULL, NULL },
  453. { "valid_service_v3", test_valid_service_v3, TT_FORK,
  454. NULL, NULL },
  455. /* Test service staging. */
  456. { "staging_service_v3", test_staging_service_v3, TT_FORK,
  457. NULL, NULL },
  458. END_OF_TESTCASES
  459. };