Fix remaining cases of hs_config failures without af_unix
[tor.git] / src / test / test_hs_config.c
blob4d4bbb8891bf17cdec2e0fe3240f19383a704fc6
1 /* Copyright (c) 2016, 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.h"
13 #include "test_helpers.h"
14 #include "log_test_helpers.h"
16 #include "config.h"
17 #include "hs_common.h"
18 #include "hs_config.h"
19 #include "hs_service.h"
20 #include "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 /* Out of order directives. */
144 const char *conf =
145 "HiddenServiceVersion 2\n"
146 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
147 "HiddenServicePort 80\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("HiddenServiceVersion with no preceding "
152 "HiddenServiceDir directive");
153 teardown_capture_of_logs();
156 done:
160 static void
161 test_valid_service(void *arg)
163 int ret;
165 (void) arg;
167 /* Mix of v2 and v3. Still valid. */
169 const char *conf =
170 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
171 "HiddenServiceVersion 2\n"
172 "HiddenServicePort 80\n"
173 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
174 "HiddenServiceVersion 3\n"
175 "HiddenServicePort 81\n"
176 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
177 "HiddenServiceVersion 2\n"
178 "HiddenServicePort 82\n";
179 ret = helper_config_service(conf, 1);
180 tt_int_op(ret, OP_EQ, 0);
183 done:
187 static void
188 test_invalid_service_v2(void *arg)
190 int validate_only = 1, ret;
192 (void) arg;
194 /* Try with a missing port configuration. */
196 const char *conf =
197 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
198 "HiddenServiceVersion 2\n";
199 setup_full_capture_of_logs(LOG_WARN);
200 ret = helper_config_service(conf, validate_only);
201 tt_int_op(ret, OP_EQ, -1);
202 expect_log_msg_containing("with no ports configured.");
203 teardown_capture_of_logs();
206 /* Too many introduction points. */
208 const char *conf =
209 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
210 "HiddenServiceVersion 2\n"
211 "HiddenServicePort 80\n"
212 "HiddenServiceNumIntroductionPoints 11\n"; /* One too many. */
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("HiddenServiceNumIntroductionPoints should "
217 "be between 0 and 10, not 11");
218 teardown_capture_of_logs();
221 /* Too little introduction points. */
223 const char *conf =
224 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
225 "HiddenServiceVersion 2\n"
226 "HiddenServicePort 80\n"
227 "HiddenServiceNumIntroductionPoints -1\n";
228 setup_full_capture_of_logs(LOG_WARN);
229 ret = helper_config_service(conf, validate_only);
230 tt_int_op(ret, OP_EQ, -1);
231 expect_log_msg_containing("HiddenServiceNumIntroductionPoints should "
232 "be between 0 and 10, not -1");
233 teardown_capture_of_logs();
236 /* Bad authorized client type. */
238 const char *conf =
239 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
240 "HiddenServiceVersion 2\n"
241 "HiddenServicePort 80\n"
242 "HiddenServiceAuthorizeClient blah alice,bob\n"; /* blah is no good. */
243 setup_full_capture_of_logs(LOG_WARN);
244 ret = helper_config_service(conf, validate_only);
245 tt_int_op(ret, OP_EQ, -1);
246 expect_log_msg_containing("HiddenServiceAuthorizeClient contains "
247 "unrecognized auth-type");
248 teardown_capture_of_logs();
251 done:
255 static void
256 test_valid_service_v2(void *arg)
258 int ret;
260 (void) arg;
262 /* Valid complex configuration. Basic client authorization. */
264 const char *conf =
265 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
266 "HiddenServiceVersion 2\n"
267 "HiddenServicePort 80\n"
268 "HiddenServicePort 22 localhost:22\n"
269 #ifndef HAVE_SYS_UN_H
270 "HiddenServicePort 42 unix:/path/to/socket\n"
271 #endif
272 "HiddenServiceAuthorizeClient basic alice,bob,eve\n"
273 "HiddenServiceAllowUnknownPorts 1\n"
274 "HiddenServiceMaxStreams 42\n"
275 "HiddenServiceMaxStreamsCloseCircuit 0\n"
276 "HiddenServiceDirGroupReadable 1\n"
277 "HiddenServiceNumIntroductionPoints 7\n";
278 ret = helper_config_service(conf, 1);
279 tt_int_op(ret, OP_EQ, 0);
282 /* Valid complex configuration. Stealth client authorization. */
284 const char *conf =
285 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
286 "HiddenServiceVersion 2\n"
287 "HiddenServicePort 65535\n"
288 "HiddenServicePort 22 1.1.1.1:22\n"
289 #ifndef HAVE_SYS_UN_H
290 "HiddenServicePort 9000 unix:/path/to/socket\n"
291 #endif
292 "HiddenServiceAuthorizeClient stealth charlie,romeo\n"
293 "HiddenServiceAllowUnknownPorts 0\n"
294 "HiddenServiceMaxStreams 42\n"
295 "HiddenServiceMaxStreamsCloseCircuit 0\n"
296 "HiddenServiceDirGroupReadable 1\n"
297 "HiddenServiceNumIntroductionPoints 8\n";
298 ret = helper_config_service(conf, 1);
299 tt_int_op(ret, OP_EQ, 0);
302 done:
306 static void
307 test_invalid_service_v3(void *arg)
309 int validate_only = 1, ret;
311 (void) arg;
313 /* Try with a missing port configuration. */
315 const char *conf =
316 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
317 "HiddenServiceVersion 3\n";
318 setup_full_capture_of_logs(LOG_WARN);
319 ret = helper_config_service(conf, validate_only);
320 tt_int_op(ret, OP_EQ, -1);
321 expect_log_msg_containing("with no ports configured.");
322 teardown_capture_of_logs();
325 /* Too many introduction points. */
327 const char *conf =
328 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
329 "HiddenServiceVersion 3\n"
330 "HiddenServicePort 80\n"
331 "HiddenServiceNumIntroductionPoints 21\n"; /* One too many. */
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("HiddenServiceNumIntroductionPoints must "
336 "be between 3 and 20, not 21.");
337 teardown_capture_of_logs();
340 /* Too little introduction points. */
342 const char *conf =
343 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
344 "HiddenServiceVersion 3\n"
345 "HiddenServicePort 80\n"
346 "HiddenServiceNumIntroductionPoints 1\n";
347 setup_full_capture_of_logs(LOG_WARN);
348 ret = helper_config_service(conf, validate_only);
349 tt_int_op(ret, OP_EQ, -1);
350 expect_log_msg_containing("HiddenServiceNumIntroductionPoints must "
351 "be between 3 and 20, not 1.");
352 teardown_capture_of_logs();
355 done:
359 static void
360 test_valid_service_v3(void *arg)
362 int ret;
364 (void) arg;
366 /* Valid complex configuration. */
368 const char *conf =
369 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
370 "HiddenServiceVersion 3\n"
371 "HiddenServicePort 80\n"
372 "HiddenServicePort 22 localhost:22\n"
373 #ifndef HAVE_SYS_UN_H
374 "HiddenServicePort 42 unix:/path/to/socket\n"
375 #endif
376 "HiddenServiceAllowUnknownPorts 1\n"
377 "HiddenServiceMaxStreams 42\n"
378 "HiddenServiceMaxStreamsCloseCircuit 0\n"
379 "HiddenServiceDirGroupReadable 1\n"
380 "HiddenServiceNumIntroductionPoints 7\n";
381 ret = helper_config_service(conf, 1);
382 tt_int_op(ret, OP_EQ, 0);
385 /* Valid complex configuration. */
387 const char *conf =
388 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
389 "HiddenServiceVersion 3\n"
390 "HiddenServicePort 65535\n"
391 "HiddenServicePort 22 1.1.1.1:22\n"
392 #ifndef HAVE_SYS_UN_H
393 "HiddenServicePort 9000 unix:/path/to/socket\n"
394 #endif
395 "HiddenServiceAllowUnknownPorts 0\n"
396 "HiddenServiceMaxStreams 42\n"
397 "HiddenServiceMaxStreamsCloseCircuit 0\n"
398 "HiddenServiceDirGroupReadable 1\n"
399 "HiddenServiceNumIntroductionPoints 20\n";
400 ret = helper_config_service(conf, 1);
401 tt_int_op(ret, OP_EQ, 0);
404 /* Mix of v2 and v3. Still valid. */
406 const char *conf =
407 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs1\n"
408 "HiddenServiceVersion 2\n"
409 "HiddenServicePort 80\n"
410 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
411 "HiddenServiceVersion 3\n"
412 "HiddenServicePort 81\n"
413 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs3\n"
414 "HiddenServiceVersion 2\n"
415 "HiddenServicePort 82\n";
416 ret = helper_config_service(conf, 1);
417 tt_int_op(ret, OP_EQ, 0);
420 done:
424 static void
425 test_staging_service_v3(void *arg)
427 int ret;
429 (void) arg;
431 /* We don't validate a service object, this is the service test that are in
432 * charge of doing so. We just check for the stable state after
433 * registration. */
435 hs_init();
437 /* Time for a valid v3 service that should get staged. */
438 const char *conf =
439 "HiddenServiceDir /tmp/tor-test-hs-RANDOM/hs2\n"
440 "HiddenServiceVersion 3\n"
441 "HiddenServicePort 65535\n"
442 "HiddenServicePort 22 1.1.1.1:22\n"
443 #ifndef HAVE_SYS_UN_H
444 "HiddenServicePort 9000 unix:/path/to/socket\n"
445 #endif
446 "HiddenServiceAllowUnknownPorts 0\n"
447 "HiddenServiceMaxStreams 42\n"
448 "HiddenServiceMaxStreamsCloseCircuit 0\n"
449 "HiddenServiceDirGroupReadable 1\n"
450 "HiddenServiceNumIntroductionPoints 20\n";
451 ret = helper_config_service(conf, 0);
452 tt_int_op(ret, OP_EQ, 0);
453 /* Ok, we have a service in our map! Registration went well. */
454 tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
455 /* Make sure we don't have a magic v2 service out of this. */
456 tt_int_op(num_rend_services(), OP_EQ, 0);
458 done:
459 hs_free_all();
462 struct testcase_t hs_config_tests[] = {
463 /* Invalid service not specific to any version. */
464 { "invalid_service", test_invalid_service, TT_FORK,
465 NULL, NULL },
466 { "valid_service", test_valid_service, TT_FORK,
467 NULL, NULL },
469 /* Test case only for version 2. */
470 { "invalid_service_v2", test_invalid_service_v2, TT_FORK,
471 NULL, NULL },
472 { "valid_service_v2", test_valid_service_v2, TT_FORK,
473 NULL, NULL },
475 /* Test case only for version 3. */
476 { "invalid_service_v3", test_invalid_service_v3, TT_FORK,
477 NULL, NULL },
478 { "valid_service_v3", test_valid_service_v3, TT_FORK,
479 NULL, NULL },
481 /* Test service staging. */
482 { "staging_service_v3", test_staging_service_v3, TT_FORK,
483 NULL, NULL },
485 END_OF_TESTCASES