Merge branch 'bug27849_redux'
[tor.git] / src / test / test_hs_config.c
blob553b96758a085cb78f85663c42e600e0ac1621fd
1 /* Copyright (c) 2016-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file test_hs_config.c
6 * \brief Test hidden service configuration functionality.
7 */
9 #define CONFIG_PRIVATE
10 #define HS_SERVICE_PRIVATE
12 #include "test/test.h"
13 #include "test/test_helpers.h"
14 #include "test/log_test_helpers.h"
16 #include "app/config/config.h"
17 #include "feature/hs/hs_common.h"
18 #include "feature/hs/hs_config.h"
19 #include "feature/hs/hs_service.h"
20 #include "feature/rend/rendservice.h"
22 static int
23 helper_config_service(const char *conf, int validate_only)
25 int ret = 0;
26 or_options_t *options = NULL;
27 tt_assert(conf);
28 options = helper_parse_options(conf);
29 tt_assert(options);
30 ret = hs_config_service_all(options, validate_only);
31 done:
32 or_options_free(options);
33 return ret;
36 static void
37 test_invalid_service(void *arg)
39 int ret;
41 (void) arg;
43 /* Try with a missing port configuration. */
45 const char *conf =
46 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
47 "HiddenServiceVersion 1\n"; /* Wrong not supported version. */
48 setup_full_capture_of_logs(LOG_WARN);
49 ret = helper_config_service(conf, 1);
50 tt_int_op(ret, OP_EQ, -1);
51 expect_log_msg_containing("HiddenServiceVersion must be between 2 and 3");
52 teardown_capture_of_logs();
55 /* Bad value of HiddenServiceAllowUnknownPorts. */
57 const char *conf =
58 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
59 "HiddenServiceVersion 2\n"
60 "HiddenServiceAllowUnknownPorts 2\n"; /* Should be 0 or 1. */
61 setup_full_capture_of_logs(LOG_WARN);
62 ret = helper_config_service(conf, 1);
63 tt_int_op(ret, OP_EQ, -1);
64 expect_log_msg_containing("HiddenServiceAllowUnknownPorts must be "
65 "between 0 and 1, not 2");
66 teardown_capture_of_logs();
69 /* Bad value of HiddenServiceDirGroupReadable */
71 const char *conf =
72 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
73 "HiddenServiceVersion 2\n"
74 "HiddenServiceDirGroupReadable 2\n"; /* Should be 0 or 1. */
75 setup_full_capture_of_logs(LOG_WARN);
76 ret = helper_config_service(conf, 1);
77 tt_int_op(ret, OP_EQ, -1);
78 expect_log_msg_containing("HiddenServiceDirGroupReadable must be "
79 "between 0 and 1, not 2");
80 teardown_capture_of_logs();
83 /* Bad value of HiddenServiceMaxStreamsCloseCircuit */
85 const char *conf =
86 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
87 "HiddenServiceVersion 2\n"
88 "HiddenServiceMaxStreamsCloseCircuit 2\n"; /* Should be 0 or 1. */
89 setup_full_capture_of_logs(LOG_WARN);
90 ret = helper_config_service(conf, 1);
91 tt_int_op(ret, OP_EQ, -1);
92 expect_log_msg_containing("HiddenServiceMaxStreamsCloseCircuit must "
93 "be between 0 and 1, not 2");
94 teardown_capture_of_logs();
97 /* Too much max streams. */
99 const char *conf =
100 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
101 "HiddenServiceVersion 2\n"
102 "HiddenServicePort 80\n"
103 "HiddenServiceMaxStreams 65536\n"; /* One too many. */
104 setup_full_capture_of_logs(LOG_WARN);
105 ret = helper_config_service(conf, 1);
106 tt_int_op(ret, OP_EQ, -1);
107 expect_log_msg_containing("HiddenServiceMaxStreams must be between "
108 "0 and 65535, not 65536");
109 teardown_capture_of_logs();
112 /* Duplicate directory directive. */
114 const char *conf =
115 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
116 "HiddenServiceVersion 2\n"
117 "HiddenServicePort 80\n"
118 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
119 "HiddenServiceVersion 2\n"
120 "HiddenServicePort 81\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("Another hidden service is already "
125 "configured for directory");
126 teardown_capture_of_logs();
129 /* Bad port. */
131 const char *conf =
132 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
133 "HiddenServiceVersion 2\n"
134 "HiddenServicePort 65536\n";
135 setup_full_capture_of_logs(LOG_WARN);
136 ret = helper_config_service(conf, 1);
137 tt_int_op(ret, OP_EQ, -1);
138 expect_log_msg_containing("Missing or invalid port");
139 teardown_capture_of_logs();
142 /* Bad target addr:port separation. */
144 const char *conf =
145 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
146 "HiddenServiceVersion 2\n"
147 "HiddenServicePort 80 127.0.0.1 8000\n";
148 setup_full_capture_of_logs(LOG_WARN);
149 ret = helper_config_service(conf, 1);
150 tt_int_op(ret, OP_EQ, -1);
151 expect_log_msg_containing("HiddenServicePort parse error: "
152 "invalid port mapping");
153 teardown_capture_of_logs();
156 /* Out of order directives. */
158 const char *conf =
159 "HiddenServiceVersion 2\n"
160 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
161 "HiddenServicePort 80\n";
162 setup_full_capture_of_logs(LOG_WARN);
163 ret = helper_config_service(conf, 1);
164 tt_int_op(ret, OP_EQ, -1);
165 expect_log_msg_containing("HiddenServiceVersion with no preceding "
166 "HiddenServiceDir directive");
167 teardown_capture_of_logs();
170 done:
174 static void
175 test_valid_service(void *arg)
177 int ret;
179 (void) arg;
181 /* Mix of v2 and v3. Still valid. */
183 const char *conf =
184 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
185 "HiddenServiceVersion 2\n"
186 "HiddenServicePort 80\n"
187 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
188 "HiddenServiceVersion 3\n"
189 "HiddenServicePort 81\n"
190 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
191 "HiddenServiceVersion 2\n"
192 "HiddenServicePort 82\n";
193 ret = helper_config_service(conf, 1);
194 tt_int_op(ret, OP_EQ, 0);
197 done:
201 static void
202 test_invalid_service_v2(void *arg)
204 int validate_only = 1, ret;
206 (void) arg;
208 /* Try with a missing port configuration. */
210 const char *conf =
211 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
212 "HiddenServiceVersion 2\n";
213 setup_full_capture_of_logs(LOG_WARN);
214 ret = helper_config_service(conf, validate_only);
215 tt_int_op(ret, OP_EQ, -1);
216 expect_log_msg_containing("with no ports configured.");
217 teardown_capture_of_logs();
220 /* Too many introduction points. */
222 const char *conf =
223 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
224 "HiddenServiceVersion 2\n"
225 "HiddenServicePort 80\n"
226 "HiddenServiceNumIntroductionPoints 11\n"; /* One too many. */
227 setup_full_capture_of_logs(LOG_WARN);
228 ret = helper_config_service(conf, validate_only);
229 tt_int_op(ret, OP_EQ, -1);
230 expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
231 "be between 0 and 10, not 11");
232 teardown_capture_of_logs();
235 /* Too little introduction points. */
237 const char *conf =
238 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
239 "HiddenServiceVersion 2\n"
240 "HiddenServicePort 80\n"
241 "HiddenServiceNumIntroductionPoints -1\n";
242 setup_full_capture_of_logs(LOG_WARN);
243 ret = helper_config_service(conf, validate_only);
244 tt_int_op(ret, OP_EQ, -1);
245 expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
246 "be between 0 and 10, not -1");
247 teardown_capture_of_logs();
250 /* Bad authorized client type. */
252 const char *conf =
253 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
254 "HiddenServiceVersion 2\n"
255 "HiddenServicePort 80\n"
256 "HiddenServiceAuthorizeClient blah alice,bob\n"; /* blah is no good. */
257 setup_full_capture_of_logs(LOG_WARN);
258 ret = helper_config_service(conf, validate_only);
259 tt_int_op(ret, OP_EQ, -1);
260 expect_log_msg_containing("HiddenServiceAuthorizeClient contains "
261 "unrecognized auth-type");
262 teardown_capture_of_logs();
265 done:
269 static void
270 test_valid_service_v2(void *arg)
272 int ret;
274 (void) arg;
276 /* Valid complex configuration. Basic client authorization. */
278 const char *conf =
279 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
280 "HiddenServiceVersion 2\n"
281 "HiddenServicePort 80\n"
282 "HiddenServicePort 22 localhost:22\n"
283 #ifdef HAVE_SYS_UN_H
284 "HiddenServicePort 42 unix:/path/to/socket\n"
285 #endif
286 "HiddenServiceAuthorizeClient basic alice,bob,eve\n"
287 "HiddenServiceAllowUnknownPorts 1\n"
288 "HiddenServiceMaxStreams 42\n"
289 "HiddenServiceMaxStreamsCloseCircuit 0\n"
290 "HiddenServiceDirGroupReadable 1\n"
291 "HiddenServiceNumIntroductionPoints 7\n";
292 ret = helper_config_service(conf, 1);
293 tt_int_op(ret, OP_EQ, 0);
296 /* Valid complex configuration. Stealth client authorization. */
298 const char *conf =
299 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
300 "HiddenServiceVersion 2\n"
301 "HiddenServicePort 65535\n"
302 "HiddenServicePort 22 1.1.1.1:22\n"
303 #ifdef HAVE_SYS_UN_H
304 "HiddenServicePort 9000 unix:/path/to/socket\n"
305 #endif
306 "HiddenServiceAuthorizeClient stealth charlie,romeo\n"
307 "HiddenServiceAllowUnknownPorts 0\n"
308 "HiddenServiceMaxStreams 42\n"
309 "HiddenServiceMaxStreamsCloseCircuit 0\n"
310 "HiddenServiceDirGroupReadable 1\n"
311 "HiddenServiceNumIntroductionPoints 8\n";
312 ret = helper_config_service(conf, 1);
313 tt_int_op(ret, OP_EQ, 0);
316 done:
320 static void
321 test_invalid_service_v3(void *arg)
323 int validate_only = 1, ret;
325 (void) arg;
327 /* Try with a missing port configuration. */
329 const char *conf =
330 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
331 "HiddenServiceVersion 3\n";
332 setup_full_capture_of_logs(LOG_WARN);
333 ret = helper_config_service(conf, validate_only);
334 tt_int_op(ret, OP_EQ, -1);
335 expect_log_msg_containing("with no ports configured.");
336 teardown_capture_of_logs();
339 /* Too many introduction points. */
341 const char *conf =
342 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
343 "HiddenServiceVersion 3\n"
344 "HiddenServicePort 80\n"
345 "HiddenServiceNumIntroductionPoints 21\n"; /* One too many. */
346 setup_full_capture_of_logs(LOG_WARN);
347 ret = helper_config_service(conf, validate_only);
348 tt_int_op(ret, OP_EQ, -1);
349 expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
350 "be between 3 and 20, not 21.");
351 teardown_capture_of_logs();
354 /* Too little introduction points. */
356 const char *conf =
357 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
358 "HiddenServiceVersion 3\n"
359 "HiddenServicePort 80\n"
360 "HiddenServiceNumIntroductionPoints 1\n";
361 setup_full_capture_of_logs(LOG_WARN);
362 ret = helper_config_service(conf, validate_only);
363 tt_int_op(ret, OP_EQ, -1);
364 expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
365 "be between 3 and 20, not 1.");
366 teardown_capture_of_logs();
369 done:
373 static void
374 test_valid_service_v3(void *arg)
376 int ret;
378 (void) arg;
380 /* Valid complex configuration. */
382 const char *conf =
383 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
384 "HiddenServiceVersion 3\n"
385 "HiddenServicePort 80\n"
386 "HiddenServicePort 22 localhost:22\n"
387 #ifdef HAVE_SYS_UN_H
388 "HiddenServicePort 42 unix:/path/to/socket\n"
389 #endif
390 "HiddenServiceAllowUnknownPorts 1\n"
391 "HiddenServiceMaxStreams 42\n"
392 "HiddenServiceMaxStreamsCloseCircuit 0\n"
393 "HiddenServiceDirGroupReadable 1\n"
394 "HiddenServiceNumIntroductionPoints 7\n";
395 ret = helper_config_service(conf, 1);
396 tt_int_op(ret, OP_EQ, 0);
399 /* Valid complex configuration. */
401 const char *conf =
402 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
403 "HiddenServiceVersion 3\n"
404 "HiddenServicePort 65535\n"
405 "HiddenServicePort 22 1.1.1.1:22\n"
406 #ifdef HAVE_SYS_UN_H
407 "HiddenServicePort 9000 unix:/path/to/socket\n"
408 #endif
409 "HiddenServiceAllowUnknownPorts 0\n"
410 "HiddenServiceMaxStreams 42\n"
411 "HiddenServiceMaxStreamsCloseCircuit 0\n"
412 "HiddenServiceDirGroupReadable 1\n"
413 "HiddenServiceNumIntroductionPoints 20\n";
414 ret = helper_config_service(conf, 1);
415 tt_int_op(ret, OP_EQ, 0);
418 /* Mix of v2 and v3. Still valid. */
420 const char *conf =
421 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
422 "HiddenServiceVersion 2\n"
423 "HiddenServicePort 80\n"
424 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
425 "HiddenServiceVersion 3\n"
426 "HiddenServicePort 81\n"
427 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
428 "HiddenServiceVersion 2\n"
429 "HiddenServicePort 82\n";
430 ret = helper_config_service(conf, 1);
431 tt_int_op(ret, OP_EQ, 0);
434 done:
438 static void
439 test_staging_service_v3(void *arg)
441 int ret;
443 (void) arg;
445 /* We don't validate a service object, this is the service test that are in
446 * charge of doing so. We just check for the stable state after
447 * registration. */
449 hs_init();
451 /* Time for a valid v3 service that should get staged. */
452 const char *conf =
453 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
454 "HiddenServiceVersion 3\n"
455 "HiddenServicePort 65535\n"
456 "HiddenServicePort 22 1.1.1.1:22\n"
457 #ifdef HAVE_SYS_UN_H
458 "HiddenServicePort 9000 unix:/path/to/socket\n"
459 #endif
460 "HiddenServiceAllowUnknownPorts 0\n"
461 "HiddenServiceMaxStreams 42\n"
462 "HiddenServiceMaxStreamsCloseCircuit 0\n"
463 "HiddenServiceDirGroupReadable 1\n"
464 "HiddenServiceNumIntroductionPoints 20\n";
465 ret = helper_config_service(conf, 0);
466 tt_int_op(ret, OP_EQ, 0);
467 /* Ok, we have a service in our map! Registration went well. */
468 tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
469 /* Make sure we don't have a magic v2 service out of this. */
470 tt_int_op(rend_num_services(), OP_EQ, 0);
472 done:
473 hs_free_all();
476 struct testcase_t hs_config_tests[] = {
477 /* Invalid service not specific to any version. */
478 { "invalid_service", test_invalid_service, TT_FORK,
479 NULL, NULL },
480 { "valid_service", test_valid_service, TT_FORK,
481 NULL, NULL },
483 /* Test case only for version 2. */
484 { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
485 NULL, NULL },
486 { "valid_service_v2", test_valid_service_v2, TT_FORK,
487 NULL, NULL },
489 /* Test case only for version 3. */
490 { "invalid_service_v3", test_invalid_service_v3, TT_FORK,
491 NULL, NULL },
492 { "valid_service_v3", test_valid_service_v3, TT_FORK,
493 NULL, NULL },
495 /* Test service staging. */
496 { "staging_service_v3", test_staging_service_v3, TT_FORK,
497 NULL, NULL },
499 END_OF_TESTCASES