Merge branch 'maint-0.2.9' into maint-0.3.3
[tor.git] / src / test / test_config.c
blob2bcc0cad7b8e7428b0be5847b66b2ad528b2bec1
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
8 #define CONFIG_PRIVATE
9 #define PT_PRIVATE
10 #define ROUTERSET_PRIVATE
11 #include "or.h"
12 #include "address.h"
13 #include "addressmap.h"
14 #include "bridges.h"
15 #include "circuitmux_ewma.h"
16 #include "circuitbuild.h"
17 #include "config.h"
18 #include "confparse.h"
19 #include "connection.h"
20 #include "connection_edge.h"
21 #include "test.h"
22 #include "util.h"
23 #include "address.h"
24 #include "connection_or.h"
25 #include "control.h"
26 #include "cpuworker.h"
27 #include "dirserv.h"
28 #include "dirvote.h"
29 #include "dns.h"
30 #include "entrynodes.h"
31 #include "transports.h"
32 #include "ext_orport.h"
33 #include "geoip.h"
34 #include "hibernate.h"
35 #include "main.h"
36 #include "networkstatus.h"
37 #include "nodelist.h"
38 #include "policies.h"
39 #include "rendclient.h"
40 #include "rendservice.h"
41 #include "router.h"
42 #include "routerlist.h"
43 #include "routerset.h"
44 #include "statefile.h"
45 #include "test.h"
46 #include "transports.h"
47 #include "util.h"
49 #include "test_helpers.h"
51 static void
52 test_config_addressmap(void *arg)
54 char buf[1024];
55 char address[256];
56 time_t expires = TIME_MAX;
57 (void)arg;
59 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
60 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
61 "MapAddress *.google.com *.torserver.exit\n"
62 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
63 "MapAddress *.cn.com www.cnn.com\n"
64 "MapAddress *.cnn.com www.cnn.com\n"
65 "MapAddress ex.com www.cnn.com\n"
66 "MapAddress ey.com *.cnn.com\n"
67 "MapAddress www.torproject.org 1.1.1.1\n"
68 "MapAddress other.torproject.org "
69 "this.torproject.org.otherserver.exit\n"
70 "MapAddress test.torproject.org 2.2.2.2\n"
71 "MapAddress www.google.com 3.3.3.3\n"
72 "MapAddress www.example.org 4.4.4.4\n"
73 "MapAddress 4.4.4.4 7.7.7.7\n"
74 "MapAddress 4.4.4.4 5.5.5.5\n"
75 "MapAddress www.infiniteloop.org 6.6.6.6\n"
76 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
77 , sizeof(buf));
79 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
80 config_register_addressmaps(get_options());
82 /* Use old interface for now, so we don't need to rewrite the unit tests */
83 #define addressmap_rewrite(a,s,eo,ao) \
84 addressmap_rewrite((a),(s), ~0, (eo),(ao))
86 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
87 strlcpy(address, "www.invalidwildcard.com", sizeof(address));
88 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
90 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
91 strlcpy(address, "www.invalidasterisk.com", sizeof(address));
92 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
94 /* Where no mapping for FQDN match on top-level domain */
95 /* MapAddress .google.com .torserver.exit */
96 strlcpy(address, "reader.google.com", sizeof(address));
97 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
98 tt_str_op(address,OP_EQ, "reader.torserver.exit");
100 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
101 strlcpy(address, "reader.yahoo.com", sizeof(address));
102 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
103 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
105 /*MapAddress *.cnn.com www.cnn.com */
106 strlcpy(address, "cnn.com", sizeof(address));
107 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
108 tt_str_op(address,OP_EQ, "www.cnn.com");
110 /* MapAddress .cn.com www.cnn.com */
111 strlcpy(address, "www.cn.com", sizeof(address));
112 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
113 tt_str_op(address,OP_EQ, "www.cnn.com");
115 /* MapAddress ex.com www.cnn.com - no match */
116 strlcpy(address, "www.ex.com", sizeof(address));
117 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
119 /* MapAddress ey.com *.cnn.com - invalid expression */
120 strlcpy(address, "ey.com", sizeof(address));
121 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
123 /* Where mapping for FQDN match on FQDN */
124 strlcpy(address, "www.google.com", sizeof(address));
125 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
126 tt_str_op(address,OP_EQ, "3.3.3.3");
128 strlcpy(address, "www.torproject.org", sizeof(address));
129 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
130 tt_str_op(address,OP_EQ, "1.1.1.1");
132 strlcpy(address, "other.torproject.org", sizeof(address));
133 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
134 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
136 strlcpy(address, "test.torproject.org", sizeof(address));
137 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
138 tt_str_op(address,OP_EQ, "2.2.2.2");
140 /* Test a chain of address mappings and the order in which they were added:
141 "MapAddress www.example.org 4.4.4.4"
142 "MapAddress 4.4.4.4 7.7.7.7"
143 "MapAddress 4.4.4.4 5.5.5.5"
145 strlcpy(address, "www.example.org", sizeof(address));
146 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
147 tt_str_op(address,OP_EQ, "5.5.5.5");
149 /* Test infinite address mapping results in no change */
150 strlcpy(address, "www.infiniteloop.org", sizeof(address));
151 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
152 tt_str_op(address,OP_EQ, "www.infiniteloop.org");
154 /* Test we don't find false positives */
155 strlcpy(address, "www.example.com", sizeof(address));
156 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
158 /* Test top-level-domain matching a bit harder */
159 config_free_lines(get_options_mutable()->AddressMap);
160 addressmap_clear_configured();
161 strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
162 "MapAddress *.torproject.org 1.1.1.1\n"
163 "MapAddress *.net 2.2.2.2\n"
164 , sizeof(buf));
165 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
166 config_register_addressmaps(get_options());
168 strlcpy(address, "www.abc.com", sizeof(address));
169 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
170 tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
172 strlcpy(address, "www.def.com", sizeof(address));
173 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
174 tt_str_op(address,OP_EQ, "www.def.torserver.exit");
176 strlcpy(address, "www.torproject.org", sizeof(address));
177 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
178 tt_str_op(address,OP_EQ, "1.1.1.1");
180 strlcpy(address, "test.torproject.org", sizeof(address));
181 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
182 tt_str_op(address,OP_EQ, "1.1.1.1");
184 strlcpy(address, "torproject.net", sizeof(address));
185 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
186 tt_str_op(address,OP_EQ, "2.2.2.2");
188 /* We don't support '*' as a mapping directive */
189 config_free_lines(get_options_mutable()->AddressMap);
190 addressmap_clear_configured();
191 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
192 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
193 config_register_addressmaps(get_options());
195 strlcpy(address, "www.abc.com", sizeof(address));
196 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
198 strlcpy(address, "www.def.net", sizeof(address));
199 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
201 strlcpy(address, "www.torproject.org", sizeof(address));
202 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
204 #undef addressmap_rewrite
206 done:
207 config_free_lines(get_options_mutable()->AddressMap);
208 get_options_mutable()->AddressMap = NULL;
209 addressmap_free_all();
212 static int
213 is_private_dir(const char* path)
215 struct stat st;
216 int r = stat(path, &st);
217 if (r) {
218 return 0;
220 #if !defined (_WIN32)
221 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
222 return 0;
224 #endif
225 return 1;
228 static void
229 test_config_check_or_create_data_subdir(void *arg)
231 or_options_t *options = get_options_mutable();
232 char *datadir;
233 const char *subdir = "test_stats";
234 char *subpath;
235 struct stat st;
236 int r;
237 #if !defined (_WIN32)
238 unsigned group_permission;
239 #endif
240 (void)arg;
242 tor_free(options->DataDirectory);
243 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
244 subpath = get_datadir_fname(subdir);
246 #if defined (_WIN32)
247 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
248 #else
249 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
250 #endif
252 r = stat(subpath, &st);
254 // The subdirectory shouldn't exist yet,
255 // but should be created by the call to check_or_create_data_subdir.
256 tt_assert(r && (errno == ENOENT));
257 tt_assert(!check_or_create_data_subdir(subdir));
258 tt_assert(is_private_dir(subpath));
260 // The check should return 0, if the directory already exists
261 // and is private to the user.
262 tt_assert(!check_or_create_data_subdir(subdir));
264 r = stat(subpath, &st);
265 if (r) {
266 tt_abort_perror("stat");
269 #if !defined (_WIN32)
270 group_permission = st.st_mode | 0070;
271 r = chmod(subpath, group_permission);
273 if (r) {
274 tt_abort_perror("chmod");
277 // If the directory exists, but its mode is too permissive
278 // a call to check_or_create_data_subdir should reset the mode.
279 tt_assert(!is_private_dir(subpath));
280 tt_assert(!check_or_create_data_subdir(subdir));
281 tt_assert(is_private_dir(subpath));
282 #endif /* !defined (_WIN32) */
284 done:
285 rmdir(subpath);
286 tor_free(datadir);
287 tor_free(subpath);
290 static void
291 test_config_write_to_data_subdir(void *arg)
293 or_options_t* options = get_options_mutable();
294 char *datadir;
295 char *cp = NULL;
296 const char* subdir = "test_stats";
297 const char* fname = "test_file";
298 const char* str =
299 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
300 "elitr, sed diam nonumy eirmod\n"
301 "tempor invidunt ut labore et dolore magna aliquyam\n"
302 "erat, sed diam voluptua.\n"
303 "At vero eos et accusam et justo duo dolores et ea\n"
304 "rebum. Stet clita kasd gubergren,\n"
305 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
306 "Lorem ipsum dolor sit amet,\n"
307 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
308 "tempor invidunt ut labore et dolore\n"
309 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
310 "accusam et justo duo dolores et\n"
311 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
312 "sanctus est Lorem ipsum dolor sit amet.";
313 char* filepath = NULL;
314 (void)arg;
316 tor_free(options->DataDirectory);
317 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
318 filepath = get_datadir_fname2(subdir, fname);
320 #if defined (_WIN32)
321 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
322 #else
323 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
324 #endif
326 // Write attempt should fail, if subdirectory doesn't exist.
327 tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
328 tt_assert(! check_or_create_data_subdir(subdir));
330 // Content of file after write attempt should be
331 // equal to the original string.
332 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
333 cp = read_file_to_str(filepath, 0, NULL);
334 tt_str_op(cp,OP_EQ, str);
335 tor_free(cp);
337 // A second write operation should overwrite the old content.
338 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
339 cp = read_file_to_str(filepath, 0, NULL);
340 tt_str_op(cp,OP_EQ, str);
341 tor_free(cp);
343 done:
344 (void) unlink(filepath);
345 rmdir(options->DataDirectory);
346 tor_free(datadir);
347 tor_free(filepath);
348 tor_free(cp);
351 /* Test helper function: Make sure that a bridge line gets parsed
352 * properly. Also make sure that the resulting bridge_line_t structure
353 * has its fields set correctly. */
354 static void
355 good_bridge_line_test(const char *string, const char *test_addrport,
356 const char *test_digest, const char *test_transport,
357 const smartlist_t *test_socks_args)
359 char *tmp = NULL;
360 bridge_line_t *bridge_line = parse_bridge_line(string);
361 tt_assert(bridge_line);
363 /* test addrport */
364 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
365 tt_str_op(test_addrport,OP_EQ, tmp);
366 tor_free(tmp);
368 /* If we were asked to validate a digest, but we did not get a
369 digest after parsing, we failed. */
370 if (test_digest && tor_digest_is_zero(bridge_line->digest))
371 tt_abort();
373 /* If we were not asked to validate a digest, and we got a digest
374 after parsing, we failed again. */
375 if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
376 tt_abort();
378 /* If we were asked to validate a digest, and we got a digest after
379 parsing, make sure it's correct. */
380 if (test_digest) {
381 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
382 tor_strlower(tmp);
383 tt_str_op(test_digest,OP_EQ, tmp);
384 tor_free(tmp);
387 /* If we were asked to validate a transport name, make sure tha it
388 matches with the transport name that was parsed. */
389 if (test_transport && !bridge_line->transport_name)
390 tt_abort();
391 if (!test_transport && bridge_line->transport_name)
392 tt_abort();
393 if (test_transport)
394 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
396 /* Validate the SOCKS argument smartlist. */
397 if (test_socks_args && !bridge_line->socks_args)
398 tt_abort();
399 if (!test_socks_args && bridge_line->socks_args)
400 tt_abort();
401 if (test_socks_args)
402 tt_assert(smartlist_strings_eq(test_socks_args,
403 bridge_line->socks_args));
405 done:
406 tor_free(tmp);
407 bridge_line_free(bridge_line);
410 /* Test helper function: Make sure that a bridge line is
411 * unparseable. */
412 static void
413 bad_bridge_line_test(const char *string)
415 bridge_line_t *bridge_line = parse_bridge_line(string);
416 if (bridge_line)
417 TT_FAIL(("%s was supposed to fail, but it didn't.", string));
418 tt_ptr_op(bridge_line, OP_EQ, NULL);
420 done:
421 bridge_line_free(bridge_line);
424 static void
425 test_config_parse_bridge_line(void *arg)
427 (void) arg;
428 good_bridge_line_test("192.0.2.1:4123",
429 "192.0.2.1:4123", NULL, NULL, NULL);
431 good_bridge_line_test("192.0.2.1",
432 "192.0.2.1:443", NULL, NULL, NULL);
434 good_bridge_line_test("transport [::1]",
435 "[::1]:443", NULL, "transport", NULL);
437 good_bridge_line_test("transport 192.0.2.1:12 "
438 "4352e58420e68f5e40bf7c74faddccd9d1349413",
439 "192.0.2.1:12",
440 "4352e58420e68f5e40bf7c74faddccd9d1349413",
441 "transport", NULL);
444 smartlist_t *sl_tmp = smartlist_new();
445 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
447 good_bridge_line_test("transport 192.0.2.1:12 "
448 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
449 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
450 "transport", sl_tmp);
452 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
453 smartlist_free(sl_tmp);
457 smartlist_t *sl_tmp = smartlist_new();
458 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
459 smartlist_add_asprintf(sl_tmp, "z=z");
461 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
462 "192.0.2.1:12", NULL, "transport", sl_tmp);
464 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
465 smartlist_free(sl_tmp);
469 smartlist_t *sl_tmp = smartlist_new();
470 smartlist_add_asprintf(sl_tmp, "dub=come");
471 smartlist_add_asprintf(sl_tmp, "save=me");
473 good_bridge_line_test("transport 192.0.2.1:12 "
474 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
475 "dub=come save=me",
477 "192.0.2.1:12",
478 "4352e58420e68f5e40bf7c74faddccd9d1349666",
479 "transport", sl_tmp);
481 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
482 smartlist_free(sl_tmp);
485 good_bridge_line_test("192.0.2.1:1231 "
486 "4352e58420e68f5e40bf7c74faddccd9d1349413",
487 "192.0.2.1:1231",
488 "4352e58420e68f5e40bf7c74faddccd9d1349413",
489 NULL, NULL);
491 /* Empty line */
492 bad_bridge_line_test("");
493 /* bad transport name */
494 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
495 /* weird ip address */
496 bad_bridge_line_test("a.b.c.d");
497 /* invalid fpr */
498 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
499 /* no k=v in the end */
500 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
501 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
502 /* no addrport */
503 bad_bridge_line_test("asdw");
504 /* huge k=v value that can't fit in SOCKS fields */
505 bad_bridge_line_test(
506 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
507 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
508 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
510 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
511 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
512 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
513 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
514 "aa=b");
517 static void
518 test_config_parse_transport_options_line(void *arg)
520 smartlist_t *options_sl = NULL, *sl_tmp = NULL;
522 (void) arg;
524 { /* too small line */
525 options_sl = get_options_from_transport_options_line("valley", NULL);
526 tt_ptr_op(options_sl, OP_EQ, NULL);
529 { /* no k=v values */
530 options_sl = get_options_from_transport_options_line("hit it!", NULL);
531 tt_ptr_op(options_sl, OP_EQ, NULL);
534 { /* correct line, but wrong transport specified */
535 options_sl =
536 get_options_from_transport_options_line("trebuchet k=v", "rook");
537 tt_ptr_op(options_sl, OP_EQ, NULL);
540 { /* correct -- no transport specified */
541 sl_tmp = smartlist_new();
542 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
543 smartlist_add_asprintf(sl_tmp, "weliketo=party");
545 options_sl =
546 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
547 NULL);
548 tt_assert(options_sl);
549 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
551 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
552 smartlist_free(sl_tmp);
553 sl_tmp = NULL;
554 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
555 smartlist_free(options_sl);
556 options_sl = NULL;
559 { /* correct -- correct transport specified */
560 sl_tmp = smartlist_new();
561 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
562 smartlist_add_asprintf(sl_tmp, "weliketo=party");
564 options_sl =
565 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
566 "rook");
567 tt_assert(options_sl);
568 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
569 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
570 smartlist_free(sl_tmp);
571 sl_tmp = NULL;
572 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
573 smartlist_free(options_sl);
574 options_sl = NULL;
577 done:
578 if (options_sl) {
579 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
580 smartlist_free(options_sl);
582 if (sl_tmp) {
583 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
584 smartlist_free(sl_tmp);
588 /* Mocks needed for the compute_max_mem_in_queues test */
589 static int get_total_system_memory_mock(size_t *mem_out);
591 static size_t total_system_memory_output = 0;
592 static int total_system_memory_return = 0;
594 static int
595 get_total_system_memory_mock(size_t *mem_out)
597 if (! mem_out)
598 return -1;
600 *mem_out = total_system_memory_output;
601 return total_system_memory_return;
604 /* Mocks needed for the transport plugin line test */
606 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
607 char **proxy_argv, int is_server);
608 static int transport_add_from_config_mock(const tor_addr_t *addr,
609 uint16_t port, const char *name,
610 int socks_ver);
611 static int transport_is_needed_mock(const char *transport_name);
613 static int pt_kickstart_proxy_mock_call_count = 0;
614 static int transport_add_from_config_mock_call_count = 0;
615 static int transport_is_needed_mock_call_count = 0;
616 static int transport_is_needed_mock_return = 0;
618 static void
619 pt_kickstart_proxy_mock(const smartlist_t *transport_list,
620 char **proxy_argv, int is_server)
622 (void) transport_list;
623 (void) proxy_argv;
624 (void) is_server;
625 /* XXXX check that args are as expected. */
627 ++pt_kickstart_proxy_mock_call_count;
629 free_execve_args(proxy_argv);
632 static int
633 transport_add_from_config_mock(const tor_addr_t *addr,
634 uint16_t port, const char *name,
635 int socks_ver)
637 (void) addr;
638 (void) port;
639 (void) name;
640 (void) socks_ver;
641 /* XXXX check that args are as expected. */
643 ++transport_add_from_config_mock_call_count;
645 return 0;
648 static int
649 transport_is_needed_mock(const char *transport_name)
651 (void) transport_name;
652 /* XXXX check that arg is as expected. */
654 ++transport_is_needed_mock_call_count;
656 return transport_is_needed_mock_return;
660 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
661 * options.
664 static void
665 test_config_parse_transport_plugin_line(void *arg)
667 (void)arg;
669 or_options_t *options = get_options_mutable();
670 int r, tmp;
671 int old_pt_kickstart_proxy_mock_call_count;
672 int old_transport_add_from_config_mock_call_count;
673 int old_transport_is_needed_mock_call_count;
675 /* Bad transport lines - too short */
676 r = parse_transport_line(options, "bad", 1, 0);
677 tt_int_op(r, OP_LT, 0);
678 r = parse_transport_line(options, "bad", 1, 1);
679 tt_int_op(r, OP_LT, 0);
680 r = parse_transport_line(options, "bad bad", 1, 0);
681 tt_int_op(r, OP_LT, 0);
682 r = parse_transport_line(options, "bad bad", 1, 1);
683 tt_int_op(r, OP_LT, 0);
685 /* Test transport list parsing */
686 r = parse_transport_line(options,
687 "transport_1 exec /usr/bin/fake-transport", 1, 0);
688 tt_int_op(r, OP_EQ, 0);
689 r = parse_transport_line(options,
690 "transport_1 exec /usr/bin/fake-transport", 1, 1);
691 tt_int_op(r, OP_EQ, 0);
692 r = parse_transport_line(options,
693 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
694 tt_int_op(r, OP_EQ, 0);
695 r = parse_transport_line(options,
696 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
697 tt_int_op(r, OP_EQ, 0);
698 /* Bad transport identifiers */
699 r = parse_transport_line(options,
700 "transport_* exec /usr/bin/fake-transport", 1, 0);
701 tt_int_op(r, OP_LT, 0);
702 r = parse_transport_line(options,
703 "transport_* exec /usr/bin/fake-transport", 1, 1);
704 tt_int_op(r, OP_LT, 0);
706 /* Check SOCKS cases for client transport */
707 r = parse_transport_line(options,
708 "transport_1 socks4 1.2.3.4:567", 1, 0);
709 tt_int_op(r, OP_EQ, 0);
710 r = parse_transport_line(options,
711 "transport_1 socks5 1.2.3.4:567", 1, 0);
712 tt_int_op(r, OP_EQ, 0);
713 /* Proxy case for server transport */
714 r = parse_transport_line(options,
715 "transport_1 proxy 1.2.3.4:567", 1, 1);
716 tt_int_op(r, OP_EQ, 0);
717 /* Multiple-transport error exit */
718 r = parse_transport_line(options,
719 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
720 tt_int_op(r, OP_LT, 0);
721 r = parse_transport_line(options,
722 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
723 tt_int_op(r, OP_LT, 0);
724 /* No port error exit */
725 r = parse_transport_line(options,
726 "transport_1 socks5 1.2.3.4", 1, 0);
727 tt_int_op(r, OP_LT, 0);
728 r = parse_transport_line(options,
729 "transport_1 proxy 1.2.3.4", 1, 1);
730 tt_int_op(r, OP_LT, 0);
731 /* Unparsable address error exit */
732 r = parse_transport_line(options,
733 "transport_1 socks5 1.2.3:6x7", 1, 0);
734 tt_int_op(r, OP_LT, 0);
735 r = parse_transport_line(options,
736 "transport_1 proxy 1.2.3:6x7", 1, 1);
737 tt_int_op(r, OP_LT, 0);
739 /* "Strange {Client|Server}TransportPlugin field" error exit */
740 r = parse_transport_line(options,
741 "transport_1 foo bar", 1, 0);
742 tt_int_op(r, OP_LT, 0);
743 r = parse_transport_line(options,
744 "transport_1 foo bar", 1, 1);
745 tt_int_op(r, OP_LT, 0);
747 /* No sandbox mode error exit */
748 tmp = options->Sandbox;
749 options->Sandbox = 1;
750 r = parse_transport_line(options,
751 "transport_1 exec /usr/bin/fake-transport", 1, 0);
752 tt_int_op(r, OP_LT, 0);
753 r = parse_transport_line(options,
754 "transport_1 exec /usr/bin/fake-transport", 1, 1);
755 tt_int_op(r, OP_LT, 0);
756 options->Sandbox = tmp;
759 * These final test cases cover code paths that only activate without
760 * validate_only, so they need mocks in place.
762 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
763 old_pt_kickstart_proxy_mock_call_count =
764 pt_kickstart_proxy_mock_call_count;
765 r = parse_transport_line(options,
766 "transport_1 exec /usr/bin/fake-transport", 0, 1);
767 tt_int_op(r, OP_EQ, 0);
768 tt_assert(pt_kickstart_proxy_mock_call_count ==
769 old_pt_kickstart_proxy_mock_call_count + 1);
770 UNMOCK(pt_kickstart_proxy);
772 /* This one hits a log line in the !validate_only case only */
773 r = parse_transport_line(options,
774 "transport_1 proxy 1.2.3.4:567", 0, 1);
775 tt_int_op(r, OP_EQ, 0);
777 /* Check mocked client transport cases */
778 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
779 MOCK(transport_add_from_config, transport_add_from_config_mock);
780 MOCK(transport_is_needed, transport_is_needed_mock);
782 /* Unnecessary transport case */
783 transport_is_needed_mock_return = 0;
784 old_pt_kickstart_proxy_mock_call_count =
785 pt_kickstart_proxy_mock_call_count;
786 old_transport_add_from_config_mock_call_count =
787 transport_add_from_config_mock_call_count;
788 old_transport_is_needed_mock_call_count =
789 transport_is_needed_mock_call_count;
790 r = parse_transport_line(options,
791 "transport_1 exec /usr/bin/fake-transport", 0, 0);
792 /* Should have succeeded */
793 tt_int_op(r, OP_EQ, 0);
794 /* transport_is_needed() should have been called */
795 tt_assert(transport_is_needed_mock_call_count ==
796 old_transport_is_needed_mock_call_count + 1);
798 * pt_kickstart_proxy() and transport_add_from_config() should
799 * not have been called.
801 tt_assert(pt_kickstart_proxy_mock_call_count ==
802 old_pt_kickstart_proxy_mock_call_count);
803 tt_assert(transport_add_from_config_mock_call_count ==
804 old_transport_add_from_config_mock_call_count);
806 /* Necessary transport case */
807 transport_is_needed_mock_return = 1;
808 old_pt_kickstart_proxy_mock_call_count =
809 pt_kickstart_proxy_mock_call_count;
810 old_transport_add_from_config_mock_call_count =
811 transport_add_from_config_mock_call_count;
812 old_transport_is_needed_mock_call_count =
813 transport_is_needed_mock_call_count;
814 r = parse_transport_line(options,
815 "transport_1 exec /usr/bin/fake-transport", 0, 0);
816 /* Should have succeeded */
817 tt_int_op(r, OP_EQ, 0);
819 * transport_is_needed() and pt_kickstart_proxy() should have been
820 * called.
822 tt_assert(pt_kickstart_proxy_mock_call_count ==
823 old_pt_kickstart_proxy_mock_call_count + 1);
824 tt_assert(transport_is_needed_mock_call_count ==
825 old_transport_is_needed_mock_call_count + 1);
826 /* transport_add_from_config() should not have been called. */
827 tt_assert(transport_add_from_config_mock_call_count ==
828 old_transport_add_from_config_mock_call_count);
830 /* proxy case */
831 transport_is_needed_mock_return = 1;
832 old_pt_kickstart_proxy_mock_call_count =
833 pt_kickstart_proxy_mock_call_count;
834 old_transport_add_from_config_mock_call_count =
835 transport_add_from_config_mock_call_count;
836 old_transport_is_needed_mock_call_count =
837 transport_is_needed_mock_call_count;
838 r = parse_transport_line(options,
839 "transport_1 socks5 1.2.3.4:567", 0, 0);
840 /* Should have succeeded */
841 tt_int_op(r, OP_EQ, 0);
843 * transport_is_needed() and transport_add_from_config() should have
844 * been called.
846 tt_assert(transport_add_from_config_mock_call_count ==
847 old_transport_add_from_config_mock_call_count + 1);
848 tt_assert(transport_is_needed_mock_call_count ==
849 old_transport_is_needed_mock_call_count + 1);
850 /* pt_kickstart_proxy() should not have been called. */
851 tt_assert(pt_kickstart_proxy_mock_call_count ==
852 old_pt_kickstart_proxy_mock_call_count);
854 /* Done with mocked client transport cases */
855 UNMOCK(transport_is_needed);
856 UNMOCK(transport_add_from_config);
857 UNMOCK(pt_kickstart_proxy);
859 done:
860 /* Make sure we undo all mocks */
861 UNMOCK(pt_kickstart_proxy);
862 UNMOCK(transport_add_from_config);
863 UNMOCK(transport_is_needed);
865 return;
868 // Tests if an options with MyFamily fingerprints missing '$' normalises
869 // them correctly and also ensure it also works with multiple fingerprints
870 static void
871 test_config_fix_my_family(void *arg)
873 char *err = NULL;
874 config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
875 family->key = tor_strdup("MyFamily");
876 family->value = tor_strdup("$1111111111111111111111111111111111111111, "
877 "1111111111111111111111111111111111111112, "
878 "$1111111111111111111111111111111111111113");
880 config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
881 family2->key = tor_strdup("MyFamily");
882 family2->value = tor_strdup("1111111111111111111111111111111111111114");
884 config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
885 family3->key = tor_strdup("MyFamily");
886 family3->value = tor_strdup("$1111111111111111111111111111111111111115");
888 family->next = family2;
889 family2->next = family3;
890 family3->next = NULL;
892 or_options_t* options = options_new();
893 or_options_t* defaults = options_new();
894 (void) arg;
896 options_init(options);
897 options_init(defaults);
898 options->MyFamily_lines = family;
900 options_validate(NULL, options, defaults, 0, &err) ;
902 if (err != NULL) {
903 TT_FAIL(("options_validate failed: %s", err));
906 const char *valid[] = { "$1111111111111111111111111111111111111111",
907 "$1111111111111111111111111111111111111112",
908 "$1111111111111111111111111111111111111113",
909 "$1111111111111111111111111111111111111114",
910 "$1111111111111111111111111111111111111115" };
911 int ret_size = 0;
912 config_line_t *ret;
913 for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
914 tt_str_op(ret->value, OP_EQ, valid[ret_size]);
915 ret_size++;
917 tt_int_op(ret_size, OP_EQ, 5);
919 done:
920 tor_free(err);
921 or_options_free(options);
922 or_options_free(defaults);
925 static int n_hostname_01010101 = 0;
927 /** This mock function is meant to replace tor_lookup_hostname().
928 * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
929 * This function increments <b>n_hostname_01010101</b> counter by one
930 * every time it is called.
932 static int
933 tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
935 n_hostname_01010101++;
937 if (name && addr) {
938 *addr = ntohl(0x01010101);
941 return 0;
944 static int n_hostname_localhost = 0;
946 /** This mock function is meant to replace tor_lookup_hostname().
947 * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
948 * This function increments <b>n_hostname_localhost</b> counter by one
949 * every time it is called.
951 static int
952 tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
954 n_hostname_localhost++;
956 if (name && addr) {
957 *addr = 0x7f000001;
960 return 0;
963 static int n_hostname_failure = 0;
965 /** This mock function is meant to replace tor_lookup_hostname().
966 * It pretends to fail by returning -1 to caller. Also, this function
967 * increments <b>n_hostname_failure</b> every time it is called.
969 static int
970 tor_lookup_hostname_failure(const char *name, uint32_t *addr)
972 (void)name;
973 (void)addr;
975 n_hostname_failure++;
977 return -1;
980 static int n_gethostname_replacement = 0;
982 /** This mock function is meant to replace tor_gethostname(). It
983 * responds with string "onionrouter!" as hostname. This function
984 * increments <b>n_gethostname_replacement</b> by one every time
985 * it is called.
987 static int
988 tor_gethostname_replacement(char *name, size_t namelen)
990 n_gethostname_replacement++;
992 if (name && namelen) {
993 strlcpy(name,"onionrouter!",namelen);
996 return 0;
999 static int n_gethostname_localhost = 0;
1001 /** This mock function is meant to replace tor_gethostname(). It
1002 * responds with string "127.0.0.1" as hostname. This function
1003 * increments <b>n_gethostname_localhost</b> by one every time
1004 * it is called.
1006 static int
1007 tor_gethostname_localhost(char *name, size_t namelen)
1009 n_gethostname_localhost++;
1011 if (name && namelen) {
1012 strlcpy(name,"127.0.0.1",namelen);
1015 return 0;
1018 static int n_gethostname_failure = 0;
1020 /** This mock function is meant to replace tor_gethostname.
1021 * It pretends to fail by returning -1. This function increments
1022 * <b>n_gethostname_failure</b> by one every time it is called.
1024 static int
1025 tor_gethostname_failure(char *name, size_t namelen)
1027 (void)name;
1028 (void)namelen;
1029 n_gethostname_failure++;
1031 return -1;
1034 static int n_get_interface_address = 0;
1036 /** This mock function is meant to replace get_interface_address().
1037 * It answers with address 8.8.8.8. This function increments
1038 * <b>n_get_interface_address</b> by one every time it is called.
1040 static int
1041 get_interface_address_08080808(int severity, uint32_t *addr)
1043 (void)severity;
1045 n_get_interface_address++;
1047 if (addr) {
1048 *addr = ntohl(0x08080808);
1051 return 0;
1054 static int n_get_interface_address6 = 0;
1055 static sa_family_t last_address6_family;
1057 /** This mock function is meant to replace get_interface_address6().
1058 * It answers with IP address 9.9.9.9 iff both of the following are true:
1059 * - <b>family</b> is AF_INET
1060 * - <b>addr</b> pointer is not NULL.
1061 * This function increments <b>n_get_interface_address6</b> by one every
1062 * time it is called.
1064 static int
1065 get_interface_address6_replacement(int severity, sa_family_t family,
1066 tor_addr_t *addr)
1068 (void)severity;
1070 last_address6_family = family;
1071 n_get_interface_address6++;
1073 if ((family != AF_INET) || !addr) {
1074 return -1;
1077 tor_addr_from_ipv4h(addr,0x09090909);
1079 return 0;
1082 static int n_get_interface_address_failure = 0;
1085 * This mock function is meant to replace get_interface_address().
1086 * It pretends to fail getting interface address by returning -1.
1087 * <b>n_get_interface_address_failure</b> is incremented by one
1088 * every time this function is called.
1090 static int
1091 get_interface_address_failure(int severity, uint32_t *addr)
1093 (void)severity;
1094 (void)addr;
1096 n_get_interface_address_failure++;
1098 return -1;
1101 static int n_get_interface_address6_failure = 0;
1104 * This mock function is meant to replace get_interface_addres6().
1105 * It will pretend to fail by return -1.
1106 * <b>n_get_interface_address6_failure</b> is incremented by one
1107 * every time this function is called and <b>last_address6_family</b>
1108 * is assigned the value of <b>family</b> argument.
1110 static int
1111 get_interface_address6_failure(int severity, sa_family_t family,
1112 tor_addr_t *addr)
1114 (void)severity;
1115 (void)addr;
1116 n_get_interface_address6_failure++;
1117 last_address6_family = family;
1119 return -1;
1122 static void
1123 test_config_resolve_my_address(void *arg)
1125 or_options_t *options;
1126 uint32_t resolved_addr;
1127 const char *method_used;
1128 char *hostname_out = NULL;
1129 int retval;
1130 int prev_n_hostname_01010101;
1131 int prev_n_hostname_localhost;
1132 int prev_n_hostname_failure;
1133 int prev_n_gethostname_replacement;
1134 int prev_n_gethostname_failure;
1135 int prev_n_gethostname_localhost;
1136 int prev_n_get_interface_address;
1137 int prev_n_get_interface_address_failure;
1138 int prev_n_get_interface_address6;
1139 int prev_n_get_interface_address6_failure;
1141 (void)arg;
1143 options = options_new();
1145 options_init(options);
1148 * CASE 1:
1149 * If options->Address is a valid IPv4 address string, we want
1150 * the corresponding address to be parsed and returned.
1153 options->Address = tor_strdup("128.52.128.105");
1155 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1156 &method_used,&hostname_out);
1158 tt_want(retval == 0);
1159 tt_want_str_op(method_used,OP_EQ,"CONFIGURED");
1160 tt_want(hostname_out == NULL);
1161 tt_assert(resolved_addr == 0x80348069);
1163 tor_free(options->Address);
1166 * CASE 2:
1167 * If options->Address is a valid DNS address, we want resolve_my_address()
1168 * function to ask tor_lookup_hostname() for help with resolving it
1169 * and return the address that was resolved (in host order).
1172 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1174 tor_free(options->Address);
1175 options->Address = tor_strdup("www.torproject.org");
1177 prev_n_hostname_01010101 = n_hostname_01010101;
1179 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1180 &method_used,&hostname_out);
1182 tt_want(retval == 0);
1183 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1184 tt_want_str_op(method_used,OP_EQ,"RESOLVED");
1185 tt_want_str_op(hostname_out,OP_EQ,"www.torproject.org");
1186 tt_assert(resolved_addr == 0x01010101);
1188 UNMOCK(tor_lookup_hostname);
1190 tor_free(options->Address);
1191 tor_free(hostname_out);
1194 * CASE 3:
1195 * Given that options->Address is NULL, we want resolve_my_address()
1196 * to try and use tor_gethostname() to get hostname AND use
1197 * tor_lookup_hostname() to get IP address.
1200 resolved_addr = 0;
1201 tor_free(options->Address);
1202 options->Address = NULL;
1204 MOCK(tor_gethostname,tor_gethostname_replacement);
1205 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1207 prev_n_gethostname_replacement = n_gethostname_replacement;
1208 prev_n_hostname_01010101 = n_hostname_01010101;
1210 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1211 &method_used,&hostname_out);
1213 tt_want(retval == 0);
1214 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1215 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1216 tt_want_str_op(method_used,OP_EQ,"GETHOSTNAME");
1217 tt_want_str_op(hostname_out,OP_EQ,"onionrouter!");
1218 tt_assert(resolved_addr == 0x01010101);
1220 UNMOCK(tor_gethostname);
1221 UNMOCK(tor_lookup_hostname);
1223 tor_free(hostname_out);
1226 * CASE 4:
1227 * Given that options->Address is a local host address, we want
1228 * resolve_my_address() function to fail.
1231 resolved_addr = 0;
1232 tor_free(options->Address);
1233 options->Address = tor_strdup("127.0.0.1");
1235 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1236 &method_used,&hostname_out);
1238 tt_want(resolved_addr == 0);
1239 tt_int_op(retval, OP_EQ, -1);
1241 tor_free(options->Address);
1242 tor_free(hostname_out);
1245 * CASE 5:
1246 * We want resolve_my_address() to fail if DNS address in options->Address
1247 * cannot be resolved.
1250 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1252 prev_n_hostname_failure = n_hostname_failure;
1254 tor_free(options->Address);
1255 options->Address = tor_strdup("www.tor-project.org");
1257 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1258 &method_used,&hostname_out);
1260 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1261 tt_int_op(retval, OP_EQ, -1);
1263 UNMOCK(tor_lookup_hostname);
1265 tor_free(options->Address);
1266 tor_free(hostname_out);
1269 * CASE 6:
1270 * If options->Address is NULL AND gettting local hostname fails, we want
1271 * resolve_my_address() to fail as well.
1274 MOCK(tor_gethostname,tor_gethostname_failure);
1276 prev_n_gethostname_failure = n_gethostname_failure;
1278 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1279 &method_used,&hostname_out);
1281 tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
1282 tt_int_op(retval, OP_EQ, -1);
1284 UNMOCK(tor_gethostname);
1285 tor_free(hostname_out);
1288 * CASE 7:
1289 * We want resolve_my_address() to try and get network interface address via
1290 * get_interface_address() if hostname returned by tor_gethostname() cannot be
1291 * resolved into IP address.
1294 MOCK(tor_gethostname,tor_gethostname_replacement);
1295 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1296 MOCK(get_interface_address,get_interface_address_08080808);
1298 prev_n_gethostname_replacement = n_gethostname_replacement;
1299 prev_n_get_interface_address = n_get_interface_address;
1301 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1302 &method_used,&hostname_out);
1304 tt_want(retval == 0);
1305 tt_want_int_op(n_gethostname_replacement, OP_EQ,
1306 prev_n_gethostname_replacement + 1);
1307 tt_want_int_op(n_get_interface_address, OP_EQ,
1308 prev_n_get_interface_address + 1);
1309 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1310 tt_want(hostname_out == NULL);
1311 tt_assert(resolved_addr == 0x08080808);
1313 UNMOCK(get_interface_address);
1314 tor_free(hostname_out);
1317 * CASE 8:
1318 * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
1319 * is unresolvable. We want resolve_my_address to fail if
1320 * get_interface_address() fails.
1323 MOCK(get_interface_address,get_interface_address_failure);
1325 prev_n_get_interface_address_failure = n_get_interface_address_failure;
1326 prev_n_gethostname_replacement = n_gethostname_replacement;
1328 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1329 &method_used,&hostname_out);
1331 tt_want(n_get_interface_address_failure ==
1332 prev_n_get_interface_address_failure + 1);
1333 tt_want(n_gethostname_replacement ==
1334 prev_n_gethostname_replacement + 1);
1335 tt_int_op(retval, OP_EQ, -1);
1337 UNMOCK(get_interface_address);
1338 tor_free(hostname_out);
1341 * CASE 9:
1342 * Given that options->Address is NULL AND tor_lookup_hostname()
1343 * fails AND hostname returned by gethostname() resolves
1344 * to local IP address, we want resolve_my_address() function to
1345 * call get_interface_address6(.,AF_INET,.) and return IP address
1346 * the latter function has found.
1349 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1350 MOCK(tor_gethostname,tor_gethostname_replacement);
1351 MOCK(get_interface_address6,get_interface_address6_replacement);
1353 prev_n_gethostname_replacement = n_gethostname_replacement;
1354 prev_n_hostname_failure = n_hostname_failure;
1355 prev_n_get_interface_address6 = n_get_interface_address6;
1357 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1358 &method_used,&hostname_out);
1360 tt_want(last_address6_family == AF_INET);
1361 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1362 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1363 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1364 tt_want(retval == 0);
1365 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1366 tt_assert(resolved_addr == 0x09090909);
1368 UNMOCK(tor_lookup_hostname);
1369 UNMOCK(tor_gethostname);
1370 UNMOCK(get_interface_address6);
1372 tor_free(hostname_out);
1375 * CASE 10: We want resolve_my_address() to fail if all of the following
1376 * are true:
1377 * 1. options->Address is not NULL
1378 * 2. ... but it cannot be converted to struct in_addr by
1379 * tor_inet_aton()
1380 * 3. ... and tor_lookup_hostname() fails to resolve the
1381 * options->Address
1384 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1386 prev_n_hostname_failure = n_hostname_failure;
1388 tor_free(options->Address);
1389 options->Address = tor_strdup("some_hostname");
1391 retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
1392 &method_used,&hostname_out);
1394 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1395 tt_int_op(retval, OP_EQ, -1);
1397 UNMOCK(tor_gethostname);
1398 UNMOCK(tor_lookup_hostname);
1400 tor_free(hostname_out);
1403 * CASE 11:
1404 * Suppose the following sequence of events:
1405 * 1. options->Address is NULL
1406 * 2. tor_gethostname() succeeds to get hostname of machine Tor
1407 * if running on.
1408 * 3. Hostname from previous step cannot be converted to
1409 * address by using tor_inet_aton() function.
1410 * 4. However, tor_lookup_hostname() succeeds in resolving the
1411 * hostname from step 2.
1412 * 5. Unfortunately, tor_addr_is_internal() deems this address
1413 * to be internal.
1414 * 6. get_interface_address6(.,AF_INET,.) returns non-internal
1415 * IPv4
1417 * We want resolve_my_addr() to succeed with method "INTERFACE"
1418 * and address from step 6.
1421 tor_free(options->Address);
1422 options->Address = NULL;
1424 MOCK(tor_gethostname,tor_gethostname_replacement);
1425 MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
1426 MOCK(get_interface_address6,get_interface_address6_replacement);
1428 prev_n_gethostname_replacement = n_gethostname_replacement;
1429 prev_n_hostname_localhost = n_hostname_localhost;
1430 prev_n_get_interface_address6 = n_get_interface_address6;
1432 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1433 &method_used,&hostname_out);
1435 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1436 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1437 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1439 tt_str_op(method_used,OP_EQ,"INTERFACE");
1440 tt_ptr_op(hostname_out, OP_EQ, NULL);
1441 tt_int_op(retval, OP_EQ, 0);
1444 * CASE 11b:
1445 * 1-5 as above.
1446 * 6. get_interface_address6() fails.
1448 * In this subcase, we want resolve_my_address() to fail.
1451 UNMOCK(get_interface_address6);
1452 MOCK(get_interface_address6,get_interface_address6_failure);
1454 prev_n_gethostname_replacement = n_gethostname_replacement;
1455 prev_n_hostname_localhost = n_hostname_localhost;
1456 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1458 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1459 &method_used,&hostname_out);
1461 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1462 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1463 tt_want(n_get_interface_address6_failure ==
1464 prev_n_get_interface_address6_failure + 1);
1466 tt_int_op(retval, OP_EQ, -1);
1468 UNMOCK(tor_gethostname);
1469 UNMOCK(tor_lookup_hostname);
1470 UNMOCK(get_interface_address6);
1472 /* CASE 12:
1473 * Suppose the following happens:
1474 * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
1475 * 2. tor_gethostname() succeeds in getting hostname of a machine ...
1476 * 3. ... which is successfully parsed by tor_inet_aton() ...
1477 * 4. into IPv4 address that tor_addr_is_inernal() considers to be
1478 * internal.
1480 * In this case, we want resolve_my_address() to fail.
1483 tor_free(options->Address);
1484 options->Address = NULL;
1485 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
1487 MOCK(tor_gethostname,tor_gethostname_localhost);
1489 prev_n_gethostname_localhost = n_gethostname_localhost;
1491 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1492 &method_used,&hostname_out);
1494 tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
1495 tt_int_op(retval, OP_EQ, -1);
1497 UNMOCK(tor_gethostname);
1499 done:
1500 tor_free(options->Address);
1501 tor_free(options->DirAuthorities);
1502 or_options_free(options);
1503 tor_free(hostname_out);
1505 UNMOCK(tor_gethostname);
1506 UNMOCK(tor_lookup_hostname);
1507 UNMOCK(get_interface_address);
1508 UNMOCK(get_interface_address6);
1509 UNMOCK(tor_gethostname);
1512 static void
1513 test_config_adding_trusted_dir_server(void *arg)
1515 (void)arg;
1517 const char digest[DIGEST_LEN] = "";
1518 dir_server_t *ds = NULL;
1519 tor_addr_port_t ipv6;
1520 int rv = -1;
1522 clear_dir_servers();
1523 routerlist_free_all();
1525 /* create a trusted ds without an IPv6 address and port */
1526 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1527 NULL, V3_DIRINFO, 1.0);
1528 tt_assert(ds);
1529 dir_server_add(ds);
1530 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
1531 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1533 /* create a trusted ds with an IPv6 address and port */
1534 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1535 tt_int_op(rv, OP_EQ, 0);
1536 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
1537 NULL, V3_DIRINFO, 1.0);
1538 tt_assert(ds);
1539 dir_server_add(ds);
1540 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
1541 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1543 done:
1544 clear_dir_servers();
1545 routerlist_free_all();
1548 static void
1549 test_config_adding_fallback_dir_server(void *arg)
1551 (void)arg;
1553 const char digest[DIGEST_LEN] = "";
1554 dir_server_t *ds = NULL;
1555 tor_addr_t ipv4;
1556 tor_addr_port_t ipv6;
1557 int rv = -1;
1559 clear_dir_servers();
1560 routerlist_free_all();
1562 rv = tor_addr_parse(&ipv4, "127.0.0.1");
1563 tt_int_op(rv, OP_EQ, AF_INET);
1565 /* create a trusted ds without an IPv6 address and port */
1566 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
1567 tt_assert(ds);
1568 dir_server_add(ds);
1569 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1571 /* create a trusted ds with an IPv6 address and port */
1572 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1573 tt_int_op(rv, OP_EQ, 0);
1574 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
1575 tt_assert(ds);
1576 dir_server_add(ds);
1577 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1579 done:
1580 clear_dir_servers();
1581 routerlist_free_all();
1584 /* No secrets here:
1585 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1586 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1587 * with added spaces
1589 #define TEST_DIR_AUTH_LINE_START \
1590 "foobar orport=12345 " \
1591 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1592 #define TEST_DIR_AUTH_LINE_END \
1593 "1.2.3.4:54321 " \
1594 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1595 #define TEST_DIR_AUTH_IPV6_FLAG \
1596 "ipv6=[feed::beef]:9 "
1598 static void
1599 test_config_parsing_trusted_dir_server(void *arg)
1601 (void)arg;
1602 int rv = -1;
1604 /* parse a trusted dir server without an IPv6 address and port */
1605 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1606 TEST_DIR_AUTH_LINE_END,
1607 V3_DIRINFO, 1);
1608 tt_int_op(rv, OP_EQ, 0);
1610 /* parse a trusted dir server with an IPv6 address and port */
1611 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1612 TEST_DIR_AUTH_IPV6_FLAG
1613 TEST_DIR_AUTH_LINE_END,
1614 V3_DIRINFO, 1);
1615 tt_int_op(rv, OP_EQ, 0);
1617 /* Since we are only validating, there is no cleanup. */
1618 done:
1622 #undef TEST_DIR_AUTH_LINE_START
1623 #undef TEST_DIR_AUTH_LINE_END
1624 #undef TEST_DIR_AUTH_IPV6_FLAG
1626 /* No secrets here:
1627 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1629 #define TEST_DIR_FALLBACK_LINE \
1630 "1.2.3.4:54321 orport=12345 " \
1631 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1632 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1633 "ipv6=[2015:c0de::deed]:9"
1635 static void
1636 test_config_parsing_fallback_dir_server(void *arg)
1638 (void)arg;
1639 int rv = -1;
1641 /* parse a trusted dir server without an IPv6 address and port */
1642 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
1643 tt_int_op(rv, OP_EQ, 0);
1645 /* parse a trusted dir server with an IPv6 address and port */
1646 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1647 TEST_DIR_FALLBACK_IPV6_FLAG,
1649 tt_int_op(rv, OP_EQ, 0);
1651 /* Since we are only validating, there is no cleanup. */
1652 done:
1656 #undef TEST_DIR_FALLBACK_LINE
1657 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1659 static void
1660 test_config_adding_default_trusted_dir_servers(void *arg)
1662 (void)arg;
1664 clear_dir_servers();
1665 routerlist_free_all();
1667 /* Assume we only have one bridge authority */
1668 add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
1669 tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
1670 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1672 /* Assume we have eight V3 authorities */
1673 add_default_trusted_dir_authorities(V3_DIRINFO);
1674 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
1675 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
1677 done:
1678 clear_dir_servers();
1679 routerlist_free_all();
1682 static int n_add_default_fallback_dir_servers_known_default = 0;
1685 * This mock function is meant to replace add_default_fallback_dir_servers().
1686 * It will parse and add one known default fallback dir server,
1687 * which has a dir_port of 99.
1688 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
1689 * one every time this function is called.
1691 static void
1692 add_default_fallback_dir_servers_known_default(void)
1694 int i;
1695 const char *fallback[] = {
1696 "127.0.0.1:60099 orport=9009 "
1697 "id=0923456789012345678901234567890123456789",
1698 NULL
1700 for (i=0; fallback[i]; i++) {
1701 if (parse_dir_fallback_line(fallback[i], 0)<0) {
1702 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
1703 fallback[i]);
1706 n_add_default_fallback_dir_servers_known_default++;
1709 /* Test all the different combinations of adding dir servers */
1710 static void
1711 test_config_adding_dir_servers(void *arg)
1713 (void)arg;
1715 /* allocate options */
1716 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
1718 /* Allocate and populate configuration lines:
1720 * Use the same format as the hard-coded directories in
1721 * add_default_trusted_dir_authorities().
1722 * Zeroing the structure has the same effect as initialising to:
1723 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
1725 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
1726 test_dir_authority->key = tor_strdup("DirAuthority");
1727 test_dir_authority->value = tor_strdup(
1728 "D0 orport=9000 "
1729 "v3ident=0023456789012345678901234567890123456789 "
1730 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1733 config_line_t *test_alt_bridge_authority = tor_malloc_zero(
1734 sizeof(config_line_t));
1735 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
1736 test_alt_bridge_authority->value = tor_strdup(
1737 "B1 orport=9001 bridge "
1738 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1741 config_line_t *test_alt_dir_authority = tor_malloc_zero(
1742 sizeof(config_line_t));
1743 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
1744 test_alt_dir_authority->value = tor_strdup(
1745 "A2 orport=9002 "
1746 "v3ident=0223456789012345678901234567890123456789 "
1747 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1750 /* Use the format specified in the manual page */
1751 config_line_t *test_fallback_directory = tor_malloc_zero(
1752 sizeof(config_line_t));
1753 test_fallback_directory->key = tor_strdup("FallbackDir");
1754 test_fallback_directory->value = tor_strdup(
1755 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
1758 /* We need to know if add_default_fallback_dir_servers is called,
1759 * whatever the size of the list in fallback_dirs.inc,
1760 * so we use a version of add_default_fallback_dir_servers that adds
1761 * one known default fallback directory. */
1762 MOCK(add_default_fallback_dir_servers,
1763 add_default_fallback_dir_servers_known_default);
1765 /* There are 16 different cases, covering each combination of set/NULL for:
1766 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
1767 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
1768 * But validate_dir_servers() ensures that:
1769 * "You cannot set both DirAuthority and Alternate*Authority."
1770 * This reduces the number of cases to 10.
1772 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
1773 * So 1001 or case 9 is:
1774 * DirAuthorities set,
1775 * AlternateBridgeAuthority NULL,
1776 * AlternateDirAuthority NULL
1777 * FallbackDir set
1778 * The valid cases are cases 0-9 counting using this method, as every case
1779 * greater than or equal to 10 = 1010 is invalid.
1781 * 1. Outcome: Use Set Directory Authorities
1782 * - No Default Authorities
1783 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1784 * if they are set
1785 * Cases expected to yield this outcome:
1786 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1787 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1788 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1790 * 2. Outcome: Use Set Bridge Authority
1791 * - Use Default Non-Bridge Directory Authorities
1792 * - Use FallbackDir if it is set, otherwise use default FallbackDir
1793 * Cases expected to yield this outcome:
1794 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
1795 * AlternateBridgeAuthority is set, and
1796 * AlternateDirAuthority is NULL)
1798 * 3. Outcome: Use Set Alternate Directory Authority
1799 * - Use Default Bridge Authorities
1800 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
1801 * Cases expected to yield this outcome:
1802 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
1803 * are both NULL, but AlternateDirAuthority is set)
1805 * 4. Outcome: Use Set Custom Fallback Directory
1806 * - Use Default Bridge & Directory Authorities
1807 * Cases expected to yield this outcome:
1808 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
1809 * are all NULL, but FallbackDir is set)
1811 * 5. Outcome: Use All Defaults
1812 * - Use Default Bridge & Directory Authorities, and
1813 * Default Fallback Directories
1814 * Cases expected to yield this outcome:
1815 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
1816 * and FallbackDir are all NULL)
1820 * Find out how many default Bridge, Non-Bridge and Fallback Directories
1821 * are hard-coded into this build.
1822 * This code makes some assumptions about the implementation.
1823 * If they are wrong, one or more of cases 0-5 could fail.
1825 int n_default_alt_bridge_authority = 0;
1826 int n_default_alt_dir_authority = 0;
1827 int n_default_fallback_dir = 0;
1828 #define n_default_authorities ((n_default_alt_bridge_authority) \
1829 + (n_default_alt_dir_authority))
1831 /* Pre-Count Number of Authorities of Each Type
1832 * Use 0000: No Directory Authorities or Fallback Directories Set
1835 /* clear fallback dirs counter */
1836 n_add_default_fallback_dir_servers_known_default = 0;
1838 /* clear options*/
1839 memset(options, 0, sizeof(or_options_t));
1841 /* clear any previous dir servers:
1842 consider_adding_dir_servers() should do this anyway */
1843 clear_dir_servers();
1845 /* assign options: 0000 */
1846 options->DirAuthorities = NULL;
1847 options->AlternateBridgeAuthority = NULL;
1848 options->AlternateDirAuthority = NULL;
1849 options->FallbackDir = NULL;
1850 options->UseDefaultFallbackDirs = 1;
1852 /* parse options - ensure we always update by passing NULL old_options */
1853 consider_adding_dir_servers(options, NULL);
1855 /* check outcome */
1857 /* we must have added the default fallback dirs */
1858 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
1860 /* we have more fallbacks than just the authorities */
1861 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1864 /* fallback_dir_servers */
1865 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1867 /* Count Bridge Authorities */
1868 SMARTLIST_FOREACH(fallback_servers,
1869 dir_server_t *,
1871 /* increment the found counter if it's a bridge auth */
1872 n_default_alt_bridge_authority +=
1873 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
1874 1 : 0)
1876 /* If we have no default bridge authority, something has gone wrong */
1877 tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
1879 /* Count v3 Authorities */
1880 SMARTLIST_FOREACH(fallback_servers,
1881 dir_server_t *,
1883 /* increment found counter if it's a v3 auth */
1884 n_default_alt_dir_authority +=
1885 ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
1886 1 : 0)
1888 /* If we have no default authorities, something has gone really wrong */
1889 tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
1891 /* Calculate Fallback Directory Count */
1892 n_default_fallback_dir = (smartlist_len(fallback_servers) -
1893 n_default_alt_bridge_authority -
1894 n_default_alt_dir_authority);
1895 /* If we have a negative count, something has gone really wrong,
1896 * or some authorities aren't being added as fallback directories.
1897 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
1898 * authorities being fallback directories.) */
1899 tt_int_op(n_default_fallback_dir, OP_GE, 0);
1904 * 1. Outcome: Use Set Directory Authorities
1905 * - No Default Authorities
1906 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1907 * if they are set
1908 * Cases expected to yield this outcome:
1909 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1910 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1911 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1914 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
1915 AlternateDirAuthority Not Set, FallbackDir Set */
1917 /* clear fallback dirs counter */
1918 n_add_default_fallback_dir_servers_known_default = 0;
1920 /* clear options*/
1921 memset(options, 0, sizeof(or_options_t));
1923 /* clear any previous dir servers:
1924 consider_adding_dir_servers() should do this anyway */
1925 clear_dir_servers();
1927 /* assign options: 1001 */
1928 options->DirAuthorities = test_dir_authority;
1929 options->AlternateBridgeAuthority = NULL;
1930 options->AlternateDirAuthority = NULL;
1931 options->FallbackDir = test_fallback_directory;
1932 options->UseDefaultFallbackDirs = 1;
1934 /* parse options - ensure we always update by passing NULL old_options */
1935 consider_adding_dir_servers(options, NULL);
1937 /* check outcome */
1939 /* we must not have added the default fallback dirs */
1940 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
1942 /* we have more fallbacks than just the authorities */
1943 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1946 /* trusted_dir_servers */
1947 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
1948 /* D0, (No B1), (No A2) */
1949 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
1951 /* DirAuthority - D0 - dir_port: 60090 */
1952 int found_D0 = 0;
1953 SMARTLIST_FOREACH(dir_servers,
1954 dir_server_t *,
1956 /* increment the found counter if dir_port matches */
1957 found_D0 +=
1958 (ds->dir_port == 60090 ?
1959 1 : 0)
1961 tt_int_op(found_D0, OP_EQ, 1);
1963 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
1964 int found_B1 = 0;
1965 SMARTLIST_FOREACH(dir_servers,
1966 dir_server_t *,
1968 /* increment the found counter if dir_port matches */
1969 found_B1 +=
1970 (ds->dir_port == 60091 ?
1971 1 : 0)
1973 tt_int_op(found_B1, OP_EQ, 0);
1975 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
1976 int found_A2 = 0;
1977 SMARTLIST_FOREACH(dir_servers,
1978 dir_server_t *,
1980 /* increment the found counter if dir_port matches */
1981 found_A2 +=
1982 (ds->dir_port == 60092 ?
1983 1 : 0)
1985 tt_int_op(found_A2, OP_EQ, 0);
1989 /* fallback_dir_servers */
1990 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1991 /* D0, (No B1), (No A2), Custom Fallback */
1992 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
1994 /* DirAuthority - D0 - dir_port: 60090 */
1995 int found_D0 = 0;
1996 SMARTLIST_FOREACH(fallback_servers,
1997 dir_server_t *,
1999 /* increment the found counter if dir_port matches */
2000 found_D0 +=
2001 (ds->dir_port == 60090 ?
2002 1 : 0)
2004 tt_int_op(found_D0, OP_EQ, 1);
2006 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2007 int found_B1 = 0;
2008 SMARTLIST_FOREACH(fallback_servers,
2009 dir_server_t *,
2011 /* increment the found counter if dir_port matches */
2012 found_B1 +=
2013 (ds->dir_port == 60091 ?
2014 1 : 0)
2016 tt_int_op(found_B1, OP_EQ, 0);
2018 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2019 int found_A2 = 0;
2020 SMARTLIST_FOREACH(fallback_servers,
2021 dir_server_t *,
2023 /* increment the found counter if dir_port matches */
2024 found_A2 +=
2025 (ds->dir_port == 60092 ?
2026 1 : 0)
2028 tt_int_op(found_A2, OP_EQ, 0);
2030 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2031 int found_non_default_fallback = 0;
2032 SMARTLIST_FOREACH(fallback_servers,
2033 dir_server_t *,
2035 /* increment the found counter if dir_port matches */
2036 found_non_default_fallback +=
2037 (ds->dir_port == 60093 ?
2038 1 : 0)
2040 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2042 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2043 int found_default_fallback = 0;
2044 SMARTLIST_FOREACH(fallback_servers,
2045 dir_server_t *,
2047 /* increment the found counter if dir_port matches */
2048 found_default_fallback +=
2049 (ds->dir_port == 60099 ?
2050 1 : 0)
2052 tt_int_op(found_default_fallback, OP_EQ, 0);
2056 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2058 /* clear fallback dirs counter */
2059 n_add_default_fallback_dir_servers_known_default = 0;
2061 /* clear options*/
2062 memset(options, 0, sizeof(or_options_t));
2064 /* clear any previous dir servers:
2065 consider_adding_dir_servers() should do this anyway */
2066 clear_dir_servers();
2068 /* assign options: 1000 */
2069 options->DirAuthorities = test_dir_authority;
2070 options->AlternateBridgeAuthority = NULL;
2071 options->AlternateDirAuthority = NULL;
2072 options->FallbackDir = NULL;
2073 options->UseDefaultFallbackDirs = 1;
2075 /* parse options - ensure we always update by passing NULL old_options */
2076 consider_adding_dir_servers(options, NULL);
2078 /* check outcome */
2080 /* we must not have added the default fallback dirs */
2081 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2083 /* we just have the authorities */
2084 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2087 /* trusted_dir_servers */
2088 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2089 /* D0, (No B1), (No A2) */
2090 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
2092 /* DirAuthority - D0 - dir_port: 60090 */
2093 int found_D0 = 0;
2094 SMARTLIST_FOREACH(dir_servers,
2095 dir_server_t *,
2097 /* increment the found counter if dir_port matches */
2098 found_D0 +=
2099 (ds->dir_port == 60090 ?
2100 1 : 0)
2102 tt_int_op(found_D0, OP_EQ, 1);
2104 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2105 int found_B1 = 0;
2106 SMARTLIST_FOREACH(dir_servers,
2107 dir_server_t *,
2109 /* increment the found counter if dir_port matches */
2110 found_B1 +=
2111 (ds->dir_port == 60091 ?
2112 1 : 0)
2114 tt_int_op(found_B1, OP_EQ, 0);
2116 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2117 int found_A2 = 0;
2118 SMARTLIST_FOREACH(dir_servers,
2119 dir_server_t *,
2121 /* increment the found counter if dir_port matches */
2122 found_A2 +=
2123 (ds->dir_port == 60092 ?
2124 1 : 0)
2126 tt_int_op(found_A2, OP_EQ, 0);
2130 /* fallback_dir_servers */
2131 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2132 /* D0, (No B1), (No A2), (No Fallback) */
2133 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
2135 /* DirAuthority - D0 - dir_port: 60090 */
2136 int found_D0 = 0;
2137 SMARTLIST_FOREACH(fallback_servers,
2138 dir_server_t *,
2140 /* increment the found counter if dir_port matches */
2141 found_D0 +=
2142 (ds->dir_port == 60090 ?
2143 1 : 0)
2145 tt_int_op(found_D0, OP_EQ, 1);
2147 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2148 int found_B1 = 0;
2149 SMARTLIST_FOREACH(fallback_servers,
2150 dir_server_t *,
2152 /* increment the found counter if dir_port matches */
2153 found_B1 +=
2154 (ds->dir_port == 60091 ?
2155 1 : 0)
2157 tt_int_op(found_B1, OP_EQ, 0);
2159 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2160 int found_A2 = 0;
2161 SMARTLIST_FOREACH(fallback_servers,
2162 dir_server_t *,
2164 /* increment the found counter if dir_port matches */
2165 found_A2 +=
2166 (ds->dir_port == 60092 ?
2167 1 : 0)
2169 tt_int_op(found_A2, OP_EQ, 0);
2171 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2172 int found_non_default_fallback = 0;
2173 SMARTLIST_FOREACH(fallback_servers,
2174 dir_server_t *,
2176 /* increment the found counter if dir_port matches */
2177 found_non_default_fallback +=
2178 (ds->dir_port == 60093 ?
2179 1 : 0)
2181 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2183 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2184 int found_default_fallback = 0;
2185 SMARTLIST_FOREACH(fallback_servers,
2186 dir_server_t *,
2188 /* increment the found counter if dir_port matches */
2189 found_default_fallback +=
2190 (ds->dir_port == 60099 ?
2191 1 : 0)
2193 tt_int_op(found_default_fallback, OP_EQ, 0);
2197 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2199 /* clear fallback dirs counter */
2200 n_add_default_fallback_dir_servers_known_default = 0;
2202 /* clear options*/
2203 memset(options, 0, sizeof(or_options_t));
2205 /* clear any previous dir servers:
2206 consider_adding_dir_servers() should do this anyway */
2207 clear_dir_servers();
2209 /* assign options: 0111 */
2210 options->DirAuthorities = NULL;
2211 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2212 options->AlternateDirAuthority = test_alt_dir_authority;
2213 options->FallbackDir = test_fallback_directory;
2214 options->UseDefaultFallbackDirs = 1;
2216 /* parse options - ensure we always update by passing NULL old_options */
2217 consider_adding_dir_servers(options, NULL);
2219 /* check outcome */
2221 /* we must not have added the default fallback dirs */
2222 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2224 /* we have more fallbacks than just the authorities */
2225 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2228 /* trusted_dir_servers */
2229 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2230 /* (No D0), B1, A2 */
2231 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2233 /* (No DirAuthority) - D0 - dir_port: 60090 */
2234 int found_D0 = 0;
2235 SMARTLIST_FOREACH(dir_servers,
2236 dir_server_t *,
2238 /* increment the found counter if dir_port matches */
2239 found_D0 +=
2240 (ds->dir_port == 60090 ?
2241 1 : 0)
2243 tt_int_op(found_D0, OP_EQ, 0);
2245 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2246 int found_B1 = 0;
2247 SMARTLIST_FOREACH(dir_servers,
2248 dir_server_t *,
2250 /* increment the found counter if dir_port matches */
2251 found_B1 +=
2252 (ds->dir_port == 60091 ?
2253 1 : 0)
2255 tt_int_op(found_B1, OP_EQ, 1);
2257 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2258 int found_A2 = 0;
2259 SMARTLIST_FOREACH(dir_servers,
2260 dir_server_t *,
2262 /* increment the found counter if dir_port matches */
2263 found_A2 +=
2264 (ds->dir_port == 60092 ?
2265 1 : 0)
2267 tt_int_op(found_A2, OP_EQ, 1);
2271 /* fallback_dir_servers */
2272 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2273 /* (No D0), B1, A2, Custom Fallback */
2274 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
2276 /* (No DirAuthority) - D0 - dir_port: 60090 */
2277 int found_D0 = 0;
2278 SMARTLIST_FOREACH(fallback_servers,
2279 dir_server_t *,
2281 /* increment the found counter if dir_port matches */
2282 found_D0 +=
2283 (ds->dir_port == 60090 ?
2284 1 : 0)
2286 tt_int_op(found_D0, OP_EQ, 0);
2288 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2289 int found_B1 = 0;
2290 SMARTLIST_FOREACH(fallback_servers,
2291 dir_server_t *,
2293 /* increment the found counter if dir_port matches */
2294 found_B1 +=
2295 (ds->dir_port == 60091 ?
2296 1 : 0)
2298 tt_int_op(found_B1, OP_EQ, 1);
2300 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2301 int found_A2 = 0;
2302 SMARTLIST_FOREACH(fallback_servers,
2303 dir_server_t *,
2305 /* increment the found counter if dir_port matches */
2306 found_A2 +=
2307 (ds->dir_port == 60092 ?
2308 1 : 0)
2310 tt_int_op(found_A2, OP_EQ, 1);
2312 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2313 int found_non_default_fallback = 0;
2314 SMARTLIST_FOREACH(fallback_servers,
2315 dir_server_t *,
2317 /* increment the found counter if dir_port matches */
2318 found_non_default_fallback +=
2319 (ds->dir_port == 60093 ?
2320 1 : 0)
2322 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2324 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2325 int found_default_fallback = 0;
2326 SMARTLIST_FOREACH(fallback_servers,
2327 dir_server_t *,
2329 /* increment the found counter if dir_port matches */
2330 found_default_fallback +=
2331 (ds->dir_port == 60099 ?
2332 1 : 0)
2334 tt_int_op(found_default_fallback, OP_EQ, 0);
2338 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2339 AlternateDirAuthority Set, FallbackDir Not Set */
2341 /* clear fallback dirs counter */
2342 n_add_default_fallback_dir_servers_known_default = 0;
2344 /* clear options*/
2345 memset(options, 0, sizeof(or_options_t));
2347 /* clear any previous dir servers:
2348 consider_adding_dir_servers() should do this anyway */
2349 clear_dir_servers();
2351 /* assign options: 0110 */
2352 options->DirAuthorities = NULL;
2353 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2354 options->AlternateDirAuthority = test_alt_dir_authority;
2355 options->FallbackDir = NULL;
2356 options->UseDefaultFallbackDirs = 1;
2358 /* parse options - ensure we always update by passing NULL old_options */
2359 consider_adding_dir_servers(options, NULL);
2361 /* check outcome */
2363 /* we must not have added the default fallback dirs */
2364 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2366 /* we have more fallbacks than just the authorities */
2367 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2370 /* trusted_dir_servers */
2371 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2372 /* (No D0), B1, A2 */
2373 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2375 /* (No DirAuthority) - D0 - dir_port: 60090 */
2376 int found_D0 = 0;
2377 SMARTLIST_FOREACH(dir_servers,
2378 dir_server_t *,
2380 /* increment the found counter if dir_port matches */
2381 found_D0 +=
2382 (ds->dir_port == 60090 ?
2383 1 : 0)
2385 tt_int_op(found_D0, OP_EQ, 0);
2387 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2388 int found_B1 = 0;
2389 SMARTLIST_FOREACH(dir_servers,
2390 dir_server_t *,
2392 /* increment the found counter if dir_port matches */
2393 found_B1 +=
2394 (ds->dir_port == 60091 ?
2395 1 : 0)
2397 tt_int_op(found_B1, OP_EQ, 1);
2399 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2400 int found_A2 = 0;
2401 SMARTLIST_FOREACH(dir_servers,
2402 dir_server_t *,
2404 /* increment the found counter if dir_port matches */
2405 found_A2 +=
2406 (ds->dir_port == 60092 ?
2407 1 : 0)
2409 tt_int_op(found_A2, OP_EQ, 1);
2413 /* fallback_dir_servers */
2414 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2415 /* (No D0), B1, A2, (No Fallback) */
2416 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2418 /* (No DirAuthority) - D0 - dir_port: 60090 */
2419 int found_D0 = 0;
2420 SMARTLIST_FOREACH(fallback_servers,
2421 dir_server_t *,
2423 /* increment the found counter if dir_port matches */
2424 found_D0 +=
2425 (ds->dir_port == 60090 ?
2426 1 : 0)
2428 tt_int_op(found_D0, OP_EQ, 0);
2430 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2431 int found_B1 = 0;
2432 SMARTLIST_FOREACH(fallback_servers,
2433 dir_server_t *,
2435 /* increment the found counter if dir_port matches */
2436 found_B1 +=
2437 (ds->dir_port == 60091 ?
2438 1 : 0)
2440 tt_int_op(found_B1, OP_EQ, 1);
2442 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2443 int found_A2 = 0;
2444 SMARTLIST_FOREACH(fallback_servers,
2445 dir_server_t *,
2447 /* increment the found counter if dir_port matches */
2448 found_A2 +=
2449 (ds->dir_port == 60092 ?
2450 1 : 0)
2452 tt_int_op(found_A2, OP_EQ, 1);
2454 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2455 int found_non_default_fallback = 0;
2456 SMARTLIST_FOREACH(fallback_servers,
2457 dir_server_t *,
2459 /* increment the found counter if dir_port matches */
2460 found_non_default_fallback +=
2461 (ds->dir_port == 60093 ?
2462 1 : 0)
2464 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2466 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2467 int found_default_fallback = 0;
2468 SMARTLIST_FOREACH(fallback_servers,
2469 dir_server_t *,
2471 /* increment the found counter if dir_port matches */
2472 found_default_fallback +=
2473 (ds->dir_port == 60099 ?
2474 1 : 0)
2476 tt_int_op(found_default_fallback, OP_EQ, 0);
2481 2. Outcome: Use Set Bridge Authority
2482 - Use Default Non-Bridge Directory Authorities
2483 - Use FallbackDir if it is set, otherwise use default FallbackDir
2484 Cases expected to yield this outcome:
2485 4 & 5 (the 2 cases where DirAuthorities is NULL,
2486 AlternateBridgeAuthority is set, and
2487 AlternateDirAuthority is NULL)
2490 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2491 AlternateDirAuthority Not Set, FallbackDir Set */
2493 /* clear fallback dirs counter */
2494 n_add_default_fallback_dir_servers_known_default = 0;
2496 /* clear options*/
2497 memset(options, 0, sizeof(or_options_t));
2499 /* clear any previous dir servers:
2500 consider_adding_dir_servers() should do this anyway */
2501 clear_dir_servers();
2503 /* assign options: 0101 */
2504 options->DirAuthorities = NULL;
2505 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2506 options->AlternateDirAuthority = NULL;
2507 options->FallbackDir = test_fallback_directory;
2508 options->UseDefaultFallbackDirs = 1;
2510 /* parse options - ensure we always update by passing NULL old_options */
2511 consider_adding_dir_servers(options, NULL);
2513 /* check outcome */
2515 /* we must not have added the default fallback dirs */
2516 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2518 /* we have more fallbacks than just the authorities */
2519 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2522 /* trusted_dir_servers */
2523 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2524 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2525 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2527 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2528 int found_D0 = 0;
2529 SMARTLIST_FOREACH(dir_servers,
2530 dir_server_t *,
2532 /* increment the found counter if dir_port matches */
2533 found_D0 +=
2534 (ds->dir_port == 60090 ?
2535 1 : 0)
2537 tt_int_op(found_D0, OP_EQ, 0);
2539 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2540 int found_B1 = 0;
2541 SMARTLIST_FOREACH(dir_servers,
2542 dir_server_t *,
2544 /* increment the found counter if dir_port matches */
2545 found_B1 +=
2546 (ds->dir_port == 60091 ?
2547 1 : 0)
2549 tt_int_op(found_B1, OP_EQ, 1);
2551 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2552 int found_A2 = 0;
2553 SMARTLIST_FOREACH(dir_servers,
2554 dir_server_t *,
2556 /* increment the found counter if dir_port matches */
2557 found_A2 +=
2558 (ds->dir_port == 60092 ?
2559 1 : 0)
2561 tt_int_op(found_A2, OP_EQ, 0);
2563 /* There's no easy way of checking that we have included all the
2564 * default v3 non-Bridge directory authorities, so let's assume that
2565 * if the total count above is correct, we have the right ones.
2570 /* fallback_dir_servers */
2571 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2572 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2573 * Custom Fallback */
2574 tt_assert(smartlist_len(fallback_servers) ==
2575 2 + n_default_alt_dir_authority);
2577 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2578 int found_D0 = 0;
2579 SMARTLIST_FOREACH(fallback_servers,
2580 dir_server_t *,
2582 /* increment the found counter if dir_port matches */
2583 found_D0 +=
2584 (ds->dir_port == 60090 ?
2585 1 : 0)
2587 tt_int_op(found_D0, OP_EQ, 0);
2589 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2590 int found_B1 = 0;
2591 SMARTLIST_FOREACH(fallback_servers,
2592 dir_server_t *,
2594 /* increment the found counter if dir_port matches */
2595 found_B1 +=
2596 (ds->dir_port == 60091 ?
2597 1 : 0)
2599 tt_int_op(found_B1, OP_EQ, 1);
2601 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2602 int found_A2 = 0;
2603 SMARTLIST_FOREACH(fallback_servers,
2604 dir_server_t *,
2606 /* increment the found counter if dir_port matches */
2607 found_A2 +=
2608 (ds->dir_port == 60092 ?
2609 1 : 0)
2611 tt_int_op(found_A2, OP_EQ, 0);
2613 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2614 int found_non_default_fallback = 0;
2615 SMARTLIST_FOREACH(fallback_servers,
2616 dir_server_t *,
2618 /* increment the found counter if dir_port matches */
2619 found_non_default_fallback +=
2620 (ds->dir_port == 60093 ?
2621 1 : 0)
2623 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2625 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2626 int found_default_fallback = 0;
2627 SMARTLIST_FOREACH(fallback_servers,
2628 dir_server_t *,
2630 /* increment the found counter if dir_port matches */
2631 found_default_fallback +=
2632 (ds->dir_port == 60099 ?
2633 1 : 0)
2635 tt_int_op(found_default_fallback, OP_EQ, 0);
2637 /* There's no easy way of checking that we have included all the
2638 * default v3 non-Bridge directory authorities, so let's assume that
2639 * if the total count above is correct, we have the right ones.
2644 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2645 AlternateDirAuthority & FallbackDir Not Set */
2647 /* clear fallback dirs counter */
2648 n_add_default_fallback_dir_servers_known_default = 0;
2650 /* clear options*/
2651 memset(options, 0, sizeof(or_options_t));
2653 /* clear any previous dir servers:
2654 consider_adding_dir_servers() should do this anyway */
2655 clear_dir_servers();
2657 /* assign options: 0100 */
2658 options->DirAuthorities = NULL;
2659 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2660 options->AlternateDirAuthority = NULL;
2661 options->FallbackDir = NULL;
2662 options->UseDefaultFallbackDirs = 1;
2664 /* parse options - ensure we always update by passing NULL old_options */
2665 consider_adding_dir_servers(options, NULL);
2667 /* check outcome */
2669 /* we must have added the default fallback dirs */
2670 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
2672 /* we have more fallbacks than just the authorities */
2673 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2676 /* trusted_dir_servers */
2677 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2678 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2679 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2681 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2682 int found_D0 = 0;
2683 SMARTLIST_FOREACH(dir_servers,
2684 dir_server_t *,
2686 /* increment the found counter if dir_port matches */
2687 found_D0 +=
2688 (ds->dir_port == 60090 ?
2689 1 : 0)
2691 tt_int_op(found_D0, OP_EQ, 0);
2693 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2694 int found_B1 = 0;
2695 SMARTLIST_FOREACH(dir_servers,
2696 dir_server_t *,
2698 /* increment the found counter if dir_port matches */
2699 found_B1 +=
2700 (ds->dir_port == 60091 ?
2701 1 : 0)
2703 tt_int_op(found_B1, OP_EQ, 1);
2705 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2706 int found_A2 = 0;
2707 SMARTLIST_FOREACH(dir_servers,
2708 dir_server_t *,
2710 /* increment the found counter if dir_port matches */
2711 found_A2 +=
2712 (ds->dir_port == 60092 ?
2713 1 : 0)
2715 tt_int_op(found_A2, OP_EQ, 0);
2717 /* There's no easy way of checking that we have included all the
2718 * default v3 non-Bridge directory authorities, so let's assume that
2719 * if the total count above is correct, we have the right ones.
2724 /* fallback_dir_servers */
2725 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2726 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2727 * Default Fallback */
2728 tt_assert(smartlist_len(fallback_servers) ==
2729 2 + n_default_alt_dir_authority);
2731 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2732 int found_D0 = 0;
2733 SMARTLIST_FOREACH(fallback_servers,
2734 dir_server_t *,
2736 /* increment the found counter if dir_port matches */
2737 found_D0 +=
2738 (ds->dir_port == 60090 ?
2739 1 : 0)
2741 tt_int_op(found_D0, OP_EQ, 0);
2743 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2744 int found_B1 = 0;
2745 SMARTLIST_FOREACH(fallback_servers,
2746 dir_server_t *,
2748 /* increment the found counter if dir_port matches */
2749 found_B1 +=
2750 (ds->dir_port == 60091 ?
2751 1 : 0)
2753 tt_int_op(found_B1, OP_EQ, 1);
2755 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2756 int found_A2 = 0;
2757 SMARTLIST_FOREACH(fallback_servers,
2758 dir_server_t *,
2760 /* increment the found counter if dir_port matches */
2761 found_A2 +=
2762 (ds->dir_port == 60092 ?
2763 1 : 0)
2765 tt_int_op(found_A2, OP_EQ, 0);
2767 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2768 int found_non_default_fallback = 0;
2769 SMARTLIST_FOREACH(fallback_servers,
2770 dir_server_t *,
2772 /* increment the found counter if dir_port matches */
2773 found_non_default_fallback +=
2774 (ds->dir_port == 60093 ?
2775 1 : 0)
2777 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2779 /* Default FallbackDir - No Nickname - dir_port: 60099 */
2780 int found_default_fallback = 0;
2781 SMARTLIST_FOREACH(fallback_servers,
2782 dir_server_t *,
2784 /* increment the found counter if dir_port matches */
2785 found_default_fallback +=
2786 (ds->dir_port == 60099 ?
2787 1 : 0)
2789 tt_int_op(found_default_fallback, OP_EQ, 1);
2791 /* There's no easy way of checking that we have included all the
2792 * default v3 non-Bridge directory authorities, so let's assume that
2793 * if the total count above is correct, we have the right ones.
2799 3. Outcome: Use Set Alternate Directory Authority
2800 - Use Default Bridge Authorities
2801 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2802 Cases expected to yield this outcome:
2803 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2804 are both NULL, but AlternateDirAuthority is set)
2807 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
2808 AlternateDirAuthority & FallbackDir Set */
2810 /* clear fallback dirs counter */
2811 n_add_default_fallback_dir_servers_known_default = 0;
2813 /* clear options*/
2814 memset(options, 0, sizeof(or_options_t));
2816 /* clear any previous dir servers:
2817 consider_adding_dir_servers() should do this anyway */
2818 clear_dir_servers();
2820 /* assign options: 0011 */
2821 options->DirAuthorities = NULL;
2822 options->AlternateBridgeAuthority = NULL;
2823 options->AlternateDirAuthority = test_alt_dir_authority;
2824 options->FallbackDir = test_fallback_directory;
2825 options->UseDefaultFallbackDirs = 1;
2827 /* parse options - ensure we always update by passing NULL old_options */
2828 consider_adding_dir_servers(options, NULL);
2830 /* check outcome */
2832 /* we must not have added the default fallback dirs */
2833 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2835 /* we have more fallbacks than just the authorities */
2836 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2839 /* trusted_dir_servers */
2840 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2841 /* (No D0), (No B1), Default Bridge Authorities, A2 */
2842 tt_assert(smartlist_len(dir_servers) ==
2843 1 + n_default_alt_bridge_authority);
2845 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2846 int found_D0 = 0;
2847 SMARTLIST_FOREACH(dir_servers,
2848 dir_server_t *,
2850 /* increment the found counter if dir_port matches */
2851 found_D0 +=
2852 (ds->dir_port == 60090 ?
2853 1 : 0)
2855 tt_int_op(found_D0, OP_EQ, 0);
2857 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2858 int found_B1 = 0;
2859 SMARTLIST_FOREACH(dir_servers,
2860 dir_server_t *,
2862 /* increment the found counter if dir_port matches */
2863 found_B1 +=
2864 (ds->dir_port == 60091 ?
2865 1 : 0)
2867 tt_int_op(found_B1, OP_EQ, 0);
2869 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2870 int found_A2 = 0;
2871 SMARTLIST_FOREACH(dir_servers,
2872 dir_server_t *,
2874 /* increment the found counter if dir_port matches */
2875 found_A2 +=
2876 (ds->dir_port == 60092 ?
2877 1 : 0)
2879 tt_int_op(found_A2, OP_EQ, 1);
2881 /* There's no easy way of checking that we have included all the
2882 * default Bridge authorities (except for hard-coding tonga's details),
2883 * so let's assume that if the total count above is correct,
2884 * we have the right ones.
2889 /* fallback_dir_servers */
2890 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2891 /* (No D0), (No B1), Default Bridge Authorities, A2,
2892 * Custom Fallback Directory, (No Default Fallback Directories) */
2893 tt_assert(smartlist_len(fallback_servers) ==
2894 2 + n_default_alt_bridge_authority);
2896 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2897 int found_D0 = 0;
2898 SMARTLIST_FOREACH(fallback_servers,
2899 dir_server_t *,
2901 /* increment the found counter if dir_port matches */
2902 found_D0 +=
2903 (ds->dir_port == 60090 ?
2904 1 : 0)
2906 tt_int_op(found_D0, OP_EQ, 0);
2908 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2909 int found_B1 = 0;
2910 SMARTLIST_FOREACH(fallback_servers,
2911 dir_server_t *,
2913 /* increment the found counter if dir_port matches */
2914 found_B1 +=
2915 (ds->dir_port == 60091 ?
2916 1 : 0)
2918 tt_int_op(found_B1, OP_EQ, 0);
2920 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2921 int found_A2 = 0;
2922 SMARTLIST_FOREACH(fallback_servers,
2923 dir_server_t *,
2925 /* increment the found counter if dir_port matches */
2926 found_A2 +=
2927 (ds->dir_port == 60092 ?
2928 1 : 0)
2930 tt_int_op(found_A2, OP_EQ, 1);
2932 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2933 int found_non_default_fallback = 0;
2934 SMARTLIST_FOREACH(fallback_servers,
2935 dir_server_t *,
2937 /* increment the found counter if dir_port matches */
2938 found_non_default_fallback +=
2939 (ds->dir_port == 60093 ?
2940 1 : 0)
2942 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2944 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2945 int found_default_fallback = 0;
2946 SMARTLIST_FOREACH(fallback_servers,
2947 dir_server_t *,
2949 /* increment the found counter if dir_port matches */
2950 found_default_fallback +=
2951 (ds->dir_port == 60099 ?
2952 1 : 0)
2954 tt_int_op(found_default_fallback, OP_EQ, 0);
2956 /* There's no easy way of checking that we have included all the
2957 * default Bridge authorities (except for hard-coding tonga's details),
2958 * so let's assume that if the total count above is correct,
2959 * we have the right ones.
2964 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
2965 AlternateDirAuthority Set, FallbackDir Not Set */
2967 /* clear fallback dirs counter */
2968 n_add_default_fallback_dir_servers_known_default = 0;
2970 /* clear options*/
2971 memset(options, 0, sizeof(or_options_t));
2973 /* clear any previous dir servers:
2974 consider_adding_dir_servers() should do this anyway */
2975 clear_dir_servers();
2977 /* assign options: 0010 */
2978 options->DirAuthorities = NULL;
2979 options->AlternateBridgeAuthority = NULL;
2980 options->AlternateDirAuthority = test_alt_dir_authority;
2981 options->FallbackDir = NULL;
2982 options->UseDefaultFallbackDirs = 1;
2984 /* parse options - ensure we always update by passing NULL old_options */
2985 consider_adding_dir_servers(options, NULL);
2987 /* check outcome */
2989 /* we must not have added the default fallback dirs */
2990 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2992 /* we just have the authorities */
2993 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2996 /* trusted_dir_servers */
2997 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2998 /* (No D0), (No B1), Default Bridge Authorities, A2,
2999 * No Default or Custom Fallback Directories */
3000 tt_assert(smartlist_len(dir_servers) ==
3001 1 + n_default_alt_bridge_authority);
3003 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3004 int found_D0 = 0;
3005 SMARTLIST_FOREACH(dir_servers,
3006 dir_server_t *,
3008 /* increment the found counter if dir_port matches */
3009 found_D0 +=
3010 (ds->dir_port == 60090 ?
3011 1 : 0)
3013 tt_int_op(found_D0, OP_EQ, 0);
3015 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3016 int found_B1 = 0;
3017 SMARTLIST_FOREACH(dir_servers,
3018 dir_server_t *,
3020 /* increment the found counter if dir_port matches */
3021 found_B1 +=
3022 (ds->dir_port == 60091 ?
3023 1 : 0)
3025 tt_int_op(found_B1, OP_EQ, 0);
3027 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3028 int found_A2 = 0;
3029 SMARTLIST_FOREACH(dir_servers,
3030 dir_server_t *,
3032 /* increment the found counter if dir_port matches */
3033 found_A2 +=
3034 (ds->dir_port == 60092 ?
3035 1 : 0)
3037 tt_int_op(found_A2, OP_EQ, 1);
3039 /* There's no easy way of checking that we have included all the
3040 * default Bridge authorities (except for hard-coding tonga's details),
3041 * so let's assume that if the total count above is correct,
3042 * we have the right ones.
3047 /* fallback_dir_servers */
3048 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3049 /* (No D0), (No B1), Default Bridge Authorities, A2,
3050 * No Custom or Default Fallback Directories */
3051 tt_assert(smartlist_len(fallback_servers) ==
3052 1 + n_default_alt_bridge_authority);
3054 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3055 int found_D0 = 0;
3056 SMARTLIST_FOREACH(fallback_servers,
3057 dir_server_t *,
3059 /* increment the found counter if dir_port matches */
3060 found_D0 +=
3061 (ds->dir_port == 60090 ?
3062 1 : 0)
3064 tt_int_op(found_D0, OP_EQ, 0);
3066 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3067 int found_B1 = 0;
3068 SMARTLIST_FOREACH(fallback_servers,
3069 dir_server_t *,
3071 /* increment the found counter if dir_port matches */
3072 found_B1 +=
3073 (ds->dir_port == 60091 ?
3074 1 : 0)
3076 tt_int_op(found_B1, OP_EQ, 0);
3078 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3079 int found_A2 = 0;
3080 SMARTLIST_FOREACH(fallback_servers,
3081 dir_server_t *,
3083 /* increment the found counter if dir_port matches */
3084 found_A2 +=
3085 (ds->dir_port == 60092 ?
3086 1 : 0)
3088 tt_int_op(found_A2, OP_EQ, 1);
3090 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3091 int found_non_default_fallback = 0;
3092 SMARTLIST_FOREACH(fallback_servers,
3093 dir_server_t *,
3095 /* increment the found counter if dir_port matches */
3096 found_non_default_fallback +=
3097 (ds->dir_port == 60093 ?
3098 1 : 0)
3100 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3102 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3103 int found_default_fallback = 0;
3104 SMARTLIST_FOREACH(fallback_servers,
3105 dir_server_t *,
3107 /* increment the found counter if dir_port matches */
3108 found_default_fallback +=
3109 (ds->dir_port == 60099 ?
3110 1 : 0)
3112 tt_int_op(found_default_fallback, OP_EQ, 0);
3114 /* There's no easy way of checking that we have included all the
3115 * default Bridge authorities (except for hard-coding tonga's details),
3116 * so let's assume that if the total count above is correct,
3117 * we have the right ones.
3123 4. Outcome: Use Set Custom Fallback Directory
3124 - Use Default Bridge & Directory Authorities
3125 Cases expected to yield this outcome:
3126 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3127 are all NULL, but FallbackDir is set)
3130 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3131 & AlternateDirAuthority Not Set, FallbackDir Set */
3133 /* clear fallback dirs counter */
3134 n_add_default_fallback_dir_servers_known_default = 0;
3136 /* clear options*/
3137 memset(options, 0, sizeof(or_options_t));
3139 /* clear any previous dir servers:
3140 consider_adding_dir_servers() should do this anyway */
3141 clear_dir_servers();
3143 /* assign options: 0001 */
3144 options->DirAuthorities = NULL;
3145 options->AlternateBridgeAuthority = NULL;
3146 options->AlternateDirAuthority = NULL;
3147 options->FallbackDir = test_fallback_directory;
3148 options->UseDefaultFallbackDirs = 1;
3150 /* parse options - ensure we always update by passing NULL old_options */
3151 consider_adding_dir_servers(options, NULL);
3153 /* check outcome */
3155 /* we must not have added the default fallback dirs */
3156 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3158 /* we have more fallbacks than just the authorities */
3159 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3162 /* trusted_dir_servers */
3163 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3164 /* (No D0), (No B1), Default Bridge Authorities,
3165 * (No A2), Default v3 Directory Authorities */
3166 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3168 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3169 int found_D0 = 0;
3170 SMARTLIST_FOREACH(dir_servers,
3171 dir_server_t *,
3173 /* increment the found counter if dir_port matches */
3174 found_D0 +=
3175 (ds->dir_port == 60090 ?
3176 1 : 0)
3178 tt_int_op(found_D0, OP_EQ, 0);
3180 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3181 int found_B1 = 0;
3182 SMARTLIST_FOREACH(dir_servers,
3183 dir_server_t *,
3185 /* increment the found counter if dir_port matches */
3186 found_B1 +=
3187 (ds->dir_port == 60091 ?
3188 1 : 0)
3190 tt_int_op(found_B1, OP_EQ, 0);
3192 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3193 int found_A2 = 0;
3194 SMARTLIST_FOREACH(dir_servers,
3195 dir_server_t *,
3197 /* increment the found counter if dir_port matches */
3198 found_A2 +=
3199 (ds->dir_port == 60092 ?
3200 1 : 0)
3202 tt_int_op(found_A2, OP_EQ, 0);
3204 /* There's no easy way of checking that we have included all the
3205 * default Bridge & V3 Directory authorities, so let's assume that
3206 * if the total count above is correct, we have the right ones.
3211 /* fallback_dir_servers */
3212 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3213 /* (No D0), (No B1), Default Bridge Authorities,
3214 * (No A2), Default v3 Directory Authorities,
3215 * Custom Fallback Directory, (No Default Fallback Directories) */
3216 tt_assert(smartlist_len(fallback_servers) ==
3217 1 + n_default_authorities);
3219 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3220 int found_D0 = 0;
3221 SMARTLIST_FOREACH(fallback_servers,
3222 dir_server_t *,
3224 /* increment the found counter if dir_port matches */
3225 found_D0 +=
3226 (ds->dir_port == 60090 ?
3227 1 : 0)
3229 tt_int_op(found_D0, OP_EQ, 0);
3231 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3232 int found_B1 = 0;
3233 SMARTLIST_FOREACH(fallback_servers,
3234 dir_server_t *,
3236 /* increment the found counter if dir_port matches */
3237 found_B1 +=
3238 (ds->dir_port == 60091 ?
3239 1 : 0)
3241 tt_int_op(found_B1, OP_EQ, 0);
3243 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3244 int found_A2 = 0;
3245 SMARTLIST_FOREACH(fallback_servers,
3246 dir_server_t *,
3248 /* increment the found counter if dir_port matches */
3249 found_A2 +=
3250 (ds->dir_port == 60092 ?
3251 1 : 0)
3253 tt_int_op(found_A2, OP_EQ, 0);
3255 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3256 int found_non_default_fallback = 0;
3257 SMARTLIST_FOREACH(fallback_servers,
3258 dir_server_t *,
3260 /* increment the found counter if dir_port matches */
3261 found_non_default_fallback +=
3262 (ds->dir_port == 60093 ?
3263 1 : 0)
3265 tt_int_op(found_non_default_fallback, OP_EQ, 1);
3267 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3268 int found_default_fallback = 0;
3269 SMARTLIST_FOREACH(fallback_servers,
3270 dir_server_t *,
3272 /* increment the found counter if dir_port matches */
3273 found_default_fallback +=
3274 (ds->dir_port == 60099 ?
3275 1 : 0)
3277 tt_int_op(found_default_fallback, OP_EQ, 0);
3279 /* There's no easy way of checking that we have included all the
3280 * default Bridge & V3 Directory authorities, so let's assume that
3281 * if the total count above is correct, we have the right ones.
3287 5. Outcome: Use All Defaults
3288 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3289 Cases expected to yield this outcome:
3290 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3291 and FallbackDir are all NULL)
3294 /* Case 0: 0000 - All Not Set */
3296 /* clear fallback dirs counter */
3297 n_add_default_fallback_dir_servers_known_default = 0;
3299 /* clear options*/
3300 memset(options, 0, sizeof(or_options_t));
3302 /* clear any previous dir servers:
3303 consider_adding_dir_servers() should do this anyway */
3304 clear_dir_servers();
3306 /* assign options: 0001 */
3307 options->DirAuthorities = NULL;
3308 options->AlternateBridgeAuthority = NULL;
3309 options->AlternateDirAuthority = NULL;
3310 options->FallbackDir = NULL;
3311 options->UseDefaultFallbackDirs = 1;
3313 /* parse options - ensure we always update by passing NULL old_options */
3314 consider_adding_dir_servers(options, NULL);
3316 /* check outcome */
3318 /* we must have added the default fallback dirs */
3319 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
3321 /* we have more fallbacks than just the authorities */
3322 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3325 /* trusted_dir_servers */
3326 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3327 /* (No D0), (No B1), Default Bridge Authorities,
3328 * (No A2), Default v3 Directory Authorities */
3329 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3331 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3332 int found_D0 = 0;
3333 SMARTLIST_FOREACH(dir_servers,
3334 dir_server_t *,
3336 /* increment the found counter if dir_port matches */
3337 found_D0 +=
3338 (ds->dir_port == 60090 ?
3339 1 : 0)
3341 tt_int_op(found_D0, OP_EQ, 0);
3343 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3344 int found_B1 = 0;
3345 SMARTLIST_FOREACH(dir_servers,
3346 dir_server_t *,
3348 /* increment the found counter if dir_port matches */
3349 found_B1 +=
3350 (ds->dir_port == 60091 ?
3351 1 : 0)
3353 tt_int_op(found_B1, OP_EQ, 0);
3355 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3356 int found_A2 = 0;
3357 SMARTLIST_FOREACH(dir_servers,
3358 dir_server_t *,
3360 /* increment the found counter if dir_port matches */
3361 found_A2 +=
3362 (ds->dir_port == 60092 ?
3363 1 : 0)
3365 tt_int_op(found_A2, OP_EQ, 0);
3367 /* There's no easy way of checking that we have included all the
3368 * default Bridge & V3 Directory authorities, so let's assume that
3369 * if the total count above is correct, we have the right ones.
3374 /* fallback_dir_servers */
3375 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3376 /* (No D0), (No B1), Default Bridge Authorities,
3377 * (No A2), Default v3 Directory Authorities,
3378 * (No Custom Fallback Directory), Default Fallback Directories */
3379 tt_assert(smartlist_len(fallback_servers) ==
3380 n_default_authorities + n_default_fallback_dir);
3382 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3383 int found_D0 = 0;
3384 SMARTLIST_FOREACH(fallback_servers,
3385 dir_server_t *,
3387 /* increment the found counter if dir_port matches */
3388 found_D0 +=
3389 (ds->dir_port == 60090 ?
3390 1 : 0)
3392 tt_int_op(found_D0, OP_EQ, 0);
3394 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3395 int found_B1 = 0;
3396 SMARTLIST_FOREACH(fallback_servers,
3397 dir_server_t *,
3399 /* increment the found counter if dir_port matches */
3400 found_B1 +=
3401 (ds->dir_port == 60091 ?
3402 1 : 0)
3404 tt_int_op(found_B1, OP_EQ, 0);
3406 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3407 int found_A2 = 0;
3408 SMARTLIST_FOREACH(fallback_servers,
3409 dir_server_t *,
3411 /* increment the found counter if dir_port matches */
3412 found_A2 +=
3413 (ds->dir_port == 60092 ?
3414 1 : 0)
3416 tt_int_op(found_A2, OP_EQ, 0);
3418 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3419 int found_non_default_fallback = 0;
3420 SMARTLIST_FOREACH(fallback_servers,
3421 dir_server_t *,
3423 /* increment the found counter if dir_port matches */
3424 found_non_default_fallback +=
3425 (ds->dir_port == 60093 ?
3426 1 : 0)
3428 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3430 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3431 int found_default_fallback = 0;
3432 SMARTLIST_FOREACH(fallback_servers,
3433 dir_server_t *,
3435 /* increment the found counter if dir_port matches */
3436 found_default_fallback +=
3437 (ds->dir_port == 60099 ?
3438 1 : 0)
3440 tt_int_op(found_default_fallback, OP_EQ, 1);
3442 /* There's no easy way of checking that we have included all the
3443 * default Bridge & V3 Directory authorities, and the default
3444 * Fallback Directories, so let's assume that if the total count
3445 * above is correct, we have the right ones.
3450 done:
3451 clear_dir_servers();
3453 tor_free(test_dir_authority->key);
3454 tor_free(test_dir_authority->value);
3455 tor_free(test_dir_authority);
3457 tor_free(test_alt_dir_authority->key);
3458 tor_free(test_alt_dir_authority->value);
3459 tor_free(test_alt_dir_authority);
3461 tor_free(test_alt_bridge_authority->key);
3462 tor_free(test_alt_bridge_authority->value);
3463 tor_free(test_alt_bridge_authority);
3465 tor_free(test_fallback_directory->key);
3466 tor_free(test_fallback_directory->value);
3467 tor_free(test_fallback_directory);
3469 options->DirAuthorities = NULL;
3470 options->AlternateBridgeAuthority = NULL;
3471 options->AlternateDirAuthority = NULL;
3472 options->FallbackDir = NULL;
3473 or_options_free(options);
3475 UNMOCK(add_default_fallback_dir_servers);
3478 static void
3479 test_config_default_dir_servers(void *arg)
3481 or_options_t *opts = NULL;
3482 (void)arg;
3483 int trusted_count = 0;
3484 int fallback_count = 0;
3486 /* new set of options should stop fallback parsing */
3487 opts = tor_malloc_zero(sizeof(or_options_t));
3488 opts->UseDefaultFallbackDirs = 0;
3489 /* set old_options to NULL to force dir update */
3490 consider_adding_dir_servers(opts, NULL);
3491 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3492 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3493 or_options_free(opts);
3494 opts = NULL;
3496 /* assume a release will never go out with less than 7 authorities */
3497 tt_int_op(trusted_count, OP_GE, 7);
3498 /* if we disable the default fallbacks, there must not be any extra */
3499 tt_assert(fallback_count == trusted_count);
3501 opts = tor_malloc_zero(sizeof(or_options_t));
3502 opts->UseDefaultFallbackDirs = 1;
3503 consider_adding_dir_servers(opts, opts);
3504 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3505 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3506 or_options_free(opts);
3507 opts = NULL;
3509 /* assume a release will never go out with less than 7 authorities */
3510 tt_int_op(trusted_count, OP_GE, 7);
3511 /* XX/teor - allow for default fallbacks to be added without breaking
3512 * the unit tests. Set a minimum fallback count once the list is stable. */
3513 tt_assert(fallback_count >= trusted_count);
3515 done:
3516 or_options_free(opts);
3519 static int mock_router_pick_published_address_result = 0;
3521 static int
3522 mock_router_pick_published_address(const or_options_t *options,
3523 uint32_t *addr, int cache_only)
3525 (void)options;
3526 (void)addr;
3527 (void)cache_only;
3528 return mock_router_pick_published_address_result;
3531 static int mock_router_my_exit_policy_is_reject_star_result = 0;
3533 static int
3534 mock_router_my_exit_policy_is_reject_star(void)
3536 return mock_router_my_exit_policy_is_reject_star_result;
3539 static int mock_advertised_server_mode_result = 0;
3541 static int
3542 mock_advertised_server_mode(void)
3544 return mock_advertised_server_mode_result;
3547 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
3549 static const routerinfo_t *
3550 mock_router_get_my_routerinfo(void)
3552 return mock_router_get_my_routerinfo_result;
3555 static void
3556 test_config_directory_fetch(void *arg)
3558 (void)arg;
3560 /* Test Setup */
3561 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
3562 routerinfo_t routerinfo;
3563 memset(&routerinfo, 0, sizeof(routerinfo));
3564 mock_router_pick_published_address_result = -1;
3565 mock_router_my_exit_policy_is_reject_star_result = 1;
3566 mock_advertised_server_mode_result = 0;
3567 mock_router_get_my_routerinfo_result = NULL;
3568 MOCK(router_pick_published_address, mock_router_pick_published_address);
3569 MOCK(router_my_exit_policy_is_reject_star,
3570 mock_router_my_exit_policy_is_reject_star);
3571 MOCK(advertised_server_mode, mock_advertised_server_mode);
3572 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
3574 /* Clients can use multiple directory mirrors for bootstrap */
3575 memset(options, 0, sizeof(or_options_t));
3576 options->ClientOnly = 1;
3577 tt_assert(server_mode(options) == 0);
3578 tt_assert(public_server_mode(options) == 0);
3579 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3580 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3581 OP_EQ, 1);
3583 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3584 memset(options, 0, sizeof(or_options_t));
3585 options->UseBridges = 1;
3586 tt_assert(server_mode(options) == 0);
3587 tt_assert(public_server_mode(options) == 0);
3588 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3589 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3590 OP_EQ, 1);
3592 /* Bridge Relays (Bridges) must act like clients, and use multiple
3593 * directory mirrors for bootstrap */
3594 memset(options, 0, sizeof(or_options_t));
3595 options->BridgeRelay = 1;
3596 options->ORPort_set = 1;
3597 tt_assert(server_mode(options) == 1);
3598 tt_assert(public_server_mode(options) == 0);
3599 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3600 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3601 OP_EQ, 1);
3603 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3604 * but can use multiple authorities for bootstrap */
3605 memset(options, 0, sizeof(or_options_t));
3606 options->FetchDirInfoEarly = 1;
3607 tt_assert(server_mode(options) == 0);
3608 tt_assert(public_server_mode(options) == 0);
3609 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3610 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3611 OP_EQ, 1);
3613 /* OR servers only fetch the consensus from the authorities when they don't
3614 * know their own address, but never use multiple directories for bootstrap
3616 memset(options, 0, sizeof(or_options_t));
3617 options->ORPort_set = 1;
3619 mock_router_pick_published_address_result = -1;
3620 tt_assert(server_mode(options) == 1);
3621 tt_assert(public_server_mode(options) == 1);
3622 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3623 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3624 OP_EQ, 0);
3626 mock_router_pick_published_address_result = 0;
3627 tt_assert(server_mode(options) == 1);
3628 tt_assert(public_server_mode(options) == 1);
3629 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3630 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3631 OP_EQ, 0);
3633 /* Exit OR servers only fetch the consensus from the authorities when they
3634 * refuse unknown exits, but never use multiple directories for bootstrap
3636 memset(options, 0, sizeof(or_options_t));
3637 options->ORPort_set = 1;
3638 options->ExitRelay = 1;
3639 mock_router_pick_published_address_result = 0;
3640 mock_router_my_exit_policy_is_reject_star_result = 0;
3641 mock_advertised_server_mode_result = 1;
3642 mock_router_get_my_routerinfo_result = &routerinfo;
3644 routerinfo.supports_tunnelled_dir_requests = 1;
3646 options->RefuseUnknownExits = 1;
3647 tt_assert(server_mode(options) == 1);
3648 tt_assert(public_server_mode(options) == 1);
3649 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3650 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3651 OP_EQ, 0);
3653 options->RefuseUnknownExits = 0;
3654 mock_router_pick_published_address_result = 0;
3655 tt_assert(server_mode(options) == 1);
3656 tt_assert(public_server_mode(options) == 1);
3657 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3658 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3659 OP_EQ, 0);
3661 /* Dir servers fetch the consensus from the authorities, unless they are not
3662 * advertising themselves (hibernating) or have no routerinfo or are not
3663 * advertising their dirport, and never use multiple directories for
3664 * bootstrap. This only applies if they are also OR servers.
3665 * (We don't care much about the behaviour of non-OR directory servers.) */
3666 memset(options, 0, sizeof(or_options_t));
3667 options->DirPort_set = 1;
3668 options->ORPort_set = 1;
3669 options->DirCache = 1;
3670 mock_router_pick_published_address_result = 0;
3671 mock_router_my_exit_policy_is_reject_star_result = 1;
3673 mock_advertised_server_mode_result = 1;
3674 routerinfo.dir_port = 1;
3675 mock_router_get_my_routerinfo_result = &routerinfo;
3676 tt_assert(server_mode(options) == 1);
3677 tt_assert(public_server_mode(options) == 1);
3678 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3679 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3680 OP_EQ, 0);
3682 mock_advertised_server_mode_result = 0;
3683 routerinfo.dir_port = 1;
3684 mock_router_get_my_routerinfo_result = &routerinfo;
3685 tt_assert(server_mode(options) == 1);
3686 tt_assert(public_server_mode(options) == 1);
3687 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3688 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3689 OP_EQ, 0);
3691 mock_advertised_server_mode_result = 1;
3692 mock_router_get_my_routerinfo_result = NULL;
3693 tt_assert(server_mode(options) == 1);
3694 tt_assert(public_server_mode(options) == 1);
3695 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3696 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3697 OP_EQ, 0);
3699 mock_advertised_server_mode_result = 1;
3700 routerinfo.dir_port = 0;
3701 routerinfo.supports_tunnelled_dir_requests = 0;
3702 mock_router_get_my_routerinfo_result = &routerinfo;
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),
3707 OP_EQ, 0);
3709 mock_advertised_server_mode_result = 1;
3710 routerinfo.dir_port = 1;
3711 routerinfo.supports_tunnelled_dir_requests = 1;
3712 mock_router_get_my_routerinfo_result = &routerinfo;
3713 tt_assert(server_mode(options) == 1);
3714 tt_assert(public_server_mode(options) == 1);
3715 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3716 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3717 OP_EQ, 0);
3719 done:
3720 tor_free(options);
3721 UNMOCK(router_pick_published_address);
3722 UNMOCK(router_get_my_routerinfo);
3723 UNMOCK(advertised_server_mode);
3724 UNMOCK(router_my_exit_policy_is_reject_star);
3727 static void
3728 test_config_default_fallback_dirs(void *arg)
3730 const char *fallback[] = {
3731 #include "../or/fallback_dirs.inc"
3732 NULL
3735 int n_included_fallback_dirs = 0;
3736 int n_added_fallback_dirs = 0;
3738 (void)arg;
3739 clear_dir_servers();
3741 while (fallback[n_included_fallback_dirs])
3742 n_included_fallback_dirs++;
3744 add_default_fallback_dir_servers();
3746 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
3748 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
3750 done:
3751 clear_dir_servers();
3754 static void
3755 test_config_port_cfg_line_extract_addrport(void *arg)
3757 (void)arg;
3758 int unixy = 0;
3759 const char *rest = NULL;
3760 char *a = NULL;
3762 tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
3763 tt_int_op(unixy, OP_EQ, 0);
3764 tt_str_op(a, OP_EQ, "");
3765 tt_str_op(rest, OP_EQ, "");
3766 tor_free(a);
3768 tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
3769 OP_EQ, 0);
3770 tt_int_op(unixy, OP_EQ, 0);
3771 tt_str_op(a, OP_EQ, "hello");
3772 tt_str_op(rest, OP_EQ, "");
3773 tor_free(a);
3775 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
3776 &a, &unixy, &rest), OP_EQ, 0);
3777 tt_int_op(unixy, OP_EQ, 0);
3778 tt_str_op(a, OP_EQ, "flipperwalt");
3779 tt_str_op(rest, OP_EQ, "gersplut");
3780 tor_free(a);
3782 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
3783 &a, &unixy, &rest), OP_EQ, 0);
3784 tt_int_op(unixy, OP_EQ, 0);
3785 tt_str_op(a, OP_EQ, "flipperwalt");
3786 tt_str_op(rest, OP_EQ, "gersplut");
3787 tor_free(a);
3789 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
3790 &a, &unixy, &rest), OP_EQ, 0);
3791 tt_int_op(unixy, OP_EQ, 0);
3792 tt_str_op(a, OP_EQ, "flipperwalt");
3793 tt_str_op(rest, OP_EQ, "gersplut");
3794 tor_free(a);
3796 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
3797 &a, &unixy, &rest), OP_EQ, 0);
3798 tt_int_op(unixy, OP_EQ, 1);
3799 tt_str_op(a, OP_EQ, "flipperwalt");
3800 tt_str_op(rest, OP_EQ, "gersplut");
3801 tor_free(a);
3803 tt_int_op(port_cfg_line_extract_addrport("lolol",
3804 &a, &unixy, &rest), OP_EQ, 0);
3805 tt_int_op(unixy, OP_EQ, 0);
3806 tt_str_op(a, OP_EQ, "lolol");
3807 tt_str_op(rest, OP_EQ, "");
3808 tor_free(a);
3810 tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
3811 &a, &unixy, &rest), OP_EQ, 0);
3812 tt_int_op(unixy, OP_EQ, 1);
3813 tt_str_op(a, OP_EQ, "lolol");
3814 tt_str_op(rest, OP_EQ, "");
3815 tor_free(a);
3817 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
3818 &a, &unixy, &rest), OP_EQ, 0);
3819 tt_int_op(unixy, OP_EQ, 1);
3820 tt_str_op(a, OP_EQ, "lolol");
3821 tt_str_op(rest, OP_EQ, "");
3822 tor_free(a);
3824 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
3825 &a, &unixy, &rest), OP_EQ, 0);
3826 tt_int_op(unixy, OP_EQ, 1);
3827 tt_str_op(a, OP_EQ, "lolol");
3828 tt_str_op(rest, OP_EQ, "");
3829 tor_free(a);
3831 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
3832 &a, &unixy, &rest), OP_EQ, 0);
3833 tt_int_op(unixy, OP_EQ, 0);
3834 tt_str_op(a, OP_EQ, "foobar:lolol");
3835 tt_str_op(rest, OP_EQ, "");
3836 tor_free(a);
3838 tt_int_op(port_cfg_line_extract_addrport(":lolol",
3839 &a, &unixy, &rest), OP_EQ, 0);
3840 tt_int_op(unixy, OP_EQ, 0);
3841 tt_str_op(a, OP_EQ, ":lolol");
3842 tt_str_op(rest, OP_EQ, "");
3843 tor_free(a);
3845 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
3846 &a, &unixy, &rest), OP_EQ, 0);
3847 tt_int_op(unixy, OP_EQ, 1);
3848 tt_str_op(a, OP_EQ, "lolol");
3849 tt_str_op(rest, OP_EQ, "");
3850 tor_free(a);
3852 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
3853 &a, &unixy, &rest), OP_EQ, 0);
3854 tt_int_op(unixy, OP_EQ, 1);
3855 tt_str_op(a, OP_EQ, "lolol");
3856 tt_str_op(rest, OP_EQ, "");
3857 tor_free(a);
3859 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
3860 &a, &unixy, &rest), OP_EQ, 0);
3861 tt_int_op(unixy, OP_EQ, 1);
3862 tt_str_op(a, OP_EQ, "lolol");
3863 tt_str_op(rest, OP_EQ, "foo ");
3864 tor_free(a);
3866 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
3867 &a, &unixy, &rest), OP_EQ, 0);
3868 tt_int_op(unixy, OP_EQ, 1);
3869 tt_str_op(a, OP_EQ, "lol ol");
3870 tt_str_op(rest, OP_EQ, "foo ");
3871 tor_free(a);
3873 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
3874 &a, &unixy, &rest), OP_EQ, 0);
3875 tt_int_op(unixy, OP_EQ, 1);
3876 tt_str_op(a, OP_EQ, "lol\" ol");
3877 tt_str_op(rest, OP_EQ, "foo ");
3878 tor_free(a);
3880 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
3881 &a, &unixy, &rest), OP_EQ, -1);
3882 tor_free(a);
3884 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
3885 &a, &unixy, &rest), OP_EQ, -1);
3886 tor_free(a);
3888 done:
3889 tor_free(a);
3892 static config_line_t *
3893 mock_config_line(const char *key, const char *val)
3895 config_line_t *config_line = tor_malloc(sizeof(config_line_t));
3896 memset(config_line, 0, sizeof(config_line_t));
3897 config_line->key = tor_strdup(key);
3898 config_line->value = tor_strdup(val);
3899 return config_line;
3902 static void
3903 test_config_parse_port_config__ports__no_ports_given(void *data)
3905 (void)data;
3906 int ret;
3907 smartlist_t *slout = NULL;
3908 port_cfg_t *port_cfg = NULL;
3910 slout = smartlist_new();
3912 // Test no defaultport, no defaultaddress and no out
3913 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
3914 tt_int_op(ret, OP_EQ, 0);
3916 // Test with defaultport, no defaultaddress and no out
3917 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
3918 tt_int_op(ret, OP_EQ, 0);
3920 // Test no defaultport, with defaultaddress and no out
3921 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3922 tt_int_op(ret, OP_EQ, 0);
3924 // Test with defaultport, with defaultaddress and no out
3925 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3926 tt_int_op(ret, OP_EQ, 0);
3928 // Test no defaultport, no defaultaddress and with out
3929 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 0, 0);
3930 tt_int_op(ret, OP_EQ, 0);
3931 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3933 // Test with defaultport, no defaultaddress and with out
3934 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 42, 0);
3935 tt_int_op(ret, OP_EQ, 0);
3936 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3938 // Test no defaultport, with defaultaddress and with out
3939 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3940 tt_int_op(ret, OP_EQ, 0);
3941 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3943 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3944 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3945 smartlist_clear(slout);
3946 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3947 tt_int_op(ret, OP_EQ, 0);
3948 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3949 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3950 tt_int_op(port_cfg->port, OP_EQ, 42);
3951 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
3953 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3954 // for a unix address
3955 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3956 smartlist_clear(slout);
3957 ret = parse_port_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
3958 42, CL_PORT_IS_UNIXSOCKET);
3959 tt_int_op(ret, OP_EQ, 0);
3960 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3961 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3962 tt_int_op(port_cfg->port, OP_EQ, 0);
3963 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
3964 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
3966 done:
3967 if (slout)
3968 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3969 smartlist_free(slout);
3972 static void
3973 test_config_parse_port_config__ports__ports_given(void *data)
3975 (void)data;
3976 int ret;
3977 smartlist_t *slout = NULL;
3978 port_cfg_t *port_cfg = NULL;
3979 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
3980 tor_addr_t addr;
3982 slout = smartlist_new();
3984 // Test error when encounters an invalid Port specification
3985 config_port_invalid = mock_config_line("DNSPort", "");
3986 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
3987 0, 0);
3988 tt_int_op(ret, OP_EQ, -1);
3990 // Test error when encounters an empty unix domain specification
3991 config_free_lines(config_port_invalid); config_port_invalid = NULL;
3992 config_port_invalid = mock_config_line("DNSPort", "unix:");
3993 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
3994 0, 0);
3995 tt_int_op(ret, OP_EQ, -1);
3997 // Test error when encounters a unix domain specification but the listener
3998 // doesn't support domain sockets
3999 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
4000 ret = parse_port_config(NULL, config_port_valid, "DNS",
4001 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
4002 tt_int_op(ret, OP_EQ, -1);
4004 // Test valid unix domain
4005 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4006 smartlist_clear(slout);
4007 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4008 CONN_TYPE_AP_LISTENER, NULL, 0, 0);
4009 #ifdef _WIN32
4010 tt_int_op(ret, OP_EQ, -1);
4011 #else
4012 tt_int_op(ret, OP_EQ, 0);
4013 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4014 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4015 tt_int_op(port_cfg->port, OP_EQ, 0);
4016 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4017 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
4018 /* Test entry port defaults as initialised in parse_port_config */
4019 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4020 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4021 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4022 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4023 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
4024 #endif /* defined(_WIN32) */
4026 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
4027 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4028 config_port_invalid = mock_config_line("SOCKSPort",
4029 "unix:/tmp/foo/bar NoIPv4Traffic "
4030 "NoIPv6Traffic "
4031 "NoOnionTraffic");
4032 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4033 CONN_TYPE_AP_LISTENER, NULL, 0,
4034 CL_PORT_TAKES_HOSTNAMES);
4035 tt_int_op(ret, OP_EQ, -1);
4037 // Test failure if we have no DNS and we're a DNSPort
4038 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4039 config_port_invalid = mock_config_line("DNSPort",
4040 "127.0.0.1:80 NoDNSRequest");
4041 ret = parse_port_config(NULL, config_port_invalid, "DNS",
4042 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4043 CL_PORT_TAKES_HOSTNAMES);
4044 tt_int_op(ret, OP_EQ, -1);
4046 // If we're a DNSPort, DNS only is ok
4047 // Use a port because DNSPort doesn't support sockets
4048 config_free_lines(config_port_valid); config_port_valid = NULL;
4049 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4050 smartlist_clear(slout);
4051 config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
4052 "NoIPv6Traffic "
4053 "NoIPv4Traffic NoOnionTraffic");
4054 ret = parse_port_config(slout, config_port_valid, "DNS",
4055 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4056 CL_PORT_TAKES_HOSTNAMES);
4057 tt_int_op(ret, OP_EQ, 0);
4058 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4059 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4060 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4061 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4062 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4063 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
4065 // Test failure if we have DNS but no ipv4 and no ipv6
4066 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4067 config_port_invalid = mock_config_line("SOCKSPort",
4068 "NoIPv6Traffic "
4069 "unix:/tmp/foo/bar NoIPv4Traffic");
4070 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4071 CONN_TYPE_AP_LISTENER, NULL, 0,
4072 CL_PORT_TAKES_HOSTNAMES);
4073 tt_int_op(ret, OP_EQ, -1);
4075 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
4076 // options)
4077 config_free_lines(config_port_valid); config_port_valid = NULL;
4078 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4079 smartlist_clear(slout);
4080 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4081 "NoIPv6Traffic "
4082 "NoDNSRequest NoIPv4Traffic");
4083 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4084 CONN_TYPE_AP_LISTENER, NULL, 0,
4085 CL_PORT_TAKES_HOSTNAMES);
4086 #ifdef _WIN32
4087 tt_int_op(ret, OP_EQ, -1);
4088 #else
4089 tt_int_op(ret, OP_EQ, 0);
4090 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4091 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4092 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4093 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4094 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4095 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4096 #endif /* defined(_WIN32) */
4098 // Test success with quoted unix: address.
4099 config_free_lines(config_port_valid); config_port_valid = NULL;
4100 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4101 smartlist_clear(slout);
4102 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
4103 "NoIPv6Traffic "
4104 "NoDNSRequest NoIPv4Traffic");
4105 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4106 CONN_TYPE_AP_LISTENER, NULL, 0,
4107 CL_PORT_TAKES_HOSTNAMES);
4108 #ifdef _WIN32
4109 tt_int_op(ret, OP_EQ, -1);
4110 #else
4111 tt_int_op(ret, OP_EQ, 0);
4112 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4113 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4114 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4115 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4116 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4117 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4118 #endif /* defined(_WIN32) */
4120 // Test failure with broken quoted unix: address.
4121 config_free_lines(config_port_valid); config_port_valid = NULL;
4122 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4123 smartlist_clear(slout);
4124 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
4125 "NoIPv6Traffic "
4126 "NoDNSRequest NoIPv4Traffic");
4127 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4128 CONN_TYPE_AP_LISTENER, NULL, 0,
4129 CL_PORT_TAKES_HOSTNAMES);
4130 tt_int_op(ret, OP_EQ, -1);
4132 // Test failure with empty quoted unix: address.
4133 config_free_lines(config_port_valid); config_port_valid = NULL;
4134 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4135 smartlist_clear(slout);
4136 config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
4137 "NoIPv6Traffic "
4138 "NoDNSRequest NoIPv4Traffic");
4139 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4140 CONN_TYPE_AP_LISTENER, NULL, 0,
4141 CL_PORT_TAKES_HOSTNAMES);
4142 tt_int_op(ret, OP_EQ, -1);
4144 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
4145 config_free_lines(config_port_valid); config_port_valid = NULL;
4146 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4147 smartlist_clear(slout);
4148 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4149 "OnionTrafficOnly");
4150 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4151 CONN_TYPE_AP_LISTENER, NULL, 0,
4152 CL_PORT_TAKES_HOSTNAMES);
4153 #ifdef _WIN32
4154 tt_int_op(ret, OP_EQ, -1);
4155 #else
4156 tt_int_op(ret, OP_EQ, 0);
4157 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4158 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4159 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4160 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4161 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4162 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4163 #endif /* defined(_WIN32) */
4165 // Test success with no ipv4 but take ipv6
4166 config_free_lines(config_port_valid); config_port_valid = NULL;
4167 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4168 smartlist_clear(slout);
4169 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4170 "NoIPv4Traffic IPv6Traffic");
4171 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4172 CONN_TYPE_AP_LISTENER, NULL, 0,
4173 CL_PORT_TAKES_HOSTNAMES);
4174 #ifdef _WIN32
4175 tt_int_op(ret, OP_EQ, -1);
4176 #else
4177 tt_int_op(ret, OP_EQ, 0);
4178 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4179 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4180 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4181 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4182 #endif /* defined(_WIN32) */
4184 // Test success with both ipv4 and ipv6
4185 config_free_lines(config_port_valid); config_port_valid = NULL;
4186 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4187 smartlist_clear(slout);
4188 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4189 "IPv4Traffic IPv6Traffic");
4190 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4191 CONN_TYPE_AP_LISTENER, NULL, 0,
4192 CL_PORT_TAKES_HOSTNAMES);
4193 #ifdef _WIN32
4194 tt_int_op(ret, OP_EQ, -1);
4195 #else
4196 tt_int_op(ret, OP_EQ, 0);
4197 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4198 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4199 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4200 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4201 #endif /* defined(_WIN32) */
4203 // Test failure if we specify world writable for an IP Port
4204 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4205 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
4206 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4207 "127.0.0.3", 0, 0);
4208 tt_int_op(ret, OP_EQ, -1);
4210 // Test failure if we specify group writable for an IP Port
4211 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4212 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
4213 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4214 "127.0.0.3", 0, 0);
4215 tt_int_op(ret, OP_EQ, -1);
4217 // Test failure if we specify group writable for an IP Port
4218 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4219 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4220 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4221 "127.0.0.3", 0, 0);
4222 tt_int_op(ret, OP_EQ, -1);
4224 // Test success with only a port (this will fail without a default address)
4225 config_free_lines(config_port_valid); config_port_valid = NULL;
4226 config_port_valid = mock_config_line("DNSPort", "42");
4227 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4228 "127.0.0.3", 0, 0);
4229 tt_int_op(ret, OP_EQ, 0);
4231 // Test success with only a port and isolate destination port
4232 config_free_lines(config_port_valid); config_port_valid = NULL;
4233 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4234 smartlist_clear(slout);
4235 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
4236 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4237 "127.0.0.3", 0, 0);
4238 tt_int_op(ret, OP_EQ, 0);
4239 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4240 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4241 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4242 ISO_DEFAULT | ISO_DESTPORT);
4244 // Test success with a negative isolate destination port, and plural
4245 config_free_lines(config_port_valid); config_port_valid = NULL;
4246 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4247 smartlist_clear(slout);
4248 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4249 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4250 "127.0.0.3", 0, 0);
4251 tt_int_op(ret, OP_EQ, 0);
4252 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4253 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4254 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4255 ISO_DEFAULT & ~ISO_DESTPORT);
4257 // Test success with isolate destination address
4258 config_free_lines(config_port_valid); config_port_valid = NULL;
4259 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4260 smartlist_clear(slout);
4261 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
4262 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4263 "127.0.0.3", 0, 0);
4264 tt_int_op(ret, OP_EQ, 0);
4265 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4266 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4267 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4268 ISO_DEFAULT | ISO_DESTADDR);
4270 // Test success with isolate socks AUTH
4271 config_free_lines(config_port_valid); config_port_valid = NULL;
4272 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4273 smartlist_clear(slout);
4274 config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
4275 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4276 "127.0.0.3", 0, 0);
4277 tt_int_op(ret, OP_EQ, 0);
4278 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4279 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4280 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4281 ISO_DEFAULT | ISO_SOCKSAUTH);
4283 // Test success with isolate client protocol
4284 config_free_lines(config_port_valid); config_port_valid = NULL;
4285 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4286 smartlist_clear(slout);
4287 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
4288 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4289 "127.0.0.3", 0, 0);
4290 tt_int_op(ret, OP_EQ, 0);
4291 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4292 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4293 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4294 ISO_DEFAULT | ISO_CLIENTPROTO);
4296 // Test success with isolate client address
4297 config_free_lines(config_port_valid); config_port_valid = NULL;
4298 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4299 smartlist_clear(slout);
4300 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
4301 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4302 "127.0.0.3", 0, 0);
4303 tt_int_op(ret, OP_EQ, 0);
4304 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4305 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4306 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4307 ISO_DEFAULT | ISO_CLIENTADDR);
4309 // Test success with ignored unknown options
4310 config_free_lines(config_port_valid); config_port_valid = NULL;
4311 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4312 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4313 "127.0.0.3", 0, 0);
4314 tt_int_op(ret, OP_EQ, 0);
4316 // Test success with no isolate socks AUTH
4317 config_free_lines(config_port_valid); config_port_valid = NULL;
4318 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4319 smartlist_clear(slout);
4320 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
4321 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4322 "127.0.0.3", 0, 0);
4323 tt_int_op(ret, OP_EQ, 0);
4324 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4325 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4326 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4328 // Test success with prefer ipv6
4329 config_free_lines(config_port_valid); config_port_valid = NULL;
4330 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4331 smartlist_clear(slout);
4332 config_port_valid = mock_config_line("SOCKSPort",
4333 "42 IPv6Traffic PreferIPv6");
4334 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4335 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
4336 CL_PORT_TAKES_HOSTNAMES);
4337 tt_int_op(ret, OP_EQ, 0);
4338 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4339 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4340 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
4342 // Test success with cache ipv4 DNS
4343 config_free_lines(config_port_valid); config_port_valid = NULL;
4344 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4345 smartlist_clear(slout);
4346 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
4347 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4348 "127.0.0.42", 0, 0);
4349 tt_int_op(ret, OP_EQ, 0);
4350 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4351 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4352 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4353 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4355 // Test success with cache ipv6 DNS
4356 config_free_lines(config_port_valid); config_port_valid = NULL;
4357 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4358 smartlist_clear(slout);
4359 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
4360 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4361 "127.0.0.42", 0, 0);
4362 tt_int_op(ret, OP_EQ, 0);
4363 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4364 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4365 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4366 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4368 // Test success with no cache ipv4 DNS
4369 config_free_lines(config_port_valid); config_port_valid = NULL;
4370 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4371 smartlist_clear(slout);
4372 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4373 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4374 "127.0.0.42", 0, 0);
4375 tt_int_op(ret, OP_EQ, 0);
4376 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4377 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4378 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4379 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4381 // Test success with cache DNS
4382 config_free_lines(config_port_valid); config_port_valid = NULL;
4383 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4384 smartlist_clear(slout);
4385 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
4386 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4387 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
4388 tt_int_op(ret, OP_EQ, 0);
4389 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4390 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4391 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4392 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4394 // Test success with use cached ipv4 DNS
4395 config_free_lines(config_port_valid); config_port_valid = NULL;
4396 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4397 smartlist_clear(slout);
4398 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
4399 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4400 "127.0.0.42", 0, 0);
4401 tt_int_op(ret, OP_EQ, 0);
4402 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4403 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4404 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4405 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
4407 // Test success with use cached ipv6 DNS
4408 config_free_lines(config_port_valid); config_port_valid = NULL;
4409 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4410 smartlist_clear(slout);
4411 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
4412 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4413 "127.0.0.42", 0, 0);
4414 tt_int_op(ret, OP_EQ, 0);
4415 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4416 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4417 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
4418 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4420 // Test success with use cached DNS
4421 config_free_lines(config_port_valid); config_port_valid = NULL;
4422 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4423 smartlist_clear(slout);
4424 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
4425 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4426 "127.0.0.42", 0, 0);
4427 tt_int_op(ret, OP_EQ, 0);
4428 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4429 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4430 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4431 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4433 // Test success with not preferring ipv6 automap
4434 config_free_lines(config_port_valid); config_port_valid = NULL;
4435 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4436 smartlist_clear(slout);
4437 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4438 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4439 "127.0.0.42", 0, 0);
4440 tt_int_op(ret, OP_EQ, 0);
4441 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4442 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4443 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
4445 // Test success with prefer SOCKS no auth
4446 config_free_lines(config_port_valid); config_port_valid = NULL;
4447 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4448 smartlist_clear(slout);
4449 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4450 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4451 "127.0.0.42", 0, 0);
4452 tt_int_op(ret, OP_EQ, 0);
4453 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4454 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4455 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4457 // Test failure with both a zero port and a non-zero port
4458 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4459 config_free_lines(config_port_valid); config_port_valid = NULL;
4460 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4461 smartlist_clear(slout);
4462 config_port_invalid = mock_config_line("DNSPort", "0");
4463 config_port_valid = mock_config_line("DNSPort", "42");
4464 config_port_invalid->next = config_port_valid;
4465 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4466 "127.0.0.42", 0, 0);
4467 tt_int_op(ret, OP_EQ, -1);
4469 // Test success with warn non-local control
4470 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4471 smartlist_clear(slout);
4472 ret = parse_port_config(slout, config_port_valid, "Control",
4473 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
4474 CL_PORT_WARN_NONLOCAL);
4475 tt_int_op(ret, OP_EQ, 0);
4477 // Test success with warn non-local listener
4478 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4479 smartlist_clear(slout);
4480 ret = parse_port_config(slout, config_port_valid, "ExtOR",
4481 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
4482 CL_PORT_WARN_NONLOCAL);
4483 tt_int_op(ret, OP_EQ, 0);
4485 // Test success with warn non-local other
4486 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4487 smartlist_clear(slout);
4488 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4489 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4490 tt_int_op(ret, OP_EQ, 0);
4492 // Test success with warn non-local other without out
4493 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4494 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4495 tt_int_op(ret, OP_EQ, 0);
4497 // Test success with both ipv4 and ipv6 but without stream options
4498 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4499 config_port_valid = NULL;
4500 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4501 smartlist_clear(slout);
4502 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
4503 "IPv6Traffic");
4504 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4505 "127.0.0.44", 0,
4506 CL_PORT_TAKES_HOSTNAMES |
4507 CL_PORT_NO_STREAM_OPTIONS);
4508 tt_int_op(ret, OP_EQ, 0);
4509 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4510 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4511 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4512 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4514 // Test failure for a SessionGroup argument with invalid value
4515 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4516 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4517 smartlist_clear(slout);
4518 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
4519 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4520 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4521 tt_int_op(ret, OP_EQ, -1);
4523 // TODO: this seems wrong. Shouldn't it be the other way around?
4524 // Potential bug.
4525 // Test failure for a SessionGroup argument with valid value but with stream
4526 // options allowed
4527 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4528 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4529 smartlist_clear(slout);
4530 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
4531 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4532 "127.0.0.44", 0, 0);
4533 tt_int_op(ret, OP_EQ, -1);
4535 // Test failure for more than one SessionGroup argument
4536 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4537 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4538 smartlist_clear(slout);
4539 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
4540 "SessionGroup=321");
4541 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4542 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4543 tt_int_op(ret, OP_EQ, -1);
4545 // Test success with a sessiongroup options
4546 config_free_lines(config_port_valid); config_port_valid = NULL;
4547 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4548 smartlist_clear(slout);
4549 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
4550 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4551 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4552 tt_int_op(ret, OP_EQ, 0);
4553 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4554 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4555 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
4557 // Test success with a zero unix domain socket, and doesnt add it to out
4558 config_free_lines(config_port_valid); config_port_valid = NULL;
4559 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4560 smartlist_clear(slout);
4561 config_port_valid = mock_config_line("DNSPort", "0");
4562 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4563 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4564 tt_int_op(ret, OP_EQ, 0);
4565 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4567 // Test success with a one unix domain socket, and doesnt add it to out
4568 config_free_lines(config_port_valid); config_port_valid = NULL;
4569 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4570 smartlist_clear(slout);
4571 config_port_valid = mock_config_line("DNSPort", "something");
4572 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4573 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4574 tt_int_op(ret, OP_EQ, 0);
4575 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4576 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4577 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4578 tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
4580 // Test success with a port of auto - it uses the default address
4581 config_free_lines(config_port_valid); config_port_valid = NULL;
4582 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4583 smartlist_clear(slout);
4584 config_port_valid = mock_config_line("DNSPort", "auto");
4585 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4586 "127.0.0.46", 0, 0);
4587 tt_int_op(ret, OP_EQ, 0);
4588 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4589 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4590 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4591 tor_addr_parse(&addr, "127.0.0.46");
4592 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4594 // Test success with parsing both an address and an auto port
4595 config_free_lines(config_port_valid); config_port_valid = NULL;
4596 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4597 smartlist_clear(slout);
4598 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
4599 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4600 "127.0.0.46", 0, 0);
4601 tt_int_op(ret, OP_EQ, 0);
4602 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4603 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4604 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4605 tor_addr_parse(&addr, "127.0.0.122");
4606 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4608 // Test failure when asked to parse an invalid address followed by auto
4609 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4610 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
4611 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
4612 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4613 "127.0.0.46", 0, 0);
4614 UNMOCK(tor_addr_lookup);
4615 tt_int_op(ret, OP_EQ, -1);
4617 // Test success with parsing both an address and a real port
4618 config_free_lines(config_port_valid); config_port_valid = NULL;
4619 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4620 smartlist_clear(slout);
4621 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
4622 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4623 "127.0.0.46", 0, 0);
4624 tt_int_op(ret, OP_EQ, 0);
4625 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4626 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4627 tt_int_op(port_cfg->port, OP_EQ, 656);
4628 tor_addr_parse(&addr, "127.0.0.123");
4629 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4631 // Test failure if we can't parse anything at all
4632 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4633 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4634 smartlist_clear(slout);
4635 config_port_invalid = mock_config_line("DNSPort", "something wrong");
4636 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4637 "127.0.0.46", 0, 0);
4638 tt_int_op(ret, OP_EQ, -1);
4640 // Test failure if we find both an address, a port and an auto
4641 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4642 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4643 smartlist_clear(slout);
4644 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
4645 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4646 "127.0.0.46", 0, 0);
4647 tt_int_op(ret, OP_EQ, -1);
4649 // Test that default to group writeable default sets group writeable for
4650 // domain socket
4651 config_free_lines(config_port_valid); config_port_valid = NULL;
4652 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4653 smartlist_clear(slout);
4654 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
4655 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4656 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
4657 CL_PORT_DFLT_GROUP_WRITABLE);
4658 #ifdef _WIN32
4659 tt_int_op(ret, OP_EQ, -1);
4660 #else
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->is_group_writable, OP_EQ, 1);
4665 #endif /* defined(_WIN32) */
4667 done:
4668 if (slout)
4669 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4670 smartlist_free(slout);
4671 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4672 config_free_lines(config_port_valid); config_port_valid = NULL;
4675 static void
4676 test_config_parse_port_config__ports__server_options(void *data)
4678 (void)data;
4679 int ret;
4680 smartlist_t *slout = NULL;
4681 port_cfg_t *port_cfg = NULL;
4682 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4684 slout = smartlist_new();
4686 // Test success with NoAdvertise option
4687 config_free_lines(config_port_valid); config_port_valid = NULL;
4688 config_port_valid = mock_config_line("DNSPort",
4689 "127.0.0.124:656 NoAdvertise");
4690 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4691 CL_PORT_SERVER_OPTIONS);
4692 tt_int_op(ret, OP_EQ, 0);
4693 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4694 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4695 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
4696 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
4698 // Test success with NoListen option
4699 config_free_lines(config_port_valid); config_port_valid = NULL;
4700 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4701 smartlist_clear(slout);
4702 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
4703 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4704 CL_PORT_SERVER_OPTIONS);
4705 tt_int_op(ret, OP_EQ, 0);
4706 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4707 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4708 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
4709 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
4711 // Test failure with both NoAdvertise and NoListen option
4712 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4713 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4714 smartlist_clear(slout);
4715 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
4716 "NoAdvertise");
4717 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4718 0, CL_PORT_SERVER_OPTIONS);
4719 tt_int_op(ret, OP_EQ, -1);
4721 // Test success with IPv4Only
4722 config_free_lines(config_port_valid); config_port_valid = NULL;
4723 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4724 smartlist_clear(slout);
4725 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
4726 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4727 CL_PORT_SERVER_OPTIONS);
4728 tt_int_op(ret, OP_EQ, 0);
4729 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4730 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4731 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
4732 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
4734 // Test success with IPv6Only
4735 config_free_lines(config_port_valid); config_port_valid = NULL;
4736 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4737 smartlist_clear(slout);
4738 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
4739 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4740 CL_PORT_SERVER_OPTIONS);
4741 tt_int_op(ret, OP_EQ, 0);
4742 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4743 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4744 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
4745 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
4747 // Test failure with both IPv4Only and IPv6Only
4748 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4749 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4750 smartlist_clear(slout);
4751 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
4752 "IPv4Only");
4753 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4754 0, CL_PORT_SERVER_OPTIONS);
4755 tt_int_op(ret, OP_EQ, -1);
4757 // Test success with invalid parameter
4758 config_free_lines(config_port_valid); config_port_valid = NULL;
4759 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4760 smartlist_clear(slout);
4761 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
4762 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4763 CL_PORT_SERVER_OPTIONS);
4764 tt_int_op(ret, OP_EQ, 0);
4765 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4767 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
4768 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4769 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4770 smartlist_clear(slout);
4771 config_port_invalid = mock_config_line("DNSPort",
4772 "127.0.0.124:656 IPv6Only");
4773 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4774 0, CL_PORT_SERVER_OPTIONS);
4775 tt_int_op(ret, OP_EQ, -1);
4777 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
4778 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4779 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4780 smartlist_clear(slout);
4781 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
4782 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4783 0, CL_PORT_SERVER_OPTIONS);
4784 tt_int_op(ret, OP_EQ, -1);
4786 // Check for failure with empty unix: address.
4787 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4788 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4789 smartlist_clear(slout);
4790 config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
4791 ret = parse_port_config(slout, config_port_invalid, "ORPort", 0, NULL,
4792 0, CL_PORT_SERVER_OPTIONS);
4793 tt_int_op(ret, OP_EQ, -1);
4795 done:
4796 if (slout)
4797 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4798 smartlist_free(slout);
4799 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4800 config_free_lines(config_port_valid); config_port_valid = NULL;
4803 static void
4804 test_config_parse_log_severity(void *data)
4806 int ret;
4807 const char *severity_log_lines[] = {
4808 "debug file /tmp/debug.log",
4809 "debug\tfile /tmp/debug.log",
4810 "[handshake]debug [~net,~mm]info notice stdout",
4811 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
4812 NULL
4814 int i;
4815 log_severity_list_t *severity;
4817 (void) data;
4819 severity = tor_malloc(sizeof(log_severity_list_t));
4820 for (i = 0; severity_log_lines[i]; i++) {
4821 memset(severity, 0, sizeof(log_severity_list_t));
4822 ret = parse_log_severity_config(&severity_log_lines[i], severity);
4823 tt_int_op(ret, OP_EQ, 0);
4826 done:
4827 tor_free(severity);
4830 static void
4831 test_config_include_limit(void *data)
4833 (void)data;
4835 config_line_t *result = NULL;
4836 char *torrc_path = NULL;
4837 char *dir = tor_strdup(get_fname("test_include_limit"));
4838 tt_ptr_op(dir, OP_NE, NULL);
4840 #ifdef _WIN32
4841 tt_int_op(mkdir(dir), OP_EQ, 0);
4842 #else
4843 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4844 #endif
4846 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
4847 char torrc_contents[1000];
4848 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4849 torrc_path);
4850 tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
4852 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4853 OP_EQ, -1);
4855 done:
4856 config_free_lines(result);
4857 tor_free(torrc_path);
4858 tor_free(dir);
4861 static void
4862 test_config_include_does_not_exist(void *data)
4864 (void)data;
4866 config_line_t *result = NULL;
4867 char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
4868 char *missing_path = NULL;
4869 tt_ptr_op(dir, OP_NE, NULL);
4871 #ifdef _WIN32
4872 tt_int_op(mkdir(dir), OP_EQ, 0);
4873 #else
4874 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4875 #endif
4877 tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
4878 char torrc_contents[1000];
4879 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4880 missing_path);
4882 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4883 OP_EQ, -1);
4885 done:
4886 config_free_lines(result);
4887 tor_free(dir);
4888 tor_free(missing_path);
4891 static void
4892 test_config_include_error_in_included_file(void *data)
4894 (void)data;
4895 config_line_t *result = NULL;
4897 char *dir = tor_strdup(get_fname("test_error_in_included_file"));
4898 char *invalid_path = NULL;
4899 tt_ptr_op(dir, OP_NE, NULL);
4901 #ifdef _WIN32
4902 tt_int_op(mkdir(dir), OP_EQ, 0);
4903 #else
4904 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4905 #endif
4907 tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
4908 tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
4910 char torrc_contents[1000];
4911 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4912 invalid_path);
4914 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4915 OP_EQ, -1);
4917 done:
4918 config_free_lines(result);
4919 tor_free(dir);
4920 tor_free(invalid_path);
4923 static void
4924 test_config_include_empty_file_folder(void *data)
4926 (void)data;
4927 config_line_t *result = NULL;
4929 char *folder_path = NULL;
4930 char *file_path = NULL;
4931 char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
4932 tt_ptr_op(dir, OP_NE, NULL);
4934 #ifdef _WIN32
4935 tt_int_op(mkdir(dir), OP_EQ, 0);
4936 #else
4937 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4938 #endif
4940 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir);
4941 #ifdef _WIN32
4942 tt_int_op(mkdir(folder_path), OP_EQ, 0);
4943 #else
4944 tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
4945 #endif
4946 tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
4947 tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
4949 char torrc_contents[1000];
4950 tor_snprintf(torrc_contents, sizeof(torrc_contents),
4951 "%%include %s\n"
4952 "%%include %s\n",
4953 folder_path, file_path);
4955 int include_used;
4956 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
4957 NULL), OP_EQ, 0);
4958 tt_ptr_op(result, OP_EQ, NULL);
4959 tt_int_op(include_used, OP_EQ, 1);
4961 done:
4962 config_free_lines(result);
4963 tor_free(folder_path);
4964 tor_free(file_path);
4965 tor_free(dir);
4968 #ifndef _WIN32
4969 static void
4970 test_config_include_no_permission(void *data)
4972 (void)data;
4973 config_line_t *result = NULL;
4975 char *folder_path = NULL;
4976 char *dir = NULL;
4977 if (geteuid() == 0)
4978 tt_skip();
4980 dir = tor_strdup(get_fname("test_include_forbidden_folder"));
4981 tt_ptr_op(dir, OP_NE, NULL);
4983 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4985 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
4986 tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
4988 char torrc_contents[1000];
4989 tor_snprintf(torrc_contents, sizeof(torrc_contents),
4990 "%%include %s\n",
4991 folder_path);
4993 int include_used;
4994 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
4995 &include_used, NULL),
4996 OP_EQ, -1);
4997 tt_ptr_op(result, OP_EQ, NULL);
4999 done:
5000 config_free_lines(result);
5001 tor_free(folder_path);
5002 if (dir)
5003 chmod(dir, 0700);
5004 tor_free(dir);
5006 #endif
5008 static void
5009 test_config_include_recursion_before_after(void *data)
5011 (void)data;
5013 config_line_t *result = NULL;
5014 char *torrc_path = NULL;
5015 char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
5016 tt_ptr_op(dir, OP_NE, NULL);
5018 #ifdef _WIN32
5019 tt_int_op(mkdir(dir), OP_EQ, 0);
5020 #else
5021 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5022 #endif
5024 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5026 char file_contents[1000];
5027 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5028 int i;
5029 // Loop backwards so file_contents has the contents of the first file by the
5030 // end of the loop
5031 for (i = limit; i > 0; i--) {
5032 if (i < limit) {
5033 tor_snprintf(file_contents, sizeof(file_contents),
5034 "Test %d\n"
5035 "%%include %s%d\n"
5036 "Test %d\n",
5037 i, torrc_path, i + 1, 2 * limit - i);
5038 } else {
5039 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
5042 if (i > 1) {
5043 char *file_path = NULL;
5044 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5045 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5046 tor_free(file_path);
5050 int include_used;
5051 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5052 NULL), OP_EQ, 0);
5053 tt_ptr_op(result, OP_NE, NULL);
5054 tt_int_op(include_used, OP_EQ, 1);
5056 int len = 0;
5057 config_line_t *next;
5058 for (next = result; next != NULL; next = next->next) {
5059 char expected[10];
5060 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5061 tt_str_op(next->key, OP_EQ, "Test");
5062 tt_str_op(next->value, OP_EQ, expected);
5063 len++;
5065 tt_int_op(len, OP_EQ, 2 * limit - 1);
5067 done:
5068 config_free_lines(result);
5069 tor_free(dir);
5070 tor_free(torrc_path);
5073 static void
5074 test_config_include_recursion_after_only(void *data)
5076 (void)data;
5078 config_line_t *result = NULL;
5079 char *torrc_path = NULL;
5080 char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
5081 tt_ptr_op(dir, OP_NE, NULL);
5083 #ifdef _WIN32
5084 tt_int_op(mkdir(dir), OP_EQ, 0);
5085 #else
5086 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5087 #endif
5089 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5091 char file_contents[1000];
5092 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5093 int i;
5094 // Loop backwards so file_contents has the contents of the first file by the
5095 // end of the loop
5096 for (i = limit; i > 0; i--) {
5097 int n = (i - limit - 1) * -1;
5098 if (i < limit) {
5099 tor_snprintf(file_contents, sizeof(file_contents),
5100 "%%include %s%d\n"
5101 "Test %d\n",
5102 torrc_path, i + 1, n);
5103 } else {
5104 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
5107 if (i > 1) {
5108 char *file_path = NULL;
5109 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5110 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5111 tor_free(file_path);
5115 int include_used;
5116 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5117 NULL), OP_EQ, 0);
5118 tt_ptr_op(result, OP_NE, NULL);
5119 tt_int_op(include_used, OP_EQ, 1);
5121 int len = 0;
5122 config_line_t *next;
5123 for (next = result; next != NULL; next = next->next) {
5124 char expected[10];
5125 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5126 tt_str_op(next->key, OP_EQ, "Test");
5127 tt_str_op(next->value, OP_EQ, expected);
5128 len++;
5130 tt_int_op(len, OP_EQ, limit);
5132 done:
5133 config_free_lines(result);
5134 tor_free(dir);
5135 tor_free(torrc_path);
5138 static void
5139 test_config_include_folder_order(void *data)
5141 (void)data;
5143 config_line_t *result = NULL;
5144 char *torrcd = NULL;
5145 char *path = NULL;
5146 char *path2 = NULL;
5147 char *dir = tor_strdup(get_fname("test_include_folder_order"));
5148 tt_ptr_op(dir, OP_NE, NULL);
5150 #ifdef _WIN32
5151 tt_int_op(mkdir(dir), OP_EQ, 0);
5152 #else
5153 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5154 #endif
5156 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5158 #ifdef _WIN32
5159 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5160 #else
5161 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5162 #endif
5164 // test that files in subfolders are ignored
5165 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5167 #ifdef _WIN32
5168 tt_int_op(mkdir(path), OP_EQ, 0);
5169 #else
5170 tt_int_op(mkdir(path, 0700), OP_EQ, 0);
5171 #endif
5173 tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
5174 tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
5175 tor_free(path);
5177 // test that files starting with . are ignored
5178 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5179 tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
5180 tor_free(path);
5182 // test file order
5183 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
5184 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5185 tor_free(path);
5187 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
5188 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
5189 tor_free(path);
5191 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
5192 tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
5193 tor_free(path);
5195 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
5196 tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
5197 tor_free(path);
5199 char torrc_contents[1000];
5200 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5201 "%%include %s\n",
5202 torrcd);
5204 int include_used;
5205 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5206 NULL), OP_EQ, 0);
5207 tt_ptr_op(result, OP_NE, NULL);
5208 tt_int_op(include_used, OP_EQ, 1);
5210 int len = 0;
5211 config_line_t *next;
5212 for (next = result; next != NULL; next = next->next) {
5213 char expected[10];
5214 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5215 tt_str_op(next->key, OP_EQ, "Test");
5216 tt_str_op(next->value, OP_EQ, expected);
5217 len++;
5219 tt_int_op(len, OP_EQ, 4);
5221 done:
5222 config_free_lines(result);
5223 tor_free(torrcd);
5224 tor_free(path);
5225 tor_free(path2);
5226 tor_free(dir);
5229 static void
5230 test_config_include_path_syntax(void *data)
5232 (void)data;
5234 config_line_t *result = NULL;
5235 char *dir = tor_strdup(get_fname("test_include_path_syntax"));
5236 char *esc_dir = NULL, *dir_with_pathsep = NULL,
5237 *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
5238 tt_ptr_op(dir, OP_NE, NULL);
5240 #ifdef _WIN32
5241 tt_int_op(mkdir(dir), OP_EQ, 0);
5242 #else
5243 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5244 #endif
5246 esc_dir = esc_for_log(dir);
5247 tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
5248 esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
5250 tor_asprintf(&torrc_contents,
5251 "%%include %s\n"
5252 "%%include %s%s \n" // space to avoid suppressing newline
5253 "%%include %s\n",
5254 esc_dir,
5255 dir, PATH_SEPARATOR,
5256 esc_dir_with_pathsep);
5258 int include_used;
5259 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5260 NULL), OP_EQ, 0);
5261 tt_ptr_op(result, OP_EQ, NULL);
5262 tt_int_op(include_used, OP_EQ, 1);
5264 done:
5265 config_free_lines(result);
5266 tor_free(dir);
5267 tor_free(torrc_contents);
5268 tor_free(esc_dir);
5269 tor_free(dir_with_pathsep);
5270 tor_free(esc_dir_with_pathsep);
5273 static void
5274 test_config_include_not_processed(void *data)
5276 (void)data;
5278 char torrc_contents[1000] = "%include does_not_exist\n";
5279 config_line_t *result = NULL;
5280 tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
5281 tt_ptr_op(result, OP_NE, NULL);
5283 int len = 0;
5284 config_line_t *next;
5285 for (next = result; next != NULL; next = next->next) {
5286 tt_str_op(next->key, OP_EQ, "%include");
5287 tt_str_op(next->value, OP_EQ, "does_not_exist");
5288 len++;
5290 tt_int_op(len, OP_EQ, 1);
5292 done:
5293 config_free_lines(result);
5296 static void
5297 test_config_include_has_include(void *data)
5299 (void)data;
5301 config_line_t *result = NULL;
5302 char *dir = tor_strdup(get_fname("test_include_has_include"));
5303 tt_ptr_op(dir, OP_NE, NULL);
5305 #ifdef _WIN32
5306 tt_int_op(mkdir(dir), OP_EQ, 0);
5307 #else
5308 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5309 #endif
5311 char torrc_contents[1000] = "Test 1\n";
5312 int include_used;
5314 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5315 NULL), OP_EQ, 0);
5316 tt_int_op(include_used, OP_EQ, 0);
5317 config_free_lines(result);
5319 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
5320 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5321 NULL), OP_EQ, 0);
5322 tt_int_op(include_used, OP_EQ, 1);
5324 done:
5325 config_free_lines(result);
5326 tor_free(dir);
5329 static void
5330 test_config_include_flag_both_without(void *data)
5332 (void)data;
5334 char *errmsg = NULL;
5335 char conf_empty[1000];
5336 tor_snprintf(conf_empty, sizeof(conf_empty),
5337 "DataDirectory %s\n",
5338 get_fname(NULL));
5339 // test with defaults-torrc and torrc without include
5340 int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
5341 NULL, &errmsg);
5342 tt_int_op(ret, OP_EQ, 0);
5344 const or_options_t *options = get_options();
5345 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5347 done:
5348 tor_free(errmsg);
5351 static void
5352 test_config_include_flag_torrc_only(void *data)
5354 (void)data;
5356 char *errmsg = NULL;
5357 char *path = NULL;
5358 char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
5359 tt_ptr_op(dir, OP_NE, NULL);
5361 #ifdef _WIN32
5362 tt_int_op(mkdir(dir), OP_EQ, 0);
5363 #else
5364 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5365 #endif
5367 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5368 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5370 char conf_empty[1000];
5371 tor_snprintf(conf_empty, sizeof(conf_empty),
5372 "DataDirectory %s\n",
5373 get_fname(NULL));
5374 char conf_include[1000];
5375 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5377 // test with defaults-torrc without include and torrc with include
5378 int ret = options_init_from_string(conf_empty, conf_include,
5379 CMD_RUN_UNITTESTS, NULL, &errmsg);
5380 tt_int_op(ret, OP_EQ, 0);
5382 const or_options_t *options = get_options();
5383 tt_int_op(options->IncludeUsed, OP_EQ, 1);
5385 done:
5386 tor_free(errmsg);
5387 tor_free(path);
5388 tor_free(dir);
5391 static void
5392 test_config_include_flag_defaults_only(void *data)
5394 (void)data;
5396 char *errmsg = NULL;
5397 char *path = NULL;
5398 char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
5399 tt_ptr_op(dir, OP_NE, NULL);
5401 #ifdef _WIN32
5402 tt_int_op(mkdir(dir), OP_EQ, 0);
5403 #else
5404 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5405 #endif
5407 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5408 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5410 char conf_empty[1000];
5411 tor_snprintf(conf_empty, sizeof(conf_empty),
5412 "DataDirectory %s\n",
5413 get_fname(NULL));
5414 char conf_include[1000];
5415 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5417 // test with defaults-torrc with include and torrc without include
5418 int ret = options_init_from_string(conf_include, conf_empty,
5419 CMD_RUN_UNITTESTS, NULL, &errmsg);
5420 tt_int_op(ret, OP_EQ, 0);
5422 const or_options_t *options = get_options();
5423 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5425 done:
5426 tor_free(errmsg);
5427 tor_free(path);
5428 tor_free(dir);
5431 static void
5432 test_config_dup_and_filter(void *arg)
5434 (void)arg;
5435 /* Test normal input. */
5436 config_line_t *line = NULL;
5437 config_line_append(&line, "abc", "def");
5438 config_line_append(&line, "ghi", "jkl");
5439 config_line_append(&line, "ABCD", "mno");
5441 config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
5442 tt_ptr_op(line_dup, OP_NE, NULL);
5443 tt_ptr_op(line_dup->next, OP_NE, NULL);
5444 tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
5446 tt_str_op(line_dup->key, OP_EQ, "abc");
5447 tt_str_op(line_dup->value, OP_EQ, "def");
5448 tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
5449 tt_str_op(line_dup->next->value, OP_EQ, "mno");
5451 /* empty output */
5452 config_free_lines(line_dup);
5453 line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
5454 tt_ptr_op(line_dup, OP_EQ, NULL);
5456 /* empty input */
5457 config_free_lines(line_dup);
5458 line_dup = config_lines_dup_and_filter(NULL, "abc");
5459 tt_ptr_op(line_dup, OP_EQ, NULL);
5461 done:
5462 config_free_lines(line);
5463 config_free_lines(line_dup);
5466 /* If we're not configured to be a bridge, but we set
5467 * BridgeDistribution, then options_validate () should return -1. */
5468 static void
5469 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
5471 or_options_t* options = get_options_mutable();
5472 or_options_t* old_options = options;
5473 or_options_t* default_options = options;
5474 char* message = NULL;
5475 int ret;
5477 (void)arg;
5479 options->BridgeRelay = 0;
5480 options->BridgeDistribution = (char*)("https");
5482 ret = options_validate(old_options, options, default_options, 0, &message);
5484 tt_int_op(ret, OP_EQ, -1);
5485 tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
5486 "didn't set BridgeRelay!");
5487 done:
5488 tor_free(message);
5489 options->BridgeDistribution = NULL;
5492 /* If the BridgeDistribution setting was valid, 0 should be returned. */
5493 static void
5494 test_config_check_bridge_distribution_setting_valid(void *arg)
5496 int ret = check_bridge_distribution_setting("https");
5498 (void)arg;
5500 tt_int_op(ret, OP_EQ, 0);
5501 done:
5502 return;
5505 /* If the BridgeDistribution setting was invalid, -1 should be returned. */
5506 static void
5507 test_config_check_bridge_distribution_setting_invalid(void *arg)
5509 int ret = check_bridge_distribution_setting("hyphens-are-allowed");
5511 (void)arg;
5513 tt_int_op(ret, OP_EQ, 0);
5515 ret = check_bridge_distribution_setting("asterisks*are*forbidden");
5517 tt_int_op(ret, OP_EQ, -1);
5518 done:
5519 return;
5522 /* If the BridgeDistribution setting was unrecognised, a warning should be
5523 * logged and 0 should be returned. */
5524 static void
5525 test_config_check_bridge_distribution_setting_unrecognised(void *arg)
5527 int ret = check_bridge_distribution_setting("unicorn");
5529 (void)arg;
5531 tt_int_op(ret, OP_EQ, 0);
5532 done:
5533 return;
5536 static void
5537 test_config_include_opened_file_list(void *data)
5539 (void)data;
5541 config_line_t *result = NULL;
5542 smartlist_t *opened_files = smartlist_new();
5543 char *torrcd = NULL;
5544 char *subfolder = NULL;
5545 char *path = NULL;
5546 char *empty = NULL;
5547 char *file = NULL;
5548 char *dot = NULL;
5549 char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
5550 tt_ptr_op(dir, OP_NE, NULL);
5552 #ifdef _WIN32
5553 tt_int_op(mkdir(dir), OP_EQ, 0);
5554 #else
5555 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5556 #endif
5558 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5560 #ifdef _WIN32
5561 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5562 #else
5563 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5564 #endif
5566 tor_asprintf(&subfolder, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5568 #ifdef _WIN32
5569 tt_int_op(mkdir(subfolder), OP_EQ, 0);
5570 #else
5571 tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
5572 #endif
5574 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", subfolder,
5575 "01_file_in_subfolder");
5576 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5578 tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty");
5579 tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
5581 tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file");
5582 tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
5584 tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5585 tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
5587 char torrc_contents[1000];
5588 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5589 "%%include %s\n",
5590 torrcd);
5592 int include_used;
5593 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5594 opened_files), OP_EQ, 0);
5595 tt_ptr_op(result, OP_NE, NULL);
5596 tt_int_op(include_used, OP_EQ, 1);
5598 tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
5599 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
5600 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
5601 // files inside subfolders are not opended, only the subfolder is opened
5602 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
5603 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
5604 // dot files are not opened as we ignore them when we get their name from
5605 // their parent folder
5607 done:
5608 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
5609 smartlist_free(opened_files);
5610 config_free_lines(result);
5611 tor_free(torrcd);
5612 tor_free(subfolder);
5613 tor_free(path);
5614 tor_free(empty);
5615 tor_free(file);
5616 tor_free(dot);
5617 tor_free(dir);
5620 static void
5621 test_config_compute_max_mem_in_queues(void *data)
5623 #define GIGABYTE(x) (U64_LITERAL(x) << 30)
5624 #define MEGABYTE(x) (U64_LITERAL(x) << 20)
5625 (void)data;
5626 MOCK(get_total_system_memory, get_total_system_memory_mock);
5628 /* We are unable to detect the amount of memory on the system. Tor will try
5629 * to use some sensible default values for 64-bit and 32-bit systems. */
5630 total_system_memory_return = -1;
5632 #if SIZEOF_VOID_P >= 8
5633 /* We are on a 64-bit system. */
5634 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
5635 #else
5636 /* We are on a 32-bit system. */
5637 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
5638 #endif
5640 /* We are able to detect the amount of RAM on the system. */
5641 total_system_memory_return = 0;
5643 /* We are running on a system with one gigabyte of RAM. */
5644 total_system_memory_output = GIGABYTE(1);
5646 /* We have 0.75 * RAM available. */
5647 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5648 3 * (GIGABYTE(1) / 4));
5650 /* We are running on a tiny machine with 256 MB of RAM. */
5651 total_system_memory_output = MEGABYTE(256);
5653 /* We will now enforce a minimum of 256 MB of RAM available for the
5654 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
5655 * MB available. */
5656 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
5658 #if SIZEOF_SIZE_T > 4
5659 /* We are running on a machine with 8 GB of RAM. */
5660 total_system_memory_output = GIGABYTE(8);
5662 /* We will have 0.4 * RAM available. */
5663 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5664 2 * (GIGABYTE(8) / 5));
5666 /* We are running on a machine with 16 GB of RAM. */
5667 total_system_memory_output = GIGABYTE(16);
5669 /* We will have 0.4 * RAM available. */
5670 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5671 2 * (GIGABYTE(16) / 5));
5673 /* We are running on a machine with 32 GB of RAM. */
5674 total_system_memory_output = GIGABYTE(32);
5676 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
5677 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5678 MAX_DEFAULT_MEMORY_QUEUE_SIZE);
5679 #endif
5681 done:
5682 UNMOCK(get_total_system_memory);
5684 #undef GIGABYTE
5685 #undef MEGABYTE
5688 #define CONFIG_TEST(name, flags) \
5689 { #name, test_config_ ## name, flags, NULL, NULL }
5691 struct testcase_t config_tests[] = {
5692 CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
5693 CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
5694 CONFIG_TEST(parsing_trusted_dir_server, 0),
5695 CONFIG_TEST(parsing_fallback_dir_server, 0),
5696 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
5697 CONFIG_TEST(adding_dir_servers, TT_FORK),
5698 CONFIG_TEST(default_dir_servers, TT_FORK),
5699 CONFIG_TEST(default_fallback_dirs, 0),
5700 CONFIG_TEST(resolve_my_address, TT_FORK),
5701 CONFIG_TEST(addressmap, 0),
5702 CONFIG_TEST(parse_bridge_line, 0),
5703 CONFIG_TEST(parse_transport_options_line, 0),
5704 CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
5705 CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
5706 CONFIG_TEST(write_to_data_subdir, TT_FORK),
5707 CONFIG_TEST(fix_my_family, 0),
5708 CONFIG_TEST(directory_fetch, 0),
5709 CONFIG_TEST(port_cfg_line_extract_addrport, 0),
5710 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
5711 CONFIG_TEST(parse_port_config__ports__server_options, 0),
5712 CONFIG_TEST(parse_port_config__ports__ports_given, 0),
5713 CONFIG_TEST(parse_log_severity, 0),
5714 CONFIG_TEST(include_limit, 0),
5715 CONFIG_TEST(include_does_not_exist, 0),
5716 CONFIG_TEST(include_error_in_included_file, 0),
5717 CONFIG_TEST(include_empty_file_folder, 0),
5718 #ifndef _WIN32
5719 CONFIG_TEST(include_no_permission, 0),
5720 #endif
5721 CONFIG_TEST(include_recursion_before_after, 0),
5722 CONFIG_TEST(include_recursion_after_only, 0),
5723 CONFIG_TEST(include_folder_order, 0),
5724 CONFIG_TEST(include_path_syntax, 0),
5725 CONFIG_TEST(include_not_processed, 0),
5726 CONFIG_TEST(include_has_include, 0),
5727 CONFIG_TEST(include_flag_both_without, TT_FORK),
5728 CONFIG_TEST(include_flag_torrc_only, TT_FORK),
5729 CONFIG_TEST(include_flag_defaults_only, TT_FORK),
5730 CONFIG_TEST(dup_and_filter, 0),
5731 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
5732 CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
5733 CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
5734 CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
5735 CONFIG_TEST(include_opened_file_list, 0),
5736 CONFIG_TEST(compute_max_mem_in_queues, 0),
5737 END_OF_TESTCASES