1 /* Copyright (c) 2016-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file test_hs_config.c
6 * \brief Test hidden service configuration functionality.
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"
23 helper_config_service(const char *conf
, int validate_only
)
26 or_options_t
*options
= NULL
;
28 options
= helper_parse_options(conf
);
30 ret
= hs_config_service_all(options
, validate_only
);
32 or_options_free(options
);
37 test_invalid_service(void *arg
)
43 /* Try with a missing port configuration. */
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. */
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 */
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 */
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. */
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. */
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();
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. */
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. */
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();
175 test_valid_service(void *arg
)
181 /* Mix of v2 and v3. Still valid. */
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);
202 test_invalid_service_v2(void *arg
)
204 int validate_only
= 1, ret
;
208 /* Try with a missing port configuration. */
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. */
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. */
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. */
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();
270 test_valid_service_v2(void *arg
)
276 /* Valid complex configuration. Basic client authorization. */
279 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
280 "HiddenServiceVersion 2\n"
281 "HiddenServicePort 80\n"
282 "HiddenServicePort 22 localhost:22\n"
284 "HiddenServicePort 42 unix:/path/to/socket\n"
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. */
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"
304 "HiddenServicePort 9000 unix:/path/to/socket\n"
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);
321 test_invalid_service_v3(void *arg
)
323 int validate_only
= 1, ret
;
327 /* Try with a missing port configuration. */
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. */
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. */
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();
374 test_valid_service_v3(void *arg
)
380 /* Valid complex configuration. */
383 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
384 "HiddenServiceVersion 3\n"
385 "HiddenServicePort 80\n"
386 "HiddenServicePort 22 localhost:22\n"
388 "HiddenServicePort 42 unix:/path/to/socket\n"
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. */
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"
407 "HiddenServicePort 9000 unix:/path/to/socket\n"
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. */
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);
439 test_staging_service_v3(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
451 /* Time for a valid v3 service that should get staged. */
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"
458 "HiddenServicePort 9000 unix:/path/to/socket\n"
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);
476 struct testcase_t hs_config_tests
[] = {
477 /* Invalid service not specific to any version. */
478 { "invalid_service", test_invalid_service
, TT_FORK
,
480 { "valid_service", test_valid_service
, TT_FORK
,
483 /* Test case only for version 2. */
484 { "invalid_service_v2", test_invalid_service_v2
, TT_FORK
,
486 { "valid_service_v2", test_valid_service_v2
, TT_FORK
,
489 /* Test case only for version 3. */
490 { "invalid_service_v3", test_invalid_service_v3
, TT_FORK
,
492 { "valid_service_v3", test_valid_service_v3
, TT_FORK
,
495 /* Test service staging. */
496 { "staging_service_v3", test_staging_service_v3
, TT_FORK
,