1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
10 #define ROUTERSET_PRIVATE
11 #include "core/or/or.h"
12 #include "lib/net/address.h"
13 #include "lib/net/resolve.h"
14 #include "feature/client/addressmap.h"
15 #include "feature/client/bridges.h"
16 #include "core/or/circuitmux_ewma.h"
17 #include "core/or/circuitbuild.h"
18 #include "app/config/config.h"
19 #include "app/config/confparse.h"
20 #include "core/mainloop/connection.h"
21 #include "core/or/connection_edge.h"
22 #include "test/test.h"
23 #include "core/or/connection_or.h"
24 #include "feature/control/control.h"
25 #include "core/mainloop/cpuworker.h"
26 #include "feature/dircache/dirserv.h"
27 #include "feature/dirauth/dirvote.h"
28 #include "feature/relay/dns.h"
29 #include "feature/client/entrynodes.h"
30 #include "feature/client/transports.h"
31 #include "feature/relay/ext_orport.h"
32 #include "lib/geoip/geoip.h"
33 #include "feature/hibernate/hibernate.h"
34 #include "core/mainloop/mainloop.h"
35 #include "feature/nodelist/networkstatus.h"
36 #include "feature/nodelist/nodelist.h"
37 #include "core/or/policies.h"
38 #include "feature/rend/rendclient.h"
39 #include "feature/rend/rendservice.h"
40 #include "feature/relay/router.h"
41 #include "feature/relay/routermode.h"
42 #include "feature/nodelist/dirlist.h"
43 #include "feature/nodelist/routerlist.h"
44 #include "feature/nodelist/routerset.h"
45 #include "app/config/statefile.h"
47 #include "test/test_helpers.h"
49 #include "feature/dirclient/dir_server_st.h"
50 #include "core/or/port_cfg_st.h"
51 #include "feature/nodelist/routerinfo_st.h"
53 #include "lib/fs/conffile.h"
54 #include "lib/meminfo/meminfo.h"
55 #include "lib/net/gethostname.h"
56 #include "lib/encoding/confline.h"
61 #ifdef HAVE_SYS_STAT_H
66 test_config_addressmap(void *arg
)
70 time_t expires
= TIME_MAX
;
73 strlcpy(buf
, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
74 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
75 "MapAddress *.google.com *.torserver.exit\n"
76 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
77 "MapAddress *.cn.com www.cnn.com\n"
78 "MapAddress *.cnn.com www.cnn.com\n"
79 "MapAddress ex.com www.cnn.com\n"
80 "MapAddress ey.com *.cnn.com\n"
81 "MapAddress www.torproject.org 1.1.1.1\n"
82 "MapAddress other.torproject.org "
83 "this.torproject.org.otherserver.exit\n"
84 "MapAddress test.torproject.org 2.2.2.2\n"
85 "MapAddress www.google.com 3.3.3.3\n"
86 "MapAddress www.example.org 4.4.4.4\n"
87 "MapAddress 4.4.4.4 7.7.7.7\n"
88 "MapAddress 4.4.4.4 5.5.5.5\n"
89 "MapAddress www.infiniteloop.org 6.6.6.6\n"
90 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
93 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
94 config_register_addressmaps(get_options());
96 /* Use old interface for now, so we don't need to rewrite the unit tests */
97 #define addressmap_rewrite(a,s,eo,ao) \
98 addressmap_rewrite((a),(s), ~0, (eo),(ao))
100 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
101 strlcpy(address
, "www.invalidwildcard.com", sizeof(address
));
102 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
104 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
105 strlcpy(address
, "www.invalidasterisk.com", sizeof(address
));
106 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
108 /* Where no mapping for FQDN match on top-level domain */
109 /* MapAddress .google.com .torserver.exit */
110 strlcpy(address
, "reader.google.com", sizeof(address
));
111 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
112 tt_str_op(address
,OP_EQ
, "reader.torserver.exit");
114 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
115 strlcpy(address
, "reader.yahoo.com", sizeof(address
));
116 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
117 tt_str_op(address
,OP_EQ
, "reader.google.com.torserver.exit");
119 /*MapAddress *.cnn.com www.cnn.com */
120 strlcpy(address
, "cnn.com", sizeof(address
));
121 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
122 tt_str_op(address
,OP_EQ
, "www.cnn.com");
124 /* MapAddress .cn.com www.cnn.com */
125 strlcpy(address
, "www.cn.com", sizeof(address
));
126 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
127 tt_str_op(address
,OP_EQ
, "www.cnn.com");
129 /* MapAddress ex.com www.cnn.com - no match */
130 strlcpy(address
, "www.ex.com", sizeof(address
));
131 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
133 /* MapAddress ey.com *.cnn.com - invalid expression */
134 strlcpy(address
, "ey.com", sizeof(address
));
135 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
137 /* Where mapping for FQDN match on FQDN */
138 strlcpy(address
, "www.google.com", sizeof(address
));
139 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
140 tt_str_op(address
,OP_EQ
, "3.3.3.3");
142 strlcpy(address
, "www.torproject.org", sizeof(address
));
143 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
144 tt_str_op(address
,OP_EQ
, "1.1.1.1");
146 strlcpy(address
, "other.torproject.org", sizeof(address
));
147 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
148 tt_str_op(address
,OP_EQ
, "this.torproject.org.otherserver.exit");
150 strlcpy(address
, "test.torproject.org", sizeof(address
));
151 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
152 tt_str_op(address
,OP_EQ
, "2.2.2.2");
154 /* Test a chain of address mappings and the order in which they were added:
155 "MapAddress www.example.org 4.4.4.4"
156 "MapAddress 4.4.4.4 7.7.7.7"
157 "MapAddress 4.4.4.4 5.5.5.5"
159 strlcpy(address
, "www.example.org", sizeof(address
));
160 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
161 tt_str_op(address
,OP_EQ
, "5.5.5.5");
163 /* Test infinite address mapping results in no change */
164 strlcpy(address
, "www.infiniteloop.org", sizeof(address
));
165 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
166 tt_str_op(address
,OP_EQ
, "www.infiniteloop.org");
168 /* Test we don't find false positives */
169 strlcpy(address
, "www.example.com", sizeof(address
));
170 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
172 /* Test top-level-domain matching a bit harder */
173 config_free_lines(get_options_mutable()->AddressMap
);
174 addressmap_clear_configured();
175 strlcpy(buf
, "MapAddress *.com *.torserver.exit\n"
176 "MapAddress *.torproject.org 1.1.1.1\n"
177 "MapAddress *.net 2.2.2.2\n"
179 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
180 config_register_addressmaps(get_options());
182 strlcpy(address
, "www.abc.com", sizeof(address
));
183 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
184 tt_str_op(address
,OP_EQ
, "www.abc.torserver.exit");
186 strlcpy(address
, "www.def.com", sizeof(address
));
187 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
188 tt_str_op(address
,OP_EQ
, "www.def.torserver.exit");
190 strlcpy(address
, "www.torproject.org", sizeof(address
));
191 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
192 tt_str_op(address
,OP_EQ
, "1.1.1.1");
194 strlcpy(address
, "test.torproject.org", sizeof(address
));
195 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
196 tt_str_op(address
,OP_EQ
, "1.1.1.1");
198 strlcpy(address
, "torproject.net", sizeof(address
));
199 tt_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
200 tt_str_op(address
,OP_EQ
, "2.2.2.2");
202 /* We don't support '*' as a mapping directive */
203 config_free_lines(get_options_mutable()->AddressMap
);
204 addressmap_clear_configured();
205 strlcpy(buf
, "MapAddress * *.torserver.exit\n", sizeof(buf
));
206 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
207 config_register_addressmaps(get_options());
209 strlcpy(address
, "www.abc.com", sizeof(address
));
210 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
212 strlcpy(address
, "www.def.net", sizeof(address
));
213 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
215 strlcpy(address
, "www.torproject.org", sizeof(address
));
216 tt_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
218 #undef addressmap_rewrite
221 config_free_lines(get_options_mutable()->AddressMap
);
222 get_options_mutable()->AddressMap
= NULL
;
223 addressmap_free_all();
227 is_private_dir(const char* path
)
230 int r
= stat(path
, &st
);
234 #if !defined (_WIN32)
235 if ((st
.st_mode
& (S_IFDIR
| 0777)) != (S_IFDIR
| 0700)) {
243 test_config_check_or_create_data_subdir(void *arg
)
245 or_options_t
*options
= get_options_mutable();
247 const char *subdir
= "test_stats";
251 #if !defined (_WIN32)
252 unsigned group_permission
;
256 tor_free(options
->DataDirectory
);
257 datadir
= options
->DataDirectory
= tor_strdup(get_fname("datadir-0"));
258 subpath
= get_datadir_fname(subdir
);
261 tt_int_op(mkdir(options
->DataDirectory
), OP_EQ
, 0);
263 tt_int_op(mkdir(options
->DataDirectory
, 0700), OP_EQ
, 0);
266 r
= stat(subpath
, &st
);
268 // The subdirectory shouldn't exist yet,
269 // but should be created by the call to check_or_create_data_subdir.
270 tt_assert(r
&& (errno
== ENOENT
));
271 tt_assert(!check_or_create_data_subdir(subdir
));
272 tt_assert(is_private_dir(subpath
));
274 // The check should return 0, if the directory already exists
275 // and is private to the user.
276 tt_assert(!check_or_create_data_subdir(subdir
));
278 r
= stat(subpath
, &st
);
280 tt_abort_perror("stat");
283 #if !defined (_WIN32)
284 group_permission
= st
.st_mode
| 0070;
285 r
= chmod(subpath
, group_permission
);
288 tt_abort_perror("chmod");
291 // If the directory exists, but its mode is too permissive
292 // a call to check_or_create_data_subdir should reset the mode.
293 tt_assert(!is_private_dir(subpath
));
294 tt_assert(!check_or_create_data_subdir(subdir
));
295 tt_assert(is_private_dir(subpath
));
296 #endif /* !defined (_WIN32) */
305 test_config_write_to_data_subdir(void *arg
)
307 or_options_t
* options
= get_options_mutable();
310 const char* subdir
= "test_stats";
311 const char* fname
= "test_file";
313 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
314 "elitr, sed diam nonumy eirmod\n"
315 "tempor invidunt ut labore et dolore magna aliquyam\n"
316 "erat, sed diam voluptua.\n"
317 "At vero eos et accusam et justo duo dolores et ea\n"
318 "rebum. Stet clita kasd gubergren,\n"
319 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
320 "Lorem ipsum dolor sit amet,\n"
321 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
322 "tempor invidunt ut labore et dolore\n"
323 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
324 "accusam et justo duo dolores et\n"
325 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
326 "sanctus est Lorem ipsum dolor sit amet.";
327 char* filepath
= NULL
;
330 tor_free(options
->DataDirectory
);
331 datadir
= options
->DataDirectory
= tor_strdup(get_fname("datadir-1"));
332 filepath
= get_datadir_fname2(subdir
, fname
);
335 tt_int_op(mkdir(options
->DataDirectory
), OP_EQ
, 0);
337 tt_int_op(mkdir(options
->DataDirectory
, 0700), OP_EQ
, 0);
340 // Write attempt should fail, if subdirectory doesn't exist.
341 tt_assert(write_to_data_subdir(subdir
, fname
, str
, NULL
));
342 tt_assert(! check_or_create_data_subdir(subdir
));
344 // Content of file after write attempt should be
345 // equal to the original string.
346 tt_assert(!write_to_data_subdir(subdir
, fname
, str
, NULL
));
347 cp
= read_file_to_str(filepath
, 0, NULL
);
348 tt_str_op(cp
,OP_EQ
, str
);
351 // A second write operation should overwrite the old content.
352 tt_assert(!write_to_data_subdir(subdir
, fname
, str
, NULL
));
353 cp
= read_file_to_str(filepath
, 0, NULL
);
354 tt_str_op(cp
,OP_EQ
, str
);
358 (void) unlink(filepath
);
359 rmdir(options
->DataDirectory
);
365 /* Test helper function: Make sure that a bridge line gets parsed
366 * properly. Also make sure that the resulting bridge_line_t structure
367 * has its fields set correctly. */
369 good_bridge_line_test(const char *string
, const char *test_addrport
,
370 const char *test_digest
, const char *test_transport
,
371 const smartlist_t
*test_socks_args
)
374 bridge_line_t
*bridge_line
= parse_bridge_line(string
);
375 tt_assert(bridge_line
);
378 tmp
= tor_strdup(fmt_addrport(&bridge_line
->addr
, bridge_line
->port
));
379 tt_str_op(test_addrport
,OP_EQ
, tmp
);
382 /* If we were asked to validate a digest, but we did not get a
383 digest after parsing, we failed. */
384 if (test_digest
&& tor_digest_is_zero(bridge_line
->digest
))
387 /* If we were not asked to validate a digest, and we got a digest
388 after parsing, we failed again. */
389 if (!test_digest
&& !tor_digest_is_zero(bridge_line
->digest
))
392 /* If we were asked to validate a digest, and we got a digest after
393 parsing, make sure it's correct. */
395 tmp
= tor_strdup(hex_str(bridge_line
->digest
, DIGEST_LEN
));
397 tt_str_op(test_digest
,OP_EQ
, tmp
);
401 /* If we were asked to validate a transport name, make sure tha it
402 matches with the transport name that was parsed. */
403 if (test_transport
&& !bridge_line
->transport_name
)
405 if (!test_transport
&& bridge_line
->transport_name
)
408 tt_str_op(test_transport
,OP_EQ
, bridge_line
->transport_name
);
410 /* Validate the SOCKS argument smartlist. */
411 if (test_socks_args
&& !bridge_line
->socks_args
)
413 if (!test_socks_args
&& bridge_line
->socks_args
)
416 tt_assert(smartlist_strings_eq(test_socks_args
,
417 bridge_line
->socks_args
));
421 bridge_line_free(bridge_line
);
424 /* Test helper function: Make sure that a bridge line is
427 bad_bridge_line_test(const char *string
)
429 bridge_line_t
*bridge_line
= parse_bridge_line(string
);
431 TT_FAIL(("%s was supposed to fail, but it didn't.", string
));
432 tt_ptr_op(bridge_line
, OP_EQ
, NULL
);
435 bridge_line_free(bridge_line
);
439 test_config_parse_bridge_line(void *arg
)
442 good_bridge_line_test("192.0.2.1:4123",
443 "192.0.2.1:4123", NULL
, NULL
, NULL
);
445 good_bridge_line_test("192.0.2.1",
446 "192.0.2.1:443", NULL
, NULL
, NULL
);
448 good_bridge_line_test("transport [::1]",
449 "[::1]:443", NULL
, "transport", NULL
);
451 good_bridge_line_test("transport 192.0.2.1:12 "
452 "4352e58420e68f5e40bf7c74faddccd9d1349413",
454 "4352e58420e68f5e40bf7c74faddccd9d1349413",
458 smartlist_t
*sl_tmp
= smartlist_new();
459 smartlist_add_asprintf(sl_tmp
, "twoandtwo=five");
461 good_bridge_line_test("transport 192.0.2.1:12 "
462 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
463 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
464 "transport", sl_tmp
);
466 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
467 smartlist_free(sl_tmp
);
471 smartlist_t
*sl_tmp
= smartlist_new();
472 smartlist_add_asprintf(sl_tmp
, "twoandtwo=five");
473 smartlist_add_asprintf(sl_tmp
, "z=z");
475 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
476 "192.0.2.1:12", NULL
, "transport", sl_tmp
);
478 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
479 smartlist_free(sl_tmp
);
483 smartlist_t
*sl_tmp
= smartlist_new();
484 smartlist_add_asprintf(sl_tmp
, "dub=come");
485 smartlist_add_asprintf(sl_tmp
, "save=me");
487 good_bridge_line_test("transport 192.0.2.1:12 "
488 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
492 "4352e58420e68f5e40bf7c74faddccd9d1349666",
493 "transport", sl_tmp
);
495 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
496 smartlist_free(sl_tmp
);
499 good_bridge_line_test("192.0.2.1:1231 "
500 "4352e58420e68f5e40bf7c74faddccd9d1349413",
502 "4352e58420e68f5e40bf7c74faddccd9d1349413",
506 bad_bridge_line_test("");
507 /* bad transport name */
508 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
509 /* weird ip address */
510 bad_bridge_line_test("a.b.c.d");
512 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
513 /* no k=v in the end */
514 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
515 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
517 bad_bridge_line_test("asdw");
518 /* huge k=v value that can't fit in SOCKS fields */
519 bad_bridge_line_test(
520 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
521 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
522 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
524 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
525 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
526 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
527 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
532 test_config_parse_transport_options_line(void *arg
)
534 smartlist_t
*options_sl
= NULL
, *sl_tmp
= NULL
;
538 { /* too small line */
539 options_sl
= get_options_from_transport_options_line("valley", NULL
);
540 tt_ptr_op(options_sl
, OP_EQ
, NULL
);
543 { /* no k=v values */
544 options_sl
= get_options_from_transport_options_line("hit it!", NULL
);
545 tt_ptr_op(options_sl
, OP_EQ
, NULL
);
548 { /* correct line, but wrong transport specified */
550 get_options_from_transport_options_line("trebuchet k=v", "rook");
551 tt_ptr_op(options_sl
, OP_EQ
, NULL
);
554 { /* correct -- no transport specified */
555 sl_tmp
= smartlist_new();
556 smartlist_add_asprintf(sl_tmp
, "ladi=dadi");
557 smartlist_add_asprintf(sl_tmp
, "weliketo=party");
560 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
562 tt_assert(options_sl
);
563 tt_assert(smartlist_strings_eq(options_sl
, sl_tmp
));
565 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
566 smartlist_free(sl_tmp
);
568 SMARTLIST_FOREACH(options_sl
, char *, s
, tor_free(s
));
569 smartlist_free(options_sl
);
573 { /* correct -- correct transport specified */
574 sl_tmp
= smartlist_new();
575 smartlist_add_asprintf(sl_tmp
, "ladi=dadi");
576 smartlist_add_asprintf(sl_tmp
, "weliketo=party");
579 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
581 tt_assert(options_sl
);
582 tt_assert(smartlist_strings_eq(options_sl
, sl_tmp
));
583 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
584 smartlist_free(sl_tmp
);
586 SMARTLIST_FOREACH(options_sl
, char *, s
, tor_free(s
));
587 smartlist_free(options_sl
);
593 SMARTLIST_FOREACH(options_sl
, char *, s
, tor_free(s
));
594 smartlist_free(options_sl
);
597 SMARTLIST_FOREACH(sl_tmp
, char *, s
, tor_free(s
));
598 smartlist_free(sl_tmp
);
602 /* Mocks needed for the compute_max_mem_in_queues test */
603 static int get_total_system_memory_mock(size_t *mem_out
);
605 static size_t total_system_memory_output
= 0;
606 static int total_system_memory_return
= 0;
609 get_total_system_memory_mock(size_t *mem_out
)
614 *mem_out
= total_system_memory_output
;
615 return total_system_memory_return
;
618 /* Mocks needed for the transport plugin line test */
620 static void pt_kickstart_proxy_mock(const smartlist_t
*transport_list
,
621 char **proxy_argv
, int is_server
);
622 static int transport_add_from_config_mock(const tor_addr_t
*addr
,
623 uint16_t port
, const char *name
,
625 static int transport_is_needed_mock(const char *transport_name
);
627 static int pt_kickstart_proxy_mock_call_count
= 0;
628 static int transport_add_from_config_mock_call_count
= 0;
629 static int transport_is_needed_mock_call_count
= 0;
630 static int transport_is_needed_mock_return
= 0;
633 pt_kickstart_proxy_mock(const smartlist_t
*transport_list
,
634 char **proxy_argv
, int is_server
)
636 (void) transport_list
;
639 /* XXXX check that args are as expected. */
641 ++pt_kickstart_proxy_mock_call_count
;
643 free_execve_args(proxy_argv
);
647 transport_add_from_config_mock(const tor_addr_t
*addr
,
648 uint16_t port
, const char *name
,
655 /* XXXX check that args are as expected. */
657 ++transport_add_from_config_mock_call_count
;
663 transport_is_needed_mock(const char *transport_name
)
665 (void) transport_name
;
666 /* XXXX check that arg is as expected. */
668 ++transport_is_needed_mock_call_count
;
670 return transport_is_needed_mock_return
;
674 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
679 test_config_parse_transport_plugin_line(void *arg
)
683 or_options_t
*options
= get_options_mutable();
685 int old_pt_kickstart_proxy_mock_call_count
;
686 int old_transport_add_from_config_mock_call_count
;
687 int old_transport_is_needed_mock_call_count
;
689 /* Bad transport lines - too short */
690 r
= parse_transport_line(options
, "bad", 1, 0);
691 tt_int_op(r
, OP_LT
, 0);
692 r
= parse_transport_line(options
, "bad", 1, 1);
693 tt_int_op(r
, OP_LT
, 0);
694 r
= parse_transport_line(options
, "bad bad", 1, 0);
695 tt_int_op(r
, OP_LT
, 0);
696 r
= parse_transport_line(options
, "bad bad", 1, 1);
697 tt_int_op(r
, OP_LT
, 0);
699 /* Test transport list parsing */
700 r
= parse_transport_line(options
,
701 "transport_1 exec /usr/bin/fake-transport", 1, 0);
702 tt_int_op(r
, OP_EQ
, 0);
703 r
= parse_transport_line(options
,
704 "transport_1 exec /usr/bin/fake-transport", 1, 1);
705 tt_int_op(r
, OP_EQ
, 0);
706 r
= parse_transport_line(options
,
707 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
708 tt_int_op(r
, OP_EQ
, 0);
709 r
= parse_transport_line(options
,
710 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
711 tt_int_op(r
, OP_EQ
, 0);
712 /* Bad transport identifiers */
713 r
= parse_transport_line(options
,
714 "transport_* exec /usr/bin/fake-transport", 1, 0);
715 tt_int_op(r
, OP_LT
, 0);
716 r
= parse_transport_line(options
,
717 "transport_* exec /usr/bin/fake-transport", 1, 1);
718 tt_int_op(r
, OP_LT
, 0);
720 /* Check SOCKS cases for client transport */
721 r
= parse_transport_line(options
,
722 "transport_1 socks4 1.2.3.4:567", 1, 0);
723 tt_int_op(r
, OP_EQ
, 0);
724 r
= parse_transport_line(options
,
725 "transport_1 socks5 1.2.3.4:567", 1, 0);
726 tt_int_op(r
, OP_EQ
, 0);
727 /* Proxy case for server transport */
728 r
= parse_transport_line(options
,
729 "transport_1 proxy 1.2.3.4:567", 1, 1);
730 tt_int_op(r
, OP_EQ
, 0);
731 /* Multiple-transport error exit */
732 r
= parse_transport_line(options
,
733 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
734 tt_int_op(r
, OP_LT
, 0);
735 r
= parse_transport_line(options
,
736 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
737 tt_int_op(r
, OP_LT
, 0);
738 /* No port error exit */
739 r
= parse_transport_line(options
,
740 "transport_1 socks5 1.2.3.4", 1, 0);
741 tt_int_op(r
, OP_LT
, 0);
742 r
= parse_transport_line(options
,
743 "transport_1 proxy 1.2.3.4", 1, 1);
744 tt_int_op(r
, OP_LT
, 0);
745 /* Unparsable address error exit */
746 r
= parse_transport_line(options
,
747 "transport_1 socks5 1.2.3:6x7", 1, 0);
748 tt_int_op(r
, OP_LT
, 0);
749 r
= parse_transport_line(options
,
750 "transport_1 proxy 1.2.3:6x7", 1, 1);
751 tt_int_op(r
, OP_LT
, 0);
753 /* "Strange {Client|Server}TransportPlugin field" error exit */
754 r
= parse_transport_line(options
,
755 "transport_1 foo bar", 1, 0);
756 tt_int_op(r
, OP_LT
, 0);
757 r
= parse_transport_line(options
,
758 "transport_1 foo bar", 1, 1);
759 tt_int_op(r
, OP_LT
, 0);
761 /* No sandbox mode error exit */
762 tmp
= options
->Sandbox
;
763 options
->Sandbox
= 1;
764 r
= parse_transport_line(options
,
765 "transport_1 exec /usr/bin/fake-transport", 1, 0);
766 tt_int_op(r
, OP_LT
, 0);
767 r
= parse_transport_line(options
,
768 "transport_1 exec /usr/bin/fake-transport", 1, 1);
769 tt_int_op(r
, OP_LT
, 0);
770 options
->Sandbox
= tmp
;
773 * These final test cases cover code paths that only activate without
774 * validate_only, so they need mocks in place.
776 MOCK(pt_kickstart_proxy
, pt_kickstart_proxy_mock
);
777 old_pt_kickstart_proxy_mock_call_count
=
778 pt_kickstart_proxy_mock_call_count
;
779 r
= parse_transport_line(options
,
780 "transport_1 exec /usr/bin/fake-transport", 0, 1);
781 tt_int_op(r
, OP_EQ
, 0);
782 tt_assert(pt_kickstart_proxy_mock_call_count
==
783 old_pt_kickstart_proxy_mock_call_count
+ 1);
784 UNMOCK(pt_kickstart_proxy
);
786 /* This one hits a log line in the !validate_only case only */
787 r
= parse_transport_line(options
,
788 "transport_1 proxy 1.2.3.4:567", 0, 1);
789 tt_int_op(r
, OP_EQ
, 0);
791 /* Check mocked client transport cases */
792 MOCK(pt_kickstart_proxy
, pt_kickstart_proxy_mock
);
793 MOCK(transport_add_from_config
, transport_add_from_config_mock
);
794 MOCK(transport_is_needed
, transport_is_needed_mock
);
796 /* Unnecessary transport case */
797 transport_is_needed_mock_return
= 0;
798 old_pt_kickstart_proxy_mock_call_count
=
799 pt_kickstart_proxy_mock_call_count
;
800 old_transport_add_from_config_mock_call_count
=
801 transport_add_from_config_mock_call_count
;
802 old_transport_is_needed_mock_call_count
=
803 transport_is_needed_mock_call_count
;
804 r
= parse_transport_line(options
,
805 "transport_1 exec /usr/bin/fake-transport", 0, 0);
806 /* Should have succeeded */
807 tt_int_op(r
, OP_EQ
, 0);
808 /* transport_is_needed() should have been called */
809 tt_assert(transport_is_needed_mock_call_count
==
810 old_transport_is_needed_mock_call_count
+ 1);
812 * pt_kickstart_proxy() and transport_add_from_config() should
813 * not have been called.
815 tt_assert(pt_kickstart_proxy_mock_call_count
==
816 old_pt_kickstart_proxy_mock_call_count
);
817 tt_assert(transport_add_from_config_mock_call_count
==
818 old_transport_add_from_config_mock_call_count
);
820 /* Necessary transport case */
821 transport_is_needed_mock_return
= 1;
822 old_pt_kickstart_proxy_mock_call_count
=
823 pt_kickstart_proxy_mock_call_count
;
824 old_transport_add_from_config_mock_call_count
=
825 transport_add_from_config_mock_call_count
;
826 old_transport_is_needed_mock_call_count
=
827 transport_is_needed_mock_call_count
;
828 r
= parse_transport_line(options
,
829 "transport_1 exec /usr/bin/fake-transport", 0, 0);
830 /* Should have succeeded */
831 tt_int_op(r
, OP_EQ
, 0);
833 * transport_is_needed() and pt_kickstart_proxy() should have been
836 tt_assert(pt_kickstart_proxy_mock_call_count
==
837 old_pt_kickstart_proxy_mock_call_count
+ 1);
838 tt_assert(transport_is_needed_mock_call_count
==
839 old_transport_is_needed_mock_call_count
+ 1);
840 /* transport_add_from_config() should not have been called. */
841 tt_assert(transport_add_from_config_mock_call_count
==
842 old_transport_add_from_config_mock_call_count
);
845 transport_is_needed_mock_return
= 1;
846 old_pt_kickstart_proxy_mock_call_count
=
847 pt_kickstart_proxy_mock_call_count
;
848 old_transport_add_from_config_mock_call_count
=
849 transport_add_from_config_mock_call_count
;
850 old_transport_is_needed_mock_call_count
=
851 transport_is_needed_mock_call_count
;
852 r
= parse_transport_line(options
,
853 "transport_1 socks5 1.2.3.4:567", 0, 0);
854 /* Should have succeeded */
855 tt_int_op(r
, OP_EQ
, 0);
857 * transport_is_needed() and transport_add_from_config() should have
860 tt_assert(transport_add_from_config_mock_call_count
==
861 old_transport_add_from_config_mock_call_count
+ 1);
862 tt_assert(transport_is_needed_mock_call_count
==
863 old_transport_is_needed_mock_call_count
+ 1);
864 /* pt_kickstart_proxy() should not have been called. */
865 tt_assert(pt_kickstart_proxy_mock_call_count
==
866 old_pt_kickstart_proxy_mock_call_count
);
868 /* Done with mocked client transport cases */
869 UNMOCK(transport_is_needed
);
870 UNMOCK(transport_add_from_config
);
871 UNMOCK(pt_kickstart_proxy
);
874 /* Make sure we undo all mocks */
875 UNMOCK(pt_kickstart_proxy
);
876 UNMOCK(transport_add_from_config
);
877 UNMOCK(transport_is_needed
);
882 // Tests if an options with MyFamily fingerprints missing '$' normalises
883 // them correctly and also ensure it also works with multiple fingerprints
885 test_config_fix_my_family(void *arg
)
888 config_line_t
*family
= tor_malloc_zero(sizeof(config_line_t
));
889 family
->key
= tor_strdup("MyFamily");
890 family
->value
= tor_strdup("$1111111111111111111111111111111111111111, "
891 "1111111111111111111111111111111111111112, "
892 "$1111111111111111111111111111111111111113");
894 config_line_t
*family2
= tor_malloc_zero(sizeof(config_line_t
));
895 family2
->key
= tor_strdup("MyFamily");
896 family2
->value
= tor_strdup("1111111111111111111111111111111111111114");
898 config_line_t
*family3
= tor_malloc_zero(sizeof(config_line_t
));
899 family3
->key
= tor_strdup("MyFamily");
900 family3
->value
= tor_strdup("$1111111111111111111111111111111111111115");
902 family
->next
= family2
;
903 family2
->next
= family3
;
904 family3
->next
= NULL
;
906 or_options_t
* options
= options_new();
907 or_options_t
* defaults
= options_new();
910 options_init(options
);
911 options_init(defaults
);
912 options
->MyFamily_lines
= family
;
914 options_validate(NULL
, options
, defaults
, 0, &err
) ;
917 TT_FAIL(("options_validate failed: %s", err
));
920 const char *valid
[] = { "$1111111111111111111111111111111111111111",
921 "$1111111111111111111111111111111111111112",
922 "$1111111111111111111111111111111111111113",
923 "$1111111111111111111111111111111111111114",
924 "$1111111111111111111111111111111111111115" };
927 for (ret
= options
->MyFamily
; ret
&& ret_size
< 5; ret
= ret
->next
) {
928 tt_str_op(ret
->value
, OP_EQ
, valid
[ret_size
]);
931 tt_int_op(ret_size
, OP_EQ
, 5);
935 or_options_free(options
);
936 or_options_free(defaults
);
939 static int n_hostname_01010101
= 0;
941 /** This mock function is meant to replace tor_lookup_hostname().
942 * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
943 * This function increments <b>n_hostname_01010101</b> counter by one
944 * every time it is called.
947 tor_lookup_hostname_01010101(const char *name
, uint32_t *addr
)
949 n_hostname_01010101
++;
952 *addr
= ntohl(0x01010101);
958 static int n_hostname_localhost
= 0;
960 /** This mock function is meant to replace tor_lookup_hostname().
961 * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
962 * This function increments <b>n_hostname_localhost</b> counter by one
963 * every time it is called.
966 tor_lookup_hostname_localhost(const char *name
, uint32_t *addr
)
968 n_hostname_localhost
++;
977 static int n_hostname_failure
= 0;
979 /** This mock function is meant to replace tor_lookup_hostname().
980 * It pretends to fail by returning -1 to caller. Also, this function
981 * increments <b>n_hostname_failure</b> every time it is called.
984 tor_lookup_hostname_failure(const char *name
, uint32_t *addr
)
989 n_hostname_failure
++;
994 static int n_gethostname_replacement
= 0;
996 /** This mock function is meant to replace tor_gethostname(). It
997 * responds with string "onionrouter!" as hostname. This function
998 * increments <b>n_gethostname_replacement</b> by one every time
1002 tor_gethostname_replacement(char *name
, size_t namelen
)
1004 n_gethostname_replacement
++;
1006 if (name
&& namelen
) {
1007 strlcpy(name
,"onionrouter!",namelen
);
1013 static int n_gethostname_localhost
= 0;
1015 /** This mock function is meant to replace tor_gethostname(). It
1016 * responds with string "127.0.0.1" as hostname. This function
1017 * increments <b>n_gethostname_localhost</b> by one every time
1021 tor_gethostname_localhost(char *name
, size_t namelen
)
1023 n_gethostname_localhost
++;
1025 if (name
&& namelen
) {
1026 strlcpy(name
,"127.0.0.1",namelen
);
1032 static int n_gethostname_failure
= 0;
1034 /** This mock function is meant to replace tor_gethostname.
1035 * It pretends to fail by returning -1. This function increments
1036 * <b>n_gethostname_failure</b> by one every time it is called.
1039 tor_gethostname_failure(char *name
, size_t namelen
)
1043 n_gethostname_failure
++;
1048 static int n_get_interface_address
= 0;
1050 /** This mock function is meant to replace get_interface_address().
1051 * It answers with address 8.8.8.8. This function increments
1052 * <b>n_get_interface_address</b> by one every time it is called.
1055 get_interface_address_08080808(int severity
, uint32_t *addr
)
1059 n_get_interface_address
++;
1062 *addr
= ntohl(0x08080808);
1068 static int n_get_interface_address6
= 0;
1069 static sa_family_t last_address6_family
;
1071 /** This mock function is meant to replace get_interface_address6().
1072 * It answers with IP address 9.9.9.9 iff both of the following are true:
1073 * - <b>family</b> is AF_INET
1074 * - <b>addr</b> pointer is not NULL.
1075 * This function increments <b>n_get_interface_address6</b> by one every
1076 * time it is called.
1079 get_interface_address6_replacement(int severity
, sa_family_t family
,
1084 last_address6_family
= family
;
1085 n_get_interface_address6
++;
1087 if ((family
!= AF_INET
) || !addr
) {
1091 tor_addr_from_ipv4h(addr
,0x09090909);
1096 static int n_get_interface_address_failure
= 0;
1099 * This mock function is meant to replace get_interface_address().
1100 * It pretends to fail getting interface address by returning -1.
1101 * <b>n_get_interface_address_failure</b> is incremented by one
1102 * every time this function is called.
1105 get_interface_address_failure(int severity
, uint32_t *addr
)
1110 n_get_interface_address_failure
++;
1115 static int n_get_interface_address6_failure
= 0;
1118 * This mock function is meant to replace get_interface_addres6().
1119 * It will pretend to fail by return -1.
1120 * <b>n_get_interface_address6_failure</b> is incremented by one
1121 * every time this function is called and <b>last_address6_family</b>
1122 * is assigned the value of <b>family</b> argument.
1125 get_interface_address6_failure(int severity
, sa_family_t family
,
1130 n_get_interface_address6_failure
++;
1131 last_address6_family
= family
;
1137 test_config_resolve_my_address(void *arg
)
1139 or_options_t
*options
;
1140 uint32_t resolved_addr
;
1141 const char *method_used
;
1142 char *hostname_out
= NULL
;
1144 int prev_n_hostname_01010101
;
1145 int prev_n_hostname_localhost
;
1146 int prev_n_hostname_failure
;
1147 int prev_n_gethostname_replacement
;
1148 int prev_n_gethostname_failure
;
1149 int prev_n_gethostname_localhost
;
1150 int prev_n_get_interface_address
;
1151 int prev_n_get_interface_address_failure
;
1152 int prev_n_get_interface_address6
;
1153 int prev_n_get_interface_address6_failure
;
1157 options
= options_new();
1159 options_init(options
);
1163 * If options->Address is a valid IPv4 address string, we want
1164 * the corresponding address to be parsed and returned.
1167 options
->Address
= tor_strdup("128.52.128.105");
1169 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1170 &method_used
,&hostname_out
);
1172 tt_want(retval
== 0);
1173 tt_want_str_op(method_used
,OP_EQ
,"CONFIGURED");
1174 tt_want(hostname_out
== NULL
);
1175 tt_assert(resolved_addr
== 0x80348069);
1177 tor_free(options
->Address
);
1181 * If options->Address is a valid DNS address, we want resolve_my_address()
1182 * function to ask tor_lookup_hostname() for help with resolving it
1183 * and return the address that was resolved (in host order).
1186 MOCK(tor_lookup_hostname
,tor_lookup_hostname_01010101
);
1188 tor_free(options
->Address
);
1189 options
->Address
= tor_strdup("www.torproject.org");
1191 prev_n_hostname_01010101
= n_hostname_01010101
;
1193 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1194 &method_used
,&hostname_out
);
1196 tt_want(retval
== 0);
1197 tt_want(n_hostname_01010101
== prev_n_hostname_01010101
+ 1);
1198 tt_want_str_op(method_used
,OP_EQ
,"RESOLVED");
1199 tt_want_str_op(hostname_out
,OP_EQ
,"www.torproject.org");
1200 tt_assert(resolved_addr
== 0x01010101);
1202 UNMOCK(tor_lookup_hostname
);
1204 tor_free(options
->Address
);
1205 tor_free(hostname_out
);
1209 * Given that options->Address is NULL, we want resolve_my_address()
1210 * to try and use tor_gethostname() to get hostname AND use
1211 * tor_lookup_hostname() to get IP address.
1215 tor_free(options
->Address
);
1216 options
->Address
= NULL
;
1218 MOCK(tor_gethostname
,tor_gethostname_replacement
);
1219 MOCK(tor_lookup_hostname
,tor_lookup_hostname_01010101
);
1221 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1222 prev_n_hostname_01010101
= n_hostname_01010101
;
1224 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1225 &method_used
,&hostname_out
);
1227 tt_want(retval
== 0);
1228 tt_want(n_gethostname_replacement
== prev_n_gethostname_replacement
+ 1);
1229 tt_want(n_hostname_01010101
== prev_n_hostname_01010101
+ 1);
1230 tt_want_str_op(method_used
,OP_EQ
,"GETHOSTNAME");
1231 tt_want_str_op(hostname_out
,OP_EQ
,"onionrouter!");
1232 tt_assert(resolved_addr
== 0x01010101);
1234 UNMOCK(tor_gethostname
);
1235 UNMOCK(tor_lookup_hostname
);
1237 tor_free(hostname_out
);
1241 * Given that options->Address is a local host address, we want
1242 * resolve_my_address() function to fail.
1246 tor_free(options
->Address
);
1247 options
->Address
= tor_strdup("127.0.0.1");
1249 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1250 &method_used
,&hostname_out
);
1252 tt_want(resolved_addr
== 0);
1253 tt_int_op(retval
, OP_EQ
, -1);
1255 tor_free(options
->Address
);
1256 tor_free(hostname_out
);
1260 * We want resolve_my_address() to fail if DNS address in options->Address
1261 * cannot be resolved.
1264 MOCK(tor_lookup_hostname
,tor_lookup_hostname_failure
);
1266 prev_n_hostname_failure
= n_hostname_failure
;
1268 tor_free(options
->Address
);
1269 options
->Address
= tor_strdup("www.tor-project.org");
1271 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1272 &method_used
,&hostname_out
);
1274 tt_want(n_hostname_failure
== prev_n_hostname_failure
+ 1);
1275 tt_int_op(retval
, OP_EQ
, -1);
1277 UNMOCK(tor_lookup_hostname
);
1279 tor_free(options
->Address
);
1280 tor_free(hostname_out
);
1284 * If options->Address is NULL AND gettting local hostname fails, we want
1285 * resolve_my_address() to fail as well.
1288 MOCK(tor_gethostname
,tor_gethostname_failure
);
1290 prev_n_gethostname_failure
= n_gethostname_failure
;
1292 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1293 &method_used
,&hostname_out
);
1295 tt_want(n_gethostname_failure
== prev_n_gethostname_failure
+ 1);
1296 tt_int_op(retval
, OP_EQ
, -1);
1298 UNMOCK(tor_gethostname
);
1299 tor_free(hostname_out
);
1303 * We want resolve_my_address() to try and get network interface address via
1304 * get_interface_address() if hostname returned by tor_gethostname() cannot be
1305 * resolved into IP address.
1308 MOCK(tor_gethostname
,tor_gethostname_replacement
);
1309 MOCK(tor_lookup_hostname
,tor_lookup_hostname_failure
);
1310 MOCK(get_interface_address
,get_interface_address_08080808
);
1312 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1313 prev_n_get_interface_address
= n_get_interface_address
;
1315 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1316 &method_used
,&hostname_out
);
1318 tt_want(retval
== 0);
1319 tt_want_int_op(n_gethostname_replacement
, OP_EQ
,
1320 prev_n_gethostname_replacement
+ 1);
1321 tt_want_int_op(n_get_interface_address
, OP_EQ
,
1322 prev_n_get_interface_address
+ 1);
1323 tt_want_str_op(method_used
,OP_EQ
,"INTERFACE");
1324 tt_want(hostname_out
== NULL
);
1325 tt_assert(resolved_addr
== 0x08080808);
1327 UNMOCK(get_interface_address
);
1328 tor_free(hostname_out
);
1332 * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
1333 * is unresolvable. We want resolve_my_address to fail if
1334 * get_interface_address() fails.
1337 MOCK(get_interface_address
,get_interface_address_failure
);
1339 prev_n_get_interface_address_failure
= n_get_interface_address_failure
;
1340 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1342 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1343 &method_used
,&hostname_out
);
1345 tt_want(n_get_interface_address_failure
==
1346 prev_n_get_interface_address_failure
+ 1);
1347 tt_want(n_gethostname_replacement
==
1348 prev_n_gethostname_replacement
+ 1);
1349 tt_int_op(retval
, OP_EQ
, -1);
1351 UNMOCK(get_interface_address
);
1352 tor_free(hostname_out
);
1356 * Given that options->Address is NULL AND tor_lookup_hostname()
1357 * fails AND hostname returned by gethostname() resolves
1358 * to local IP address, we want resolve_my_address() function to
1359 * call get_interface_address6(.,AF_INET,.) and return IP address
1360 * the latter function has found.
1363 MOCK(tor_lookup_hostname
,tor_lookup_hostname_failure
);
1364 MOCK(tor_gethostname
,tor_gethostname_replacement
);
1365 MOCK(get_interface_address6
,get_interface_address6_replacement
);
1367 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1368 prev_n_hostname_failure
= n_hostname_failure
;
1369 prev_n_get_interface_address6
= n_get_interface_address6
;
1371 retval
= resolve_my_address(LOG_NOTICE
,options
,&resolved_addr
,
1372 &method_used
,&hostname_out
);
1374 tt_want(last_address6_family
== AF_INET
);
1375 tt_want(n_get_interface_address6
== prev_n_get_interface_address6
+ 1);
1376 tt_want(n_hostname_failure
== prev_n_hostname_failure
+ 1);
1377 tt_want(n_gethostname_replacement
== prev_n_gethostname_replacement
+ 1);
1378 tt_want(retval
== 0);
1379 tt_want_str_op(method_used
,OP_EQ
,"INTERFACE");
1380 tt_assert(resolved_addr
== 0x09090909);
1382 UNMOCK(tor_lookup_hostname
);
1383 UNMOCK(tor_gethostname
);
1384 UNMOCK(get_interface_address6
);
1386 tor_free(hostname_out
);
1389 * CASE 10: We want resolve_my_address() to fail if all of the following
1391 * 1. options->Address is not NULL
1392 * 2. ... but it cannot be converted to struct in_addr by
1394 * 3. ... and tor_lookup_hostname() fails to resolve the
1398 MOCK(tor_lookup_hostname
,tor_lookup_hostname_failure
);
1400 prev_n_hostname_failure
= n_hostname_failure
;
1402 tor_free(options
->Address
);
1403 options
->Address
= tor_strdup("some_hostname");
1405 retval
= resolve_my_address(LOG_NOTICE
, options
, &resolved_addr
,
1406 &method_used
,&hostname_out
);
1408 tt_want(n_hostname_failure
== prev_n_hostname_failure
+ 1);
1409 tt_int_op(retval
, OP_EQ
, -1);
1411 UNMOCK(tor_gethostname
);
1412 UNMOCK(tor_lookup_hostname
);
1414 tor_free(hostname_out
);
1418 * Suppose the following sequence of events:
1419 * 1. options->Address is NULL
1420 * 2. tor_gethostname() succeeds to get hostname of machine Tor
1422 * 3. Hostname from previous step cannot be converted to
1423 * address by using tor_inet_aton() function.
1424 * 4. However, tor_lookup_hostname() succeeds in resolving the
1425 * hostname from step 2.
1426 * 5. Unfortunately, tor_addr_is_internal() deems this address
1428 * 6. get_interface_address6(.,AF_INET,.) returns non-internal
1431 * We want resolve_my_addr() to succeed with method "INTERFACE"
1432 * and address from step 6.
1435 tor_free(options
->Address
);
1436 options
->Address
= NULL
;
1438 MOCK(tor_gethostname
,tor_gethostname_replacement
);
1439 MOCK(tor_lookup_hostname
,tor_lookup_hostname_localhost
);
1440 MOCK(get_interface_address6
,get_interface_address6_replacement
);
1442 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1443 prev_n_hostname_localhost
= n_hostname_localhost
;
1444 prev_n_get_interface_address6
= n_get_interface_address6
;
1446 retval
= resolve_my_address(LOG_DEBUG
, options
, &resolved_addr
,
1447 &method_used
,&hostname_out
);
1449 tt_want(n_gethostname_replacement
== prev_n_gethostname_replacement
+ 1);
1450 tt_want(n_hostname_localhost
== prev_n_hostname_localhost
+ 1);
1451 tt_want(n_get_interface_address6
== prev_n_get_interface_address6
+ 1);
1453 tt_str_op(method_used
,OP_EQ
,"INTERFACE");
1454 tt_ptr_op(hostname_out
, OP_EQ
, NULL
);
1455 tt_int_op(retval
, OP_EQ
, 0);
1460 * 6. get_interface_address6() fails.
1462 * In this subcase, we want resolve_my_address() to fail.
1465 UNMOCK(get_interface_address6
);
1466 MOCK(get_interface_address6
,get_interface_address6_failure
);
1468 prev_n_gethostname_replacement
= n_gethostname_replacement
;
1469 prev_n_hostname_localhost
= n_hostname_localhost
;
1470 prev_n_get_interface_address6_failure
= n_get_interface_address6_failure
;
1472 retval
= resolve_my_address(LOG_DEBUG
, options
, &resolved_addr
,
1473 &method_used
,&hostname_out
);
1475 tt_want(n_gethostname_replacement
== prev_n_gethostname_replacement
+ 1);
1476 tt_want(n_hostname_localhost
== prev_n_hostname_localhost
+ 1);
1477 tt_want(n_get_interface_address6_failure
==
1478 prev_n_get_interface_address6_failure
+ 1);
1480 tt_int_op(retval
, OP_EQ
, -1);
1482 UNMOCK(tor_gethostname
);
1483 UNMOCK(tor_lookup_hostname
);
1484 UNMOCK(get_interface_address6
);
1487 * Suppose the following happens:
1488 * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
1489 * 2. tor_gethostname() succeeds in getting hostname of a machine ...
1490 * 3. ... which is successfully parsed by tor_inet_aton() ...
1491 * 4. into IPv4 address that tor_addr_is_inernal() considers to be
1494 * In this case, we want resolve_my_address() to fail.
1497 tor_free(options
->Address
);
1498 options
->Address
= NULL
;
1499 options
->DirAuthorities
= tor_malloc_zero(sizeof(config_line_t
));
1501 MOCK(tor_gethostname
,tor_gethostname_localhost
);
1503 prev_n_gethostname_localhost
= n_gethostname_localhost
;
1505 retval
= resolve_my_address(LOG_DEBUG
, options
, &resolved_addr
,
1506 &method_used
,&hostname_out
);
1508 tt_want(n_gethostname_localhost
== prev_n_gethostname_localhost
+ 1);
1509 tt_int_op(retval
, OP_EQ
, -1);
1511 UNMOCK(tor_gethostname
);
1514 tor_free(options
->Address
);
1515 tor_free(options
->DirAuthorities
);
1516 or_options_free(options
);
1517 tor_free(hostname_out
);
1519 UNMOCK(tor_gethostname
);
1520 UNMOCK(tor_lookup_hostname
);
1521 UNMOCK(get_interface_address
);
1522 UNMOCK(get_interface_address6
);
1523 UNMOCK(tor_gethostname
);
1527 test_config_adding_trusted_dir_server(void *arg
)
1531 const char digest
[DIGEST_LEN
] = "";
1532 dir_server_t
*ds
= NULL
;
1533 tor_addr_port_t ipv6
;
1536 clear_dir_servers();
1537 routerlist_free_all();
1539 /* create a trusted ds without an IPv6 address and port */
1540 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1541 NULL
, V3_DIRINFO
, 1.0);
1544 tt_int_op(get_n_authorities(V3_DIRINFO
), OP_EQ
, 1);
1545 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 1);
1547 /* create a trusted ds with an IPv6 address and port */
1548 rv
= tor_addr_port_parse(LOG_WARN
, "[::1]:9061", &ipv6
.addr
, &ipv6
.port
, -1);
1549 tt_int_op(rv
, OP_EQ
, 0);
1550 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6
, digest
,
1551 NULL
, V3_DIRINFO
, 1.0);
1554 tt_int_op(get_n_authorities(V3_DIRINFO
), OP_EQ
, 2);
1555 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 2);
1558 clear_dir_servers();
1559 routerlist_free_all();
1563 test_config_adding_fallback_dir_server(void *arg
)
1567 const char digest
[DIGEST_LEN
] = "";
1568 dir_server_t
*ds
= NULL
;
1570 tor_addr_port_t ipv6
;
1573 clear_dir_servers();
1574 routerlist_free_all();
1576 rv
= tor_addr_parse(&ipv4
, "127.0.0.1");
1577 tt_int_op(rv
, OP_EQ
, AF_INET
);
1579 /* create a trusted ds without an IPv6 address and port */
1580 ds
= fallback_dir_server_new(&ipv4
, 9059, 9060, NULL
, digest
, 1.0);
1583 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 1);
1585 /* create a trusted ds with an IPv6 address and port */
1586 rv
= tor_addr_port_parse(LOG_WARN
, "[::1]:9061", &ipv6
.addr
, &ipv6
.port
, -1);
1587 tt_int_op(rv
, OP_EQ
, 0);
1588 ds
= fallback_dir_server_new(&ipv4
, 9059, 9060, &ipv6
, digest
, 1.0);
1591 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 2);
1594 clear_dir_servers();
1595 routerlist_free_all();
1599 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1600 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1603 #define TEST_DIR_AUTH_LINE_START \
1604 "foobar orport=12345 " \
1605 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1606 #define TEST_DIR_AUTH_LINE_END \
1608 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1609 #define TEST_DIR_AUTH_IPV6_FLAG \
1610 "ipv6=[feed::beef]:9 "
1613 test_config_parsing_trusted_dir_server(void *arg
)
1618 /* parse a trusted dir server without an IPv6 address and port */
1619 rv
= parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1620 TEST_DIR_AUTH_LINE_END
,
1622 tt_int_op(rv
, OP_EQ
, 0);
1624 /* parse a trusted dir server with an IPv6 address and port */
1625 rv
= parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1626 TEST_DIR_AUTH_IPV6_FLAG
1627 TEST_DIR_AUTH_LINE_END
,
1629 tt_int_op(rv
, OP_EQ
, 0);
1631 /* Since we are only validating, there is no cleanup. */
1636 #undef TEST_DIR_AUTH_LINE_START
1637 #undef TEST_DIR_AUTH_LINE_END
1638 #undef TEST_DIR_AUTH_IPV6_FLAG
1640 #define TEST_DIR_AUTH_LINE_START \
1641 "foobar orport=12345 " \
1642 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1643 #define TEST_DIR_AUTH_LINE_END_BAD_IP \
1644 "0.256.3.4:54321 " \
1645 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1646 #define TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR \
1647 "torproject.org:54321 " \
1648 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1651 test_config_parsing_invalid_dir_address(void *arg
)
1656 rv
= parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1657 TEST_DIR_AUTH_LINE_END_BAD_IP
,
1659 tt_int_op(rv
, OP_EQ
, -1);
1661 rv
= parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1662 TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR
,
1664 tt_int_op(rv
, OP_EQ
, -1);
1670 #undef TEST_DIR_AUTH_LINE_START
1671 #undef TEST_DIR_AUTH_LINE_END_BAD_IP
1672 #undef TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR
1675 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1677 #define TEST_DIR_FALLBACK_LINE \
1678 "1.2.3.4:54321 orport=12345 " \
1679 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1680 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1681 "ipv6=[2015:c0de::deed]:9"
1684 test_config_parsing_fallback_dir_server(void *arg
)
1689 /* parse a trusted dir server without an IPv6 address and port */
1690 rv
= parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
, 1);
1691 tt_int_op(rv
, OP_EQ
, 0);
1693 /* parse a trusted dir server with an IPv6 address and port */
1694 rv
= parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1695 TEST_DIR_FALLBACK_IPV6_FLAG
,
1697 tt_int_op(rv
, OP_EQ
, 0);
1699 /* Since we are only validating, there is no cleanup. */
1704 #undef TEST_DIR_FALLBACK_LINE
1705 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1708 test_config_adding_default_trusted_dir_servers(void *arg
)
1712 clear_dir_servers();
1713 routerlist_free_all();
1715 /* Assume we only have one bridge authority */
1716 add_default_trusted_dir_authorities(BRIDGE_DIRINFO
);
1717 tt_int_op(get_n_authorities(BRIDGE_DIRINFO
), OP_EQ
, 1);
1718 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 1);
1720 /* Assume we have eight V3 authorities */
1721 add_default_trusted_dir_authorities(V3_DIRINFO
);
1722 tt_int_op(get_n_authorities(V3_DIRINFO
), OP_EQ
, 9);
1723 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ
, 10);
1726 clear_dir_servers();
1727 routerlist_free_all();
1730 static int n_add_default_fallback_dir_servers_known_default
= 0;
1733 * This mock function is meant to replace add_default_fallback_dir_servers().
1734 * It will parse and add one known default fallback dir server,
1735 * which has a dir_port of 99.
1736 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
1737 * one every time this function is called.
1740 add_default_fallback_dir_servers_known_default(void)
1743 const char *fallback
[] = {
1744 "127.0.0.1:60099 orport=9009 "
1745 "id=0923456789012345678901234567890123456789",
1748 for (i
=0; fallback
[i
]; i
++) {
1749 if (parse_dir_fallback_line(fallback
[i
], 0)<0) {
1750 log_err(LD_BUG
, "Couldn't parse internal FallbackDir line %s",
1754 n_add_default_fallback_dir_servers_known_default
++;
1757 /* Test all the different combinations of adding dir servers */
1759 test_config_adding_dir_servers(void *arg
)
1763 /* allocate options */
1764 or_options_t
*options
= tor_malloc_zero(sizeof(or_options_t
));
1766 /* Allocate and populate configuration lines:
1768 * Use the same format as the hard-coded directories in
1769 * add_default_trusted_dir_authorities().
1770 * Zeroing the structure has the same effect as initialising to:
1771 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
1773 config_line_t
*test_dir_authority
= tor_malloc_zero(sizeof(config_line_t
));
1774 test_dir_authority
->key
= tor_strdup("DirAuthority");
1775 test_dir_authority
->value
= tor_strdup(
1777 "v3ident=0023456789012345678901234567890123456789 "
1778 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1781 config_line_t
*test_alt_bridge_authority
= tor_malloc_zero(
1782 sizeof(config_line_t
));
1783 test_alt_bridge_authority
->key
= tor_strdup("AlternateBridgeAuthority");
1784 test_alt_bridge_authority
->value
= tor_strdup(
1785 "B1 orport=9001 bridge "
1786 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1789 config_line_t
*test_alt_dir_authority
= tor_malloc_zero(
1790 sizeof(config_line_t
));
1791 test_alt_dir_authority
->key
= tor_strdup("AlternateDirAuthority");
1792 test_alt_dir_authority
->value
= tor_strdup(
1794 "v3ident=0223456789012345678901234567890123456789 "
1795 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1798 /* Use the format specified in the manual page */
1799 config_line_t
*test_fallback_directory
= tor_malloc_zero(
1800 sizeof(config_line_t
));
1801 test_fallback_directory
->key
= tor_strdup("FallbackDir");
1802 test_fallback_directory
->value
= tor_strdup(
1803 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
1806 /* We need to know if add_default_fallback_dir_servers is called,
1807 * whatever the size of the list in fallback_dirs.inc,
1808 * so we use a version of add_default_fallback_dir_servers that adds
1809 * one known default fallback directory. */
1810 MOCK(add_default_fallback_dir_servers
,
1811 add_default_fallback_dir_servers_known_default
);
1813 /* There are 16 different cases, covering each combination of set/NULL for:
1814 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
1815 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
1816 * But validate_dir_servers() ensures that:
1817 * "You cannot set both DirAuthority and Alternate*Authority."
1818 * This reduces the number of cases to 10.
1820 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
1821 * So 1001 or case 9 is:
1822 * DirAuthorities set,
1823 * AlternateBridgeAuthority NULL,
1824 * AlternateDirAuthority NULL
1826 * The valid cases are cases 0-9 counting using this method, as every case
1827 * greater than or equal to 10 = 1010 is invalid.
1829 * 1. Outcome: Use Set Directory Authorities
1830 * - No Default Authorities
1831 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1833 * Cases expected to yield this outcome:
1834 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1835 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1836 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1838 * 2. Outcome: Use Set Bridge Authority
1839 * - Use Default Non-Bridge Directory Authorities
1840 * - Use FallbackDir if it is set, otherwise use default FallbackDir
1841 * Cases expected to yield this outcome:
1842 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
1843 * AlternateBridgeAuthority is set, and
1844 * AlternateDirAuthority is NULL)
1846 * 3. Outcome: Use Set Alternate Directory Authority
1847 * - Use Default Bridge Authorities
1848 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
1849 * Cases expected to yield this outcome:
1850 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
1851 * are both NULL, but AlternateDirAuthority is set)
1853 * 4. Outcome: Use Set Custom Fallback Directory
1854 * - Use Default Bridge & Directory Authorities
1855 * Cases expected to yield this outcome:
1856 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
1857 * are all NULL, but FallbackDir is set)
1859 * 5. Outcome: Use All Defaults
1860 * - Use Default Bridge & Directory Authorities, and
1861 * Default Fallback Directories
1862 * Cases expected to yield this outcome:
1863 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
1864 * and FallbackDir are all NULL)
1868 * Find out how many default Bridge, Non-Bridge and Fallback Directories
1869 * are hard-coded into this build.
1870 * This code makes some assumptions about the implementation.
1871 * If they are wrong, one or more of cases 0-5 could fail.
1873 int n_default_alt_bridge_authority
= 0;
1874 int n_default_alt_dir_authority
= 0;
1875 int n_default_fallback_dir
= 0;
1876 #define n_default_authorities ((n_default_alt_bridge_authority) \
1877 + (n_default_alt_dir_authority))
1879 /* Pre-Count Number of Authorities of Each Type
1880 * Use 0000: No Directory Authorities or Fallback Directories Set
1883 /* clear fallback dirs counter */
1884 n_add_default_fallback_dir_servers_known_default
= 0;
1887 memset(options
, 0, sizeof(or_options_t
));
1889 /* clear any previous dir servers:
1890 consider_adding_dir_servers() should do this anyway */
1891 clear_dir_servers();
1893 /* assign options: 0000 */
1894 options
->DirAuthorities
= NULL
;
1895 options
->AlternateBridgeAuthority
= NULL
;
1896 options
->AlternateDirAuthority
= NULL
;
1897 options
->FallbackDir
= NULL
;
1898 options
->UseDefaultFallbackDirs
= 1;
1900 /* parse options - ensure we always update by passing NULL old_options */
1901 consider_adding_dir_servers(options
, NULL
);
1905 /* we must have added the default fallback dirs */
1906 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 1);
1908 /* we have more fallbacks than just the authorities */
1909 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
1912 /* fallback_dir_servers */
1913 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
1915 /* Count Bridge Authorities */
1916 SMARTLIST_FOREACH(fallback_servers
,
1919 /* increment the found counter if it's a bridge auth */
1920 n_default_alt_bridge_authority
+=
1921 ((ds
->is_authority
&& (ds
->type
& BRIDGE_DIRINFO
)) ?
1924 /* If we have no default bridge authority, something has gone wrong */
1925 tt_int_op(n_default_alt_bridge_authority
, OP_GE
, 1);
1927 /* Count v3 Authorities */
1928 SMARTLIST_FOREACH(fallback_servers
,
1931 /* increment found counter if it's a v3 auth */
1932 n_default_alt_dir_authority
+=
1933 ((ds
->is_authority
&& (ds
->type
& V3_DIRINFO
)) ?
1936 /* If we have no default authorities, something has gone really wrong */
1937 tt_int_op(n_default_alt_dir_authority
, OP_GE
, 1);
1939 /* Calculate Fallback Directory Count */
1940 n_default_fallback_dir
= (smartlist_len(fallback_servers
) -
1941 n_default_alt_bridge_authority
-
1942 n_default_alt_dir_authority
);
1943 /* If we have a negative count, something has gone really wrong,
1944 * or some authorities aren't being added as fallback directories.
1945 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
1946 * authorities being fallback directories.) */
1947 tt_int_op(n_default_fallback_dir
, OP_GE
, 0);
1952 * 1. Outcome: Use Set Directory Authorities
1953 * - No Default Authorities
1954 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1956 * Cases expected to yield this outcome:
1957 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1958 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1959 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1962 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
1963 AlternateDirAuthority Not Set, FallbackDir Set */
1965 /* clear fallback dirs counter */
1966 n_add_default_fallback_dir_servers_known_default
= 0;
1969 memset(options
, 0, sizeof(or_options_t
));
1971 /* clear any previous dir servers:
1972 consider_adding_dir_servers() should do this anyway */
1973 clear_dir_servers();
1975 /* assign options: 1001 */
1976 options
->DirAuthorities
= test_dir_authority
;
1977 options
->AlternateBridgeAuthority
= NULL
;
1978 options
->AlternateDirAuthority
= NULL
;
1979 options
->FallbackDir
= test_fallback_directory
;
1980 options
->UseDefaultFallbackDirs
= 1;
1982 /* parse options - ensure we always update by passing NULL old_options */
1983 consider_adding_dir_servers(options
, NULL
);
1987 /* we must not have added the default fallback dirs */
1988 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
1990 /* we have more fallbacks than just the authorities */
1991 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
1994 /* trusted_dir_servers */
1995 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
1996 /* D0, (No B1), (No A2) */
1997 tt_int_op(smartlist_len(dir_servers
), OP_EQ
, 1);
1999 /* DirAuthority - D0 - dir_port: 60090 */
2001 SMARTLIST_FOREACH(dir_servers
,
2004 /* increment the found counter if dir_port matches */
2006 (ds
->dir_port
== 60090 ?
2009 tt_int_op(found_D0
, OP_EQ
, 1);
2011 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2013 SMARTLIST_FOREACH(dir_servers
,
2016 /* increment the found counter if dir_port matches */
2018 (ds
->dir_port
== 60091 ?
2021 tt_int_op(found_B1
, OP_EQ
, 0);
2023 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2025 SMARTLIST_FOREACH(dir_servers
,
2028 /* increment the found counter if dir_port matches */
2030 (ds
->dir_port
== 60092 ?
2033 tt_int_op(found_A2
, OP_EQ
, 0);
2037 /* fallback_dir_servers */
2038 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2039 /* D0, (No B1), (No A2), Custom Fallback */
2040 tt_int_op(smartlist_len(fallback_servers
), OP_EQ
, 2);
2042 /* DirAuthority - D0 - dir_port: 60090 */
2044 SMARTLIST_FOREACH(fallback_servers
,
2047 /* increment the found counter if dir_port matches */
2049 (ds
->dir_port
== 60090 ?
2052 tt_int_op(found_D0
, OP_EQ
, 1);
2054 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2056 SMARTLIST_FOREACH(fallback_servers
,
2059 /* increment the found counter if dir_port matches */
2061 (ds
->dir_port
== 60091 ?
2064 tt_int_op(found_B1
, OP_EQ
, 0);
2066 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2068 SMARTLIST_FOREACH(fallback_servers
,
2071 /* increment the found counter if dir_port matches */
2073 (ds
->dir_port
== 60092 ?
2076 tt_int_op(found_A2
, OP_EQ
, 0);
2078 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2079 int found_non_default_fallback
= 0;
2080 SMARTLIST_FOREACH(fallback_servers
,
2083 /* increment the found counter if dir_port matches */
2084 found_non_default_fallback
+=
2085 (ds
->dir_port
== 60093 ?
2088 tt_int_op(found_non_default_fallback
, OP_EQ
, 1);
2090 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2091 int found_default_fallback
= 0;
2092 SMARTLIST_FOREACH(fallback_servers
,
2095 /* increment the found counter if dir_port matches */
2096 found_default_fallback
+=
2097 (ds
->dir_port
== 60099 ?
2100 tt_int_op(found_default_fallback
, OP_EQ
, 0);
2104 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2106 /* clear fallback dirs counter */
2107 n_add_default_fallback_dir_servers_known_default
= 0;
2110 memset(options
, 0, sizeof(or_options_t
));
2112 /* clear any previous dir servers:
2113 consider_adding_dir_servers() should do this anyway */
2114 clear_dir_servers();
2116 /* assign options: 1000 */
2117 options
->DirAuthorities
= test_dir_authority
;
2118 options
->AlternateBridgeAuthority
= NULL
;
2119 options
->AlternateDirAuthority
= NULL
;
2120 options
->FallbackDir
= NULL
;
2121 options
->UseDefaultFallbackDirs
= 1;
2123 /* parse options - ensure we always update by passing NULL old_options */
2124 consider_adding_dir_servers(options
, NULL
);
2128 /* we must not have added the default fallback dirs */
2129 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
2131 /* we just have the authorities */
2132 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 0);
2135 /* trusted_dir_servers */
2136 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2137 /* D0, (No B1), (No A2) */
2138 tt_int_op(smartlist_len(dir_servers
), OP_EQ
, 1);
2140 /* DirAuthority - D0 - dir_port: 60090 */
2142 SMARTLIST_FOREACH(dir_servers
,
2145 /* increment the found counter if dir_port matches */
2147 (ds
->dir_port
== 60090 ?
2150 tt_int_op(found_D0
, OP_EQ
, 1);
2152 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2154 SMARTLIST_FOREACH(dir_servers
,
2157 /* increment the found counter if dir_port matches */
2159 (ds
->dir_port
== 60091 ?
2162 tt_int_op(found_B1
, OP_EQ
, 0);
2164 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2166 SMARTLIST_FOREACH(dir_servers
,
2169 /* increment the found counter if dir_port matches */
2171 (ds
->dir_port
== 60092 ?
2174 tt_int_op(found_A2
, OP_EQ
, 0);
2178 /* fallback_dir_servers */
2179 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2180 /* D0, (No B1), (No A2), (No Fallback) */
2181 tt_int_op(smartlist_len(fallback_servers
), OP_EQ
, 1);
2183 /* DirAuthority - D0 - dir_port: 60090 */
2185 SMARTLIST_FOREACH(fallback_servers
,
2188 /* increment the found counter if dir_port matches */
2190 (ds
->dir_port
== 60090 ?
2193 tt_int_op(found_D0
, OP_EQ
, 1);
2195 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2197 SMARTLIST_FOREACH(fallback_servers
,
2200 /* increment the found counter if dir_port matches */
2202 (ds
->dir_port
== 60091 ?
2205 tt_int_op(found_B1
, OP_EQ
, 0);
2207 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2209 SMARTLIST_FOREACH(fallback_servers
,
2212 /* increment the found counter if dir_port matches */
2214 (ds
->dir_port
== 60092 ?
2217 tt_int_op(found_A2
, OP_EQ
, 0);
2219 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2220 int found_non_default_fallback
= 0;
2221 SMARTLIST_FOREACH(fallback_servers
,
2224 /* increment the found counter if dir_port matches */
2225 found_non_default_fallback
+=
2226 (ds
->dir_port
== 60093 ?
2229 tt_int_op(found_non_default_fallback
, OP_EQ
, 0);
2231 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2232 int found_default_fallback
= 0;
2233 SMARTLIST_FOREACH(fallback_servers
,
2236 /* increment the found counter if dir_port matches */
2237 found_default_fallback
+=
2238 (ds
->dir_port
== 60099 ?
2241 tt_int_op(found_default_fallback
, OP_EQ
, 0);
2245 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2247 /* clear fallback dirs counter */
2248 n_add_default_fallback_dir_servers_known_default
= 0;
2251 memset(options
, 0, sizeof(or_options_t
));
2253 /* clear any previous dir servers:
2254 consider_adding_dir_servers() should do this anyway */
2255 clear_dir_servers();
2257 /* assign options: 0111 */
2258 options
->DirAuthorities
= NULL
;
2259 options
->AlternateBridgeAuthority
= test_alt_bridge_authority
;
2260 options
->AlternateDirAuthority
= test_alt_dir_authority
;
2261 options
->FallbackDir
= test_fallback_directory
;
2262 options
->UseDefaultFallbackDirs
= 1;
2264 /* parse options - ensure we always update by passing NULL old_options */
2265 consider_adding_dir_servers(options
, NULL
);
2269 /* we must not have added the default fallback dirs */
2270 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
2272 /* we have more fallbacks than just the authorities */
2273 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
2276 /* trusted_dir_servers */
2277 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2278 /* (No D0), B1, A2 */
2279 tt_int_op(smartlist_len(dir_servers
), OP_EQ
, 2);
2281 /* (No DirAuthority) - D0 - dir_port: 60090 */
2283 SMARTLIST_FOREACH(dir_servers
,
2286 /* increment the found counter if dir_port matches */
2288 (ds
->dir_port
== 60090 ?
2291 tt_int_op(found_D0
, OP_EQ
, 0);
2293 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2295 SMARTLIST_FOREACH(dir_servers
,
2298 /* increment the found counter if dir_port matches */
2300 (ds
->dir_port
== 60091 ?
2303 tt_int_op(found_B1
, OP_EQ
, 1);
2305 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2307 SMARTLIST_FOREACH(dir_servers
,
2310 /* increment the found counter if dir_port matches */
2312 (ds
->dir_port
== 60092 ?
2315 tt_int_op(found_A2
, OP_EQ
, 1);
2319 /* fallback_dir_servers */
2320 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2321 /* (No D0), B1, A2, Custom Fallback */
2322 tt_int_op(smartlist_len(fallback_servers
), OP_EQ
, 3);
2324 /* (No DirAuthority) - D0 - dir_port: 60090 */
2326 SMARTLIST_FOREACH(fallback_servers
,
2329 /* increment the found counter if dir_port matches */
2331 (ds
->dir_port
== 60090 ?
2334 tt_int_op(found_D0
, OP_EQ
, 0);
2336 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2338 SMARTLIST_FOREACH(fallback_servers
,
2341 /* increment the found counter if dir_port matches */
2343 (ds
->dir_port
== 60091 ?
2346 tt_int_op(found_B1
, OP_EQ
, 1);
2348 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2350 SMARTLIST_FOREACH(fallback_servers
,
2353 /* increment the found counter if dir_port matches */
2355 (ds
->dir_port
== 60092 ?
2358 tt_int_op(found_A2
, OP_EQ
, 1);
2360 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2361 int found_non_default_fallback
= 0;
2362 SMARTLIST_FOREACH(fallback_servers
,
2365 /* increment the found counter if dir_port matches */
2366 found_non_default_fallback
+=
2367 (ds
->dir_port
== 60093 ?
2370 tt_int_op(found_non_default_fallback
, OP_EQ
, 1);
2372 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2373 int found_default_fallback
= 0;
2374 SMARTLIST_FOREACH(fallback_servers
,
2377 /* increment the found counter if dir_port matches */
2378 found_default_fallback
+=
2379 (ds
->dir_port
== 60099 ?
2382 tt_int_op(found_default_fallback
, OP_EQ
, 0);
2386 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2387 AlternateDirAuthority Set, FallbackDir Not Set */
2389 /* clear fallback dirs counter */
2390 n_add_default_fallback_dir_servers_known_default
= 0;
2393 memset(options
, 0, sizeof(or_options_t
));
2395 /* clear any previous dir servers:
2396 consider_adding_dir_servers() should do this anyway */
2397 clear_dir_servers();
2399 /* assign options: 0110 */
2400 options
->DirAuthorities
= NULL
;
2401 options
->AlternateBridgeAuthority
= test_alt_bridge_authority
;
2402 options
->AlternateDirAuthority
= test_alt_dir_authority
;
2403 options
->FallbackDir
= NULL
;
2404 options
->UseDefaultFallbackDirs
= 1;
2406 /* parse options - ensure we always update by passing NULL old_options */
2407 consider_adding_dir_servers(options
, NULL
);
2411 /* we must not have added the default fallback dirs */
2412 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
2414 /* we have more fallbacks than just the authorities */
2415 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 0);
2418 /* trusted_dir_servers */
2419 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2420 /* (No D0), B1, A2 */
2421 tt_int_op(smartlist_len(dir_servers
), OP_EQ
, 2);
2423 /* (No DirAuthority) - D0 - dir_port: 60090 */
2425 SMARTLIST_FOREACH(dir_servers
,
2428 /* increment the found counter if dir_port matches */
2430 (ds
->dir_port
== 60090 ?
2433 tt_int_op(found_D0
, OP_EQ
, 0);
2435 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2437 SMARTLIST_FOREACH(dir_servers
,
2440 /* increment the found counter if dir_port matches */
2442 (ds
->dir_port
== 60091 ?
2445 tt_int_op(found_B1
, OP_EQ
, 1);
2447 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2449 SMARTLIST_FOREACH(dir_servers
,
2452 /* increment the found counter if dir_port matches */
2454 (ds
->dir_port
== 60092 ?
2457 tt_int_op(found_A2
, OP_EQ
, 1);
2461 /* fallback_dir_servers */
2462 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2463 /* (No D0), B1, A2, (No Fallback) */
2464 tt_int_op(smartlist_len(fallback_servers
), OP_EQ
, 2);
2466 /* (No DirAuthority) - D0 - dir_port: 60090 */
2468 SMARTLIST_FOREACH(fallback_servers
,
2471 /* increment the found counter if dir_port matches */
2473 (ds
->dir_port
== 60090 ?
2476 tt_int_op(found_D0
, OP_EQ
, 0);
2478 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2480 SMARTLIST_FOREACH(fallback_servers
,
2483 /* increment the found counter if dir_port matches */
2485 (ds
->dir_port
== 60091 ?
2488 tt_int_op(found_B1
, OP_EQ
, 1);
2490 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2492 SMARTLIST_FOREACH(fallback_servers
,
2495 /* increment the found counter if dir_port matches */
2497 (ds
->dir_port
== 60092 ?
2500 tt_int_op(found_A2
, OP_EQ
, 1);
2502 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2503 int found_non_default_fallback
= 0;
2504 SMARTLIST_FOREACH(fallback_servers
,
2507 /* increment the found counter if dir_port matches */
2508 found_non_default_fallback
+=
2509 (ds
->dir_port
== 60093 ?
2512 tt_int_op(found_non_default_fallback
, OP_EQ
, 0);
2514 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2515 int found_default_fallback
= 0;
2516 SMARTLIST_FOREACH(fallback_servers
,
2519 /* increment the found counter if dir_port matches */
2520 found_default_fallback
+=
2521 (ds
->dir_port
== 60099 ?
2524 tt_int_op(found_default_fallback
, OP_EQ
, 0);
2529 2. Outcome: Use Set Bridge Authority
2530 - Use Default Non-Bridge Directory Authorities
2531 - Use FallbackDir if it is set, otherwise use default FallbackDir
2532 Cases expected to yield this outcome:
2533 4 & 5 (the 2 cases where DirAuthorities is NULL,
2534 AlternateBridgeAuthority is set, and
2535 AlternateDirAuthority is NULL)
2538 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2539 AlternateDirAuthority Not Set, FallbackDir Set */
2541 /* clear fallback dirs counter */
2542 n_add_default_fallback_dir_servers_known_default
= 0;
2545 memset(options
, 0, sizeof(or_options_t
));
2547 /* clear any previous dir servers:
2548 consider_adding_dir_servers() should do this anyway */
2549 clear_dir_servers();
2551 /* assign options: 0101 */
2552 options
->DirAuthorities
= NULL
;
2553 options
->AlternateBridgeAuthority
= test_alt_bridge_authority
;
2554 options
->AlternateDirAuthority
= NULL
;
2555 options
->FallbackDir
= test_fallback_directory
;
2556 options
->UseDefaultFallbackDirs
= 1;
2558 /* parse options - ensure we always update by passing NULL old_options */
2559 consider_adding_dir_servers(options
, NULL
);
2563 /* we must not have added the default fallback dirs */
2564 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
2566 /* we have more fallbacks than just the authorities */
2567 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
2570 /* trusted_dir_servers */
2571 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2572 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2573 tt_assert(smartlist_len(dir_servers
) == 1 + n_default_alt_dir_authority
);
2575 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2577 SMARTLIST_FOREACH(dir_servers
,
2580 /* increment the found counter if dir_port matches */
2582 (ds
->dir_port
== 60090 ?
2585 tt_int_op(found_D0
, OP_EQ
, 0);
2587 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2589 SMARTLIST_FOREACH(dir_servers
,
2592 /* increment the found counter if dir_port matches */
2594 (ds
->dir_port
== 60091 ?
2597 tt_int_op(found_B1
, OP_EQ
, 1);
2599 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2601 SMARTLIST_FOREACH(dir_servers
,
2604 /* increment the found counter if dir_port matches */
2606 (ds
->dir_port
== 60092 ?
2609 tt_int_op(found_A2
, OP_EQ
, 0);
2611 /* There's no easy way of checking that we have included all the
2612 * default v3 non-Bridge directory authorities, so let's assume that
2613 * if the total count above is correct, we have the right ones.
2618 /* fallback_dir_servers */
2619 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2620 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2621 * Custom Fallback */
2622 tt_assert(smartlist_len(fallback_servers
) ==
2623 2 + n_default_alt_dir_authority
);
2625 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2627 SMARTLIST_FOREACH(fallback_servers
,
2630 /* increment the found counter if dir_port matches */
2632 (ds
->dir_port
== 60090 ?
2635 tt_int_op(found_D0
, OP_EQ
, 0);
2637 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2639 SMARTLIST_FOREACH(fallback_servers
,
2642 /* increment the found counter if dir_port matches */
2644 (ds
->dir_port
== 60091 ?
2647 tt_int_op(found_B1
, OP_EQ
, 1);
2649 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2651 SMARTLIST_FOREACH(fallback_servers
,
2654 /* increment the found counter if dir_port matches */
2656 (ds
->dir_port
== 60092 ?
2659 tt_int_op(found_A2
, OP_EQ
, 0);
2661 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2662 int found_non_default_fallback
= 0;
2663 SMARTLIST_FOREACH(fallback_servers
,
2666 /* increment the found counter if dir_port matches */
2667 found_non_default_fallback
+=
2668 (ds
->dir_port
== 60093 ?
2671 tt_int_op(found_non_default_fallback
, OP_EQ
, 1);
2673 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2674 int found_default_fallback
= 0;
2675 SMARTLIST_FOREACH(fallback_servers
,
2678 /* increment the found counter if dir_port matches */
2679 found_default_fallback
+=
2680 (ds
->dir_port
== 60099 ?
2683 tt_int_op(found_default_fallback
, OP_EQ
, 0);
2685 /* There's no easy way of checking that we have included all the
2686 * default v3 non-Bridge directory authorities, so let's assume that
2687 * if the total count above is correct, we have the right ones.
2692 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2693 AlternateDirAuthority & FallbackDir Not Set */
2695 /* clear fallback dirs counter */
2696 n_add_default_fallback_dir_servers_known_default
= 0;
2699 memset(options
, 0, sizeof(or_options_t
));
2701 /* clear any previous dir servers:
2702 consider_adding_dir_servers() should do this anyway */
2703 clear_dir_servers();
2705 /* assign options: 0100 */
2706 options
->DirAuthorities
= NULL
;
2707 options
->AlternateBridgeAuthority
= test_alt_bridge_authority
;
2708 options
->AlternateDirAuthority
= NULL
;
2709 options
->FallbackDir
= NULL
;
2710 options
->UseDefaultFallbackDirs
= 1;
2712 /* parse options - ensure we always update by passing NULL old_options */
2713 consider_adding_dir_servers(options
, NULL
);
2717 /* we must have added the default fallback dirs */
2718 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 1);
2720 /* we have more fallbacks than just the authorities */
2721 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
2724 /* trusted_dir_servers */
2725 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2726 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2727 tt_assert(smartlist_len(dir_servers
) == 1 + n_default_alt_dir_authority
);
2729 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2731 SMARTLIST_FOREACH(dir_servers
,
2734 /* increment the found counter if dir_port matches */
2736 (ds
->dir_port
== 60090 ?
2739 tt_int_op(found_D0
, OP_EQ
, 0);
2741 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2743 SMARTLIST_FOREACH(dir_servers
,
2746 /* increment the found counter if dir_port matches */
2748 (ds
->dir_port
== 60091 ?
2751 tt_int_op(found_B1
, OP_EQ
, 1);
2753 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2755 SMARTLIST_FOREACH(dir_servers
,
2758 /* increment the found counter if dir_port matches */
2760 (ds
->dir_port
== 60092 ?
2763 tt_int_op(found_A2
, OP_EQ
, 0);
2765 /* There's no easy way of checking that we have included all the
2766 * default v3 non-Bridge directory authorities, so let's assume that
2767 * if the total count above is correct, we have the right ones.
2772 /* fallback_dir_servers */
2773 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2774 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2775 * Default Fallback */
2776 tt_assert(smartlist_len(fallback_servers
) ==
2777 2 + n_default_alt_dir_authority
);
2779 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2781 SMARTLIST_FOREACH(fallback_servers
,
2784 /* increment the found counter if dir_port matches */
2786 (ds
->dir_port
== 60090 ?
2789 tt_int_op(found_D0
, OP_EQ
, 0);
2791 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2793 SMARTLIST_FOREACH(fallback_servers
,
2796 /* increment the found counter if dir_port matches */
2798 (ds
->dir_port
== 60091 ?
2801 tt_int_op(found_B1
, OP_EQ
, 1);
2803 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2805 SMARTLIST_FOREACH(fallback_servers
,
2808 /* increment the found counter if dir_port matches */
2810 (ds
->dir_port
== 60092 ?
2813 tt_int_op(found_A2
, OP_EQ
, 0);
2815 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2816 int found_non_default_fallback
= 0;
2817 SMARTLIST_FOREACH(fallback_servers
,
2820 /* increment the found counter if dir_port matches */
2821 found_non_default_fallback
+=
2822 (ds
->dir_port
== 60093 ?
2825 tt_int_op(found_non_default_fallback
, OP_EQ
, 0);
2827 /* Default FallbackDir - No Nickname - dir_port: 60099 */
2828 int found_default_fallback
= 0;
2829 SMARTLIST_FOREACH(fallback_servers
,
2832 /* increment the found counter if dir_port matches */
2833 found_default_fallback
+=
2834 (ds
->dir_port
== 60099 ?
2837 tt_int_op(found_default_fallback
, OP_EQ
, 1);
2839 /* There's no easy way of checking that we have included all the
2840 * default v3 non-Bridge directory authorities, so let's assume that
2841 * if the total count above is correct, we have the right ones.
2847 3. Outcome: Use Set Alternate Directory Authority
2848 - Use Default Bridge Authorities
2849 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2850 Cases expected to yield this outcome:
2851 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2852 are both NULL, but AlternateDirAuthority is set)
2855 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
2856 AlternateDirAuthority & FallbackDir Set */
2858 /* clear fallback dirs counter */
2859 n_add_default_fallback_dir_servers_known_default
= 0;
2862 memset(options
, 0, sizeof(or_options_t
));
2864 /* clear any previous dir servers:
2865 consider_adding_dir_servers() should do this anyway */
2866 clear_dir_servers();
2868 /* assign options: 0011 */
2869 options
->DirAuthorities
= NULL
;
2870 options
->AlternateBridgeAuthority
= NULL
;
2871 options
->AlternateDirAuthority
= test_alt_dir_authority
;
2872 options
->FallbackDir
= test_fallback_directory
;
2873 options
->UseDefaultFallbackDirs
= 1;
2875 /* parse options - ensure we always update by passing NULL old_options */
2876 consider_adding_dir_servers(options
, NULL
);
2880 /* we must not have added the default fallback dirs */
2881 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
2883 /* we have more fallbacks than just the authorities */
2884 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
2887 /* trusted_dir_servers */
2888 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
2889 /* (No D0), (No B1), Default Bridge Authorities, A2 */
2890 tt_assert(smartlist_len(dir_servers
) ==
2891 1 + n_default_alt_bridge_authority
);
2893 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2895 SMARTLIST_FOREACH(dir_servers
,
2898 /* increment the found counter if dir_port matches */
2900 (ds
->dir_port
== 60090 ?
2903 tt_int_op(found_D0
, OP_EQ
, 0);
2905 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2907 SMARTLIST_FOREACH(dir_servers
,
2910 /* increment the found counter if dir_port matches */
2912 (ds
->dir_port
== 60091 ?
2915 tt_int_op(found_B1
, OP_EQ
, 0);
2917 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2919 SMARTLIST_FOREACH(dir_servers
,
2922 /* increment the found counter if dir_port matches */
2924 (ds
->dir_port
== 60092 ?
2927 tt_int_op(found_A2
, OP_EQ
, 1);
2929 /* There's no easy way of checking that we have included all the
2930 * default Bridge authorities (except for hard-coding tonga's details),
2931 * so let's assume that if the total count above is correct,
2932 * we have the right ones.
2937 /* fallback_dir_servers */
2938 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
2939 /* (No D0), (No B1), Default Bridge Authorities, A2,
2940 * Custom Fallback Directory, (No Default Fallback Directories) */
2941 tt_assert(smartlist_len(fallback_servers
) ==
2942 2 + n_default_alt_bridge_authority
);
2944 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2946 SMARTLIST_FOREACH(fallback_servers
,
2949 /* increment the found counter if dir_port matches */
2951 (ds
->dir_port
== 60090 ?
2954 tt_int_op(found_D0
, OP_EQ
, 0);
2956 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2958 SMARTLIST_FOREACH(fallback_servers
,
2961 /* increment the found counter if dir_port matches */
2963 (ds
->dir_port
== 60091 ?
2966 tt_int_op(found_B1
, OP_EQ
, 0);
2968 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2970 SMARTLIST_FOREACH(fallback_servers
,
2973 /* increment the found counter if dir_port matches */
2975 (ds
->dir_port
== 60092 ?
2978 tt_int_op(found_A2
, OP_EQ
, 1);
2980 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2981 int found_non_default_fallback
= 0;
2982 SMARTLIST_FOREACH(fallback_servers
,
2985 /* increment the found counter if dir_port matches */
2986 found_non_default_fallback
+=
2987 (ds
->dir_port
== 60093 ?
2990 tt_int_op(found_non_default_fallback
, OP_EQ
, 1);
2992 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2993 int found_default_fallback
= 0;
2994 SMARTLIST_FOREACH(fallback_servers
,
2997 /* increment the found counter if dir_port matches */
2998 found_default_fallback
+=
2999 (ds
->dir_port
== 60099 ?
3002 tt_int_op(found_default_fallback
, OP_EQ
, 0);
3004 /* There's no easy way of checking that we have included all the
3005 * default Bridge authorities (except for hard-coding tonga's details),
3006 * so let's assume that if the total count above is correct,
3007 * we have the right ones.
3012 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
3013 AlternateDirAuthority Set, FallbackDir Not Set */
3015 /* clear fallback dirs counter */
3016 n_add_default_fallback_dir_servers_known_default
= 0;
3019 memset(options
, 0, sizeof(or_options_t
));
3021 /* clear any previous dir servers:
3022 consider_adding_dir_servers() should do this anyway */
3023 clear_dir_servers();
3025 /* assign options: 0010 */
3026 options
->DirAuthorities
= NULL
;
3027 options
->AlternateBridgeAuthority
= NULL
;
3028 options
->AlternateDirAuthority
= test_alt_dir_authority
;
3029 options
->FallbackDir
= NULL
;
3030 options
->UseDefaultFallbackDirs
= 1;
3032 /* parse options - ensure we always update by passing NULL old_options */
3033 consider_adding_dir_servers(options
, NULL
);
3037 /* we must not have added the default fallback dirs */
3038 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
3040 /* we just have the authorities */
3041 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 0);
3044 /* trusted_dir_servers */
3045 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
3046 /* (No D0), (No B1), Default Bridge Authorities, A2,
3047 * No Default or Custom Fallback Directories */
3048 tt_assert(smartlist_len(dir_servers
) ==
3049 1 + n_default_alt_bridge_authority
);
3051 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3053 SMARTLIST_FOREACH(dir_servers
,
3056 /* increment the found counter if dir_port matches */
3058 (ds
->dir_port
== 60090 ?
3061 tt_int_op(found_D0
, OP_EQ
, 0);
3063 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3065 SMARTLIST_FOREACH(dir_servers
,
3068 /* increment the found counter if dir_port matches */
3070 (ds
->dir_port
== 60091 ?
3073 tt_int_op(found_B1
, OP_EQ
, 0);
3075 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3077 SMARTLIST_FOREACH(dir_servers
,
3080 /* increment the found counter if dir_port matches */
3082 (ds
->dir_port
== 60092 ?
3085 tt_int_op(found_A2
, OP_EQ
, 1);
3087 /* There's no easy way of checking that we have included all the
3088 * default Bridge authorities (except for hard-coding tonga's details),
3089 * so let's assume that if the total count above is correct,
3090 * we have the right ones.
3095 /* fallback_dir_servers */
3096 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
3097 /* (No D0), (No B1), Default Bridge Authorities, A2,
3098 * No Custom or Default Fallback Directories */
3099 tt_assert(smartlist_len(fallback_servers
) ==
3100 1 + n_default_alt_bridge_authority
);
3102 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3104 SMARTLIST_FOREACH(fallback_servers
,
3107 /* increment the found counter if dir_port matches */
3109 (ds
->dir_port
== 60090 ?
3112 tt_int_op(found_D0
, OP_EQ
, 0);
3114 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3116 SMARTLIST_FOREACH(fallback_servers
,
3119 /* increment the found counter if dir_port matches */
3121 (ds
->dir_port
== 60091 ?
3124 tt_int_op(found_B1
, OP_EQ
, 0);
3126 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3128 SMARTLIST_FOREACH(fallback_servers
,
3131 /* increment the found counter if dir_port matches */
3133 (ds
->dir_port
== 60092 ?
3136 tt_int_op(found_A2
, OP_EQ
, 1);
3138 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3139 int found_non_default_fallback
= 0;
3140 SMARTLIST_FOREACH(fallback_servers
,
3143 /* increment the found counter if dir_port matches */
3144 found_non_default_fallback
+=
3145 (ds
->dir_port
== 60093 ?
3148 tt_int_op(found_non_default_fallback
, OP_EQ
, 0);
3150 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3151 int found_default_fallback
= 0;
3152 SMARTLIST_FOREACH(fallback_servers
,
3155 /* increment the found counter if dir_port matches */
3156 found_default_fallback
+=
3157 (ds
->dir_port
== 60099 ?
3160 tt_int_op(found_default_fallback
, OP_EQ
, 0);
3162 /* There's no easy way of checking that we have included all the
3163 * default Bridge authorities (except for hard-coding tonga's details),
3164 * so let's assume that if the total count above is correct,
3165 * we have the right ones.
3171 4. Outcome: Use Set Custom Fallback Directory
3172 - Use Default Bridge & Directory Authorities
3173 Cases expected to yield this outcome:
3174 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3175 are all NULL, but FallbackDir is set)
3178 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3179 & AlternateDirAuthority Not Set, FallbackDir Set */
3181 /* clear fallback dirs counter */
3182 n_add_default_fallback_dir_servers_known_default
= 0;
3185 memset(options
, 0, sizeof(or_options_t
));
3187 /* clear any previous dir servers:
3188 consider_adding_dir_servers() should do this anyway */
3189 clear_dir_servers();
3191 /* assign options: 0001 */
3192 options
->DirAuthorities
= NULL
;
3193 options
->AlternateBridgeAuthority
= NULL
;
3194 options
->AlternateDirAuthority
= NULL
;
3195 options
->FallbackDir
= test_fallback_directory
;
3196 options
->UseDefaultFallbackDirs
= 1;
3198 /* parse options - ensure we always update by passing NULL old_options */
3199 consider_adding_dir_servers(options
, NULL
);
3203 /* we must not have added the default fallback dirs */
3204 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 0);
3206 /* we have more fallbacks than just the authorities */
3207 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
3210 /* trusted_dir_servers */
3211 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
3212 /* (No D0), (No B1), Default Bridge Authorities,
3213 * (No A2), Default v3 Directory Authorities */
3214 tt_assert(smartlist_len(dir_servers
) == n_default_authorities
);
3216 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3218 SMARTLIST_FOREACH(dir_servers
,
3221 /* increment the found counter if dir_port matches */
3223 (ds
->dir_port
== 60090 ?
3226 tt_int_op(found_D0
, OP_EQ
, 0);
3228 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3230 SMARTLIST_FOREACH(dir_servers
,
3233 /* increment the found counter if dir_port matches */
3235 (ds
->dir_port
== 60091 ?
3238 tt_int_op(found_B1
, OP_EQ
, 0);
3240 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3242 SMARTLIST_FOREACH(dir_servers
,
3245 /* increment the found counter if dir_port matches */
3247 (ds
->dir_port
== 60092 ?
3250 tt_int_op(found_A2
, OP_EQ
, 0);
3252 /* There's no easy way of checking that we have included all the
3253 * default Bridge & V3 Directory authorities, so let's assume that
3254 * if the total count above is correct, we have the right ones.
3259 /* fallback_dir_servers */
3260 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
3261 /* (No D0), (No B1), Default Bridge Authorities,
3262 * (No A2), Default v3 Directory Authorities,
3263 * Custom Fallback Directory, (No Default Fallback Directories) */
3264 tt_assert(smartlist_len(fallback_servers
) ==
3265 1 + n_default_authorities
);
3267 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3269 SMARTLIST_FOREACH(fallback_servers
,
3272 /* increment the found counter if dir_port matches */
3274 (ds
->dir_port
== 60090 ?
3277 tt_int_op(found_D0
, OP_EQ
, 0);
3279 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3281 SMARTLIST_FOREACH(fallback_servers
,
3284 /* increment the found counter if dir_port matches */
3286 (ds
->dir_port
== 60091 ?
3289 tt_int_op(found_B1
, OP_EQ
, 0);
3291 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3293 SMARTLIST_FOREACH(fallback_servers
,
3296 /* increment the found counter if dir_port matches */
3298 (ds
->dir_port
== 60092 ?
3301 tt_int_op(found_A2
, OP_EQ
, 0);
3303 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3304 int found_non_default_fallback
= 0;
3305 SMARTLIST_FOREACH(fallback_servers
,
3308 /* increment the found counter if dir_port matches */
3309 found_non_default_fallback
+=
3310 (ds
->dir_port
== 60093 ?
3313 tt_int_op(found_non_default_fallback
, OP_EQ
, 1);
3315 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3316 int found_default_fallback
= 0;
3317 SMARTLIST_FOREACH(fallback_servers
,
3320 /* increment the found counter if dir_port matches */
3321 found_default_fallback
+=
3322 (ds
->dir_port
== 60099 ?
3325 tt_int_op(found_default_fallback
, OP_EQ
, 0);
3327 /* There's no easy way of checking that we have included all the
3328 * default Bridge & V3 Directory authorities, so let's assume that
3329 * if the total count above is correct, we have the right ones.
3335 5. Outcome: Use All Defaults
3336 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3337 Cases expected to yield this outcome:
3338 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3339 and FallbackDir are all NULL)
3342 /* Case 0: 0000 - All Not Set */
3344 /* clear fallback dirs counter */
3345 n_add_default_fallback_dir_servers_known_default
= 0;
3348 memset(options
, 0, sizeof(or_options_t
));
3350 /* clear any previous dir servers:
3351 consider_adding_dir_servers() should do this anyway */
3352 clear_dir_servers();
3354 /* assign options: 0001 */
3355 options
->DirAuthorities
= NULL
;
3356 options
->AlternateBridgeAuthority
= NULL
;
3357 options
->AlternateDirAuthority
= NULL
;
3358 options
->FallbackDir
= NULL
;
3359 options
->UseDefaultFallbackDirs
= 1;
3361 /* parse options - ensure we always update by passing NULL old_options */
3362 consider_adding_dir_servers(options
, NULL
);
3366 /* we must have added the default fallback dirs */
3367 tt_int_op(n_add_default_fallback_dir_servers_known_default
, OP_EQ
, 1);
3369 /* we have more fallbacks than just the authorities */
3370 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options
) == 1);
3373 /* trusted_dir_servers */
3374 const smartlist_t
*dir_servers
= router_get_trusted_dir_servers();
3375 /* (No D0), (No B1), Default Bridge Authorities,
3376 * (No A2), Default v3 Directory Authorities */
3377 tt_assert(smartlist_len(dir_servers
) == n_default_authorities
);
3379 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3381 SMARTLIST_FOREACH(dir_servers
,
3384 /* increment the found counter if dir_port matches */
3386 (ds
->dir_port
== 60090 ?
3389 tt_int_op(found_D0
, OP_EQ
, 0);
3391 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3393 SMARTLIST_FOREACH(dir_servers
,
3396 /* increment the found counter if dir_port matches */
3398 (ds
->dir_port
== 60091 ?
3401 tt_int_op(found_B1
, OP_EQ
, 0);
3403 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3405 SMARTLIST_FOREACH(dir_servers
,
3408 /* increment the found counter if dir_port matches */
3410 (ds
->dir_port
== 60092 ?
3413 tt_int_op(found_A2
, OP_EQ
, 0);
3415 /* There's no easy way of checking that we have included all the
3416 * default Bridge & V3 Directory authorities, so let's assume that
3417 * if the total count above is correct, we have the right ones.
3422 /* fallback_dir_servers */
3423 const smartlist_t
*fallback_servers
= router_get_fallback_dir_servers();
3424 /* (No D0), (No B1), Default Bridge Authorities,
3425 * (No A2), Default v3 Directory Authorities,
3426 * (No Custom Fallback Directory), Default Fallback Directories */
3427 tt_assert(smartlist_len(fallback_servers
) ==
3428 n_default_authorities
+ n_default_fallback_dir
);
3430 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3432 SMARTLIST_FOREACH(fallback_servers
,
3435 /* increment the found counter if dir_port matches */
3437 (ds
->dir_port
== 60090 ?
3440 tt_int_op(found_D0
, OP_EQ
, 0);
3442 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3444 SMARTLIST_FOREACH(fallback_servers
,
3447 /* increment the found counter if dir_port matches */
3449 (ds
->dir_port
== 60091 ?
3452 tt_int_op(found_B1
, OP_EQ
, 0);
3454 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3456 SMARTLIST_FOREACH(fallback_servers
,
3459 /* increment the found counter if dir_port matches */
3461 (ds
->dir_port
== 60092 ?
3464 tt_int_op(found_A2
, OP_EQ
, 0);
3466 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3467 int found_non_default_fallback
= 0;
3468 SMARTLIST_FOREACH(fallback_servers
,
3471 /* increment the found counter if dir_port matches */
3472 found_non_default_fallback
+=
3473 (ds
->dir_port
== 60093 ?
3476 tt_int_op(found_non_default_fallback
, OP_EQ
, 0);
3478 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3479 int found_default_fallback
= 0;
3480 SMARTLIST_FOREACH(fallback_servers
,
3483 /* increment the found counter if dir_port matches */
3484 found_default_fallback
+=
3485 (ds
->dir_port
== 60099 ?
3488 tt_int_op(found_default_fallback
, OP_EQ
, 1);
3490 /* There's no easy way of checking that we have included all the
3491 * default Bridge & V3 Directory authorities, and the default
3492 * Fallback Directories, so let's assume that if the total count
3493 * above is correct, we have the right ones.
3499 clear_dir_servers();
3501 tor_free(test_dir_authority
->key
);
3502 tor_free(test_dir_authority
->value
);
3503 tor_free(test_dir_authority
);
3505 tor_free(test_alt_dir_authority
->key
);
3506 tor_free(test_alt_dir_authority
->value
);
3507 tor_free(test_alt_dir_authority
);
3509 tor_free(test_alt_bridge_authority
->key
);
3510 tor_free(test_alt_bridge_authority
->value
);
3511 tor_free(test_alt_bridge_authority
);
3513 tor_free(test_fallback_directory
->key
);
3514 tor_free(test_fallback_directory
->value
);
3515 tor_free(test_fallback_directory
);
3517 options
->DirAuthorities
= NULL
;
3518 options
->AlternateBridgeAuthority
= NULL
;
3519 options
->AlternateDirAuthority
= NULL
;
3520 options
->FallbackDir
= NULL
;
3521 or_options_free(options
);
3523 UNMOCK(add_default_fallback_dir_servers
);
3527 test_config_default_dir_servers(void *arg
)
3529 or_options_t
*opts
= NULL
;
3531 int trusted_count
= 0;
3532 int fallback_count
= 0;
3534 /* new set of options should stop fallback parsing */
3535 opts
= tor_malloc_zero(sizeof(or_options_t
));
3536 opts
->UseDefaultFallbackDirs
= 0;
3537 /* set old_options to NULL to force dir update */
3538 consider_adding_dir_servers(opts
, NULL
);
3539 trusted_count
= smartlist_len(router_get_trusted_dir_servers());
3540 fallback_count
= smartlist_len(router_get_fallback_dir_servers());
3541 or_options_free(opts
);
3544 /* assume a release will never go out with less than 7 authorities */
3545 tt_int_op(trusted_count
, OP_GE
, 7);
3546 /* if we disable the default fallbacks, there must not be any extra */
3547 tt_assert(fallback_count
== trusted_count
);
3549 opts
= tor_malloc_zero(sizeof(or_options_t
));
3550 opts
->UseDefaultFallbackDirs
= 1;
3551 consider_adding_dir_servers(opts
, opts
);
3552 trusted_count
= smartlist_len(router_get_trusted_dir_servers());
3553 fallback_count
= smartlist_len(router_get_fallback_dir_servers());
3554 or_options_free(opts
);
3557 /* assume a release will never go out with less than 7 authorities */
3558 tt_int_op(trusted_count
, OP_GE
, 7);
3559 /* XX/teor - allow for default fallbacks to be added without breaking
3560 * the unit tests. Set a minimum fallback count once the list is stable. */
3561 tt_assert(fallback_count
>= trusted_count
);
3564 or_options_free(opts
);
3567 static int mock_router_pick_published_address_result
= 0;
3570 mock_router_pick_published_address(const or_options_t
*options
,
3571 uint32_t *addr
, int cache_only
)
3576 return mock_router_pick_published_address_result
;
3579 static int mock_router_my_exit_policy_is_reject_star_result
= 0;
3582 mock_router_my_exit_policy_is_reject_star(void)
3584 return mock_router_my_exit_policy_is_reject_star_result
;
3587 static int mock_advertised_server_mode_result
= 0;
3590 mock_advertised_server_mode(void)
3592 return mock_advertised_server_mode_result
;
3595 static routerinfo_t
*mock_router_get_my_routerinfo_result
= NULL
;
3597 static const routerinfo_t
*
3598 mock_router_get_my_routerinfo(void)
3600 return mock_router_get_my_routerinfo_result
;
3604 test_config_directory_fetch(void *arg
)
3609 or_options_t
*options
= tor_malloc_zero(sizeof(or_options_t
));
3610 routerinfo_t routerinfo
;
3611 memset(&routerinfo
, 0, sizeof(routerinfo
));
3612 mock_router_pick_published_address_result
= -1;
3613 mock_router_my_exit_policy_is_reject_star_result
= 1;
3614 mock_advertised_server_mode_result
= 0;
3615 mock_router_get_my_routerinfo_result
= NULL
;
3616 MOCK(router_pick_published_address
, mock_router_pick_published_address
);
3617 MOCK(router_my_exit_policy_is_reject_star
,
3618 mock_router_my_exit_policy_is_reject_star
);
3619 MOCK(advertised_server_mode
, mock_advertised_server_mode
);
3620 MOCK(router_get_my_routerinfo
, mock_router_get_my_routerinfo
);
3622 /* Clients can use multiple directory mirrors for bootstrap */
3623 memset(options
, 0, sizeof(or_options_t
));
3624 options
->ClientOnly
= 1;
3625 tt_assert(server_mode(options
) == 0);
3626 tt_assert(public_server_mode(options
) == 0);
3627 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3628 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3631 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3632 memset(options
, 0, sizeof(or_options_t
));
3633 options
->UseBridges
= 1;
3634 tt_assert(server_mode(options
) == 0);
3635 tt_assert(public_server_mode(options
) == 0);
3636 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3637 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3640 /* Bridge Relays (Bridges) must act like clients, and use multiple
3641 * directory mirrors for bootstrap */
3642 memset(options
, 0, sizeof(or_options_t
));
3643 options
->BridgeRelay
= 1;
3644 options
->ORPort_set
= 1;
3645 tt_assert(server_mode(options
) == 1);
3646 tt_assert(public_server_mode(options
) == 0);
3647 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3648 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3651 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3652 * but can use multiple authorities for bootstrap */
3653 memset(options
, 0, sizeof(or_options_t
));
3654 options
->FetchDirInfoEarly
= 1;
3655 tt_assert(server_mode(options
) == 0);
3656 tt_assert(public_server_mode(options
) == 0);
3657 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 1);
3658 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3661 /* OR servers only fetch the consensus from the authorities when they don't
3662 * know their own address, but never use multiple directories for bootstrap
3664 memset(options
, 0, sizeof(or_options_t
));
3665 options
->ORPort_set
= 1;
3667 mock_router_pick_published_address_result
= -1;
3668 tt_assert(server_mode(options
) == 1);
3669 tt_assert(public_server_mode(options
) == 1);
3670 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 1);
3671 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3674 mock_router_pick_published_address_result
= 0;
3675 tt_assert(server_mode(options
) == 1);
3676 tt_assert(public_server_mode(options
) == 1);
3677 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3678 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3681 /* Exit OR servers only fetch the consensus from the authorities when they
3682 * refuse unknown exits, but never use multiple directories for bootstrap
3684 memset(options
, 0, sizeof(or_options_t
));
3685 options
->ORPort_set
= 1;
3686 options
->ExitRelay
= 1;
3687 mock_router_pick_published_address_result
= 0;
3688 mock_router_my_exit_policy_is_reject_star_result
= 0;
3689 mock_advertised_server_mode_result
= 1;
3690 mock_router_get_my_routerinfo_result
= &routerinfo
;
3692 routerinfo
.supports_tunnelled_dir_requests
= 1;
3694 options
->RefuseUnknownExits
= 1;
3695 tt_assert(server_mode(options
) == 1);
3696 tt_assert(public_server_mode(options
) == 1);
3697 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 1);
3698 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3701 options
->RefuseUnknownExits
= 0;
3702 mock_router_pick_published_address_result
= 0;
3703 tt_assert(server_mode(options
) == 1);
3704 tt_assert(public_server_mode(options
) == 1);
3705 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3706 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3709 /* Dir servers fetch the consensus from the authorities, unless they are not
3710 * advertising themselves (hibernating) or have no routerinfo or are not
3711 * advertising their dirport, and never use multiple directories for
3712 * bootstrap. This only applies if they are also OR servers.
3713 * (We don't care much about the behaviour of non-OR directory servers.) */
3714 memset(options
, 0, sizeof(or_options_t
));
3715 options
->DirPort_set
= 1;
3716 options
->ORPort_set
= 1;
3717 options
->DirCache
= 1;
3718 mock_router_pick_published_address_result
= 0;
3719 mock_router_my_exit_policy_is_reject_star_result
= 1;
3721 mock_advertised_server_mode_result
= 1;
3722 routerinfo
.dir_port
= 1;
3723 mock_router_get_my_routerinfo_result
= &routerinfo
;
3724 tt_assert(server_mode(options
) == 1);
3725 tt_assert(public_server_mode(options
) == 1);
3726 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 1);
3727 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3730 mock_advertised_server_mode_result
= 0;
3731 routerinfo
.dir_port
= 1;
3732 mock_router_get_my_routerinfo_result
= &routerinfo
;
3733 tt_assert(server_mode(options
) == 1);
3734 tt_assert(public_server_mode(options
) == 1);
3735 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3736 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3739 mock_advertised_server_mode_result
= 1;
3740 mock_router_get_my_routerinfo_result
= NULL
;
3741 tt_assert(server_mode(options
) == 1);
3742 tt_assert(public_server_mode(options
) == 1);
3743 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3744 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3747 mock_advertised_server_mode_result
= 1;
3748 routerinfo
.dir_port
= 0;
3749 routerinfo
.supports_tunnelled_dir_requests
= 0;
3750 mock_router_get_my_routerinfo_result
= &routerinfo
;
3751 tt_assert(server_mode(options
) == 1);
3752 tt_assert(public_server_mode(options
) == 1);
3753 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 0);
3754 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3757 mock_advertised_server_mode_result
= 1;
3758 routerinfo
.dir_port
= 1;
3759 routerinfo
.supports_tunnelled_dir_requests
= 1;
3760 mock_router_get_my_routerinfo_result
= &routerinfo
;
3761 tt_assert(server_mode(options
) == 1);
3762 tt_assert(public_server_mode(options
) == 1);
3763 tt_int_op(directory_fetches_from_authorities(options
), OP_EQ
, 1);
3764 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options
),
3769 UNMOCK(router_pick_published_address
);
3770 UNMOCK(router_get_my_routerinfo
);
3771 UNMOCK(advertised_server_mode
);
3772 UNMOCK(router_my_exit_policy_is_reject_star
);
3776 test_config_default_fallback_dirs(void *arg
)
3778 const char *fallback
[] = {
3779 #include "app/config/fallback_dirs.inc"
3783 int n_included_fallback_dirs
= 0;
3784 int n_added_fallback_dirs
= 0;
3787 clear_dir_servers();
3789 while (fallback
[n_included_fallback_dirs
])
3790 n_included_fallback_dirs
++;
3792 add_default_fallback_dir_servers();
3794 n_added_fallback_dirs
= smartlist_len(router_get_fallback_dir_servers());
3796 tt_assert(n_included_fallback_dirs
== n_added_fallback_dirs
);
3799 clear_dir_servers();
3803 test_config_port_cfg_line_extract_addrport(void *arg
)
3807 const char *rest
= NULL
;
3810 tt_int_op(port_cfg_line_extract_addrport("", &a
, &unixy
, &rest
), OP_EQ
, 0);
3811 tt_int_op(unixy
, OP_EQ
, 0);
3812 tt_str_op(a
, OP_EQ
, "");
3813 tt_str_op(rest
, OP_EQ
, "");
3816 tt_int_op(port_cfg_line_extract_addrport("hello", &a
, &unixy
, &rest
),
3818 tt_int_op(unixy
, OP_EQ
, 0);
3819 tt_str_op(a
, OP_EQ
, "hello");
3820 tt_str_op(rest
, OP_EQ
, "");
3823 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
3824 &a
, &unixy
, &rest
), OP_EQ
, 0);
3825 tt_int_op(unixy
, OP_EQ
, 0);
3826 tt_str_op(a
, OP_EQ
, "flipperwalt");
3827 tt_str_op(rest
, OP_EQ
, "gersplut");
3830 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
3831 &a
, &unixy
, &rest
), OP_EQ
, 0);
3832 tt_int_op(unixy
, OP_EQ
, 0);
3833 tt_str_op(a
, OP_EQ
, "flipperwalt");
3834 tt_str_op(rest
, OP_EQ
, "gersplut");
3837 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
3838 &a
, &unixy
, &rest
), OP_EQ
, 0);
3839 tt_int_op(unixy
, OP_EQ
, 0);
3840 tt_str_op(a
, OP_EQ
, "flipperwalt");
3841 tt_str_op(rest
, OP_EQ
, "gersplut");
3844 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
3845 &a
, &unixy
, &rest
), OP_EQ
, 0);
3846 tt_int_op(unixy
, OP_EQ
, 1);
3847 tt_str_op(a
, OP_EQ
, "flipperwalt");
3848 tt_str_op(rest
, OP_EQ
, "gersplut");
3851 tt_int_op(port_cfg_line_extract_addrport("lolol",
3852 &a
, &unixy
, &rest
), OP_EQ
, 0);
3853 tt_int_op(unixy
, OP_EQ
, 0);
3854 tt_str_op(a
, OP_EQ
, "lolol");
3855 tt_str_op(rest
, OP_EQ
, "");
3858 tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
3859 &a
, &unixy
, &rest
), OP_EQ
, 0);
3860 tt_int_op(unixy
, OP_EQ
, 1);
3861 tt_str_op(a
, OP_EQ
, "lolol");
3862 tt_str_op(rest
, OP_EQ
, "");
3865 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
3866 &a
, &unixy
, &rest
), OP_EQ
, 0);
3867 tt_int_op(unixy
, OP_EQ
, 1);
3868 tt_str_op(a
, OP_EQ
, "lolol");
3869 tt_str_op(rest
, OP_EQ
, "");
3872 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
3873 &a
, &unixy
, &rest
), OP_EQ
, 0);
3874 tt_int_op(unixy
, OP_EQ
, 1);
3875 tt_str_op(a
, OP_EQ
, "lolol");
3876 tt_str_op(rest
, OP_EQ
, "");
3879 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
3880 &a
, &unixy
, &rest
), OP_EQ
, 0);
3881 tt_int_op(unixy
, OP_EQ
, 0);
3882 tt_str_op(a
, OP_EQ
, "foobar:lolol");
3883 tt_str_op(rest
, OP_EQ
, "");
3886 tt_int_op(port_cfg_line_extract_addrport(":lolol",
3887 &a
, &unixy
, &rest
), OP_EQ
, 0);
3888 tt_int_op(unixy
, OP_EQ
, 0);
3889 tt_str_op(a
, OP_EQ
, ":lolol");
3890 tt_str_op(rest
, OP_EQ
, "");
3893 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
3894 &a
, &unixy
, &rest
), OP_EQ
, 0);
3895 tt_int_op(unixy
, OP_EQ
, 1);
3896 tt_str_op(a
, OP_EQ
, "lolol");
3897 tt_str_op(rest
, OP_EQ
, "");
3900 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
3901 &a
, &unixy
, &rest
), OP_EQ
, 0);
3902 tt_int_op(unixy
, OP_EQ
, 1);
3903 tt_str_op(a
, OP_EQ
, "lolol");
3904 tt_str_op(rest
, OP_EQ
, "");
3907 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
3908 &a
, &unixy
, &rest
), OP_EQ
, 0);
3909 tt_int_op(unixy
, OP_EQ
, 1);
3910 tt_str_op(a
, OP_EQ
, "lolol");
3911 tt_str_op(rest
, OP_EQ
, "foo ");
3914 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
3915 &a
, &unixy
, &rest
), OP_EQ
, 0);
3916 tt_int_op(unixy
, OP_EQ
, 1);
3917 tt_str_op(a
, OP_EQ
, "lol ol");
3918 tt_str_op(rest
, OP_EQ
, "foo ");
3921 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
3922 &a
, &unixy
, &rest
), OP_EQ
, 0);
3923 tt_int_op(unixy
, OP_EQ
, 1);
3924 tt_str_op(a
, OP_EQ
, "lol\" ol");
3925 tt_str_op(rest
, OP_EQ
, "foo ");
3928 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
3929 &a
, &unixy
, &rest
), OP_EQ
, -1);
3932 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
3933 &a
, &unixy
, &rest
), OP_EQ
, -1);
3940 static config_line_t
*
3941 mock_config_line(const char *key
, const char *val
)
3943 config_line_t
*config_line
= tor_malloc(sizeof(config_line_t
));
3944 memset(config_line
, 0, sizeof(config_line_t
));
3945 config_line
->key
= tor_strdup(key
);
3946 config_line
->value
= tor_strdup(val
);
3951 test_config_parse_port_config__ports__no_ports_given(void *data
)
3955 smartlist_t
*slout
= NULL
;
3956 port_cfg_t
*port_cfg
= NULL
;
3958 slout
= smartlist_new();
3960 // Test no defaultport, no defaultaddress and no out
3961 ret
= parse_port_config(NULL
, NULL
, "DNS", 0, NULL
, 0, 0);
3962 tt_int_op(ret
, OP_EQ
, 0);
3964 // Test with defaultport, no defaultaddress and no out
3965 ret
= parse_port_config(NULL
, NULL
, "DNS", 0, NULL
, 42, 0);
3966 tt_int_op(ret
, OP_EQ
, 0);
3968 // Test no defaultport, with defaultaddress and no out
3969 ret
= parse_port_config(NULL
, NULL
, "DNS", 0, "127.0.0.2", 0, 0);
3970 tt_int_op(ret
, OP_EQ
, 0);
3972 // Test with defaultport, with defaultaddress and no out
3973 ret
= parse_port_config(NULL
, NULL
, "DNS", 0, "127.0.0.2", 42, 0);
3974 tt_int_op(ret
, OP_EQ
, 0);
3976 // Test no defaultport, no defaultaddress and with out
3977 ret
= parse_port_config(slout
, NULL
, "DNS", 0, NULL
, 0, 0);
3978 tt_int_op(ret
, OP_EQ
, 0);
3979 tt_int_op(smartlist_len(slout
), OP_EQ
, 0);
3981 // Test with defaultport, no defaultaddress and with out
3982 ret
= parse_port_config(slout
, NULL
, "DNS", 0, NULL
, 42, 0);
3983 tt_int_op(ret
, OP_EQ
, 0);
3984 tt_int_op(smartlist_len(slout
), OP_EQ
, 0);
3986 // Test no defaultport, with defaultaddress and with out
3987 ret
= parse_port_config(slout
, NULL
, "DNS", 0, "127.0.0.2", 0, 0);
3988 tt_int_op(ret
, OP_EQ
, 0);
3989 tt_int_op(smartlist_len(slout
), OP_EQ
, 0);
3991 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3992 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
3993 smartlist_clear(slout
);
3994 ret
= parse_port_config(slout
, NULL
, "DNS", 0, "127.0.0.2", 42, 0);
3995 tt_int_op(ret
, OP_EQ
, 0);
3996 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
3997 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
3998 tt_int_op(port_cfg
->port
, OP_EQ
, 42);
3999 tt_int_op(port_cfg
->is_unix_addr
, OP_EQ
, 0);
4001 // Test with defaultport, with defaultaddress and out, adds a new port cfg
4002 // for a unix address
4003 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4004 smartlist_clear(slout
);
4005 ret
= parse_port_config(slout
, NULL
, "DNS", 0, "/foo/bar/unixdomain",
4006 42, CL_PORT_IS_UNIXSOCKET
);
4007 tt_int_op(ret
, OP_EQ
, 0);
4008 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4009 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4010 tt_int_op(port_cfg
->port
, OP_EQ
, 0);
4011 tt_int_op(port_cfg
->is_unix_addr
, OP_EQ
, 1);
4012 tt_str_op(port_cfg
->unix_addr
, OP_EQ
, "/foo/bar/unixdomain");
4016 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4017 smartlist_free(slout
);
4021 test_config_parse_port_config__ports__ports_given(void *data
)
4025 smartlist_t
*slout
= NULL
;
4026 port_cfg_t
*port_cfg
= NULL
;
4027 config_line_t
*config_port_invalid
= NULL
, *config_port_valid
= NULL
;
4030 slout
= smartlist_new();
4032 // Test error when encounters an invalid Port specification
4033 config_port_invalid
= mock_config_line("DNSPort", "");
4034 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0, NULL
,
4036 tt_int_op(ret
, OP_EQ
, -1);
4038 // Test error when encounters an empty unix domain specification
4039 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4040 config_port_invalid
= mock_config_line("DNSPort", "unix:");
4041 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0, NULL
,
4043 tt_int_op(ret
, OP_EQ
, -1);
4045 // Test error when encounters a unix domain specification but the listener
4046 // doesn't support domain sockets
4047 config_port_valid
= mock_config_line("DNSPort", "unix:/tmp/foo/bar");
4048 ret
= parse_port_config(NULL
, config_port_valid
, "DNS",
4049 CONN_TYPE_AP_DNS_LISTENER
, NULL
, 0, 0);
4050 tt_int_op(ret
, OP_EQ
, -1);
4052 // Test valid unix domain
4053 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4054 smartlist_clear(slout
);
4055 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4056 CONN_TYPE_AP_LISTENER
, NULL
, 0, 0);
4058 tt_int_op(ret
, OP_EQ
, -1);
4060 tt_int_op(ret
, OP_EQ
, 0);
4061 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4062 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4063 tt_int_op(port_cfg
->port
, OP_EQ
, 0);
4064 tt_int_op(port_cfg
->is_unix_addr
, OP_EQ
, 1);
4065 tt_str_op(port_cfg
->unix_addr
, OP_EQ
, "/tmp/foo/bar");
4066 /* Test entry port defaults as initialised in parse_port_config */
4067 tt_int_op(port_cfg
->entry_cfg
.dns_request
, OP_EQ
, 1);
4068 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 1);
4069 tt_int_op(port_cfg
->entry_cfg
.onion_traffic
, OP_EQ
, 1);
4070 tt_int_op(port_cfg
->entry_cfg
.cache_ipv4_answers
, OP_EQ
, 0);
4071 tt_int_op(port_cfg
->entry_cfg
.prefer_ipv6_virtaddr
, OP_EQ
, 1);
4072 #endif /* defined(_WIN32) */
4074 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
4075 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4076 config_port_invalid
= mock_config_line("SOCKSPort",
4077 "unix:/tmp/foo/bar NoIPv4Traffic "
4080 ret
= parse_port_config(NULL
, config_port_invalid
, "SOCKS",
4081 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4082 CL_PORT_TAKES_HOSTNAMES
);
4083 tt_int_op(ret
, OP_EQ
, -1);
4085 // Test failure if we have no DNS and we're a DNSPort
4086 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4087 config_port_invalid
= mock_config_line("DNSPort",
4088 "127.0.0.1:80 NoDNSRequest");
4089 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS",
4090 CONN_TYPE_AP_DNS_LISTENER
, NULL
, 0,
4091 CL_PORT_TAKES_HOSTNAMES
);
4092 tt_int_op(ret
, OP_EQ
, -1);
4094 // If we're a DNSPort, DNS only is ok
4095 // Use a port because DNSPort doesn't support sockets
4096 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4097 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4098 smartlist_clear(slout
);
4099 config_port_valid
= mock_config_line("DNSPort", "127.0.0.1:80 "
4101 "NoIPv4Traffic NoOnionTraffic");
4102 ret
= parse_port_config(slout
, config_port_valid
, "DNS",
4103 CONN_TYPE_AP_DNS_LISTENER
, NULL
, 0,
4104 CL_PORT_TAKES_HOSTNAMES
);
4105 tt_int_op(ret
, OP_EQ
, 0);
4106 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4107 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4108 tt_int_op(port_cfg
->entry_cfg
.dns_request
, OP_EQ
, 1);
4109 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 0);
4110 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 0);
4111 tt_int_op(port_cfg
->entry_cfg
.onion_traffic
, OP_EQ
, 0);
4113 // Test failure if we have DNS but no ipv4 and no ipv6
4114 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4115 config_port_invalid
= mock_config_line("SOCKSPort",
4117 "unix:/tmp/foo/bar NoIPv4Traffic");
4118 ret
= parse_port_config(NULL
, config_port_invalid
, "SOCKS",
4119 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4120 CL_PORT_TAKES_HOSTNAMES
);
4121 tt_int_op(ret
, OP_EQ
, -1);
4123 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
4125 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4126 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4127 smartlist_clear(slout
);
4128 config_port_valid
= mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4130 "NoDNSRequest NoIPv4Traffic");
4131 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4132 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4133 CL_PORT_TAKES_HOSTNAMES
);
4135 tt_int_op(ret
, OP_EQ
, -1);
4137 tt_int_op(ret
, OP_EQ
, 0);
4138 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4139 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4140 tt_int_op(port_cfg
->entry_cfg
.dns_request
, OP_EQ
, 0);
4141 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 0);
4142 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 0);
4143 tt_int_op(port_cfg
->entry_cfg
.onion_traffic
, OP_EQ
, 1);
4144 #endif /* defined(_WIN32) */
4146 // Test success with quoted unix: address.
4147 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4148 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4149 smartlist_clear(slout
);
4150 config_port_valid
= mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
4152 "NoDNSRequest NoIPv4Traffic");
4153 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4154 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4155 CL_PORT_TAKES_HOSTNAMES
);
4157 tt_int_op(ret
, OP_EQ
, -1);
4159 tt_int_op(ret
, OP_EQ
, 0);
4160 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4161 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4162 tt_int_op(port_cfg
->entry_cfg
.dns_request
, OP_EQ
, 0);
4163 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 0);
4164 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 0);
4165 tt_int_op(port_cfg
->entry_cfg
.onion_traffic
, OP_EQ
, 1);
4166 #endif /* defined(_WIN32) */
4168 // Test failure with broken quoted unix: address.
4169 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4170 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4171 smartlist_clear(slout
);
4172 config_port_valid
= mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
4174 "NoDNSRequest NoIPv4Traffic");
4175 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4176 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4177 CL_PORT_TAKES_HOSTNAMES
);
4178 tt_int_op(ret
, OP_EQ
, -1);
4180 // Test failure with empty quoted unix: address.
4181 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4182 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4183 smartlist_clear(slout
);
4184 config_port_valid
= mock_config_line("SOCKSPort", "unix:\"\" "
4186 "NoDNSRequest NoIPv4Traffic");
4187 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4188 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4189 CL_PORT_TAKES_HOSTNAMES
);
4190 tt_int_op(ret
, OP_EQ
, -1);
4192 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
4193 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4194 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4195 smartlist_clear(slout
);
4196 config_port_valid
= mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4197 "OnionTrafficOnly");
4198 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4199 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4200 CL_PORT_TAKES_HOSTNAMES
);
4202 tt_int_op(ret
, OP_EQ
, -1);
4204 tt_int_op(ret
, OP_EQ
, 0);
4205 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4206 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4207 tt_int_op(port_cfg
->entry_cfg
.dns_request
, OP_EQ
, 0);
4208 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 0);
4209 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 0);
4210 tt_int_op(port_cfg
->entry_cfg
.onion_traffic
, OP_EQ
, 1);
4211 #endif /* defined(_WIN32) */
4213 // Test success with no ipv4 but take ipv6
4214 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4215 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4216 smartlist_clear(slout
);
4217 config_port_valid
= mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4218 "NoIPv4Traffic IPv6Traffic");
4219 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4220 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4221 CL_PORT_TAKES_HOSTNAMES
);
4223 tt_int_op(ret
, OP_EQ
, -1);
4225 tt_int_op(ret
, OP_EQ
, 0);
4226 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4227 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4228 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 0);
4229 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 1);
4230 #endif /* defined(_WIN32) */
4232 // Test success with both ipv4 and ipv6
4233 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4234 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4235 smartlist_clear(slout
);
4236 config_port_valid
= mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4237 "IPv4Traffic IPv6Traffic");
4238 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4239 CONN_TYPE_AP_LISTENER
, NULL
, 0,
4240 CL_PORT_TAKES_HOSTNAMES
);
4242 tt_int_op(ret
, OP_EQ
, -1);
4244 tt_int_op(ret
, OP_EQ
, 0);
4245 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4246 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4247 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 1);
4248 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 1);
4249 #endif /* defined(_WIN32) */
4251 // Test failure if we specify world writable for an IP Port
4252 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4253 config_port_invalid
= mock_config_line("DNSPort", "42 WorldWritable");
4254 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0,
4256 tt_int_op(ret
, OP_EQ
, -1);
4258 // Test failure if we specify group writable for an IP Port
4259 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4260 config_port_invalid
= mock_config_line("DNSPort", "42 GroupWritable");
4261 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0,
4263 tt_int_op(ret
, OP_EQ
, -1);
4265 // Test failure if we specify group writable for an IP Port
4266 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4267 config_port_invalid
= mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4268 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0,
4270 tt_int_op(ret
, OP_EQ
, -1);
4272 // Test success with only a port (this will fail without a default address)
4273 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4274 config_port_valid
= mock_config_line("DNSPort", "42");
4275 ret
= parse_port_config(NULL
, config_port_valid
, "DNS", 0,
4277 tt_int_op(ret
, OP_EQ
, 0);
4279 // Test success with only a port and isolate destination port
4280 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4281 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4282 smartlist_clear(slout
);
4283 config_port_valid
= mock_config_line("DNSPort", "42 IsolateDestPort");
4284 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4286 tt_int_op(ret
, OP_EQ
, 0);
4287 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4288 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4289 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4290 ISO_DEFAULT
| ISO_DESTPORT
);
4292 // Test success with a negative isolate destination port, and plural
4293 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4294 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4295 smartlist_clear(slout
);
4296 config_port_valid
= mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4297 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4299 tt_int_op(ret
, OP_EQ
, 0);
4300 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4301 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4302 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4303 ISO_DEFAULT
& ~ISO_DESTPORT
);
4305 // Test success with isolate destination address
4306 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4307 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4308 smartlist_clear(slout
);
4309 config_port_valid
= mock_config_line("DNSPort", "42 IsolateDestAddr");
4310 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4312 tt_int_op(ret
, OP_EQ
, 0);
4313 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4314 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4315 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4316 ISO_DEFAULT
| ISO_DESTADDR
);
4318 // Test success with isolate socks AUTH
4319 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4320 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4321 smartlist_clear(slout
);
4322 config_port_valid
= mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
4323 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4325 tt_int_op(ret
, OP_EQ
, 0);
4326 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4327 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4328 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4329 ISO_DEFAULT
| ISO_SOCKSAUTH
);
4331 // Test success with isolate client protocol
4332 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4333 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4334 smartlist_clear(slout
);
4335 config_port_valid
= mock_config_line("DNSPort", "42 IsolateClientProtocol");
4336 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4338 tt_int_op(ret
, OP_EQ
, 0);
4339 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4340 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4341 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4342 ISO_DEFAULT
| ISO_CLIENTPROTO
);
4344 // Test success with isolate client address
4345 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4346 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4347 smartlist_clear(slout
);
4348 config_port_valid
= mock_config_line("DNSPort", "42 IsolateClientAddr");
4349 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4351 tt_int_op(ret
, OP_EQ
, 0);
4352 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4353 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4354 tt_int_op(port_cfg
->entry_cfg
.isolation_flags
, OP_EQ
,
4355 ISO_DEFAULT
| ISO_CLIENTADDR
);
4357 // Test success with ignored unknown options
4358 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4359 config_port_valid
= mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4360 ret
= parse_port_config(NULL
, config_port_valid
, "DNS", 0,
4362 tt_int_op(ret
, OP_EQ
, 0);
4364 // Test success with no isolate socks AUTH
4365 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4366 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4367 smartlist_clear(slout
);
4368 config_port_valid
= mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
4369 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4371 tt_int_op(ret
, OP_EQ
, 0);
4372 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4373 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4374 tt_int_op(port_cfg
->entry_cfg
.socks_prefer_no_auth
, OP_EQ
, 1);
4376 // Test success with prefer ipv6
4377 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4378 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4379 smartlist_clear(slout
);
4380 config_port_valid
= mock_config_line("SOCKSPort",
4381 "42 IPv6Traffic PreferIPv6");
4382 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4383 CONN_TYPE_AP_LISTENER
, "127.0.0.42", 0,
4384 CL_PORT_TAKES_HOSTNAMES
);
4385 tt_int_op(ret
, OP_EQ
, 0);
4386 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4387 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4388 tt_int_op(port_cfg
->entry_cfg
.prefer_ipv6
, OP_EQ
, 1);
4390 // Test success with cache ipv4 DNS
4391 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4392 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4393 smartlist_clear(slout
);
4394 config_port_valid
= mock_config_line("DNSPort", "42 CacheIPv4DNS");
4395 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4396 "127.0.0.42", 0, 0);
4397 tt_int_op(ret
, OP_EQ
, 0);
4398 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4399 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4400 tt_int_op(port_cfg
->entry_cfg
.cache_ipv4_answers
, OP_EQ
, 1);
4401 tt_int_op(port_cfg
->entry_cfg
.cache_ipv6_answers
, OP_EQ
, 0);
4403 // Test success with cache ipv6 DNS
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", "42 CacheIPv6DNS");
4408 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4409 "127.0.0.42", 0, 0);
4410 tt_int_op(ret
, OP_EQ
, 0);
4411 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4412 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4413 tt_int_op(port_cfg
->entry_cfg
.cache_ipv4_answers
, OP_EQ
, 0);
4414 tt_int_op(port_cfg
->entry_cfg
.cache_ipv6_answers
, OP_EQ
, 1);
4416 // Test success with no cache ipv4 DNS
4417 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4418 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4419 smartlist_clear(slout
);
4420 config_port_valid
= mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4421 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4422 "127.0.0.42", 0, 0);
4423 tt_int_op(ret
, OP_EQ
, 0);
4424 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4425 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4426 tt_int_op(port_cfg
->entry_cfg
.cache_ipv4_answers
, OP_EQ
, 0);
4427 tt_int_op(port_cfg
->entry_cfg
.cache_ipv6_answers
, OP_EQ
, 0);
4429 // Test success with cache DNS
4430 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4431 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4432 smartlist_clear(slout
);
4433 config_port_valid
= mock_config_line("DNSPort", "42 CacheDNS");
4434 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4435 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES
);
4436 tt_int_op(ret
, OP_EQ
, 0);
4437 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4438 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4439 tt_int_op(port_cfg
->entry_cfg
.cache_ipv4_answers
, OP_EQ
, 1);
4440 tt_int_op(port_cfg
->entry_cfg
.cache_ipv6_answers
, OP_EQ
, 1);
4442 // Test success with use cached ipv4 DNS
4443 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4444 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4445 smartlist_clear(slout
);
4446 config_port_valid
= mock_config_line("DNSPort", "42 UseIPv4Cache");
4447 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4448 "127.0.0.42", 0, 0);
4449 tt_int_op(ret
, OP_EQ
, 0);
4450 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4451 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4452 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv4_answers
, OP_EQ
, 1);
4453 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv6_answers
, OP_EQ
, 0);
4455 // Test success with use cached ipv6 DNS
4456 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4457 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4458 smartlist_clear(slout
);
4459 config_port_valid
= mock_config_line("DNSPort", "42 UseIPv6Cache");
4460 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4461 "127.0.0.42", 0, 0);
4462 tt_int_op(ret
, OP_EQ
, 0);
4463 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4464 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4465 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv4_answers
, OP_EQ
, 0);
4466 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv6_answers
, OP_EQ
, 1);
4468 // Test success with use cached DNS
4469 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4470 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4471 smartlist_clear(slout
);
4472 config_port_valid
= mock_config_line("DNSPort", "42 UseDNSCache");
4473 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4474 "127.0.0.42", 0, 0);
4475 tt_int_op(ret
, OP_EQ
, 0);
4476 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4477 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4478 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv4_answers
, OP_EQ
, 1);
4479 tt_int_op(port_cfg
->entry_cfg
.use_cached_ipv6_answers
, OP_EQ
, 1);
4481 // Test success with not preferring ipv6 automap
4482 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4483 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4484 smartlist_clear(slout
);
4485 config_port_valid
= mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4486 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4487 "127.0.0.42", 0, 0);
4488 tt_int_op(ret
, OP_EQ
, 0);
4489 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4490 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4491 tt_int_op(port_cfg
->entry_cfg
.prefer_ipv6_virtaddr
, OP_EQ
, 0);
4493 // Test success with prefer SOCKS no auth
4494 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4495 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4496 smartlist_clear(slout
);
4497 config_port_valid
= mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4498 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4499 "127.0.0.42", 0, 0);
4500 tt_int_op(ret
, OP_EQ
, 0);
4501 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4502 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4503 tt_int_op(port_cfg
->entry_cfg
.socks_prefer_no_auth
, OP_EQ
, 1);
4505 // Test failure with both a zero port and a non-zero port
4506 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4507 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4508 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4509 smartlist_clear(slout
);
4510 config_port_invalid
= mock_config_line("DNSPort", "0");
4511 config_port_valid
= mock_config_line("DNSPort", "42");
4512 config_port_invalid
->next
= config_port_valid
;
4513 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4514 "127.0.0.42", 0, 0);
4515 tt_int_op(ret
, OP_EQ
, -1);
4517 // Test success with warn non-local control
4518 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4519 smartlist_clear(slout
);
4520 ret
= parse_port_config(slout
, config_port_valid
, "Control",
4521 CONN_TYPE_CONTROL_LISTENER
, "127.0.0.42", 0,
4522 CL_PORT_WARN_NONLOCAL
);
4523 tt_int_op(ret
, OP_EQ
, 0);
4525 // Test success with warn non-local listener
4526 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4527 smartlist_clear(slout
);
4528 ret
= parse_port_config(slout
, config_port_valid
, "ExtOR",
4529 CONN_TYPE_EXT_OR_LISTENER
, "127.0.0.42", 0,
4530 CL_PORT_WARN_NONLOCAL
);
4531 tt_int_op(ret
, OP_EQ
, 0);
4533 // Test success with warn non-local other
4534 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4535 smartlist_clear(slout
);
4536 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4537 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL
);
4538 tt_int_op(ret
, OP_EQ
, 0);
4540 // Test success with warn non-local other without out
4541 ret
= parse_port_config(NULL
, config_port_valid
, "DNS", 0,
4542 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL
);
4543 tt_int_op(ret
, OP_EQ
, 0);
4545 // Test success with both ipv4 and ipv6 but without stream options
4546 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4547 config_port_valid
= NULL
;
4548 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4549 smartlist_clear(slout
);
4550 config_port_valid
= mock_config_line("DNSPort", "42 IPv4Traffic "
4552 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4554 CL_PORT_TAKES_HOSTNAMES
|
4555 CL_PORT_NO_STREAM_OPTIONS
);
4556 tt_int_op(ret
, OP_EQ
, 0);
4557 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4558 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4559 tt_int_op(port_cfg
->entry_cfg
.ipv4_traffic
, OP_EQ
, 1);
4560 tt_int_op(port_cfg
->entry_cfg
.ipv6_traffic
, OP_EQ
, 1);
4562 // Test failure for a SessionGroup argument with invalid value
4563 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4564 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4565 smartlist_clear(slout
);
4566 config_port_invalid
= mock_config_line("DNSPort", "42 SessionGroup=invalid");
4567 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4568 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS
);
4569 tt_int_op(ret
, OP_EQ
, -1);
4571 // Test failure for a SessionGroup argument with valid value but with no
4572 // stream options allowed
4573 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4574 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4575 smartlist_clear(slout
);
4576 config_port_invalid
= mock_config_line("DNSPort", "42 SessionGroup=123");
4577 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4578 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS
);
4579 tt_int_op(ret
, OP_EQ
, -1);
4581 // Test failure for more than one SessionGroup argument
4582 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4583 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4584 smartlist_clear(slout
);
4585 config_port_invalid
= mock_config_line("DNSPort", "42 SessionGroup=123 "
4586 "SessionGroup=321");
4587 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4588 "127.0.0.44", 0, 0);
4589 tt_int_op(ret
, OP_EQ
, -1);
4591 // Test success with a sessiongroup options
4592 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4593 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4594 smartlist_clear(slout
);
4595 config_port_valid
= mock_config_line("DNSPort", "42 SessionGroup=1111122");
4596 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4597 "127.0.0.44", 0, 0);
4598 tt_int_op(ret
, OP_EQ
, 0);
4599 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4600 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4601 tt_int_op(port_cfg
->entry_cfg
.session_group
, OP_EQ
, 1111122);
4603 // Test success with a zero unix domain socket, and doesnt add it to out
4604 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4605 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4606 smartlist_clear(slout
);
4607 config_port_valid
= mock_config_line("DNSPort", "0");
4608 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4609 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET
);
4610 tt_int_op(ret
, OP_EQ
, 0);
4611 tt_int_op(smartlist_len(slout
), OP_EQ
, 0);
4613 // Test success with a one unix domain socket, and doesnt add it to out
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", "something");
4618 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4619 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET
);
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
->is_unix_addr
, OP_EQ
, 1);
4624 tt_str_op(port_cfg
->unix_addr
, OP_EQ
, "something");
4626 // Test success with a port of auto - it uses the default address
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", "auto");
4631 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4632 "127.0.0.46", 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
->port
, OP_EQ
, CFG_AUTO_PORT
);
4637 tor_addr_parse(&addr
, "127.0.0.46");
4638 tt_assert(tor_addr_eq(&port_cfg
->addr
, &addr
))
4640 // Test success with a port of auto in mixed case
4641 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4642 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4643 smartlist_clear(slout
);
4644 config_port_valid
= mock_config_line("DNSPort", "AuTo");
4645 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4646 "127.0.0.46", 0, 0);
4647 tt_int_op(ret
, OP_EQ
, 0);
4648 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4649 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4650 tt_int_op(port_cfg
->port
, OP_EQ
, CFG_AUTO_PORT
);
4651 tor_addr_parse(&addr
, "127.0.0.46");
4652 tt_assert(tor_addr_eq(&port_cfg
->addr
, &addr
))
4654 // Test success with parsing both an address and an auto port
4655 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4656 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4657 smartlist_clear(slout
);
4658 config_port_valid
= mock_config_line("DNSPort", "127.0.0.122:auto");
4659 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4660 "127.0.0.46", 0, 0);
4661 tt_int_op(ret
, OP_EQ
, 0);
4662 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4663 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4664 tt_int_op(port_cfg
->port
, OP_EQ
, CFG_AUTO_PORT
);
4665 tor_addr_parse(&addr
, "127.0.0.122");
4666 tt_assert(tor_addr_eq(&port_cfg
->addr
, &addr
))
4668 // Test failure when asked to parse an invalid address followed by auto
4669 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4670 config_port_invalid
= mock_config_line("DNSPort", "invalidstuff!!:auto");
4671 MOCK(tor_addr_lookup
, mock_tor_addr_lookup__fail_on_bad_addrs
);
4672 ret
= parse_port_config(NULL
, config_port_invalid
, "DNS", 0,
4673 "127.0.0.46", 0, 0);
4674 UNMOCK(tor_addr_lookup
);
4675 tt_int_op(ret
, OP_EQ
, -1);
4677 // Test success with parsing both an address and a real port
4678 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4679 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4680 smartlist_clear(slout
);
4681 config_port_valid
= mock_config_line("DNSPort", "127.0.0.123:656");
4682 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0,
4683 "127.0.0.46", 0, 0);
4684 tt_int_op(ret
, OP_EQ
, 0);
4685 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4686 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4687 tt_int_op(port_cfg
->port
, OP_EQ
, 656);
4688 tor_addr_parse(&addr
, "127.0.0.123");
4689 tt_assert(tor_addr_eq(&port_cfg
->addr
, &addr
))
4691 // Test failure if we can't parse anything at all
4692 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4693 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4694 smartlist_clear(slout
);
4695 config_port_invalid
= mock_config_line("DNSPort", "something wrong");
4696 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4697 "127.0.0.46", 0, 0);
4698 tt_int_op(ret
, OP_EQ
, -1);
4700 // Test failure if we find both an address, a port and an auto
4701 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4702 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4703 smartlist_clear(slout
);
4704 config_port_invalid
= mock_config_line("DNSPort", "127.0.1.0:123:auto");
4705 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0,
4706 "127.0.0.46", 0, 0);
4707 tt_int_op(ret
, OP_EQ
, -1);
4709 // Test that default to group writeable default sets group writeable for
4711 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4712 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4713 smartlist_clear(slout
);
4714 config_port_valid
= mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
4715 ret
= parse_port_config(slout
, config_port_valid
, "SOCKS",
4716 CONN_TYPE_AP_LISTENER
, "127.0.0.46", 0,
4717 CL_PORT_DFLT_GROUP_WRITABLE
);
4719 tt_int_op(ret
, OP_EQ
, -1);
4721 tt_int_op(ret
, OP_EQ
, 0);
4722 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4723 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4724 tt_int_op(port_cfg
->is_group_writable
, OP_EQ
, 1);
4725 #endif /* defined(_WIN32) */
4729 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4730 smartlist_free(slout
);
4731 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4732 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4736 test_config_parse_port_config__ports__server_options(void *data
)
4740 smartlist_t
*slout
= NULL
;
4741 port_cfg_t
*port_cfg
= NULL
;
4742 config_line_t
*config_port_invalid
= NULL
, *config_port_valid
= NULL
;
4744 slout
= smartlist_new();
4746 // Test success with NoAdvertise option
4747 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4748 config_port_valid
= mock_config_line("DNSPort",
4749 "127.0.0.124:656 NoAdvertise");
4750 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0, NULL
, 0,
4751 CL_PORT_SERVER_OPTIONS
);
4752 tt_int_op(ret
, OP_EQ
, 0);
4753 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4754 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4755 tt_int_op(port_cfg
->server_cfg
.no_advertise
, OP_EQ
, 1);
4756 tt_int_op(port_cfg
->server_cfg
.no_listen
, OP_EQ
, 0);
4758 // Test success with NoListen option
4759 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4760 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4761 smartlist_clear(slout
);
4762 config_port_valid
= mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
4763 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0, NULL
, 0,
4764 CL_PORT_SERVER_OPTIONS
);
4765 tt_int_op(ret
, OP_EQ
, 0);
4766 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4767 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4768 tt_int_op(port_cfg
->server_cfg
.no_advertise
, OP_EQ
, 0);
4769 tt_int_op(port_cfg
->server_cfg
.no_listen
, OP_EQ
, 1);
4771 // Test failure with both NoAdvertise and NoListen option
4772 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4773 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4774 smartlist_clear(slout
);
4775 config_port_invalid
= mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
4777 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0, NULL
,
4778 0, CL_PORT_SERVER_OPTIONS
);
4779 tt_int_op(ret
, OP_EQ
, -1);
4781 // Test success with IPv4Only
4782 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4783 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4784 smartlist_clear(slout
);
4785 config_port_valid
= mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
4786 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0, NULL
, 0,
4787 CL_PORT_SERVER_OPTIONS
);
4788 tt_int_op(ret
, OP_EQ
, 0);
4789 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4790 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4791 tt_int_op(port_cfg
->server_cfg
.bind_ipv4_only
, OP_EQ
, 1);
4792 tt_int_op(port_cfg
->server_cfg
.bind_ipv6_only
, OP_EQ
, 0);
4794 // Test success with IPv6Only
4795 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4796 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4797 smartlist_clear(slout
);
4798 config_port_valid
= mock_config_line("DNSPort", "[::1]:656 IPv6Only");
4799 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0, NULL
, 0,
4800 CL_PORT_SERVER_OPTIONS
);
4801 tt_int_op(ret
, OP_EQ
, 0);
4802 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4803 port_cfg
= (port_cfg_t
*)smartlist_get(slout
, 0);
4804 tt_int_op(port_cfg
->server_cfg
.bind_ipv4_only
, OP_EQ
, 0);
4805 tt_int_op(port_cfg
->server_cfg
.bind_ipv6_only
, OP_EQ
, 1);
4807 // Test failure with both IPv4Only and IPv6Only
4808 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4809 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4810 smartlist_clear(slout
);
4811 config_port_invalid
= mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
4813 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0, NULL
,
4814 0, CL_PORT_SERVER_OPTIONS
);
4815 tt_int_op(ret
, OP_EQ
, -1);
4817 // Test success with invalid parameter
4818 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4819 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4820 smartlist_clear(slout
);
4821 config_port_valid
= mock_config_line("DNSPort", "127.0.0.124:656 unknown");
4822 ret
= parse_port_config(slout
, config_port_valid
, "DNS", 0, NULL
, 0,
4823 CL_PORT_SERVER_OPTIONS
);
4824 tt_int_op(ret
, OP_EQ
, 0);
4825 tt_int_op(smartlist_len(slout
), OP_EQ
, 1);
4827 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
4828 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4829 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4830 smartlist_clear(slout
);
4831 config_port_invalid
= mock_config_line("DNSPort",
4832 "127.0.0.124:656 IPv6Only");
4833 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0, NULL
,
4834 0, CL_PORT_SERVER_OPTIONS
);
4835 tt_int_op(ret
, OP_EQ
, -1);
4837 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
4838 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4839 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4840 smartlist_clear(slout
);
4841 config_port_invalid
= mock_config_line("DNSPort", "[::1]:656 IPv4Only");
4842 ret
= parse_port_config(slout
, config_port_invalid
, "DNS", 0, NULL
,
4843 0, CL_PORT_SERVER_OPTIONS
);
4844 tt_int_op(ret
, OP_EQ
, -1);
4846 // Check for failure with empty unix: address.
4847 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4848 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4849 smartlist_clear(slout
);
4850 config_port_invalid
= mock_config_line("ORPort", "unix:\"\"");
4851 ret
= parse_port_config(slout
, config_port_invalid
, "ORPort", 0, NULL
,
4852 0, CL_PORT_SERVER_OPTIONS
);
4853 tt_int_op(ret
, OP_EQ
, -1);
4857 SMARTLIST_FOREACH(slout
,port_cfg_t
*,pf
,port_cfg_free(pf
));
4858 smartlist_free(slout
);
4859 config_free_lines(config_port_invalid
); config_port_invalid
= NULL
;
4860 config_free_lines(config_port_valid
); config_port_valid
= NULL
;
4864 test_config_get_first_advertised(void *data
)
4869 or_options_t
*opts
= options_new();
4871 const tor_addr_t
*addr
;
4873 // no ports are configured? We get NULL.
4874 port
= get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER
,
4876 tt_int_op(port
, OP_EQ
, 0);
4877 addr
= get_first_advertised_addr_by_type_af(CONN_TYPE_OR_LISTENER
,
4879 tt_ptr_op(addr
, OP_EQ
, NULL
);
4881 port
= get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER
,
4883 tt_int_op(port
, OP_EQ
, 0);
4884 addr
= get_first_advertised_addr_by_type_af(CONN_TYPE_OR_LISTENER
,
4886 tt_ptr_op(addr
, OP_EQ
, NULL
);
4888 config_line_append(&opts
->ORPort_lines
, "ORPort", "[1234::5678]:8080");
4889 config_line_append(&opts
->ORPort_lines
, "ORPort",
4890 "1.2.3.4:9999 noadvertise");
4891 config_line_append(&opts
->ORPort_lines
, "ORPort",
4892 "5.6.7.8:9911 nolisten");
4894 r
= parse_ports(opts
, 0, &msg
, &n
, &w
);
4897 // UNSPEC gets us nothing.
4898 port
= get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER
,
4900 tt_int_op(port
, OP_EQ
, 0);
4901 addr
= get_first_advertised_addr_by_type_af(CONN_TYPE_OR_LISTENER
,
4903 tt_ptr_op(addr
, OP_EQ
, NULL
);
4906 port
= get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER
,
4908 tt_int_op(port
, OP_EQ
, 9911);
4909 addr
= get_first_advertised_addr_by_type_af(CONN_TYPE_OR_LISTENER
,
4911 tt_ptr_op(addr
, OP_NE
, NULL
);
4912 tt_str_op(fmt_addrport(addr
,port
), OP_EQ
, "5.6.7.8:9911");
4915 port
= get_first_advertised_port_by_type_af(CONN_TYPE_OR_LISTENER
,
4917 tt_int_op(port
, OP_EQ
, 8080);
4918 addr
= get_first_advertised_addr_by_type_af(CONN_TYPE_OR_LISTENER
,
4920 tt_ptr_op(addr
, OP_NE
, NULL
);
4921 tt_str_op(fmt_addrport(addr
,port
), OP_EQ
, "[1234::5678]:8080");
4924 or_options_free(opts
);
4929 test_config_parse_log_severity(void *data
)
4932 const char *severity_log_lines
[] = {
4933 "debug file /tmp/debug.log",
4934 "debug\tfile /tmp/debug.log",
4935 "[handshake]debug [~net,~mm]info notice stdout",
4936 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
4940 log_severity_list_t
*severity
;
4944 severity
= tor_malloc(sizeof(log_severity_list_t
));
4945 for (i
= 0; severity_log_lines
[i
]; i
++) {
4946 memset(severity
, 0, sizeof(log_severity_list_t
));
4947 ret
= parse_log_severity_config(&severity_log_lines
[i
], severity
);
4948 tt_int_op(ret
, OP_EQ
, 0);
4956 test_config_include_limit(void *data
)
4960 config_line_t
*result
= NULL
;
4961 char *torrc_path
= NULL
;
4962 char *dir
= tor_strdup(get_fname("test_include_limit"));
4963 tt_ptr_op(dir
, OP_NE
, NULL
);
4966 tt_int_op(mkdir(dir
), OP_EQ
, 0);
4968 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
4971 tor_asprintf(&torrc_path
, "%s"PATH_SEPARATOR
"torrc", dir
);
4972 char torrc_contents
[1000];
4973 tor_snprintf(torrc_contents
, sizeof(torrc_contents
), "%%include %s",
4975 tt_int_op(write_str_to_file(torrc_path
, torrc_contents
, 0), OP_EQ
, 0);
4977 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, NULL
, NULL
),
4981 config_free_lines(result
);
4982 tor_free(torrc_path
);
4987 test_config_include_does_not_exist(void *data
)
4991 config_line_t
*result
= NULL
;
4992 char *dir
= tor_strdup(get_fname("test_include_does_not_exist"));
4993 char *missing_path
= NULL
;
4994 tt_ptr_op(dir
, OP_NE
, NULL
);
4997 tt_int_op(mkdir(dir
), OP_EQ
, 0);
4999 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5002 tor_asprintf(&missing_path
, "%s"PATH_SEPARATOR
"missing", dir
);
5003 char torrc_contents
[1000];
5004 tor_snprintf(torrc_contents
, sizeof(torrc_contents
), "%%include %s",
5007 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, NULL
, NULL
),
5011 config_free_lines(result
);
5013 tor_free(missing_path
);
5017 test_config_include_error_in_included_file(void *data
)
5020 config_line_t
*result
= NULL
;
5022 char *dir
= tor_strdup(get_fname("test_error_in_included_file"));
5023 char *invalid_path
= NULL
;
5024 tt_ptr_op(dir
, OP_NE
, NULL
);
5027 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5029 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5032 tor_asprintf(&invalid_path
, "%s"PATH_SEPARATOR
"invalid", dir
);
5033 tt_int_op(write_str_to_file(invalid_path
, "unclosed \"", 0), OP_EQ
, 0);
5035 char torrc_contents
[1000];
5036 tor_snprintf(torrc_contents
, sizeof(torrc_contents
), "%%include %s",
5039 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, NULL
, NULL
),
5043 config_free_lines(result
);
5045 tor_free(invalid_path
);
5049 test_config_include_empty_file_folder(void *data
)
5052 config_line_t
*result
= NULL
;
5054 char *folder_path
= NULL
;
5055 char *file_path
= NULL
;
5056 char *dir
= tor_strdup(get_fname("test_include_empty_file_folder"));
5057 tt_ptr_op(dir
, OP_NE
, NULL
);
5060 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5062 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5065 tor_asprintf(&folder_path
, "%s"PATH_SEPARATOR
"empty_dir", dir
);
5067 tt_int_op(mkdir(folder_path
), OP_EQ
, 0);
5069 tt_int_op(mkdir(folder_path
, 0700), OP_EQ
, 0);
5071 tor_asprintf(&file_path
, "%s"PATH_SEPARATOR
"empty_file", dir
);
5072 tt_int_op(write_str_to_file(file_path
, "", 0), OP_EQ
, 0);
5074 char torrc_contents
[1000];
5075 tor_snprintf(torrc_contents
, sizeof(torrc_contents
),
5078 folder_path
, file_path
);
5081 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0,&include_used
,
5083 tt_ptr_op(result
, OP_EQ
, NULL
);
5084 tt_int_op(include_used
, OP_EQ
, 1);
5087 config_free_lines(result
);
5088 tor_free(folder_path
);
5089 tor_free(file_path
);
5095 test_config_include_no_permission(void *data
)
5098 config_line_t
*result
= NULL
;
5100 char *folder_path
= NULL
;
5105 dir
= tor_strdup(get_fname("test_include_forbidden_folder"));
5106 tt_ptr_op(dir
, OP_NE
, NULL
);
5108 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5110 tor_asprintf(&folder_path
, "%s"PATH_SEPARATOR
"forbidden_dir", dir
);
5111 tt_int_op(mkdir(folder_path
, 0100), OP_EQ
, 0);
5113 char torrc_contents
[1000];
5114 tor_snprintf(torrc_contents
, sizeof(torrc_contents
),
5119 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0,
5120 &include_used
, NULL
),
5122 tt_ptr_op(result
, OP_EQ
, NULL
);
5125 config_free_lines(result
);
5126 tor_free(folder_path
);
5134 test_config_include_recursion_before_after(void *data
)
5138 config_line_t
*result
= NULL
;
5139 char *torrc_path
= NULL
;
5140 char *dir
= tor_strdup(get_fname("test_include_recursion_before_after"));
5141 tt_ptr_op(dir
, OP_NE
, NULL
);
5144 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5146 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5149 tor_asprintf(&torrc_path
, "%s"PATH_SEPARATOR
"torrc", dir
);
5151 char file_contents
[1000];
5152 const int limit
= MAX_INCLUDE_RECURSION_LEVEL
;
5154 // Loop backwards so file_contents has the contents of the first file by the
5156 for (i
= limit
; i
> 0; i
--) {
5158 tor_snprintf(file_contents
, sizeof(file_contents
),
5162 i
, torrc_path
, i
+ 1, 2 * limit
- i
);
5164 tor_snprintf(file_contents
, sizeof(file_contents
), "Test %d\n", i
);
5168 char *file_path
= NULL
;
5169 tor_asprintf(&file_path
, "%s%d", torrc_path
, i
);
5170 tt_int_op(write_str_to_file(file_path
, file_contents
, 0), OP_EQ
, 0);
5171 tor_free(file_path
);
5176 tt_int_op(config_get_lines_include(file_contents
, &result
, 0, &include_used
,
5178 tt_ptr_op(result
, OP_NE
, NULL
);
5179 tt_int_op(include_used
, OP_EQ
, 1);
5182 config_line_t
*next
;
5183 for (next
= result
; next
!= NULL
; next
= next
->next
) {
5185 tor_snprintf(expected
, sizeof(expected
), "%d", len
+ 1);
5186 tt_str_op(next
->key
, OP_EQ
, "Test");
5187 tt_str_op(next
->value
, OP_EQ
, expected
);
5190 tt_int_op(len
, OP_EQ
, 2 * limit
- 1);
5193 config_free_lines(result
);
5195 tor_free(torrc_path
);
5199 test_config_include_recursion_after_only(void *data
)
5203 config_line_t
*result
= NULL
;
5204 char *torrc_path
= NULL
;
5205 char *dir
= tor_strdup(get_fname("test_include_recursion_after_only"));
5206 tt_ptr_op(dir
, OP_NE
, NULL
);
5209 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5211 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5214 tor_asprintf(&torrc_path
, "%s"PATH_SEPARATOR
"torrc", dir
);
5216 char file_contents
[1000];
5217 const int limit
= MAX_INCLUDE_RECURSION_LEVEL
;
5219 // Loop backwards so file_contents has the contents of the first file by the
5221 for (i
= limit
; i
> 0; i
--) {
5222 int n
= (i
- limit
- 1) * -1;
5224 tor_snprintf(file_contents
, sizeof(file_contents
),
5227 torrc_path
, i
+ 1, n
);
5229 tor_snprintf(file_contents
, sizeof(file_contents
), "Test %d\n", n
);
5233 char *file_path
= NULL
;
5234 tor_asprintf(&file_path
, "%s%d", torrc_path
, i
);
5235 tt_int_op(write_str_to_file(file_path
, file_contents
, 0), OP_EQ
, 0);
5236 tor_free(file_path
);
5241 tt_int_op(config_get_lines_include(file_contents
, &result
, 0, &include_used
,
5243 tt_ptr_op(result
, OP_NE
, NULL
);
5244 tt_int_op(include_used
, OP_EQ
, 1);
5247 config_line_t
*next
;
5248 for (next
= result
; next
!= NULL
; next
= next
->next
) {
5250 tor_snprintf(expected
, sizeof(expected
), "%d", len
+ 1);
5251 tt_str_op(next
->key
, OP_EQ
, "Test");
5252 tt_str_op(next
->value
, OP_EQ
, expected
);
5255 tt_int_op(len
, OP_EQ
, limit
);
5258 config_free_lines(result
);
5260 tor_free(torrc_path
);
5264 test_config_include_folder_order(void *data
)
5268 config_line_t
*result
= NULL
;
5269 char *torrcd
= NULL
;
5272 char *dir
= tor_strdup(get_fname("test_include_folder_order"));
5273 tt_ptr_op(dir
, OP_NE
, NULL
);
5276 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5278 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5281 tor_asprintf(&torrcd
, "%s"PATH_SEPARATOR
"%s", dir
, "torrc.d");
5284 tt_int_op(mkdir(torrcd
), OP_EQ
, 0);
5286 tt_int_op(mkdir(torrcd
, 0700), OP_EQ
, 0);
5289 // test that files in subfolders are ignored
5290 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "subfolder");
5293 tt_int_op(mkdir(path
), OP_EQ
, 0);
5295 tt_int_op(mkdir(path
, 0700), OP_EQ
, 0);
5298 tor_asprintf(&path2
, "%s"PATH_SEPARATOR
"%s", path
, "01_ignore");
5299 tt_int_op(write_str_to_file(path2
, "ShouldNotSee 1\n", 0), OP_EQ
, 0);
5302 // test that files starting with . are ignored
5303 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, ".dot");
5304 tt_int_op(write_str_to_file(path
, "ShouldNotSee 2\n", 0), OP_EQ
, 0);
5308 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "01_1st");
5309 tt_int_op(write_str_to_file(path
, "Test 1\n", 0), OP_EQ
, 0);
5312 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "02_2nd");
5313 tt_int_op(write_str_to_file(path
, "Test 2\n", 0), OP_EQ
, 0);
5316 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "aa_3rd");
5317 tt_int_op(write_str_to_file(path
, "Test 3\n", 0), OP_EQ
, 0);
5320 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "ab_4th");
5321 tt_int_op(write_str_to_file(path
, "Test 4\n", 0), OP_EQ
, 0);
5324 char torrc_contents
[1000];
5325 tor_snprintf(torrc_contents
, sizeof(torrc_contents
),
5330 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, &include_used
,
5332 tt_ptr_op(result
, OP_NE
, NULL
);
5333 tt_int_op(include_used
, OP_EQ
, 1);
5336 config_line_t
*next
;
5337 for (next
= result
; next
!= NULL
; next
= next
->next
) {
5339 tor_snprintf(expected
, sizeof(expected
), "%d", len
+ 1);
5340 tt_str_op(next
->key
, OP_EQ
, "Test");
5341 tt_str_op(next
->value
, OP_EQ
, expected
);
5344 tt_int_op(len
, OP_EQ
, 4);
5347 config_free_lines(result
);
5355 test_config_include_blank_file_last(void *data
)
5359 config_line_t
*result
= NULL
;
5360 char *torrcd
= NULL
;
5362 char *dir
= tor_strdup(get_fname("test_include_blank_file_last"));
5363 tt_ptr_op(dir
, OP_NE
, NULL
);
5366 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5368 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5371 tor_asprintf(&torrcd
, "%s"PATH_SEPARATOR
"%s", dir
, "torrc.d");
5374 tt_int_op(mkdir(torrcd
), OP_EQ
, 0);
5376 tt_int_op(mkdir(torrcd
, 0700), OP_EQ
, 0);
5379 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "aa_1st");
5380 tt_int_op(write_str_to_file(path
, "Test 1\n", 0), OP_EQ
, 0);
5383 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "bb_2nd");
5384 tt_int_op(write_str_to_file(path
, "Test 2\n", 0), OP_EQ
, 0);
5387 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", torrcd
, "cc_comment");
5388 tt_int_op(write_str_to_file(path
, "# comment only\n", 0), OP_EQ
, 0);
5391 char torrc_contents
[1000];
5392 tor_snprintf(torrc_contents
, sizeof(torrc_contents
),
5398 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, &include_used
,
5400 tt_ptr_op(result
, OP_NE
, NULL
);
5401 tt_int_op(include_used
, OP_EQ
, 1);
5404 config_line_t
*next
;
5405 for (next
= result
; next
!= NULL
; next
= next
->next
) {
5407 tor_snprintf(expected
, sizeof(expected
), "%d", len
+ 1);
5408 tt_str_op(next
->key
, OP_EQ
, "Test");
5409 tt_str_op(next
->value
, OP_EQ
, expected
);
5412 tt_int_op(len
, OP_EQ
, 3);
5415 config_free_lines(result
);
5422 test_config_include_path_syntax(void *data
)
5426 config_line_t
*result
= NULL
;
5427 char *dir
= tor_strdup(get_fname("test_include_path_syntax"));
5428 char *esc_dir
= NULL
, *dir_with_pathsep
= NULL
,
5429 *esc_dir_with_pathsep
= NULL
, *torrc_contents
= NULL
;
5430 tt_ptr_op(dir
, OP_NE
, NULL
);
5433 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5435 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5438 esc_dir
= esc_for_log(dir
);
5439 tor_asprintf(&dir_with_pathsep
, "%s%s", dir
, PATH_SEPARATOR
);
5440 esc_dir_with_pathsep
= esc_for_log(dir_with_pathsep
);
5442 tor_asprintf(&torrc_contents
,
5444 "%%include %s%s \n" // space to avoid suppressing newline
5447 dir
, PATH_SEPARATOR
,
5448 esc_dir_with_pathsep
);
5451 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0,&include_used
,
5453 tt_ptr_op(result
, OP_EQ
, NULL
);
5454 tt_int_op(include_used
, OP_EQ
, 1);
5457 config_free_lines(result
);
5459 tor_free(torrc_contents
);
5461 tor_free(dir_with_pathsep
);
5462 tor_free(esc_dir_with_pathsep
);
5466 test_config_include_not_processed(void *data
)
5470 char torrc_contents
[1000] = "%include does_not_exist\n";
5471 config_line_t
*result
= NULL
;
5472 tt_int_op(config_get_lines(torrc_contents
, &result
, 0),OP_EQ
, 0);
5473 tt_ptr_op(result
, OP_NE
, NULL
);
5476 config_line_t
*next
;
5477 for (next
= result
; next
!= NULL
; next
= next
->next
) {
5478 tt_str_op(next
->key
, OP_EQ
, "%include");
5479 tt_str_op(next
->value
, OP_EQ
, "does_not_exist");
5482 tt_int_op(len
, OP_EQ
, 1);
5485 config_free_lines(result
);
5489 test_config_include_has_include(void *data
)
5493 config_line_t
*result
= NULL
;
5494 char *dir
= tor_strdup(get_fname("test_include_has_include"));
5495 tt_ptr_op(dir
, OP_NE
, NULL
);
5498 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5500 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5503 char torrc_contents
[1000] = "Test 1\n";
5506 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0,&include_used
,
5508 tt_int_op(include_used
, OP_EQ
, 0);
5509 config_free_lines(result
);
5511 tor_snprintf(torrc_contents
, sizeof(torrc_contents
), "%%include %s\n", dir
);
5512 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0,&include_used
,
5514 tt_int_op(include_used
, OP_EQ
, 1);
5517 config_free_lines(result
);
5522 test_config_include_flag_both_without(void *data
)
5526 char *errmsg
= NULL
;
5527 char conf_empty
[1000];
5528 tor_snprintf(conf_empty
, sizeof(conf_empty
),
5529 "DataDirectory %s\n",
5531 // test with defaults-torrc and torrc without include
5532 int ret
= options_init_from_string(conf_empty
, conf_empty
, CMD_RUN_UNITTESTS
,
5534 tt_int_op(ret
, OP_EQ
, 0);
5536 const or_options_t
*options
= get_options();
5537 tt_int_op(options
->IncludeUsed
, OP_EQ
, 0);
5544 test_config_include_flag_torrc_only(void *data
)
5548 char *errmsg
= NULL
;
5550 char *dir
= tor_strdup(get_fname("test_include_flag_torrc_only"));
5551 tt_ptr_op(dir
, OP_NE
, NULL
);
5554 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5556 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5559 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", dir
, "dummy");
5560 tt_int_op(write_str_to_file(path
, "\n", 0), OP_EQ
, 0);
5562 char conf_empty
[1000];
5563 tor_snprintf(conf_empty
, sizeof(conf_empty
),
5564 "DataDirectory %s\n",
5566 char conf_include
[1000];
5567 tor_snprintf(conf_include
, sizeof(conf_include
), "%%include %s", path
);
5569 // test with defaults-torrc without include and torrc with include
5570 int ret
= options_init_from_string(conf_empty
, conf_include
,
5571 CMD_RUN_UNITTESTS
, NULL
, &errmsg
);
5572 tt_int_op(ret
, OP_EQ
, 0);
5574 const or_options_t
*options
= get_options();
5575 tt_int_op(options
->IncludeUsed
, OP_EQ
, 1);
5584 test_config_include_flag_defaults_only(void *data
)
5588 char *errmsg
= NULL
;
5590 char *dir
= tor_strdup(get_fname("test_include_flag_defaults_only"));
5591 tt_ptr_op(dir
, OP_NE
, NULL
);
5594 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5596 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5599 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", dir
, "dummy");
5600 tt_int_op(write_str_to_file(path
, "\n", 0), OP_EQ
, 0);
5602 char conf_empty
[1000];
5603 tor_snprintf(conf_empty
, sizeof(conf_empty
),
5604 "DataDirectory %s\n",
5606 char conf_include
[1000];
5607 tor_snprintf(conf_include
, sizeof(conf_include
), "%%include %s", path
);
5609 // test with defaults-torrc with include and torrc without include
5610 int ret
= options_init_from_string(conf_include
, conf_empty
,
5611 CMD_RUN_UNITTESTS
, NULL
, &errmsg
);
5612 tt_int_op(ret
, OP_EQ
, 0);
5614 const or_options_t
*options
= get_options();
5615 tt_int_op(options
->IncludeUsed
, OP_EQ
, 0);
5624 test_config_dup_and_filter(void *arg
)
5627 /* Test normal input. */
5628 config_line_t
*line
= NULL
;
5629 config_line_append(&line
, "abc", "def");
5630 config_line_append(&line
, "ghi", "jkl");
5631 config_line_append(&line
, "ABCD", "mno");
5633 config_line_t
*line_dup
= config_lines_dup_and_filter(line
, "aBc");
5634 tt_ptr_op(line_dup
, OP_NE
, NULL
);
5635 tt_ptr_op(line_dup
->next
, OP_NE
, NULL
);
5636 tt_ptr_op(line_dup
->next
->next
, OP_EQ
, NULL
);
5638 tt_str_op(line_dup
->key
, OP_EQ
, "abc");
5639 tt_str_op(line_dup
->value
, OP_EQ
, "def");
5640 tt_str_op(line_dup
->next
->key
, OP_EQ
, "ABCD");
5641 tt_str_op(line_dup
->next
->value
, OP_EQ
, "mno");
5644 config_free_lines(line_dup
);
5645 line_dup
= config_lines_dup_and_filter(line
, "skdjfsdkljf");
5646 tt_ptr_op(line_dup
, OP_EQ
, NULL
);
5649 config_free_lines(line_dup
);
5650 line_dup
= config_lines_dup_and_filter(NULL
, "abc");
5651 tt_ptr_op(line_dup
, OP_EQ
, NULL
);
5654 config_free_lines(line
);
5655 config_free_lines(line_dup
);
5658 /* If we're not configured to be a bridge, but we set
5659 * BridgeDistribution, then options_validate () should return -1. */
5661 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg
)
5663 or_options_t
* options
= get_options_mutable();
5664 or_options_t
* old_options
= options
;
5665 or_options_t
* default_options
= options
;
5666 char* message
= NULL
;
5671 options
->BridgeRelay
= 0;
5672 options
->BridgeDistribution
= (char*)("https");
5674 ret
= options_validate(old_options
, options
, default_options
, 0, &message
);
5676 tt_int_op(ret
, OP_EQ
, -1);
5677 tt_str_op(message
, OP_EQ
, "You set BridgeDistribution, but you "
5678 "didn't set BridgeRelay!");
5681 options
->BridgeDistribution
= NULL
;
5684 /* If the BridgeDistribution setting was valid, 0 should be returned. */
5686 test_config_check_bridge_distribution_setting_valid(void *arg
)
5690 // Check all the possible values we support right now.
5691 tt_int_op(check_bridge_distribution_setting("none"), OP_EQ
, 0);
5692 tt_int_op(check_bridge_distribution_setting("any"), OP_EQ
, 0);
5693 tt_int_op(check_bridge_distribution_setting("https"), OP_EQ
, 0);
5694 tt_int_op(check_bridge_distribution_setting("email"), OP_EQ
, 0);
5695 tt_int_op(check_bridge_distribution_setting("moat"), OP_EQ
, 0);
5697 // Check all the possible values we support right now with weird casing.
5698 tt_int_op(check_bridge_distribution_setting("NoNe"), OP_EQ
, 0);
5699 tt_int_op(check_bridge_distribution_setting("anY"), OP_EQ
, 0);
5700 tt_int_op(check_bridge_distribution_setting("hTTps"), OP_EQ
, 0);
5701 tt_int_op(check_bridge_distribution_setting("emAIl"), OP_EQ
, 0);
5702 tt_int_op(check_bridge_distribution_setting("moAt"), OP_EQ
, 0);
5705 tt_int_op(check_bridge_distribution_setting("x\rx"), OP_EQ
, -1);
5706 tt_int_op(check_bridge_distribution_setting("x\nx"), OP_EQ
, -1);
5707 tt_int_op(check_bridge_distribution_setting("\t\t\t"), OP_EQ
, -1);
5713 /* If the BridgeDistribution setting was invalid, -1 should be returned. */
5715 test_config_check_bridge_distribution_setting_invalid(void *arg
)
5717 int ret
= check_bridge_distribution_setting("hyphens-are-allowed");
5721 tt_int_op(ret
, OP_EQ
, 0);
5723 ret
= check_bridge_distribution_setting("asterisks*are*forbidden");
5725 tt_int_op(ret
, OP_EQ
, -1);
5730 /* If the BridgeDistribution setting was unrecognised, a warning should be
5731 * logged and 0 should be returned. */
5733 test_config_check_bridge_distribution_setting_unrecognised(void *arg
)
5735 int ret
= check_bridge_distribution_setting("unicorn");
5739 tt_int_op(ret
, OP_EQ
, 0);
5745 test_config_include_opened_file_list(void *data
)
5749 config_line_t
*result
= NULL
;
5750 smartlist_t
*opened_files
= smartlist_new();
5751 char *torrcd
= NULL
;
5752 char *subfolder
= NULL
;
5757 char *dir
= tor_strdup(get_fname("test_include_opened_file_list"));
5758 tt_ptr_op(dir
, OP_NE
, NULL
);
5761 tt_int_op(mkdir(dir
), OP_EQ
, 0);
5763 tt_int_op(mkdir(dir
, 0700), OP_EQ
, 0);
5766 tor_asprintf(&torrcd
, "%s"PATH_SEPARATOR
"%s", dir
, "torrc.d");
5769 tt_int_op(mkdir(torrcd
), OP_EQ
, 0);
5771 tt_int_op(mkdir(torrcd
, 0700), OP_EQ
, 0);
5774 tor_asprintf(&subfolder
, "%s"PATH_SEPARATOR
"%s", torrcd
, "subfolder");
5777 tt_int_op(mkdir(subfolder
), OP_EQ
, 0);
5779 tt_int_op(mkdir(subfolder
, 0700), OP_EQ
, 0);
5782 tor_asprintf(&path
, "%s"PATH_SEPARATOR
"%s", subfolder
,
5783 "01_file_in_subfolder");
5784 tt_int_op(write_str_to_file(path
, "Test 1\n", 0), OP_EQ
, 0);
5786 tor_asprintf(&empty
, "%s"PATH_SEPARATOR
"%s", torrcd
, "empty");
5787 tt_int_op(write_str_to_file(empty
, "", 0), OP_EQ
, 0);
5789 tor_asprintf(&file
, "%s"PATH_SEPARATOR
"%s", torrcd
, "file");
5790 tt_int_op(write_str_to_file(file
, "Test 2\n", 0), OP_EQ
, 0);
5792 tor_asprintf(&dot
, "%s"PATH_SEPARATOR
"%s", torrcd
, ".dot");
5793 tt_int_op(write_str_to_file(dot
, "Test 3\n", 0), OP_EQ
, 0);
5795 char torrc_contents
[1000];
5796 tor_snprintf(torrc_contents
, sizeof(torrc_contents
),
5801 tt_int_op(config_get_lines_include(torrc_contents
, &result
, 0, &include_used
,
5802 opened_files
), OP_EQ
, 0);
5803 tt_ptr_op(result
, OP_NE
, NULL
);
5804 tt_int_op(include_used
, OP_EQ
, 1);
5806 tt_int_op(smartlist_len(opened_files
), OP_EQ
, 4);
5807 tt_int_op(smartlist_contains_string(opened_files
, torrcd
), OP_EQ
, 1);
5808 tt_int_op(smartlist_contains_string(opened_files
, subfolder
), OP_EQ
, 1);
5809 // files inside subfolders are not opended, only the subfolder is opened
5810 tt_int_op(smartlist_contains_string(opened_files
, empty
), OP_EQ
, 1);
5811 tt_int_op(smartlist_contains_string(opened_files
, file
), OP_EQ
, 1);
5812 // dot files are not opened as we ignore them when we get their name from
5813 // their parent folder
5816 SMARTLIST_FOREACH(opened_files
, char *, f
, tor_free(f
));
5817 smartlist_free(opened_files
);
5818 config_free_lines(result
);
5820 tor_free(subfolder
);
5829 test_config_compute_max_mem_in_queues(void *data
)
5831 #define GIGABYTE(x) (UINT64_C(x) << 30)
5832 #define MEGABYTE(x) (UINT64_C(x) << 20)
5834 MOCK(get_total_system_memory
, get_total_system_memory_mock
);
5836 /* We are unable to detect the amount of memory on the system. Tor will try
5837 * to use some sensible default values for 64-bit and 32-bit systems. */
5838 total_system_memory_return
= -1;
5840 #if SIZEOF_VOID_P >= 8
5841 /* We are on a 64-bit system. */
5842 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
, GIGABYTE(8));
5844 /* We are on a 32-bit system. */
5845 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
, GIGABYTE(1));
5848 /* We are able to detect the amount of RAM on the system. */
5849 total_system_memory_return
= 0;
5851 /* We are running on a system with one gigabyte of RAM. */
5852 total_system_memory_output
= GIGABYTE(1);
5854 /* We have 0.75 * RAM available. */
5855 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
,
5856 3 * (GIGABYTE(1) / 4));
5858 /* We are running on a tiny machine with 256 MB of RAM. */
5859 total_system_memory_output
= MEGABYTE(256);
5861 /* We will now enforce a minimum of 256 MB of RAM available for the
5862 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
5864 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
, MEGABYTE(256));
5866 #if SIZEOF_SIZE_T > 4
5867 /* We are running on a machine with 8 GB of RAM. */
5868 total_system_memory_output
= GIGABYTE(8);
5870 /* We will have 0.4 * RAM available. */
5871 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
,
5872 2 * (GIGABYTE(8) / 5));
5874 /* We are running on a machine with 16 GB of RAM. */
5875 total_system_memory_output
= GIGABYTE(16);
5877 /* We will have 0.4 * RAM available. */
5878 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
,
5879 2 * (GIGABYTE(16) / 5));
5881 /* We are running on a machine with 32 GB of RAM. */
5882 total_system_memory_output
= GIGABYTE(32);
5884 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
5885 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ
,
5886 MAX_DEFAULT_MEMORY_QUEUE_SIZE
);
5890 UNMOCK(get_total_system_memory
);
5897 test_config_extended_fmt(void *arg
)
5900 config_line_t
*lines
= NULL
, *lp
;
5901 const char string1
[] =
5903 "+thing2 is over here\n"
5905 "/thing4 is back here\n";
5907 /* Try with the "extended" flag disabled. */
5908 int r
= config_get_lines(string1
, &lines
, 0);
5909 tt_int_op(r
, OP_EQ
, 0);
5911 tt_ptr_op(lp
, OP_NE
, NULL
);
5912 tt_str_op(lp
->key
, OP_EQ
, "thing1");
5913 tt_str_op(lp
->value
, OP_EQ
, "is here");
5914 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_NORMAL
);
5916 tt_ptr_op(lp
, OP_NE
, NULL
);
5917 tt_str_op(lp
->key
, OP_EQ
, "+thing2");
5918 tt_str_op(lp
->value
, OP_EQ
, "is over here");
5919 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_NORMAL
);
5921 tt_ptr_op(lp
, OP_NE
, NULL
);
5922 tt_str_op(lp
->key
, OP_EQ
, "/thing3");
5923 tt_str_op(lp
->value
, OP_EQ
, "");
5924 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_NORMAL
);
5926 tt_ptr_op(lp
, OP_NE
, NULL
);
5927 tt_str_op(lp
->key
, OP_EQ
, "/thing4");
5928 tt_str_op(lp
->value
, OP_EQ
, "is back here");
5929 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_NORMAL
);
5931 config_free_lines(lines
);
5933 /* Try with the "extended" flag enabled. */
5934 r
= config_get_lines(string1
, &lines
, 1);
5935 tt_int_op(r
, OP_EQ
, 0);
5937 tt_ptr_op(lp
, OP_NE
, NULL
);
5938 tt_str_op(lp
->key
, OP_EQ
, "thing1");
5939 tt_str_op(lp
->value
, OP_EQ
, "is here");
5940 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_NORMAL
);
5942 tt_ptr_op(lp
, OP_NE
, NULL
);
5943 tt_str_op(lp
->key
, OP_EQ
, "thing2");
5944 tt_str_op(lp
->value
, OP_EQ
, "is over here");
5945 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_APPEND
);
5947 tt_ptr_op(lp
, OP_NE
, NULL
);
5948 tt_str_op(lp
->key
, OP_EQ
, "thing3");
5949 tt_str_op(lp
->value
, OP_EQ
, "");
5950 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_CLEAR
);
5952 tt_ptr_op(lp
, OP_NE
, NULL
);
5953 tt_str_op(lp
->key
, OP_EQ
, "thing4");
5954 tt_str_op(lp
->value
, OP_EQ
, "");
5955 tt_int_op(lp
->command
, OP_EQ
, CONFIG_LINE_CLEAR
);
5959 config_free_lines(lines
);
5962 #define CONFIG_TEST(name, flags) \
5963 { #name, test_config_ ## name, flags, NULL, NULL }
5965 struct testcase_t config_tests
[] = {
5966 CONFIG_TEST(adding_trusted_dir_server
, TT_FORK
),
5967 CONFIG_TEST(adding_fallback_dir_server
, TT_FORK
),
5968 CONFIG_TEST(parsing_trusted_dir_server
, 0),
5969 CONFIG_TEST(parsing_invalid_dir_address
, 0),
5970 CONFIG_TEST(parsing_fallback_dir_server
, 0),
5971 CONFIG_TEST(adding_default_trusted_dir_servers
, TT_FORK
),
5972 CONFIG_TEST(adding_dir_servers
, TT_FORK
),
5973 CONFIG_TEST(default_dir_servers
, TT_FORK
),
5974 CONFIG_TEST(default_fallback_dirs
, 0),
5975 CONFIG_TEST(resolve_my_address
, TT_FORK
),
5976 CONFIG_TEST(addressmap
, 0),
5977 CONFIG_TEST(parse_bridge_line
, 0),
5978 CONFIG_TEST(parse_transport_options_line
, 0),
5979 CONFIG_TEST(parse_transport_plugin_line
, TT_FORK
),
5980 CONFIG_TEST(check_or_create_data_subdir
, TT_FORK
),
5981 CONFIG_TEST(write_to_data_subdir
, TT_FORK
),
5982 CONFIG_TEST(fix_my_family
, 0),
5983 CONFIG_TEST(directory_fetch
, 0),
5984 CONFIG_TEST(port_cfg_line_extract_addrport
, 0),
5985 CONFIG_TEST(parse_port_config__ports__no_ports_given
, 0),
5986 CONFIG_TEST(parse_port_config__ports__server_options
, 0),
5987 CONFIG_TEST(parse_port_config__ports__ports_given
, 0),
5988 CONFIG_TEST(get_first_advertised
, TT_FORK
),
5989 CONFIG_TEST(parse_log_severity
, 0),
5990 CONFIG_TEST(include_limit
, 0),
5991 CONFIG_TEST(include_does_not_exist
, 0),
5992 CONFIG_TEST(include_error_in_included_file
, 0),
5993 CONFIG_TEST(include_empty_file_folder
, 0),
5995 CONFIG_TEST(include_no_permission
, 0),
5997 CONFIG_TEST(include_recursion_before_after
, 0),
5998 CONFIG_TEST(include_recursion_after_only
, 0),
5999 CONFIG_TEST(include_folder_order
, 0),
6000 CONFIG_TEST(include_blank_file_last
, 0),
6001 CONFIG_TEST(include_path_syntax
, 0),
6002 CONFIG_TEST(include_not_processed
, 0),
6003 CONFIG_TEST(include_has_include
, 0),
6004 CONFIG_TEST(include_flag_both_without
, TT_FORK
),
6005 CONFIG_TEST(include_flag_torrc_only
, TT_FORK
),
6006 CONFIG_TEST(include_flag_defaults_only
, TT_FORK
),
6007 CONFIG_TEST(dup_and_filter
, 0),
6008 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge
, TT_FORK
),
6009 CONFIG_TEST(check_bridge_distribution_setting_valid
, 0),
6010 CONFIG_TEST(check_bridge_distribution_setting_invalid
, 0),
6011 CONFIG_TEST(check_bridge_distribution_setting_unrecognised
, 0),
6012 CONFIG_TEST(include_opened_file_list
, 0),
6013 CONFIG_TEST(compute_max_mem_in_queues
, 0),
6014 CONFIG_TEST(extended_fmt
, 0),