dirvote: Handling adding vote and signature if module is disabled
[tor.git] / src / test / test_config.c
blob461aa646d637d92b3ded08a9045a4ffd46fd7add
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 "connection_or.h"
24 #include "control.h"
25 #include "cpuworker.h"
26 #include "dirserv.h"
27 #include "dirauth/dirvote.h"
28 #include "dns.h"
29 #include "entrynodes.h"
30 #include "transports.h"
31 #include "ext_orport.h"
32 #include "geoip.h"
33 #include "hibernate.h"
34 #include "main.h"
35 #include "networkstatus.h"
36 #include "nodelist.h"
37 #include "policies.h"
38 #include "rendclient.h"
39 #include "rendservice.h"
40 #include "router.h"
41 #include "routerlist.h"
42 #include "routerset.h"
43 #include "statefile.h"
45 #include "test_helpers.h"
47 static void
48 test_config_addressmap(void *arg)
50 char buf[1024];
51 char address[256];
52 time_t expires = TIME_MAX;
53 (void)arg;
55 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
56 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
57 "MapAddress *.google.com *.torserver.exit\n"
58 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
59 "MapAddress *.cn.com www.cnn.com\n"
60 "MapAddress *.cnn.com www.cnn.com\n"
61 "MapAddress ex.com www.cnn.com\n"
62 "MapAddress ey.com *.cnn.com\n"
63 "MapAddress www.torproject.org 1.1.1.1\n"
64 "MapAddress other.torproject.org "
65 "this.torproject.org.otherserver.exit\n"
66 "MapAddress test.torproject.org 2.2.2.2\n"
67 "MapAddress www.google.com 3.3.3.3\n"
68 "MapAddress www.example.org 4.4.4.4\n"
69 "MapAddress 4.4.4.4 7.7.7.7\n"
70 "MapAddress 4.4.4.4 5.5.5.5\n"
71 "MapAddress www.infiniteloop.org 6.6.6.6\n"
72 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
73 , sizeof(buf));
75 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
76 config_register_addressmaps(get_options());
78 /* Use old interface for now, so we don't need to rewrite the unit tests */
79 #define addressmap_rewrite(a,s,eo,ao) \
80 addressmap_rewrite((a),(s), ~0, (eo),(ao))
82 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
83 strlcpy(address, "www.invalidwildcard.com", sizeof(address));
84 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
86 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
87 strlcpy(address, "www.invalidasterisk.com", sizeof(address));
88 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
90 /* Where no mapping for FQDN match on top-level domain */
91 /* MapAddress .google.com .torserver.exit */
92 strlcpy(address, "reader.google.com", sizeof(address));
93 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
94 tt_str_op(address,OP_EQ, "reader.torserver.exit");
96 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
97 strlcpy(address, "reader.yahoo.com", sizeof(address));
98 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
99 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
101 /*MapAddress *.cnn.com www.cnn.com */
102 strlcpy(address, "cnn.com", sizeof(address));
103 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
104 tt_str_op(address,OP_EQ, "www.cnn.com");
106 /* MapAddress .cn.com www.cnn.com */
107 strlcpy(address, "www.cn.com", sizeof(address));
108 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
109 tt_str_op(address,OP_EQ, "www.cnn.com");
111 /* MapAddress ex.com www.cnn.com - no match */
112 strlcpy(address, "www.ex.com", sizeof(address));
113 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
115 /* MapAddress ey.com *.cnn.com - invalid expression */
116 strlcpy(address, "ey.com", sizeof(address));
117 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
119 /* Where mapping for FQDN match on FQDN */
120 strlcpy(address, "www.google.com", sizeof(address));
121 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
122 tt_str_op(address,OP_EQ, "3.3.3.3");
124 strlcpy(address, "www.torproject.org", sizeof(address));
125 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
126 tt_str_op(address,OP_EQ, "1.1.1.1");
128 strlcpy(address, "other.torproject.org", sizeof(address));
129 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
130 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
132 strlcpy(address, "test.torproject.org", sizeof(address));
133 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
134 tt_str_op(address,OP_EQ, "2.2.2.2");
136 /* Test a chain of address mappings and the order in which they were added:
137 "MapAddress www.example.org 4.4.4.4"
138 "MapAddress 4.4.4.4 7.7.7.7"
139 "MapAddress 4.4.4.4 5.5.5.5"
141 strlcpy(address, "www.example.org", sizeof(address));
142 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
143 tt_str_op(address,OP_EQ, "5.5.5.5");
145 /* Test infinite address mapping results in no change */
146 strlcpy(address, "www.infiniteloop.org", sizeof(address));
147 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
148 tt_str_op(address,OP_EQ, "www.infiniteloop.org");
150 /* Test we don't find false positives */
151 strlcpy(address, "www.example.com", sizeof(address));
152 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
154 /* Test top-level-domain matching a bit harder */
155 config_free_lines(get_options_mutable()->AddressMap);
156 addressmap_clear_configured();
157 strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
158 "MapAddress *.torproject.org 1.1.1.1\n"
159 "MapAddress *.net 2.2.2.2\n"
160 , sizeof(buf));
161 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
162 config_register_addressmaps(get_options());
164 strlcpy(address, "www.abc.com", sizeof(address));
165 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
166 tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
168 strlcpy(address, "www.def.com", sizeof(address));
169 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
170 tt_str_op(address,OP_EQ, "www.def.torserver.exit");
172 strlcpy(address, "www.torproject.org", sizeof(address));
173 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
174 tt_str_op(address,OP_EQ, "1.1.1.1");
176 strlcpy(address, "test.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, "torproject.net", sizeof(address));
181 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
182 tt_str_op(address,OP_EQ, "2.2.2.2");
184 /* We don't support '*' as a mapping directive */
185 config_free_lines(get_options_mutable()->AddressMap);
186 addressmap_clear_configured();
187 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
188 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
189 config_register_addressmaps(get_options());
191 strlcpy(address, "www.abc.com", sizeof(address));
192 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
194 strlcpy(address, "www.def.net", sizeof(address));
195 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
197 strlcpy(address, "www.torproject.org", sizeof(address));
198 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
200 #undef addressmap_rewrite
202 done:
203 config_free_lines(get_options_mutable()->AddressMap);
204 get_options_mutable()->AddressMap = NULL;
205 addressmap_free_all();
208 static int
209 is_private_dir(const char* path)
211 struct stat st;
212 int r = stat(path, &st);
213 if (r) {
214 return 0;
216 #if !defined (_WIN32)
217 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
218 return 0;
220 #endif
221 return 1;
224 static void
225 test_config_check_or_create_data_subdir(void *arg)
227 or_options_t *options = get_options_mutable();
228 char *datadir;
229 const char *subdir = "test_stats";
230 char *subpath;
231 struct stat st;
232 int r;
233 #if !defined (_WIN32)
234 unsigned group_permission;
235 #endif
236 (void)arg;
238 tor_free(options->DataDirectory);
239 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
240 subpath = get_datadir_fname(subdir);
242 #if defined (_WIN32)
243 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
244 #else
245 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
246 #endif
248 r = stat(subpath, &st);
250 // The subdirectory shouldn't exist yet,
251 // but should be created by the call to check_or_create_data_subdir.
252 tt_assert(r && (errno == ENOENT));
253 tt_assert(!check_or_create_data_subdir(subdir));
254 tt_assert(is_private_dir(subpath));
256 // The check should return 0, if the directory already exists
257 // and is private to the user.
258 tt_assert(!check_or_create_data_subdir(subdir));
260 r = stat(subpath, &st);
261 if (r) {
262 tt_abort_perror("stat");
265 #if !defined (_WIN32)
266 group_permission = st.st_mode | 0070;
267 r = chmod(subpath, group_permission);
269 if (r) {
270 tt_abort_perror("chmod");
273 // If the directory exists, but its mode is too permissive
274 // a call to check_or_create_data_subdir should reset the mode.
275 tt_assert(!is_private_dir(subpath));
276 tt_assert(!check_or_create_data_subdir(subdir));
277 tt_assert(is_private_dir(subpath));
278 #endif /* !defined (_WIN32) */
280 done:
281 rmdir(subpath);
282 tor_free(datadir);
283 tor_free(subpath);
286 static void
287 test_config_write_to_data_subdir(void *arg)
289 or_options_t* options = get_options_mutable();
290 char *datadir;
291 char *cp = NULL;
292 const char* subdir = "test_stats";
293 const char* fname = "test_file";
294 const char* str =
295 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
296 "elitr, sed diam nonumy eirmod\n"
297 "tempor invidunt ut labore et dolore magna aliquyam\n"
298 "erat, sed diam voluptua.\n"
299 "At vero eos et accusam et justo duo dolores et ea\n"
300 "rebum. Stet clita kasd gubergren,\n"
301 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
302 "Lorem ipsum dolor sit amet,\n"
303 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
304 "tempor invidunt ut labore et dolore\n"
305 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
306 "accusam et justo duo dolores et\n"
307 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
308 "sanctus est Lorem ipsum dolor sit amet.";
309 char* filepath = NULL;
310 (void)arg;
312 tor_free(options->DataDirectory);
313 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
314 filepath = get_datadir_fname2(subdir, fname);
316 #if defined (_WIN32)
317 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
318 #else
319 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
320 #endif
322 // Write attempt should fail, if subdirectory doesn't exist.
323 tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
324 tt_assert(! check_or_create_data_subdir(subdir));
326 // Content of file after write attempt should be
327 // equal to the original string.
328 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
329 cp = read_file_to_str(filepath, 0, NULL);
330 tt_str_op(cp,OP_EQ, str);
331 tor_free(cp);
333 // A second write operation should overwrite the old content.
334 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
335 cp = read_file_to_str(filepath, 0, NULL);
336 tt_str_op(cp,OP_EQ, str);
337 tor_free(cp);
339 done:
340 (void) unlink(filepath);
341 rmdir(options->DataDirectory);
342 tor_free(datadir);
343 tor_free(filepath);
344 tor_free(cp);
347 /* Test helper function: Make sure that a bridge line gets parsed
348 * properly. Also make sure that the resulting bridge_line_t structure
349 * has its fields set correctly. */
350 static void
351 good_bridge_line_test(const char *string, const char *test_addrport,
352 const char *test_digest, const char *test_transport,
353 const smartlist_t *test_socks_args)
355 char *tmp = NULL;
356 bridge_line_t *bridge_line = parse_bridge_line(string);
357 tt_assert(bridge_line);
359 /* test addrport */
360 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
361 tt_str_op(test_addrport,OP_EQ, tmp);
362 tor_free(tmp);
364 /* If we were asked to validate a digest, but we did not get a
365 digest after parsing, we failed. */
366 if (test_digest && tor_digest_is_zero(bridge_line->digest))
367 tt_abort();
369 /* If we were not asked to validate a digest, and we got a digest
370 after parsing, we failed again. */
371 if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
372 tt_abort();
374 /* If we were asked to validate a digest, and we got a digest after
375 parsing, make sure it's correct. */
376 if (test_digest) {
377 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
378 tor_strlower(tmp);
379 tt_str_op(test_digest,OP_EQ, tmp);
380 tor_free(tmp);
383 /* If we were asked to validate a transport name, make sure tha it
384 matches with the transport name that was parsed. */
385 if (test_transport && !bridge_line->transport_name)
386 tt_abort();
387 if (!test_transport && bridge_line->transport_name)
388 tt_abort();
389 if (test_transport)
390 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
392 /* Validate the SOCKS argument smartlist. */
393 if (test_socks_args && !bridge_line->socks_args)
394 tt_abort();
395 if (!test_socks_args && bridge_line->socks_args)
396 tt_abort();
397 if (test_socks_args)
398 tt_assert(smartlist_strings_eq(test_socks_args,
399 bridge_line->socks_args));
401 done:
402 tor_free(tmp);
403 bridge_line_free(bridge_line);
406 /* Test helper function: Make sure that a bridge line is
407 * unparseable. */
408 static void
409 bad_bridge_line_test(const char *string)
411 bridge_line_t *bridge_line = parse_bridge_line(string);
412 if (bridge_line)
413 TT_FAIL(("%s was supposed to fail, but it didn't.", string));
414 tt_ptr_op(bridge_line, OP_EQ, NULL);
416 done:
417 bridge_line_free(bridge_line);
420 static void
421 test_config_parse_bridge_line(void *arg)
423 (void) arg;
424 good_bridge_line_test("192.0.2.1:4123",
425 "192.0.2.1:4123", NULL, NULL, NULL);
427 good_bridge_line_test("192.0.2.1",
428 "192.0.2.1:443", NULL, NULL, NULL);
430 good_bridge_line_test("transport [::1]",
431 "[::1]:443", NULL, "transport", NULL);
433 good_bridge_line_test("transport 192.0.2.1:12 "
434 "4352e58420e68f5e40bf7c74faddccd9d1349413",
435 "192.0.2.1:12",
436 "4352e58420e68f5e40bf7c74faddccd9d1349413",
437 "transport", NULL);
440 smartlist_t *sl_tmp = smartlist_new();
441 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
443 good_bridge_line_test("transport 192.0.2.1:12 "
444 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
445 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
446 "transport", sl_tmp);
448 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
449 smartlist_free(sl_tmp);
453 smartlist_t *sl_tmp = smartlist_new();
454 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
455 smartlist_add_asprintf(sl_tmp, "z=z");
457 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
458 "192.0.2.1:12", NULL, "transport", sl_tmp);
460 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
461 smartlist_free(sl_tmp);
465 smartlist_t *sl_tmp = smartlist_new();
466 smartlist_add_asprintf(sl_tmp, "dub=come");
467 smartlist_add_asprintf(sl_tmp, "save=me");
469 good_bridge_line_test("transport 192.0.2.1:12 "
470 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
471 "dub=come save=me",
473 "192.0.2.1:12",
474 "4352e58420e68f5e40bf7c74faddccd9d1349666",
475 "transport", sl_tmp);
477 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
478 smartlist_free(sl_tmp);
481 good_bridge_line_test("192.0.2.1:1231 "
482 "4352e58420e68f5e40bf7c74faddccd9d1349413",
483 "192.0.2.1:1231",
484 "4352e58420e68f5e40bf7c74faddccd9d1349413",
485 NULL, NULL);
487 /* Empty line */
488 bad_bridge_line_test("");
489 /* bad transport name */
490 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
491 /* weird ip address */
492 bad_bridge_line_test("a.b.c.d");
493 /* invalid fpr */
494 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
495 /* no k=v in the end */
496 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
497 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
498 /* no addrport */
499 bad_bridge_line_test("asdw");
500 /* huge k=v value that can't fit in SOCKS fields */
501 bad_bridge_line_test(
502 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
503 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
504 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
505 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
506 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
507 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
508 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
510 "aa=b");
513 static void
514 test_config_parse_transport_options_line(void *arg)
516 smartlist_t *options_sl = NULL, *sl_tmp = NULL;
518 (void) arg;
520 { /* too small line */
521 options_sl = get_options_from_transport_options_line("valley", NULL);
522 tt_ptr_op(options_sl, OP_EQ, NULL);
525 { /* no k=v values */
526 options_sl = get_options_from_transport_options_line("hit it!", NULL);
527 tt_ptr_op(options_sl, OP_EQ, NULL);
530 { /* correct line, but wrong transport specified */
531 options_sl =
532 get_options_from_transport_options_line("trebuchet k=v", "rook");
533 tt_ptr_op(options_sl, OP_EQ, NULL);
536 { /* correct -- no transport specified */
537 sl_tmp = smartlist_new();
538 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
539 smartlist_add_asprintf(sl_tmp, "weliketo=party");
541 options_sl =
542 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
543 NULL);
544 tt_assert(options_sl);
545 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
547 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
548 smartlist_free(sl_tmp);
549 sl_tmp = NULL;
550 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
551 smartlist_free(options_sl);
552 options_sl = NULL;
555 { /* correct -- correct transport specified */
556 sl_tmp = smartlist_new();
557 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
558 smartlist_add_asprintf(sl_tmp, "weliketo=party");
560 options_sl =
561 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
562 "rook");
563 tt_assert(options_sl);
564 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
565 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
566 smartlist_free(sl_tmp);
567 sl_tmp = NULL;
568 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
569 smartlist_free(options_sl);
570 options_sl = NULL;
573 done:
574 if (options_sl) {
575 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
576 smartlist_free(options_sl);
578 if (sl_tmp) {
579 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
580 smartlist_free(sl_tmp);
584 /* Mocks needed for the compute_max_mem_in_queues test */
585 static int get_total_system_memory_mock(size_t *mem_out);
587 static size_t total_system_memory_output = 0;
588 static int total_system_memory_return = 0;
590 static int
591 get_total_system_memory_mock(size_t *mem_out)
593 if (! mem_out)
594 return -1;
596 *mem_out = total_system_memory_output;
597 return total_system_memory_return;
600 /* Mocks needed for the transport plugin line test */
602 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
603 char **proxy_argv, int is_server);
604 static int transport_add_from_config_mock(const tor_addr_t *addr,
605 uint16_t port, const char *name,
606 int socks_ver);
607 static int transport_is_needed_mock(const char *transport_name);
609 static int pt_kickstart_proxy_mock_call_count = 0;
610 static int transport_add_from_config_mock_call_count = 0;
611 static int transport_is_needed_mock_call_count = 0;
612 static int transport_is_needed_mock_return = 0;
614 static void
615 pt_kickstart_proxy_mock(const smartlist_t *transport_list,
616 char **proxy_argv, int is_server)
618 (void) transport_list;
619 (void) proxy_argv;
620 (void) is_server;
621 /* XXXX check that args are as expected. */
623 ++pt_kickstart_proxy_mock_call_count;
625 free_execve_args(proxy_argv);
628 static int
629 transport_add_from_config_mock(const tor_addr_t *addr,
630 uint16_t port, const char *name,
631 int socks_ver)
633 (void) addr;
634 (void) port;
635 (void) name;
636 (void) socks_ver;
637 /* XXXX check that args are as expected. */
639 ++transport_add_from_config_mock_call_count;
641 return 0;
644 static int
645 transport_is_needed_mock(const char *transport_name)
647 (void) transport_name;
648 /* XXXX check that arg is as expected. */
650 ++transport_is_needed_mock_call_count;
652 return transport_is_needed_mock_return;
656 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
657 * options.
660 static void
661 test_config_parse_transport_plugin_line(void *arg)
663 (void)arg;
665 or_options_t *options = get_options_mutable();
666 int r, tmp;
667 int old_pt_kickstart_proxy_mock_call_count;
668 int old_transport_add_from_config_mock_call_count;
669 int old_transport_is_needed_mock_call_count;
671 /* Bad transport lines - too short */
672 r = parse_transport_line(options, "bad", 1, 0);
673 tt_int_op(r, OP_LT, 0);
674 r = parse_transport_line(options, "bad", 1, 1);
675 tt_int_op(r, OP_LT, 0);
676 r = parse_transport_line(options, "bad bad", 1, 0);
677 tt_int_op(r, OP_LT, 0);
678 r = parse_transport_line(options, "bad bad", 1, 1);
679 tt_int_op(r, OP_LT, 0);
681 /* Test transport list parsing */
682 r = parse_transport_line(options,
683 "transport_1 exec /usr/bin/fake-transport", 1, 0);
684 tt_int_op(r, OP_EQ, 0);
685 r = parse_transport_line(options,
686 "transport_1 exec /usr/bin/fake-transport", 1, 1);
687 tt_int_op(r, OP_EQ, 0);
688 r = parse_transport_line(options,
689 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
690 tt_int_op(r, OP_EQ, 0);
691 r = parse_transport_line(options,
692 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
693 tt_int_op(r, OP_EQ, 0);
694 /* Bad transport identifiers */
695 r = parse_transport_line(options,
696 "transport_* exec /usr/bin/fake-transport", 1, 0);
697 tt_int_op(r, OP_LT, 0);
698 r = parse_transport_line(options,
699 "transport_* exec /usr/bin/fake-transport", 1, 1);
700 tt_int_op(r, OP_LT, 0);
702 /* Check SOCKS cases for client transport */
703 r = parse_transport_line(options,
704 "transport_1 socks4 1.2.3.4:567", 1, 0);
705 tt_int_op(r, OP_EQ, 0);
706 r = parse_transport_line(options,
707 "transport_1 socks5 1.2.3.4:567", 1, 0);
708 tt_int_op(r, OP_EQ, 0);
709 /* Proxy case for server transport */
710 r = parse_transport_line(options,
711 "transport_1 proxy 1.2.3.4:567", 1, 1);
712 tt_int_op(r, OP_EQ, 0);
713 /* Multiple-transport error exit */
714 r = parse_transport_line(options,
715 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
716 tt_int_op(r, OP_LT, 0);
717 r = parse_transport_line(options,
718 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
719 tt_int_op(r, OP_LT, 0);
720 /* No port error exit */
721 r = parse_transport_line(options,
722 "transport_1 socks5 1.2.3.4", 1, 0);
723 tt_int_op(r, OP_LT, 0);
724 r = parse_transport_line(options,
725 "transport_1 proxy 1.2.3.4", 1, 1);
726 tt_int_op(r, OP_LT, 0);
727 /* Unparsable address error exit */
728 r = parse_transport_line(options,
729 "transport_1 socks5 1.2.3:6x7", 1, 0);
730 tt_int_op(r, OP_LT, 0);
731 r = parse_transport_line(options,
732 "transport_1 proxy 1.2.3:6x7", 1, 1);
733 tt_int_op(r, OP_LT, 0);
735 /* "Strange {Client|Server}TransportPlugin field" error exit */
736 r = parse_transport_line(options,
737 "transport_1 foo bar", 1, 0);
738 tt_int_op(r, OP_LT, 0);
739 r = parse_transport_line(options,
740 "transport_1 foo bar", 1, 1);
741 tt_int_op(r, OP_LT, 0);
743 /* No sandbox mode error exit */
744 tmp = options->Sandbox;
745 options->Sandbox = 1;
746 r = parse_transport_line(options,
747 "transport_1 exec /usr/bin/fake-transport", 1, 0);
748 tt_int_op(r, OP_LT, 0);
749 r = parse_transport_line(options,
750 "transport_1 exec /usr/bin/fake-transport", 1, 1);
751 tt_int_op(r, OP_LT, 0);
752 options->Sandbox = tmp;
755 * These final test cases cover code paths that only activate without
756 * validate_only, so they need mocks in place.
758 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
759 old_pt_kickstart_proxy_mock_call_count =
760 pt_kickstart_proxy_mock_call_count;
761 r = parse_transport_line(options,
762 "transport_1 exec /usr/bin/fake-transport", 0, 1);
763 tt_int_op(r, OP_EQ, 0);
764 tt_assert(pt_kickstart_proxy_mock_call_count ==
765 old_pt_kickstart_proxy_mock_call_count + 1);
766 UNMOCK(pt_kickstart_proxy);
768 /* This one hits a log line in the !validate_only case only */
769 r = parse_transport_line(options,
770 "transport_1 proxy 1.2.3.4:567", 0, 1);
771 tt_int_op(r, OP_EQ, 0);
773 /* Check mocked client transport cases */
774 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
775 MOCK(transport_add_from_config, transport_add_from_config_mock);
776 MOCK(transport_is_needed, transport_is_needed_mock);
778 /* Unnecessary transport case */
779 transport_is_needed_mock_return = 0;
780 old_pt_kickstart_proxy_mock_call_count =
781 pt_kickstart_proxy_mock_call_count;
782 old_transport_add_from_config_mock_call_count =
783 transport_add_from_config_mock_call_count;
784 old_transport_is_needed_mock_call_count =
785 transport_is_needed_mock_call_count;
786 r = parse_transport_line(options,
787 "transport_1 exec /usr/bin/fake-transport", 0, 0);
788 /* Should have succeeded */
789 tt_int_op(r, OP_EQ, 0);
790 /* transport_is_needed() should have been called */
791 tt_assert(transport_is_needed_mock_call_count ==
792 old_transport_is_needed_mock_call_count + 1);
794 * pt_kickstart_proxy() and transport_add_from_config() should
795 * not have been called.
797 tt_assert(pt_kickstart_proxy_mock_call_count ==
798 old_pt_kickstart_proxy_mock_call_count);
799 tt_assert(transport_add_from_config_mock_call_count ==
800 old_transport_add_from_config_mock_call_count);
802 /* Necessary transport case */
803 transport_is_needed_mock_return = 1;
804 old_pt_kickstart_proxy_mock_call_count =
805 pt_kickstart_proxy_mock_call_count;
806 old_transport_add_from_config_mock_call_count =
807 transport_add_from_config_mock_call_count;
808 old_transport_is_needed_mock_call_count =
809 transport_is_needed_mock_call_count;
810 r = parse_transport_line(options,
811 "transport_1 exec /usr/bin/fake-transport", 0, 0);
812 /* Should have succeeded */
813 tt_int_op(r, OP_EQ, 0);
815 * transport_is_needed() and pt_kickstart_proxy() should have been
816 * called.
818 tt_assert(pt_kickstart_proxy_mock_call_count ==
819 old_pt_kickstart_proxy_mock_call_count + 1);
820 tt_assert(transport_is_needed_mock_call_count ==
821 old_transport_is_needed_mock_call_count + 1);
822 /* transport_add_from_config() should not have been called. */
823 tt_assert(transport_add_from_config_mock_call_count ==
824 old_transport_add_from_config_mock_call_count);
826 /* proxy case */
827 transport_is_needed_mock_return = 1;
828 old_pt_kickstart_proxy_mock_call_count =
829 pt_kickstart_proxy_mock_call_count;
830 old_transport_add_from_config_mock_call_count =
831 transport_add_from_config_mock_call_count;
832 old_transport_is_needed_mock_call_count =
833 transport_is_needed_mock_call_count;
834 r = parse_transport_line(options,
835 "transport_1 socks5 1.2.3.4:567", 0, 0);
836 /* Should have succeeded */
837 tt_int_op(r, OP_EQ, 0);
839 * transport_is_needed() and transport_add_from_config() should have
840 * been called.
842 tt_assert(transport_add_from_config_mock_call_count ==
843 old_transport_add_from_config_mock_call_count + 1);
844 tt_assert(transport_is_needed_mock_call_count ==
845 old_transport_is_needed_mock_call_count + 1);
846 /* pt_kickstart_proxy() should not have been called. */
847 tt_assert(pt_kickstart_proxy_mock_call_count ==
848 old_pt_kickstart_proxy_mock_call_count);
850 /* Done with mocked client transport cases */
851 UNMOCK(transport_is_needed);
852 UNMOCK(transport_add_from_config);
853 UNMOCK(pt_kickstart_proxy);
855 done:
856 /* Make sure we undo all mocks */
857 UNMOCK(pt_kickstart_proxy);
858 UNMOCK(transport_add_from_config);
859 UNMOCK(transport_is_needed);
861 return;
864 // Tests if an options with MyFamily fingerprints missing '$' normalises
865 // them correctly and also ensure it also works with multiple fingerprints
866 static void
867 test_config_fix_my_family(void *arg)
869 char *err = NULL;
870 config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
871 family->key = tor_strdup("MyFamily");
872 family->value = tor_strdup("$1111111111111111111111111111111111111111, "
873 "1111111111111111111111111111111111111112, "
874 "$1111111111111111111111111111111111111113");
876 config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
877 family2->key = tor_strdup("MyFamily");
878 family2->value = tor_strdup("1111111111111111111111111111111111111114");
880 config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
881 family3->key = tor_strdup("MyFamily");
882 family3->value = tor_strdup("$1111111111111111111111111111111111111115");
884 family->next = family2;
885 family2->next = family3;
886 family3->next = NULL;
888 or_options_t* options = options_new();
889 or_options_t* defaults = options_new();
890 (void) arg;
892 options_init(options);
893 options_init(defaults);
894 options->MyFamily_lines = family;
896 options_validate(NULL, options, defaults, 0, &err) ;
898 if (err != NULL) {
899 TT_FAIL(("options_validate failed: %s", err));
902 const char *valid[] = { "$1111111111111111111111111111111111111111",
903 "$1111111111111111111111111111111111111112",
904 "$1111111111111111111111111111111111111113",
905 "$1111111111111111111111111111111111111114",
906 "$1111111111111111111111111111111111111115" };
907 int ret_size = 0;
908 config_line_t *ret;
909 for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
910 tt_str_op(ret->value, OP_EQ, valid[ret_size]);
911 ret_size++;
913 tt_int_op(ret_size, OP_EQ, 5);
915 done:
916 tor_free(err);
917 or_options_free(options);
918 or_options_free(defaults);
921 static int n_hostname_01010101 = 0;
923 /** This mock function is meant to replace tor_lookup_hostname().
924 * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
925 * This function increments <b>n_hostname_01010101</b> counter by one
926 * every time it is called.
928 static int
929 tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
931 n_hostname_01010101++;
933 if (name && addr) {
934 *addr = ntohl(0x01010101);
937 return 0;
940 static int n_hostname_localhost = 0;
942 /** This mock function is meant to replace tor_lookup_hostname().
943 * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
944 * This function increments <b>n_hostname_localhost</b> counter by one
945 * every time it is called.
947 static int
948 tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
950 n_hostname_localhost++;
952 if (name && addr) {
953 *addr = 0x7f000001;
956 return 0;
959 static int n_hostname_failure = 0;
961 /** This mock function is meant to replace tor_lookup_hostname().
962 * It pretends to fail by returning -1 to caller. Also, this function
963 * increments <b>n_hostname_failure</b> every time it is called.
965 static int
966 tor_lookup_hostname_failure(const char *name, uint32_t *addr)
968 (void)name;
969 (void)addr;
971 n_hostname_failure++;
973 return -1;
976 static int n_gethostname_replacement = 0;
978 /** This mock function is meant to replace tor_gethostname(). It
979 * responds with string "onionrouter!" as hostname. This function
980 * increments <b>n_gethostname_replacement</b> by one every time
981 * it is called.
983 static int
984 tor_gethostname_replacement(char *name, size_t namelen)
986 n_gethostname_replacement++;
988 if (name && namelen) {
989 strlcpy(name,"onionrouter!",namelen);
992 return 0;
995 static int n_gethostname_localhost = 0;
997 /** This mock function is meant to replace tor_gethostname(). It
998 * responds with string "127.0.0.1" as hostname. This function
999 * increments <b>n_gethostname_localhost</b> by one every time
1000 * it is called.
1002 static int
1003 tor_gethostname_localhost(char *name, size_t namelen)
1005 n_gethostname_localhost++;
1007 if (name && namelen) {
1008 strlcpy(name,"127.0.0.1",namelen);
1011 return 0;
1014 static int n_gethostname_failure = 0;
1016 /** This mock function is meant to replace tor_gethostname.
1017 * It pretends to fail by returning -1. This function increments
1018 * <b>n_gethostname_failure</b> by one every time it is called.
1020 static int
1021 tor_gethostname_failure(char *name, size_t namelen)
1023 (void)name;
1024 (void)namelen;
1025 n_gethostname_failure++;
1027 return -1;
1030 static int n_get_interface_address = 0;
1032 /** This mock function is meant to replace get_interface_address().
1033 * It answers with address 8.8.8.8. This function increments
1034 * <b>n_get_interface_address</b> by one every time it is called.
1036 static int
1037 get_interface_address_08080808(int severity, uint32_t *addr)
1039 (void)severity;
1041 n_get_interface_address++;
1043 if (addr) {
1044 *addr = ntohl(0x08080808);
1047 return 0;
1050 static int n_get_interface_address6 = 0;
1051 static sa_family_t last_address6_family;
1053 /** This mock function is meant to replace get_interface_address6().
1054 * It answers with IP address 9.9.9.9 iff both of the following are true:
1055 * - <b>family</b> is AF_INET
1056 * - <b>addr</b> pointer is not NULL.
1057 * This function increments <b>n_get_interface_address6</b> by one every
1058 * time it is called.
1060 static int
1061 get_interface_address6_replacement(int severity, sa_family_t family,
1062 tor_addr_t *addr)
1064 (void)severity;
1066 last_address6_family = family;
1067 n_get_interface_address6++;
1069 if ((family != AF_INET) || !addr) {
1070 return -1;
1073 tor_addr_from_ipv4h(addr,0x09090909);
1075 return 0;
1078 static int n_get_interface_address_failure = 0;
1081 * This mock function is meant to replace get_interface_address().
1082 * It pretends to fail getting interface address by returning -1.
1083 * <b>n_get_interface_address_failure</b> is incremented by one
1084 * every time this function is called.
1086 static int
1087 get_interface_address_failure(int severity, uint32_t *addr)
1089 (void)severity;
1090 (void)addr;
1092 n_get_interface_address_failure++;
1094 return -1;
1097 static int n_get_interface_address6_failure = 0;
1100 * This mock function is meant to replace get_interface_addres6().
1101 * It will pretend to fail by return -1.
1102 * <b>n_get_interface_address6_failure</b> is incremented by one
1103 * every time this function is called and <b>last_address6_family</b>
1104 * is assigned the value of <b>family</b> argument.
1106 static int
1107 get_interface_address6_failure(int severity, sa_family_t family,
1108 tor_addr_t *addr)
1110 (void)severity;
1111 (void)addr;
1112 n_get_interface_address6_failure++;
1113 last_address6_family = family;
1115 return -1;
1118 static void
1119 test_config_resolve_my_address(void *arg)
1121 or_options_t *options;
1122 uint32_t resolved_addr;
1123 const char *method_used;
1124 char *hostname_out = NULL;
1125 int retval;
1126 int prev_n_hostname_01010101;
1127 int prev_n_hostname_localhost;
1128 int prev_n_hostname_failure;
1129 int prev_n_gethostname_replacement;
1130 int prev_n_gethostname_failure;
1131 int prev_n_gethostname_localhost;
1132 int prev_n_get_interface_address;
1133 int prev_n_get_interface_address_failure;
1134 int prev_n_get_interface_address6;
1135 int prev_n_get_interface_address6_failure;
1137 (void)arg;
1139 options = options_new();
1141 options_init(options);
1144 * CASE 1:
1145 * If options->Address is a valid IPv4 address string, we want
1146 * the corresponding address to be parsed and returned.
1149 options->Address = tor_strdup("128.52.128.105");
1151 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1152 &method_used,&hostname_out);
1154 tt_want(retval == 0);
1155 tt_want_str_op(method_used,OP_EQ,"CONFIGURED");
1156 tt_want(hostname_out == NULL);
1157 tt_assert(resolved_addr == 0x80348069);
1159 tor_free(options->Address);
1162 * CASE 2:
1163 * If options->Address is a valid DNS address, we want resolve_my_address()
1164 * function to ask tor_lookup_hostname() for help with resolving it
1165 * and return the address that was resolved (in host order).
1168 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1170 tor_free(options->Address);
1171 options->Address = tor_strdup("www.torproject.org");
1173 prev_n_hostname_01010101 = n_hostname_01010101;
1175 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1176 &method_used,&hostname_out);
1178 tt_want(retval == 0);
1179 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1180 tt_want_str_op(method_used,OP_EQ,"RESOLVED");
1181 tt_want_str_op(hostname_out,OP_EQ,"www.torproject.org");
1182 tt_assert(resolved_addr == 0x01010101);
1184 UNMOCK(tor_lookup_hostname);
1186 tor_free(options->Address);
1187 tor_free(hostname_out);
1190 * CASE 3:
1191 * Given that options->Address is NULL, we want resolve_my_address()
1192 * to try and use tor_gethostname() to get hostname AND use
1193 * tor_lookup_hostname() to get IP address.
1196 resolved_addr = 0;
1197 tor_free(options->Address);
1198 options->Address = NULL;
1200 MOCK(tor_gethostname,tor_gethostname_replacement);
1201 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1203 prev_n_gethostname_replacement = n_gethostname_replacement;
1204 prev_n_hostname_01010101 = n_hostname_01010101;
1206 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1207 &method_used,&hostname_out);
1209 tt_want(retval == 0);
1210 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1211 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1212 tt_want_str_op(method_used,OP_EQ,"GETHOSTNAME");
1213 tt_want_str_op(hostname_out,OP_EQ,"onionrouter!");
1214 tt_assert(resolved_addr == 0x01010101);
1216 UNMOCK(tor_gethostname);
1217 UNMOCK(tor_lookup_hostname);
1219 tor_free(hostname_out);
1222 * CASE 4:
1223 * Given that options->Address is a local host address, we want
1224 * resolve_my_address() function to fail.
1227 resolved_addr = 0;
1228 tor_free(options->Address);
1229 options->Address = tor_strdup("127.0.0.1");
1231 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1232 &method_used,&hostname_out);
1234 tt_want(resolved_addr == 0);
1235 tt_int_op(retval, OP_EQ, -1);
1237 tor_free(options->Address);
1238 tor_free(hostname_out);
1241 * CASE 5:
1242 * We want resolve_my_address() to fail if DNS address in options->Address
1243 * cannot be resolved.
1246 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1248 prev_n_hostname_failure = n_hostname_failure;
1250 tor_free(options->Address);
1251 options->Address = tor_strdup("www.tor-project.org");
1253 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1254 &method_used,&hostname_out);
1256 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1257 tt_int_op(retval, OP_EQ, -1);
1259 UNMOCK(tor_lookup_hostname);
1261 tor_free(options->Address);
1262 tor_free(hostname_out);
1265 * CASE 6:
1266 * If options->Address is NULL AND gettting local hostname fails, we want
1267 * resolve_my_address() to fail as well.
1270 MOCK(tor_gethostname,tor_gethostname_failure);
1272 prev_n_gethostname_failure = n_gethostname_failure;
1274 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1275 &method_used,&hostname_out);
1277 tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
1278 tt_int_op(retval, OP_EQ, -1);
1280 UNMOCK(tor_gethostname);
1281 tor_free(hostname_out);
1284 * CASE 7:
1285 * We want resolve_my_address() to try and get network interface address via
1286 * get_interface_address() if hostname returned by tor_gethostname() cannot be
1287 * resolved into IP address.
1290 MOCK(tor_gethostname,tor_gethostname_replacement);
1291 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1292 MOCK(get_interface_address,get_interface_address_08080808);
1294 prev_n_gethostname_replacement = n_gethostname_replacement;
1295 prev_n_get_interface_address = n_get_interface_address;
1297 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1298 &method_used,&hostname_out);
1300 tt_want(retval == 0);
1301 tt_want_int_op(n_gethostname_replacement, OP_EQ,
1302 prev_n_gethostname_replacement + 1);
1303 tt_want_int_op(n_get_interface_address, OP_EQ,
1304 prev_n_get_interface_address + 1);
1305 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1306 tt_want(hostname_out == NULL);
1307 tt_assert(resolved_addr == 0x08080808);
1309 UNMOCK(get_interface_address);
1310 tor_free(hostname_out);
1313 * CASE 8:
1314 * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
1315 * is unresolvable. We want resolve_my_address to fail if
1316 * get_interface_address() fails.
1319 MOCK(get_interface_address,get_interface_address_failure);
1321 prev_n_get_interface_address_failure = n_get_interface_address_failure;
1322 prev_n_gethostname_replacement = n_gethostname_replacement;
1324 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1325 &method_used,&hostname_out);
1327 tt_want(n_get_interface_address_failure ==
1328 prev_n_get_interface_address_failure + 1);
1329 tt_want(n_gethostname_replacement ==
1330 prev_n_gethostname_replacement + 1);
1331 tt_int_op(retval, OP_EQ, -1);
1333 UNMOCK(get_interface_address);
1334 tor_free(hostname_out);
1337 * CASE 9:
1338 * Given that options->Address is NULL AND tor_lookup_hostname()
1339 * fails AND hostname returned by gethostname() resolves
1340 * to local IP address, we want resolve_my_address() function to
1341 * call get_interface_address6(.,AF_INET,.) and return IP address
1342 * the latter function has found.
1345 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1346 MOCK(tor_gethostname,tor_gethostname_replacement);
1347 MOCK(get_interface_address6,get_interface_address6_replacement);
1349 prev_n_gethostname_replacement = n_gethostname_replacement;
1350 prev_n_hostname_failure = n_hostname_failure;
1351 prev_n_get_interface_address6 = n_get_interface_address6;
1353 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1354 &method_used,&hostname_out);
1356 tt_want(last_address6_family == AF_INET);
1357 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1358 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1359 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1360 tt_want(retval == 0);
1361 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1362 tt_assert(resolved_addr == 0x09090909);
1364 UNMOCK(tor_lookup_hostname);
1365 UNMOCK(tor_gethostname);
1366 UNMOCK(get_interface_address6);
1368 tor_free(hostname_out);
1371 * CASE 10: We want resolve_my_address() to fail if all of the following
1372 * are true:
1373 * 1. options->Address is not NULL
1374 * 2. ... but it cannot be converted to struct in_addr by
1375 * tor_inet_aton()
1376 * 3. ... and tor_lookup_hostname() fails to resolve the
1377 * options->Address
1380 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1382 prev_n_hostname_failure = n_hostname_failure;
1384 tor_free(options->Address);
1385 options->Address = tor_strdup("some_hostname");
1387 retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
1388 &method_used,&hostname_out);
1390 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1391 tt_int_op(retval, OP_EQ, -1);
1393 UNMOCK(tor_gethostname);
1394 UNMOCK(tor_lookup_hostname);
1396 tor_free(hostname_out);
1399 * CASE 11:
1400 * Suppose the following sequence of events:
1401 * 1. options->Address is NULL
1402 * 2. tor_gethostname() succeeds to get hostname of machine Tor
1403 * if running on.
1404 * 3. Hostname from previous step cannot be converted to
1405 * address by using tor_inet_aton() function.
1406 * 4. However, tor_lookup_hostname() succeeds in resolving the
1407 * hostname from step 2.
1408 * 5. Unfortunately, tor_addr_is_internal() deems this address
1409 * to be internal.
1410 * 6. get_interface_address6(.,AF_INET,.) returns non-internal
1411 * IPv4
1413 * We want resolve_my_addr() to succeed with method "INTERFACE"
1414 * and address from step 6.
1417 tor_free(options->Address);
1418 options->Address = NULL;
1420 MOCK(tor_gethostname,tor_gethostname_replacement);
1421 MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
1422 MOCK(get_interface_address6,get_interface_address6_replacement);
1424 prev_n_gethostname_replacement = n_gethostname_replacement;
1425 prev_n_hostname_localhost = n_hostname_localhost;
1426 prev_n_get_interface_address6 = n_get_interface_address6;
1428 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1429 &method_used,&hostname_out);
1431 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1432 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1433 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1435 tt_str_op(method_used,OP_EQ,"INTERFACE");
1436 tt_ptr_op(hostname_out, OP_EQ, NULL);
1437 tt_int_op(retval, OP_EQ, 0);
1440 * CASE 11b:
1441 * 1-5 as above.
1442 * 6. get_interface_address6() fails.
1444 * In this subcase, we want resolve_my_address() to fail.
1447 UNMOCK(get_interface_address6);
1448 MOCK(get_interface_address6,get_interface_address6_failure);
1450 prev_n_gethostname_replacement = n_gethostname_replacement;
1451 prev_n_hostname_localhost = n_hostname_localhost;
1452 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1454 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1455 &method_used,&hostname_out);
1457 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1458 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1459 tt_want(n_get_interface_address6_failure ==
1460 prev_n_get_interface_address6_failure + 1);
1462 tt_int_op(retval, OP_EQ, -1);
1464 UNMOCK(tor_gethostname);
1465 UNMOCK(tor_lookup_hostname);
1466 UNMOCK(get_interface_address6);
1468 /* CASE 12:
1469 * Suppose the following happens:
1470 * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
1471 * 2. tor_gethostname() succeeds in getting hostname of a machine ...
1472 * 3. ... which is successfully parsed by tor_inet_aton() ...
1473 * 4. into IPv4 address that tor_addr_is_inernal() considers to be
1474 * internal.
1476 * In this case, we want resolve_my_address() to fail.
1479 tor_free(options->Address);
1480 options->Address = NULL;
1481 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
1483 MOCK(tor_gethostname,tor_gethostname_localhost);
1485 prev_n_gethostname_localhost = n_gethostname_localhost;
1487 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1488 &method_used,&hostname_out);
1490 tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
1491 tt_int_op(retval, OP_EQ, -1);
1493 UNMOCK(tor_gethostname);
1495 done:
1496 tor_free(options->Address);
1497 tor_free(options->DirAuthorities);
1498 or_options_free(options);
1499 tor_free(hostname_out);
1501 UNMOCK(tor_gethostname);
1502 UNMOCK(tor_lookup_hostname);
1503 UNMOCK(get_interface_address);
1504 UNMOCK(get_interface_address6);
1505 UNMOCK(tor_gethostname);
1508 static void
1509 test_config_adding_trusted_dir_server(void *arg)
1511 (void)arg;
1513 const char digest[DIGEST_LEN] = "";
1514 dir_server_t *ds = NULL;
1515 tor_addr_port_t ipv6;
1516 int rv = -1;
1518 clear_dir_servers();
1519 routerlist_free_all();
1521 /* create a trusted ds without an IPv6 address and port */
1522 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1523 NULL, V3_DIRINFO, 1.0);
1524 tt_assert(ds);
1525 dir_server_add(ds);
1526 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
1527 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1529 /* create a trusted ds with an IPv6 address and port */
1530 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1531 tt_int_op(rv, OP_EQ, 0);
1532 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
1533 NULL, V3_DIRINFO, 1.0);
1534 tt_assert(ds);
1535 dir_server_add(ds);
1536 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
1537 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1539 done:
1540 clear_dir_servers();
1541 routerlist_free_all();
1544 static void
1545 test_config_adding_fallback_dir_server(void *arg)
1547 (void)arg;
1549 const char digest[DIGEST_LEN] = "";
1550 dir_server_t *ds = NULL;
1551 tor_addr_t ipv4;
1552 tor_addr_port_t ipv6;
1553 int rv = -1;
1555 clear_dir_servers();
1556 routerlist_free_all();
1558 rv = tor_addr_parse(&ipv4, "127.0.0.1");
1559 tt_int_op(rv, OP_EQ, AF_INET);
1561 /* create a trusted ds without an IPv6 address and port */
1562 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
1563 tt_assert(ds);
1564 dir_server_add(ds);
1565 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1567 /* create a trusted ds with an IPv6 address and port */
1568 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1569 tt_int_op(rv, OP_EQ, 0);
1570 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
1571 tt_assert(ds);
1572 dir_server_add(ds);
1573 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1575 done:
1576 clear_dir_servers();
1577 routerlist_free_all();
1580 /* No secrets here:
1581 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1582 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1583 * with added spaces
1585 #define TEST_DIR_AUTH_LINE_START \
1586 "foobar orport=12345 " \
1587 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1588 #define TEST_DIR_AUTH_LINE_END \
1589 "1.2.3.4:54321 " \
1590 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1591 #define TEST_DIR_AUTH_IPV6_FLAG \
1592 "ipv6=[feed::beef]:9 "
1594 static void
1595 test_config_parsing_trusted_dir_server(void *arg)
1597 (void)arg;
1598 int rv = -1;
1600 /* parse a trusted dir server without an IPv6 address and port */
1601 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1602 TEST_DIR_AUTH_LINE_END,
1603 V3_DIRINFO, 1);
1604 tt_int_op(rv, OP_EQ, 0);
1606 /* parse a trusted dir server with an IPv6 address and port */
1607 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1608 TEST_DIR_AUTH_IPV6_FLAG
1609 TEST_DIR_AUTH_LINE_END,
1610 V3_DIRINFO, 1);
1611 tt_int_op(rv, OP_EQ, 0);
1613 /* Since we are only validating, there is no cleanup. */
1614 done:
1618 #undef TEST_DIR_AUTH_LINE_START
1619 #undef TEST_DIR_AUTH_LINE_END
1620 #undef TEST_DIR_AUTH_IPV6_FLAG
1622 /* No secrets here:
1623 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1625 #define TEST_DIR_FALLBACK_LINE \
1626 "1.2.3.4:54321 orport=12345 " \
1627 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1628 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1629 "ipv6=[2015:c0de::deed]:9"
1631 static void
1632 test_config_parsing_fallback_dir_server(void *arg)
1634 (void)arg;
1635 int rv = -1;
1637 /* parse a trusted dir server without an IPv6 address and port */
1638 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
1639 tt_int_op(rv, OP_EQ, 0);
1641 /* parse a trusted dir server with an IPv6 address and port */
1642 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1643 TEST_DIR_FALLBACK_IPV6_FLAG,
1645 tt_int_op(rv, OP_EQ, 0);
1647 /* Since we are only validating, there is no cleanup. */
1648 done:
1652 #undef TEST_DIR_FALLBACK_LINE
1653 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1655 static void
1656 test_config_adding_default_trusted_dir_servers(void *arg)
1658 (void)arg;
1660 clear_dir_servers();
1661 routerlist_free_all();
1663 /* Assume we only have one bridge authority */
1664 add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
1665 tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
1666 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1668 /* Assume we have eight V3 authorities */
1669 add_default_trusted_dir_authorities(V3_DIRINFO);
1670 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
1671 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
1673 done:
1674 clear_dir_servers();
1675 routerlist_free_all();
1678 static int n_add_default_fallback_dir_servers_known_default = 0;
1681 * This mock function is meant to replace add_default_fallback_dir_servers().
1682 * It will parse and add one known default fallback dir server,
1683 * which has a dir_port of 99.
1684 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
1685 * one every time this function is called.
1687 static void
1688 add_default_fallback_dir_servers_known_default(void)
1690 int i;
1691 const char *fallback[] = {
1692 "127.0.0.1:60099 orport=9009 "
1693 "id=0923456789012345678901234567890123456789",
1694 NULL
1696 for (i=0; fallback[i]; i++) {
1697 if (parse_dir_fallback_line(fallback[i], 0)<0) {
1698 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
1699 fallback[i]);
1702 n_add_default_fallback_dir_servers_known_default++;
1705 /* Test all the different combinations of adding dir servers */
1706 static void
1707 test_config_adding_dir_servers(void *arg)
1709 (void)arg;
1711 /* allocate options */
1712 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
1714 /* Allocate and populate configuration lines:
1716 * Use the same format as the hard-coded directories in
1717 * add_default_trusted_dir_authorities().
1718 * Zeroing the structure has the same effect as initialising to:
1719 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
1721 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
1722 test_dir_authority->key = tor_strdup("DirAuthority");
1723 test_dir_authority->value = tor_strdup(
1724 "D0 orport=9000 "
1725 "v3ident=0023456789012345678901234567890123456789 "
1726 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1729 config_line_t *test_alt_bridge_authority = tor_malloc_zero(
1730 sizeof(config_line_t));
1731 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
1732 test_alt_bridge_authority->value = tor_strdup(
1733 "B1 orport=9001 bridge "
1734 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1737 config_line_t *test_alt_dir_authority = tor_malloc_zero(
1738 sizeof(config_line_t));
1739 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
1740 test_alt_dir_authority->value = tor_strdup(
1741 "A2 orport=9002 "
1742 "v3ident=0223456789012345678901234567890123456789 "
1743 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1746 /* Use the format specified in the manual page */
1747 config_line_t *test_fallback_directory = tor_malloc_zero(
1748 sizeof(config_line_t));
1749 test_fallback_directory->key = tor_strdup("FallbackDir");
1750 test_fallback_directory->value = tor_strdup(
1751 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
1754 /* We need to know if add_default_fallback_dir_servers is called,
1755 * whatever the size of the list in fallback_dirs.inc,
1756 * so we use a version of add_default_fallback_dir_servers that adds
1757 * one known default fallback directory. */
1758 MOCK(add_default_fallback_dir_servers,
1759 add_default_fallback_dir_servers_known_default);
1761 /* There are 16 different cases, covering each combination of set/NULL for:
1762 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
1763 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
1764 * But validate_dir_servers() ensures that:
1765 * "You cannot set both DirAuthority and Alternate*Authority."
1766 * This reduces the number of cases to 10.
1768 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
1769 * So 1001 or case 9 is:
1770 * DirAuthorities set,
1771 * AlternateBridgeAuthority NULL,
1772 * AlternateDirAuthority NULL
1773 * FallbackDir set
1774 * The valid cases are cases 0-9 counting using this method, as every case
1775 * greater than or equal to 10 = 1010 is invalid.
1777 * 1. Outcome: Use Set Directory Authorities
1778 * - No Default Authorities
1779 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1780 * if they are set
1781 * Cases expected to yield this outcome:
1782 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1783 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1784 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1786 * 2. Outcome: Use Set Bridge Authority
1787 * - Use Default Non-Bridge Directory Authorities
1788 * - Use FallbackDir if it is set, otherwise use default FallbackDir
1789 * Cases expected to yield this outcome:
1790 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
1791 * AlternateBridgeAuthority is set, and
1792 * AlternateDirAuthority is NULL)
1794 * 3. Outcome: Use Set Alternate Directory Authority
1795 * - Use Default Bridge Authorities
1796 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
1797 * Cases expected to yield this outcome:
1798 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
1799 * are both NULL, but AlternateDirAuthority is set)
1801 * 4. Outcome: Use Set Custom Fallback Directory
1802 * - Use Default Bridge & Directory Authorities
1803 * Cases expected to yield this outcome:
1804 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
1805 * are all NULL, but FallbackDir is set)
1807 * 5. Outcome: Use All Defaults
1808 * - Use Default Bridge & Directory Authorities, and
1809 * Default Fallback Directories
1810 * Cases expected to yield this outcome:
1811 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
1812 * and FallbackDir are all NULL)
1816 * Find out how many default Bridge, Non-Bridge and Fallback Directories
1817 * are hard-coded into this build.
1818 * This code makes some assumptions about the implementation.
1819 * If they are wrong, one or more of cases 0-5 could fail.
1821 int n_default_alt_bridge_authority = 0;
1822 int n_default_alt_dir_authority = 0;
1823 int n_default_fallback_dir = 0;
1824 #define n_default_authorities ((n_default_alt_bridge_authority) \
1825 + (n_default_alt_dir_authority))
1827 /* Pre-Count Number of Authorities of Each Type
1828 * Use 0000: No Directory Authorities or Fallback Directories Set
1831 /* clear fallback dirs counter */
1832 n_add_default_fallback_dir_servers_known_default = 0;
1834 /* clear options*/
1835 memset(options, 0, sizeof(or_options_t));
1837 /* clear any previous dir servers:
1838 consider_adding_dir_servers() should do this anyway */
1839 clear_dir_servers();
1841 /* assign options: 0000 */
1842 options->DirAuthorities = NULL;
1843 options->AlternateBridgeAuthority = NULL;
1844 options->AlternateDirAuthority = NULL;
1845 options->FallbackDir = NULL;
1846 options->UseDefaultFallbackDirs = 1;
1848 /* parse options - ensure we always update by passing NULL old_options */
1849 consider_adding_dir_servers(options, NULL);
1851 /* check outcome */
1853 /* we must have added the default fallback dirs */
1854 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
1856 /* we have more fallbacks than just the authorities */
1857 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1860 /* fallback_dir_servers */
1861 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1863 /* Count Bridge Authorities */
1864 SMARTLIST_FOREACH(fallback_servers,
1865 dir_server_t *,
1867 /* increment the found counter if it's a bridge auth */
1868 n_default_alt_bridge_authority +=
1869 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
1870 1 : 0)
1872 /* If we have no default bridge authority, something has gone wrong */
1873 tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
1875 /* Count v3 Authorities */
1876 SMARTLIST_FOREACH(fallback_servers,
1877 dir_server_t *,
1879 /* increment found counter if it's a v3 auth */
1880 n_default_alt_dir_authority +=
1881 ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
1882 1 : 0)
1884 /* If we have no default authorities, something has gone really wrong */
1885 tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
1887 /* Calculate Fallback Directory Count */
1888 n_default_fallback_dir = (smartlist_len(fallback_servers) -
1889 n_default_alt_bridge_authority -
1890 n_default_alt_dir_authority);
1891 /* If we have a negative count, something has gone really wrong,
1892 * or some authorities aren't being added as fallback directories.
1893 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
1894 * authorities being fallback directories.) */
1895 tt_int_op(n_default_fallback_dir, OP_GE, 0);
1900 * 1. Outcome: Use Set Directory Authorities
1901 * - No Default Authorities
1902 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1903 * if they are set
1904 * Cases expected to yield this outcome:
1905 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1906 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1907 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1910 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
1911 AlternateDirAuthority Not Set, FallbackDir Set */
1913 /* clear fallback dirs counter */
1914 n_add_default_fallback_dir_servers_known_default = 0;
1916 /* clear options*/
1917 memset(options, 0, sizeof(or_options_t));
1919 /* clear any previous dir servers:
1920 consider_adding_dir_servers() should do this anyway */
1921 clear_dir_servers();
1923 /* assign options: 1001 */
1924 options->DirAuthorities = test_dir_authority;
1925 options->AlternateBridgeAuthority = NULL;
1926 options->AlternateDirAuthority = NULL;
1927 options->FallbackDir = test_fallback_directory;
1928 options->UseDefaultFallbackDirs = 1;
1930 /* parse options - ensure we always update by passing NULL old_options */
1931 consider_adding_dir_servers(options, NULL);
1933 /* check outcome */
1935 /* we must not have added the default fallback dirs */
1936 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
1938 /* we have more fallbacks than just the authorities */
1939 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1942 /* trusted_dir_servers */
1943 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
1944 /* D0, (No B1), (No A2) */
1945 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
1947 /* DirAuthority - D0 - dir_port: 60090 */
1948 int found_D0 = 0;
1949 SMARTLIST_FOREACH(dir_servers,
1950 dir_server_t *,
1952 /* increment the found counter if dir_port matches */
1953 found_D0 +=
1954 (ds->dir_port == 60090 ?
1955 1 : 0)
1957 tt_int_op(found_D0, OP_EQ, 1);
1959 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
1960 int found_B1 = 0;
1961 SMARTLIST_FOREACH(dir_servers,
1962 dir_server_t *,
1964 /* increment the found counter if dir_port matches */
1965 found_B1 +=
1966 (ds->dir_port == 60091 ?
1967 1 : 0)
1969 tt_int_op(found_B1, OP_EQ, 0);
1971 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
1972 int found_A2 = 0;
1973 SMARTLIST_FOREACH(dir_servers,
1974 dir_server_t *,
1976 /* increment the found counter if dir_port matches */
1977 found_A2 +=
1978 (ds->dir_port == 60092 ?
1979 1 : 0)
1981 tt_int_op(found_A2, OP_EQ, 0);
1985 /* fallback_dir_servers */
1986 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1987 /* D0, (No B1), (No A2), Custom Fallback */
1988 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
1990 /* DirAuthority - D0 - dir_port: 60090 */
1991 int found_D0 = 0;
1992 SMARTLIST_FOREACH(fallback_servers,
1993 dir_server_t *,
1995 /* increment the found counter if dir_port matches */
1996 found_D0 +=
1997 (ds->dir_port == 60090 ?
1998 1 : 0)
2000 tt_int_op(found_D0, OP_EQ, 1);
2002 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2003 int found_B1 = 0;
2004 SMARTLIST_FOREACH(fallback_servers,
2005 dir_server_t *,
2007 /* increment the found counter if dir_port matches */
2008 found_B1 +=
2009 (ds->dir_port == 60091 ?
2010 1 : 0)
2012 tt_int_op(found_B1, OP_EQ, 0);
2014 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2015 int found_A2 = 0;
2016 SMARTLIST_FOREACH(fallback_servers,
2017 dir_server_t *,
2019 /* increment the found counter if dir_port matches */
2020 found_A2 +=
2021 (ds->dir_port == 60092 ?
2022 1 : 0)
2024 tt_int_op(found_A2, OP_EQ, 0);
2026 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2027 int found_non_default_fallback = 0;
2028 SMARTLIST_FOREACH(fallback_servers,
2029 dir_server_t *,
2031 /* increment the found counter if dir_port matches */
2032 found_non_default_fallback +=
2033 (ds->dir_port == 60093 ?
2034 1 : 0)
2036 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2038 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2039 int found_default_fallback = 0;
2040 SMARTLIST_FOREACH(fallback_servers,
2041 dir_server_t *,
2043 /* increment the found counter if dir_port matches */
2044 found_default_fallback +=
2045 (ds->dir_port == 60099 ?
2046 1 : 0)
2048 tt_int_op(found_default_fallback, OP_EQ, 0);
2052 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2054 /* clear fallback dirs counter */
2055 n_add_default_fallback_dir_servers_known_default = 0;
2057 /* clear options*/
2058 memset(options, 0, sizeof(or_options_t));
2060 /* clear any previous dir servers:
2061 consider_adding_dir_servers() should do this anyway */
2062 clear_dir_servers();
2064 /* assign options: 1000 */
2065 options->DirAuthorities = test_dir_authority;
2066 options->AlternateBridgeAuthority = NULL;
2067 options->AlternateDirAuthority = NULL;
2068 options->FallbackDir = NULL;
2069 options->UseDefaultFallbackDirs = 1;
2071 /* parse options - ensure we always update by passing NULL old_options */
2072 consider_adding_dir_servers(options, NULL);
2074 /* check outcome */
2076 /* we must not have added the default fallback dirs */
2077 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2079 /* we just have the authorities */
2080 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2083 /* trusted_dir_servers */
2084 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2085 /* D0, (No B1), (No A2) */
2086 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
2088 /* DirAuthority - D0 - dir_port: 60090 */
2089 int found_D0 = 0;
2090 SMARTLIST_FOREACH(dir_servers,
2091 dir_server_t *,
2093 /* increment the found counter if dir_port matches */
2094 found_D0 +=
2095 (ds->dir_port == 60090 ?
2096 1 : 0)
2098 tt_int_op(found_D0, OP_EQ, 1);
2100 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2101 int found_B1 = 0;
2102 SMARTLIST_FOREACH(dir_servers,
2103 dir_server_t *,
2105 /* increment the found counter if dir_port matches */
2106 found_B1 +=
2107 (ds->dir_port == 60091 ?
2108 1 : 0)
2110 tt_int_op(found_B1, OP_EQ, 0);
2112 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2113 int found_A2 = 0;
2114 SMARTLIST_FOREACH(dir_servers,
2115 dir_server_t *,
2117 /* increment the found counter if dir_port matches */
2118 found_A2 +=
2119 (ds->dir_port == 60092 ?
2120 1 : 0)
2122 tt_int_op(found_A2, OP_EQ, 0);
2126 /* fallback_dir_servers */
2127 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2128 /* D0, (No B1), (No A2), (No Fallback) */
2129 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
2131 /* DirAuthority - D0 - dir_port: 60090 */
2132 int found_D0 = 0;
2133 SMARTLIST_FOREACH(fallback_servers,
2134 dir_server_t *,
2136 /* increment the found counter if dir_port matches */
2137 found_D0 +=
2138 (ds->dir_port == 60090 ?
2139 1 : 0)
2141 tt_int_op(found_D0, OP_EQ, 1);
2143 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2144 int found_B1 = 0;
2145 SMARTLIST_FOREACH(fallback_servers,
2146 dir_server_t *,
2148 /* increment the found counter if dir_port matches */
2149 found_B1 +=
2150 (ds->dir_port == 60091 ?
2151 1 : 0)
2153 tt_int_op(found_B1, OP_EQ, 0);
2155 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2156 int found_A2 = 0;
2157 SMARTLIST_FOREACH(fallback_servers,
2158 dir_server_t *,
2160 /* increment the found counter if dir_port matches */
2161 found_A2 +=
2162 (ds->dir_port == 60092 ?
2163 1 : 0)
2165 tt_int_op(found_A2, OP_EQ, 0);
2167 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2168 int found_non_default_fallback = 0;
2169 SMARTLIST_FOREACH(fallback_servers,
2170 dir_server_t *,
2172 /* increment the found counter if dir_port matches */
2173 found_non_default_fallback +=
2174 (ds->dir_port == 60093 ?
2175 1 : 0)
2177 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2179 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2180 int found_default_fallback = 0;
2181 SMARTLIST_FOREACH(fallback_servers,
2182 dir_server_t *,
2184 /* increment the found counter if dir_port matches */
2185 found_default_fallback +=
2186 (ds->dir_port == 60099 ?
2187 1 : 0)
2189 tt_int_op(found_default_fallback, OP_EQ, 0);
2193 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2195 /* clear fallback dirs counter */
2196 n_add_default_fallback_dir_servers_known_default = 0;
2198 /* clear options*/
2199 memset(options, 0, sizeof(or_options_t));
2201 /* clear any previous dir servers:
2202 consider_adding_dir_servers() should do this anyway */
2203 clear_dir_servers();
2205 /* assign options: 0111 */
2206 options->DirAuthorities = NULL;
2207 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2208 options->AlternateDirAuthority = test_alt_dir_authority;
2209 options->FallbackDir = test_fallback_directory;
2210 options->UseDefaultFallbackDirs = 1;
2212 /* parse options - ensure we always update by passing NULL old_options */
2213 consider_adding_dir_servers(options, NULL);
2215 /* check outcome */
2217 /* we must not have added the default fallback dirs */
2218 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2220 /* we have more fallbacks than just the authorities */
2221 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2224 /* trusted_dir_servers */
2225 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2226 /* (No D0), B1, A2 */
2227 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2229 /* (No DirAuthority) - D0 - dir_port: 60090 */
2230 int found_D0 = 0;
2231 SMARTLIST_FOREACH(dir_servers,
2232 dir_server_t *,
2234 /* increment the found counter if dir_port matches */
2235 found_D0 +=
2236 (ds->dir_port == 60090 ?
2237 1 : 0)
2239 tt_int_op(found_D0, OP_EQ, 0);
2241 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2242 int found_B1 = 0;
2243 SMARTLIST_FOREACH(dir_servers,
2244 dir_server_t *,
2246 /* increment the found counter if dir_port matches */
2247 found_B1 +=
2248 (ds->dir_port == 60091 ?
2249 1 : 0)
2251 tt_int_op(found_B1, OP_EQ, 1);
2253 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2254 int found_A2 = 0;
2255 SMARTLIST_FOREACH(dir_servers,
2256 dir_server_t *,
2258 /* increment the found counter if dir_port matches */
2259 found_A2 +=
2260 (ds->dir_port == 60092 ?
2261 1 : 0)
2263 tt_int_op(found_A2, OP_EQ, 1);
2267 /* fallback_dir_servers */
2268 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2269 /* (No D0), B1, A2, Custom Fallback */
2270 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
2272 /* (No DirAuthority) - D0 - dir_port: 60090 */
2273 int found_D0 = 0;
2274 SMARTLIST_FOREACH(fallback_servers,
2275 dir_server_t *,
2277 /* increment the found counter if dir_port matches */
2278 found_D0 +=
2279 (ds->dir_port == 60090 ?
2280 1 : 0)
2282 tt_int_op(found_D0, OP_EQ, 0);
2284 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2285 int found_B1 = 0;
2286 SMARTLIST_FOREACH(fallback_servers,
2287 dir_server_t *,
2289 /* increment the found counter if dir_port matches */
2290 found_B1 +=
2291 (ds->dir_port == 60091 ?
2292 1 : 0)
2294 tt_int_op(found_B1, OP_EQ, 1);
2296 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2297 int found_A2 = 0;
2298 SMARTLIST_FOREACH(fallback_servers,
2299 dir_server_t *,
2301 /* increment the found counter if dir_port matches */
2302 found_A2 +=
2303 (ds->dir_port == 60092 ?
2304 1 : 0)
2306 tt_int_op(found_A2, OP_EQ, 1);
2308 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2309 int found_non_default_fallback = 0;
2310 SMARTLIST_FOREACH(fallback_servers,
2311 dir_server_t *,
2313 /* increment the found counter if dir_port matches */
2314 found_non_default_fallback +=
2315 (ds->dir_port == 60093 ?
2316 1 : 0)
2318 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2320 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2321 int found_default_fallback = 0;
2322 SMARTLIST_FOREACH(fallback_servers,
2323 dir_server_t *,
2325 /* increment the found counter if dir_port matches */
2326 found_default_fallback +=
2327 (ds->dir_port == 60099 ?
2328 1 : 0)
2330 tt_int_op(found_default_fallback, OP_EQ, 0);
2334 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2335 AlternateDirAuthority Set, FallbackDir Not Set */
2337 /* clear fallback dirs counter */
2338 n_add_default_fallback_dir_servers_known_default = 0;
2340 /* clear options*/
2341 memset(options, 0, sizeof(or_options_t));
2343 /* clear any previous dir servers:
2344 consider_adding_dir_servers() should do this anyway */
2345 clear_dir_servers();
2347 /* assign options: 0110 */
2348 options->DirAuthorities = NULL;
2349 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2350 options->AlternateDirAuthority = test_alt_dir_authority;
2351 options->FallbackDir = NULL;
2352 options->UseDefaultFallbackDirs = 1;
2354 /* parse options - ensure we always update by passing NULL old_options */
2355 consider_adding_dir_servers(options, NULL);
2357 /* check outcome */
2359 /* we must not have added the default fallback dirs */
2360 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2362 /* we have more fallbacks than just the authorities */
2363 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2366 /* trusted_dir_servers */
2367 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2368 /* (No D0), B1, A2 */
2369 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2371 /* (No DirAuthority) - D0 - dir_port: 60090 */
2372 int found_D0 = 0;
2373 SMARTLIST_FOREACH(dir_servers,
2374 dir_server_t *,
2376 /* increment the found counter if dir_port matches */
2377 found_D0 +=
2378 (ds->dir_port == 60090 ?
2379 1 : 0)
2381 tt_int_op(found_D0, OP_EQ, 0);
2383 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2384 int found_B1 = 0;
2385 SMARTLIST_FOREACH(dir_servers,
2386 dir_server_t *,
2388 /* increment the found counter if dir_port matches */
2389 found_B1 +=
2390 (ds->dir_port == 60091 ?
2391 1 : 0)
2393 tt_int_op(found_B1, OP_EQ, 1);
2395 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2396 int found_A2 = 0;
2397 SMARTLIST_FOREACH(dir_servers,
2398 dir_server_t *,
2400 /* increment the found counter if dir_port matches */
2401 found_A2 +=
2402 (ds->dir_port == 60092 ?
2403 1 : 0)
2405 tt_int_op(found_A2, OP_EQ, 1);
2409 /* fallback_dir_servers */
2410 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2411 /* (No D0), B1, A2, (No Fallback) */
2412 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2414 /* (No DirAuthority) - D0 - dir_port: 60090 */
2415 int found_D0 = 0;
2416 SMARTLIST_FOREACH(fallback_servers,
2417 dir_server_t *,
2419 /* increment the found counter if dir_port matches */
2420 found_D0 +=
2421 (ds->dir_port == 60090 ?
2422 1 : 0)
2424 tt_int_op(found_D0, OP_EQ, 0);
2426 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2427 int found_B1 = 0;
2428 SMARTLIST_FOREACH(fallback_servers,
2429 dir_server_t *,
2431 /* increment the found counter if dir_port matches */
2432 found_B1 +=
2433 (ds->dir_port == 60091 ?
2434 1 : 0)
2436 tt_int_op(found_B1, OP_EQ, 1);
2438 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2439 int found_A2 = 0;
2440 SMARTLIST_FOREACH(fallback_servers,
2441 dir_server_t *,
2443 /* increment the found counter if dir_port matches */
2444 found_A2 +=
2445 (ds->dir_port == 60092 ?
2446 1 : 0)
2448 tt_int_op(found_A2, OP_EQ, 1);
2450 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2451 int found_non_default_fallback = 0;
2452 SMARTLIST_FOREACH(fallback_servers,
2453 dir_server_t *,
2455 /* increment the found counter if dir_port matches */
2456 found_non_default_fallback +=
2457 (ds->dir_port == 60093 ?
2458 1 : 0)
2460 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2462 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2463 int found_default_fallback = 0;
2464 SMARTLIST_FOREACH(fallback_servers,
2465 dir_server_t *,
2467 /* increment the found counter if dir_port matches */
2468 found_default_fallback +=
2469 (ds->dir_port == 60099 ?
2470 1 : 0)
2472 tt_int_op(found_default_fallback, OP_EQ, 0);
2477 2. Outcome: Use Set Bridge Authority
2478 - Use Default Non-Bridge Directory Authorities
2479 - Use FallbackDir if it is set, otherwise use default FallbackDir
2480 Cases expected to yield this outcome:
2481 4 & 5 (the 2 cases where DirAuthorities is NULL,
2482 AlternateBridgeAuthority is set, and
2483 AlternateDirAuthority is NULL)
2486 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2487 AlternateDirAuthority Not Set, FallbackDir Set */
2489 /* clear fallback dirs counter */
2490 n_add_default_fallback_dir_servers_known_default = 0;
2492 /* clear options*/
2493 memset(options, 0, sizeof(or_options_t));
2495 /* clear any previous dir servers:
2496 consider_adding_dir_servers() should do this anyway */
2497 clear_dir_servers();
2499 /* assign options: 0101 */
2500 options->DirAuthorities = NULL;
2501 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2502 options->AlternateDirAuthority = NULL;
2503 options->FallbackDir = test_fallback_directory;
2504 options->UseDefaultFallbackDirs = 1;
2506 /* parse options - ensure we always update by passing NULL old_options */
2507 consider_adding_dir_servers(options, NULL);
2509 /* check outcome */
2511 /* we must not have added the default fallback dirs */
2512 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2514 /* we have more fallbacks than just the authorities */
2515 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2518 /* trusted_dir_servers */
2519 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2520 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2521 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2523 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2524 int found_D0 = 0;
2525 SMARTLIST_FOREACH(dir_servers,
2526 dir_server_t *,
2528 /* increment the found counter if dir_port matches */
2529 found_D0 +=
2530 (ds->dir_port == 60090 ?
2531 1 : 0)
2533 tt_int_op(found_D0, OP_EQ, 0);
2535 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2536 int found_B1 = 0;
2537 SMARTLIST_FOREACH(dir_servers,
2538 dir_server_t *,
2540 /* increment the found counter if dir_port matches */
2541 found_B1 +=
2542 (ds->dir_port == 60091 ?
2543 1 : 0)
2545 tt_int_op(found_B1, OP_EQ, 1);
2547 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2548 int found_A2 = 0;
2549 SMARTLIST_FOREACH(dir_servers,
2550 dir_server_t *,
2552 /* increment the found counter if dir_port matches */
2553 found_A2 +=
2554 (ds->dir_port == 60092 ?
2555 1 : 0)
2557 tt_int_op(found_A2, OP_EQ, 0);
2559 /* There's no easy way of checking that we have included all the
2560 * default v3 non-Bridge directory authorities, so let's assume that
2561 * if the total count above is correct, we have the right ones.
2566 /* fallback_dir_servers */
2567 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2568 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2569 * Custom Fallback */
2570 tt_assert(smartlist_len(fallback_servers) ==
2571 2 + n_default_alt_dir_authority);
2573 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2574 int found_D0 = 0;
2575 SMARTLIST_FOREACH(fallback_servers,
2576 dir_server_t *,
2578 /* increment the found counter if dir_port matches */
2579 found_D0 +=
2580 (ds->dir_port == 60090 ?
2581 1 : 0)
2583 tt_int_op(found_D0, OP_EQ, 0);
2585 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2586 int found_B1 = 0;
2587 SMARTLIST_FOREACH(fallback_servers,
2588 dir_server_t *,
2590 /* increment the found counter if dir_port matches */
2591 found_B1 +=
2592 (ds->dir_port == 60091 ?
2593 1 : 0)
2595 tt_int_op(found_B1, OP_EQ, 1);
2597 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2598 int found_A2 = 0;
2599 SMARTLIST_FOREACH(fallback_servers,
2600 dir_server_t *,
2602 /* increment the found counter if dir_port matches */
2603 found_A2 +=
2604 (ds->dir_port == 60092 ?
2605 1 : 0)
2607 tt_int_op(found_A2, OP_EQ, 0);
2609 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2610 int found_non_default_fallback = 0;
2611 SMARTLIST_FOREACH(fallback_servers,
2612 dir_server_t *,
2614 /* increment the found counter if dir_port matches */
2615 found_non_default_fallback +=
2616 (ds->dir_port == 60093 ?
2617 1 : 0)
2619 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2621 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2622 int found_default_fallback = 0;
2623 SMARTLIST_FOREACH(fallback_servers,
2624 dir_server_t *,
2626 /* increment the found counter if dir_port matches */
2627 found_default_fallback +=
2628 (ds->dir_port == 60099 ?
2629 1 : 0)
2631 tt_int_op(found_default_fallback, OP_EQ, 0);
2633 /* There's no easy way of checking that we have included all the
2634 * default v3 non-Bridge directory authorities, so let's assume that
2635 * if the total count above is correct, we have the right ones.
2640 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2641 AlternateDirAuthority & FallbackDir Not Set */
2643 /* clear fallback dirs counter */
2644 n_add_default_fallback_dir_servers_known_default = 0;
2646 /* clear options*/
2647 memset(options, 0, sizeof(or_options_t));
2649 /* clear any previous dir servers:
2650 consider_adding_dir_servers() should do this anyway */
2651 clear_dir_servers();
2653 /* assign options: 0100 */
2654 options->DirAuthorities = NULL;
2655 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2656 options->AlternateDirAuthority = NULL;
2657 options->FallbackDir = NULL;
2658 options->UseDefaultFallbackDirs = 1;
2660 /* parse options - ensure we always update by passing NULL old_options */
2661 consider_adding_dir_servers(options, NULL);
2663 /* check outcome */
2665 /* we must have added the default fallback dirs */
2666 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
2668 /* we have more fallbacks than just the authorities */
2669 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2672 /* trusted_dir_servers */
2673 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2674 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2675 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2677 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2678 int found_D0 = 0;
2679 SMARTLIST_FOREACH(dir_servers,
2680 dir_server_t *,
2682 /* increment the found counter if dir_port matches */
2683 found_D0 +=
2684 (ds->dir_port == 60090 ?
2685 1 : 0)
2687 tt_int_op(found_D0, OP_EQ, 0);
2689 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2690 int found_B1 = 0;
2691 SMARTLIST_FOREACH(dir_servers,
2692 dir_server_t *,
2694 /* increment the found counter if dir_port matches */
2695 found_B1 +=
2696 (ds->dir_port == 60091 ?
2697 1 : 0)
2699 tt_int_op(found_B1, OP_EQ, 1);
2701 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2702 int found_A2 = 0;
2703 SMARTLIST_FOREACH(dir_servers,
2704 dir_server_t *,
2706 /* increment the found counter if dir_port matches */
2707 found_A2 +=
2708 (ds->dir_port == 60092 ?
2709 1 : 0)
2711 tt_int_op(found_A2, OP_EQ, 0);
2713 /* There's no easy way of checking that we have included all the
2714 * default v3 non-Bridge directory authorities, so let's assume that
2715 * if the total count above is correct, we have the right ones.
2720 /* fallback_dir_servers */
2721 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2722 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2723 * Default Fallback */
2724 tt_assert(smartlist_len(fallback_servers) ==
2725 2 + n_default_alt_dir_authority);
2727 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2728 int found_D0 = 0;
2729 SMARTLIST_FOREACH(fallback_servers,
2730 dir_server_t *,
2732 /* increment the found counter if dir_port matches */
2733 found_D0 +=
2734 (ds->dir_port == 60090 ?
2735 1 : 0)
2737 tt_int_op(found_D0, OP_EQ, 0);
2739 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2740 int found_B1 = 0;
2741 SMARTLIST_FOREACH(fallback_servers,
2742 dir_server_t *,
2744 /* increment the found counter if dir_port matches */
2745 found_B1 +=
2746 (ds->dir_port == 60091 ?
2747 1 : 0)
2749 tt_int_op(found_B1, OP_EQ, 1);
2751 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2752 int found_A2 = 0;
2753 SMARTLIST_FOREACH(fallback_servers,
2754 dir_server_t *,
2756 /* increment the found counter if dir_port matches */
2757 found_A2 +=
2758 (ds->dir_port == 60092 ?
2759 1 : 0)
2761 tt_int_op(found_A2, OP_EQ, 0);
2763 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2764 int found_non_default_fallback = 0;
2765 SMARTLIST_FOREACH(fallback_servers,
2766 dir_server_t *,
2768 /* increment the found counter if dir_port matches */
2769 found_non_default_fallback +=
2770 (ds->dir_port == 60093 ?
2771 1 : 0)
2773 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2775 /* Default FallbackDir - No Nickname - dir_port: 60099 */
2776 int found_default_fallback = 0;
2777 SMARTLIST_FOREACH(fallback_servers,
2778 dir_server_t *,
2780 /* increment the found counter if dir_port matches */
2781 found_default_fallback +=
2782 (ds->dir_port == 60099 ?
2783 1 : 0)
2785 tt_int_op(found_default_fallback, OP_EQ, 1);
2787 /* There's no easy way of checking that we have included all the
2788 * default v3 non-Bridge directory authorities, so let's assume that
2789 * if the total count above is correct, we have the right ones.
2795 3. Outcome: Use Set Alternate Directory Authority
2796 - Use Default Bridge Authorities
2797 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2798 Cases expected to yield this outcome:
2799 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2800 are both NULL, but AlternateDirAuthority is set)
2803 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
2804 AlternateDirAuthority & FallbackDir Set */
2806 /* clear fallback dirs counter */
2807 n_add_default_fallback_dir_servers_known_default = 0;
2809 /* clear options*/
2810 memset(options, 0, sizeof(or_options_t));
2812 /* clear any previous dir servers:
2813 consider_adding_dir_servers() should do this anyway */
2814 clear_dir_servers();
2816 /* assign options: 0011 */
2817 options->DirAuthorities = NULL;
2818 options->AlternateBridgeAuthority = NULL;
2819 options->AlternateDirAuthority = test_alt_dir_authority;
2820 options->FallbackDir = test_fallback_directory;
2821 options->UseDefaultFallbackDirs = 1;
2823 /* parse options - ensure we always update by passing NULL old_options */
2824 consider_adding_dir_servers(options, NULL);
2826 /* check outcome */
2828 /* we must not have added the default fallback dirs */
2829 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2831 /* we have more fallbacks than just the authorities */
2832 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2835 /* trusted_dir_servers */
2836 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2837 /* (No D0), (No B1), Default Bridge Authorities, A2 */
2838 tt_assert(smartlist_len(dir_servers) ==
2839 1 + n_default_alt_bridge_authority);
2841 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2842 int found_D0 = 0;
2843 SMARTLIST_FOREACH(dir_servers,
2844 dir_server_t *,
2846 /* increment the found counter if dir_port matches */
2847 found_D0 +=
2848 (ds->dir_port == 60090 ?
2849 1 : 0)
2851 tt_int_op(found_D0, OP_EQ, 0);
2853 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2854 int found_B1 = 0;
2855 SMARTLIST_FOREACH(dir_servers,
2856 dir_server_t *,
2858 /* increment the found counter if dir_port matches */
2859 found_B1 +=
2860 (ds->dir_port == 60091 ?
2861 1 : 0)
2863 tt_int_op(found_B1, OP_EQ, 0);
2865 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2866 int found_A2 = 0;
2867 SMARTLIST_FOREACH(dir_servers,
2868 dir_server_t *,
2870 /* increment the found counter if dir_port matches */
2871 found_A2 +=
2872 (ds->dir_port == 60092 ?
2873 1 : 0)
2875 tt_int_op(found_A2, OP_EQ, 1);
2877 /* There's no easy way of checking that we have included all the
2878 * default Bridge authorities (except for hard-coding tonga's details),
2879 * so let's assume that if the total count above is correct,
2880 * we have the right ones.
2885 /* fallback_dir_servers */
2886 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2887 /* (No D0), (No B1), Default Bridge Authorities, A2,
2888 * Custom Fallback Directory, (No Default Fallback Directories) */
2889 tt_assert(smartlist_len(fallback_servers) ==
2890 2 + n_default_alt_bridge_authority);
2892 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2893 int found_D0 = 0;
2894 SMARTLIST_FOREACH(fallback_servers,
2895 dir_server_t *,
2897 /* increment the found counter if dir_port matches */
2898 found_D0 +=
2899 (ds->dir_port == 60090 ?
2900 1 : 0)
2902 tt_int_op(found_D0, OP_EQ, 0);
2904 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2905 int found_B1 = 0;
2906 SMARTLIST_FOREACH(fallback_servers,
2907 dir_server_t *,
2909 /* increment the found counter if dir_port matches */
2910 found_B1 +=
2911 (ds->dir_port == 60091 ?
2912 1 : 0)
2914 tt_int_op(found_B1, OP_EQ, 0);
2916 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2917 int found_A2 = 0;
2918 SMARTLIST_FOREACH(fallback_servers,
2919 dir_server_t *,
2921 /* increment the found counter if dir_port matches */
2922 found_A2 +=
2923 (ds->dir_port == 60092 ?
2924 1 : 0)
2926 tt_int_op(found_A2, OP_EQ, 1);
2928 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2929 int found_non_default_fallback = 0;
2930 SMARTLIST_FOREACH(fallback_servers,
2931 dir_server_t *,
2933 /* increment the found counter if dir_port matches */
2934 found_non_default_fallback +=
2935 (ds->dir_port == 60093 ?
2936 1 : 0)
2938 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2940 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2941 int found_default_fallback = 0;
2942 SMARTLIST_FOREACH(fallback_servers,
2943 dir_server_t *,
2945 /* increment the found counter if dir_port matches */
2946 found_default_fallback +=
2947 (ds->dir_port == 60099 ?
2948 1 : 0)
2950 tt_int_op(found_default_fallback, OP_EQ, 0);
2952 /* There's no easy way of checking that we have included all the
2953 * default Bridge authorities (except for hard-coding tonga's details),
2954 * so let's assume that if the total count above is correct,
2955 * we have the right ones.
2960 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
2961 AlternateDirAuthority Set, FallbackDir Not Set */
2963 /* clear fallback dirs counter */
2964 n_add_default_fallback_dir_servers_known_default = 0;
2966 /* clear options*/
2967 memset(options, 0, sizeof(or_options_t));
2969 /* clear any previous dir servers:
2970 consider_adding_dir_servers() should do this anyway */
2971 clear_dir_servers();
2973 /* assign options: 0010 */
2974 options->DirAuthorities = NULL;
2975 options->AlternateBridgeAuthority = NULL;
2976 options->AlternateDirAuthority = test_alt_dir_authority;
2977 options->FallbackDir = NULL;
2978 options->UseDefaultFallbackDirs = 1;
2980 /* parse options - ensure we always update by passing NULL old_options */
2981 consider_adding_dir_servers(options, NULL);
2983 /* check outcome */
2985 /* we must not have added the default fallback dirs */
2986 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2988 /* we just have the authorities */
2989 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2992 /* trusted_dir_servers */
2993 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2994 /* (No D0), (No B1), Default Bridge Authorities, A2,
2995 * No Default or Custom Fallback Directories */
2996 tt_assert(smartlist_len(dir_servers) ==
2997 1 + n_default_alt_bridge_authority);
2999 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3000 int found_D0 = 0;
3001 SMARTLIST_FOREACH(dir_servers,
3002 dir_server_t *,
3004 /* increment the found counter if dir_port matches */
3005 found_D0 +=
3006 (ds->dir_port == 60090 ?
3007 1 : 0)
3009 tt_int_op(found_D0, OP_EQ, 0);
3011 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3012 int found_B1 = 0;
3013 SMARTLIST_FOREACH(dir_servers,
3014 dir_server_t *,
3016 /* increment the found counter if dir_port matches */
3017 found_B1 +=
3018 (ds->dir_port == 60091 ?
3019 1 : 0)
3021 tt_int_op(found_B1, OP_EQ, 0);
3023 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3024 int found_A2 = 0;
3025 SMARTLIST_FOREACH(dir_servers,
3026 dir_server_t *,
3028 /* increment the found counter if dir_port matches */
3029 found_A2 +=
3030 (ds->dir_port == 60092 ?
3031 1 : 0)
3033 tt_int_op(found_A2, OP_EQ, 1);
3035 /* There's no easy way of checking that we have included all the
3036 * default Bridge authorities (except for hard-coding tonga's details),
3037 * so let's assume that if the total count above is correct,
3038 * we have the right ones.
3043 /* fallback_dir_servers */
3044 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3045 /* (No D0), (No B1), Default Bridge Authorities, A2,
3046 * No Custom or Default Fallback Directories */
3047 tt_assert(smartlist_len(fallback_servers) ==
3048 1 + n_default_alt_bridge_authority);
3050 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3051 int found_D0 = 0;
3052 SMARTLIST_FOREACH(fallback_servers,
3053 dir_server_t *,
3055 /* increment the found counter if dir_port matches */
3056 found_D0 +=
3057 (ds->dir_port == 60090 ?
3058 1 : 0)
3060 tt_int_op(found_D0, OP_EQ, 0);
3062 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3063 int found_B1 = 0;
3064 SMARTLIST_FOREACH(fallback_servers,
3065 dir_server_t *,
3067 /* increment the found counter if dir_port matches */
3068 found_B1 +=
3069 (ds->dir_port == 60091 ?
3070 1 : 0)
3072 tt_int_op(found_B1, OP_EQ, 0);
3074 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3075 int found_A2 = 0;
3076 SMARTLIST_FOREACH(fallback_servers,
3077 dir_server_t *,
3079 /* increment the found counter if dir_port matches */
3080 found_A2 +=
3081 (ds->dir_port == 60092 ?
3082 1 : 0)
3084 tt_int_op(found_A2, OP_EQ, 1);
3086 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3087 int found_non_default_fallback = 0;
3088 SMARTLIST_FOREACH(fallback_servers,
3089 dir_server_t *,
3091 /* increment the found counter if dir_port matches */
3092 found_non_default_fallback +=
3093 (ds->dir_port == 60093 ?
3094 1 : 0)
3096 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3098 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3099 int found_default_fallback = 0;
3100 SMARTLIST_FOREACH(fallback_servers,
3101 dir_server_t *,
3103 /* increment the found counter if dir_port matches */
3104 found_default_fallback +=
3105 (ds->dir_port == 60099 ?
3106 1 : 0)
3108 tt_int_op(found_default_fallback, OP_EQ, 0);
3110 /* There's no easy way of checking that we have included all the
3111 * default Bridge authorities (except for hard-coding tonga's details),
3112 * so let's assume that if the total count above is correct,
3113 * we have the right ones.
3119 4. Outcome: Use Set Custom Fallback Directory
3120 - Use Default Bridge & Directory Authorities
3121 Cases expected to yield this outcome:
3122 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3123 are all NULL, but FallbackDir is set)
3126 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3127 & AlternateDirAuthority Not Set, FallbackDir Set */
3129 /* clear fallback dirs counter */
3130 n_add_default_fallback_dir_servers_known_default = 0;
3132 /* clear options*/
3133 memset(options, 0, sizeof(or_options_t));
3135 /* clear any previous dir servers:
3136 consider_adding_dir_servers() should do this anyway */
3137 clear_dir_servers();
3139 /* assign options: 0001 */
3140 options->DirAuthorities = NULL;
3141 options->AlternateBridgeAuthority = NULL;
3142 options->AlternateDirAuthority = NULL;
3143 options->FallbackDir = test_fallback_directory;
3144 options->UseDefaultFallbackDirs = 1;
3146 /* parse options - ensure we always update by passing NULL old_options */
3147 consider_adding_dir_servers(options, NULL);
3149 /* check outcome */
3151 /* we must not have added the default fallback dirs */
3152 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3154 /* we have more fallbacks than just the authorities */
3155 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3158 /* trusted_dir_servers */
3159 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3160 /* (No D0), (No B1), Default Bridge Authorities,
3161 * (No A2), Default v3 Directory Authorities */
3162 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3164 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3165 int found_D0 = 0;
3166 SMARTLIST_FOREACH(dir_servers,
3167 dir_server_t *,
3169 /* increment the found counter if dir_port matches */
3170 found_D0 +=
3171 (ds->dir_port == 60090 ?
3172 1 : 0)
3174 tt_int_op(found_D0, OP_EQ, 0);
3176 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3177 int found_B1 = 0;
3178 SMARTLIST_FOREACH(dir_servers,
3179 dir_server_t *,
3181 /* increment the found counter if dir_port matches */
3182 found_B1 +=
3183 (ds->dir_port == 60091 ?
3184 1 : 0)
3186 tt_int_op(found_B1, OP_EQ, 0);
3188 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3189 int found_A2 = 0;
3190 SMARTLIST_FOREACH(dir_servers,
3191 dir_server_t *,
3193 /* increment the found counter if dir_port matches */
3194 found_A2 +=
3195 (ds->dir_port == 60092 ?
3196 1 : 0)
3198 tt_int_op(found_A2, OP_EQ, 0);
3200 /* There's no easy way of checking that we have included all the
3201 * default Bridge & V3 Directory authorities, so let's assume that
3202 * if the total count above is correct, we have the right ones.
3207 /* fallback_dir_servers */
3208 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3209 /* (No D0), (No B1), Default Bridge Authorities,
3210 * (No A2), Default v3 Directory Authorities,
3211 * Custom Fallback Directory, (No Default Fallback Directories) */
3212 tt_assert(smartlist_len(fallback_servers) ==
3213 1 + n_default_authorities);
3215 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3216 int found_D0 = 0;
3217 SMARTLIST_FOREACH(fallback_servers,
3218 dir_server_t *,
3220 /* increment the found counter if dir_port matches */
3221 found_D0 +=
3222 (ds->dir_port == 60090 ?
3223 1 : 0)
3225 tt_int_op(found_D0, OP_EQ, 0);
3227 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3228 int found_B1 = 0;
3229 SMARTLIST_FOREACH(fallback_servers,
3230 dir_server_t *,
3232 /* increment the found counter if dir_port matches */
3233 found_B1 +=
3234 (ds->dir_port == 60091 ?
3235 1 : 0)
3237 tt_int_op(found_B1, OP_EQ, 0);
3239 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3240 int found_A2 = 0;
3241 SMARTLIST_FOREACH(fallback_servers,
3242 dir_server_t *,
3244 /* increment the found counter if dir_port matches */
3245 found_A2 +=
3246 (ds->dir_port == 60092 ?
3247 1 : 0)
3249 tt_int_op(found_A2, OP_EQ, 0);
3251 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3252 int found_non_default_fallback = 0;
3253 SMARTLIST_FOREACH(fallback_servers,
3254 dir_server_t *,
3256 /* increment the found counter if dir_port matches */
3257 found_non_default_fallback +=
3258 (ds->dir_port == 60093 ?
3259 1 : 0)
3261 tt_int_op(found_non_default_fallback, OP_EQ, 1);
3263 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3264 int found_default_fallback = 0;
3265 SMARTLIST_FOREACH(fallback_servers,
3266 dir_server_t *,
3268 /* increment the found counter if dir_port matches */
3269 found_default_fallback +=
3270 (ds->dir_port == 60099 ?
3271 1 : 0)
3273 tt_int_op(found_default_fallback, OP_EQ, 0);
3275 /* There's no easy way of checking that we have included all the
3276 * default Bridge & V3 Directory authorities, so let's assume that
3277 * if the total count above is correct, we have the right ones.
3283 5. Outcome: Use All Defaults
3284 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3285 Cases expected to yield this outcome:
3286 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3287 and FallbackDir are all NULL)
3290 /* Case 0: 0000 - All Not Set */
3292 /* clear fallback dirs counter */
3293 n_add_default_fallback_dir_servers_known_default = 0;
3295 /* clear options*/
3296 memset(options, 0, sizeof(or_options_t));
3298 /* clear any previous dir servers:
3299 consider_adding_dir_servers() should do this anyway */
3300 clear_dir_servers();
3302 /* assign options: 0001 */
3303 options->DirAuthorities = NULL;
3304 options->AlternateBridgeAuthority = NULL;
3305 options->AlternateDirAuthority = NULL;
3306 options->FallbackDir = NULL;
3307 options->UseDefaultFallbackDirs = 1;
3309 /* parse options - ensure we always update by passing NULL old_options */
3310 consider_adding_dir_servers(options, NULL);
3312 /* check outcome */
3314 /* we must have added the default fallback dirs */
3315 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
3317 /* we have more fallbacks than just the authorities */
3318 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3321 /* trusted_dir_servers */
3322 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3323 /* (No D0), (No B1), Default Bridge Authorities,
3324 * (No A2), Default v3 Directory Authorities */
3325 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3327 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3328 int found_D0 = 0;
3329 SMARTLIST_FOREACH(dir_servers,
3330 dir_server_t *,
3332 /* increment the found counter if dir_port matches */
3333 found_D0 +=
3334 (ds->dir_port == 60090 ?
3335 1 : 0)
3337 tt_int_op(found_D0, OP_EQ, 0);
3339 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3340 int found_B1 = 0;
3341 SMARTLIST_FOREACH(dir_servers,
3342 dir_server_t *,
3344 /* increment the found counter if dir_port matches */
3345 found_B1 +=
3346 (ds->dir_port == 60091 ?
3347 1 : 0)
3349 tt_int_op(found_B1, OP_EQ, 0);
3351 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3352 int found_A2 = 0;
3353 SMARTLIST_FOREACH(dir_servers,
3354 dir_server_t *,
3356 /* increment the found counter if dir_port matches */
3357 found_A2 +=
3358 (ds->dir_port == 60092 ?
3359 1 : 0)
3361 tt_int_op(found_A2, OP_EQ, 0);
3363 /* There's no easy way of checking that we have included all the
3364 * default Bridge & V3 Directory authorities, so let's assume that
3365 * if the total count above is correct, we have the right ones.
3370 /* fallback_dir_servers */
3371 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3372 /* (No D0), (No B1), Default Bridge Authorities,
3373 * (No A2), Default v3 Directory Authorities,
3374 * (No Custom Fallback Directory), Default Fallback Directories */
3375 tt_assert(smartlist_len(fallback_servers) ==
3376 n_default_authorities + n_default_fallback_dir);
3378 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3379 int found_D0 = 0;
3380 SMARTLIST_FOREACH(fallback_servers,
3381 dir_server_t *,
3383 /* increment the found counter if dir_port matches */
3384 found_D0 +=
3385 (ds->dir_port == 60090 ?
3386 1 : 0)
3388 tt_int_op(found_D0, OP_EQ, 0);
3390 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3391 int found_B1 = 0;
3392 SMARTLIST_FOREACH(fallback_servers,
3393 dir_server_t *,
3395 /* increment the found counter if dir_port matches */
3396 found_B1 +=
3397 (ds->dir_port == 60091 ?
3398 1 : 0)
3400 tt_int_op(found_B1, OP_EQ, 0);
3402 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3403 int found_A2 = 0;
3404 SMARTLIST_FOREACH(fallback_servers,
3405 dir_server_t *,
3407 /* increment the found counter if dir_port matches */
3408 found_A2 +=
3409 (ds->dir_port == 60092 ?
3410 1 : 0)
3412 tt_int_op(found_A2, OP_EQ, 0);
3414 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3415 int found_non_default_fallback = 0;
3416 SMARTLIST_FOREACH(fallback_servers,
3417 dir_server_t *,
3419 /* increment the found counter if dir_port matches */
3420 found_non_default_fallback +=
3421 (ds->dir_port == 60093 ?
3422 1 : 0)
3424 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3426 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3427 int found_default_fallback = 0;
3428 SMARTLIST_FOREACH(fallback_servers,
3429 dir_server_t *,
3431 /* increment the found counter if dir_port matches */
3432 found_default_fallback +=
3433 (ds->dir_port == 60099 ?
3434 1 : 0)
3436 tt_int_op(found_default_fallback, OP_EQ, 1);
3438 /* There's no easy way of checking that we have included all the
3439 * default Bridge & V3 Directory authorities, and the default
3440 * Fallback Directories, so let's assume that if the total count
3441 * above is correct, we have the right ones.
3446 done:
3447 clear_dir_servers();
3449 tor_free(test_dir_authority->key);
3450 tor_free(test_dir_authority->value);
3451 tor_free(test_dir_authority);
3453 tor_free(test_alt_dir_authority->key);
3454 tor_free(test_alt_dir_authority->value);
3455 tor_free(test_alt_dir_authority);
3457 tor_free(test_alt_bridge_authority->key);
3458 tor_free(test_alt_bridge_authority->value);
3459 tor_free(test_alt_bridge_authority);
3461 tor_free(test_fallback_directory->key);
3462 tor_free(test_fallback_directory->value);
3463 tor_free(test_fallback_directory);
3465 options->DirAuthorities = NULL;
3466 options->AlternateBridgeAuthority = NULL;
3467 options->AlternateDirAuthority = NULL;
3468 options->FallbackDir = NULL;
3469 or_options_free(options);
3471 UNMOCK(add_default_fallback_dir_servers);
3474 static void
3475 test_config_default_dir_servers(void *arg)
3477 or_options_t *opts = NULL;
3478 (void)arg;
3479 int trusted_count = 0;
3480 int fallback_count = 0;
3482 /* new set of options should stop fallback parsing */
3483 opts = tor_malloc_zero(sizeof(or_options_t));
3484 opts->UseDefaultFallbackDirs = 0;
3485 /* set old_options to NULL to force dir update */
3486 consider_adding_dir_servers(opts, NULL);
3487 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3488 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3489 or_options_free(opts);
3490 opts = NULL;
3492 /* assume a release will never go out with less than 7 authorities */
3493 tt_int_op(trusted_count, OP_GE, 7);
3494 /* if we disable the default fallbacks, there must not be any extra */
3495 tt_assert(fallback_count == trusted_count);
3497 opts = tor_malloc_zero(sizeof(or_options_t));
3498 opts->UseDefaultFallbackDirs = 1;
3499 consider_adding_dir_servers(opts, opts);
3500 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3501 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3502 or_options_free(opts);
3503 opts = NULL;
3505 /* assume a release will never go out with less than 7 authorities */
3506 tt_int_op(trusted_count, OP_GE, 7);
3507 /* XX/teor - allow for default fallbacks to be added without breaking
3508 * the unit tests. Set a minimum fallback count once the list is stable. */
3509 tt_assert(fallback_count >= trusted_count);
3511 done:
3512 or_options_free(opts);
3515 static int mock_router_pick_published_address_result = 0;
3517 static int
3518 mock_router_pick_published_address(const or_options_t *options,
3519 uint32_t *addr, int cache_only)
3521 (void)options;
3522 (void)addr;
3523 (void)cache_only;
3524 return mock_router_pick_published_address_result;
3527 static int mock_router_my_exit_policy_is_reject_star_result = 0;
3529 static int
3530 mock_router_my_exit_policy_is_reject_star(void)
3532 return mock_router_my_exit_policy_is_reject_star_result;
3535 static int mock_advertised_server_mode_result = 0;
3537 static int
3538 mock_advertised_server_mode(void)
3540 return mock_advertised_server_mode_result;
3543 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
3545 static const routerinfo_t *
3546 mock_router_get_my_routerinfo(void)
3548 return mock_router_get_my_routerinfo_result;
3551 static void
3552 test_config_directory_fetch(void *arg)
3554 (void)arg;
3556 /* Test Setup */
3557 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
3558 routerinfo_t routerinfo;
3559 memset(&routerinfo, 0, sizeof(routerinfo));
3560 mock_router_pick_published_address_result = -1;
3561 mock_router_my_exit_policy_is_reject_star_result = 1;
3562 mock_advertised_server_mode_result = 0;
3563 mock_router_get_my_routerinfo_result = NULL;
3564 MOCK(router_pick_published_address, mock_router_pick_published_address);
3565 MOCK(router_my_exit_policy_is_reject_star,
3566 mock_router_my_exit_policy_is_reject_star);
3567 MOCK(advertised_server_mode, mock_advertised_server_mode);
3568 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
3570 /* Clients can use multiple directory mirrors for bootstrap */
3571 memset(options, 0, sizeof(or_options_t));
3572 options->ClientOnly = 1;
3573 tt_assert(server_mode(options) == 0);
3574 tt_assert(public_server_mode(options) == 0);
3575 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3576 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3577 OP_EQ, 1);
3579 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3580 memset(options, 0, sizeof(or_options_t));
3581 options->UseBridges = 1;
3582 tt_assert(server_mode(options) == 0);
3583 tt_assert(public_server_mode(options) == 0);
3584 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3585 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3586 OP_EQ, 1);
3588 /* Bridge Relays (Bridges) must act like clients, and use multiple
3589 * directory mirrors for bootstrap */
3590 memset(options, 0, sizeof(or_options_t));
3591 options->BridgeRelay = 1;
3592 options->ORPort_set = 1;
3593 tt_assert(server_mode(options) == 1);
3594 tt_assert(public_server_mode(options) == 0);
3595 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3596 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3597 OP_EQ, 1);
3599 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3600 * but can use multiple authorities for bootstrap */
3601 memset(options, 0, sizeof(or_options_t));
3602 options->FetchDirInfoEarly = 1;
3603 tt_assert(server_mode(options) == 0);
3604 tt_assert(public_server_mode(options) == 0);
3605 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3606 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3607 OP_EQ, 1);
3609 /* OR servers only fetch the consensus from the authorities when they don't
3610 * know their own address, but never use multiple directories for bootstrap
3612 memset(options, 0, sizeof(or_options_t));
3613 options->ORPort_set = 1;
3615 mock_router_pick_published_address_result = -1;
3616 tt_assert(server_mode(options) == 1);
3617 tt_assert(public_server_mode(options) == 1);
3618 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3619 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3620 OP_EQ, 0);
3622 mock_router_pick_published_address_result = 0;
3623 tt_assert(server_mode(options) == 1);
3624 tt_assert(public_server_mode(options) == 1);
3625 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3626 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3627 OP_EQ, 0);
3629 /* Exit OR servers only fetch the consensus from the authorities when they
3630 * refuse unknown exits, but never use multiple directories for bootstrap
3632 memset(options, 0, sizeof(or_options_t));
3633 options->ORPort_set = 1;
3634 options->ExitRelay = 1;
3635 mock_router_pick_published_address_result = 0;
3636 mock_router_my_exit_policy_is_reject_star_result = 0;
3637 mock_advertised_server_mode_result = 1;
3638 mock_router_get_my_routerinfo_result = &routerinfo;
3640 routerinfo.supports_tunnelled_dir_requests = 1;
3642 options->RefuseUnknownExits = 1;
3643 tt_assert(server_mode(options) == 1);
3644 tt_assert(public_server_mode(options) == 1);
3645 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3646 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3647 OP_EQ, 0);
3649 options->RefuseUnknownExits = 0;
3650 mock_router_pick_published_address_result = 0;
3651 tt_assert(server_mode(options) == 1);
3652 tt_assert(public_server_mode(options) == 1);
3653 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3654 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3655 OP_EQ, 0);
3657 /* Dir servers fetch the consensus from the authorities, unless they are not
3658 * advertising themselves (hibernating) or have no routerinfo or are not
3659 * advertising their dirport, and never use multiple directories for
3660 * bootstrap. This only applies if they are also OR servers.
3661 * (We don't care much about the behaviour of non-OR directory servers.) */
3662 memset(options, 0, sizeof(or_options_t));
3663 options->DirPort_set = 1;
3664 options->ORPort_set = 1;
3665 options->DirCache = 1;
3666 mock_router_pick_published_address_result = 0;
3667 mock_router_my_exit_policy_is_reject_star_result = 1;
3669 mock_advertised_server_mode_result = 1;
3670 routerinfo.dir_port = 1;
3671 mock_router_get_my_routerinfo_result = &routerinfo;
3672 tt_assert(server_mode(options) == 1);
3673 tt_assert(public_server_mode(options) == 1);
3674 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3675 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3676 OP_EQ, 0);
3678 mock_advertised_server_mode_result = 0;
3679 routerinfo.dir_port = 1;
3680 mock_router_get_my_routerinfo_result = &routerinfo;
3681 tt_assert(server_mode(options) == 1);
3682 tt_assert(public_server_mode(options) == 1);
3683 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3684 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3685 OP_EQ, 0);
3687 mock_advertised_server_mode_result = 1;
3688 mock_router_get_my_routerinfo_result = NULL;
3689 tt_assert(server_mode(options) == 1);
3690 tt_assert(public_server_mode(options) == 1);
3691 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3692 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3693 OP_EQ, 0);
3695 mock_advertised_server_mode_result = 1;
3696 routerinfo.dir_port = 0;
3697 routerinfo.supports_tunnelled_dir_requests = 0;
3698 mock_router_get_my_routerinfo_result = &routerinfo;
3699 tt_assert(server_mode(options) == 1);
3700 tt_assert(public_server_mode(options) == 1);
3701 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3702 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3703 OP_EQ, 0);
3705 mock_advertised_server_mode_result = 1;
3706 routerinfo.dir_port = 1;
3707 routerinfo.supports_tunnelled_dir_requests = 1;
3708 mock_router_get_my_routerinfo_result = &routerinfo;
3709 tt_assert(server_mode(options) == 1);
3710 tt_assert(public_server_mode(options) == 1);
3711 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3712 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3713 OP_EQ, 0);
3715 done:
3716 tor_free(options);
3717 UNMOCK(router_pick_published_address);
3718 UNMOCK(router_get_my_routerinfo);
3719 UNMOCK(advertised_server_mode);
3720 UNMOCK(router_my_exit_policy_is_reject_star);
3723 static void
3724 test_config_default_fallback_dirs(void *arg)
3726 const char *fallback[] = {
3727 #include "../or/fallback_dirs.inc"
3728 NULL
3731 int n_included_fallback_dirs = 0;
3732 int n_added_fallback_dirs = 0;
3734 (void)arg;
3735 clear_dir_servers();
3737 while (fallback[n_included_fallback_dirs])
3738 n_included_fallback_dirs++;
3740 add_default_fallback_dir_servers();
3742 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
3744 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
3746 done:
3747 clear_dir_servers();
3750 static void
3751 test_config_port_cfg_line_extract_addrport(void *arg)
3753 (void)arg;
3754 int unixy = 0;
3755 const char *rest = NULL;
3756 char *a = NULL;
3758 tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
3759 tt_int_op(unixy, OP_EQ, 0);
3760 tt_str_op(a, OP_EQ, "");
3761 tt_str_op(rest, OP_EQ, "");
3762 tor_free(a);
3764 tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
3765 OP_EQ, 0);
3766 tt_int_op(unixy, OP_EQ, 0);
3767 tt_str_op(a, OP_EQ, "hello");
3768 tt_str_op(rest, OP_EQ, "");
3769 tor_free(a);
3771 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
3772 &a, &unixy, &rest), OP_EQ, 0);
3773 tt_int_op(unixy, OP_EQ, 0);
3774 tt_str_op(a, OP_EQ, "flipperwalt");
3775 tt_str_op(rest, OP_EQ, "gersplut");
3776 tor_free(a);
3778 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
3779 &a, &unixy, &rest), OP_EQ, 0);
3780 tt_int_op(unixy, OP_EQ, 0);
3781 tt_str_op(a, OP_EQ, "flipperwalt");
3782 tt_str_op(rest, OP_EQ, "gersplut");
3783 tor_free(a);
3785 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
3786 &a, &unixy, &rest), OP_EQ, 0);
3787 tt_int_op(unixy, OP_EQ, 0);
3788 tt_str_op(a, OP_EQ, "flipperwalt");
3789 tt_str_op(rest, OP_EQ, "gersplut");
3790 tor_free(a);
3792 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
3793 &a, &unixy, &rest), OP_EQ, 0);
3794 tt_int_op(unixy, OP_EQ, 1);
3795 tt_str_op(a, OP_EQ, "flipperwalt");
3796 tt_str_op(rest, OP_EQ, "gersplut");
3797 tor_free(a);
3799 tt_int_op(port_cfg_line_extract_addrport("lolol",
3800 &a, &unixy, &rest), OP_EQ, 0);
3801 tt_int_op(unixy, OP_EQ, 0);
3802 tt_str_op(a, OP_EQ, "lolol");
3803 tt_str_op(rest, OP_EQ, "");
3804 tor_free(a);
3806 tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
3807 &a, &unixy, &rest), OP_EQ, 0);
3808 tt_int_op(unixy, OP_EQ, 1);
3809 tt_str_op(a, OP_EQ, "lolol");
3810 tt_str_op(rest, OP_EQ, "");
3811 tor_free(a);
3813 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
3814 &a, &unixy, &rest), OP_EQ, 0);
3815 tt_int_op(unixy, OP_EQ, 1);
3816 tt_str_op(a, OP_EQ, "lolol");
3817 tt_str_op(rest, OP_EQ, "");
3818 tor_free(a);
3820 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
3821 &a, &unixy, &rest), OP_EQ, 0);
3822 tt_int_op(unixy, OP_EQ, 1);
3823 tt_str_op(a, OP_EQ, "lolol");
3824 tt_str_op(rest, OP_EQ, "");
3825 tor_free(a);
3827 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
3828 &a, &unixy, &rest), OP_EQ, 0);
3829 tt_int_op(unixy, OP_EQ, 0);
3830 tt_str_op(a, OP_EQ, "foobar:lolol");
3831 tt_str_op(rest, OP_EQ, "");
3832 tor_free(a);
3834 tt_int_op(port_cfg_line_extract_addrport(":lolol",
3835 &a, &unixy, &rest), OP_EQ, 0);
3836 tt_int_op(unixy, OP_EQ, 0);
3837 tt_str_op(a, OP_EQ, ":lolol");
3838 tt_str_op(rest, OP_EQ, "");
3839 tor_free(a);
3841 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
3842 &a, &unixy, &rest), OP_EQ, 0);
3843 tt_int_op(unixy, OP_EQ, 1);
3844 tt_str_op(a, OP_EQ, "lolol");
3845 tt_str_op(rest, OP_EQ, "");
3846 tor_free(a);
3848 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
3849 &a, &unixy, &rest), OP_EQ, 0);
3850 tt_int_op(unixy, OP_EQ, 1);
3851 tt_str_op(a, OP_EQ, "lolol");
3852 tt_str_op(rest, OP_EQ, "");
3853 tor_free(a);
3855 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
3856 &a, &unixy, &rest), OP_EQ, 0);
3857 tt_int_op(unixy, OP_EQ, 1);
3858 tt_str_op(a, OP_EQ, "lolol");
3859 tt_str_op(rest, OP_EQ, "foo ");
3860 tor_free(a);
3862 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
3863 &a, &unixy, &rest), OP_EQ, 0);
3864 tt_int_op(unixy, OP_EQ, 1);
3865 tt_str_op(a, OP_EQ, "lol ol");
3866 tt_str_op(rest, OP_EQ, "foo ");
3867 tor_free(a);
3869 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
3870 &a, &unixy, &rest), OP_EQ, 0);
3871 tt_int_op(unixy, OP_EQ, 1);
3872 tt_str_op(a, OP_EQ, "lol\" ol");
3873 tt_str_op(rest, OP_EQ, "foo ");
3874 tor_free(a);
3876 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
3877 &a, &unixy, &rest), OP_EQ, -1);
3878 tor_free(a);
3880 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
3881 &a, &unixy, &rest), OP_EQ, -1);
3882 tor_free(a);
3884 done:
3885 tor_free(a);
3888 static config_line_t *
3889 mock_config_line(const char *key, const char *val)
3891 config_line_t *config_line = tor_malloc(sizeof(config_line_t));
3892 memset(config_line, 0, sizeof(config_line_t));
3893 config_line->key = tor_strdup(key);
3894 config_line->value = tor_strdup(val);
3895 return config_line;
3898 static void
3899 test_config_parse_port_config__ports__no_ports_given(void *data)
3901 (void)data;
3902 int ret;
3903 smartlist_t *slout = NULL;
3904 port_cfg_t *port_cfg = NULL;
3906 slout = smartlist_new();
3908 // Test no defaultport, no defaultaddress and no out
3909 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
3910 tt_int_op(ret, OP_EQ, 0);
3912 // Test with defaultport, no defaultaddress and no out
3913 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
3914 tt_int_op(ret, OP_EQ, 0);
3916 // Test no defaultport, with defaultaddress and no out
3917 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3918 tt_int_op(ret, OP_EQ, 0);
3920 // Test with defaultport, with defaultaddress and no out
3921 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3922 tt_int_op(ret, OP_EQ, 0);
3924 // Test no defaultport, no defaultaddress and with out
3925 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 0, 0);
3926 tt_int_op(ret, OP_EQ, 0);
3927 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3929 // Test with defaultport, no defaultaddress and with out
3930 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 42, 0);
3931 tt_int_op(ret, OP_EQ, 0);
3932 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3934 // Test no defaultport, with defaultaddress and with out
3935 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3936 tt_int_op(ret, OP_EQ, 0);
3937 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3939 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3940 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3941 smartlist_clear(slout);
3942 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3943 tt_int_op(ret, OP_EQ, 0);
3944 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3945 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3946 tt_int_op(port_cfg->port, OP_EQ, 42);
3947 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
3949 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3950 // for a unix address
3951 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3952 smartlist_clear(slout);
3953 ret = parse_port_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
3954 42, CL_PORT_IS_UNIXSOCKET);
3955 tt_int_op(ret, OP_EQ, 0);
3956 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3957 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3958 tt_int_op(port_cfg->port, OP_EQ, 0);
3959 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
3960 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
3962 done:
3963 if (slout)
3964 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3965 smartlist_free(slout);
3968 static void
3969 test_config_parse_port_config__ports__ports_given(void *data)
3971 (void)data;
3972 int ret;
3973 smartlist_t *slout = NULL;
3974 port_cfg_t *port_cfg = NULL;
3975 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
3976 tor_addr_t addr;
3978 slout = smartlist_new();
3980 // Test error when encounters an invalid Port specification
3981 config_port_invalid = mock_config_line("DNSPort", "");
3982 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
3983 0, 0);
3984 tt_int_op(ret, OP_EQ, -1);
3986 // Test error when encounters an empty unix domain specification
3987 config_free_lines(config_port_invalid); config_port_invalid = NULL;
3988 config_port_invalid = mock_config_line("DNSPort", "unix:");
3989 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
3990 0, 0);
3991 tt_int_op(ret, OP_EQ, -1);
3993 // Test error when encounters a unix domain specification but the listener
3994 // doesn't support domain sockets
3995 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
3996 ret = parse_port_config(NULL, config_port_valid, "DNS",
3997 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
3998 tt_int_op(ret, OP_EQ, -1);
4000 // Test valid unix domain
4001 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4002 smartlist_clear(slout);
4003 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4004 CONN_TYPE_AP_LISTENER, NULL, 0, 0);
4005 #ifdef _WIN32
4006 tt_int_op(ret, OP_EQ, -1);
4007 #else
4008 tt_int_op(ret, OP_EQ, 0);
4009 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4010 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4011 tt_int_op(port_cfg->port, OP_EQ, 0);
4012 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4013 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
4014 /* Test entry port defaults as initialised in parse_port_config */
4015 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4016 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4017 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4018 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4019 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
4020 #endif /* defined(_WIN32) */
4022 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
4023 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4024 config_port_invalid = mock_config_line("SOCKSPort",
4025 "unix:/tmp/foo/bar NoIPv4Traffic "
4026 "NoIPv6Traffic "
4027 "NoOnionTraffic");
4028 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4029 CONN_TYPE_AP_LISTENER, NULL, 0,
4030 CL_PORT_TAKES_HOSTNAMES);
4031 tt_int_op(ret, OP_EQ, -1);
4033 // Test failure if we have no DNS and we're a DNSPort
4034 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4035 config_port_invalid = mock_config_line("DNSPort",
4036 "127.0.0.1:80 NoDNSRequest");
4037 ret = parse_port_config(NULL, config_port_invalid, "DNS",
4038 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4039 CL_PORT_TAKES_HOSTNAMES);
4040 tt_int_op(ret, OP_EQ, -1);
4042 // If we're a DNSPort, DNS only is ok
4043 // Use a port because DNSPort doesn't support sockets
4044 config_free_lines(config_port_valid); config_port_valid = NULL;
4045 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4046 smartlist_clear(slout);
4047 config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
4048 "NoIPv6Traffic "
4049 "NoIPv4Traffic NoOnionTraffic");
4050 ret = parse_port_config(slout, config_port_valid, "DNS",
4051 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4052 CL_PORT_TAKES_HOSTNAMES);
4053 tt_int_op(ret, OP_EQ, 0);
4054 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4055 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4056 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4057 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4058 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4059 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
4061 // Test failure if we have DNS but no ipv4 and no ipv6
4062 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4063 config_port_invalid = mock_config_line("SOCKSPort",
4064 "NoIPv6Traffic "
4065 "unix:/tmp/foo/bar NoIPv4Traffic");
4066 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4067 CONN_TYPE_AP_LISTENER, NULL, 0,
4068 CL_PORT_TAKES_HOSTNAMES);
4069 tt_int_op(ret, OP_EQ, -1);
4071 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
4072 // options)
4073 config_free_lines(config_port_valid); config_port_valid = NULL;
4074 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4075 smartlist_clear(slout);
4076 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4077 "NoIPv6Traffic "
4078 "NoDNSRequest NoIPv4Traffic");
4079 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4080 CONN_TYPE_AP_LISTENER, NULL, 0,
4081 CL_PORT_TAKES_HOSTNAMES);
4082 #ifdef _WIN32
4083 tt_int_op(ret, OP_EQ, -1);
4084 #else
4085 tt_int_op(ret, OP_EQ, 0);
4086 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4087 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4088 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4089 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4090 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4091 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4092 #endif /* defined(_WIN32) */
4094 // Test success with quoted unix: address.
4095 config_free_lines(config_port_valid); config_port_valid = NULL;
4096 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4097 smartlist_clear(slout);
4098 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
4099 "NoIPv6Traffic "
4100 "NoDNSRequest NoIPv4Traffic");
4101 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4102 CONN_TYPE_AP_LISTENER, NULL, 0,
4103 CL_PORT_TAKES_HOSTNAMES);
4104 #ifdef _WIN32
4105 tt_int_op(ret, OP_EQ, -1);
4106 #else
4107 tt_int_op(ret, OP_EQ, 0);
4108 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4109 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4110 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4111 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4112 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4113 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4114 #endif /* defined(_WIN32) */
4116 // Test failure with broken quoted unix: address.
4117 config_free_lines(config_port_valid); config_port_valid = NULL;
4118 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4119 smartlist_clear(slout);
4120 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
4121 "NoIPv6Traffic "
4122 "NoDNSRequest NoIPv4Traffic");
4123 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4124 CONN_TYPE_AP_LISTENER, NULL, 0,
4125 CL_PORT_TAKES_HOSTNAMES);
4126 tt_int_op(ret, OP_EQ, -1);
4128 // Test failure with empty quoted unix: address.
4129 config_free_lines(config_port_valid); config_port_valid = NULL;
4130 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4131 smartlist_clear(slout);
4132 config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
4133 "NoIPv6Traffic "
4134 "NoDNSRequest NoIPv4Traffic");
4135 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4136 CONN_TYPE_AP_LISTENER, NULL, 0,
4137 CL_PORT_TAKES_HOSTNAMES);
4138 tt_int_op(ret, OP_EQ, -1);
4140 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
4141 config_free_lines(config_port_valid); config_port_valid = NULL;
4142 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4143 smartlist_clear(slout);
4144 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4145 "OnionTrafficOnly");
4146 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4147 CONN_TYPE_AP_LISTENER, NULL, 0,
4148 CL_PORT_TAKES_HOSTNAMES);
4149 #ifdef _WIN32
4150 tt_int_op(ret, OP_EQ, -1);
4151 #else
4152 tt_int_op(ret, OP_EQ, 0);
4153 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4154 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4155 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4156 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4157 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4158 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4159 #endif /* defined(_WIN32) */
4161 // Test success with no ipv4 but take ipv6
4162 config_free_lines(config_port_valid); config_port_valid = NULL;
4163 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4164 smartlist_clear(slout);
4165 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4166 "NoIPv4Traffic IPv6Traffic");
4167 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4168 CONN_TYPE_AP_LISTENER, NULL, 0,
4169 CL_PORT_TAKES_HOSTNAMES);
4170 #ifdef _WIN32
4171 tt_int_op(ret, OP_EQ, -1);
4172 #else
4173 tt_int_op(ret, OP_EQ, 0);
4174 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4175 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4176 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4177 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4178 #endif /* defined(_WIN32) */
4180 // Test success with both ipv4 and ipv6
4181 config_free_lines(config_port_valid); config_port_valid = NULL;
4182 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4183 smartlist_clear(slout);
4184 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4185 "IPv4Traffic IPv6Traffic");
4186 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4187 CONN_TYPE_AP_LISTENER, NULL, 0,
4188 CL_PORT_TAKES_HOSTNAMES);
4189 #ifdef _WIN32
4190 tt_int_op(ret, OP_EQ, -1);
4191 #else
4192 tt_int_op(ret, OP_EQ, 0);
4193 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4194 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4195 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4196 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4197 #endif /* defined(_WIN32) */
4199 // Test failure if we specify world writable for an IP Port
4200 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4201 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
4202 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4203 "127.0.0.3", 0, 0);
4204 tt_int_op(ret, OP_EQ, -1);
4206 // Test failure if we specify group writable for an IP Port
4207 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4208 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
4209 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4210 "127.0.0.3", 0, 0);
4211 tt_int_op(ret, OP_EQ, -1);
4213 // Test failure if we specify group writable for an IP Port
4214 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4215 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4216 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4217 "127.0.0.3", 0, 0);
4218 tt_int_op(ret, OP_EQ, -1);
4220 // Test success with only a port (this will fail without a default address)
4221 config_free_lines(config_port_valid); config_port_valid = NULL;
4222 config_port_valid = mock_config_line("DNSPort", "42");
4223 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4224 "127.0.0.3", 0, 0);
4225 tt_int_op(ret, OP_EQ, 0);
4227 // Test success with only a port and isolate destination port
4228 config_free_lines(config_port_valid); config_port_valid = NULL;
4229 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4230 smartlist_clear(slout);
4231 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
4232 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4233 "127.0.0.3", 0, 0);
4234 tt_int_op(ret, OP_EQ, 0);
4235 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4236 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4237 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4238 ISO_DEFAULT | ISO_DESTPORT);
4240 // Test success with a negative isolate destination port, and plural
4241 config_free_lines(config_port_valid); config_port_valid = NULL;
4242 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4243 smartlist_clear(slout);
4244 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4245 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4246 "127.0.0.3", 0, 0);
4247 tt_int_op(ret, OP_EQ, 0);
4248 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4249 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4250 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4251 ISO_DEFAULT & ~ISO_DESTPORT);
4253 // Test success with isolate destination address
4254 config_free_lines(config_port_valid); config_port_valid = NULL;
4255 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4256 smartlist_clear(slout);
4257 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
4258 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4259 "127.0.0.3", 0, 0);
4260 tt_int_op(ret, OP_EQ, 0);
4261 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4262 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4263 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4264 ISO_DEFAULT | ISO_DESTADDR);
4266 // Test success with isolate socks AUTH
4267 config_free_lines(config_port_valid); config_port_valid = NULL;
4268 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4269 smartlist_clear(slout);
4270 config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
4271 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4272 "127.0.0.3", 0, 0);
4273 tt_int_op(ret, OP_EQ, 0);
4274 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4275 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4276 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4277 ISO_DEFAULT | ISO_SOCKSAUTH);
4279 // Test success with isolate client protocol
4280 config_free_lines(config_port_valid); config_port_valid = NULL;
4281 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4282 smartlist_clear(slout);
4283 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
4284 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4285 "127.0.0.3", 0, 0);
4286 tt_int_op(ret, OP_EQ, 0);
4287 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4288 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4289 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4290 ISO_DEFAULT | ISO_CLIENTPROTO);
4292 // Test success with isolate client address
4293 config_free_lines(config_port_valid); config_port_valid = NULL;
4294 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4295 smartlist_clear(slout);
4296 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
4297 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4298 "127.0.0.3", 0, 0);
4299 tt_int_op(ret, OP_EQ, 0);
4300 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4301 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4302 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4303 ISO_DEFAULT | ISO_CLIENTADDR);
4305 // Test success with ignored unknown options
4306 config_free_lines(config_port_valid); config_port_valid = NULL;
4307 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4308 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4309 "127.0.0.3", 0, 0);
4310 tt_int_op(ret, OP_EQ, 0);
4312 // Test success with no isolate socks AUTH
4313 config_free_lines(config_port_valid); config_port_valid = NULL;
4314 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4315 smartlist_clear(slout);
4316 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
4317 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4318 "127.0.0.3", 0, 0);
4319 tt_int_op(ret, OP_EQ, 0);
4320 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4321 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4322 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4324 // Test success with prefer ipv6
4325 config_free_lines(config_port_valid); config_port_valid = NULL;
4326 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4327 smartlist_clear(slout);
4328 config_port_valid = mock_config_line("SOCKSPort",
4329 "42 IPv6Traffic PreferIPv6");
4330 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4331 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
4332 CL_PORT_TAKES_HOSTNAMES);
4333 tt_int_op(ret, OP_EQ, 0);
4334 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4335 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4336 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
4338 // Test success with cache ipv4 DNS
4339 config_free_lines(config_port_valid); config_port_valid = NULL;
4340 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4341 smartlist_clear(slout);
4342 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
4343 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4344 "127.0.0.42", 0, 0);
4345 tt_int_op(ret, OP_EQ, 0);
4346 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4347 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4348 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4349 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4351 // Test success with cache ipv6 DNS
4352 config_free_lines(config_port_valid); config_port_valid = NULL;
4353 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4354 smartlist_clear(slout);
4355 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
4356 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4357 "127.0.0.42", 0, 0);
4358 tt_int_op(ret, OP_EQ, 0);
4359 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4360 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4361 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4362 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4364 // Test success with no cache ipv4 DNS
4365 config_free_lines(config_port_valid); config_port_valid = NULL;
4366 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4367 smartlist_clear(slout);
4368 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4369 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4370 "127.0.0.42", 0, 0);
4371 tt_int_op(ret, OP_EQ, 0);
4372 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4373 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4374 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4375 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4377 // Test success with cache DNS
4378 config_free_lines(config_port_valid); config_port_valid = NULL;
4379 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4380 smartlist_clear(slout);
4381 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
4382 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4383 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
4384 tt_int_op(ret, OP_EQ, 0);
4385 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4386 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4387 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4388 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4390 // Test success with use cached ipv4 DNS
4391 config_free_lines(config_port_valid); config_port_valid = NULL;
4392 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4393 smartlist_clear(slout);
4394 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
4395 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4396 "127.0.0.42", 0, 0);
4397 tt_int_op(ret, OP_EQ, 0);
4398 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4399 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4400 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4401 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
4403 // Test success with use cached ipv6 DNS
4404 config_free_lines(config_port_valid); config_port_valid = NULL;
4405 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4406 smartlist_clear(slout);
4407 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
4408 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4409 "127.0.0.42", 0, 0);
4410 tt_int_op(ret, OP_EQ, 0);
4411 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4412 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4413 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
4414 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4416 // Test success with use cached DNS
4417 config_free_lines(config_port_valid); config_port_valid = NULL;
4418 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4419 smartlist_clear(slout);
4420 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
4421 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4422 "127.0.0.42", 0, 0);
4423 tt_int_op(ret, OP_EQ, 0);
4424 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4425 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4426 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4427 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4429 // Test success with not preferring ipv6 automap
4430 config_free_lines(config_port_valid); config_port_valid = NULL;
4431 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4432 smartlist_clear(slout);
4433 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4434 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4435 "127.0.0.42", 0, 0);
4436 tt_int_op(ret, OP_EQ, 0);
4437 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4438 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4439 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
4441 // Test success with prefer SOCKS no auth
4442 config_free_lines(config_port_valid); config_port_valid = NULL;
4443 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4444 smartlist_clear(slout);
4445 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4446 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4447 "127.0.0.42", 0, 0);
4448 tt_int_op(ret, OP_EQ, 0);
4449 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4450 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4451 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4453 // Test failure with both a zero port and a non-zero port
4454 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4455 config_free_lines(config_port_valid); config_port_valid = NULL;
4456 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4457 smartlist_clear(slout);
4458 config_port_invalid = mock_config_line("DNSPort", "0");
4459 config_port_valid = mock_config_line("DNSPort", "42");
4460 config_port_invalid->next = config_port_valid;
4461 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4462 "127.0.0.42", 0, 0);
4463 tt_int_op(ret, OP_EQ, -1);
4465 // Test success with warn non-local control
4466 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4467 smartlist_clear(slout);
4468 ret = parse_port_config(slout, config_port_valid, "Control",
4469 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
4470 CL_PORT_WARN_NONLOCAL);
4471 tt_int_op(ret, OP_EQ, 0);
4473 // Test success with warn non-local listener
4474 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4475 smartlist_clear(slout);
4476 ret = parse_port_config(slout, config_port_valid, "ExtOR",
4477 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
4478 CL_PORT_WARN_NONLOCAL);
4479 tt_int_op(ret, OP_EQ, 0);
4481 // Test success with warn non-local other
4482 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4483 smartlist_clear(slout);
4484 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4485 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4486 tt_int_op(ret, OP_EQ, 0);
4488 // Test success with warn non-local other without out
4489 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4490 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4491 tt_int_op(ret, OP_EQ, 0);
4493 // Test success with both ipv4 and ipv6 but without stream options
4494 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4495 config_port_valid = NULL;
4496 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4497 smartlist_clear(slout);
4498 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
4499 "IPv6Traffic");
4500 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4501 "127.0.0.44", 0,
4502 CL_PORT_TAKES_HOSTNAMES |
4503 CL_PORT_NO_STREAM_OPTIONS);
4504 tt_int_op(ret, OP_EQ, 0);
4505 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4506 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4507 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4508 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4510 // Test failure for a SessionGroup argument with invalid value
4511 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4512 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4513 smartlist_clear(slout);
4514 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
4515 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4516 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4517 tt_int_op(ret, OP_EQ, -1);
4519 // TODO: this seems wrong. Shouldn't it be the other way around?
4520 // Potential bug.
4521 // Test failure for a SessionGroup argument with valid value but with stream
4522 // options allowed
4523 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4524 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4525 smartlist_clear(slout);
4526 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
4527 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4528 "127.0.0.44", 0, 0);
4529 tt_int_op(ret, OP_EQ, -1);
4531 // Test failure for more than one SessionGroup argument
4532 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4533 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4534 smartlist_clear(slout);
4535 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
4536 "SessionGroup=321");
4537 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4538 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4539 tt_int_op(ret, OP_EQ, -1);
4541 // Test success with a sessiongroup options
4542 config_free_lines(config_port_valid); config_port_valid = NULL;
4543 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4544 smartlist_clear(slout);
4545 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
4546 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4547 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4548 tt_int_op(ret, OP_EQ, 0);
4549 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4550 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4551 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
4553 // Test success with a zero unix domain socket, and doesnt add it to out
4554 config_free_lines(config_port_valid); config_port_valid = NULL;
4555 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4556 smartlist_clear(slout);
4557 config_port_valid = mock_config_line("DNSPort", "0");
4558 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4559 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4560 tt_int_op(ret, OP_EQ, 0);
4561 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4563 // Test success with a one unix domain socket, and doesnt add it to out
4564 config_free_lines(config_port_valid); config_port_valid = NULL;
4565 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4566 smartlist_clear(slout);
4567 config_port_valid = mock_config_line("DNSPort", "something");
4568 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4569 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4570 tt_int_op(ret, OP_EQ, 0);
4571 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4572 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4573 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4574 tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
4576 // Test success with a port of auto - it uses the default address
4577 config_free_lines(config_port_valid); config_port_valid = NULL;
4578 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4579 smartlist_clear(slout);
4580 config_port_valid = mock_config_line("DNSPort", "auto");
4581 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4582 "127.0.0.46", 0, 0);
4583 tt_int_op(ret, OP_EQ, 0);
4584 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4585 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4586 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4587 tor_addr_parse(&addr, "127.0.0.46");
4588 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4590 // Test success with parsing both an address and an auto port
4591 config_free_lines(config_port_valid); config_port_valid = NULL;
4592 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4593 smartlist_clear(slout);
4594 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
4595 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4596 "127.0.0.46", 0, 0);
4597 tt_int_op(ret, OP_EQ, 0);
4598 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4599 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4600 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4601 tor_addr_parse(&addr, "127.0.0.122");
4602 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4604 // Test failure when asked to parse an invalid address followed by auto
4605 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4606 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
4607 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
4608 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4609 "127.0.0.46", 0, 0);
4610 UNMOCK(tor_addr_lookup);
4611 tt_int_op(ret, OP_EQ, -1);
4613 // Test success with parsing both an address and a real port
4614 config_free_lines(config_port_valid); config_port_valid = NULL;
4615 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4616 smartlist_clear(slout);
4617 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
4618 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4619 "127.0.0.46", 0, 0);
4620 tt_int_op(ret, OP_EQ, 0);
4621 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4622 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4623 tt_int_op(port_cfg->port, OP_EQ, 656);
4624 tor_addr_parse(&addr, "127.0.0.123");
4625 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4627 // Test failure if we can't parse anything at all
4628 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4629 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4630 smartlist_clear(slout);
4631 config_port_invalid = mock_config_line("DNSPort", "something wrong");
4632 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4633 "127.0.0.46", 0, 0);
4634 tt_int_op(ret, OP_EQ, -1);
4636 // Test failure if we find both an address, a port and an auto
4637 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4638 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4639 smartlist_clear(slout);
4640 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
4641 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4642 "127.0.0.46", 0, 0);
4643 tt_int_op(ret, OP_EQ, -1);
4645 // Test that default to group writeable default sets group writeable for
4646 // domain socket
4647 config_free_lines(config_port_valid); config_port_valid = NULL;
4648 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4649 smartlist_clear(slout);
4650 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
4651 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4652 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
4653 CL_PORT_DFLT_GROUP_WRITABLE);
4654 #ifdef _WIN32
4655 tt_int_op(ret, OP_EQ, -1);
4656 #else
4657 tt_int_op(ret, OP_EQ, 0);
4658 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4659 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4660 tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
4661 #endif /* defined(_WIN32) */
4663 done:
4664 if (slout)
4665 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4666 smartlist_free(slout);
4667 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4668 config_free_lines(config_port_valid); config_port_valid = NULL;
4671 static void
4672 test_config_parse_port_config__ports__server_options(void *data)
4674 (void)data;
4675 int ret;
4676 smartlist_t *slout = NULL;
4677 port_cfg_t *port_cfg = NULL;
4678 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4680 slout = smartlist_new();
4682 // Test success with NoAdvertise option
4683 config_free_lines(config_port_valid); config_port_valid = NULL;
4684 config_port_valid = mock_config_line("DNSPort",
4685 "127.0.0.124:656 NoAdvertise");
4686 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4687 CL_PORT_SERVER_OPTIONS);
4688 tt_int_op(ret, OP_EQ, 0);
4689 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4690 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4691 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
4692 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
4694 // Test success with NoListen option
4695 config_free_lines(config_port_valid); config_port_valid = NULL;
4696 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4697 smartlist_clear(slout);
4698 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
4699 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4700 CL_PORT_SERVER_OPTIONS);
4701 tt_int_op(ret, OP_EQ, 0);
4702 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4703 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4704 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
4705 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
4707 // Test failure with both NoAdvertise and NoListen option
4708 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4709 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4710 smartlist_clear(slout);
4711 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
4712 "NoAdvertise");
4713 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4714 0, CL_PORT_SERVER_OPTIONS);
4715 tt_int_op(ret, OP_EQ, -1);
4717 // Test success with IPv4Only
4718 config_free_lines(config_port_valid); config_port_valid = NULL;
4719 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4720 smartlist_clear(slout);
4721 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
4722 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4723 CL_PORT_SERVER_OPTIONS);
4724 tt_int_op(ret, OP_EQ, 0);
4725 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4726 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4727 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
4728 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
4730 // Test success with IPv6Only
4731 config_free_lines(config_port_valid); config_port_valid = NULL;
4732 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4733 smartlist_clear(slout);
4734 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
4735 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4736 CL_PORT_SERVER_OPTIONS);
4737 tt_int_op(ret, OP_EQ, 0);
4738 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4739 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4740 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
4741 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
4743 // Test failure with both IPv4Only and IPv6Only
4744 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4745 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4746 smartlist_clear(slout);
4747 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
4748 "IPv4Only");
4749 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4750 0, CL_PORT_SERVER_OPTIONS);
4751 tt_int_op(ret, OP_EQ, -1);
4753 // Test success with invalid parameter
4754 config_free_lines(config_port_valid); config_port_valid = NULL;
4755 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4756 smartlist_clear(slout);
4757 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
4758 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4759 CL_PORT_SERVER_OPTIONS);
4760 tt_int_op(ret, OP_EQ, 0);
4761 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4763 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
4764 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4765 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4766 smartlist_clear(slout);
4767 config_port_invalid = mock_config_line("DNSPort",
4768 "127.0.0.124:656 IPv6Only");
4769 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4770 0, CL_PORT_SERVER_OPTIONS);
4771 tt_int_op(ret, OP_EQ, -1);
4773 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
4774 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4775 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4776 smartlist_clear(slout);
4777 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
4778 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4779 0, CL_PORT_SERVER_OPTIONS);
4780 tt_int_op(ret, OP_EQ, -1);
4782 // Check for failure with empty unix: address.
4783 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4784 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4785 smartlist_clear(slout);
4786 config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
4787 ret = parse_port_config(slout, config_port_invalid, "ORPort", 0, NULL,
4788 0, CL_PORT_SERVER_OPTIONS);
4789 tt_int_op(ret, OP_EQ, -1);
4791 done:
4792 if (slout)
4793 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4794 smartlist_free(slout);
4795 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4796 config_free_lines(config_port_valid); config_port_valid = NULL;
4799 static void
4800 test_config_parse_log_severity(void *data)
4802 int ret;
4803 const char *severity_log_lines[] = {
4804 "debug file /tmp/debug.log",
4805 "debug\tfile /tmp/debug.log",
4806 "[handshake]debug [~net,~mm]info notice stdout",
4807 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
4808 NULL
4810 int i;
4811 log_severity_list_t *severity;
4813 (void) data;
4815 severity = tor_malloc(sizeof(log_severity_list_t));
4816 for (i = 0; severity_log_lines[i]; i++) {
4817 memset(severity, 0, sizeof(log_severity_list_t));
4818 ret = parse_log_severity_config(&severity_log_lines[i], severity);
4819 tt_int_op(ret, OP_EQ, 0);
4822 done:
4823 tor_free(severity);
4826 static void
4827 test_config_include_limit(void *data)
4829 (void)data;
4831 config_line_t *result = NULL;
4832 char *torrc_path = NULL;
4833 char *dir = tor_strdup(get_fname("test_include_limit"));
4834 tt_ptr_op(dir, OP_NE, NULL);
4836 #ifdef _WIN32
4837 tt_int_op(mkdir(dir), OP_EQ, 0);
4838 #else
4839 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4840 #endif
4842 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
4843 char torrc_contents[1000];
4844 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4845 torrc_path);
4846 tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
4848 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4849 OP_EQ, -1);
4851 done:
4852 config_free_lines(result);
4853 tor_free(torrc_path);
4854 tor_free(dir);
4857 static void
4858 test_config_include_does_not_exist(void *data)
4860 (void)data;
4862 config_line_t *result = NULL;
4863 char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
4864 char *missing_path = NULL;
4865 tt_ptr_op(dir, OP_NE, NULL);
4867 #ifdef _WIN32
4868 tt_int_op(mkdir(dir), OP_EQ, 0);
4869 #else
4870 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4871 #endif
4873 tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
4874 char torrc_contents[1000];
4875 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4876 missing_path);
4878 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4879 OP_EQ, -1);
4881 done:
4882 config_free_lines(result);
4883 tor_free(dir);
4884 tor_free(missing_path);
4887 static void
4888 test_config_include_error_in_included_file(void *data)
4890 (void)data;
4891 config_line_t *result = NULL;
4893 char *dir = tor_strdup(get_fname("test_error_in_included_file"));
4894 char *invalid_path = NULL;
4895 tt_ptr_op(dir, OP_NE, NULL);
4897 #ifdef _WIN32
4898 tt_int_op(mkdir(dir), OP_EQ, 0);
4899 #else
4900 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4901 #endif
4903 tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
4904 tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
4906 char torrc_contents[1000];
4907 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4908 invalid_path);
4910 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4911 OP_EQ, -1);
4913 done:
4914 config_free_lines(result);
4915 tor_free(dir);
4916 tor_free(invalid_path);
4919 static void
4920 test_config_include_empty_file_folder(void *data)
4922 (void)data;
4923 config_line_t *result = NULL;
4925 char *folder_path = NULL;
4926 char *file_path = NULL;
4927 char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
4928 tt_ptr_op(dir, OP_NE, NULL);
4930 #ifdef _WIN32
4931 tt_int_op(mkdir(dir), OP_EQ, 0);
4932 #else
4933 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4934 #endif
4936 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir);
4937 #ifdef _WIN32
4938 tt_int_op(mkdir(folder_path), OP_EQ, 0);
4939 #else
4940 tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
4941 #endif
4942 tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
4943 tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
4945 char torrc_contents[1000];
4946 tor_snprintf(torrc_contents, sizeof(torrc_contents),
4947 "%%include %s\n"
4948 "%%include %s\n",
4949 folder_path, file_path);
4951 int include_used;
4952 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
4953 NULL), OP_EQ, 0);
4954 tt_ptr_op(result, OP_EQ, NULL);
4955 tt_int_op(include_used, OP_EQ, 1);
4957 done:
4958 config_free_lines(result);
4959 tor_free(folder_path);
4960 tor_free(file_path);
4961 tor_free(dir);
4964 #ifndef _WIN32
4965 static void
4966 test_config_include_no_permission(void *data)
4968 (void)data;
4969 config_line_t *result = NULL;
4971 char *folder_path = NULL;
4972 char *dir = NULL;
4973 if (geteuid() == 0)
4974 tt_skip();
4976 dir = tor_strdup(get_fname("test_include_forbidden_folder"));
4977 tt_ptr_op(dir, OP_NE, NULL);
4979 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4981 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
4982 tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
4984 char torrc_contents[1000];
4985 tor_snprintf(torrc_contents, sizeof(torrc_contents),
4986 "%%include %s\n",
4987 folder_path);
4989 int include_used;
4990 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
4991 &include_used, NULL),
4992 OP_EQ, -1);
4993 tt_ptr_op(result, OP_EQ, NULL);
4995 done:
4996 config_free_lines(result);
4997 tor_free(folder_path);
4998 if (dir)
4999 chmod(dir, 0700);
5000 tor_free(dir);
5002 #endif
5004 static void
5005 test_config_include_recursion_before_after(void *data)
5007 (void)data;
5009 config_line_t *result = NULL;
5010 char *torrc_path = NULL;
5011 char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
5012 tt_ptr_op(dir, OP_NE, NULL);
5014 #ifdef _WIN32
5015 tt_int_op(mkdir(dir), OP_EQ, 0);
5016 #else
5017 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5018 #endif
5020 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5022 char file_contents[1000];
5023 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5024 int i;
5025 // Loop backwards so file_contents has the contents of the first file by the
5026 // end of the loop
5027 for (i = limit; i > 0; i--) {
5028 if (i < limit) {
5029 tor_snprintf(file_contents, sizeof(file_contents),
5030 "Test %d\n"
5031 "%%include %s%d\n"
5032 "Test %d\n",
5033 i, torrc_path, i + 1, 2 * limit - i);
5034 } else {
5035 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
5038 if (i > 1) {
5039 char *file_path = NULL;
5040 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5041 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5042 tor_free(file_path);
5046 int include_used;
5047 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5048 NULL), OP_EQ, 0);
5049 tt_ptr_op(result, OP_NE, NULL);
5050 tt_int_op(include_used, OP_EQ, 1);
5052 int len = 0;
5053 config_line_t *next;
5054 for (next = result; next != NULL; next = next->next) {
5055 char expected[10];
5056 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5057 tt_str_op(next->key, OP_EQ, "Test");
5058 tt_str_op(next->value, OP_EQ, expected);
5059 len++;
5061 tt_int_op(len, OP_EQ, 2 * limit - 1);
5063 done:
5064 config_free_lines(result);
5065 tor_free(dir);
5066 tor_free(torrc_path);
5069 static void
5070 test_config_include_recursion_after_only(void *data)
5072 (void)data;
5074 config_line_t *result = NULL;
5075 char *torrc_path = NULL;
5076 char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
5077 tt_ptr_op(dir, OP_NE, NULL);
5079 #ifdef _WIN32
5080 tt_int_op(mkdir(dir), OP_EQ, 0);
5081 #else
5082 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5083 #endif
5085 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5087 char file_contents[1000];
5088 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5089 int i;
5090 // Loop backwards so file_contents has the contents of the first file by the
5091 // end of the loop
5092 for (i = limit; i > 0; i--) {
5093 int n = (i - limit - 1) * -1;
5094 if (i < limit) {
5095 tor_snprintf(file_contents, sizeof(file_contents),
5096 "%%include %s%d\n"
5097 "Test %d\n",
5098 torrc_path, i + 1, n);
5099 } else {
5100 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
5103 if (i > 1) {
5104 char *file_path = NULL;
5105 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5106 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5107 tor_free(file_path);
5111 int include_used;
5112 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5113 NULL), OP_EQ, 0);
5114 tt_ptr_op(result, OP_NE, NULL);
5115 tt_int_op(include_used, OP_EQ, 1);
5117 int len = 0;
5118 config_line_t *next;
5119 for (next = result; next != NULL; next = next->next) {
5120 char expected[10];
5121 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5122 tt_str_op(next->key, OP_EQ, "Test");
5123 tt_str_op(next->value, OP_EQ, expected);
5124 len++;
5126 tt_int_op(len, OP_EQ, limit);
5128 done:
5129 config_free_lines(result);
5130 tor_free(dir);
5131 tor_free(torrc_path);
5134 static void
5135 test_config_include_folder_order(void *data)
5137 (void)data;
5139 config_line_t *result = NULL;
5140 char *torrcd = NULL;
5141 char *path = NULL;
5142 char *path2 = NULL;
5143 char *dir = tor_strdup(get_fname("test_include_folder_order"));
5144 tt_ptr_op(dir, OP_NE, NULL);
5146 #ifdef _WIN32
5147 tt_int_op(mkdir(dir), OP_EQ, 0);
5148 #else
5149 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5150 #endif
5152 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5154 #ifdef _WIN32
5155 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5156 #else
5157 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5158 #endif
5160 // test that files in subfolders are ignored
5161 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5163 #ifdef _WIN32
5164 tt_int_op(mkdir(path), OP_EQ, 0);
5165 #else
5166 tt_int_op(mkdir(path, 0700), OP_EQ, 0);
5167 #endif
5169 tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
5170 tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
5171 tor_free(path);
5173 // test that files starting with . are ignored
5174 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5175 tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
5176 tor_free(path);
5178 // test file order
5179 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
5180 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5181 tor_free(path);
5183 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
5184 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
5185 tor_free(path);
5187 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
5188 tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
5189 tor_free(path);
5191 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
5192 tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
5193 tor_free(path);
5195 char torrc_contents[1000];
5196 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5197 "%%include %s\n",
5198 torrcd);
5200 int include_used;
5201 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5202 NULL), OP_EQ, 0);
5203 tt_ptr_op(result, OP_NE, NULL);
5204 tt_int_op(include_used, OP_EQ, 1);
5206 int len = 0;
5207 config_line_t *next;
5208 for (next = result; next != NULL; next = next->next) {
5209 char expected[10];
5210 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5211 tt_str_op(next->key, OP_EQ, "Test");
5212 tt_str_op(next->value, OP_EQ, expected);
5213 len++;
5215 tt_int_op(len, OP_EQ, 4);
5217 done:
5218 config_free_lines(result);
5219 tor_free(torrcd);
5220 tor_free(path);
5221 tor_free(path2);
5222 tor_free(dir);
5225 static void
5226 test_config_include_path_syntax(void *data)
5228 (void)data;
5230 config_line_t *result = NULL;
5231 char *dir = tor_strdup(get_fname("test_include_path_syntax"));
5232 char *esc_dir = NULL, *dir_with_pathsep = NULL,
5233 *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
5234 tt_ptr_op(dir, OP_NE, NULL);
5236 #ifdef _WIN32
5237 tt_int_op(mkdir(dir), OP_EQ, 0);
5238 #else
5239 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5240 #endif
5242 esc_dir = esc_for_log(dir);
5243 tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
5244 esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
5246 tor_asprintf(&torrc_contents,
5247 "%%include %s\n"
5248 "%%include %s%s \n" // space to avoid suppressing newline
5249 "%%include %s\n",
5250 esc_dir,
5251 dir, PATH_SEPARATOR,
5252 esc_dir_with_pathsep);
5254 int include_used;
5255 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5256 NULL), OP_EQ, 0);
5257 tt_ptr_op(result, OP_EQ, NULL);
5258 tt_int_op(include_used, OP_EQ, 1);
5260 done:
5261 config_free_lines(result);
5262 tor_free(dir);
5263 tor_free(torrc_contents);
5264 tor_free(esc_dir);
5265 tor_free(dir_with_pathsep);
5266 tor_free(esc_dir_with_pathsep);
5269 static void
5270 test_config_include_not_processed(void *data)
5272 (void)data;
5274 char torrc_contents[1000] = "%include does_not_exist\n";
5275 config_line_t *result = NULL;
5276 tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
5277 tt_ptr_op(result, OP_NE, NULL);
5279 int len = 0;
5280 config_line_t *next;
5281 for (next = result; next != NULL; next = next->next) {
5282 tt_str_op(next->key, OP_EQ, "%include");
5283 tt_str_op(next->value, OP_EQ, "does_not_exist");
5284 len++;
5286 tt_int_op(len, OP_EQ, 1);
5288 done:
5289 config_free_lines(result);
5292 static void
5293 test_config_include_has_include(void *data)
5295 (void)data;
5297 config_line_t *result = NULL;
5298 char *dir = tor_strdup(get_fname("test_include_has_include"));
5299 tt_ptr_op(dir, OP_NE, NULL);
5301 #ifdef _WIN32
5302 tt_int_op(mkdir(dir), OP_EQ, 0);
5303 #else
5304 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5305 #endif
5307 char torrc_contents[1000] = "Test 1\n";
5308 int include_used;
5310 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5311 NULL), OP_EQ, 0);
5312 tt_int_op(include_used, OP_EQ, 0);
5313 config_free_lines(result);
5315 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
5316 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5317 NULL), OP_EQ, 0);
5318 tt_int_op(include_used, OP_EQ, 1);
5320 done:
5321 config_free_lines(result);
5322 tor_free(dir);
5325 static void
5326 test_config_include_flag_both_without(void *data)
5328 (void)data;
5330 char *errmsg = NULL;
5331 char conf_empty[1000];
5332 tor_snprintf(conf_empty, sizeof(conf_empty),
5333 "DataDirectory %s\n",
5334 get_fname(NULL));
5335 // test with defaults-torrc and torrc without include
5336 int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
5337 NULL, &errmsg);
5338 tt_int_op(ret, OP_EQ, 0);
5340 const or_options_t *options = get_options();
5341 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5343 done:
5344 tor_free(errmsg);
5347 static void
5348 test_config_include_flag_torrc_only(void *data)
5350 (void)data;
5352 char *errmsg = NULL;
5353 char *path = NULL;
5354 char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
5355 tt_ptr_op(dir, OP_NE, NULL);
5357 #ifdef _WIN32
5358 tt_int_op(mkdir(dir), OP_EQ, 0);
5359 #else
5360 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5361 #endif
5363 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5364 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5366 char conf_empty[1000];
5367 tor_snprintf(conf_empty, sizeof(conf_empty),
5368 "DataDirectory %s\n",
5369 get_fname(NULL));
5370 char conf_include[1000];
5371 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5373 // test with defaults-torrc without include and torrc with include
5374 int ret = options_init_from_string(conf_empty, conf_include,
5375 CMD_RUN_UNITTESTS, NULL, &errmsg);
5376 tt_int_op(ret, OP_EQ, 0);
5378 const or_options_t *options = get_options();
5379 tt_int_op(options->IncludeUsed, OP_EQ, 1);
5381 done:
5382 tor_free(errmsg);
5383 tor_free(path);
5384 tor_free(dir);
5387 static void
5388 test_config_include_flag_defaults_only(void *data)
5390 (void)data;
5392 char *errmsg = NULL;
5393 char *path = NULL;
5394 char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
5395 tt_ptr_op(dir, OP_NE, NULL);
5397 #ifdef _WIN32
5398 tt_int_op(mkdir(dir), OP_EQ, 0);
5399 #else
5400 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5401 #endif
5403 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5404 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5406 char conf_empty[1000];
5407 tor_snprintf(conf_empty, sizeof(conf_empty),
5408 "DataDirectory %s\n",
5409 get_fname(NULL));
5410 char conf_include[1000];
5411 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5413 // test with defaults-torrc with include and torrc without include
5414 int ret = options_init_from_string(conf_include, conf_empty,
5415 CMD_RUN_UNITTESTS, NULL, &errmsg);
5416 tt_int_op(ret, OP_EQ, 0);
5418 const or_options_t *options = get_options();
5419 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5421 done:
5422 tor_free(errmsg);
5423 tor_free(path);
5424 tor_free(dir);
5427 static void
5428 test_config_dup_and_filter(void *arg)
5430 (void)arg;
5431 /* Test normal input. */
5432 config_line_t *line = NULL;
5433 config_line_append(&line, "abc", "def");
5434 config_line_append(&line, "ghi", "jkl");
5435 config_line_append(&line, "ABCD", "mno");
5437 config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
5438 tt_ptr_op(line_dup, OP_NE, NULL);
5439 tt_ptr_op(line_dup->next, OP_NE, NULL);
5440 tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
5442 tt_str_op(line_dup->key, OP_EQ, "abc");
5443 tt_str_op(line_dup->value, OP_EQ, "def");
5444 tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
5445 tt_str_op(line_dup->next->value, OP_EQ, "mno");
5447 /* empty output */
5448 config_free_lines(line_dup);
5449 line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
5450 tt_ptr_op(line_dup, OP_EQ, NULL);
5452 /* empty input */
5453 config_free_lines(line_dup);
5454 line_dup = config_lines_dup_and_filter(NULL, "abc");
5455 tt_ptr_op(line_dup, OP_EQ, NULL);
5457 done:
5458 config_free_lines(line);
5459 config_free_lines(line_dup);
5462 /* If we're not configured to be a bridge, but we set
5463 * BridgeDistribution, then options_validate () should return -1. */
5464 static void
5465 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
5467 or_options_t* options = get_options_mutable();
5468 or_options_t* old_options = options;
5469 or_options_t* default_options = options;
5470 char* message = NULL;
5471 int ret;
5473 (void)arg;
5475 options->BridgeRelay = 0;
5476 options->BridgeDistribution = (char*)("https");
5478 ret = options_validate(old_options, options, default_options, 0, &message);
5480 tt_int_op(ret, OP_EQ, -1);
5481 tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
5482 "didn't set BridgeRelay!");
5483 done:
5484 tor_free(message);
5485 options->BridgeDistribution = NULL;
5488 /* If the BridgeDistribution setting was valid, 0 should be returned. */
5489 static void
5490 test_config_check_bridge_distribution_setting_valid(void *arg)
5492 int ret = check_bridge_distribution_setting("https");
5494 (void)arg;
5496 tt_int_op(ret, OP_EQ, 0);
5497 done:
5498 return;
5501 /* If the BridgeDistribution setting was invalid, -1 should be returned. */
5502 static void
5503 test_config_check_bridge_distribution_setting_invalid(void *arg)
5505 int ret = check_bridge_distribution_setting("hyphens-are-allowed");
5507 (void)arg;
5509 tt_int_op(ret, OP_EQ, 0);
5511 ret = check_bridge_distribution_setting("asterisks*are*forbidden");
5513 tt_int_op(ret, OP_EQ, -1);
5514 done:
5515 return;
5518 /* If the BridgeDistribution setting was unrecognised, a warning should be
5519 * logged and 0 should be returned. */
5520 static void
5521 test_config_check_bridge_distribution_setting_unrecognised(void *arg)
5523 int ret = check_bridge_distribution_setting("unicorn");
5525 (void)arg;
5527 tt_int_op(ret, OP_EQ, 0);
5528 done:
5529 return;
5532 static void
5533 test_config_include_opened_file_list(void *data)
5535 (void)data;
5537 config_line_t *result = NULL;
5538 smartlist_t *opened_files = smartlist_new();
5539 char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
5540 tt_ptr_op(dir, OP_NE, NULL);
5542 #ifdef _WIN32
5543 tt_int_op(mkdir(dir), OP_EQ, 0);
5544 #else
5545 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5546 #endif
5548 char torrcd[PATH_MAX+1];
5549 tor_snprintf(torrcd, sizeof(torrcd), "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5551 #ifdef _WIN32
5552 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5553 #else
5554 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5555 #endif
5557 char subfolder[PATH_MAX+1];
5558 tor_snprintf(subfolder, sizeof(subfolder), "%s"PATH_SEPARATOR"%s", torrcd,
5559 "subfolder");
5561 #ifdef _WIN32
5562 tt_int_op(mkdir(subfolder), OP_EQ, 0);
5563 #else
5564 tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
5565 #endif
5567 char path[PATH_MAX+1];
5568 tor_snprintf(path, sizeof(path), "%s"PATH_SEPARATOR"%s", subfolder,
5569 "01_file_in_subfolder");
5570 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5572 char empty[PATH_MAX+1];
5573 tor_snprintf(empty, sizeof(empty), "%s"PATH_SEPARATOR"%s", torrcd, "empty");
5574 tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
5576 char file[PATH_MAX+1];
5577 tor_snprintf(file, sizeof(file), "%s"PATH_SEPARATOR"%s", torrcd, "file");
5578 tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
5580 char dot[PATH_MAX+1];
5581 tor_snprintf(dot, sizeof(dot), "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5582 tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
5584 char torrc_contents[1000];
5585 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5586 "%%include %s\n",
5587 torrcd);
5589 int include_used;
5590 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5591 opened_files), OP_EQ, 0);
5592 tt_ptr_op(result, OP_NE, NULL);
5593 tt_int_op(include_used, OP_EQ, 1);
5595 tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
5596 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
5597 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
5598 // files inside subfolders are not opended, only the subfolder is opened
5599 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
5600 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
5601 // dot files are not opened as we ignore them when we get their name from
5602 // their parent folder
5604 done:
5605 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
5606 smartlist_free(opened_files);
5607 config_free_lines(result);
5608 tor_free(dir);
5611 static void
5612 test_config_compute_max_mem_in_queues(void *data)
5614 #define GIGABYTE(x) (U64_LITERAL(x) << 30)
5615 #define MEGABYTE(x) (U64_LITERAL(x) << 20)
5616 (void)data;
5617 MOCK(get_total_system_memory, get_total_system_memory_mock);
5619 /* We are unable to detect the amount of memory on the system. Tor will try
5620 * to use some sensible default values for 64-bit and 32-bit systems. */
5621 total_system_memory_return = -1;
5623 #if SIZEOF_VOID_P >= 8
5624 /* We are on a 64-bit system. */
5625 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
5626 #else
5627 /* We are on a 32-bit system. */
5628 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
5629 #endif
5631 /* We are able to detect the amount of RAM on the system. */
5632 total_system_memory_return = 0;
5634 /* We are running on a system with one gigabyte of RAM. */
5635 total_system_memory_output = GIGABYTE(1);
5637 /* We have 0.75 * RAM available. */
5638 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5639 3 * (GIGABYTE(1) / 4));
5641 /* We are running on a tiny machine with 256 MB of RAM. */
5642 total_system_memory_output = MEGABYTE(256);
5644 /* We will now enforce a minimum of 256 MB of RAM available for the
5645 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
5646 * MB available. */
5647 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
5649 #if SIZEOF_SIZE_T > 4
5650 /* We are running on a machine with 8 GB of RAM. */
5651 total_system_memory_output = GIGABYTE(8);
5653 /* We will have 0.4 * RAM available. */
5654 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5655 2 * (GIGABYTE(8) / 5));
5657 /* We are running on a machine with 16 GB of RAM. */
5658 total_system_memory_output = GIGABYTE(16);
5660 /* We will have 0.4 * RAM available. */
5661 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5662 2 * (GIGABYTE(16) / 5));
5664 /* We are running on a machine with 32 GB of RAM. */
5665 total_system_memory_output = GIGABYTE(32);
5667 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
5668 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5669 MAX_DEFAULT_MEMORY_QUEUE_SIZE);
5670 #endif
5672 done:
5673 UNMOCK(get_total_system_memory);
5675 #undef GIGABYTE
5676 #undef MEGABYTE
5679 #define CONFIG_TEST(name, flags) \
5680 { #name, test_config_ ## name, flags, NULL, NULL }
5682 struct testcase_t config_tests[] = {
5683 CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
5684 CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
5685 CONFIG_TEST(parsing_trusted_dir_server, 0),
5686 CONFIG_TEST(parsing_fallback_dir_server, 0),
5687 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
5688 CONFIG_TEST(adding_dir_servers, TT_FORK),
5689 CONFIG_TEST(default_dir_servers, TT_FORK),
5690 CONFIG_TEST(default_fallback_dirs, 0),
5691 CONFIG_TEST(resolve_my_address, TT_FORK),
5692 CONFIG_TEST(addressmap, 0),
5693 CONFIG_TEST(parse_bridge_line, 0),
5694 CONFIG_TEST(parse_transport_options_line, 0),
5695 CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
5696 CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
5697 CONFIG_TEST(write_to_data_subdir, TT_FORK),
5698 CONFIG_TEST(fix_my_family, 0),
5699 CONFIG_TEST(directory_fetch, 0),
5700 CONFIG_TEST(port_cfg_line_extract_addrport, 0),
5701 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
5702 CONFIG_TEST(parse_port_config__ports__server_options, 0),
5703 CONFIG_TEST(parse_port_config__ports__ports_given, 0),
5704 CONFIG_TEST(parse_log_severity, 0),
5705 CONFIG_TEST(include_limit, 0),
5706 CONFIG_TEST(include_does_not_exist, 0),
5707 CONFIG_TEST(include_error_in_included_file, 0),
5708 CONFIG_TEST(include_empty_file_folder, 0),
5709 #ifndef _WIN32
5710 CONFIG_TEST(include_no_permission, 0),
5711 #endif
5712 CONFIG_TEST(include_recursion_before_after, 0),
5713 CONFIG_TEST(include_recursion_after_only, 0),
5714 CONFIG_TEST(include_folder_order, 0),
5715 CONFIG_TEST(include_path_syntax, 0),
5716 CONFIG_TEST(include_not_processed, 0),
5717 CONFIG_TEST(include_has_include, 0),
5718 CONFIG_TEST(include_flag_both_without, TT_FORK),
5719 CONFIG_TEST(include_flag_torrc_only, TT_FORK),
5720 CONFIG_TEST(include_flag_defaults_only, TT_FORK),
5721 CONFIG_TEST(dup_and_filter, 0),
5722 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
5723 CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
5724 CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
5725 CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
5726 CONFIG_TEST(include_opened_file_list, 0),
5727 CONFIG_TEST(compute_max_mem_in_queues, 0),
5728 END_OF_TESTCASES