Merge branch 'fix-changelogs' into 'main'
[tor.git] / src / test / test_config.c
blobc45b10657bdd34b057e0f5eafcdd88bfd2d79b8e
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
8 #define CONFIG_PRIVATE
9 #define RELAY_CONFIG_PRIVATE
10 #define RELAY_TRANSPORT_CONFIG_PRIVATE
11 #define PT_PRIVATE
12 #define ROUTERSET_PRIVATE
13 #include "core/or/or.h"
14 #include "lib/net/address.h"
15 #include "lib/net/resolve.h"
16 #include "feature/client/addressmap.h"
17 #include "feature/client/bridges.h"
18 #include "core/or/circuitmux_ewma.h"
19 #include "core/or/circuitbuild.h"
20 #include "app/config/config.h"
21 #include "app/config/resolve_addr.h"
22 #include "feature/relay/relay_config.h"
23 #include "feature/relay/transport_config.h"
24 #include "lib/confmgt/confmgt.h"
25 #include "core/mainloop/connection.h"
26 #include "core/or/connection_edge.h"
27 #include "test/test.h"
28 #include "core/or/connection_or.h"
29 #include "feature/control/control.h"
30 #include "core/mainloop/cpuworker.h"
31 #include "feature/dircache/dirserv.h"
32 #include "feature/dirclient/dirclient_modes.h"
33 #include "feature/dirauth/dirvote.h"
34 #include "feature/relay/dns.h"
35 #include "feature/client/entrynodes.h"
36 #include "feature/client/transports.h"
37 #include "feature/relay/ext_orport.h"
38 #include "lib/geoip/geoip.h"
39 #include "feature/hibernate/hibernate.h"
40 #include "core/mainloop/mainloop.h"
41 #include "feature/nodelist/networkstatus.h"
42 #include "feature/nodelist/nodelist.h"
43 #include "core/or/policies.h"
44 #include "feature/relay/relay_find_addr.h"
45 #include "feature/relay/router.h"
46 #include "feature/relay/routermode.h"
47 #include "feature/nodelist/dirlist.h"
48 #include "feature/nodelist/routerlist.h"
49 #include "feature/nodelist/routerset.h"
50 #include "app/config/statefile.h"
52 #include "test/test_helpers.h"
53 #include "test/resolve_test_helpers.h"
54 #include "test/log_test_helpers.h"
56 #include "feature/dirclient/dir_server_st.h"
57 #include "core/or/port_cfg_st.h"
58 #include "feature/nodelist/routerinfo_st.h"
60 #include "lib/fs/conffile.h"
61 #include "lib/meminfo/meminfo.h"
62 #include "lib/net/gethostname.h"
63 #include "lib/encoding/confline.h"
64 #include "lib/encoding/kvline.h"
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69 #ifdef HAVE_SYS_STAT_H
70 #include <sys/stat.h>
71 #endif
73 static void
74 test_config_addressmap(void *arg)
76 char buf[1024];
77 char address[256];
78 time_t expires = TIME_MAX;
79 (void)arg;
81 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
82 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
83 "MapAddress *.google.com *.torserver.exit\n"
84 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
85 "MapAddress *.cn.com www.cnn.com\n"
86 "MapAddress *.cnn.com www.cnn.com\n"
87 "MapAddress ex.com www.cnn.com\n"
88 "MapAddress ey.com *.cnn.com\n"
89 "MapAddress www.torproject.org 1.1.1.1\n"
90 "MapAddress other.torproject.org "
91 "this.torproject.org.otherserver.exit\n"
92 "MapAddress test.torproject.org 2.2.2.2\n"
93 "MapAddress www.google.com 3.3.3.3\n"
94 "MapAddress www.example.org 4.4.4.4\n"
95 "MapAddress 4.4.4.4 7.7.7.7\n"
96 "MapAddress 4.4.4.4 5.5.5.5\n"
97 "MapAddress www.infiniteloop.org 6.6.6.6\n"
98 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
99 , sizeof(buf));
101 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
102 config_register_addressmaps(get_options());
104 /* Use old interface for now, so we don't need to rewrite the unit tests */
105 #define addressmap_rewrite(a,s,eo,ao) \
106 addressmap_rewrite((a),(s), ~0, (eo),(ao))
108 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
109 strlcpy(address, "www.invalidwildcard.com", sizeof(address));
110 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
112 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
113 strlcpy(address, "www.invalidasterisk.com", sizeof(address));
114 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
116 /* Where no mapping for FQDN match on top-level domain */
117 /* MapAddress .google.com .torserver.exit */
118 strlcpy(address, "reader.google.com", sizeof(address));
119 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
120 tt_str_op(address,OP_EQ, "reader.torserver.exit");
122 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
123 strlcpy(address, "reader.yahoo.com", sizeof(address));
124 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
125 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
127 /*MapAddress *.cnn.com www.cnn.com */
128 strlcpy(address, "cnn.com", sizeof(address));
129 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
130 tt_str_op(address,OP_EQ, "www.cnn.com");
132 /* MapAddress .cn.com www.cnn.com */
133 strlcpy(address, "www.cn.com", sizeof(address));
134 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
135 tt_str_op(address,OP_EQ, "www.cnn.com");
137 /* MapAddress ex.com www.cnn.com - no match */
138 strlcpy(address, "www.ex.com", sizeof(address));
139 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
141 /* MapAddress ey.com *.cnn.com - invalid expression */
142 strlcpy(address, "ey.com", sizeof(address));
143 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
145 /* Where mapping for FQDN match on FQDN */
146 strlcpy(address, "www.google.com", sizeof(address));
147 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
148 tt_str_op(address,OP_EQ, "3.3.3.3");
150 strlcpy(address, "www.torproject.org", sizeof(address));
151 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
152 tt_str_op(address,OP_EQ, "1.1.1.1");
154 strlcpy(address, "other.torproject.org", sizeof(address));
155 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
156 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
158 strlcpy(address, "test.torproject.org", sizeof(address));
159 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
160 tt_str_op(address,OP_EQ, "2.2.2.2");
162 /* Test a chain of address mappings and the order in which they were added:
163 "MapAddress www.example.org 4.4.4.4"
164 "MapAddress 4.4.4.4 7.7.7.7"
165 "MapAddress 4.4.4.4 5.5.5.5"
167 strlcpy(address, "www.example.org", sizeof(address));
168 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
169 tt_str_op(address,OP_EQ, "5.5.5.5");
171 /* Test infinite address mapping results in no change */
172 strlcpy(address, "www.infiniteloop.org", sizeof(address));
173 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
174 tt_str_op(address,OP_EQ, "www.infiniteloop.org");
176 /* Test we don't find false positives */
177 strlcpy(address, "www.example.com", sizeof(address));
178 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
180 /* Test top-level-domain matching a bit harder */
181 config_free_lines(get_options_mutable()->AddressMap);
182 addressmap_clear_configured();
183 strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
184 "MapAddress *.torproject.org 1.1.1.1\n"
185 "MapAddress *.net 2.2.2.2\n"
186 , sizeof(buf));
187 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
188 config_register_addressmaps(get_options());
190 strlcpy(address, "www.abc.com", sizeof(address));
191 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
192 tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
194 strlcpy(address, "www.def.com", sizeof(address));
195 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
196 tt_str_op(address,OP_EQ, "www.def.torserver.exit");
198 strlcpy(address, "www.torproject.org", sizeof(address));
199 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
200 tt_str_op(address,OP_EQ, "1.1.1.1");
202 strlcpy(address, "test.torproject.org", sizeof(address));
203 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
204 tt_str_op(address,OP_EQ, "1.1.1.1");
206 strlcpy(address, "torproject.net", sizeof(address));
207 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
208 tt_str_op(address,OP_EQ, "2.2.2.2");
210 /* We don't support '*' as a mapping directive */
211 config_free_lines(get_options_mutable()->AddressMap);
212 addressmap_clear_configured();
213 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
214 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
215 config_register_addressmaps(get_options());
217 strlcpy(address, "www.abc.com", sizeof(address));
218 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
220 strlcpy(address, "www.def.net", sizeof(address));
221 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
223 strlcpy(address, "www.torproject.org", sizeof(address));
224 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
226 #undef addressmap_rewrite
228 done:
229 config_free_lines(get_options_mutable()->AddressMap);
230 get_options_mutable()->AddressMap = NULL;
231 addressmap_free_all();
234 static int
235 is_private_dir(const char* path)
237 struct stat st;
238 int r = stat(path, &st);
239 if (r) {
240 return 0;
242 #if !defined (_WIN32)
243 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
244 return 0;
246 #endif
247 return 1;
250 static void
251 test_config_check_or_create_data_subdir(void *arg)
253 or_options_t *options = get_options_mutable();
254 char *datadir;
255 const char *subdir = "test_stats";
256 char *subpath;
257 struct stat st;
258 int r;
259 #if !defined (_WIN32)
260 unsigned group_permission;
261 #endif
262 (void)arg;
264 tor_free(options->DataDirectory);
265 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
266 subpath = get_datadir_fname(subdir);
268 #if defined (_WIN32)
269 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
270 #else
271 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
272 #endif
274 r = stat(subpath, &st);
276 // The subdirectory shouldn't exist yet,
277 // but should be created by the call to check_or_create_data_subdir.
278 tt_assert(r && (errno == ENOENT));
279 tt_assert(!check_or_create_data_subdir(subdir));
280 tt_assert(is_private_dir(subpath));
282 // The check should return 0, if the directory already exists
283 // and is private to the user.
284 tt_assert(!check_or_create_data_subdir(subdir));
286 r = stat(subpath, &st);
287 if (r) {
288 tt_abort_perror("stat");
291 #if !defined (_WIN32)
292 group_permission = st.st_mode | 0070;
293 r = chmod(subpath, group_permission);
295 if (r) {
296 tt_abort_perror("chmod");
299 // If the directory exists, but its mode is too permissive
300 // a call to check_or_create_data_subdir should reset the mode.
301 tt_assert(!is_private_dir(subpath));
302 tt_assert(!check_or_create_data_subdir(subdir));
303 tt_assert(is_private_dir(subpath));
304 #endif /* !defined (_WIN32) */
306 done:
307 rmdir(subpath);
308 tor_free(datadir);
309 tor_free(subpath);
312 static void
313 test_config_write_to_data_subdir(void *arg)
315 or_options_t* options = get_options_mutable();
316 char *datadir;
317 char *cp = NULL;
318 const char* subdir = "test_stats";
319 const char* fname = "test_file";
320 const char* str =
321 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
322 "elitr, sed diam nonumy eirmod\n"
323 "tempor invidunt ut labore et dolore magna aliquyam\n"
324 "erat, sed diam voluptua.\n"
325 "At vero eos et accusam et justo duo dolores et ea\n"
326 "rebum. Stet clita kasd gubergren,\n"
327 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
328 "Lorem ipsum dolor sit amet,\n"
329 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
330 "tempor invidunt ut labore et dolore\n"
331 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
332 "accusam et justo duo dolores et\n"
333 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
334 "sanctus est Lorem ipsum dolor sit amet.";
335 char* filepath = NULL;
336 (void)arg;
338 tor_free(options->DataDirectory);
339 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
340 filepath = get_datadir_fname2(subdir, fname);
342 #if defined (_WIN32)
343 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
344 #else
345 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
346 #endif
348 // Write attempt should fail, if subdirectory doesn't exist.
349 tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
350 tt_assert(! check_or_create_data_subdir(subdir));
352 // Content of file after write attempt should be
353 // equal to the original string.
354 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
355 cp = read_file_to_str(filepath, 0, NULL);
356 tt_str_op(cp,OP_EQ, str);
357 tor_free(cp);
359 // A second write operation should overwrite the old content.
360 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
361 cp = read_file_to_str(filepath, 0, NULL);
362 tt_str_op(cp,OP_EQ, str);
363 tor_free(cp);
365 done:
366 (void) unlink(filepath);
367 rmdir(options->DataDirectory);
368 tor_free(datadir);
369 tor_free(filepath);
370 tor_free(cp);
373 /* Test helper function: Make sure that a bridge line gets parsed
374 * properly. Also make sure that the resulting bridge_line_t structure
375 * has its fields set correctly. */
376 static void
377 good_bridge_line_test(const char *string, const char *test_addrport,
378 const char *test_digest, const char *test_transport,
379 const smartlist_t *test_socks_args)
381 char *tmp = NULL;
382 bridge_line_t *bridge_line = parse_bridge_line(string);
383 tt_assert(bridge_line);
385 /* test addrport */
386 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
387 tt_str_op(test_addrport,OP_EQ, tmp);
388 tor_free(tmp);
390 /* If we were asked to validate a digest, but we did not get a
391 digest after parsing, we failed. */
392 if (test_digest && tor_digest_is_zero(bridge_line->digest))
393 tt_abort();
395 /* If we were not asked to validate a digest, and we got a digest
396 after parsing, we failed again. */
397 if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
398 tt_abort();
400 /* If we were asked to validate a digest, and we got a digest after
401 parsing, make sure it's correct. */
402 if (test_digest) {
403 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
404 tor_strlower(tmp);
405 tt_str_op(test_digest,OP_EQ, tmp);
406 tor_free(tmp);
409 /* If we were asked to validate a transport name, make sure that it
410 matches with the transport name that was parsed. */
411 if (test_transport && !bridge_line->transport_name)
412 tt_abort();
413 if (!test_transport && bridge_line->transport_name)
414 tt_abort();
415 if (test_transport)
416 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
418 /* Validate the SOCKS argument smartlist. */
419 if (test_socks_args && !bridge_line->socks_args)
420 tt_abort();
421 if (!test_socks_args && bridge_line->socks_args)
422 tt_abort();
423 if (test_socks_args)
424 tt_assert(smartlist_strings_eq(test_socks_args,
425 bridge_line->socks_args));
427 done:
428 tor_free(tmp);
429 bridge_line_free(bridge_line);
432 /* Test helper function: Make sure that a bridge line is
433 * unparseable. */
434 static void
435 bad_bridge_line_test(const char *string)
437 bridge_line_t *bridge_line = parse_bridge_line(string);
438 if (bridge_line)
439 TT_FAIL(("%s was supposed to fail, but it didn't.", string));
440 tt_ptr_op(bridge_line, OP_EQ, NULL);
442 done:
443 bridge_line_free(bridge_line);
446 static void
447 test_config_parse_bridge_line(void *arg)
449 (void) arg;
450 good_bridge_line_test("192.0.2.1:4123",
451 "192.0.2.1:4123", NULL, NULL, NULL);
453 good_bridge_line_test("192.0.2.1",
454 "192.0.2.1:443", NULL, NULL, NULL);
456 good_bridge_line_test("transport [::1]",
457 "[::1]:443", NULL, "transport", NULL);
459 good_bridge_line_test("transport 192.0.2.1:12 "
460 "4352e58420e68f5e40bf7c74faddccd9d1349413",
461 "192.0.2.1:12",
462 "4352e58420e68f5e40bf7c74faddccd9d1349413",
463 "transport", NULL);
466 smartlist_t *sl_tmp = smartlist_new();
467 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
469 good_bridge_line_test("transport 192.0.2.1:12 "
470 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
471 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
472 "transport", sl_tmp);
474 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
475 smartlist_free(sl_tmp);
479 smartlist_t *sl_tmp = smartlist_new();
480 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
481 smartlist_add_asprintf(sl_tmp, "z=z");
483 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
484 "192.0.2.1:12", NULL, "transport", sl_tmp);
486 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
487 smartlist_free(sl_tmp);
491 smartlist_t *sl_tmp = smartlist_new();
492 smartlist_add_asprintf(sl_tmp, "dub=come");
493 smartlist_add_asprintf(sl_tmp, "save=me");
495 good_bridge_line_test("transport 192.0.2.1:12 "
496 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
497 "dub=come save=me",
499 "192.0.2.1:12",
500 "4352e58420e68f5e40bf7c74faddccd9d1349666",
501 "transport", sl_tmp);
503 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
504 smartlist_free(sl_tmp);
507 good_bridge_line_test("192.0.2.1:1231 "
508 "4352e58420e68f5e40bf7c74faddccd9d1349413",
509 "192.0.2.1:1231",
510 "4352e58420e68f5e40bf7c74faddccd9d1349413",
511 NULL, NULL);
513 /* Empty line */
514 bad_bridge_line_test("");
515 /* bad transport name */
516 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
517 /* weird ip address */
518 bad_bridge_line_test("a.b.c.d");
519 /* invalid fpr */
520 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
521 /* no k=v in the end */
522 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
523 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
524 /* no addrport */
525 bad_bridge_line_test("asdw");
526 /* huge k=v value that can't fit in SOCKS fields */
527 bad_bridge_line_test(
528 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
529 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
530 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
531 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
532 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
533 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
534 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
536 "aa=b");
539 static void
540 test_config_parse_transport_options_line(void *arg)
542 smartlist_t *options_sl = NULL, *sl_tmp = NULL;
544 (void) arg;
546 { /* too small line */
547 options_sl = get_options_from_transport_options_line("valley", NULL);
548 tt_ptr_op(options_sl, OP_EQ, NULL);
551 { /* no k=v values */
552 options_sl = get_options_from_transport_options_line("hit it!", NULL);
553 tt_ptr_op(options_sl, OP_EQ, NULL);
556 { /* correct line, but wrong transport specified */
557 options_sl =
558 get_options_from_transport_options_line("trebuchet k=v", "rook");
559 tt_ptr_op(options_sl, OP_EQ, NULL);
562 { /* correct -- no transport specified */
563 sl_tmp = smartlist_new();
564 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
565 smartlist_add_asprintf(sl_tmp, "weliketo=party");
567 options_sl =
568 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
569 NULL);
570 tt_assert(options_sl);
571 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
573 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
574 smartlist_free(sl_tmp);
575 sl_tmp = NULL;
576 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
577 smartlist_free(options_sl);
578 options_sl = NULL;
581 { /* correct -- correct transport specified */
582 sl_tmp = smartlist_new();
583 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
584 smartlist_add_asprintf(sl_tmp, "weliketo=party");
586 options_sl =
587 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
588 "rook");
589 tt_assert(options_sl);
590 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
591 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
592 smartlist_free(sl_tmp);
593 sl_tmp = NULL;
594 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
595 smartlist_free(options_sl);
596 options_sl = NULL;
599 done:
600 if (options_sl) {
601 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
602 smartlist_free(options_sl);
604 if (sl_tmp) {
605 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
606 smartlist_free(sl_tmp);
610 /* Mocks needed for the compute_max_mem_in_queues test */
611 static int get_total_system_memory_mock(size_t *mem_out);
613 static size_t total_system_memory_output = 0;
614 static int total_system_memory_return = 0;
616 static int
617 get_total_system_memory_mock(size_t *mem_out)
619 if (! mem_out)
620 return -1;
622 *mem_out = total_system_memory_output;
623 return total_system_memory_return;
626 /* Mocks needed for the transport plugin line test */
628 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
629 char **proxy_argv, int is_server);
630 static int transport_add_from_config_mock(const tor_addr_t *addr,
631 uint16_t port, const char *name,
632 int socks_ver);
633 static int transport_is_needed_mock(const char *transport_name);
635 static int pt_kickstart_proxy_mock_call_count = 0;
636 static int transport_add_from_config_mock_call_count = 0;
637 static int transport_is_needed_mock_call_count = 0;
638 static int transport_is_needed_mock_return = 0;
640 static void
641 pt_kickstart_proxy_mock(const smartlist_t *transport_list,
642 char **proxy_argv, int is_server)
644 (void) transport_list;
645 (void) proxy_argv;
646 (void) is_server;
647 /* XXXX check that args are as expected. */
649 ++pt_kickstart_proxy_mock_call_count;
651 free_execve_args(proxy_argv);
654 static int
655 transport_add_from_config_mock(const tor_addr_t *addr,
656 uint16_t port, const char *name,
657 int socks_ver)
659 (void) addr;
660 (void) port;
661 (void) name;
662 (void) socks_ver;
663 /* XXXX check that args are as expected. */
665 ++transport_add_from_config_mock_call_count;
667 return 0;
670 static int
671 transport_is_needed_mock(const char *transport_name)
673 (void) transport_name;
674 /* XXXX check that arg is as expected. */
676 ++transport_is_needed_mock_call_count;
678 return transport_is_needed_mock_return;
681 static void
682 test_config_parse_tcp_proxy_line(void *arg)
684 (void)arg;
686 int ret;
687 char *msg = NULL;
688 or_options_t *options = get_options_mutable();
690 /* Bad TCPProxy line - too short. */
691 ret = parse_tcp_proxy_line("haproxy", options, &msg);
692 /* Return error. */
693 tt_int_op(ret, OP_EQ, -1);
694 /* Correct error message. */
695 tt_str_op(msg, OP_EQ, "TCPProxy has no address/port. Please fix.");
696 /* Free error message. */
697 tor_free(msg);
699 /* Bad TCPProxy line - unsupported protocol. */
700 ret = parse_tcp_proxy_line("unsupported 95.216.163.36:443", options, &msg);
701 tt_int_op(ret, OP_EQ, -1);
702 tt_str_op(msg, OP_EQ, "TCPProxy protocol is not supported. Currently the "
703 "only supported protocol is 'haproxy'. Please fix.");
704 tor_free(msg);
706 /* Bad TCPProxy line - unparsable address/port. */
707 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
708 ret = parse_tcp_proxy_line("haproxy bogus_address!/300", options, &msg);
709 tt_int_op(ret, OP_EQ, -1);
710 tt_str_op(msg, OP_EQ, "TCPProxy address/port failed to parse or resolve. "
711 "Please fix.");
712 tor_free(msg);
713 UNMOCK(tor_addr_lookup);
715 /* Good TCPProxy line - ipv4. */
716 ret = parse_tcp_proxy_line("haproxy 95.216.163.36:443", options, &msg);
717 tt_int_op(ret, OP_EQ, 0);
718 tt_ptr_op(msg, OP_EQ, NULL);
719 tt_int_op(options->TCPProxyProtocol, OP_EQ, TCP_PROXY_PROTOCOL_HAPROXY);
720 /* Correct the address. */
721 tt_assert(tor_addr_eq_ipv4h(&options->TCPProxyAddr, 0x5fd8a324));
722 tt_int_op(options->TCPProxyPort, OP_EQ, 443);
723 tor_free(msg);
725 done:
726 UNMOCK(tor_addr_lookup);
730 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
731 * options.
734 static void
735 test_config_parse_transport_plugin_line(void *arg)
737 (void)arg;
739 or_options_t *options = get_options_mutable();
740 int r, tmp;
741 int old_pt_kickstart_proxy_mock_call_count;
742 int old_transport_add_from_config_mock_call_count;
743 int old_transport_is_needed_mock_call_count;
745 /* Bad transport lines - too short */
746 r = pt_parse_transport_line(options, "bad", 1, 0);
747 tt_int_op(r, OP_LT, 0);
748 r = pt_parse_transport_line(options, "bad", 1, 1);
749 tt_int_op(r, OP_LT, 0);
750 r = pt_parse_transport_line(options, "bad bad", 1, 0);
751 tt_int_op(r, OP_LT, 0);
752 r = pt_parse_transport_line(options, "bad bad", 1, 1);
753 tt_int_op(r, OP_LT, 0);
755 /* Test transport list parsing */
756 r = pt_parse_transport_line(options,
757 "transport_1 exec /usr/bin/fake-transport", 1, 0);
758 tt_int_op(r, OP_EQ, 0);
759 r = pt_parse_transport_line(options,
760 "transport_1 exec /usr/bin/fake-transport", 1, 1);
761 tt_int_op(r, OP_EQ, 0);
762 r = pt_parse_transport_line(options,
763 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
764 tt_int_op(r, OP_EQ, 0);
765 r = pt_parse_transport_line(options,
766 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
767 tt_int_op(r, OP_EQ, 0);
768 /* Bad transport identifiers */
769 r = pt_parse_transport_line(options,
770 "transport_* exec /usr/bin/fake-transport", 1, 0);
771 tt_int_op(r, OP_LT, 0);
772 r = pt_parse_transport_line(options,
773 "transport_* exec /usr/bin/fake-transport", 1, 1);
774 tt_int_op(r, OP_LT, 0);
776 /* Check SOCKS cases for client transport */
777 r = pt_parse_transport_line(options,
778 "transport_1 socks4 1.2.3.4:567", 1, 0);
779 tt_int_op(r, OP_EQ, 0);
780 r = pt_parse_transport_line(options,
781 "transport_1 socks5 1.2.3.4:567", 1, 0);
782 tt_int_op(r, OP_EQ, 0);
783 /* Proxy case for server transport */
784 r = pt_parse_transport_line(options,
785 "transport_1 proxy 1.2.3.4:567", 1, 1);
786 tt_int_op(r, OP_EQ, 0);
787 /* Multiple-transport error exit */
788 r = pt_parse_transport_line(options,
789 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
790 tt_int_op(r, OP_LT, 0);
791 r = pt_parse_transport_line(options,
792 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
793 tt_int_op(r, OP_LT, 0);
794 /* No port error exit */
795 r = pt_parse_transport_line(options,
796 "transport_1 socks5 1.2.3.4", 1, 0);
797 tt_int_op(r, OP_LT, 0);
798 r = pt_parse_transport_line(options,
799 "transport_1 proxy 1.2.3.4", 1, 1);
800 tt_int_op(r, OP_LT, 0);
801 /* Unparsable address error exit */
802 r = pt_parse_transport_line(options,
803 "transport_1 socks5 1.2.3:6x7", 1, 0);
804 tt_int_op(r, OP_LT, 0);
805 r = pt_parse_transport_line(options,
806 "transport_1 proxy 1.2.3:6x7", 1, 1);
807 tt_int_op(r, OP_LT, 0);
809 /* "Strange {Client|Server}TransportPlugin field" error exit */
810 r = pt_parse_transport_line(options,
811 "transport_1 foo bar", 1, 0);
812 tt_int_op(r, OP_LT, 0);
813 r = pt_parse_transport_line(options,
814 "transport_1 foo bar", 1, 1);
815 tt_int_op(r, OP_LT, 0);
817 /* No sandbox mode error exit */
818 tmp = options->Sandbox;
819 options->Sandbox = 1;
820 r = pt_parse_transport_line(options,
821 "transport_1 exec /usr/bin/fake-transport", 1, 0);
822 tt_int_op(r, OP_LT, 0);
823 r = pt_parse_transport_line(options,
824 "transport_1 exec /usr/bin/fake-transport", 1, 1);
825 tt_int_op(r, OP_LT, 0);
826 options->Sandbox = tmp;
829 * These final test cases cover code paths that only activate without
830 * validate_only, so they need mocks in place.
832 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
833 old_pt_kickstart_proxy_mock_call_count =
834 pt_kickstart_proxy_mock_call_count;
835 r = pt_parse_transport_line(options,
836 "transport_1 exec /usr/bin/fake-transport", 0, 1);
837 tt_int_op(r, OP_EQ, 0);
838 tt_assert(pt_kickstart_proxy_mock_call_count ==
839 old_pt_kickstart_proxy_mock_call_count + 1);
840 UNMOCK(pt_kickstart_proxy);
842 /* This one hits a log line in the !validate_only case only */
843 r = pt_parse_transport_line(options,
844 "transport_1 proxy 1.2.3.4:567", 0, 1);
845 tt_int_op(r, OP_EQ, 0);
847 /* Check mocked client transport cases */
848 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
849 MOCK(transport_add_from_config, transport_add_from_config_mock);
850 MOCK(transport_is_needed, transport_is_needed_mock);
852 /* Unnecessary transport case */
853 transport_is_needed_mock_return = 0;
854 old_pt_kickstart_proxy_mock_call_count =
855 pt_kickstart_proxy_mock_call_count;
856 old_transport_add_from_config_mock_call_count =
857 transport_add_from_config_mock_call_count;
858 old_transport_is_needed_mock_call_count =
859 transport_is_needed_mock_call_count;
860 r = pt_parse_transport_line(options,
861 "transport_1 exec /usr/bin/fake-transport", 0, 0);
862 /* Should have succeeded */
863 tt_int_op(r, OP_EQ, 0);
864 /* transport_is_needed() should have been called */
865 tt_assert(transport_is_needed_mock_call_count ==
866 old_transport_is_needed_mock_call_count + 1);
868 * pt_kickstart_proxy() and transport_add_from_config() should
869 * not have been called.
871 tt_assert(pt_kickstart_proxy_mock_call_count ==
872 old_pt_kickstart_proxy_mock_call_count);
873 tt_assert(transport_add_from_config_mock_call_count ==
874 old_transport_add_from_config_mock_call_count);
876 /* Necessary transport case */
877 transport_is_needed_mock_return = 1;
878 old_pt_kickstart_proxy_mock_call_count =
879 pt_kickstart_proxy_mock_call_count;
880 old_transport_add_from_config_mock_call_count =
881 transport_add_from_config_mock_call_count;
882 old_transport_is_needed_mock_call_count =
883 transport_is_needed_mock_call_count;
884 r = pt_parse_transport_line(options,
885 "transport_1 exec /usr/bin/fake-transport", 0, 0);
886 /* Should have succeeded */
887 tt_int_op(r, OP_EQ, 0);
889 * transport_is_needed() and pt_kickstart_proxy() should have been
890 * called.
892 tt_assert(pt_kickstart_proxy_mock_call_count ==
893 old_pt_kickstart_proxy_mock_call_count + 1);
894 tt_assert(transport_is_needed_mock_call_count ==
895 old_transport_is_needed_mock_call_count + 1);
896 /* transport_add_from_config() should not have been called. */
897 tt_assert(transport_add_from_config_mock_call_count ==
898 old_transport_add_from_config_mock_call_count);
900 /* proxy case */
901 transport_is_needed_mock_return = 1;
902 old_pt_kickstart_proxy_mock_call_count =
903 pt_kickstart_proxy_mock_call_count;
904 old_transport_add_from_config_mock_call_count =
905 transport_add_from_config_mock_call_count;
906 old_transport_is_needed_mock_call_count =
907 transport_is_needed_mock_call_count;
908 r = pt_parse_transport_line(options,
909 "transport_1 socks5 1.2.3.4:567", 0, 0);
910 /* Should have succeeded */
911 tt_int_op(r, OP_EQ, 0);
913 * transport_is_needed() and transport_add_from_config() should have
914 * been called.
916 tt_assert(transport_add_from_config_mock_call_count ==
917 old_transport_add_from_config_mock_call_count + 1);
918 tt_assert(transport_is_needed_mock_call_count ==
919 old_transport_is_needed_mock_call_count + 1);
920 /* pt_kickstart_proxy() should not have been called. */
921 tt_assert(pt_kickstart_proxy_mock_call_count ==
922 old_pt_kickstart_proxy_mock_call_count);
924 /* Done with mocked client transport cases */
925 UNMOCK(transport_is_needed);
926 UNMOCK(transport_add_from_config);
927 UNMOCK(pt_kickstart_proxy);
929 done:
930 /* Make sure we undo all mocks */
931 UNMOCK(pt_kickstart_proxy);
932 UNMOCK(transport_add_from_config);
933 UNMOCK(transport_is_needed);
935 return;
938 // Tests if an options with MyFamily fingerprints missing '$' normalises
939 // them correctly and also ensure it also works with multiple fingerprints
940 static void
941 test_config_fix_my_family(void *arg)
943 char *err = NULL;
944 config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
945 family->key = tor_strdup("MyFamily");
946 family->value = tor_strdup("$1111111111111111111111111111111111111111, "
947 "1111111111111111111111111111111111111112, "
948 "$1111111111111111111111111111111111111113");
950 config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
951 family2->key = tor_strdup("MyFamily");
952 family2->value = tor_strdup("1111111111111111111111111111111111111114");
954 config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
955 family3->key = tor_strdup("MyFamily");
956 family3->value = tor_strdup("$1111111111111111111111111111111111111115");
958 family->next = family2;
959 family2->next = family3;
960 family3->next = NULL;
962 or_options_t* options = options_new();
963 (void) arg;
965 options_init(options);
966 options->MyFamily_lines = family;
968 options_validate(NULL, options, &err) ;
970 if (err != NULL) {
971 TT_FAIL(("options_validate failed: %s", err));
974 const char *valid[] = { "$1111111111111111111111111111111111111111",
975 "$1111111111111111111111111111111111111112",
976 "$1111111111111111111111111111111111111113",
977 "$1111111111111111111111111111111111111114",
978 "$1111111111111111111111111111111111111115" };
979 int ret_size = 0;
980 config_line_t *ret;
981 for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
982 tt_str_op(ret->value, OP_EQ, valid[ret_size]);
983 ret_size++;
985 tt_int_op(ret_size, OP_EQ, 5);
987 done:
988 tor_free(err);
989 or_options_free(options);
992 static int n_hostname_01010101 = 0;
993 static const char *ret_addr_lookup_01010101[2] = {
994 "1.1.1.1", "0101::0101",
997 /** This mock function is meant to replace tor_addr_lookup().
998 * It answers with 1.1.1.1 as IP address that resulted from lookup.
999 * This function increments <b>n_hostname_01010101</b> counter by one
1000 * every time it is called.
1002 static int
1003 tor_addr_lookup_01010101(const char *name, uint16_t family, tor_addr_t *addr)
1005 n_hostname_01010101++;
1007 if (family == AF_INET) {
1008 if (name && addr) {
1009 int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[0]);
1010 tt_int_op(ret, OP_EQ, family);
1012 } else if (family == AF_INET6) {
1013 if (name && addr) {
1014 int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[1]);
1015 tt_int_op(ret, OP_EQ, family);
1018 done:
1019 return 0;
1022 static int n_hostname_localhost = 0;
1024 /** This mock function is meant to replace tor_addr_lookup().
1025 * It answers with 127.0.0.1 as IP address that resulted from lookup.
1026 * This function increments <b>n_hostname_localhost</b> counter by one
1027 * every time it is called.
1029 static int
1030 tor_addr_lookup_localhost(const char *name, uint16_t family, tor_addr_t *addr)
1032 n_hostname_localhost++;
1034 if (family == AF_INET) {
1035 if (name && addr) {
1036 tor_addr_from_ipv4h(addr, 0x7f000001);
1038 } else if (family == AF_INET6) {
1039 if (name && addr) {
1040 int ret = tor_addr_parse(addr, "::1");
1041 tt_int_op(ret, OP_EQ, AF_INET6);
1044 done:
1045 return 0;
1048 static int n_hostname_failure = 0;
1050 /** This mock function is meant to replace tor_addr_lookup().
1051 * It pretends to fail by returning -1 to caller. Also, this function
1052 * increments <b>n_hostname_failure</b> every time it is called.
1054 static int
1055 tor_addr_lookup_failure(const char *name, uint16_t family, tor_addr_t *addr)
1057 (void)name;
1058 (void)family;
1059 (void)addr;
1061 n_hostname_failure++;
1063 return -1;
1066 /** Mock function for tor_addr_lookup().
1068 * Depending on the given hostname and family, resolve either to IPv4 or IPv6.
1070 * If the requested hostname family is not the same as the family provided, an
1071 * error is returned.
1073 * Possible hostnames:
1074 * - www.torproject.org.v4 for IPv4 -> 1.1.1.1
1075 * - www.torproject.org.v6 for IPv6 -> [0101::0101]
1077 static int
1078 tor_addr_lookup_mixed(const char *name, uint16_t family, tor_addr_t *addr)
1080 tt_assert(addr);
1081 tt_assert(name);
1083 if (!strcmp(name, "www.torproject.org.v4")) {
1084 if (family == AF_INET) {
1085 tor_addr_from_ipv4h(addr, 0x01010101);
1086 return 0;
1088 /* Resolving AF_INET but the asked family is different. Failure. */
1089 return -1;
1092 if (!strcmp(name, "www.torproject.org.v6")) {
1093 if (family == AF_INET6) {
1094 int ret = tor_addr_parse(addr, "0101::0101");
1095 tt_int_op(ret, OP_EQ, AF_INET6);
1096 return 0;
1098 /* Resolving AF_INET6 but the asked family is not. Failure. */
1099 return -1;
1102 done:
1103 return 0;
1106 static int n_gethostname_replacement = 0;
1108 /** This mock function is meant to replace tor_gethostname(). It
1109 * responds with string "onionrouter!" as hostname. This function
1110 * increments <b>n_gethostname_replacement</b> by one every time
1111 * it is called.
1113 static int
1114 tor_gethostname_replacement(char *name, size_t namelen)
1116 n_gethostname_replacement++;
1118 if (name && namelen) {
1119 strlcpy(name,"onionrouter!",namelen);
1122 return 0;
1125 static int n_gethostname_localhost = 0;
1127 /** This mock function is meant to replace tor_gethostname(). It
1128 * responds with string "127.0.0.1" as hostname. This function
1129 * increments <b>n_gethostname_localhost</b> by one every time
1130 * it is called.
1132 static int
1133 tor_gethostname_localhost(char *name, size_t namelen)
1135 n_gethostname_localhost++;
1137 if (name && namelen) {
1138 strlcpy(name,"127.0.0.1",namelen);
1141 return 0;
1144 static int n_gethostname_failure = 0;
1146 /** This mock function is meant to replace tor_gethostname.
1147 * It pretends to fail by returning -1. This function increments
1148 * <b>n_gethostname_failure</b> by one every time it is called.
1150 static int
1151 tor_gethostname_failure(char *name, size_t namelen)
1153 (void)name;
1154 (void)namelen;
1155 n_gethostname_failure++;
1157 return -1;
1160 static int n_get_interface_address6 = 0;
1161 static sa_family_t last_address6_family;
1162 static const char *ret_get_interface_address6_08080808[2] = {
1163 "8.8.8.8", "0808::0808",
1166 /** This mock function is meant to replace get_interface_address().
1167 * It answers with address 8.8.8.8. This function increments
1168 * <b>n_get_interface_address</b> by one every time it is called.
1170 static int
1171 get_interface_address6_08080808(int severity, sa_family_t family,
1172 tor_addr_t *addr)
1174 (void)severity;
1176 n_get_interface_address6++;
1178 if (family == AF_INET) {
1179 if (addr) {
1180 int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[0]);
1181 tt_int_op(ret, OP_EQ, AF_INET);
1183 } else if (family == AF_INET6) {
1184 if (addr) {
1185 int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[1]);
1186 tt_int_op(ret, OP_EQ, AF_INET6);
1189 done:
1190 return 0;
1193 /** This mock function is meant to replace get_interface_address6().
1194 * It answers with IP address 9.9.9.9 iff both of the following are true:
1195 * - <b>family</b> is AF_INET
1196 * - <b>addr</b> pointer is not NULL.
1197 * This function increments <b>n_get_interface_address6</b> by one every
1198 * time it is called.
1200 #if 0
1201 static int
1202 get_interface_address6_replacement(int severity, sa_family_t family,
1203 tor_addr_t *addr)
1205 (void)severity;
1207 last_address6_family = family;
1208 n_get_interface_address6++;
1210 if ((family != AF_INET) || !addr) {
1211 return -1;
1214 tor_addr_from_ipv4h(addr,0x09090909);
1216 return 0;
1218 #endif /* 0 */
1220 static int n_get_interface_address6_failure = 0;
1223 * This mock function is meant to replace get_interface_addres6().
1224 * It will pretend to fail by return -1.
1225 * <b>n_get_interface_address6_failure</b> is incremented by one
1226 * every time this function is called and <b>last_address6_family</b>
1227 * is assigned the value of <b>family</b> argument.
1229 static int
1230 get_interface_address6_failure(int severity, sa_family_t family,
1231 tor_addr_t *addr)
1233 (void)severity;
1234 (void)addr;
1235 n_get_interface_address6_failure++;
1236 last_address6_family = family;
1238 return -1;
1241 /** Helper macro: to validate the returned value from find_my_address() so we
1242 * don't copy those all the time. */
1243 #undef VALIDATE_FOUND_ADDRESS
1244 #define VALIDATE_FOUND_ADDRESS(ret, method, hostname) \
1245 do { \
1246 tt_int_op(retval, OP_EQ, ret); \
1247 tt_int_op(method, OP_EQ, method_used); \
1248 if (hostname == NULL) tt_assert(!hostname_out); \
1249 else tt_str_op(hostname_out, OP_EQ, hostname); \
1250 if (ret == true) { \
1251 tt_assert(tor_addr_eq(&resolved_addr, &test_addr)); \
1253 } while (0)
1255 /** Helper macro: Cleanup the address and variables used after a
1256 * find_my_address() call. */
1257 #undef CLEANUP_FOUND_ADDRESS
1258 #define CLEANUP_FOUND_ADDRESS \
1259 do { \
1260 config_free_lines(options->Address); \
1261 config_free_lines(options->ORPort_lines); \
1262 options->AddressDisableIPv6 = 0; \
1263 options->ORPort_set = 0; \
1264 tor_free(options->DirAuthorities); \
1265 tor_free(hostname_out); \
1266 tor_addr_make_unspec(&resolved_addr); \
1267 tor_addr_make_unspec(&test_addr); \
1268 } while (0)
1270 /** Test both IPv4 and IPv6 coexisting together in the configuration. */
1271 static void
1272 test_config_find_my_address_mixed(void *arg)
1274 or_options_t *options;
1275 tor_addr_t resolved_addr, test_addr;
1276 resolved_addr_method_t method_used;
1277 char *hostname_out = NULL;
1278 bool retval;
1280 (void)arg;
1282 options = options_new();
1284 options_init(options);
1287 * CASE 1: Only IPv6 address. Accepted.
1289 config_line_append(&options->Address, "Address",
1290 "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
1291 tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
1293 /* IPv6 address should be found and considered configured. */
1294 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
1295 &method_used, &hostname_out);
1296 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1298 CLEANUP_FOUND_ADDRESS;
1301 * Case 2: IPv4 _and_ IPv6 given. Accepted.
1303 config_line_append(&options->Address, "Address",
1304 "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
1305 config_line_append(&options->Address, "Address", "1.1.1.1");
1306 tor_addr_parse(&test_addr, "1.1.1.1");
1308 /* IPv4 address should be found and considered configured. */
1309 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
1310 &method_used, &hostname_out);
1311 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1313 /* IPv6 address should be found and considered configured. */
1314 tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
1315 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
1316 &method_used, &hostname_out);
1317 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1319 CLEANUP_FOUND_ADDRESS;
1322 * Case 3: Two hostnames, IPv4 and IPv6.
1324 config_line_append(&options->Address, "Address", "www.torproject.org.v4");
1325 config_line_append(&options->Address, "Address", "www.torproject.org.v6");
1327 /* Looks at specific hostname to learn which address family to use. */
1328 MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
1330 /* IPv4 address should be found and considered resolved. */
1331 tor_addr_parse(&test_addr, "1.1.1.1");
1332 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
1333 &method_used, &hostname_out);
1334 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
1335 "www.torproject.org.v4");
1336 tor_free(hostname_out);
1338 /* IPv6 address should be found and considered resolved. */
1339 tor_addr_parse(&test_addr, "0101::0101");
1340 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
1341 &method_used, &hostname_out);
1342 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
1343 "www.torproject.org.v6");
1345 CLEANUP_FOUND_ADDRESS;
1346 UNMOCK(tor_addr_lookup);
1349 * Case 4: IPv4 address and a hostname resolving to IPV6.
1351 config_line_append(&options->Address, "Address", "1.1.1.1");
1352 config_line_append(&options->Address, "Address", "www.torproject.org.v6");
1354 /* Looks at specific hostname to learn which address family to use. */
1355 MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
1357 /* IPv4 address should be found and configured. */
1358 tor_addr_parse(&test_addr, "1.1.1.1");
1359 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
1360 &method_used, &hostname_out);
1361 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1363 /* IPv6 address should be found and considered resolved. */
1364 tor_addr_parse(&test_addr, "0101::0101");
1365 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
1366 &method_used, &hostname_out);
1367 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
1368 "www.torproject.org.v6");
1370 CLEANUP_FOUND_ADDRESS;
1371 UNMOCK(tor_addr_lookup);
1374 * Case 5: Hostname resolving to IPv4 and an IPv6 address.
1376 config_line_append(&options->Address, "Address", "0101::0101");
1377 config_line_append(&options->Address, "Address", "www.torproject.org.v4");
1379 /* Looks at specific hostname to learn which address family to use. */
1380 MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
1382 /* IPv4 address should be found and resolved. */
1383 tor_addr_parse(&test_addr, "1.1.1.1");
1384 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
1385 &method_used, &hostname_out);
1386 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
1387 "www.torproject.org.v4");
1388 tor_free(hostname_out);
1390 /* IPv6 address should be found and considered resolved. */
1391 tor_addr_parse(&test_addr, "0101::0101");
1392 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
1393 &method_used, &hostname_out);
1394 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1395 CLEANUP_FOUND_ADDRESS;
1397 UNMOCK(tor_addr_lookup);
1399 done:
1400 config_free_lines(options->Address);
1401 or_options_free(options);
1402 tor_free(hostname_out);
1404 UNMOCK(tor_addr_lookup);
1407 /** Parameters for the find_my_address() test. We test both AF_INET and
1408 * AF_INET6 but we have one interface to do so thus we run the same exact unit
1409 * tests for both without copying them. */
1410 typedef struct find_my_address_params_t {
1411 /* Index where the mock function results are located. For instance,
1412 * tor_addr_lookup_01010101() will have its returned value depending on the
1413 * family in ret_addr_lookup_01010101[].
1415 * Values that can be found:
1416 * AF_INET : index 0.
1417 * AF_INET6: index 1.
1419 int idx;
1420 int family;
1421 const char *public_ip;
1422 const char *internal_ip;
1423 const char *orport;
1424 } find_my_address_params_t;
1426 static find_my_address_params_t addr_param_v4 = {
1427 .idx = 0,
1428 .family = AF_INET,
1429 .public_ip = "128.52.128.105",
1430 .internal_ip = "127.0.0.1",
1433 static find_my_address_params_t addr_param_v6 = {
1434 .idx = 1,
1435 .family = AF_INET6,
1436 .public_ip = "[4242::4242]",
1437 .internal_ip = "[::1]",
1440 static void
1441 test_config_find_my_address(void *arg)
1443 or_options_t *options;
1444 tor_addr_t resolved_addr, test_addr;
1445 resolved_addr_method_t method_used;
1446 char *hostname_out = NULL;
1447 bool retval;
1448 int prev_n_hostname_01010101;
1449 int prev_n_hostname_failure;
1450 int prev_n_hostname_localhost;
1451 int prev_n_gethostname_replacement;
1452 int prev_n_gethostname_failure;
1453 int prev_n_gethostname_localhost;
1454 int prev_n_get_interface_address6;
1455 int prev_n_get_interface_address6_failure;
1457 const find_my_address_params_t *p = arg;
1459 options = options_new();
1460 options_init(options);
1461 options->PublishServerDescriptor_ = V3_DIRINFO;
1464 * Case 0:
1465 * AddressDisableIPv6 is set.
1467 * Only run this if we are in the IPv6 test.
1469 if (p->family == AF_INET6) {
1470 options->AddressDisableIPv6 = 1;
1471 /* Set a valid IPv6. However, the discovery should still fail. */
1472 config_line_append(&options->Address, "Address", p->public_ip);
1473 tor_addr_parse(&test_addr, p->public_ip);
1475 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1476 &method_used, &hostname_out);
1477 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1478 CLEANUP_FOUND_ADDRESS;
1482 * Case 1:
1483 * 1. Address is a valid address.
1485 * Expected to succeed.
1487 config_line_append(&options->Address, "Address", p->public_ip);
1488 tor_addr_parse(&test_addr, p->public_ip);
1490 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1491 &method_used, &hostname_out);
1493 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1494 CLEANUP_FOUND_ADDRESS;
1497 * Case 2: Address is a resolvable address. Expected to succeed.
1499 MOCK(tor_addr_lookup, tor_addr_lookup_01010101);
1501 config_line_append(&options->Address, "Address", "www.torproject.org");
1502 tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]);
1504 prev_n_hostname_01010101 = n_hostname_01010101;
1506 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1507 &method_used, &hostname_out);
1509 tt_int_op(n_hostname_01010101, OP_EQ, ++prev_n_hostname_01010101);
1510 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, "www.torproject.org");
1511 CLEANUP_FOUND_ADDRESS;
1513 UNMOCK(tor_addr_lookup);
1516 * Case 3: Address is a local addressi (internal). Expected to fail.
1518 config_line_append(&options->Address, "Address", p->internal_ip);
1520 setup_full_capture_of_logs(LOG_NOTICE);
1522 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1523 &method_used, &hostname_out);
1525 expect_log_msg_containing("is a private IP address. Tor relays that "
1526 "use the default DirAuthorities must have "
1527 "public IP addresses.");
1528 teardown_capture_of_logs();
1530 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1531 CLEANUP_FOUND_ADDRESS;
1534 * Case 4: Address is a local address but custom authorities. Expected to
1535 * succeed.
1537 config_line_append(&options->Address, "Address", p->internal_ip);
1538 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
1539 tor_addr_parse(&test_addr, p->internal_ip);
1541 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1542 &method_used, &hostname_out);
1544 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1545 CLEANUP_FOUND_ADDRESS;
1548 * Case 5: Multiple address in Address. Expected to fail.
1550 config_line_append(&options->Address, "Address", p->public_ip);
1551 config_line_append(&options->Address, "Address", p->public_ip);
1553 setup_full_capture_of_logs(LOG_NOTICE);
1555 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1556 &method_used, &hostname_out);
1558 expect_log_msg_containing("Found 2 Address statement of address family");
1559 teardown_capture_of_logs();
1561 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1562 CLEANUP_FOUND_ADDRESS;
1565 * Case 8:
1566 * 1. Address is NULL
1567 * 2. Interface address is a valid address.
1569 * Expected to succeed.
1571 options->Address = NULL;
1572 tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]);
1574 MOCK(get_interface_address6, get_interface_address6_08080808);
1576 prev_n_get_interface_address6 = n_get_interface_address6;
1578 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1579 &method_used, &hostname_out);
1581 tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6);
1582 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL);
1583 CLEANUP_FOUND_ADDRESS;
1585 UNMOCK(get_interface_address6);
1588 * Case 9:
1589 * 1. Address is NULL
1590 * 2. Interface address fails to be found.
1591 * 3. Local hostname resolves to a valid address.
1593 * Expected to succeed.
1595 options->Address = NULL;
1596 tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]);
1598 MOCK(get_interface_address6, get_interface_address6_failure);
1599 MOCK(tor_gethostname, tor_gethostname_replacement);
1600 MOCK(tor_addr_lookup, tor_addr_lookup_01010101);
1602 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1603 prev_n_hostname_01010101 = n_hostname_01010101;
1604 prev_n_gethostname_replacement = n_gethostname_replacement;
1606 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1607 &method_used, &hostname_out);
1609 tt_int_op(n_get_interface_address6_failure, OP_EQ,
1610 ++prev_n_get_interface_address6_failure);
1611 tt_int_op(n_hostname_01010101, OP_EQ,
1612 ++prev_n_hostname_01010101);
1613 tt_int_op(n_gethostname_replacement, OP_EQ,
1614 ++prev_n_gethostname_replacement);
1615 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_GETHOSTNAME, "onionrouter!");
1616 CLEANUP_FOUND_ADDRESS;
1618 UNMOCK(get_interface_address6);
1619 UNMOCK(tor_gethostname);
1620 UNMOCK(tor_addr_lookup);
1623 * Case 10:
1624 * 1. Address is NULL
1625 * 2. Interface address fails to be found.
1626 * 3. Local hostname resolves to an internal address.
1628 * Expected to fail.
1630 options->Address = NULL;
1632 MOCK(get_interface_address6, get_interface_address6_failure);
1633 MOCK(tor_gethostname, tor_gethostname_localhost);
1634 MOCK(tor_addr_lookup, tor_addr_lookup_localhost);
1636 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1637 prev_n_hostname_localhost = n_hostname_localhost;
1638 prev_n_gethostname_localhost = n_gethostname_localhost;
1640 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1641 &method_used, &hostname_out);
1643 tt_int_op(n_get_interface_address6_failure, OP_EQ,
1644 ++prev_n_get_interface_address6_failure);
1645 tt_int_op(n_hostname_localhost, OP_EQ,
1646 ++prev_n_hostname_localhost);
1647 tt_int_op(n_gethostname_localhost, OP_EQ,
1648 ++prev_n_gethostname_localhost);
1649 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1650 CLEANUP_FOUND_ADDRESS;
1652 UNMOCK(get_interface_address6);
1653 UNMOCK(tor_gethostname);
1654 UNMOCK(tor_addr_lookup);
1657 * Case 11:
1658 * 1. Address is NULL
1659 * 2. Interface address fails to be found.
1660 * 3. Local hostname fails to be found.
1662 * Expected to fail.
1664 options->Address = NULL;
1666 MOCK(get_interface_address6, get_interface_address6_failure);
1667 MOCK(tor_gethostname, tor_gethostname_failure);
1669 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1670 prev_n_gethostname_failure = n_gethostname_failure;
1672 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1673 &method_used, &hostname_out);
1675 tt_int_op(n_get_interface_address6_failure, OP_EQ,
1676 ++prev_n_get_interface_address6_failure);
1677 tt_int_op(n_gethostname_failure, OP_EQ,
1678 ++prev_n_gethostname_failure);
1679 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1680 CLEANUP_FOUND_ADDRESS;
1682 UNMOCK(get_interface_address6);
1683 UNMOCK(tor_gethostname);
1686 * Case 12:
1687 * 1. Address is NULL
1688 * 2. Interface address fails to be found.
1689 * 3. Local hostname can't be resolved.
1691 * Expected to fail.
1693 options->Address = NULL;
1695 MOCK(get_interface_address6, get_interface_address6_failure);
1696 MOCK(tor_gethostname, tor_gethostname_replacement);
1697 MOCK(tor_addr_lookup, tor_addr_lookup_failure);
1699 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1700 prev_n_gethostname_replacement = n_gethostname_replacement;
1701 prev_n_hostname_failure = n_hostname_failure;
1703 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1704 &method_used, &hostname_out);
1706 tt_int_op(n_get_interface_address6_failure, OP_EQ,
1707 ++prev_n_get_interface_address6_failure);
1708 tt_int_op(n_gethostname_replacement, OP_EQ,
1709 ++prev_n_gethostname_replacement);
1710 tt_int_op(n_hostname_failure, OP_EQ,
1711 ++prev_n_hostname_failure);
1712 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
1713 CLEANUP_FOUND_ADDRESS;
1716 * Case 13:
1717 * 1. Address is NULL.
1718 * 2. ORPort has a valid public address.
1721 char *msg = NULL;
1722 int n, w, ret;
1723 char *orport_line = NULL;
1725 options->Address = NULL;
1726 tor_asprintf(&orport_line, "%s:9001", p->public_ip);
1727 config_line_append(&options->ORPort_lines, "ORPort", orport_line);
1728 tor_free(orport_line);
1730 if (p->family == AF_INET6) {
1731 /* XXX: Tor does _not_ allow an IPv6 only ORPort thus we need to add a
1732 * bogus IPv4 at the moment. */
1733 config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001");
1736 ret = parse_ports(options, 0, &msg, &n, &w);
1737 tt_int_op(ret, OP_EQ, 0);
1738 tor_addr_parse(&test_addr, p->public_ip);
1741 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1742 &method_used, &hostname_out);
1743 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED_ORPORT, NULL);
1744 CLEANUP_FOUND_ADDRESS;
1747 * Case 14:
1748 * 1. Address is NULL.
1749 * 2. ORPort has an internal address thus fails.
1750 * 3. Interface as a valid address.
1753 char *msg = NULL;
1754 int n, w, ret;
1755 char *orport_line = NULL;
1757 options->Address = NULL;
1758 tor_asprintf(&orport_line, "%s:9001", p->internal_ip);
1759 config_line_append(&options->ORPort_lines, "ORPort", orport_line);
1760 tor_free(orport_line);
1762 if (p->family == AF_INET6) {
1763 /* XXX: Tor does _not_ allow an IPv6 only ORPort thus we need to add a
1764 * bogus IPv4 at the moment. */
1765 config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001");
1768 ret = parse_ports(options, 0, &msg, &n, &w);
1769 tt_int_op(ret, OP_EQ, 0);
1771 tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]);
1773 MOCK(get_interface_address6, get_interface_address6_08080808);
1775 prev_n_get_interface_address6 = n_get_interface_address6;
1777 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1778 &method_used, &hostname_out);
1780 tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6);
1781 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL);
1782 CLEANUP_FOUND_ADDRESS;
1785 * Case 15: Address is a local address (internal) but we unset
1786 * PublishServerDescriptor_ so we are allowed to hold it.
1788 options->PublishServerDescriptor_ = NO_DIRINFO;
1789 if (p->family == AF_INET) {
1790 options->AssumeReachable = 1;
1792 config_line_append(&options->Address, "Address", p->internal_ip);
1794 tor_addr_parse(&test_addr, p->internal_ip);
1795 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
1796 &method_used, &hostname_out);
1797 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
1798 CLEANUP_FOUND_ADDRESS;
1800 UNMOCK(get_interface_address6);
1801 UNMOCK(tor_gethostname);
1802 UNMOCK(tor_addr_lookup);
1804 done:
1805 or_options_free(options);
1807 UNMOCK(tor_gethostname);
1808 UNMOCK(tor_addr_lookup);
1809 UNMOCK(get_interface_address6);
1812 static void
1813 test_config_adding_trusted_dir_server(void *arg)
1815 (void)arg;
1817 const char digest[DIGEST_LEN] = "";
1818 dir_server_t *ds = NULL;
1819 tor_addr_port_t ipv6;
1820 int rv = -1;
1822 clear_dir_servers();
1823 routerlist_free_all();
1825 /* create a trusted ds without an IPv6 address and port */
1826 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1827 NULL, V3_DIRINFO, 1.0);
1828 tt_assert(ds);
1829 dir_server_add(ds);
1830 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
1831 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1833 /* create a trusted ds with an IPv6 address and port */
1834 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1835 tt_int_op(rv, OP_EQ, 0);
1836 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
1837 NULL, V3_DIRINFO, 1.0);
1838 tt_assert(ds);
1839 dir_server_add(ds);
1840 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
1841 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1843 done:
1844 clear_dir_servers();
1845 routerlist_free_all();
1848 static void
1849 test_config_adding_fallback_dir_server(void *arg)
1851 (void)arg;
1853 const char digest[DIGEST_LEN] = "";
1854 dir_server_t *ds = NULL;
1855 tor_addr_t ipv4;
1856 tor_addr_port_t ipv6;
1857 int rv = -1;
1859 clear_dir_servers();
1860 routerlist_free_all();
1862 rv = tor_addr_parse(&ipv4, "127.0.0.1");
1863 tt_int_op(rv, OP_EQ, AF_INET);
1865 /* create a trusted ds without an IPv6 address and port */
1866 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
1867 tt_assert(ds);
1868 dir_server_add(ds);
1869 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1871 /* create a trusted ds with an IPv6 address and port */
1872 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1873 tt_int_op(rv, OP_EQ, 0);
1874 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
1875 tt_assert(ds);
1876 dir_server_add(ds);
1877 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1879 done:
1880 clear_dir_servers();
1881 routerlist_free_all();
1884 /* No secrets here:
1885 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1886 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1887 * with added spaces
1889 #define TEST_DIR_AUTH_LINE_START \
1890 "foobar orport=12345 " \
1891 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1892 #define TEST_DIR_AUTH_LINE_END \
1893 "1.2.3.4:54321 " \
1894 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1895 #define TEST_DIR_AUTH_IPV6_FLAG \
1896 "ipv6=[feed::beef]:9 "
1898 static void
1899 test_config_parsing_trusted_dir_server(void *arg)
1901 (void)arg;
1902 int rv = -1;
1904 /* parse a trusted dir server without an IPv6 address and port */
1905 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1906 TEST_DIR_AUTH_LINE_END,
1907 V3_DIRINFO, 1);
1908 tt_int_op(rv, OP_EQ, 0);
1910 /* parse a trusted dir server with an IPv6 address and port */
1911 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1912 TEST_DIR_AUTH_IPV6_FLAG
1913 TEST_DIR_AUTH_LINE_END,
1914 V3_DIRINFO, 1);
1915 tt_int_op(rv, OP_EQ, 0);
1917 /* Since we are only validating, there is no cleanup. */
1918 done:
1922 #undef TEST_DIR_AUTH_LINE_START
1923 #undef TEST_DIR_AUTH_LINE_END
1924 #undef TEST_DIR_AUTH_IPV6_FLAG
1926 #define TEST_DIR_AUTH_LINE_START \
1927 "foobar orport=12345 " \
1928 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1929 #define TEST_DIR_AUTH_LINE_END_BAD_IP \
1930 "0.256.3.4:54321 " \
1931 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1932 #define TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR \
1933 "torproject.org:54321 " \
1934 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1936 static void
1937 test_config_parsing_invalid_dir_address(void *arg)
1939 (void)arg;
1940 int rv;
1942 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1943 TEST_DIR_AUTH_LINE_END_BAD_IP,
1944 V3_DIRINFO, 1);
1945 tt_int_op(rv, OP_EQ, -1);
1947 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1948 TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR,
1949 V3_DIRINFO, 1);
1950 tt_int_op(rv, OP_EQ, -1);
1952 done:
1953 return;
1956 #undef TEST_DIR_AUTH_LINE_START
1957 #undef TEST_DIR_AUTH_LINE_END_BAD_IP
1958 #undef TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR
1960 /* No secrets here:
1961 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1963 #define TEST_DIR_FALLBACK_LINE \
1964 "1.2.3.4:54321 orport=12345 " \
1965 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1966 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1967 "ipv6=[2015:c0de::deed]:9"
1969 static void
1970 test_config_parsing_fallback_dir_server(void *arg)
1972 (void)arg;
1973 int rv = -1;
1975 /* parse a trusted dir server without an IPv6 address and port */
1976 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
1977 tt_int_op(rv, OP_EQ, 0);
1979 /* parse a trusted dir server with an IPv6 address and port */
1980 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1981 TEST_DIR_FALLBACK_IPV6_FLAG,
1983 tt_int_op(rv, OP_EQ, 0);
1985 /* Since we are only validating, there is no cleanup. */
1986 done:
1990 #undef TEST_DIR_FALLBACK_LINE
1991 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1993 static void
1994 test_config_adding_default_trusted_dir_servers(void *arg)
1996 (void)arg;
1998 clear_dir_servers();
1999 routerlist_free_all();
2001 /* Assume we only have one bridge authority */
2002 add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
2003 tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
2004 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
2006 /* Assume we have nine V3 authorities */
2007 add_default_trusted_dir_authorities(V3_DIRINFO);
2008 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
2009 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
2011 done:
2012 clear_dir_servers();
2013 routerlist_free_all();
2016 static int n_add_default_fallback_dir_servers_known_default = 0;
2019 * This mock function is meant to replace add_default_fallback_dir_servers().
2020 * It will parse and add one known default fallback dir server,
2021 * which has a dir_port of 99.
2022 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
2023 * one every time this function is called.
2025 static void
2026 add_default_fallback_dir_servers_known_default(void)
2028 int i;
2029 const char *fallback[] = {
2030 "127.0.0.1:60099 orport=9009 "
2031 "id=0923456789012345678901234567890123456789",
2032 NULL
2034 for (i=0; fallback[i]; i++) {
2035 if (parse_dir_fallback_line(fallback[i], 0)<0) {
2036 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
2037 fallback[i]);
2040 n_add_default_fallback_dir_servers_known_default++;
2043 /* Helper for test_config_adding_dir_servers(), which should be
2044 * refactored: clear the fields in the options which the options object
2045 * does not really own. */
2046 static void
2047 ads_clear_helper(or_options_t *options)
2049 options->DirAuthorities = NULL;
2050 options->AlternateBridgeAuthority = NULL;
2051 options->AlternateDirAuthority = NULL;
2052 options->FallbackDir = NULL;
2055 /* Test all the different combinations of adding dir servers */
2056 static void
2057 test_config_adding_dir_servers(void *arg)
2059 (void)arg;
2061 /* allocate options */
2062 or_options_t *options = options_new();
2064 /* Allocate and populate configuration lines:
2066 * Use the same format as the hard-coded directories in
2067 * add_default_trusted_dir_authorities().
2068 * Zeroing the structure has the same effect as initialising to:
2069 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
2071 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
2072 test_dir_authority->key = tor_strdup("DirAuthority");
2073 test_dir_authority->value = tor_strdup(
2074 "D0 orport=9000 "
2075 "v3ident=0023456789012345678901234567890123456789 "
2076 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
2079 config_line_t *test_alt_bridge_authority = tor_malloc_zero(
2080 sizeof(config_line_t));
2081 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
2082 test_alt_bridge_authority->value = tor_strdup(
2083 "B1 orport=9001 bridge "
2084 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
2087 config_line_t *test_alt_dir_authority = tor_malloc_zero(
2088 sizeof(config_line_t));
2089 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
2090 test_alt_dir_authority->value = tor_strdup(
2091 "A2 orport=9002 "
2092 "v3ident=0223456789012345678901234567890123456789 "
2093 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
2096 /* Use the format specified in the manual page */
2097 config_line_t *test_fallback_directory = tor_malloc_zero(
2098 sizeof(config_line_t));
2099 test_fallback_directory->key = tor_strdup("FallbackDir");
2100 test_fallback_directory->value = tor_strdup(
2101 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
2104 /* We need to know if add_default_fallback_dir_servers is called,
2105 * whatever the size of the list in fallback_dirs.inc,
2106 * so we use a version of add_default_fallback_dir_servers that adds
2107 * one known default fallback directory. */
2108 MOCK(add_default_fallback_dir_servers,
2109 add_default_fallback_dir_servers_known_default);
2111 /* There are 16 different cases, covering each combination of set/NULL for:
2112 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
2113 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
2114 * But validate_dir_servers() ensures that:
2115 * "You cannot set both DirAuthority and Alternate*Authority."
2116 * This reduces the number of cases to 10.
2118 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
2119 * So 1001 or case 9 is:
2120 * DirAuthorities set,
2121 * AlternateBridgeAuthority NULL,
2122 * AlternateDirAuthority NULL
2123 * FallbackDir set
2124 * The valid cases are cases 0-9 counting using this method, as every case
2125 * greater than or equal to 10 = 1010 is invalid.
2127 * 1. Outcome: Use Set Directory Authorities
2128 * - No Default Authorities
2129 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
2130 * if they are set
2131 * Cases expected to yield this outcome:
2132 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
2133 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
2134 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
2136 * 2. Outcome: Use Set Bridge Authority
2137 * - Use Default Non-Bridge Directory Authorities
2138 * - Use FallbackDir if it is set, otherwise use default FallbackDir
2139 * Cases expected to yield this outcome:
2140 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
2141 * AlternateBridgeAuthority is set, and
2142 * AlternateDirAuthority is NULL)
2144 * 3. Outcome: Use Set Alternate Directory Authority
2145 * - Use Default Bridge Authorities
2146 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2147 * Cases expected to yield this outcome:
2148 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2149 * are both NULL, but AlternateDirAuthority is set)
2151 * 4. Outcome: Use Set Custom Fallback Directory
2152 * - Use Default Bridge & Directory Authorities
2153 * Cases expected to yield this outcome:
2154 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
2155 * are all NULL, but FallbackDir is set)
2157 * 5. Outcome: Use All Defaults
2158 * - Use Default Bridge & Directory Authorities, and
2159 * Default Fallback Directories
2160 * Cases expected to yield this outcome:
2161 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
2162 * and FallbackDir are all NULL)
2166 * Find out how many default Bridge, Non-Bridge and Fallback Directories
2167 * are hard-coded into this build.
2168 * This code makes some assumptions about the implementation.
2169 * If they are wrong, one or more of cases 0-5 could fail.
2171 int n_default_alt_bridge_authority = 0;
2172 int n_default_alt_dir_authority = 0;
2173 int n_default_fallback_dir = 0;
2174 #define n_default_authorities ((n_default_alt_bridge_authority) \
2175 + (n_default_alt_dir_authority))
2177 /* Pre-Count Number of Authorities of Each Type
2178 * Use 0000: No Directory Authorities or Fallback Directories Set
2181 /* clear fallback dirs counter */
2182 n_add_default_fallback_dir_servers_known_default = 0;
2184 /* clear options*/
2185 ads_clear_helper(options);
2186 or_options_free(options);
2187 options = options_new();
2189 /* clear any previous dir servers:
2190 consider_adding_dir_servers() should do this anyway */
2191 clear_dir_servers();
2193 /* assign options: 0000 */
2194 options->DirAuthorities = NULL;
2195 options->AlternateBridgeAuthority = NULL;
2196 options->AlternateDirAuthority = NULL;
2197 options->FallbackDir = NULL;
2198 options->UseDefaultFallbackDirs = 1;
2200 /* parse options - ensure we always update by passing NULL old_options */
2201 consider_adding_dir_servers(options, NULL);
2203 /* check outcome */
2205 /* we must have added the default fallback dirs */
2206 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
2208 /* we have more fallbacks than just the authorities */
2209 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2212 /* fallback_dir_servers */
2213 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2215 /* Count Bridge Authorities */
2216 SMARTLIST_FOREACH(fallback_servers,
2217 dir_server_t *,
2219 /* increment the found counter if it's a bridge auth */
2220 n_default_alt_bridge_authority +=
2221 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
2222 1 : 0)
2224 /* If we have no default bridge authority, something has gone wrong */
2225 tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
2227 /* Count v3 Authorities */
2228 SMARTLIST_FOREACH(fallback_servers,
2229 dir_server_t *,
2231 /* increment found counter if it's a v3 auth */
2232 n_default_alt_dir_authority +=
2233 ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
2234 1 : 0)
2236 /* If we have no default authorities, something has gone really wrong */
2237 tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
2239 /* Calculate Fallback Directory Count */
2240 n_default_fallback_dir = (smartlist_len(fallback_servers) -
2241 n_default_alt_bridge_authority -
2242 n_default_alt_dir_authority);
2243 /* If we have a negative count, something has gone really wrong,
2244 * or some authorities aren't being added as fallback directories.
2245 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
2246 * authorities being fallback directories.) */
2247 tt_int_op(n_default_fallback_dir, OP_GE, 0);
2252 * 1. Outcome: Use Set Directory Authorities
2253 * - No Default Authorities
2254 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
2255 * if they are set
2256 * Cases expected to yield this outcome:
2257 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
2258 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
2259 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
2262 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
2263 AlternateDirAuthority Not Set, FallbackDir Set */
2265 /* clear fallback dirs counter */
2266 n_add_default_fallback_dir_servers_known_default = 0;
2268 /* clear options*/
2269 ads_clear_helper(options);
2270 or_options_free(options);
2271 options = options_new();
2273 /* clear any previous dir servers:
2274 consider_adding_dir_servers() should do this anyway */
2275 clear_dir_servers();
2277 /* assign options: 1001 */
2278 options->DirAuthorities = test_dir_authority;
2279 options->AlternateBridgeAuthority = NULL;
2280 options->AlternateDirAuthority = NULL;
2281 options->FallbackDir = test_fallback_directory;
2282 options->UseDefaultFallbackDirs = 1;
2284 /* parse options - ensure we always update by passing NULL old_options */
2285 consider_adding_dir_servers(options, NULL);
2287 /* check outcome */
2289 /* we must not have added the default fallback dirs */
2290 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2292 /* we have more fallbacks than just the authorities */
2293 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2296 /* trusted_dir_servers */
2297 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2298 /* D0, (No B1), (No A2) */
2299 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
2301 /* DirAuthority - D0 - dir_port: 60090 */
2302 int found_D0 = 0;
2303 SMARTLIST_FOREACH(dir_servers,
2304 dir_server_t *,
2306 /* increment the found counter if dir_port matches */
2307 found_D0 +=
2308 (ds->ipv4_dirport == 60090 ?
2309 1 : 0)
2311 tt_int_op(found_D0, OP_EQ, 1);
2313 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2314 int found_B1 = 0;
2315 SMARTLIST_FOREACH(dir_servers,
2316 dir_server_t *,
2318 /* increment the found counter if dir_port matches */
2319 found_B1 +=
2320 (ds->ipv4_dirport == 60091 ?
2321 1 : 0)
2323 tt_int_op(found_B1, OP_EQ, 0);
2325 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2326 int found_A2 = 0;
2327 SMARTLIST_FOREACH(dir_servers,
2328 dir_server_t *,
2330 /* increment the found counter if dir_port matches */
2331 found_A2 +=
2332 (ds->ipv4_dirport == 60092 ?
2333 1 : 0)
2335 tt_int_op(found_A2, OP_EQ, 0);
2339 /* fallback_dir_servers */
2340 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2341 /* D0, (No B1), (No A2), Custom Fallback */
2342 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2344 /* DirAuthority - D0 - dir_port: 60090 */
2345 int found_D0 = 0;
2346 SMARTLIST_FOREACH(fallback_servers,
2347 dir_server_t *,
2349 /* increment the found counter if dir_port matches */
2350 found_D0 +=
2351 (ds->ipv4_dirport == 60090 ?
2352 1 : 0)
2354 tt_int_op(found_D0, OP_EQ, 1);
2356 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2357 int found_B1 = 0;
2358 SMARTLIST_FOREACH(fallback_servers,
2359 dir_server_t *,
2361 /* increment the found counter if dir_port matches */
2362 found_B1 +=
2363 (ds->ipv4_dirport == 60091 ?
2364 1 : 0)
2366 tt_int_op(found_B1, OP_EQ, 0);
2368 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2369 int found_A2 = 0;
2370 SMARTLIST_FOREACH(fallback_servers,
2371 dir_server_t *,
2373 /* increment the found counter if dir_port matches */
2374 found_A2 +=
2375 (ds->ipv4_dirport == 60092 ?
2376 1 : 0)
2378 tt_int_op(found_A2, OP_EQ, 0);
2380 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2381 int found_non_default_fallback = 0;
2382 SMARTLIST_FOREACH(fallback_servers,
2383 dir_server_t *,
2385 /* increment the found counter if dir_port matches */
2386 found_non_default_fallback +=
2387 (ds->ipv4_dirport == 60093 ?
2388 1 : 0)
2390 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2392 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2393 int found_default_fallback = 0;
2394 SMARTLIST_FOREACH(fallback_servers,
2395 dir_server_t *,
2397 /* increment the found counter if dir_port matches */
2398 found_default_fallback +=
2399 (ds->ipv4_dirport == 60099 ?
2400 1 : 0)
2402 tt_int_op(found_default_fallback, OP_EQ, 0);
2406 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2408 /* clear fallback dirs counter */
2409 n_add_default_fallback_dir_servers_known_default = 0;
2411 /* clear options*/
2412 ads_clear_helper(options);
2413 or_options_free(options);
2414 options = options_new();
2416 /* clear any previous dir servers:
2417 consider_adding_dir_servers() should do this anyway */
2418 clear_dir_servers();
2420 /* assign options: 1000 */
2421 options->DirAuthorities = test_dir_authority;
2422 options->AlternateBridgeAuthority = NULL;
2423 options->AlternateDirAuthority = NULL;
2424 options->FallbackDir = NULL;
2425 options->UseDefaultFallbackDirs = 1;
2427 /* parse options - ensure we always update by passing NULL old_options */
2428 consider_adding_dir_servers(options, NULL);
2430 /* check outcome */
2432 /* we must not have added the default fallback dirs */
2433 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2435 /* we just have the authorities */
2436 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2439 /* trusted_dir_servers */
2440 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2441 /* D0, (No B1), (No A2) */
2442 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
2444 /* DirAuthority - D0 - dir_port: 60090 */
2445 int found_D0 = 0;
2446 SMARTLIST_FOREACH(dir_servers,
2447 dir_server_t *,
2449 /* increment the found counter if dir_port matches */
2450 found_D0 +=
2451 (ds->ipv4_dirport == 60090 ?
2452 1 : 0)
2454 tt_int_op(found_D0, OP_EQ, 1);
2456 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2457 int found_B1 = 0;
2458 SMARTLIST_FOREACH(dir_servers,
2459 dir_server_t *,
2461 /* increment the found counter if dir_port matches */
2462 found_B1 +=
2463 (ds->ipv4_dirport == 60091 ?
2464 1 : 0)
2466 tt_int_op(found_B1, OP_EQ, 0);
2468 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2469 int found_A2 = 0;
2470 SMARTLIST_FOREACH(dir_servers,
2471 dir_server_t *,
2473 /* increment the found counter if dir_port matches */
2474 found_A2 +=
2475 (ds->ipv4_dirport == 60092 ?
2476 1 : 0)
2478 tt_int_op(found_A2, OP_EQ, 0);
2482 /* fallback_dir_servers */
2483 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2484 /* D0, (No B1), (No A2), (No Fallback) */
2485 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
2487 /* DirAuthority - D0 - dir_port: 60090 */
2488 int found_D0 = 0;
2489 SMARTLIST_FOREACH(fallback_servers,
2490 dir_server_t *,
2492 /* increment the found counter if dir_port matches */
2493 found_D0 +=
2494 (ds->ipv4_dirport == 60090 ?
2495 1 : 0)
2497 tt_int_op(found_D0, OP_EQ, 1);
2499 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2500 int found_B1 = 0;
2501 SMARTLIST_FOREACH(fallback_servers,
2502 dir_server_t *,
2504 /* increment the found counter if dir_port matches */
2505 found_B1 +=
2506 (ds->ipv4_dirport == 60091 ?
2507 1 : 0)
2509 tt_int_op(found_B1, OP_EQ, 0);
2511 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2512 int found_A2 = 0;
2513 SMARTLIST_FOREACH(fallback_servers,
2514 dir_server_t *,
2516 /* increment the found counter if dir_port matches */
2517 found_A2 +=
2518 (ds->ipv4_dirport == 60092 ?
2519 1 : 0)
2521 tt_int_op(found_A2, OP_EQ, 0);
2523 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2524 int found_non_default_fallback = 0;
2525 SMARTLIST_FOREACH(fallback_servers,
2526 dir_server_t *,
2528 /* increment the found counter if dir_port matches */
2529 found_non_default_fallback +=
2530 (ds->ipv4_dirport == 60093 ?
2531 1 : 0)
2533 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2535 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2536 int found_default_fallback = 0;
2537 SMARTLIST_FOREACH(fallback_servers,
2538 dir_server_t *,
2540 /* increment the found counter if dir_port matches */
2541 found_default_fallback +=
2542 (ds->ipv4_dirport == 60099 ?
2543 1 : 0)
2545 tt_int_op(found_default_fallback, OP_EQ, 0);
2549 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2551 /* clear fallback dirs counter */
2552 n_add_default_fallback_dir_servers_known_default = 0;
2554 /* clear options*/
2555 ads_clear_helper(options);
2556 or_options_free(options);
2557 options = options_new();
2559 /* clear any previous dir servers:
2560 consider_adding_dir_servers() should do this anyway */
2561 clear_dir_servers();
2563 /* assign options: 0111 */
2564 options->DirAuthorities = NULL;
2565 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2566 options->AlternateDirAuthority = test_alt_dir_authority;
2567 options->FallbackDir = test_fallback_directory;
2568 options->UseDefaultFallbackDirs = 1;
2570 /* parse options - ensure we always update by passing NULL old_options */
2571 consider_adding_dir_servers(options, NULL);
2573 /* check outcome */
2575 /* we must not have added the default fallback dirs */
2576 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2578 /* we have more fallbacks than just the authorities */
2579 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2582 /* trusted_dir_servers */
2583 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2584 /* (No D0), B1, A2 */
2585 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2587 /* (No DirAuthority) - D0 - dir_port: 60090 */
2588 int found_D0 = 0;
2589 SMARTLIST_FOREACH(dir_servers,
2590 dir_server_t *,
2592 /* increment the found counter if dir_port matches */
2593 found_D0 +=
2594 (ds->ipv4_dirport == 60090 ?
2595 1 : 0)
2597 tt_int_op(found_D0, OP_EQ, 0);
2599 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2600 int found_B1 = 0;
2601 SMARTLIST_FOREACH(dir_servers,
2602 dir_server_t *,
2604 /* increment the found counter if dir_port matches */
2605 found_B1 +=
2606 (ds->ipv4_dirport == 60091 ?
2607 1 : 0)
2609 tt_int_op(found_B1, OP_EQ, 1);
2611 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2612 int found_A2 = 0;
2613 SMARTLIST_FOREACH(dir_servers,
2614 dir_server_t *,
2616 /* increment the found counter if dir_port matches */
2617 found_A2 +=
2618 (ds->ipv4_dirport == 60092 ?
2619 1 : 0)
2621 tt_int_op(found_A2, OP_EQ, 1);
2625 /* fallback_dir_servers */
2626 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2627 /* (No D0), B1, A2, Custom Fallback */
2628 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
2630 /* (No DirAuthority) - D0 - dir_port: 60090 */
2631 int found_D0 = 0;
2632 SMARTLIST_FOREACH(fallback_servers,
2633 dir_server_t *,
2635 /* increment the found counter if dir_port matches */
2636 found_D0 +=
2637 (ds->ipv4_dirport == 60090 ?
2638 1 : 0)
2640 tt_int_op(found_D0, OP_EQ, 0);
2642 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2643 int found_B1 = 0;
2644 SMARTLIST_FOREACH(fallback_servers,
2645 dir_server_t *,
2647 /* increment the found counter if dir_port matches */
2648 found_B1 +=
2649 (ds->ipv4_dirport == 60091 ?
2650 1 : 0)
2652 tt_int_op(found_B1, OP_EQ, 1);
2654 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2655 int found_A2 = 0;
2656 SMARTLIST_FOREACH(fallback_servers,
2657 dir_server_t *,
2659 /* increment the found counter if dir_port matches */
2660 found_A2 +=
2661 (ds->ipv4_dirport == 60092 ?
2662 1 : 0)
2664 tt_int_op(found_A2, OP_EQ, 1);
2666 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2667 int found_non_default_fallback = 0;
2668 SMARTLIST_FOREACH(fallback_servers,
2669 dir_server_t *,
2671 /* increment the found counter if dir_port matches */
2672 found_non_default_fallback +=
2673 (ds->ipv4_dirport == 60093 ?
2674 1 : 0)
2676 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2678 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2679 int found_default_fallback = 0;
2680 SMARTLIST_FOREACH(fallback_servers,
2681 dir_server_t *,
2683 /* increment the found counter if dir_port matches */
2684 found_default_fallback +=
2685 (ds->ipv4_dirport == 60099 ?
2686 1 : 0)
2688 tt_int_op(found_default_fallback, OP_EQ, 0);
2692 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2693 AlternateDirAuthority Set, FallbackDir Not Set */
2695 /* clear fallback dirs counter */
2696 n_add_default_fallback_dir_servers_known_default = 0;
2698 /* clear options*/
2699 ads_clear_helper(options);
2700 or_options_free(options);
2701 options = options_new();
2703 /* clear any previous dir servers:
2704 consider_adding_dir_servers() should do this anyway */
2705 clear_dir_servers();
2707 /* assign options: 0110 */
2708 options->DirAuthorities = NULL;
2709 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2710 options->AlternateDirAuthority = test_alt_dir_authority;
2711 options->FallbackDir = NULL;
2712 options->UseDefaultFallbackDirs = 1;
2714 /* parse options - ensure we always update by passing NULL old_options */
2715 consider_adding_dir_servers(options, NULL);
2717 /* check outcome */
2719 /* we must not have added the default fallback dirs */
2720 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2722 /* we have more fallbacks than just the authorities */
2723 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2726 /* trusted_dir_servers */
2727 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2728 /* (No D0), B1, A2 */
2729 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2731 /* (No DirAuthority) - D0 - dir_port: 60090 */
2732 int found_D0 = 0;
2733 SMARTLIST_FOREACH(dir_servers,
2734 dir_server_t *,
2736 /* increment the found counter if dir_port matches */
2737 found_D0 +=
2738 (ds->ipv4_dirport == 60090 ?
2739 1 : 0)
2741 tt_int_op(found_D0, OP_EQ, 0);
2743 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2744 int found_B1 = 0;
2745 SMARTLIST_FOREACH(dir_servers,
2746 dir_server_t *,
2748 /* increment the found counter if dir_port matches */
2749 found_B1 +=
2750 (ds->ipv4_dirport == 60091 ?
2751 1 : 0)
2753 tt_int_op(found_B1, OP_EQ, 1);
2755 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2756 int found_A2 = 0;
2757 SMARTLIST_FOREACH(dir_servers,
2758 dir_server_t *,
2760 /* increment the found counter if dir_port matches */
2761 found_A2 +=
2762 (ds->ipv4_dirport == 60092 ?
2763 1 : 0)
2765 tt_int_op(found_A2, OP_EQ, 1);
2769 /* fallback_dir_servers */
2770 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2771 /* (No D0), B1, A2, (No Fallback) */
2772 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2774 /* (No DirAuthority) - D0 - dir_port: 60090 */
2775 int found_D0 = 0;
2776 SMARTLIST_FOREACH(fallback_servers,
2777 dir_server_t *,
2779 /* increment the found counter if dir_port matches */
2780 found_D0 +=
2781 (ds->ipv4_dirport == 60090 ?
2782 1 : 0)
2784 tt_int_op(found_D0, OP_EQ, 0);
2786 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2787 int found_B1 = 0;
2788 SMARTLIST_FOREACH(fallback_servers,
2789 dir_server_t *,
2791 /* increment the found counter if dir_port matches */
2792 found_B1 +=
2793 (ds->ipv4_dirport == 60091 ?
2794 1 : 0)
2796 tt_int_op(found_B1, OP_EQ, 1);
2798 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2799 int found_A2 = 0;
2800 SMARTLIST_FOREACH(fallback_servers,
2801 dir_server_t *,
2803 /* increment the found counter if dir_port matches */
2804 found_A2 +=
2805 (ds->ipv4_dirport == 60092 ?
2806 1 : 0)
2808 tt_int_op(found_A2, OP_EQ, 1);
2810 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2811 int found_non_default_fallback = 0;
2812 SMARTLIST_FOREACH(fallback_servers,
2813 dir_server_t *,
2815 /* increment the found counter if dir_port matches */
2816 found_non_default_fallback +=
2817 (ds->ipv4_dirport == 60093 ?
2818 1 : 0)
2820 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2822 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2823 int found_default_fallback = 0;
2824 SMARTLIST_FOREACH(fallback_servers,
2825 dir_server_t *,
2827 /* increment the found counter if dir_port matches */
2828 found_default_fallback +=
2829 (ds->ipv4_dirport == 60099 ?
2830 1 : 0)
2832 tt_int_op(found_default_fallback, OP_EQ, 0);
2837 2. Outcome: Use Set Bridge Authority
2838 - Use Default Non-Bridge Directory Authorities
2839 - Use FallbackDir if it is set, otherwise use default FallbackDir
2840 Cases expected to yield this outcome:
2841 4 & 5 (the 2 cases where DirAuthorities is NULL,
2842 AlternateBridgeAuthority is set, and
2843 AlternateDirAuthority is NULL)
2846 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2847 AlternateDirAuthority Not Set, FallbackDir Set */
2849 /* clear fallback dirs counter */
2850 n_add_default_fallback_dir_servers_known_default = 0;
2852 /* clear options*/
2853 ads_clear_helper(options);
2854 or_options_free(options);
2855 options = options_new();
2857 /* clear any previous dir servers:
2858 consider_adding_dir_servers() should do this anyway */
2859 clear_dir_servers();
2861 /* assign options: 0101 */
2862 options->DirAuthorities = NULL;
2863 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2864 options->AlternateDirAuthority = NULL;
2865 options->FallbackDir = test_fallback_directory;
2866 options->UseDefaultFallbackDirs = 1;
2868 /* parse options - ensure we always update by passing NULL old_options */
2869 consider_adding_dir_servers(options, NULL);
2871 /* check outcome */
2873 /* we must not have added the default fallback dirs */
2874 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2876 /* we have more fallbacks than just the authorities */
2877 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2880 /* trusted_dir_servers */
2881 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2882 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2883 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2885 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2886 int found_D0 = 0;
2887 SMARTLIST_FOREACH(dir_servers,
2888 dir_server_t *,
2890 /* increment the found counter if dir_port matches */
2891 found_D0 +=
2892 (ds->ipv4_dirport == 60090 ?
2893 1 : 0)
2895 tt_int_op(found_D0, OP_EQ, 0);
2897 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2898 int found_B1 = 0;
2899 SMARTLIST_FOREACH(dir_servers,
2900 dir_server_t *,
2902 /* increment the found counter if dir_port matches */
2903 found_B1 +=
2904 (ds->ipv4_dirport == 60091 ?
2905 1 : 0)
2907 tt_int_op(found_B1, OP_EQ, 1);
2909 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2910 int found_A2 = 0;
2911 SMARTLIST_FOREACH(dir_servers,
2912 dir_server_t *,
2914 /* increment the found counter if dir_port matches */
2915 found_A2 +=
2916 (ds->ipv4_dirport == 60092 ?
2917 1 : 0)
2919 tt_int_op(found_A2, OP_EQ, 0);
2921 /* There's no easy way of checking that we have included all the
2922 * default v3 non-Bridge directory authorities, so let's assume that
2923 * if the total count above is correct, we have the right ones.
2928 /* fallback_dir_servers */
2929 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2930 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2931 * Custom Fallback */
2932 tt_assert(smartlist_len(fallback_servers) ==
2933 2 + n_default_alt_dir_authority);
2935 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2936 int found_D0 = 0;
2937 SMARTLIST_FOREACH(fallback_servers,
2938 dir_server_t *,
2940 /* increment the found counter if dir_port matches */
2941 found_D0 +=
2942 (ds->ipv4_dirport == 60090 ?
2943 1 : 0)
2945 tt_int_op(found_D0, OP_EQ, 0);
2947 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2948 int found_B1 = 0;
2949 SMARTLIST_FOREACH(fallback_servers,
2950 dir_server_t *,
2952 /* increment the found counter if dir_port matches */
2953 found_B1 +=
2954 (ds->ipv4_dirport == 60091 ?
2955 1 : 0)
2957 tt_int_op(found_B1, OP_EQ, 1);
2959 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2960 int found_A2 = 0;
2961 SMARTLIST_FOREACH(fallback_servers,
2962 dir_server_t *,
2964 /* increment the found counter if dir_port matches */
2965 found_A2 +=
2966 (ds->ipv4_dirport == 60092 ?
2967 1 : 0)
2969 tt_int_op(found_A2, OP_EQ, 0);
2971 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2972 int found_non_default_fallback = 0;
2973 SMARTLIST_FOREACH(fallback_servers,
2974 dir_server_t *,
2976 /* increment the found counter if dir_port matches */
2977 found_non_default_fallback +=
2978 (ds->ipv4_dirport == 60093 ?
2979 1 : 0)
2981 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2983 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2984 int found_default_fallback = 0;
2985 SMARTLIST_FOREACH(fallback_servers,
2986 dir_server_t *,
2988 /* increment the found counter if dir_port matches */
2989 found_default_fallback +=
2990 (ds->ipv4_dirport == 60099 ?
2991 1 : 0)
2993 tt_int_op(found_default_fallback, OP_EQ, 0);
2995 /* There's no easy way of checking that we have included all the
2996 * default v3 non-Bridge directory authorities, so let's assume that
2997 * if the total count above is correct, we have the right ones.
3002 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
3003 AlternateDirAuthority & FallbackDir Not Set */
3005 /* clear fallback dirs counter */
3006 n_add_default_fallback_dir_servers_known_default = 0;
3008 /* clear options*/
3009 ads_clear_helper(options);
3010 or_options_free(options);
3011 options = options_new();
3013 /* clear any previous dir servers:
3014 consider_adding_dir_servers() should do this anyway */
3015 clear_dir_servers();
3017 /* assign options: 0100 */
3018 options->DirAuthorities = NULL;
3019 options->AlternateBridgeAuthority = test_alt_bridge_authority;
3020 options->AlternateDirAuthority = NULL;
3021 options->FallbackDir = NULL;
3022 options->UseDefaultFallbackDirs = 1;
3024 /* parse options - ensure we always update by passing NULL old_options */
3025 consider_adding_dir_servers(options, NULL);
3027 /* check outcome */
3029 /* we must have added the default fallback dirs */
3030 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
3032 /* we have more fallbacks than just the authorities */
3033 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3036 /* trusted_dir_servers */
3037 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3038 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
3039 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
3041 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3042 int found_D0 = 0;
3043 SMARTLIST_FOREACH(dir_servers,
3044 dir_server_t *,
3046 /* increment the found counter if dir_port matches */
3047 found_D0 +=
3048 (ds->ipv4_dirport == 60090 ?
3049 1 : 0)
3051 tt_int_op(found_D0, OP_EQ, 0);
3053 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
3054 int found_B1 = 0;
3055 SMARTLIST_FOREACH(dir_servers,
3056 dir_server_t *,
3058 /* increment the found counter if dir_port matches */
3059 found_B1 +=
3060 (ds->ipv4_dirport == 60091 ?
3061 1 : 0)
3063 tt_int_op(found_B1, OP_EQ, 1);
3065 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3066 int found_A2 = 0;
3067 SMARTLIST_FOREACH(dir_servers,
3068 dir_server_t *,
3070 /* increment the found counter if dir_port matches */
3071 found_A2 +=
3072 (ds->ipv4_dirport == 60092 ?
3073 1 : 0)
3075 tt_int_op(found_A2, OP_EQ, 0);
3077 /* There's no easy way of checking that we have included all the
3078 * default v3 non-Bridge directory authorities, so let's assume that
3079 * if the total count above is correct, we have the right ones.
3084 /* fallback_dir_servers */
3085 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3086 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
3087 * Default Fallback */
3088 tt_assert(smartlist_len(fallback_servers) ==
3089 2 + n_default_alt_dir_authority);
3091 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3092 int found_D0 = 0;
3093 SMARTLIST_FOREACH(fallback_servers,
3094 dir_server_t *,
3096 /* increment the found counter if dir_port matches */
3097 found_D0 +=
3098 (ds->ipv4_dirport == 60090 ?
3099 1 : 0)
3101 tt_int_op(found_D0, OP_EQ, 0);
3103 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
3104 int found_B1 = 0;
3105 SMARTLIST_FOREACH(fallback_servers,
3106 dir_server_t *,
3108 /* increment the found counter if dir_port matches */
3109 found_B1 +=
3110 (ds->ipv4_dirport == 60091 ?
3111 1 : 0)
3113 tt_int_op(found_B1, OP_EQ, 1);
3115 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3116 int found_A2 = 0;
3117 SMARTLIST_FOREACH(fallback_servers,
3118 dir_server_t *,
3120 /* increment the found counter if dir_port matches */
3121 found_A2 +=
3122 (ds->ipv4_dirport == 60092 ?
3123 1 : 0)
3125 tt_int_op(found_A2, OP_EQ, 0);
3127 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3128 int found_non_default_fallback = 0;
3129 SMARTLIST_FOREACH(fallback_servers,
3130 dir_server_t *,
3132 /* increment the found counter if dir_port matches */
3133 found_non_default_fallback +=
3134 (ds->ipv4_dirport == 60093 ?
3135 1 : 0)
3137 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3139 /* Default FallbackDir - No Nickname - dir_port: 60099 */
3140 int found_default_fallback = 0;
3141 SMARTLIST_FOREACH(fallback_servers,
3142 dir_server_t *,
3144 /* increment the found counter if dir_port matches */
3145 found_default_fallback +=
3146 (ds->ipv4_dirport == 60099 ?
3147 1 : 0)
3149 tt_int_op(found_default_fallback, OP_EQ, 1);
3151 /* There's no easy way of checking that we have included all the
3152 * default v3 non-Bridge directory authorities, so let's assume that
3153 * if the total count above is correct, we have the right ones.
3159 3. Outcome: Use Set Alternate Directory Authority
3160 - Use Default Bridge Authorities
3161 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
3162 Cases expected to yield this outcome:
3163 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
3164 are both NULL, but AlternateDirAuthority is set)
3167 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
3168 AlternateDirAuthority & FallbackDir Set */
3170 /* clear fallback dirs counter */
3171 n_add_default_fallback_dir_servers_known_default = 0;
3173 /* clear options*/
3174 ads_clear_helper(options);
3175 or_options_free(options);
3176 options = options_new();
3178 /* clear any previous dir servers:
3179 consider_adding_dir_servers() should do this anyway */
3180 clear_dir_servers();
3182 /* assign options: 0011 */
3183 options->DirAuthorities = NULL;
3184 options->AlternateBridgeAuthority = NULL;
3185 options->AlternateDirAuthority = test_alt_dir_authority;
3186 options->FallbackDir = test_fallback_directory;
3187 options->UseDefaultFallbackDirs = 1;
3189 /* parse options - ensure we always update by passing NULL old_options */
3190 consider_adding_dir_servers(options, NULL);
3192 /* check outcome */
3194 /* we must not have added the default fallback dirs */
3195 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3197 /* we have more fallbacks than just the authorities */
3198 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3201 /* trusted_dir_servers */
3202 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3203 /* (No D0), (No B1), Default Bridge Authorities, A2 */
3204 tt_assert(smartlist_len(dir_servers) ==
3205 1 + n_default_alt_bridge_authority);
3207 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3208 int found_D0 = 0;
3209 SMARTLIST_FOREACH(dir_servers,
3210 dir_server_t *,
3212 /* increment the found counter if dir_port matches */
3213 found_D0 +=
3214 (ds->ipv4_dirport == 60090 ?
3215 1 : 0)
3217 tt_int_op(found_D0, OP_EQ, 0);
3219 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3220 int found_B1 = 0;
3221 SMARTLIST_FOREACH(dir_servers,
3222 dir_server_t *,
3224 /* increment the found counter if dir_port matches */
3225 found_B1 +=
3226 (ds->ipv4_dirport == 60091 ?
3227 1 : 0)
3229 tt_int_op(found_B1, OP_EQ, 0);
3231 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3232 int found_A2 = 0;
3233 SMARTLIST_FOREACH(dir_servers,
3234 dir_server_t *,
3236 /* increment the found counter if dir_port matches */
3237 found_A2 +=
3238 (ds->ipv4_dirport == 60092 ?
3239 1 : 0)
3241 tt_int_op(found_A2, OP_EQ, 1);
3243 /* There's no easy way of checking that we have included all the
3244 * default Bridge authorities (except for hard-coding tonga's details),
3245 * so let's assume that if the total count above is correct,
3246 * we have the right ones.
3251 /* fallback_dir_servers */
3252 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3253 /* (No D0), (No B1), Default Bridge Authorities, A2,
3254 * Custom Fallback Directory, (No Default Fallback Directories) */
3255 tt_assert(smartlist_len(fallback_servers) ==
3256 2 + n_default_alt_bridge_authority);
3258 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3259 int found_D0 = 0;
3260 SMARTLIST_FOREACH(fallback_servers,
3261 dir_server_t *,
3263 /* increment the found counter if dir_port matches */
3264 found_D0 +=
3265 (ds->ipv4_dirport == 60090 ?
3266 1 : 0)
3268 tt_int_op(found_D0, OP_EQ, 0);
3270 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3271 int found_B1 = 0;
3272 SMARTLIST_FOREACH(fallback_servers,
3273 dir_server_t *,
3275 /* increment the found counter if dir_port matches */
3276 found_B1 +=
3277 (ds->ipv4_dirport == 60091 ?
3278 1 : 0)
3280 tt_int_op(found_B1, OP_EQ, 0);
3282 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3283 int found_A2 = 0;
3284 SMARTLIST_FOREACH(fallback_servers,
3285 dir_server_t *,
3287 /* increment the found counter if dir_port matches */
3288 found_A2 +=
3289 (ds->ipv4_dirport == 60092 ?
3290 1 : 0)
3292 tt_int_op(found_A2, OP_EQ, 1);
3294 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3295 int found_non_default_fallback = 0;
3296 SMARTLIST_FOREACH(fallback_servers,
3297 dir_server_t *,
3299 /* increment the found counter if dir_port matches */
3300 found_non_default_fallback +=
3301 (ds->ipv4_dirport == 60093 ?
3302 1 : 0)
3304 tt_int_op(found_non_default_fallback, OP_EQ, 1);
3306 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3307 int found_default_fallback = 0;
3308 SMARTLIST_FOREACH(fallback_servers,
3309 dir_server_t *,
3311 /* increment the found counter if dir_port matches */
3312 found_default_fallback +=
3313 (ds->ipv4_dirport == 60099 ?
3314 1 : 0)
3316 tt_int_op(found_default_fallback, OP_EQ, 0);
3318 /* There's no easy way of checking that we have included all the
3319 * default Bridge authorities (except for hard-coding tonga's details),
3320 * so let's assume that if the total count above is correct,
3321 * we have the right ones.
3326 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
3327 AlternateDirAuthority Set, FallbackDir Not Set */
3329 /* clear fallback dirs counter */
3330 n_add_default_fallback_dir_servers_known_default = 0;
3332 /* clear options*/
3333 ads_clear_helper(options);
3334 or_options_free(options);
3335 options = options_new();
3337 /* clear any previous dir servers:
3338 consider_adding_dir_servers() should do this anyway */
3339 clear_dir_servers();
3341 /* assign options: 0010 */
3342 options->DirAuthorities = NULL;
3343 options->AlternateBridgeAuthority = NULL;
3344 options->AlternateDirAuthority = test_alt_dir_authority;
3345 options->FallbackDir = NULL;
3346 options->UseDefaultFallbackDirs = 1;
3348 /* parse options - ensure we always update by passing NULL old_options */
3349 consider_adding_dir_servers(options, NULL);
3351 /* check outcome */
3353 /* we must not have added the default fallback dirs */
3354 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3356 /* we just have the authorities */
3357 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
3360 /* trusted_dir_servers */
3361 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3362 /* (No D0), (No B1), Default Bridge Authorities, A2,
3363 * No Default or Custom Fallback Directories */
3364 tt_assert(smartlist_len(dir_servers) ==
3365 1 + n_default_alt_bridge_authority);
3367 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3368 int found_D0 = 0;
3369 SMARTLIST_FOREACH(dir_servers,
3370 dir_server_t *,
3372 /* increment the found counter if dir_port matches */
3373 found_D0 +=
3374 (ds->ipv4_dirport == 60090 ?
3375 1 : 0)
3377 tt_int_op(found_D0, OP_EQ, 0);
3379 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3380 int found_B1 = 0;
3381 SMARTLIST_FOREACH(dir_servers,
3382 dir_server_t *,
3384 /* increment the found counter if dir_port matches */
3385 found_B1 +=
3386 (ds->ipv4_dirport == 60091 ?
3387 1 : 0)
3389 tt_int_op(found_B1, OP_EQ, 0);
3391 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3392 int found_A2 = 0;
3393 SMARTLIST_FOREACH(dir_servers,
3394 dir_server_t *,
3396 /* increment the found counter if dir_port matches */
3397 found_A2 +=
3398 (ds->ipv4_dirport == 60092 ?
3399 1 : 0)
3401 tt_int_op(found_A2, OP_EQ, 1);
3403 /* There's no easy way of checking that we have included all the
3404 * default Bridge authorities (except for hard-coding tonga's details),
3405 * so let's assume that if the total count above is correct,
3406 * we have the right ones.
3411 /* fallback_dir_servers */
3412 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3413 /* (No D0), (No B1), Default Bridge Authorities, A2,
3414 * No Custom or Default Fallback Directories */
3415 tt_assert(smartlist_len(fallback_servers) ==
3416 1 + n_default_alt_bridge_authority);
3418 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3419 int found_D0 = 0;
3420 SMARTLIST_FOREACH(fallback_servers,
3421 dir_server_t *,
3423 /* increment the found counter if dir_port matches */
3424 found_D0 +=
3425 (ds->ipv4_dirport == 60090 ?
3426 1 : 0)
3428 tt_int_op(found_D0, OP_EQ, 0);
3430 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3431 int found_B1 = 0;
3432 SMARTLIST_FOREACH(fallback_servers,
3433 dir_server_t *,
3435 /* increment the found counter if dir_port matches */
3436 found_B1 +=
3437 (ds->ipv4_dirport == 60091 ?
3438 1 : 0)
3440 tt_int_op(found_B1, OP_EQ, 0);
3442 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3443 int found_A2 = 0;
3444 SMARTLIST_FOREACH(fallback_servers,
3445 dir_server_t *,
3447 /* increment the found counter if dir_port matches */
3448 found_A2 +=
3449 (ds->ipv4_dirport == 60092 ?
3450 1 : 0)
3452 tt_int_op(found_A2, OP_EQ, 1);
3454 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3455 int found_non_default_fallback = 0;
3456 SMARTLIST_FOREACH(fallback_servers,
3457 dir_server_t *,
3459 /* increment the found counter if dir_port matches */
3460 found_non_default_fallback +=
3461 (ds->ipv4_dirport == 60093 ?
3462 1 : 0)
3464 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3466 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3467 int found_default_fallback = 0;
3468 SMARTLIST_FOREACH(fallback_servers,
3469 dir_server_t *,
3471 /* increment the found counter if dir_port matches */
3472 found_default_fallback +=
3473 (ds->ipv4_dirport == 60099 ?
3474 1 : 0)
3476 tt_int_op(found_default_fallback, OP_EQ, 0);
3478 /* There's no easy way of checking that we have included all the
3479 * default Bridge authorities (except for hard-coding tonga's details),
3480 * so let's assume that if the total count above is correct,
3481 * we have the right ones.
3487 4. Outcome: Use Set Custom Fallback Directory
3488 - Use Default Bridge & Directory Authorities
3489 Cases expected to yield this outcome:
3490 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3491 are all NULL, but FallbackDir is set)
3494 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3495 & AlternateDirAuthority Not Set, FallbackDir Set */
3497 /* clear fallback dirs counter */
3498 n_add_default_fallback_dir_servers_known_default = 0;
3500 /* clear options*/
3501 ads_clear_helper(options);
3502 or_options_free(options);
3503 options = options_new();
3505 /* clear any previous dir servers:
3506 consider_adding_dir_servers() should do this anyway */
3507 clear_dir_servers();
3509 /* assign options: 0001 */
3510 options->DirAuthorities = NULL;
3511 options->AlternateBridgeAuthority = NULL;
3512 options->AlternateDirAuthority = NULL;
3513 options->FallbackDir = test_fallback_directory;
3514 options->UseDefaultFallbackDirs = 1;
3516 /* parse options - ensure we always update by passing NULL old_options */
3517 consider_adding_dir_servers(options, NULL);
3519 /* check outcome */
3521 /* we must not have added the default fallback dirs */
3522 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3524 /* we have more fallbacks than just the authorities */
3525 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3528 /* trusted_dir_servers */
3529 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3530 /* (No D0), (No B1), Default Bridge Authorities,
3531 * (No A2), Default v3 Directory Authorities */
3532 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3534 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3535 int found_D0 = 0;
3536 SMARTLIST_FOREACH(dir_servers,
3537 dir_server_t *,
3539 /* increment the found counter if dir_port matches */
3540 found_D0 +=
3541 (ds->ipv4_dirport == 60090 ?
3542 1 : 0)
3544 tt_int_op(found_D0, OP_EQ, 0);
3546 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3547 int found_B1 = 0;
3548 SMARTLIST_FOREACH(dir_servers,
3549 dir_server_t *,
3551 /* increment the found counter if dir_port matches */
3552 found_B1 +=
3553 (ds->ipv4_dirport == 60091 ?
3554 1 : 0)
3556 tt_int_op(found_B1, OP_EQ, 0);
3558 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3559 int found_A2 = 0;
3560 SMARTLIST_FOREACH(dir_servers,
3561 dir_server_t *,
3563 /* increment the found counter if dir_port matches */
3564 found_A2 +=
3565 (ds->ipv4_dirport == 60092 ?
3566 1 : 0)
3568 tt_int_op(found_A2, OP_EQ, 0);
3570 /* There's no easy way of checking that we have included all the
3571 * default Bridge & V3 Directory authorities, so let's assume that
3572 * if the total count above is correct, we have the right ones.
3577 /* fallback_dir_servers */
3578 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3579 /* (No D0), (No B1), Default Bridge Authorities,
3580 * (No A2), Default v3 Directory Authorities,
3581 * Custom Fallback Directory, (No Default Fallback Directories) */
3582 tt_assert(smartlist_len(fallback_servers) ==
3583 1 + n_default_authorities);
3585 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3586 int found_D0 = 0;
3587 SMARTLIST_FOREACH(fallback_servers,
3588 dir_server_t *,
3590 /* increment the found counter if dir_port matches */
3591 found_D0 +=
3592 (ds->ipv4_dirport == 60090 ?
3593 1 : 0)
3595 tt_int_op(found_D0, OP_EQ, 0);
3597 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3598 int found_B1 = 0;
3599 SMARTLIST_FOREACH(fallback_servers,
3600 dir_server_t *,
3602 /* increment the found counter if dir_port matches */
3603 found_B1 +=
3604 (ds->ipv4_dirport == 60091 ?
3605 1 : 0)
3607 tt_int_op(found_B1, OP_EQ, 0);
3609 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3610 int found_A2 = 0;
3611 SMARTLIST_FOREACH(fallback_servers,
3612 dir_server_t *,
3614 /* increment the found counter if dir_port matches */
3615 found_A2 +=
3616 (ds->ipv4_dirport == 60092 ?
3617 1 : 0)
3619 tt_int_op(found_A2, OP_EQ, 0);
3621 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3622 int found_non_default_fallback = 0;
3623 SMARTLIST_FOREACH(fallback_servers,
3624 dir_server_t *,
3626 /* increment the found counter if dir_port matches */
3627 found_non_default_fallback +=
3628 (ds->ipv4_dirport == 60093 ?
3629 1 : 0)
3631 tt_int_op(found_non_default_fallback, OP_EQ, 1);
3633 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3634 int found_default_fallback = 0;
3635 SMARTLIST_FOREACH(fallback_servers,
3636 dir_server_t *,
3638 /* increment the found counter if dir_port matches */
3639 found_default_fallback +=
3640 (ds->ipv4_dirport == 60099 ?
3641 1 : 0)
3643 tt_int_op(found_default_fallback, OP_EQ, 0);
3645 /* There's no easy way of checking that we have included all the
3646 * default Bridge & V3 Directory authorities, so let's assume that
3647 * if the total count above is correct, we have the right ones.
3653 5. Outcome: Use All Defaults
3654 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3655 Cases expected to yield this outcome:
3656 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3657 and FallbackDir are all NULL)
3660 /* Case 0: 0000 - All Not Set */
3662 /* clear fallback dirs counter */
3663 n_add_default_fallback_dir_servers_known_default = 0;
3665 /* clear options*/
3666 ads_clear_helper(options);
3667 or_options_free(options);
3668 options = options_new();
3670 /* clear any previous dir servers:
3671 consider_adding_dir_servers() should do this anyway */
3672 clear_dir_servers();
3674 /* assign options: 0001 */
3675 options->DirAuthorities = NULL;
3676 options->AlternateBridgeAuthority = NULL;
3677 options->AlternateDirAuthority = NULL;
3678 options->FallbackDir = NULL;
3679 options->UseDefaultFallbackDirs = 1;
3681 /* parse options - ensure we always update by passing NULL old_options */
3682 consider_adding_dir_servers(options, NULL);
3684 /* check outcome */
3686 /* we must have added the default fallback dirs */
3687 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
3689 /* we have more fallbacks than just the authorities */
3690 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3693 /* trusted_dir_servers */
3694 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3695 /* (No D0), (No B1), Default Bridge Authorities,
3696 * (No A2), Default v3 Directory Authorities */
3697 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3699 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3700 int found_D0 = 0;
3701 SMARTLIST_FOREACH(dir_servers,
3702 dir_server_t *,
3704 /* increment the found counter if dir_port matches */
3705 found_D0 +=
3706 (ds->ipv4_dirport == 60090 ?
3707 1 : 0)
3709 tt_int_op(found_D0, OP_EQ, 0);
3711 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3712 int found_B1 = 0;
3713 SMARTLIST_FOREACH(dir_servers,
3714 dir_server_t *,
3716 /* increment the found counter if dir_port matches */
3717 found_B1 +=
3718 (ds->ipv4_dirport == 60091 ?
3719 1 : 0)
3721 tt_int_op(found_B1, OP_EQ, 0);
3723 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3724 int found_A2 = 0;
3725 SMARTLIST_FOREACH(dir_servers,
3726 dir_server_t *,
3728 /* increment the found counter if dir_port matches */
3729 found_A2 +=
3730 (ds->ipv4_dirport == 60092 ?
3731 1 : 0)
3733 tt_int_op(found_A2, OP_EQ, 0);
3735 /* There's no easy way of checking that we have included all the
3736 * default Bridge & V3 Directory authorities, so let's assume that
3737 * if the total count above is correct, we have the right ones.
3742 /* fallback_dir_servers */
3743 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3744 /* (No D0), (No B1), Default Bridge Authorities,
3745 * (No A2), Default v3 Directory Authorities,
3746 * (No Custom Fallback Directory), Default Fallback Directories */
3747 tt_assert(smartlist_len(fallback_servers) ==
3748 n_default_authorities + n_default_fallback_dir);
3750 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3751 int found_D0 = 0;
3752 SMARTLIST_FOREACH(fallback_servers,
3753 dir_server_t *,
3755 /* increment the found counter if dir_port matches */
3756 found_D0 +=
3757 (ds->ipv4_dirport == 60090 ?
3758 1 : 0)
3760 tt_int_op(found_D0, OP_EQ, 0);
3762 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3763 int found_B1 = 0;
3764 SMARTLIST_FOREACH(fallback_servers,
3765 dir_server_t *,
3767 /* increment the found counter if dir_port matches */
3768 found_B1 +=
3769 (ds->ipv4_dirport == 60091 ?
3770 1 : 0)
3772 tt_int_op(found_B1, OP_EQ, 0);
3774 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3775 int found_A2 = 0;
3776 SMARTLIST_FOREACH(fallback_servers,
3777 dir_server_t *,
3779 /* increment the found counter if dir_port matches */
3780 found_A2 +=
3781 (ds->ipv4_dirport == 60092 ?
3782 1 : 0)
3784 tt_int_op(found_A2, OP_EQ, 0);
3786 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3787 int found_non_default_fallback = 0;
3788 SMARTLIST_FOREACH(fallback_servers,
3789 dir_server_t *,
3791 /* increment the found counter if dir_port matches */
3792 found_non_default_fallback +=
3793 (ds->ipv4_dirport == 60093 ?
3794 1 : 0)
3796 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3798 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3799 int found_default_fallback = 0;
3800 SMARTLIST_FOREACH(fallback_servers,
3801 dir_server_t *,
3803 /* increment the found counter if dir_port matches */
3804 found_default_fallback +=
3805 (ds->ipv4_dirport == 60099 ?
3806 1 : 0)
3808 tt_int_op(found_default_fallback, OP_EQ, 1);
3810 /* There's no easy way of checking that we have included all the
3811 * default Bridge & V3 Directory authorities, and the default
3812 * Fallback Directories, so let's assume that if the total count
3813 * above is correct, we have the right ones.
3818 done:
3819 clear_dir_servers();
3821 tor_free(test_dir_authority->key);
3822 tor_free(test_dir_authority->value);
3823 tor_free(test_dir_authority);
3825 tor_free(test_alt_dir_authority->key);
3826 tor_free(test_alt_dir_authority->value);
3827 tor_free(test_alt_dir_authority);
3829 tor_free(test_alt_bridge_authority->key);
3830 tor_free(test_alt_bridge_authority->value);
3831 tor_free(test_alt_bridge_authority);
3833 tor_free(test_fallback_directory->key);
3834 tor_free(test_fallback_directory->value);
3835 tor_free(test_fallback_directory);
3837 ads_clear_helper(options);
3838 or_options_free(options);
3840 UNMOCK(add_default_fallback_dir_servers);
3843 static void
3844 test_config_default_dir_servers(void *arg)
3846 or_options_t *opts = NULL;
3847 (void)arg;
3848 int trusted_count = 0;
3849 int fallback_count = 0;
3851 /* new set of options should stop fallback parsing */
3852 opts = options_new();
3853 opts->UseDefaultFallbackDirs = 0;
3854 /* set old_options to NULL to force dir update */
3855 consider_adding_dir_servers(opts, NULL);
3856 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3857 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3858 or_options_free(opts);
3859 opts = NULL;
3861 /* assume a release will never go out with less than 7 authorities */
3862 tt_int_op(trusted_count, OP_GE, 7);
3863 /* if we disable the default fallbacks, there must not be any extra */
3864 tt_assert(fallback_count == trusted_count);
3866 opts = options_new();
3867 opts->UseDefaultFallbackDirs = 1;
3868 consider_adding_dir_servers(opts, opts);
3869 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3870 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3871 or_options_free(opts);
3872 opts = NULL;
3874 /* assume a release will never go out with less than 7 authorities */
3875 tt_int_op(trusted_count, OP_GE, 7);
3876 /* XX/teor - allow for default fallbacks to be added without breaking
3877 * the unit tests. Set a minimum fallback count once the list is stable. */
3878 tt_assert(fallback_count >= trusted_count);
3880 done:
3881 or_options_free(opts);
3884 static bool mock_relay_find_addr_to_publish_result = true;
3886 static bool
3887 mock_relay_find_addr_to_publish(const or_options_t *options, int family,
3888 int flags, tor_addr_t *addr_out)
3890 (void) options;
3891 (void) family;
3892 (void) flags;
3893 (void) addr_out;
3894 return mock_relay_find_addr_to_publish_result;
3897 static int mock_router_my_exit_policy_is_reject_star_result = 0;
3899 static int
3900 mock_router_my_exit_policy_is_reject_star(void)
3902 return mock_router_my_exit_policy_is_reject_star_result;
3905 static int mock_advertised_server_mode_result = 0;
3907 static int
3908 mock_advertised_server_mode(void)
3910 return mock_advertised_server_mode_result;
3913 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
3915 static const routerinfo_t *
3916 mock_router_get_my_routerinfo(void)
3918 return mock_router_get_my_routerinfo_result;
3921 static void
3922 test_config_directory_fetch(void *arg)
3924 (void)arg;
3926 /* Test Setup */
3927 or_options_t *options = options_new();
3928 routerinfo_t routerinfo;
3929 memset(&routerinfo, 0, sizeof(routerinfo));
3930 mock_relay_find_addr_to_publish_result = false;
3931 mock_router_my_exit_policy_is_reject_star_result = 1;
3932 mock_advertised_server_mode_result = 0;
3933 mock_router_get_my_routerinfo_result = NULL;
3934 MOCK(relay_find_addr_to_publish, mock_relay_find_addr_to_publish);
3935 MOCK(router_my_exit_policy_is_reject_star,
3936 mock_router_my_exit_policy_is_reject_star);
3937 MOCK(advertised_server_mode, mock_advertised_server_mode);
3938 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
3939 or_options_free(options);
3940 options = options_new();
3942 /* Clients can use multiple directory mirrors for bootstrap */
3943 options->ClientOnly = 1;
3944 tt_assert(server_mode(options) == 0);
3945 tt_assert(public_server_mode(options) == 0);
3946 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
3947 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3948 OP_EQ, 1);
3950 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3951 or_options_free(options);
3952 options = options_new();
3953 options->UseBridges = 1;
3954 tt_assert(server_mode(options) == 0);
3955 tt_assert(public_server_mode(options) == 0);
3956 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
3957 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3958 OP_EQ, 1);
3960 /* Bridge Relays (Bridges) must act like clients, and use multiple
3961 * directory mirrors for bootstrap */
3962 or_options_free(options);
3963 options = options_new();
3964 options->BridgeRelay = 1;
3965 options->ORPort_set = 1;
3966 tt_assert(server_mode(options) == 1);
3967 tt_assert(public_server_mode(options) == 0);
3968 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
3969 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3970 OP_EQ, 1);
3972 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3973 * but can use multiple authorities for bootstrap */
3974 or_options_free(options);
3975 options = options_new();
3976 options->FetchDirInfoEarly = 1;
3977 tt_assert(server_mode(options) == 0);
3978 tt_assert(public_server_mode(options) == 0);
3979 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
3980 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3981 OP_EQ, 1);
3983 /* Exit OR servers only fetch the consensus from the authorities when they
3984 * refuse unknown exits, but never use multiple directories for bootstrap
3986 or_options_free(options);
3987 options = options_new();
3988 options->ORPort_set = 1;
3989 options->ExitRelay = 1;
3990 mock_relay_find_addr_to_publish_result = true;
3991 mock_router_my_exit_policy_is_reject_star_result = 0;
3992 mock_advertised_server_mode_result = 1;
3993 mock_router_get_my_routerinfo_result = &routerinfo;
3995 routerinfo.supports_tunnelled_dir_requests = 1;
3997 options->RefuseUnknownExits = 1;
3998 tt_assert(server_mode(options) == 1);
3999 tt_assert(public_server_mode(options) == 1);
4000 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
4001 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4002 OP_EQ, 0);
4004 options->RefuseUnknownExits = 0;
4005 mock_relay_find_addr_to_publish_result = true;
4006 tt_assert(server_mode(options) == 1);
4007 tt_assert(public_server_mode(options) == 1);
4008 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
4009 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4010 OP_EQ, 0);
4012 /* Dir servers fetch the consensus from the authorities, unless they are not
4013 * advertising themselves (hibernating) or have no routerinfo or are not
4014 * advertising their dirport, and never use multiple directories for
4015 * bootstrap. This only applies if they are also OR servers.
4016 * (We don't care much about the behaviour of non-OR directory servers.) */
4017 or_options_free(options);
4018 options = options_new();
4019 options->DirPort_set = 1;
4020 options->ORPort_set = 1;
4021 options->DirCache = 1;
4022 mock_relay_find_addr_to_publish_result = true;
4023 mock_router_my_exit_policy_is_reject_star_result = 1;
4025 mock_advertised_server_mode_result = 1;
4026 routerinfo.ipv4_dirport = 1;
4027 mock_router_get_my_routerinfo_result = &routerinfo;
4028 tt_assert(server_mode(options) == 1);
4029 tt_assert(public_server_mode(options) == 1);
4030 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
4031 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4032 OP_EQ, 0);
4034 mock_advertised_server_mode_result = 0;
4035 routerinfo.ipv4_dirport = 1;
4036 mock_router_get_my_routerinfo_result = &routerinfo;
4037 tt_assert(server_mode(options) == 1);
4038 tt_assert(public_server_mode(options) == 1);
4039 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
4040 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4041 OP_EQ, 0);
4043 mock_advertised_server_mode_result = 1;
4044 mock_router_get_my_routerinfo_result = NULL;
4045 tt_assert(server_mode(options) == 1);
4046 tt_assert(public_server_mode(options) == 1);
4047 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
4048 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4049 OP_EQ, 0);
4051 mock_advertised_server_mode_result = 1;
4052 routerinfo.ipv4_dirport = 0;
4053 routerinfo.supports_tunnelled_dir_requests = 0;
4054 mock_router_get_my_routerinfo_result = &routerinfo;
4055 tt_assert(server_mode(options) == 1);
4056 tt_assert(public_server_mode(options) == 1);
4057 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
4058 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4059 OP_EQ, 0);
4061 mock_advertised_server_mode_result = 1;
4062 routerinfo.ipv4_dirport = 1;
4063 routerinfo.supports_tunnelled_dir_requests = 1;
4064 mock_router_get_my_routerinfo_result = &routerinfo;
4065 tt_assert(server_mode(options) == 1);
4066 tt_assert(public_server_mode(options) == 1);
4067 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
4068 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
4069 OP_EQ, 0);
4071 done:
4072 or_options_free(options);
4073 UNMOCK(relay_find_addr_to_publish);
4074 UNMOCK(router_get_my_routerinfo);
4075 UNMOCK(advertised_server_mode);
4076 UNMOCK(router_my_exit_policy_is_reject_star);
4079 static void
4080 test_config_default_fallback_dirs(void *arg)
4082 const char *fallback[] = {
4083 #ifndef COCCI
4084 #include "app/config/fallback_dirs.inc"
4085 #endif
4086 NULL
4089 int n_included_fallback_dirs = 0;
4090 int n_added_fallback_dirs = 0;
4092 (void)arg;
4093 clear_dir_servers();
4095 while (fallback[n_included_fallback_dirs])
4096 n_included_fallback_dirs++;
4098 add_default_fallback_dir_servers();
4100 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
4102 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
4104 done:
4105 clear_dir_servers();
4108 static void
4109 test_config_port_cfg_line_extract_addrport(void *arg)
4111 (void)arg;
4112 int unixy = 0;
4113 const char *rest = NULL;
4114 char *a = NULL;
4116 tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
4117 tt_int_op(unixy, OP_EQ, 0);
4118 tt_str_op(a, OP_EQ, "");
4119 tt_str_op(rest, OP_EQ, "");
4120 tor_free(a);
4122 tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
4123 OP_EQ, 0);
4124 tt_int_op(unixy, OP_EQ, 0);
4125 tt_str_op(a, OP_EQ, "hello");
4126 tt_str_op(rest, OP_EQ, "");
4127 tor_free(a);
4129 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
4130 &a, &unixy, &rest), OP_EQ, 0);
4131 tt_int_op(unixy, OP_EQ, 0);
4132 tt_str_op(a, OP_EQ, "flipperwalt");
4133 tt_str_op(rest, OP_EQ, "gersplut");
4134 tor_free(a);
4136 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
4137 &a, &unixy, &rest), OP_EQ, 0);
4138 tt_int_op(unixy, OP_EQ, 0);
4139 tt_str_op(a, OP_EQ, "flipperwalt");
4140 tt_str_op(rest, OP_EQ, "gersplut");
4141 tor_free(a);
4143 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
4144 &a, &unixy, &rest), OP_EQ, 0);
4145 tt_int_op(unixy, OP_EQ, 0);
4146 tt_str_op(a, OP_EQ, "flipperwalt");
4147 tt_str_op(rest, OP_EQ, "gersplut");
4148 tor_free(a);
4150 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
4151 &a, &unixy, &rest), OP_EQ, 0);
4152 tt_int_op(unixy, OP_EQ, 1);
4153 tt_str_op(a, OP_EQ, "flipperwalt");
4154 tt_str_op(rest, OP_EQ, "gersplut");
4155 tor_free(a);
4157 tt_int_op(port_cfg_line_extract_addrport("lolol",
4158 &a, &unixy, &rest), OP_EQ, 0);
4159 tt_int_op(unixy, OP_EQ, 0);
4160 tt_str_op(a, OP_EQ, "lolol");
4161 tt_str_op(rest, OP_EQ, "");
4162 tor_free(a);
4164 tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
4165 &a, &unixy, &rest), OP_EQ, 0);
4166 tt_int_op(unixy, OP_EQ, 1);
4167 tt_str_op(a, OP_EQ, "lolol");
4168 tt_str_op(rest, OP_EQ, "");
4169 tor_free(a);
4171 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
4172 &a, &unixy, &rest), OP_EQ, 0);
4173 tt_int_op(unixy, OP_EQ, 1);
4174 tt_str_op(a, OP_EQ, "lolol");
4175 tt_str_op(rest, OP_EQ, "");
4176 tor_free(a);
4178 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
4179 &a, &unixy, &rest), OP_EQ, 0);
4180 tt_int_op(unixy, OP_EQ, 1);
4181 tt_str_op(a, OP_EQ, "lolol");
4182 tt_str_op(rest, OP_EQ, "");
4183 tor_free(a);
4185 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
4186 &a, &unixy, &rest), OP_EQ, 0);
4187 tt_int_op(unixy, OP_EQ, 0);
4188 tt_str_op(a, OP_EQ, "foobar:lolol");
4189 tt_str_op(rest, OP_EQ, "");
4190 tor_free(a);
4192 tt_int_op(port_cfg_line_extract_addrport(":lolol",
4193 &a, &unixy, &rest), OP_EQ, 0);
4194 tt_int_op(unixy, OP_EQ, 0);
4195 tt_str_op(a, OP_EQ, ":lolol");
4196 tt_str_op(rest, OP_EQ, "");
4197 tor_free(a);
4199 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
4200 &a, &unixy, &rest), OP_EQ, 0);
4201 tt_int_op(unixy, OP_EQ, 1);
4202 tt_str_op(a, OP_EQ, "lolol");
4203 tt_str_op(rest, OP_EQ, "");
4204 tor_free(a);
4206 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
4207 &a, &unixy, &rest), OP_EQ, 0);
4208 tt_int_op(unixy, OP_EQ, 1);
4209 tt_str_op(a, OP_EQ, "lolol");
4210 tt_str_op(rest, OP_EQ, "");
4211 tor_free(a);
4213 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
4214 &a, &unixy, &rest), OP_EQ, 0);
4215 tt_int_op(unixy, OP_EQ, 1);
4216 tt_str_op(a, OP_EQ, "lolol");
4217 tt_str_op(rest, OP_EQ, "foo ");
4218 tor_free(a);
4220 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
4221 &a, &unixy, &rest), OP_EQ, 0);
4222 tt_int_op(unixy, OP_EQ, 1);
4223 tt_str_op(a, OP_EQ, "lol ol");
4224 tt_str_op(rest, OP_EQ, "foo ");
4225 tor_free(a);
4227 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
4228 &a, &unixy, &rest), OP_EQ, 0);
4229 tt_int_op(unixy, OP_EQ, 1);
4230 tt_str_op(a, OP_EQ, "lol\" ol");
4231 tt_str_op(rest, OP_EQ, "foo ");
4232 tor_free(a);
4234 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
4235 &a, &unixy, &rest), OP_EQ, -1);
4236 tor_free(a);
4238 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
4239 &a, &unixy, &rest), OP_EQ, -1);
4240 tor_free(a);
4242 done:
4243 tor_free(a);
4246 static config_line_t *
4247 mock_config_line(const char *key, const char *val)
4249 config_line_t *config_line = tor_malloc(sizeof(config_line_t));
4250 memset(config_line, 0, sizeof(config_line_t));
4251 config_line->key = tor_strdup(key);
4252 config_line->value = tor_strdup(val);
4253 return config_line;
4256 static void
4257 test_config_parse_port_config__ports__no_ports_given(void *data)
4259 (void)data;
4260 int ret;
4261 smartlist_t *slout = NULL;
4262 port_cfg_t *port_cfg = NULL;
4264 slout = smartlist_new();
4266 // Test no defaultport, no defaultaddress and no out
4267 ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
4268 tt_int_op(ret, OP_EQ, 0);
4270 // Test with defaultport, no defaultaddress and no out
4271 ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
4272 tt_int_op(ret, OP_EQ, 0);
4274 // Test no defaultport, with defaultaddress and no out
4275 ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
4276 tt_int_op(ret, OP_EQ, 0);
4278 // Test with defaultport, with defaultaddress and no out
4279 ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
4280 tt_int_op(ret, OP_EQ, 0);
4282 // Test no defaultport, no defaultaddress and with out
4283 ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 0, 0);
4284 tt_int_op(ret, OP_EQ, 0);
4285 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4287 // Test with defaultport, no defaultaddress and with out
4288 ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 42, 0);
4289 tt_int_op(ret, OP_EQ, 0);
4290 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4292 // Test no defaultport, with defaultaddress and with out
4293 ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
4294 tt_int_op(ret, OP_EQ, 0);
4295 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4297 // Test with defaultport, with defaultaddress and out, adds a new port cfg
4298 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4299 smartlist_clear(slout);
4300 ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
4301 tt_int_op(ret, OP_EQ, 0);
4302 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4303 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4304 tt_int_op(port_cfg->port, OP_EQ, 42);
4305 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
4307 // Test with defaultport, with defaultaddress and out, adds a new port cfg
4308 // for a unix address
4309 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4310 smartlist_clear(slout);
4311 ret = port_parse_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
4312 42, CL_PORT_IS_UNIXSOCKET);
4313 tt_int_op(ret, OP_EQ, 0);
4314 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4315 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4316 tt_int_op(port_cfg->port, OP_EQ, 0);
4317 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4318 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
4320 done:
4321 if (slout)
4322 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4323 smartlist_free(slout);
4326 static void
4327 test_config_parse_port_config__ports__ports_given(void *data)
4329 (void)data;
4330 int ret;
4331 smartlist_t *slout = NULL;
4332 port_cfg_t *port_cfg = NULL;
4333 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4334 tor_addr_t addr;
4336 slout = smartlist_new();
4338 mock_hostname_resolver();
4340 // Test error when encounters an invalid Port specification
4341 config_port_invalid = mock_config_line("DNSPort", "");
4342 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL,
4343 0, 0);
4344 tt_int_op(ret, OP_EQ, -1);
4346 // Test error when encounters an empty unix domain specification
4347 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4348 config_port_invalid = mock_config_line("DNSPort", "unix:");
4349 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL,
4350 0, 0);
4351 tt_int_op(ret, OP_EQ, -1);
4353 // Test error when encounters a unix domain specification but the listener
4354 // doesn't support domain sockets
4355 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
4356 ret = port_parse_config(NULL, config_port_valid, "DNS",
4357 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
4358 tt_int_op(ret, OP_EQ, -1);
4360 // Test valid unix domain
4361 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4362 smartlist_clear(slout);
4363 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4364 CONN_TYPE_AP_LISTENER, NULL, 0, 0);
4365 #ifdef _WIN32
4366 tt_int_op(ret, OP_EQ, -1);
4367 #else
4368 tt_int_op(ret, OP_EQ, 0);
4369 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4370 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4371 tt_int_op(port_cfg->port, OP_EQ, 0);
4372 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4373 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
4374 /* Test entry port defaults as initialised in port_parse_config */
4375 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4376 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4377 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4378 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4379 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
4380 #endif /* defined(_WIN32) */
4382 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
4383 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4384 config_port_invalid = mock_config_line("SOCKSPort",
4385 "unix:/tmp/foo/bar NoIPv4Traffic "
4386 "NoIPv6Traffic "
4387 "NoOnionTraffic");
4388 ret = port_parse_config(NULL, config_port_invalid, "SOCKS",
4389 CONN_TYPE_AP_LISTENER, NULL, 0,
4390 CL_PORT_TAKES_HOSTNAMES);
4391 tt_int_op(ret, OP_EQ, -1);
4393 // Test failure if we have no DNS and we're a DNSPort
4394 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4395 config_port_invalid = mock_config_line("DNSPort",
4396 "127.0.0.1:80 NoDNSRequest");
4397 ret = port_parse_config(NULL, config_port_invalid, "DNS",
4398 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4399 CL_PORT_TAKES_HOSTNAMES);
4400 tt_int_op(ret, OP_EQ, -1);
4402 // If we're a DNSPort, DNS only is ok
4403 // Use a port because DNSPort doesn't support sockets
4404 config_free_lines(config_port_valid); config_port_valid = NULL;
4405 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4406 smartlist_clear(slout);
4407 config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
4408 "NoIPv6Traffic "
4409 "NoIPv4Traffic NoOnionTraffic");
4410 ret = port_parse_config(slout, config_port_valid, "DNS",
4411 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4412 CL_PORT_TAKES_HOSTNAMES);
4413 tt_int_op(ret, OP_EQ, 0);
4414 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4415 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4416 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4417 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4418 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4419 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
4421 // Test failure if we have DNS but no ipv4 and no ipv6
4422 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4423 config_port_invalid = mock_config_line("SOCKSPort",
4424 "NoIPv6Traffic "
4425 "unix:/tmp/foo/bar NoIPv4Traffic");
4426 ret = port_parse_config(NULL, config_port_invalid, "SOCKS",
4427 CONN_TYPE_AP_LISTENER, NULL, 0,
4428 CL_PORT_TAKES_HOSTNAMES);
4429 tt_int_op(ret, OP_EQ, -1);
4431 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
4432 // options)
4433 config_free_lines(config_port_valid); config_port_valid = NULL;
4434 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4435 smartlist_clear(slout);
4436 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4437 "NoIPv6Traffic "
4438 "NoDNSRequest NoIPv4Traffic");
4439 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4440 CONN_TYPE_AP_LISTENER, NULL, 0,
4441 CL_PORT_TAKES_HOSTNAMES);
4442 #ifdef _WIN32
4443 tt_int_op(ret, OP_EQ, -1);
4444 #else
4445 tt_int_op(ret, OP_EQ, 0);
4446 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4447 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4448 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4449 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4450 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4451 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4452 #endif /* defined(_WIN32) */
4454 // Test success with quoted unix: address.
4455 config_free_lines(config_port_valid); config_port_valid = NULL;
4456 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4457 smartlist_clear(slout);
4458 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
4459 "NoIPv6Traffic "
4460 "NoDNSRequest NoIPv4Traffic");
4461 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4462 CONN_TYPE_AP_LISTENER, NULL, 0,
4463 CL_PORT_TAKES_HOSTNAMES);
4464 #ifdef _WIN32
4465 tt_int_op(ret, OP_EQ, -1);
4466 #else
4467 tt_int_op(ret, OP_EQ, 0);
4468 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4469 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4470 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4471 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4472 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4473 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4474 #endif /* defined(_WIN32) */
4476 // Test failure with broken quoted unix: address.
4477 config_free_lines(config_port_valid); config_port_valid = NULL;
4478 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4479 smartlist_clear(slout);
4480 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
4481 "NoIPv6Traffic "
4482 "NoDNSRequest NoIPv4Traffic");
4483 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4484 CONN_TYPE_AP_LISTENER, NULL, 0,
4485 CL_PORT_TAKES_HOSTNAMES);
4486 tt_int_op(ret, OP_EQ, -1);
4488 // Test failure with empty quoted unix: address.
4489 config_free_lines(config_port_valid); config_port_valid = NULL;
4490 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4491 smartlist_clear(slout);
4492 config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
4493 "NoIPv6Traffic "
4494 "NoDNSRequest NoIPv4Traffic");
4495 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4496 CONN_TYPE_AP_LISTENER, NULL, 0,
4497 CL_PORT_TAKES_HOSTNAMES);
4498 tt_int_op(ret, OP_EQ, -1);
4500 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
4501 config_free_lines(config_port_valid); config_port_valid = NULL;
4502 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4503 smartlist_clear(slout);
4504 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4505 "OnionTrafficOnly");
4506 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4507 CONN_TYPE_AP_LISTENER, NULL, 0,
4508 CL_PORT_TAKES_HOSTNAMES);
4509 #ifdef _WIN32
4510 tt_int_op(ret, OP_EQ, -1);
4511 #else
4512 tt_int_op(ret, OP_EQ, 0);
4513 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4514 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4515 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4516 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4517 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4518 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4519 #endif /* defined(_WIN32) */
4521 // Test success with no ipv4 but take ipv6
4522 config_free_lines(config_port_valid); config_port_valid = NULL;
4523 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4524 smartlist_clear(slout);
4525 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4526 "NoIPv4Traffic IPv6Traffic");
4527 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4528 CONN_TYPE_AP_LISTENER, NULL, 0,
4529 CL_PORT_TAKES_HOSTNAMES);
4530 #ifdef _WIN32
4531 tt_int_op(ret, OP_EQ, -1);
4532 #else
4533 tt_int_op(ret, OP_EQ, 0);
4534 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4535 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4536 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4537 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4538 #endif /* defined(_WIN32) */
4540 // Test success with both ipv4 and ipv6
4541 config_free_lines(config_port_valid); config_port_valid = NULL;
4542 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4543 smartlist_clear(slout);
4544 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4545 "IPv4Traffic IPv6Traffic");
4546 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4547 CONN_TYPE_AP_LISTENER, NULL, 0,
4548 CL_PORT_TAKES_HOSTNAMES);
4549 #ifdef _WIN32
4550 tt_int_op(ret, OP_EQ, -1);
4551 #else
4552 tt_int_op(ret, OP_EQ, 0);
4553 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4554 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4555 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4556 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4557 #endif /* defined(_WIN32) */
4559 // Test failure if we specify world writable for an IP Port
4560 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4561 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
4562 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
4563 "127.0.0.3", 0, 0);
4564 tt_int_op(ret, OP_EQ, -1);
4566 // Test failure if we specify group writable for an IP Port
4567 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4568 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
4569 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
4570 "127.0.0.3", 0, 0);
4571 tt_int_op(ret, OP_EQ, -1);
4573 // Test failure if we specify group writable for an IP Port
4574 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4575 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4576 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
4577 "127.0.0.3", 0, 0);
4578 tt_int_op(ret, OP_EQ, -1);
4580 // Test success with only a port (this will fail without a default address)
4581 config_free_lines(config_port_valid); config_port_valid = NULL;
4582 config_port_valid = mock_config_line("DNSPort", "42");
4583 ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
4584 "127.0.0.3", 0, 0);
4585 tt_int_op(ret, OP_EQ, 0);
4587 // Test success with only a port and isolate destination port
4588 config_free_lines(config_port_valid); config_port_valid = NULL;
4589 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4590 smartlist_clear(slout);
4591 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
4592 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4593 "127.0.0.3", 0, 0);
4594 tt_int_op(ret, OP_EQ, 0);
4595 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4596 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4597 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4598 ISO_DEFAULT | ISO_DESTPORT);
4600 // Test success with a negative isolate destination port, and plural
4601 config_free_lines(config_port_valid); config_port_valid = NULL;
4602 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4603 smartlist_clear(slout);
4604 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4605 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4606 "127.0.0.3", 0, 0);
4607 tt_int_op(ret, OP_EQ, 0);
4608 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4609 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4610 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4611 ISO_DEFAULT & ~ISO_DESTPORT);
4613 // Test success with isolate destination address
4614 config_free_lines(config_port_valid); config_port_valid = NULL;
4615 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4616 smartlist_clear(slout);
4617 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
4618 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4619 "127.0.0.3", 0, 0);
4620 tt_int_op(ret, OP_EQ, 0);
4621 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4622 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4623 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4624 ISO_DEFAULT | ISO_DESTADDR);
4626 // Test success with isolate socks AUTH
4627 config_free_lines(config_port_valid); config_port_valid = NULL;
4628 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4629 smartlist_clear(slout);
4630 config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
4631 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4632 "127.0.0.3", 0, 0);
4633 tt_int_op(ret, OP_EQ, 0);
4634 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4635 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4636 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4637 ISO_DEFAULT | ISO_SOCKSAUTH);
4639 // Test success with isolate client protocol
4640 config_free_lines(config_port_valid); config_port_valid = NULL;
4641 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4642 smartlist_clear(slout);
4643 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
4644 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4645 "127.0.0.3", 0, 0);
4646 tt_int_op(ret, OP_EQ, 0);
4647 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4648 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4649 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4650 ISO_DEFAULT | ISO_CLIENTPROTO);
4652 // Test success with isolate client address
4653 config_free_lines(config_port_valid); config_port_valid = NULL;
4654 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4655 smartlist_clear(slout);
4656 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
4657 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4658 "127.0.0.3", 0, 0);
4659 tt_int_op(ret, OP_EQ, 0);
4660 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4661 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4662 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4663 ISO_DEFAULT | ISO_CLIENTADDR);
4665 // Test success with ignored unknown options
4666 config_free_lines(config_port_valid); config_port_valid = NULL;
4667 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4668 ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
4669 "127.0.0.3", 0, 0);
4670 tt_int_op(ret, OP_EQ, 0);
4672 // Test success with no isolate socks AUTH
4673 config_free_lines(config_port_valid); config_port_valid = NULL;
4674 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4675 smartlist_clear(slout);
4676 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
4677 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4678 "127.0.0.3", 0, 0);
4679 tt_int_op(ret, OP_EQ, 0);
4680 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4681 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4682 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4684 // Test success with prefer ipv6
4685 config_free_lines(config_port_valid); config_port_valid = NULL;
4686 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4687 smartlist_clear(slout);
4688 config_port_valid = mock_config_line("SOCKSPort",
4689 "42 IPv6Traffic PreferIPv6");
4690 ret = port_parse_config(slout, config_port_valid, "SOCKS",
4691 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
4692 CL_PORT_TAKES_HOSTNAMES);
4693 tt_int_op(ret, OP_EQ, 0);
4694 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4695 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4696 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
4698 // Test success with cache ipv4 DNS
4699 config_free_lines(config_port_valid); config_port_valid = NULL;
4700 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4701 smartlist_clear(slout);
4702 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
4703 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4704 "127.0.0.42", 0, 0);
4705 tt_int_op(ret, OP_EQ, 0);
4706 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4707 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4708 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4709 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4711 // Test success with cache ipv6 DNS
4712 config_free_lines(config_port_valid); config_port_valid = NULL;
4713 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4714 smartlist_clear(slout);
4715 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
4716 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4717 "127.0.0.42", 0, 0);
4718 tt_int_op(ret, OP_EQ, 0);
4719 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4720 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4721 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4722 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4724 // Test success with no cache ipv4 DNS
4725 config_free_lines(config_port_valid); config_port_valid = NULL;
4726 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4727 smartlist_clear(slout);
4728 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4729 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4730 "127.0.0.42", 0, 0);
4731 tt_int_op(ret, OP_EQ, 0);
4732 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4733 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4734 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4735 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4737 // Test success with cache DNS
4738 config_free_lines(config_port_valid); config_port_valid = NULL;
4739 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4740 smartlist_clear(slout);
4741 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
4742 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4743 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
4744 tt_int_op(ret, OP_EQ, 0);
4745 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4746 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4747 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4748 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4750 // Test success with use cached ipv4 DNS
4751 config_free_lines(config_port_valid); config_port_valid = NULL;
4752 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4753 smartlist_clear(slout);
4754 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
4755 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4756 "127.0.0.42", 0, 0);
4757 tt_int_op(ret, OP_EQ, 0);
4758 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4759 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4760 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4761 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
4763 // Test success with use cached ipv6 DNS
4764 config_free_lines(config_port_valid); config_port_valid = NULL;
4765 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4766 smartlist_clear(slout);
4767 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
4768 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4769 "127.0.0.42", 0, 0);
4770 tt_int_op(ret, OP_EQ, 0);
4771 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4772 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4773 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
4774 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4776 // Test success with use cached DNS
4777 config_free_lines(config_port_valid); config_port_valid = NULL;
4778 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4779 smartlist_clear(slout);
4780 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
4781 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4782 "127.0.0.42", 0, 0);
4783 tt_int_op(ret, OP_EQ, 0);
4784 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4785 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4786 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4787 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4789 // Test success with not preferring ipv6 automap
4790 config_free_lines(config_port_valid); config_port_valid = NULL;
4791 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4792 smartlist_clear(slout);
4793 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4794 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4795 "127.0.0.42", 0, 0);
4796 tt_int_op(ret, OP_EQ, 0);
4797 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4798 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4799 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
4801 // Test success with prefer SOCKS no auth
4802 config_free_lines(config_port_valid); config_port_valid = NULL;
4803 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4804 smartlist_clear(slout);
4805 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4806 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4807 "127.0.0.42", 0, 0);
4808 tt_int_op(ret, OP_EQ, 0);
4809 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4810 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4811 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4813 // Test failure with both a zero port and a non-zero port
4814 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4815 config_free_lines(config_port_valid); config_port_valid = NULL;
4816 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4817 smartlist_clear(slout);
4818 config_port_invalid = mock_config_line("DNSPort", "0");
4819 config_port_valid = mock_config_line("DNSPort", "42");
4820 config_port_invalid->next = config_port_valid;
4821 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
4822 "127.0.0.42", 0, 0);
4823 tt_int_op(ret, OP_EQ, -1);
4825 // Test success with warn non-local control
4826 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4827 smartlist_clear(slout);
4828 ret = port_parse_config(slout, config_port_valid, "Control",
4829 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
4830 CL_PORT_WARN_NONLOCAL);
4831 tt_int_op(ret, OP_EQ, 0);
4833 // Test success with warn non-local listener
4834 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4835 smartlist_clear(slout);
4836 ret = port_parse_config(slout, config_port_valid, "ExtOR",
4837 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
4838 CL_PORT_WARN_NONLOCAL);
4839 tt_int_op(ret, OP_EQ, 0);
4841 // Test success with warn non-local other
4842 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4843 smartlist_clear(slout);
4844 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4845 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4846 tt_int_op(ret, OP_EQ, 0);
4848 // Test success with warn non-local other without out
4849 ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
4850 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4851 tt_int_op(ret, OP_EQ, 0);
4853 // Test success with both ipv4 and ipv6 but without stream options
4854 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4855 config_port_valid = NULL;
4856 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4857 smartlist_clear(slout);
4858 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
4859 "IPv6Traffic");
4860 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4861 "127.0.0.44", 0,
4862 CL_PORT_TAKES_HOSTNAMES |
4863 CL_PORT_NO_STREAM_OPTIONS);
4864 tt_int_op(ret, OP_EQ, 0);
4865 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4866 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4867 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4868 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4870 // Test failure for a SessionGroup argument with invalid value
4871 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4872 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4873 smartlist_clear(slout);
4874 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
4875 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
4876 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4877 tt_int_op(ret, OP_EQ, -1);
4879 // Test failure for a SessionGroup argument with valid value but with no
4880 // stream options allowed
4881 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4882 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4883 smartlist_clear(slout);
4884 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
4885 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
4886 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4887 tt_int_op(ret, OP_EQ, -1);
4889 // Test failure for more than one SessionGroup argument
4890 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4891 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4892 smartlist_clear(slout);
4893 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
4894 "SessionGroup=321");
4895 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
4896 "127.0.0.44", 0, 0);
4897 tt_int_op(ret, OP_EQ, -1);
4899 // Test success with a sessiongroup options
4900 config_free_lines(config_port_valid); config_port_valid = NULL;
4901 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4902 smartlist_clear(slout);
4903 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
4904 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4905 "127.0.0.44", 0, 0);
4906 tt_int_op(ret, OP_EQ, 0);
4907 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4908 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4909 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
4911 // Test success with a zero unix domain socket, and doesn't add it to out
4912 config_free_lines(config_port_valid); config_port_valid = NULL;
4913 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4914 smartlist_clear(slout);
4915 config_port_valid = mock_config_line("DNSPort", "0");
4916 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4917 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4918 tt_int_op(ret, OP_EQ, 0);
4919 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4921 // Test success with a one unix domain socket, and doesn't add it to out
4922 config_free_lines(config_port_valid); config_port_valid = NULL;
4923 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4924 smartlist_clear(slout);
4925 config_port_valid = mock_config_line("DNSPort", "something");
4926 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4927 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4928 tt_int_op(ret, OP_EQ, 0);
4929 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4930 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4931 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4932 tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
4934 // Test success with a port of auto - it uses the default address
4935 config_free_lines(config_port_valid); config_port_valid = NULL;
4936 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4937 smartlist_clear(slout);
4938 config_port_valid = mock_config_line("DNSPort", "auto");
4939 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4940 "127.0.0.46", 0, 0);
4941 tt_int_op(ret, OP_EQ, 0);
4942 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4943 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4944 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4945 tor_addr_parse(&addr, "127.0.0.46");
4946 tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
4948 // Test success with a port of auto in mixed case
4949 config_free_lines(config_port_valid); config_port_valid = NULL;
4950 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4951 smartlist_clear(slout);
4952 config_port_valid = mock_config_line("DNSPort", "AuTo");
4953 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4954 "127.0.0.46", 0, 0);
4955 tt_int_op(ret, OP_EQ, 0);
4956 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4957 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4958 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4959 tor_addr_parse(&addr, "127.0.0.46");
4960 tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
4962 // Test success with parsing both an address and an auto port
4963 config_free_lines(config_port_valid); config_port_valid = NULL;
4964 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4965 smartlist_clear(slout);
4966 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
4967 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4968 "127.0.0.46", 0, 0);
4969 tt_int_op(ret, OP_EQ, 0);
4970 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4971 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4972 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4973 tor_addr_parse(&addr, "127.0.0.122");
4974 tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
4976 // Test failure when asked to parse an invalid address followed by auto
4977 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4978 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
4979 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
4980 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
4981 "127.0.0.46", 0, 0);
4982 UNMOCK(tor_addr_lookup);
4983 tt_int_op(ret, OP_EQ, -1);
4985 // Test success with parsing both an address and a real port
4986 config_free_lines(config_port_valid); config_port_valid = NULL;
4987 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4988 smartlist_clear(slout);
4989 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
4990 ret = port_parse_config(slout, config_port_valid, "DNS", 0,
4991 "127.0.0.46", 0, 0);
4992 tt_int_op(ret, OP_EQ, 0);
4993 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4994 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4995 tt_int_op(port_cfg->port, OP_EQ, 656);
4996 tor_addr_parse(&addr, "127.0.0.123");
4997 tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
4999 // Test failure if we can't parse anything at all
5000 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5001 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5002 smartlist_clear(slout);
5003 config_port_invalid = mock_config_line("DNSPort", "something wrong");
5004 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
5005 "127.0.0.46", 0, 0);
5006 tt_int_op(ret, OP_EQ, -1);
5008 // Test failure if we find both an address, a port and an auto
5009 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5010 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5011 smartlist_clear(slout);
5012 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
5013 ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
5014 "127.0.0.46", 0, 0);
5015 tt_int_op(ret, OP_EQ, -1);
5017 // Test that default to group writeable default sets group writeable for
5018 // domain socket
5019 config_free_lines(config_port_valid); config_port_valid = NULL;
5020 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5021 smartlist_clear(slout);
5022 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
5023 ret = port_parse_config(slout, config_port_valid, "SOCKS",
5024 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
5025 CL_PORT_DFLT_GROUP_WRITABLE);
5026 #ifdef _WIN32
5027 tt_int_op(ret, OP_EQ, -1);
5028 #else
5029 tt_int_op(ret, OP_EQ, 0);
5030 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5031 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
5032 tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
5033 #endif /* defined(_WIN32) */
5035 done:
5036 unmock_hostname_resolver();
5037 if (slout)
5038 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5039 smartlist_free(slout);
5040 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5041 config_free_lines(config_port_valid); config_port_valid = NULL;
5044 static void
5045 test_config_parse_port_config__ports__server_options(void *data)
5047 (void)data;
5048 int ret;
5049 smartlist_t *slout = NULL;
5050 port_cfg_t *port_cfg = NULL;
5051 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
5053 slout = smartlist_new();
5055 // Test success with NoAdvertise option
5056 config_free_lines(config_port_valid); config_port_valid = NULL;
5057 config_port_valid = mock_config_line("DNSPort",
5058 "127.0.0.124:656 NoAdvertise");
5059 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
5060 CL_PORT_SERVER_OPTIONS);
5061 tt_int_op(ret, OP_EQ, 0);
5062 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5063 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
5064 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
5065 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
5067 // Test success with NoListen option
5068 config_free_lines(config_port_valid); config_port_valid = NULL;
5069 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5070 smartlist_clear(slout);
5071 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
5072 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
5073 CL_PORT_SERVER_OPTIONS);
5074 tt_int_op(ret, OP_EQ, 0);
5075 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5076 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
5077 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
5078 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
5080 // Test failure with both NoAdvertise and NoListen option
5081 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5082 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5083 smartlist_clear(slout);
5084 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
5085 "NoAdvertise");
5086 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
5087 0, CL_PORT_SERVER_OPTIONS);
5088 tt_int_op(ret, OP_EQ, -1);
5090 // Test success with IPv4Only
5091 config_free_lines(config_port_valid); config_port_valid = NULL;
5092 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5093 smartlist_clear(slout);
5094 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
5095 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
5096 CL_PORT_SERVER_OPTIONS);
5097 tt_int_op(ret, OP_EQ, 0);
5098 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5099 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
5100 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
5101 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
5103 // Test success with IPv6Only
5104 config_free_lines(config_port_valid); config_port_valid = NULL;
5105 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5106 smartlist_clear(slout);
5107 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
5108 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
5109 CL_PORT_SERVER_OPTIONS);
5110 tt_int_op(ret, OP_EQ, 0);
5111 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5112 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
5113 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
5114 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
5116 // Test failure with both IPv4Only and IPv6Only
5117 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5118 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5119 smartlist_clear(slout);
5120 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
5121 "IPv4Only");
5122 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
5123 0, CL_PORT_SERVER_OPTIONS);
5124 tt_int_op(ret, OP_EQ, -1);
5126 // Test success with invalid parameter
5127 config_free_lines(config_port_valid); config_port_valid = NULL;
5128 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5129 smartlist_clear(slout);
5130 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
5131 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
5132 CL_PORT_SERVER_OPTIONS);
5133 tt_int_op(ret, OP_EQ, 0);
5134 tt_int_op(smartlist_len(slout), OP_EQ, 1);
5136 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
5137 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5138 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5139 smartlist_clear(slout);
5140 config_port_invalid = mock_config_line("DNSPort",
5141 "127.0.0.124:656 IPv6Only");
5142 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
5143 0, CL_PORT_SERVER_OPTIONS);
5144 tt_int_op(ret, OP_EQ, -1);
5146 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
5147 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5148 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5149 smartlist_clear(slout);
5150 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
5151 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
5152 0, CL_PORT_SERVER_OPTIONS);
5153 tt_int_op(ret, OP_EQ, -1);
5155 // Check for failure with empty unix: address.
5156 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5157 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5158 smartlist_clear(slout);
5159 config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
5160 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, NULL,
5161 0, CL_PORT_SERVER_OPTIONS);
5162 tt_int_op(ret, OP_EQ, -1);
5164 /* Default address is IPv4 but pass IPv6Only flag. Should be ignored. */
5165 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5166 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5167 smartlist_clear(slout);
5168 config_port_invalid = mock_config_line("ORPort", "9050 IPv6Only");
5169 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
5170 "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS);
5171 tt_int_op(ret, OP_EQ, 0);
5173 /* Default address is IPv6 but pass IPv4Only flag. Should be ignored. */
5174 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5175 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5176 smartlist_clear(slout);
5177 config_port_invalid = mock_config_line("ORPort", "9050 IPv4Only");
5178 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
5179 "[::]", 0, CL_PORT_SERVER_OPTIONS);
5180 tt_int_op(ret, OP_EQ, 0);
5182 /* Explicit address is IPv6 but pass IPv4Only flag. Should error. */
5183 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5184 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5185 smartlist_clear(slout);
5186 config_port_invalid = mock_config_line("ORPort",
5187 "[4242::4242]:9050 IPv4Only");
5188 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
5189 "[::]", 0, CL_PORT_SERVER_OPTIONS);
5190 tt_int_op(ret, OP_EQ, -1);
5192 /* Explicit address is IPv4 but pass IPv6Only flag. Should error. */
5193 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5194 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5195 smartlist_clear(slout);
5196 config_port_invalid = mock_config_line("ORPort",
5197 "1.2.3.4:9050 IPv6Only");
5198 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
5199 "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS);
5200 tt_int_op(ret, OP_EQ, -1);
5202 done:
5203 if (slout)
5204 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
5205 smartlist_free(slout);
5206 config_free_lines(config_port_invalid); config_port_invalid = NULL;
5207 config_free_lines(config_port_valid); config_port_valid = NULL;
5210 static void
5211 test_config_get_first_advertised(void *data)
5213 (void)data;
5214 int r, w=0, n=0;
5215 char *msg=NULL;
5216 or_options_t *opts = options_new();
5217 int port;
5218 const tor_addr_t *addr;
5220 // no ports are configured? We get NULL.
5221 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
5222 AF_INET);
5223 tt_int_op(port, OP_EQ, 0);
5224 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
5225 AF_INET);
5226 tt_ptr_op(addr, OP_EQ, NULL);
5228 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
5229 AF_INET6);
5230 tt_int_op(port, OP_EQ, 0);
5231 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
5232 AF_INET6);
5233 tt_ptr_op(addr, OP_EQ, NULL);
5235 config_line_append(&opts->ORPort_lines, "ORPort", "[1234::5678]:8080");
5236 config_line_append(&opts->ORPort_lines, "ORPort",
5237 "1.2.3.4:9999 noadvertise");
5238 config_line_append(&opts->ORPort_lines, "ORPort",
5239 "5.6.7.8:9911 nolisten");
5241 r = parse_ports(opts, 0, &msg, &n, &w);
5242 tt_assert(r == 0);
5244 // UNSPEC gets us nothing.
5245 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
5246 AF_UNSPEC);
5247 tt_int_op(port, OP_EQ, 0);
5248 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
5249 AF_UNSPEC);
5250 tt_ptr_op(addr, OP_EQ, NULL);
5252 // Try AF_INET.
5253 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
5254 AF_INET);
5255 tt_int_op(port, OP_EQ, 9911);
5256 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
5257 AF_INET);
5258 tt_ptr_op(addr, OP_NE, NULL);
5259 tt_str_op(fmt_addrport(addr,port), OP_EQ, "5.6.7.8:9911");
5261 // Try AF_INET6
5262 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
5263 AF_INET6);
5264 tt_int_op(port, OP_EQ, 8080);
5265 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
5266 AF_INET6);
5267 tt_ptr_op(addr, OP_NE, NULL);
5268 tt_str_op(fmt_addrport(addr,port), OP_EQ, "[1234::5678]:8080");
5270 done:
5271 or_options_free(opts);
5272 config_free_all();
5275 static void
5276 test_config_parse_log_severity(void *data)
5278 int ret;
5279 const char *severity_log_lines[] = {
5280 "debug file /tmp/debug.log",
5281 "debug\tfile /tmp/debug.log",
5282 "[handshake]debug [~net,~mm]info notice stdout",
5283 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
5284 NULL
5286 int i;
5287 log_severity_list_t *severity;
5289 (void) data;
5291 severity = tor_malloc(sizeof(log_severity_list_t));
5292 for (i = 0; severity_log_lines[i]; i++) {
5293 memset(severity, 0, sizeof(log_severity_list_t));
5294 ret = parse_log_severity_config(&severity_log_lines[i], severity);
5295 tt_int_op(ret, OP_EQ, 0);
5298 done:
5299 tor_free(severity);
5302 static void
5303 test_config_include_limit(void *data)
5305 (void)data;
5307 config_line_t *result = NULL;
5308 char *torrc_path = NULL;
5309 char *dir = tor_strdup(get_fname("test_include_limit"));
5310 tt_ptr_op(dir, OP_NE, NULL);
5312 #ifdef _WIN32
5313 tt_int_op(mkdir(dir), OP_EQ, 0);
5314 #else
5315 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5316 #endif
5318 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5319 char torrc_contents[1000];
5320 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
5321 torrc_path);
5322 tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
5324 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
5325 OP_EQ, -1);
5327 done:
5328 config_free_lines(result);
5329 tor_free(torrc_path);
5330 tor_free(dir);
5333 static void
5334 test_config_include_does_not_exist(void *data)
5336 (void)data;
5338 config_line_t *result = NULL;
5339 char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
5340 char *missing_path = NULL;
5341 tt_ptr_op(dir, OP_NE, NULL);
5343 #ifdef _WIN32
5344 tt_int_op(mkdir(dir), OP_EQ, 0);
5345 #else
5346 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5347 #endif
5349 tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
5350 char torrc_contents[1000];
5351 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
5352 missing_path);
5354 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
5355 OP_EQ, -1);
5357 done:
5358 config_free_lines(result);
5359 tor_free(dir);
5360 tor_free(missing_path);
5363 static void
5364 test_config_include_error_in_included_file(void *data)
5366 (void)data;
5367 config_line_t *result = NULL;
5369 char *dir = tor_strdup(get_fname("test_error_in_included_file"));
5370 char *invalid_path = NULL;
5371 tt_ptr_op(dir, OP_NE, NULL);
5373 #ifdef _WIN32
5374 tt_int_op(mkdir(dir), OP_EQ, 0);
5375 #else
5376 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5377 #endif
5379 tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
5380 tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
5382 char torrc_contents[1000];
5383 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
5384 invalid_path);
5386 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
5387 OP_EQ, -1);
5389 done:
5390 config_free_lines(result);
5391 tor_free(dir);
5392 tor_free(invalid_path);
5395 static void
5396 test_config_include_empty_file_folder(void *data)
5398 (void)data;
5399 config_line_t *result = NULL;
5401 char *folder_path = NULL;
5402 char *file_path = NULL;
5403 char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
5404 tt_ptr_op(dir, OP_NE, NULL);
5406 #ifdef _WIN32
5407 tt_int_op(mkdir(dir), OP_EQ, 0);
5408 #else
5409 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5410 #endif
5412 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir);
5413 #ifdef _WIN32
5414 tt_int_op(mkdir(folder_path), OP_EQ, 0);
5415 #else
5416 tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
5417 #endif
5418 tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
5419 tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
5421 char torrc_contents[1000];
5422 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5423 "%%include %s\n"
5424 "%%include %s\n",
5425 folder_path, file_path);
5427 int include_used;
5428 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5429 NULL), OP_EQ, 0);
5430 tt_ptr_op(result, OP_EQ, NULL);
5431 tt_int_op(include_used, OP_EQ, 1);
5433 done:
5434 config_free_lines(result);
5435 tor_free(folder_path);
5436 tor_free(file_path);
5437 tor_free(dir);
5440 #ifndef _WIN32
5441 static void
5442 test_config_include_no_permission(void *data)
5444 (void)data;
5445 config_line_t *result = NULL;
5447 char *folder_path = NULL;
5448 char *dir = NULL;
5449 if (geteuid() == 0)
5450 tt_skip();
5452 dir = tor_strdup(get_fname("test_include_forbidden_folder"));
5453 tt_ptr_op(dir, OP_NE, NULL);
5455 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5457 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
5458 tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
5460 char torrc_contents[1000];
5461 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5462 "%%include %s\n",
5463 folder_path);
5465 int include_used;
5466 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
5467 &include_used, NULL),
5468 OP_EQ, -1);
5469 tt_ptr_op(result, OP_EQ, NULL);
5471 done:
5472 config_free_lines(result);
5473 tor_free(folder_path);
5474 if (dir)
5475 chmod(dir, 0700);
5476 tor_free(dir);
5478 #endif /* !defined(_WIN32) */
5480 static void
5481 test_config_include_recursion_before_after(void *data)
5483 (void)data;
5485 config_line_t *result = NULL;
5486 char *torrc_path = NULL;
5487 char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
5488 tt_ptr_op(dir, OP_NE, NULL);
5490 #ifdef _WIN32
5491 tt_int_op(mkdir(dir), OP_EQ, 0);
5492 #else
5493 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5494 #endif
5496 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5498 char file_contents[1000];
5499 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5500 int i;
5501 // Loop backwards so file_contents has the contents of the first file by the
5502 // end of the loop
5503 for (i = limit; i > 0; i--) {
5504 if (i < limit) {
5505 tor_snprintf(file_contents, sizeof(file_contents),
5506 "Test %d\n"
5507 "%%include %s%d\n"
5508 "Test %d\n",
5509 i, torrc_path, i + 1, 2 * limit - i);
5510 } else {
5511 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
5514 if (i > 1) {
5515 char *file_path = NULL;
5516 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5517 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5518 tor_free(file_path);
5522 int include_used;
5523 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5524 NULL), OP_EQ, 0);
5525 tt_ptr_op(result, OP_NE, NULL);
5526 tt_int_op(include_used, OP_EQ, 1);
5528 int len = 0;
5529 config_line_t *next;
5530 for (next = result; next != NULL; next = next->next) {
5531 char expected[10];
5532 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5533 tt_str_op(next->key, OP_EQ, "Test");
5534 tt_str_op(next->value, OP_EQ, expected);
5535 len++;
5537 tt_int_op(len, OP_EQ, 2 * limit - 1);
5539 done:
5540 config_free_lines(result);
5541 tor_free(dir);
5542 tor_free(torrc_path);
5545 static void
5546 test_config_include_recursion_after_only(void *data)
5548 (void)data;
5550 config_line_t *result = NULL;
5551 char *torrc_path = NULL;
5552 char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
5553 tt_ptr_op(dir, OP_NE, NULL);
5555 #ifdef _WIN32
5556 tt_int_op(mkdir(dir), OP_EQ, 0);
5557 #else
5558 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5559 #endif
5561 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5563 char file_contents[1000];
5564 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5565 int i;
5566 // Loop backwards so file_contents has the contents of the first file by the
5567 // end of the loop
5568 for (i = limit; i > 0; i--) {
5569 int n = (i - limit - 1) * -1;
5570 if (i < limit) {
5571 tor_snprintf(file_contents, sizeof(file_contents),
5572 "%%include %s%d\n"
5573 "Test %d\n",
5574 torrc_path, i + 1, n);
5575 } else {
5576 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
5579 if (i > 1) {
5580 char *file_path = NULL;
5581 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5582 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5583 tor_free(file_path);
5587 int include_used;
5588 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5589 NULL), OP_EQ, 0);
5590 tt_ptr_op(result, OP_NE, NULL);
5591 tt_int_op(include_used, OP_EQ, 1);
5593 int len = 0;
5594 config_line_t *next;
5595 for (next = result; next != NULL; next = next->next) {
5596 char expected[10];
5597 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5598 tt_str_op(next->key, OP_EQ, "Test");
5599 tt_str_op(next->value, OP_EQ, expected);
5600 len++;
5602 tt_int_op(len, OP_EQ, limit);
5604 done:
5605 config_free_lines(result);
5606 tor_free(dir);
5607 tor_free(torrc_path);
5610 static void
5611 test_config_include_folder_order(void *data)
5613 (void)data;
5615 config_line_t *result = NULL;
5616 char *torrcd = NULL;
5617 char *path = NULL;
5618 char *path2 = NULL;
5619 char *dir = tor_strdup(get_fname("test_include_folder_order"));
5620 tt_ptr_op(dir, OP_NE, NULL);
5622 #ifdef _WIN32
5623 tt_int_op(mkdir(dir), OP_EQ, 0);
5624 #else
5625 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5626 #endif
5628 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5630 #ifdef _WIN32
5631 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5632 #else
5633 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5634 #endif
5636 // test that files in subfolders are ignored
5637 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5639 #ifdef _WIN32
5640 tt_int_op(mkdir(path), OP_EQ, 0);
5641 #else
5642 tt_int_op(mkdir(path, 0700), OP_EQ, 0);
5643 #endif
5645 tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
5646 tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
5647 tor_free(path);
5649 // test that files starting with . are ignored
5650 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5651 tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
5652 tor_free(path);
5654 // test file order
5655 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
5656 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5657 tor_free(path);
5659 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
5660 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
5661 tor_free(path);
5663 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
5664 tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
5665 tor_free(path);
5667 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
5668 tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
5669 tor_free(path);
5671 char torrc_contents[1000];
5672 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5673 "%%include %s\n",
5674 torrcd);
5676 int include_used;
5677 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5678 NULL), OP_EQ, 0);
5679 tt_ptr_op(result, OP_NE, NULL);
5680 tt_int_op(include_used, OP_EQ, 1);
5682 int len = 0;
5683 config_line_t *next;
5684 for (next = result; next != NULL; next = next->next) {
5685 char expected[10];
5686 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5687 tt_str_op(next->key, OP_EQ, "Test");
5688 tt_str_op(next->value, OP_EQ, expected);
5689 len++;
5691 tt_int_op(len, OP_EQ, 4);
5693 done:
5694 config_free_lines(result);
5695 tor_free(torrcd);
5696 tor_free(path);
5697 tor_free(path2);
5698 tor_free(dir);
5701 static void
5702 test_config_include_blank_file_last(void *data)
5704 (void)data;
5706 config_line_t *result = NULL;
5707 char *torrcd = NULL;
5708 char *path = NULL;
5709 char *dir = tor_strdup(get_fname("test_include_blank_file_last"));
5710 tt_ptr_op(dir, OP_NE, NULL);
5712 #ifdef _WIN32
5713 tt_int_op(mkdir(dir), OP_EQ, 0);
5714 #else
5715 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5716 #endif
5718 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5720 #ifdef _WIN32
5721 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5722 #else
5723 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5724 #endif
5726 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_1st");
5727 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5728 tor_free(path);
5730 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "bb_2nd");
5731 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
5732 tor_free(path);
5734 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "cc_comment");
5735 tt_int_op(write_str_to_file(path, "# comment only\n", 0), OP_EQ, 0);
5736 tor_free(path);
5738 char torrc_contents[1000];
5739 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5740 "%%include %s\n"
5741 "Test 3\n",
5742 torrcd);
5744 int include_used;
5745 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5746 NULL), OP_EQ, 0);
5747 tt_ptr_op(result, OP_NE, NULL);
5748 tt_int_op(include_used, OP_EQ, 1);
5750 int len = 0;
5751 config_line_t *next;
5752 for (next = result; next != NULL; next = next->next) {
5753 char expected[10];
5754 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5755 tt_str_op(next->key, OP_EQ, "Test");
5756 tt_str_op(next->value, OP_EQ, expected);
5757 len++;
5759 tt_int_op(len, OP_EQ, 3);
5761 done:
5762 config_free_lines(result);
5763 tor_free(torrcd);
5764 tor_free(path);
5765 tor_free(dir);
5768 static void
5769 test_config_include_path_syntax(void *data)
5771 (void)data;
5773 config_line_t *result = NULL;
5774 char *dir = tor_strdup(get_fname("test_include_path_syntax"));
5775 char *esc_dir = NULL, *dir_with_pathsep = NULL,
5776 *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
5777 tt_ptr_op(dir, OP_NE, NULL);
5779 #ifdef _WIN32
5780 tt_int_op(mkdir(dir), OP_EQ, 0);
5781 #else
5782 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5783 #endif
5785 esc_dir = esc_for_log(dir);
5786 tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
5787 esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
5789 tor_asprintf(&torrc_contents,
5790 "%%include %s\n"
5791 "%%include %s%s \n" // space to avoid suppressing newline
5792 "%%include %s\n",
5793 esc_dir,
5794 dir, PATH_SEPARATOR,
5795 esc_dir_with_pathsep);
5797 int include_used;
5798 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5799 NULL), OP_EQ, 0);
5800 tt_ptr_op(result, OP_EQ, NULL);
5801 tt_int_op(include_used, OP_EQ, 1);
5803 done:
5804 config_free_lines(result);
5805 tor_free(dir);
5806 tor_free(torrc_contents);
5807 tor_free(esc_dir);
5808 tor_free(dir_with_pathsep);
5809 tor_free(esc_dir_with_pathsep);
5812 static void
5813 test_config_include_not_processed(void *data)
5815 (void)data;
5817 char torrc_contents[1000] = "%include does_not_exist\n";
5818 config_line_t *result = NULL;
5819 tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
5820 tt_ptr_op(result, OP_NE, NULL);
5822 int len = 0;
5823 config_line_t *next;
5824 for (next = result; next != NULL; next = next->next) {
5825 tt_str_op(next->key, OP_EQ, "%include");
5826 tt_str_op(next->value, OP_EQ, "does_not_exist");
5827 len++;
5829 tt_int_op(len, OP_EQ, 1);
5831 done:
5832 config_free_lines(result);
5835 static void
5836 test_config_include_has_include(void *data)
5838 (void)data;
5840 config_line_t *result = NULL;
5841 char *dir = tor_strdup(get_fname("test_include_has_include"));
5842 tt_ptr_op(dir, OP_NE, NULL);
5844 #ifdef _WIN32
5845 tt_int_op(mkdir(dir), OP_EQ, 0);
5846 #else
5847 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5848 #endif
5850 char torrc_contents[1000] = "Test 1\n";
5851 int include_used;
5853 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5854 NULL), OP_EQ, 0);
5855 tt_int_op(include_used, OP_EQ, 0);
5856 config_free_lines(result);
5858 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
5859 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5860 NULL), OP_EQ, 0);
5861 tt_int_op(include_used, OP_EQ, 1);
5863 done:
5864 config_free_lines(result);
5865 tor_free(dir);
5868 static void
5869 test_config_include_flag_both_without(void *data)
5871 (void)data;
5873 char *errmsg = NULL;
5874 char conf_empty[1000];
5875 tor_snprintf(conf_empty, sizeof(conf_empty),
5876 "DataDirectory %s\n",
5877 get_fname(NULL));
5878 // test with defaults-torrc and torrc without include
5879 int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
5880 NULL, &errmsg);
5881 tt_int_op(ret, OP_EQ, 0);
5883 const or_options_t *options = get_options();
5884 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5886 done:
5887 tor_free(errmsg);
5888 config_free_all();
5891 static void
5892 test_config_include_flag_torrc_only(void *data)
5894 (void)data;
5896 char *errmsg = NULL;
5897 char *path = NULL;
5898 char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
5899 tt_ptr_op(dir, OP_NE, NULL);
5901 #ifdef _WIN32
5902 tt_int_op(mkdir(dir), OP_EQ, 0);
5903 #else
5904 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5905 #endif
5907 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5908 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5910 char conf_empty[1000];
5911 tor_snprintf(conf_empty, sizeof(conf_empty),
5912 "DataDirectory %s\n",
5913 get_fname(NULL));
5914 char conf_include[1000];
5915 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5917 // test with defaults-torrc without include and torrc with include
5918 int ret = options_init_from_string(conf_empty, conf_include,
5919 CMD_RUN_UNITTESTS, NULL, &errmsg);
5920 tt_int_op(ret, OP_EQ, 0);
5922 const or_options_t *options = get_options();
5923 tt_int_op(options->IncludeUsed, OP_EQ, 1);
5925 done:
5926 tor_free(errmsg);
5927 tor_free(path);
5928 tor_free(dir);
5929 config_free_all();
5932 static void
5933 test_config_include_flag_defaults_only(void *data)
5935 (void)data;
5937 char *errmsg = NULL;
5938 char *path = NULL;
5939 char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
5940 tt_ptr_op(dir, OP_NE, NULL);
5942 #ifdef _WIN32
5943 tt_int_op(mkdir(dir), OP_EQ, 0);
5944 #else
5945 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5946 #endif
5948 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5949 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5951 char conf_empty[1000];
5952 tor_snprintf(conf_empty, sizeof(conf_empty),
5953 "DataDirectory %s\n",
5954 get_fname(NULL));
5955 char conf_include[1000];
5956 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5958 // test with defaults-torrc with include and torrc without include
5959 int ret = options_init_from_string(conf_include, conf_empty,
5960 CMD_RUN_UNITTESTS, NULL, &errmsg);
5961 tt_int_op(ret, OP_EQ, 0);
5963 const or_options_t *options = get_options();
5964 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5966 done:
5967 tor_free(errmsg);
5968 tor_free(path);
5969 tor_free(dir);
5970 config_free_all();
5973 static void
5974 test_config_include_wildcards(void *data)
5976 (void)data;
5978 char *temp = NULL, *folder = NULL;
5979 config_line_t *result = NULL;
5980 char *dir = tor_strdup(get_fname("test_include_wildcards"));
5981 tt_ptr_op(dir, OP_NE, NULL);
5983 #ifdef _WIN32
5984 tt_int_op(mkdir(dir), OP_EQ, 0);
5985 #else
5986 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5987 #endif
5989 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "01_one.conf");
5990 tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0);
5991 tor_free(temp);
5993 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "02_two.conf");
5994 tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0);
5995 tor_free(temp);
5997 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "aa_three.conf");
5998 tt_int_op(write_str_to_file(temp, "Test 3\n", 0), OP_EQ, 0);
5999 tor_free(temp);
6001 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "foo");
6002 tt_int_op(write_str_to_file(temp, "Test 6\n", 0), OP_EQ, 0);
6003 tor_free(temp);
6005 tor_asprintf(&folder, "%s"PATH_SEPARATOR"%s", dir, "folder");
6007 #ifdef _WIN32
6008 tt_int_op(mkdir(folder), OP_EQ, 0);
6009 #else
6010 tt_int_op(mkdir(folder, 0700), OP_EQ, 0);
6011 #endif
6013 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "04_four.conf");
6014 tt_int_op(write_str_to_file(temp, "Test 4\n", 0), OP_EQ, 0);
6015 tor_free(temp);
6017 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "05_five.conf");
6018 tt_int_op(write_str_to_file(temp, "Test 5\n", 0), OP_EQ, 0);
6019 tor_free(temp);
6021 char torrc_contents[1000];
6022 int include_used;
6024 // test pattern that matches no file
6025 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6026 "%%include %s"PATH_SEPARATOR"not-exist*\n",
6027 dir);
6028 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6029 NULL), OP_EQ, 0);
6030 tt_ptr_op(result, OP_EQ, NULL);
6031 tt_int_op(include_used, OP_EQ, 1);
6032 config_free_lines(result);
6034 #ifndef _WIN32
6035 // test wildcard escaping
6036 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6037 "%%include %s"PATH_SEPARATOR"\\*\n",
6038 dir);
6039 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6040 NULL), OP_EQ, -1);
6041 tt_ptr_op(result, OP_EQ, NULL);
6042 tt_int_op(include_used, OP_EQ, 1);
6043 config_free_lines(result);
6044 #endif /* !defined(_WIN32) */
6046 // test pattern *.conf
6047 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6048 "%%include %s"PATH_SEPARATOR"*.conf\n",
6049 dir);
6050 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6051 NULL), OP_EQ, 0);
6052 tt_ptr_op(result, OP_NE, NULL);
6053 tt_int_op(include_used, OP_EQ, 1);
6055 int len = 0;
6056 config_line_t *next;
6057 char expected[10];
6058 for (next = result; next != NULL; next = next->next) {
6059 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
6060 tt_str_op(next->key, OP_EQ, "Test");
6061 tt_str_op(next->value, OP_EQ, expected);
6062 len++;
6064 tt_int_op(len, OP_EQ, 3);
6065 config_free_lines(result);
6067 // test pattern that matches folder and files
6068 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6069 "%%include %s"PATH_SEPARATOR"*\n",
6070 dir);
6071 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6072 NULL), OP_EQ, 0);
6073 tt_ptr_op(result, OP_NE, NULL);
6074 tt_int_op(include_used, OP_EQ, 1);
6076 len = 0;
6077 for (next = result; next != NULL; next = next->next) {
6078 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
6079 tt_str_op(next->key, OP_EQ, "Test");
6080 tt_str_op(next->value, OP_EQ, expected);
6081 len++;
6083 tt_int_op(len, OP_EQ, 6);
6084 config_free_lines(result);
6086 // test pattern ending in PATH_SEPARATOR, test linux path separator
6087 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6088 "%%include %s/f*/\n",
6089 dir);
6090 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6091 NULL), OP_EQ, 0);
6092 tt_ptr_op(result, OP_NE, NULL);
6093 tt_int_op(include_used, OP_EQ, 1);
6095 len = 0;
6096 for (next = result; next != NULL; next = next->next) {
6097 tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3);
6098 tt_str_op(next->key, OP_EQ, "Test");
6099 tt_str_op(next->value, OP_EQ, expected);
6100 len++;
6102 tt_int_op(len, OP_EQ, 2);
6103 config_free_lines(result);
6105 // test pattern with wildcards in folder and file
6106 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6107 "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*.conf\n",
6108 dir);
6109 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6110 NULL), OP_EQ, 0);
6111 tt_ptr_op(result, OP_NE, NULL);
6112 tt_int_op(include_used, OP_EQ, 1);
6114 len = 0;
6115 for (next = result; next != NULL; next = next->next) {
6116 tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3);
6117 tt_str_op(next->key, OP_EQ, "Test");
6118 tt_str_op(next->value, OP_EQ, expected);
6119 len++;
6121 tt_int_op(len, OP_EQ, 2);
6122 config_free_lines(result);
6124 done:
6125 config_free_lines(result);
6126 tor_free(folder);
6127 tor_free(temp);
6128 tor_free(dir);
6131 static void
6132 test_config_include_hidden(void *data)
6134 (void)data;
6136 char *temp = NULL, *folder = NULL;
6137 config_line_t *result = NULL;
6138 char *dir = tor_strdup(get_fname("test_include_hidden"));
6139 tt_ptr_op(dir, OP_NE, NULL);
6141 #ifdef _WIN32
6142 tt_int_op(mkdir(dir), OP_EQ, 0);
6143 #else
6144 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
6145 #endif
6147 tor_asprintf(&folder, "%s"PATH_SEPARATOR"%s", dir, ".dotdir");
6149 #ifdef _WIN32
6150 tt_int_op(mkdir(folder), OP_EQ, 0);
6151 #else
6152 tt_int_op(mkdir(folder, 0700), OP_EQ, 0);
6153 #endif
6155 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, ".dotfile");
6156 tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0);
6157 tor_free(temp);
6159 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "file");
6160 tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0);
6161 tor_free(temp);
6163 char torrc_contents[1000];
6164 int include_used;
6165 int len = 0;
6166 config_line_t *next;
6167 char expected[10];
6169 // test wildcards do not expand to dot folders (except for windows)
6170 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6171 "%%include %s"PATH_SEPARATOR"*\n",
6172 dir);
6173 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6174 NULL), OP_EQ, 0);
6175 tt_int_op(include_used, OP_EQ, 1);
6176 #ifdef _WIN32 // wildcard expansion includes dot files on Windows
6177 for (next = result; next != NULL; next = next->next) {
6178 tor_snprintf(expected, sizeof(expected), "%d", len + 2);
6179 tt_str_op(next->key, OP_EQ, "Test");
6180 tt_str_op(next->value, OP_EQ, expected);
6181 len++;
6183 tt_int_op(len, OP_EQ, 1);
6184 #else /* !defined(_WIN32) */
6185 tt_ptr_op(result, OP_EQ, NULL);
6186 #endif /* defined(_WIN32) */
6187 config_free_lines(result);
6189 // test wildcards match hidden folders when explicitly in the pattern
6190 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6191 "%%include %s"PATH_SEPARATOR".*\n",
6192 dir);
6193 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6194 NULL), OP_EQ, 0);
6195 tt_ptr_op(result, OP_NE, NULL);
6196 tt_int_op(include_used, OP_EQ, 1);
6198 len = 0;
6199 for (next = result; next != NULL; next = next->next) {
6200 tor_snprintf(expected, sizeof(expected), "%d", len + 2);
6201 tt_str_op(next->key, OP_EQ, "Test");
6202 tt_str_op(next->value, OP_EQ, expected);
6203 len++;
6205 tt_int_op(len, OP_EQ, 1);
6206 config_free_lines(result);
6208 // test hidden dir when explicitly included
6209 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6210 "%%include %s"PATH_SEPARATOR".dotdir\n",
6211 dir);
6212 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6213 NULL), OP_EQ, 0);
6214 tt_ptr_op(result, OP_NE, NULL);
6215 tt_int_op(include_used, OP_EQ, 1);
6217 len = 0;
6218 for (next = result; next != NULL; next = next->next) {
6219 tor_snprintf(expected, sizeof(expected), "%d", len + 2);
6220 tt_str_op(next->key, OP_EQ, "Test");
6221 tt_str_op(next->value, OP_EQ, expected);
6222 len++;
6224 tt_int_op(len, OP_EQ, 1);
6225 config_free_lines(result);
6227 // test hidden file when explicitly included
6228 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6229 "%%include %s"PATH_SEPARATOR".dotdir"PATH_SEPARATOR".dotfile\n",
6230 dir);
6231 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6232 NULL), OP_EQ, 0);
6233 tt_ptr_op(result, OP_NE, NULL);
6234 tt_int_op(include_used, OP_EQ, 1);
6236 len = 0;
6237 for (next = result; next != NULL; next = next->next) {
6238 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
6239 tt_str_op(next->key, OP_EQ, "Test");
6240 tt_str_op(next->value, OP_EQ, expected);
6241 len++;
6243 tt_int_op(len, OP_EQ, 1);
6244 config_free_lines(result);
6246 done:
6247 config_free_lines(result);
6248 tor_free(folder);
6249 tor_free(temp);
6250 tor_free(dir);
6253 static void
6254 test_config_dup_and_filter(void *arg)
6256 (void)arg;
6257 /* Test normal input. */
6258 config_line_t *line = NULL;
6259 config_line_append(&line, "abc", "def");
6260 config_line_append(&line, "ghi", "jkl");
6261 config_line_append(&line, "ABCD", "mno");
6263 config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
6264 tt_ptr_op(line_dup, OP_NE, NULL);
6265 tt_ptr_op(line_dup->next, OP_NE, NULL);
6266 tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
6268 tt_str_op(line_dup->key, OP_EQ, "abc");
6269 tt_str_op(line_dup->value, OP_EQ, "def");
6270 tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
6271 tt_str_op(line_dup->next->value, OP_EQ, "mno");
6273 /* empty output */
6274 config_free_lines(line_dup);
6275 line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
6276 tt_ptr_op(line_dup, OP_EQ, NULL);
6278 /* empty input */
6279 config_free_lines(line_dup);
6280 line_dup = config_lines_dup_and_filter(NULL, "abc");
6281 tt_ptr_op(line_dup, OP_EQ, NULL);
6283 done:
6284 config_free_lines(line);
6285 config_free_lines(line_dup);
6288 /* If we're not configured to be a bridge, but we set
6289 * BridgeDistribution, then options_validate () should return -1. */
6290 static void
6291 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
6293 or_options_t* options = get_options_mutable();
6294 or_options_t* old_options = options;
6295 char* message = NULL;
6296 int ret;
6298 (void)arg;
6300 options->BridgeRelay = 0;
6301 options->BridgeDistribution = (char*)("https");
6303 ret = options_validate(old_options, options, &message);
6305 tt_int_op(ret, OP_EQ, -1);
6306 tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
6307 "didn't set BridgeRelay!");
6308 done:
6309 tor_free(message);
6310 options->BridgeDistribution = NULL;
6313 /* If the BridgeDistribution setting was valid, 0 should be returned. */
6314 static void
6315 test_config_check_bridge_distribution_setting_valid(void *arg)
6317 (void)arg;
6319 // Check all the possible values we support right now.
6320 tt_int_op(check_bridge_distribution_setting("none"), OP_EQ, 0);
6321 tt_int_op(check_bridge_distribution_setting("any"), OP_EQ, 0);
6322 tt_int_op(check_bridge_distribution_setting("https"), OP_EQ, 0);
6323 tt_int_op(check_bridge_distribution_setting("email"), OP_EQ, 0);
6324 tt_int_op(check_bridge_distribution_setting("moat"), OP_EQ, 0);
6326 // Check all the possible values we support right now with weird casing.
6327 tt_int_op(check_bridge_distribution_setting("NoNe"), OP_EQ, 0);
6328 tt_int_op(check_bridge_distribution_setting("anY"), OP_EQ, 0);
6329 tt_int_op(check_bridge_distribution_setting("hTTps"), OP_EQ, 0);
6330 tt_int_op(check_bridge_distribution_setting("emAIl"), OP_EQ, 0);
6331 tt_int_op(check_bridge_distribution_setting("moAt"), OP_EQ, 0);
6333 // Invalid values.
6334 tt_int_op(check_bridge_distribution_setting("x\rx"), OP_EQ, -1);
6335 tt_int_op(check_bridge_distribution_setting("x\nx"), OP_EQ, -1);
6336 tt_int_op(check_bridge_distribution_setting("\t\t\t"), OP_EQ, -1);
6338 done:
6339 return;
6342 /* If the BridgeDistribution setting was invalid, -1 should be returned. */
6343 static void
6344 test_config_check_bridge_distribution_setting_invalid(void *arg)
6346 int ret = check_bridge_distribution_setting("hyphens-are-allowed");
6348 (void)arg;
6350 tt_int_op(ret, OP_EQ, 0);
6352 ret = check_bridge_distribution_setting("asterisks*are*forbidden");
6354 tt_int_op(ret, OP_EQ, -1);
6355 done:
6356 return;
6359 /* If the BridgeDistribution setting was unrecognised, a warning should be
6360 * logged and 0 should be returned. */
6361 static void
6362 test_config_check_bridge_distribution_setting_unrecognised(void *arg)
6364 int ret = check_bridge_distribution_setting("unicorn");
6366 (void)arg;
6368 tt_int_op(ret, OP_EQ, 0);
6369 done:
6370 return;
6373 static void
6374 test_config_include_opened_file_list(void *data)
6376 (void)data;
6378 config_line_t *result = NULL;
6379 smartlist_t *opened_files = smartlist_new();
6380 char *torrcd = NULL;
6381 char *subfolder = NULL;
6382 char *in_subfolder = NULL;
6383 char *empty = NULL;
6384 char *file = NULL;
6385 char *dot = NULL;
6386 char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
6387 tt_ptr_op(dir, OP_NE, NULL);
6389 #ifdef _WIN32
6390 tt_int_op(mkdir(dir), OP_EQ, 0);
6391 #else
6392 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
6393 #endif
6395 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
6397 #ifdef _WIN32
6398 tt_int_op(mkdir(torrcd), OP_EQ, 0);
6399 #else
6400 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
6401 #endif
6403 tor_asprintf(&subfolder, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
6405 #ifdef _WIN32
6406 tt_int_op(mkdir(subfolder), OP_EQ, 0);
6407 #else
6408 tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
6409 #endif
6411 tor_asprintf(&in_subfolder, "%s"PATH_SEPARATOR"%s", subfolder,
6412 "01_file_in_subfolder");
6413 tt_int_op(write_str_to_file(in_subfolder, "Test 1\n", 0), OP_EQ, 0);
6415 tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty");
6416 tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
6418 tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file");
6419 tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
6421 tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
6422 tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
6424 char torrc_contents[1000];
6425 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6426 "%%include %s\n",
6427 torrcd);
6429 int include_used;
6430 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6431 opened_files), OP_EQ, 0);
6432 tt_ptr_op(result, OP_NE, NULL);
6433 tt_int_op(include_used, OP_EQ, 1);
6435 tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
6436 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
6437 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
6438 // files inside subfolders are not opened, only the subfolder is opened
6439 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
6440 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
6441 // dot files are not opened as we ignore them when we get their name from
6442 // their parent folder
6444 // test with wildcards
6445 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
6446 smartlist_clear(opened_files);
6447 config_free_lines(result);
6448 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6449 "%%include %s"PATH_SEPARATOR"*\n",
6450 torrcd);
6451 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6452 opened_files), OP_EQ, 0);
6453 tt_ptr_op(result, OP_NE, NULL);
6454 tt_int_op(include_used, OP_EQ, 1);
6456 #ifdef _WIN32
6457 tt_int_op(smartlist_len(opened_files), OP_EQ, 6);
6458 #else
6459 tt_int_op(smartlist_len(opened_files), OP_EQ, 5);
6460 #endif
6461 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
6462 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
6463 // * will match the subfolder inside torrc.d, so it will be included
6464 tt_int_op(smartlist_contains_string(opened_files, in_subfolder), OP_EQ, 1);
6465 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
6466 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
6467 #ifdef _WIN32
6468 // * matches the dot file on Windows
6469 tt_int_op(smartlist_contains_string(opened_files, dot), OP_EQ, 1);
6470 #endif
6472 // test with wildcards in folder and file
6473 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
6474 smartlist_clear(opened_files);
6475 config_free_lines(result);
6476 tor_snprintf(torrc_contents, sizeof(torrc_contents),
6477 "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*\n",
6478 torrcd);
6479 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
6480 opened_files), OP_EQ, 0);
6481 tt_ptr_op(result, OP_NE, NULL);
6482 tt_int_op(include_used, OP_EQ, 1);
6484 #ifdef _WIN32
6485 tt_int_op(smartlist_len(opened_files), OP_EQ, 6);
6486 #else
6487 tt_int_op(smartlist_len(opened_files), OP_EQ, 5);
6488 #endif
6489 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
6490 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
6491 tt_int_op(smartlist_contains_string(opened_files, in_subfolder), OP_EQ, 1);
6492 // stat is called on the following files, so they count as opened
6493 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
6494 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
6495 #ifdef _WIN32
6496 // * matches the dot file on Windows
6497 tt_int_op(smartlist_contains_string(opened_files, dot), OP_EQ, 1);
6498 #endif
6500 done:
6501 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
6502 smartlist_free(opened_files);
6503 config_free_lines(result);
6504 tor_free(torrcd);
6505 tor_free(subfolder);
6506 tor_free(in_subfolder);
6507 tor_free(empty);
6508 tor_free(file);
6509 tor_free(dot);
6510 tor_free(dir);
6513 static void
6514 test_config_compute_max_mem_in_queues(void *data)
6516 #define GIGABYTE(x) (UINT64_C(x) << 30)
6517 #define MEGABYTE(x) (UINT64_C(x) << 20)
6518 (void)data;
6519 MOCK(get_total_system_memory, get_total_system_memory_mock);
6521 /* We are unable to detect the amount of memory on the system. Tor will try
6522 * to use some sensible default values for 64-bit and 32-bit systems. */
6523 total_system_memory_return = -1;
6525 #if SIZEOF_VOID_P >= 8
6526 /* We are on a 64-bit system. */
6527 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
6528 #else
6529 /* We are on a 32-bit system. */
6530 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
6531 #endif /* SIZEOF_VOID_P >= 8 */
6533 /* We are able to detect the amount of RAM on the system. */
6534 total_system_memory_return = 0;
6536 /* We are running on a system with one gigabyte of RAM. */
6537 total_system_memory_output = GIGABYTE(1);
6539 /* We have 0.75 * RAM available. */
6540 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
6541 3 * (GIGABYTE(1) / 4));
6543 /* We are running on a tiny machine with 256 MB of RAM. */
6544 total_system_memory_output = MEGABYTE(256);
6546 /* We will now enforce a minimum of 256 MB of RAM available for the
6547 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
6548 * MB available. */
6549 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
6551 #if SIZEOF_SIZE_T > 4
6552 /* We are running on a machine with 8 GB of RAM. */
6553 total_system_memory_output = GIGABYTE(8);
6555 /* We will have 0.4 * RAM available. */
6556 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
6557 2 * (GIGABYTE(8) / 5));
6559 /* We are running on a machine with 16 GB of RAM. */
6560 total_system_memory_output = GIGABYTE(16);
6562 /* We will have 0.4 * RAM available. */
6563 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
6564 2 * (GIGABYTE(16) / 5));
6566 /* We are running on a machine with 32 GB of RAM. */
6567 total_system_memory_output = GIGABYTE(32);
6569 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
6570 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
6571 MAX_DEFAULT_MEMORY_QUEUE_SIZE);
6572 #endif /* SIZEOF_SIZE_T > 4 */
6574 done:
6575 UNMOCK(get_total_system_memory);
6577 #undef GIGABYTE
6578 #undef MEGABYTE
6581 static void
6582 test_config_extended_fmt(void *arg)
6584 (void)arg;
6585 config_line_t *lines = NULL, *lp;
6586 const char string1[] =
6587 "thing1 is here\n"
6588 "+thing2 is over here\n"
6589 "/thing3\n"
6590 "/thing4 is back here\n";
6592 /* Try with the "extended" flag disabled. */
6593 int r = config_get_lines(string1, &lines, 0);
6594 tt_int_op(r, OP_EQ, 0);
6595 lp = lines;
6596 tt_ptr_op(lp, OP_NE, NULL);
6597 tt_str_op(lp->key, OP_EQ, "thing1");
6598 tt_str_op(lp->value, OP_EQ, "is here");
6599 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
6600 lp = lp->next;
6601 tt_ptr_op(lp, OP_NE, NULL);
6602 tt_str_op(lp->key, OP_EQ, "+thing2");
6603 tt_str_op(lp->value, OP_EQ, "is over here");
6604 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
6605 lp = lp->next;
6606 tt_ptr_op(lp, OP_NE, NULL);
6607 tt_str_op(lp->key, OP_EQ, "/thing3");
6608 tt_str_op(lp->value, OP_EQ, "");
6609 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
6610 lp = lp->next;
6611 tt_ptr_op(lp, OP_NE, NULL);
6612 tt_str_op(lp->key, OP_EQ, "/thing4");
6613 tt_str_op(lp->value, OP_EQ, "is back here");
6614 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
6615 lp = lp->next;
6616 tt_assert(!lp);
6617 config_free_lines(lines);
6619 /* Try with the "extended" flag enabled. */
6620 r = config_get_lines(string1, &lines, 1);
6621 tt_int_op(r, OP_EQ, 0);
6622 lp = lines;
6623 tt_ptr_op(lp, OP_NE, NULL);
6624 tt_str_op(lp->key, OP_EQ, "thing1");
6625 tt_str_op(lp->value, OP_EQ, "is here");
6626 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
6627 lp = lp->next;
6628 tt_ptr_op(lp, OP_NE, NULL);
6629 tt_str_op(lp->key, OP_EQ, "thing2");
6630 tt_str_op(lp->value, OP_EQ, "is over here");
6631 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_APPEND);
6632 lp = lp->next;
6633 tt_ptr_op(lp, OP_NE, NULL);
6634 tt_str_op(lp->key, OP_EQ, "thing3");
6635 tt_str_op(lp->value, OP_EQ, "");
6636 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
6637 lp = lp->next;
6638 tt_ptr_op(lp, OP_NE, NULL);
6639 tt_str_op(lp->key, OP_EQ, "thing4");
6640 tt_str_op(lp->value, OP_EQ, "");
6641 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
6642 lp = lp->next;
6643 tt_assert(!lp);
6645 done:
6646 config_free_lines(lines);
6649 static void
6650 test_config_kvline_parse(void *arg)
6652 (void)arg;
6654 config_line_t *lines = NULL;
6655 char *enc = NULL;
6657 lines = kvline_parse("A=B CD=EF", 0);
6658 tt_assert(lines);
6659 tt_str_op(lines->key, OP_EQ, "A");
6660 tt_str_op(lines->value, OP_EQ, "B");
6661 tt_str_op(lines->next->key, OP_EQ, "CD");
6662 tt_str_op(lines->next->value, OP_EQ, "EF");
6663 enc = kvline_encode(lines, 0);
6664 tt_str_op(enc, OP_EQ, "A=B CD=EF");
6665 tor_free(enc);
6666 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
6667 tt_str_op(enc, OP_EQ, "A=B CD=EF");
6668 tor_free(enc);
6669 config_free_lines(lines);
6671 lines = kvline_parse("AB CDE=F", 0);
6672 tt_assert(! lines);
6674 lines = kvline_parse("AB CDE=F", KV_OMIT_KEYS);
6675 tt_assert(lines);
6676 tt_str_op(lines->key, OP_EQ, "");
6677 tt_str_op(lines->value, OP_EQ, "AB");
6678 tt_str_op(lines->next->key, OP_EQ, "CDE");
6679 tt_str_op(lines->next->value, OP_EQ, "F");
6680 tt_assert(lines);
6681 enc = kvline_encode(lines, 0);
6682 tt_assert(!enc);
6683 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
6684 tt_str_op(enc, OP_EQ, "AB CDE=F");
6685 tor_free(enc);
6686 config_free_lines(lines);
6688 lines = kvline_parse("AB=C CDE=\"F G\"", 0);
6689 tt_assert(!lines);
6691 lines = kvline_parse("AB=C CDE=\"F G\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
6692 tt_assert(lines);
6693 tt_str_op(lines->key, OP_EQ, "AB");
6694 tt_str_op(lines->value, OP_EQ, "C");
6695 tt_str_op(lines->next->key, OP_EQ, "CDE");
6696 tt_str_op(lines->next->value, OP_EQ, "F G");
6697 tt_str_op(lines->next->next->key, OP_EQ, "");
6698 tt_str_op(lines->next->next->value, OP_EQ, "GHI");
6699 enc = kvline_encode(lines, 0);
6700 tt_assert(!enc);
6701 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
6702 tt_str_op(enc, OP_EQ, "AB=C CDE=\"F G\" GHI");
6703 tor_free(enc);
6704 config_free_lines(lines);
6706 lines = kvline_parse("A\"B=C CDE=\"F\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
6707 tt_assert(! lines);
6709 lines = kvline_parse("AB=", KV_QUOTED);
6710 tt_assert(lines);
6711 tt_str_op(lines->key, OP_EQ, "AB");
6712 tt_str_op(lines->value, OP_EQ, "");
6713 config_free_lines(lines);
6715 lines = kvline_parse("AB=", 0);
6716 tt_assert(lines);
6717 tt_str_op(lines->key, OP_EQ, "AB");
6718 tt_str_op(lines->value, OP_EQ, "");
6719 config_free_lines(lines);
6721 lines = kvline_parse("AB=", KV_OMIT_VALS);
6722 tt_assert(lines);
6723 tt_str_op(lines->key, OP_EQ, "AB");
6724 tt_str_op(lines->value, OP_EQ, "");
6725 config_free_lines(lines);
6727 lines = kvline_parse(" AB ", KV_OMIT_VALS);
6728 tt_assert(lines);
6729 tt_str_op(lines->key, OP_EQ, "AB");
6730 tt_str_op(lines->value, OP_EQ, "");
6731 config_free_lines(lines);
6733 lines = kvline_parse("AB", KV_OMIT_VALS);
6734 tt_assert(lines);
6735 tt_str_op(lines->key, OP_EQ, "AB");
6736 tt_str_op(lines->value, OP_EQ, "");
6737 enc = kvline_encode(lines, KV_OMIT_VALS);
6738 tt_str_op(enc, OP_EQ, "AB");
6739 tor_free(enc);
6740 config_free_lines(lines);
6742 lines = kvline_parse("AB=CD", KV_OMIT_VALS);
6743 tt_assert(lines);
6744 tt_str_op(lines->key, OP_EQ, "AB");
6745 tt_str_op(lines->value, OP_EQ, "CD");
6746 enc = kvline_encode(lines, KV_OMIT_VALS);
6747 tt_str_op(enc, OP_EQ, "AB=CD");
6748 tor_free(enc);
6749 config_free_lines(lines);
6751 lines = kvline_parse("AB=CD DE FGH=I", KV_OMIT_VALS);
6752 tt_assert(lines);
6753 tt_str_op(lines->key, OP_EQ, "AB");
6754 tt_str_op(lines->value, OP_EQ, "CD");
6755 tt_str_op(lines->next->key, OP_EQ, "DE");
6756 tt_str_op(lines->next->value, OP_EQ, "");
6757 tt_str_op(lines->next->next->key, OP_EQ, "FGH");
6758 tt_str_op(lines->next->next->value, OP_EQ, "I");
6759 enc = kvline_encode(lines, KV_OMIT_VALS);
6760 tt_str_op(enc, OP_EQ, "AB=CD DE FGH=I");
6761 tor_free(enc);
6762 config_free_lines(lines);
6764 lines = kvline_parse("AB=\"CD E\" DE FGH=\"I\"", KV_OMIT_VALS|KV_QUOTED);
6765 tt_assert(lines);
6766 tt_str_op(lines->key, OP_EQ, "AB");
6767 tt_str_op(lines->value, OP_EQ, "CD E");
6768 tt_str_op(lines->next->key, OP_EQ, "DE");
6769 tt_str_op(lines->next->value, OP_EQ, "");
6770 tt_str_op(lines->next->next->key, OP_EQ, "FGH");
6771 tt_str_op(lines->next->next->value, OP_EQ, "I");
6772 enc = kvline_encode(lines, KV_OMIT_VALS|KV_QUOTED);
6773 tt_str_op(enc, OP_EQ, "AB=\"CD E\" DE FGH=I");
6774 tor_free(enc);
6775 config_free_lines(lines);
6777 lines = kvline_parse("AB=CD \"EF=GH\"", KV_OMIT_KEYS|KV_QUOTED);
6778 tt_assert(lines);
6779 tt_str_op(lines->key, OP_EQ, "AB");
6780 tt_str_op(lines->value, OP_EQ, "CD");
6781 tt_str_op(lines->next->key, OP_EQ, "");
6782 tt_str_op(lines->next->value, OP_EQ, "EF=GH");
6783 enc = kvline_encode(lines, KV_OMIT_KEYS);
6784 tt_assert(!enc);
6785 enc = kvline_encode(lines, KV_OMIT_KEYS|KV_QUOTED);
6786 tt_assert(enc);
6787 tt_str_op(enc, OP_EQ, "AB=CD \"EF=GH\"");
6788 tor_free(enc);
6789 config_free_lines(lines);
6791 lines = tor_malloc_zero(sizeof(*lines));
6792 lines->key = tor_strdup("A=B");
6793 lines->value = tor_strdup("CD");
6794 enc = kvline_encode(lines, 0);
6795 tt_assert(!enc);
6796 config_free_lines(lines);
6798 config_line_append(&lines, "A", "B C");
6799 enc = kvline_encode(lines, 0);
6800 tt_assert(!enc);
6801 enc = kvline_encode(lines, KV_RAW);
6802 tt_assert(enc);
6803 tt_str_op(enc, OP_EQ, "A=B C");
6805 done:
6806 config_free_lines(lines);
6807 tor_free(enc);
6810 static void
6811 test_config_getinfo_config_names(void *arg)
6813 (void)arg;
6814 char *answer = NULL;
6815 const char *error = NULL;
6816 int rv;
6818 rv = getinfo_helper_config(NULL, "config/names", &answer, &error);
6819 tt_int_op(rv, OP_EQ, 0);
6820 tt_ptr_op(error, OP_EQ, NULL);
6822 // ContactInfo should be listed.
6823 tt_assert(strstr(answer, "\nContactInfo String\n"));
6825 // V1AuthoritativeDirectory should not be listed, since it is obsolete.
6826 tt_assert(! strstr(answer, "V1AuthoritativeDirectory"));
6828 // ___UsingTestNetworkDefaults should not be listed, since it is invisible.
6829 tt_assert(! strstr(answer, "UsingTestNetworkDefaults"));
6831 done:
6832 tor_free(answer);
6835 static void
6836 test_config_duplicate_orports(void *arg)
6838 (void)arg;
6840 config_line_t *config_port = NULL;
6841 smartlist_t *ports = smartlist_new();
6843 // Pretend that the user has specified an implicit 0.0.0.0:9050, an implicit
6844 // [::]:9050, and an explicit on [::1]:9050.
6845 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
6846 config_line_append(&config_port, "ORPort", "[::1]:9050");
6848 // Parse IPv4, then IPv6.
6849 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
6850 0, CL_PORT_SERVER_OPTIONS);
6851 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]",
6852 0, CL_PORT_SERVER_OPTIONS);
6854 /* There should be 4 ports at this point that is:
6855 * - 0.0.0.0:9050
6856 * - [::]:9050
6857 * - [::1]:9050
6858 * - [::1]:9050
6860 tt_int_op(smartlist_len(ports), OP_EQ, 4);
6862 /* This will remove the [::] and the extra [::1]. */
6863 remove_duplicate_orports(ports);
6865 tt_int_op(smartlist_len(ports), OP_EQ, 2);
6866 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
6867 "ORPort 9050");
6868 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
6869 "ORPort [::1]:9050");
6871 /* Reset. Test different ORPort value. */
6872 SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p));
6873 smartlist_free(ports);
6874 config_free_lines(config_port);
6875 config_port = NULL;
6876 ports = smartlist_new();
6878 /* Implicit port and then specific IPv6 addresses but more than one. */
6879 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
6880 config_line_append(&config_port, "ORPort", "[4242::1]:9051");
6881 config_line_append(&config_port, "ORPort", "[4242::2]:9051");
6883 // Parse IPv4, then IPv6.
6884 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
6885 0, CL_PORT_SERVER_OPTIONS);
6886 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]",
6887 0, CL_PORT_SERVER_OPTIONS);
6889 /* There should be 6 ports at this point that is:
6890 * - 0.0.0.0:9050
6891 * - [::]:9050
6892 * - [4242::1]:9051
6893 * - [4242::1]:9051
6894 * - [4242::2]:9051
6895 * - [4242::2]:9051
6897 tt_int_op(smartlist_len(ports), OP_EQ, 6);
6899 /* This will remove the [::] and the duplicates. */
6900 remove_duplicate_orports(ports);
6902 /* We have four address here, 1 IPv4 on 9050, IPv6 on 9050, IPv6 on 9051 and
6903 * a different IPv6 on 9051. */
6904 tt_int_op(smartlist_len(ports), OP_EQ, 4);
6905 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
6906 "ORPort 9050");
6907 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
6908 "ORPort [4242::1]:9051");
6909 tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ,
6910 "ORPort [4242::2]:9051");
6911 tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ,
6912 "ORPort 9050");
6914 /* Reset. Test different ORPort value. */
6915 SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p));
6916 smartlist_free(ports);
6917 config_free_lines(config_port);
6918 config_port = NULL;
6919 ports = smartlist_new();
6921 /* Three different ports. */
6922 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
6923 config_line_append(&config_port, "ORPort", "[4242::1]:9051");
6924 config_line_append(&config_port, "ORPort", "[4242::2]:9052");
6926 // Parse IPv4, then IPv6.
6927 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
6928 0, CL_PORT_SERVER_OPTIONS);
6929 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]",
6930 0, CL_PORT_SERVER_OPTIONS);
6932 /* There should be 6 ports at this point that is:
6933 * - 0.0.0.0:9050
6934 * - [::]:9050
6935 * - [4242::1]:9051
6936 * - [4242::1]:9051
6937 * - [4242::2]:9052
6938 * - [4242::2]:9052
6940 tt_int_op(smartlist_len(ports), OP_EQ, 6);
6942 /* This will remove the [::] and the duplicates. */
6943 remove_duplicate_orports(ports);
6945 /* We have four address here, 1 IPv4 on 9050, IPv6 on 9050, IPv6 on 9051 and
6946 * IPv6 on 9052. */
6947 tt_int_op(smartlist_len(ports), OP_EQ, 4);
6948 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
6949 "ORPort 9050");
6950 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
6951 "ORPort [4242::1]:9051");
6952 tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ,
6953 "ORPort [4242::2]:9052");
6954 tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ,
6955 "ORPort 9050");
6957 done:
6958 SMARTLIST_FOREACH(ports,port_cfg_t *,pf,port_cfg_free(pf));
6959 smartlist_free(ports);
6960 config_free_lines(config_port);
6963 static void
6964 test_config_multifamily_port(void *arg)
6966 (void) arg;
6968 config_line_t *config_port = NULL;
6969 smartlist_t *ports = smartlist_new();
6971 config_line_append(&config_port, "SocksPort", "9050");
6972 config_line_append(&config_port, "SocksPort", "[::1]:9050");
6974 // Parse IPv4, then IPv6.
6975 port_parse_config(ports, config_port, "SOCKS", CONN_TYPE_AP_LISTENER,
6976 "0.0.0.0", 9050, 0);
6978 /* There should be 2 ports at this point that is:
6979 * - 0.0.0.0:9050
6980 * - [::1]:9050
6982 tt_int_op(smartlist_len(ports), OP_EQ, 2);
6984 done:
6985 SMARTLIST_FOREACH(ports, port_cfg_t *, cfg, port_cfg_free(cfg));
6986 smartlist_free(ports);
6987 config_free_lines(config_port);
6990 #ifndef COCCI
6991 #define CONFIG_TEST(name, flags) \
6992 { #name, test_config_ ## name, flags, NULL, NULL }
6994 #define CONFIG_TEST_SETUP(suffix, name, flags, setup, setup_data) \
6995 { #name#suffix, test_config_ ## name, flags, setup, setup_data }
6996 #endif /* !defined(COCCI) */
6998 struct testcase_t config_tests[] = {
6999 CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
7000 CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
7001 CONFIG_TEST(parsing_trusted_dir_server, 0),
7002 CONFIG_TEST(parsing_invalid_dir_address, 0),
7003 CONFIG_TEST(parsing_fallback_dir_server, 0),
7004 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
7005 CONFIG_TEST(adding_dir_servers, TT_FORK),
7006 CONFIG_TEST(default_dir_servers, TT_FORK),
7007 CONFIG_TEST(default_fallback_dirs, 0),
7008 CONFIG_TEST_SETUP(_v4, find_my_address, TT_FORK,
7009 &passthrough_setup, &addr_param_v4),
7010 CONFIG_TEST_SETUP(_v6, find_my_address, TT_FORK,
7011 &passthrough_setup, &addr_param_v6),
7012 CONFIG_TEST(find_my_address_mixed, TT_FORK),
7013 CONFIG_TEST(addressmap, 0),
7014 CONFIG_TEST(parse_bridge_line, 0),
7015 CONFIG_TEST(parse_transport_options_line, 0),
7016 CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
7017 CONFIG_TEST(parse_tcp_proxy_line, TT_FORK),
7018 CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
7019 CONFIG_TEST(write_to_data_subdir, TT_FORK),
7020 CONFIG_TEST(fix_my_family, 0),
7021 CONFIG_TEST(directory_fetch, 0),
7022 CONFIG_TEST(port_cfg_line_extract_addrport, 0),
7023 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
7024 CONFIG_TEST(parse_port_config__ports__server_options, 0),
7025 CONFIG_TEST(parse_port_config__ports__ports_given, 0),
7026 CONFIG_TEST(get_first_advertised, TT_FORK),
7027 CONFIG_TEST(parse_log_severity, 0),
7028 CONFIG_TEST(include_limit, 0),
7029 CONFIG_TEST(include_does_not_exist, 0),
7030 CONFIG_TEST(include_error_in_included_file, 0),
7031 CONFIG_TEST(include_empty_file_folder, 0),
7032 #ifndef _WIN32
7033 CONFIG_TEST(include_no_permission, 0),
7034 #endif
7035 CONFIG_TEST(include_recursion_before_after, 0),
7036 CONFIG_TEST(include_recursion_after_only, 0),
7037 CONFIG_TEST(include_folder_order, 0),
7038 CONFIG_TEST(include_blank_file_last, 0),
7039 CONFIG_TEST(include_path_syntax, 0),
7040 CONFIG_TEST(include_not_processed, 0),
7041 CONFIG_TEST(include_has_include, 0),
7042 CONFIG_TEST(include_flag_both_without, TT_FORK),
7043 CONFIG_TEST(include_flag_torrc_only, TT_FORK),
7044 CONFIG_TEST(include_flag_defaults_only, TT_FORK),
7045 CONFIG_TEST(include_wildcards, 0),
7046 CONFIG_TEST(include_hidden, 0),
7047 CONFIG_TEST(dup_and_filter, 0),
7048 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
7049 CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
7050 CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
7051 CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
7052 CONFIG_TEST(include_opened_file_list, 0),
7053 CONFIG_TEST(compute_max_mem_in_queues, 0),
7054 CONFIG_TEST(extended_fmt, 0),
7055 CONFIG_TEST(kvline_parse, 0),
7056 CONFIG_TEST(getinfo_config_names, 0),
7057 CONFIG_TEST(duplicate_orports, 0),
7058 CONFIG_TEST(multifamily_port, 0),
7059 END_OF_TESTCASES