Edit changelog a little for clarity and conciseness
[tor.git] / src / test / test_config.c
blob90ea4da87d90643afaea017527d6647c9c340855
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, 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 "circuitmux_ewma.h"
15 #include "circuitbuild.h"
16 #include "config.h"
17 #include "confparse.h"
18 #include "connection.h"
19 #include "connection_edge.h"
20 #include "test.h"
21 #include "util.h"
22 #include "address.h"
23 #include "connection_or.h"
24 #include "control.h"
25 #include "cpuworker.h"
26 #include "dirserv.h"
27 #include "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"
44 #include "test.h"
45 #include "transports.h"
46 #include "util.h"
48 static void
49 test_config_addressmap(void *arg)
51 char buf[1024];
52 char address[256];
53 time_t expires = TIME_MAX;
54 (void)arg;
56 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
57 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
58 "MapAddress *.google.com *.torserver.exit\n"
59 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
60 "MapAddress *.cn.com www.cnn.com\n"
61 "MapAddress *.cnn.com www.cnn.com\n"
62 "MapAddress ex.com www.cnn.com\n"
63 "MapAddress ey.com *.cnn.com\n"
64 "MapAddress www.torproject.org 1.1.1.1\n"
65 "MapAddress other.torproject.org "
66 "this.torproject.org.otherserver.exit\n"
67 "MapAddress test.torproject.org 2.2.2.2\n"
68 "MapAddress www.google.com 3.3.3.3\n"
69 "MapAddress www.example.org 4.4.4.4\n"
70 "MapAddress 4.4.4.4 7.7.7.7\n"
71 "MapAddress 4.4.4.4 5.5.5.5\n"
72 "MapAddress www.infiniteloop.org 6.6.6.6\n"
73 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
74 , sizeof(buf));
76 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
77 config_register_addressmaps(get_options());
79 /* Use old interface for now, so we don't need to rewrite the unit tests */
80 #define addressmap_rewrite(a,s,eo,ao) \
81 addressmap_rewrite((a),(s), ~0, (eo),(ao))
83 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
84 strlcpy(address, "www.invalidwildcard.com", sizeof(address));
85 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
87 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
88 strlcpy(address, "www.invalidasterisk.com", sizeof(address));
89 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
91 /* Where no mapping for FQDN match on top-level domain */
92 /* MapAddress .google.com .torserver.exit */
93 strlcpy(address, "reader.google.com", sizeof(address));
94 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
95 tt_str_op(address,OP_EQ, "reader.torserver.exit");
97 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
98 strlcpy(address, "reader.yahoo.com", sizeof(address));
99 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
100 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
102 /*MapAddress *.cnn.com www.cnn.com */
103 strlcpy(address, "cnn.com", sizeof(address));
104 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
105 tt_str_op(address,OP_EQ, "www.cnn.com");
107 /* MapAddress .cn.com www.cnn.com */
108 strlcpy(address, "www.cn.com", sizeof(address));
109 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
110 tt_str_op(address,OP_EQ, "www.cnn.com");
112 /* MapAddress ex.com www.cnn.com - no match */
113 strlcpy(address, "www.ex.com", sizeof(address));
114 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
116 /* MapAddress ey.com *.cnn.com - invalid expression */
117 strlcpy(address, "ey.com", sizeof(address));
118 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
120 /* Where mapping for FQDN match on FQDN */
121 strlcpy(address, "www.google.com", sizeof(address));
122 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
123 tt_str_op(address,OP_EQ, "3.3.3.3");
125 strlcpy(address, "www.torproject.org", sizeof(address));
126 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
127 tt_str_op(address,OP_EQ, "1.1.1.1");
129 strlcpy(address, "other.torproject.org", sizeof(address));
130 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
131 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
133 strlcpy(address, "test.torproject.org", sizeof(address));
134 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
135 tt_str_op(address,OP_EQ, "2.2.2.2");
137 /* Test a chain of address mappings and the order in which they were added:
138 "MapAddress www.example.org 4.4.4.4"
139 "MapAddress 4.4.4.4 7.7.7.7"
140 "MapAddress 4.4.4.4 5.5.5.5"
142 strlcpy(address, "www.example.org", sizeof(address));
143 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
144 tt_str_op(address,OP_EQ, "5.5.5.5");
146 /* Test infinite address mapping results in no change */
147 strlcpy(address, "www.infiniteloop.org", sizeof(address));
148 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
149 tt_str_op(address,OP_EQ, "www.infiniteloop.org");
151 /* Test we don't find false positives */
152 strlcpy(address, "www.example.com", sizeof(address));
153 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
155 /* Test top-level-domain matching a bit harder */
156 config_free_lines(get_options_mutable()->AddressMap);
157 addressmap_clear_configured();
158 strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
159 "MapAddress *.torproject.org 1.1.1.1\n"
160 "MapAddress *.net 2.2.2.2\n"
161 , sizeof(buf));
162 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
163 config_register_addressmaps(get_options());
165 strlcpy(address, "www.abc.com", sizeof(address));
166 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
167 tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
169 strlcpy(address, "www.def.com", sizeof(address));
170 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
171 tt_str_op(address,OP_EQ, "www.def.torserver.exit");
173 strlcpy(address, "www.torproject.org", sizeof(address));
174 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
175 tt_str_op(address,OP_EQ, "1.1.1.1");
177 strlcpy(address, "test.torproject.org", sizeof(address));
178 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
179 tt_str_op(address,OP_EQ, "1.1.1.1");
181 strlcpy(address, "torproject.net", sizeof(address));
182 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
183 tt_str_op(address,OP_EQ, "2.2.2.2");
185 /* We don't support '*' as a mapping directive */
186 config_free_lines(get_options_mutable()->AddressMap);
187 addressmap_clear_configured();
188 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
189 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
190 config_register_addressmaps(get_options());
192 strlcpy(address, "www.abc.com", sizeof(address));
193 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
195 strlcpy(address, "www.def.net", sizeof(address));
196 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
198 strlcpy(address, "www.torproject.org", sizeof(address));
199 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
201 #undef addressmap_rewrite
203 done:
204 config_free_lines(get_options_mutable()->AddressMap);
205 get_options_mutable()->AddressMap = NULL;
206 addressmap_free_all();
209 static int
210 is_private_dir(const char* path)
212 struct stat st;
213 int r = stat(path, &st);
214 if (r) {
215 return 0;
217 #if !defined (_WIN32)
218 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
219 return 0;
221 #endif
222 return 1;
225 static void
226 test_config_check_or_create_data_subdir(void *arg)
228 or_options_t *options = get_options_mutable();
229 char *datadir;
230 const char *subdir = "test_stats";
231 char *subpath;
232 struct stat st;
233 int r;
234 #if !defined (_WIN32)
235 unsigned group_permission;
236 #endif
237 (void)arg;
239 tor_free(options->DataDirectory);
240 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
241 subpath = get_datadir_fname(subdir);
243 #if defined (_WIN32)
244 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
245 #else
246 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
247 #endif
249 r = stat(subpath, &st);
251 // The subdirectory shouldn't exist yet,
252 // but should be created by the call to check_or_create_data_subdir.
253 tt_assert(r && (errno == ENOENT));
254 tt_assert(!check_or_create_data_subdir(subdir));
255 tt_assert(is_private_dir(subpath));
257 // The check should return 0, if the directory already exists
258 // and is private to the user.
259 tt_assert(!check_or_create_data_subdir(subdir));
261 r = stat(subpath, &st);
262 if (r) {
263 tt_abort_perror("stat");
266 #if !defined (_WIN32)
267 group_permission = st.st_mode | 0070;
268 r = chmod(subpath, group_permission);
270 if (r) {
271 tt_abort_perror("chmod");
274 // If the directory exists, but its mode is too permissive
275 // a call to check_or_create_data_subdir should reset the mode.
276 tt_assert(!is_private_dir(subpath));
277 tt_assert(!check_or_create_data_subdir(subdir));
278 tt_assert(is_private_dir(subpath));
279 #endif
281 done:
282 rmdir(subpath);
283 tor_free(datadir);
284 tor_free(subpath);
287 static void
288 test_config_write_to_data_subdir(void *arg)
290 or_options_t* options = get_options_mutable();
291 char *datadir;
292 char *cp = NULL;
293 const char* subdir = "test_stats";
294 const char* fname = "test_file";
295 const char* str =
296 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
297 "elitr, sed diam nonumy eirmod\n"
298 "tempor invidunt ut labore et dolore magna aliquyam\n"
299 "erat, sed diam voluptua.\n"
300 "At vero eos et accusam et justo duo dolores et ea\n"
301 "rebum. Stet clita kasd gubergren,\n"
302 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
303 "Lorem ipsum dolor sit amet,\n"
304 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
305 "tempor invidunt ut labore et dolore\n"
306 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
307 "accusam et justo duo dolores et\n"
308 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
309 "sanctus est Lorem ipsum dolor sit amet.";
310 char* filepath = NULL;
311 (void)arg;
313 tor_free(options->DataDirectory);
314 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
315 filepath = get_datadir_fname2(subdir, fname);
317 #if defined (_WIN32)
318 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
319 #else
320 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
321 #endif
323 // Write attempt shoudl fail, if subdirectory doesn't exist.
324 tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
325 tt_assert(! check_or_create_data_subdir(subdir));
327 // Content of file after write attempt should be
328 // equal to the original string.
329 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
330 cp = read_file_to_str(filepath, 0, NULL);
331 tt_str_op(cp,OP_EQ, str);
332 tor_free(cp);
334 // A second write operation should overwrite the old content.
335 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
336 cp = read_file_to_str(filepath, 0, NULL);
337 tt_str_op(cp,OP_EQ, str);
338 tor_free(cp);
340 done:
341 (void) unlink(filepath);
342 rmdir(options->DataDirectory);
343 tor_free(datadir);
344 tor_free(filepath);
345 tor_free(cp);
348 /* Test helper function: Make sure that a bridge line gets parsed
349 * properly. Also make sure that the resulting bridge_line_t structure
350 * has its fields set correctly. */
351 static void
352 good_bridge_line_test(const char *string, const char *test_addrport,
353 const char *test_digest, const char *test_transport,
354 const smartlist_t *test_socks_args)
356 char *tmp = NULL;
357 bridge_line_t *bridge_line = parse_bridge_line(string);
358 tt_assert(bridge_line);
360 /* test addrport */
361 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
362 tt_str_op(test_addrport,OP_EQ, tmp);
363 tor_free(tmp);
365 /* If we were asked to validate a digest, but we did not get a
366 digest after parsing, we failed. */
367 if (test_digest && tor_digest_is_zero(bridge_line->digest))
368 tt_assert(0);
370 /* If we were not asked to validate a digest, and we got a digest
371 after parsing, we failed again. */
372 if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
373 tt_assert(0);
375 /* If we were asked to validate a digest, and we got a digest after
376 parsing, make sure it's correct. */
377 if (test_digest) {
378 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
379 tor_strlower(tmp);
380 tt_str_op(test_digest,OP_EQ, tmp);
381 tor_free(tmp);
384 /* If we were asked to validate a transport name, make sure tha it
385 matches with the transport name that was parsed. */
386 if (test_transport && !bridge_line->transport_name)
387 tt_assert(0);
388 if (!test_transport && bridge_line->transport_name)
389 tt_assert(0);
390 if (test_transport)
391 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
393 /* Validate the SOCKS argument smartlist. */
394 if (test_socks_args && !bridge_line->socks_args)
395 tt_assert(0);
396 if (!test_socks_args && bridge_line->socks_args)
397 tt_assert(0);
398 if (test_socks_args)
399 tt_assert(smartlist_strings_eq(test_socks_args,
400 bridge_line->socks_args));
402 done:
403 tor_free(tmp);
404 bridge_line_free(bridge_line);
407 /* Test helper function: Make sure that a bridge line is
408 * unparseable. */
409 static void
410 bad_bridge_line_test(const char *string)
412 bridge_line_t *bridge_line = parse_bridge_line(string);
413 if (bridge_line)
414 TT_FAIL(("%s was supposed to fail, but it didn't.", string));
415 tt_assert(!bridge_line);
417 done:
418 bridge_line_free(bridge_line);
421 static void
422 test_config_parse_bridge_line(void *arg)
424 (void) arg;
425 good_bridge_line_test("192.0.2.1:4123",
426 "192.0.2.1:4123", NULL, NULL, NULL);
428 good_bridge_line_test("192.0.2.1",
429 "192.0.2.1:443", NULL, NULL, NULL);
431 good_bridge_line_test("transport [::1]",
432 "[::1]:443", NULL, "transport", NULL);
434 good_bridge_line_test("transport 192.0.2.1:12 "
435 "4352e58420e68f5e40bf7c74faddccd9d1349413",
436 "192.0.2.1:12",
437 "4352e58420e68f5e40bf7c74faddccd9d1349413",
438 "transport", NULL);
441 smartlist_t *sl_tmp = smartlist_new();
442 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
444 good_bridge_line_test("transport 192.0.2.1:12 "
445 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
446 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
447 "transport", sl_tmp);
449 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
450 smartlist_free(sl_tmp);
454 smartlist_t *sl_tmp = smartlist_new();
455 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
456 smartlist_add_asprintf(sl_tmp, "z=z");
458 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
459 "192.0.2.1:12", NULL, "transport", sl_tmp);
461 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
462 smartlist_free(sl_tmp);
466 smartlist_t *sl_tmp = smartlist_new();
467 smartlist_add_asprintf(sl_tmp, "dub=come");
468 smartlist_add_asprintf(sl_tmp, "save=me");
470 good_bridge_line_test("transport 192.0.2.1:12 "
471 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
472 "dub=come save=me",
474 "192.0.2.1:12",
475 "4352e58420e68f5e40bf7c74faddccd9d1349666",
476 "transport", sl_tmp);
478 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
479 smartlist_free(sl_tmp);
482 good_bridge_line_test("192.0.2.1:1231 "
483 "4352e58420e68f5e40bf7c74faddccd9d1349413",
484 "192.0.2.1:1231",
485 "4352e58420e68f5e40bf7c74faddccd9d1349413",
486 NULL, NULL);
488 /* Empty line */
489 bad_bridge_line_test("");
490 /* bad transport name */
491 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
492 /* weird ip address */
493 bad_bridge_line_test("a.b.c.d");
494 /* invalid fpr */
495 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
496 /* no k=v in the end */
497 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
498 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
499 /* no addrport */
500 bad_bridge_line_test("asdw");
501 /* huge k=v value that can't fit in SOCKS fields */
502 bad_bridge_line_test(
503 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
504 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
505 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
506 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
507 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
508 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
510 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
511 "aa=b");
514 static void
515 test_config_parse_transport_options_line(void *arg)
517 smartlist_t *options_sl = NULL, *sl_tmp = NULL;
519 (void) arg;
521 { /* too small line */
522 options_sl = get_options_from_transport_options_line("valley", NULL);
523 tt_assert(!options_sl);
526 { /* no k=v values */
527 options_sl = get_options_from_transport_options_line("hit it!", NULL);
528 tt_assert(!options_sl);
531 { /* correct line, but wrong transport specified */
532 options_sl =
533 get_options_from_transport_options_line("trebuchet k=v", "rook");
534 tt_assert(!options_sl);
537 { /* correct -- no transport specified */
538 sl_tmp = smartlist_new();
539 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
540 smartlist_add_asprintf(sl_tmp, "weliketo=party");
542 options_sl =
543 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
544 NULL);
545 tt_assert(options_sl);
546 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
548 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
549 smartlist_free(sl_tmp);
550 sl_tmp = NULL;
551 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
552 smartlist_free(options_sl);
553 options_sl = NULL;
556 { /* correct -- correct transport specified */
557 sl_tmp = smartlist_new();
558 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
559 smartlist_add_asprintf(sl_tmp, "weliketo=party");
561 options_sl =
562 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
563 "rook");
564 tt_assert(options_sl);
565 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
566 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
567 smartlist_free(sl_tmp);
568 sl_tmp = NULL;
569 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
570 smartlist_free(options_sl);
571 options_sl = NULL;
574 done:
575 if (options_sl) {
576 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
577 smartlist_free(options_sl);
579 if (sl_tmp) {
580 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
581 smartlist_free(sl_tmp);
585 /* Mocks needed for the transport plugin line test */
587 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
588 char **proxy_argv, int is_server);
589 static int transport_add_from_config_mock(const tor_addr_t *addr,
590 uint16_t port, const char *name,
591 int socks_ver);
592 static int transport_is_needed_mock(const char *transport_name);
594 static int pt_kickstart_proxy_mock_call_count = 0;
595 static int transport_add_from_config_mock_call_count = 0;
596 static int transport_is_needed_mock_call_count = 0;
597 static int transport_is_needed_mock_return = 0;
599 static void
600 pt_kickstart_proxy_mock(const smartlist_t *transport_list,
601 char **proxy_argv, int is_server)
603 (void) transport_list;
604 (void) proxy_argv;
605 (void) is_server;
606 /* XXXX check that args are as expected. */
608 ++pt_kickstart_proxy_mock_call_count;
610 free_execve_args(proxy_argv);
613 static int
614 transport_add_from_config_mock(const tor_addr_t *addr,
615 uint16_t port, const char *name,
616 int socks_ver)
618 (void) addr;
619 (void) port;
620 (void) name;
621 (void) socks_ver;
622 /* XXXX check that args are as expected. */
624 ++transport_add_from_config_mock_call_count;
626 return 0;
629 static int
630 transport_is_needed_mock(const char *transport_name)
632 (void) transport_name;
633 /* XXXX check that arg is as expected. */
635 ++transport_is_needed_mock_call_count;
637 return transport_is_needed_mock_return;
641 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
642 * options.
645 static void
646 test_config_parse_transport_plugin_line(void *arg)
648 (void)arg;
650 or_options_t *options = get_options_mutable();
651 int r, tmp;
652 int old_pt_kickstart_proxy_mock_call_count;
653 int old_transport_add_from_config_mock_call_count;
654 int old_transport_is_needed_mock_call_count;
656 /* Bad transport lines - too short */
657 r = parse_transport_line(options, "bad", 1, 0);
658 tt_assert(r < 0);
659 r = parse_transport_line(options, "bad", 1, 1);
660 tt_assert(r < 0);
661 r = parse_transport_line(options, "bad bad", 1, 0);
662 tt_assert(r < 0);
663 r = parse_transport_line(options, "bad bad", 1, 1);
664 tt_assert(r < 0);
666 /* Test transport list parsing */
667 r = parse_transport_line(options,
668 "transport_1 exec /usr/bin/fake-transport", 1, 0);
669 tt_assert(r == 0);
670 r = parse_transport_line(options,
671 "transport_1 exec /usr/bin/fake-transport", 1, 1);
672 tt_assert(r == 0);
673 r = parse_transport_line(options,
674 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
675 tt_assert(r == 0);
676 r = parse_transport_line(options,
677 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
678 tt_assert(r == 0);
679 /* Bad transport identifiers */
680 r = parse_transport_line(options,
681 "transport_* exec /usr/bin/fake-transport", 1, 0);
682 tt_assert(r < 0);
683 r = parse_transport_line(options,
684 "transport_* exec /usr/bin/fake-transport", 1, 1);
685 tt_assert(r < 0);
687 /* Check SOCKS cases for client transport */
688 r = parse_transport_line(options,
689 "transport_1 socks4 1.2.3.4:567", 1, 0);
690 tt_assert(r == 0);
691 r = parse_transport_line(options,
692 "transport_1 socks5 1.2.3.4:567", 1, 0);
693 tt_assert(r == 0);
694 /* Proxy case for server transport */
695 r = parse_transport_line(options,
696 "transport_1 proxy 1.2.3.4:567", 1, 1);
697 tt_assert(r == 0);
698 /* Multiple-transport error exit */
699 r = parse_transport_line(options,
700 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
701 tt_assert(r < 0);
702 r = parse_transport_line(options,
703 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
704 /* No port error exit */
705 r = parse_transport_line(options,
706 "transport_1 socks5 1.2.3.4", 1, 0);
707 tt_assert(r < 0);
708 r = parse_transport_line(options,
709 "transport_1 proxy 1.2.3.4", 1, 1);
710 tt_assert(r < 0);
711 /* Unparsable address error exit */
712 r = parse_transport_line(options,
713 "transport_1 socks5 1.2.3:6x7", 1, 0);
714 tt_assert(r < 0);
715 r = parse_transport_line(options,
716 "transport_1 proxy 1.2.3:6x7", 1, 1);
717 tt_assert(r < 0);
719 /* "Strange {Client|Server}TransportPlugin field" error exit */
720 r = parse_transport_line(options,
721 "transport_1 foo bar", 1, 0);
722 tt_assert(r < 0);
723 r = parse_transport_line(options,
724 "transport_1 foo bar", 1, 1);
725 tt_assert(r < 0);
727 /* No sandbox mode error exit */
728 tmp = options->Sandbox;
729 options->Sandbox = 1;
730 r = parse_transport_line(options,
731 "transport_1 exec /usr/bin/fake-transport", 1, 0);
732 tt_assert(r < 0);
733 r = parse_transport_line(options,
734 "transport_1 exec /usr/bin/fake-transport", 1, 1);
735 tt_assert(r < 0);
736 options->Sandbox = tmp;
739 * These final test cases cover code paths that only activate without
740 * validate_only, so they need mocks in place.
742 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
743 old_pt_kickstart_proxy_mock_call_count =
744 pt_kickstart_proxy_mock_call_count;
745 r = parse_transport_line(options,
746 "transport_1 exec /usr/bin/fake-transport", 0, 1);
747 tt_assert(r == 0);
748 tt_assert(pt_kickstart_proxy_mock_call_count ==
749 old_pt_kickstart_proxy_mock_call_count + 1);
750 UNMOCK(pt_kickstart_proxy);
752 /* This one hits a log line in the !validate_only case only */
753 r = parse_transport_line(options,
754 "transport_1 proxy 1.2.3.4:567", 0, 1);
755 tt_assert(r == 0);
757 /* Check mocked client transport cases */
758 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
759 MOCK(transport_add_from_config, transport_add_from_config_mock);
760 MOCK(transport_is_needed, transport_is_needed_mock);
762 /* Unnecessary transport case */
763 transport_is_needed_mock_return = 0;
764 old_pt_kickstart_proxy_mock_call_count =
765 pt_kickstart_proxy_mock_call_count;
766 old_transport_add_from_config_mock_call_count =
767 transport_add_from_config_mock_call_count;
768 old_transport_is_needed_mock_call_count =
769 transport_is_needed_mock_call_count;
770 r = parse_transport_line(options,
771 "transport_1 exec /usr/bin/fake-transport", 0, 0);
772 /* Should have succeeded */
773 tt_assert(r == 0);
774 /* transport_is_needed() should have been called */
775 tt_assert(transport_is_needed_mock_call_count ==
776 old_transport_is_needed_mock_call_count + 1);
778 * pt_kickstart_proxy() and transport_add_from_config() should
779 * not have been called.
781 tt_assert(pt_kickstart_proxy_mock_call_count ==
782 old_pt_kickstart_proxy_mock_call_count);
783 tt_assert(transport_add_from_config_mock_call_count ==
784 old_transport_add_from_config_mock_call_count);
786 /* Necessary transport case */
787 transport_is_needed_mock_return = 1;
788 old_pt_kickstart_proxy_mock_call_count =
789 pt_kickstart_proxy_mock_call_count;
790 old_transport_add_from_config_mock_call_count =
791 transport_add_from_config_mock_call_count;
792 old_transport_is_needed_mock_call_count =
793 transport_is_needed_mock_call_count;
794 r = parse_transport_line(options,
795 "transport_1 exec /usr/bin/fake-transport", 0, 0);
796 /* Should have succeeded */
797 tt_assert(r == 0);
799 * transport_is_needed() and pt_kickstart_proxy() should have been
800 * called.
802 tt_assert(pt_kickstart_proxy_mock_call_count ==
803 old_pt_kickstart_proxy_mock_call_count + 1);
804 tt_assert(transport_is_needed_mock_call_count ==
805 old_transport_is_needed_mock_call_count + 1);
806 /* transport_add_from_config() should not have been called. */
807 tt_assert(transport_add_from_config_mock_call_count ==
808 old_transport_add_from_config_mock_call_count);
810 /* proxy case */
811 transport_is_needed_mock_return = 1;
812 old_pt_kickstart_proxy_mock_call_count =
813 pt_kickstart_proxy_mock_call_count;
814 old_transport_add_from_config_mock_call_count =
815 transport_add_from_config_mock_call_count;
816 old_transport_is_needed_mock_call_count =
817 transport_is_needed_mock_call_count;
818 r = parse_transport_line(options,
819 "transport_1 socks5 1.2.3.4:567", 0, 0);
820 /* Should have succeeded */
821 tt_assert(r == 0);
823 * transport_is_needed() and transport_add_from_config() should have
824 * been called.
826 tt_assert(transport_add_from_config_mock_call_count ==
827 old_transport_add_from_config_mock_call_count + 1);
828 tt_assert(transport_is_needed_mock_call_count ==
829 old_transport_is_needed_mock_call_count + 1);
830 /* pt_kickstart_proxy() should not have been called. */
831 tt_assert(pt_kickstart_proxy_mock_call_count ==
832 old_pt_kickstart_proxy_mock_call_count);
834 /* Done with mocked client transport cases */
835 UNMOCK(transport_is_needed);
836 UNMOCK(transport_add_from_config);
837 UNMOCK(pt_kickstart_proxy);
839 done:
840 /* Make sure we undo all mocks */
841 UNMOCK(pt_kickstart_proxy);
842 UNMOCK(transport_add_from_config);
843 UNMOCK(transport_is_needed);
845 return;
848 // Tests if an options with MyFamily fingerprints missing '$' normalises
849 // them correctly and also ensure it also works with multiple fingerprints
850 static void
851 test_config_fix_my_family(void *arg)
853 char *err = NULL;
854 const char *family = "$1111111111111111111111111111111111111111, "
855 "1111111111111111111111111111111111111112, "
856 "$1111111111111111111111111111111111111113";
858 or_options_t* options = options_new();
859 or_options_t* defaults = options_new();
860 (void) arg;
862 options_init(options);
863 options_init(defaults);
864 options->MyFamily = tor_strdup(family);
866 options_validate(NULL, options, defaults, 0, &err) ;
868 if (err != NULL) {
869 TT_FAIL(("options_validate failed: %s", err));
872 tt_str_op(options->MyFamily,OP_EQ,
873 "$1111111111111111111111111111111111111111, "
874 "$1111111111111111111111111111111111111112, "
875 "$1111111111111111111111111111111111111113");
877 done:
878 if (err != NULL) {
879 tor_free(err);
882 or_options_free(options);
883 or_options_free(defaults);
886 static int n_hostname_01010101 = 0;
888 /** This mock function is meant to replace tor_lookup_hostname().
889 * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
890 * This function increments <b>n_hostname_01010101</b> counter by one
891 * every time it is called.
893 static int
894 tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
896 n_hostname_01010101++;
898 if (name && addr) {
899 *addr = ntohl(0x01010101);
902 return 0;
905 static int n_hostname_localhost = 0;
907 /** This mock function is meant to replace tor_lookup_hostname().
908 * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
909 * This function increments <b>n_hostname_localhost</b> counter by one
910 * every time it is called.
912 static int
913 tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
915 n_hostname_localhost++;
917 if (name && addr) {
918 *addr = 0x7f000001;
921 return 0;
924 static int n_hostname_failure = 0;
926 /** This mock function is meant to replace tor_lookup_hostname().
927 * It pretends to fail by returning -1 to caller. Also, this function
928 * increments <b>n_hostname_failure</b> every time it is called.
930 static int
931 tor_lookup_hostname_failure(const char *name, uint32_t *addr)
933 (void)name;
934 (void)addr;
936 n_hostname_failure++;
938 return -1;
941 static int n_gethostname_replacement = 0;
943 /** This mock function is meant to replace tor_gethostname(). It
944 * responds with string "onionrouter!" as hostname. This function
945 * increments <b>n_gethostname_replacement</b> by one every time
946 * it is called.
948 static int
949 tor_gethostname_replacement(char *name, size_t namelen)
951 n_gethostname_replacement++;
953 if (name && namelen) {
954 strlcpy(name,"onionrouter!",namelen);
957 return 0;
960 static int n_gethostname_localhost = 0;
962 /** This mock function is meant to replace tor_gethostname(). It
963 * responds with string "127.0.0.1" as hostname. This function
964 * increments <b>n_gethostname_localhost</b> by one every time
965 * it is called.
967 static int
968 tor_gethostname_localhost(char *name, size_t namelen)
970 n_gethostname_localhost++;
972 if (name && namelen) {
973 strlcpy(name,"127.0.0.1",namelen);
976 return 0;
979 static int n_gethostname_failure = 0;
981 /** This mock function is meant to replace tor_gethostname.
982 * It pretends to fail by returning -1. This function increments
983 * <b>n_gethostname_failure</b> by one every time it is called.
985 static int
986 tor_gethostname_failure(char *name, size_t namelen)
988 (void)name;
989 (void)namelen;
990 n_gethostname_failure++;
992 return -1;
995 static int n_get_interface_address = 0;
997 /** This mock function is meant to replace get_interface_address().
998 * It answers with address 8.8.8.8. This function increments
999 * <b>n_get_interface_address</b> by one every time it is called.
1001 static int
1002 get_interface_address_08080808(int severity, uint32_t *addr)
1004 (void)severity;
1006 n_get_interface_address++;
1008 if (addr) {
1009 *addr = ntohl(0x08080808);
1012 return 0;
1015 static int n_get_interface_address6 = 0;
1016 static sa_family_t last_address6_family;
1018 /** This mock function is meant to replace get_interface_address6().
1019 * It answers with IP address 9.9.9.9 iff both of the following are true:
1020 * - <b>family</b> is AF_INET
1021 * - <b>addr</b> pointer is not NULL.
1022 * This function increments <b>n_get_interface_address6</b> by one every
1023 * time it is called.
1025 static int
1026 get_interface_address6_replacement(int severity, sa_family_t family,
1027 tor_addr_t *addr)
1029 (void)severity;
1031 last_address6_family = family;
1032 n_get_interface_address6++;
1034 if ((family != AF_INET) || !addr) {
1035 return -1;
1038 tor_addr_from_ipv4h(addr,0x09090909);
1040 return 0;
1043 static int n_get_interface_address_failure = 0;
1046 * This mock function is meant to replace get_interface_address().
1047 * It pretends to fail getting interface address by returning -1.
1048 * <b>n_get_interface_address_failure</b> is incremented by one
1049 * every time this function is called.
1051 static int
1052 get_interface_address_failure(int severity, uint32_t *addr)
1054 (void)severity;
1055 (void)addr;
1057 n_get_interface_address_failure++;
1059 return -1;
1062 static int n_get_interface_address6_failure = 0;
1065 * This mock function is meant to replace get_interface_addres6().
1066 * It will pretend to fail by return -1.
1067 * <b>n_get_interface_address6_failure</b> is incremented by one
1068 * every time this function is called and <b>last_address6_family</b>
1069 * is assigned the value of <b>family</b> argument.
1071 static int
1072 get_interface_address6_failure(int severity, sa_family_t family,
1073 tor_addr_t *addr)
1075 (void)severity;
1076 (void)addr;
1077 n_get_interface_address6_failure++;
1078 last_address6_family = family;
1080 return -1;
1083 static void
1084 test_config_resolve_my_address(void *arg)
1086 or_options_t *options;
1087 uint32_t resolved_addr;
1088 const char *method_used;
1089 char *hostname_out = NULL;
1090 int retval;
1091 int prev_n_hostname_01010101;
1092 int prev_n_hostname_localhost;
1093 int prev_n_hostname_failure;
1094 int prev_n_gethostname_replacement;
1095 int prev_n_gethostname_failure;
1096 int prev_n_gethostname_localhost;
1097 int prev_n_get_interface_address;
1098 int prev_n_get_interface_address_failure;
1099 int prev_n_get_interface_address6;
1100 int prev_n_get_interface_address6_failure;
1102 (void)arg;
1104 options = options_new();
1106 options_init(options);
1109 * CASE 1:
1110 * If options->Address is a valid IPv4 address string, we want
1111 * the corresponding address to be parsed and returned.
1114 options->Address = tor_strdup("128.52.128.105");
1116 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1117 &method_used,&hostname_out);
1119 tt_want(retval == 0);
1120 tt_want_str_op(method_used,==,"CONFIGURED");
1121 tt_want(hostname_out == NULL);
1122 tt_assert(resolved_addr == 0x80348069);
1124 tor_free(options->Address);
1127 * CASE 2:
1128 * If options->Address is a valid DNS address, we want resolve_my_address()
1129 * function to ask tor_lookup_hostname() for help with resolving it
1130 * and return the address that was resolved (in host order).
1133 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1135 tor_free(options->Address);
1136 options->Address = tor_strdup("www.torproject.org");
1138 prev_n_hostname_01010101 = n_hostname_01010101;
1140 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1141 &method_used,&hostname_out);
1143 tt_want(retval == 0);
1144 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1145 tt_want_str_op(method_used,==,"RESOLVED");
1146 tt_want_str_op(hostname_out,==,"www.torproject.org");
1147 tt_assert(resolved_addr == 0x01010101);
1149 UNMOCK(tor_lookup_hostname);
1151 tor_free(options->Address);
1152 tor_free(hostname_out);
1155 * CASE 3:
1156 * Given that options->Address is NULL, we want resolve_my_address()
1157 * to try and use tor_gethostname() to get hostname AND use
1158 * tor_lookup_hostname() to get IP address.
1161 resolved_addr = 0;
1162 tor_free(options->Address);
1163 options->Address = NULL;
1165 MOCK(tor_gethostname,tor_gethostname_replacement);
1166 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1168 prev_n_gethostname_replacement = n_gethostname_replacement;
1169 prev_n_hostname_01010101 = n_hostname_01010101;
1171 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1172 &method_used,&hostname_out);
1174 tt_want(retval == 0);
1175 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1176 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1177 tt_want_str_op(method_used,==,"GETHOSTNAME");
1178 tt_want_str_op(hostname_out,==,"onionrouter!");
1179 tt_assert(resolved_addr == 0x01010101);
1181 UNMOCK(tor_gethostname);
1182 UNMOCK(tor_lookup_hostname);
1184 tor_free(hostname_out);
1187 * CASE 4:
1188 * Given that options->Address is a local host address, we want
1189 * resolve_my_address() function to fail.
1192 resolved_addr = 0;
1193 tor_free(options->Address);
1194 options->Address = tor_strdup("127.0.0.1");
1196 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1197 &method_used,&hostname_out);
1199 tt_want(resolved_addr == 0);
1200 tt_assert(retval == -1);
1202 tor_free(options->Address);
1203 tor_free(hostname_out);
1206 * CASE 5:
1207 * We want resolve_my_address() to fail if DNS address in options->Address
1208 * cannot be resolved.
1211 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1213 prev_n_hostname_failure = n_hostname_failure;
1215 tor_free(options->Address);
1216 options->Address = tor_strdup("www.tor-project.org");
1218 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1219 &method_used,&hostname_out);
1221 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1222 tt_assert(retval == -1);
1224 UNMOCK(tor_lookup_hostname);
1226 tor_free(options->Address);
1227 tor_free(hostname_out);
1230 * CASE 6:
1231 * If options->Address is NULL AND gettting local hostname fails, we want
1232 * resolve_my_address() to fail as well.
1235 MOCK(tor_gethostname,tor_gethostname_failure);
1237 prev_n_gethostname_failure = n_gethostname_failure;
1239 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1240 &method_used,&hostname_out);
1242 tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
1243 tt_assert(retval == -1);
1245 UNMOCK(tor_gethostname);
1246 tor_free(hostname_out);
1249 * CASE 7:
1250 * We want resolve_my_address() to try and get network interface address via
1251 * get_interface_address() if hostname returned by tor_gethostname() cannot be
1252 * resolved into IP address.
1255 MOCK(tor_gethostname,tor_gethostname_replacement);
1256 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1257 MOCK(get_interface_address,get_interface_address_08080808);
1259 prev_n_gethostname_replacement = n_gethostname_replacement;
1260 prev_n_get_interface_address = n_get_interface_address;
1262 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1263 &method_used,&hostname_out);
1265 tt_want(retval == 0);
1266 tt_want_int_op(n_gethostname_replacement, ==,
1267 prev_n_gethostname_replacement + 1);
1268 tt_want_int_op(n_get_interface_address, ==,
1269 prev_n_get_interface_address + 1);
1270 tt_want_str_op(method_used,==,"INTERFACE");
1271 tt_want(hostname_out == NULL);
1272 tt_assert(resolved_addr == 0x08080808);
1274 UNMOCK(get_interface_address);
1275 tor_free(hostname_out);
1278 * CASE 8:
1279 * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
1280 * is unresolvable. We want resolve_my_address to fail if
1281 * get_interface_address() fails.
1284 MOCK(get_interface_address,get_interface_address_failure);
1286 prev_n_get_interface_address_failure = n_get_interface_address_failure;
1287 prev_n_gethostname_replacement = n_gethostname_replacement;
1289 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1290 &method_used,&hostname_out);
1292 tt_want(n_get_interface_address_failure ==
1293 prev_n_get_interface_address_failure + 1);
1294 tt_want(n_gethostname_replacement ==
1295 prev_n_gethostname_replacement + 1);
1296 tt_assert(retval == -1);
1298 UNMOCK(get_interface_address);
1299 tor_free(hostname_out);
1302 * CASE 9:
1303 * Given that options->Address is NULL AND tor_lookup_hostname()
1304 * fails AND hostname returned by gethostname() resolves
1305 * to local IP address, we want resolve_my_address() function to
1306 * call get_interface_address6(.,AF_INET,.) and return IP address
1307 * the latter function has found.
1310 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1311 MOCK(tor_gethostname,tor_gethostname_replacement);
1312 MOCK(get_interface_address6,get_interface_address6_replacement);
1314 prev_n_gethostname_replacement = n_gethostname_replacement;
1315 prev_n_hostname_failure = n_hostname_failure;
1316 prev_n_get_interface_address6 = n_get_interface_address6;
1318 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1319 &method_used,&hostname_out);
1321 tt_want(last_address6_family == AF_INET);
1322 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1323 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1324 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1325 tt_want(retval == 0);
1326 tt_want_str_op(method_used,==,"INTERFACE");
1327 tt_assert(resolved_addr == 0x09090909);
1329 UNMOCK(tor_lookup_hostname);
1330 UNMOCK(tor_gethostname);
1331 UNMOCK(get_interface_address6);
1333 tor_free(hostname_out);
1336 * CASE 10: We want resolve_my_address() to fail if all of the following
1337 * are true:
1338 * 1. options->Address is not NULL
1339 * 2. ... but it cannot be converted to struct in_addr by
1340 * tor_inet_aton()
1341 * 3. ... and tor_lookup_hostname() fails to resolve the
1342 * options->Address
1345 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1347 prev_n_hostname_failure = n_hostname_failure;
1349 tor_free(options->Address);
1350 options->Address = tor_strdup("some_hostname");
1352 retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
1353 &method_used,&hostname_out);
1355 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1356 tt_assert(retval == -1);
1358 UNMOCK(tor_gethostname);
1359 UNMOCK(tor_lookup_hostname);
1361 tor_free(hostname_out);
1364 * CASE 11:
1365 * Suppose the following sequence of events:
1366 * 1. options->Address is NULL
1367 * 2. tor_gethostname() succeeds to get hostname of machine Tor
1368 * if running on.
1369 * 3. Hostname from previous step cannot be converted to
1370 * address by using tor_inet_aton() function.
1371 * 4. However, tor_lookup_hostname() succeds in resolving the
1372 * hostname from step 2.
1373 * 5. Unfortunately, tor_addr_is_internal() deems this address
1374 * to be internal.
1375 * 6. get_interface_address6(.,AF_INET,.) returns non-internal
1376 * IPv4
1378 * We want resolve_my_addr() to succeed with method "INTERFACE"
1379 * and address from step 6.
1382 tor_free(options->Address);
1383 options->Address = NULL;
1385 MOCK(tor_gethostname,tor_gethostname_replacement);
1386 MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
1387 MOCK(get_interface_address6,get_interface_address6_replacement);
1389 prev_n_gethostname_replacement = n_gethostname_replacement;
1390 prev_n_hostname_localhost = n_hostname_localhost;
1391 prev_n_get_interface_address6 = n_get_interface_address6;
1393 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1394 &method_used,&hostname_out);
1396 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1397 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1398 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1400 tt_str_op(method_used,==,"INTERFACE");
1401 tt_assert(!hostname_out);
1402 tt_assert(retval == 0);
1405 * CASE 11b:
1406 * 1-5 as above.
1407 * 6. get_interface_address6() fails.
1409 * In this subcase, we want resolve_my_address() to fail.
1412 UNMOCK(get_interface_address6);
1413 MOCK(get_interface_address6,get_interface_address6_failure);
1415 prev_n_gethostname_replacement = n_gethostname_replacement;
1416 prev_n_hostname_localhost = n_hostname_localhost;
1417 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1419 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1420 &method_used,&hostname_out);
1422 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1423 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1424 tt_want(n_get_interface_address6_failure ==
1425 prev_n_get_interface_address6_failure + 1);
1427 tt_assert(retval == -1);
1429 UNMOCK(tor_gethostname);
1430 UNMOCK(tor_lookup_hostname);
1431 UNMOCK(get_interface_address6);
1433 /* CASE 12:
1434 * Suppose the following happens:
1435 * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
1436 * 2. tor_gethostname() succeeds in getting hostname of a machine ...
1437 * 3. ... which is successfully parsed by tor_inet_aton() ...
1438 * 4. into IPv4 address that tor_addr_is_inernal() considers to be
1439 * internal.
1441 * In this case, we want resolve_my_address() to fail.
1444 tor_free(options->Address);
1445 options->Address = NULL;
1446 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
1448 MOCK(tor_gethostname,tor_gethostname_localhost);
1450 prev_n_gethostname_localhost = n_gethostname_localhost;
1452 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1453 &method_used,&hostname_out);
1455 tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
1456 tt_assert(retval == -1);
1458 UNMOCK(tor_gethostname);
1460 done:
1461 tor_free(options->Address);
1462 tor_free(options->DirAuthorities);
1463 or_options_free(options);
1464 tor_free(hostname_out);
1466 UNMOCK(tor_gethostname);
1467 UNMOCK(tor_lookup_hostname);
1468 UNMOCK(get_interface_address);
1469 UNMOCK(get_interface_address6);
1470 UNMOCK(tor_gethostname);
1473 static void
1474 test_config_adding_trusted_dir_server(void *arg)
1476 (void)arg;
1478 const char digest[DIGEST_LEN] = "";
1479 dir_server_t *ds = NULL;
1480 tor_addr_port_t ipv6;
1481 int rv = -1;
1483 clear_dir_servers();
1484 routerlist_free_all();
1486 /* create a trusted ds without an IPv6 address and port */
1487 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1488 NULL, V3_DIRINFO, 1.0);
1489 tt_assert(ds);
1490 dir_server_add(ds);
1491 tt_assert(get_n_authorities(V3_DIRINFO) == 1);
1492 tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
1494 /* create a trusted ds with an IPv6 address and port */
1495 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1496 tt_assert(rv == 0);
1497 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
1498 NULL, V3_DIRINFO, 1.0);
1499 tt_assert(ds);
1500 dir_server_add(ds);
1501 tt_assert(get_n_authorities(V3_DIRINFO) == 2);
1502 tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
1504 done:
1505 clear_dir_servers();
1506 routerlist_free_all();
1509 static void
1510 test_config_adding_fallback_dir_server(void *arg)
1512 (void)arg;
1514 const char digest[DIGEST_LEN] = "";
1515 dir_server_t *ds = NULL;
1516 tor_addr_t ipv4;
1517 tor_addr_port_t ipv6;
1518 int rv = -1;
1520 clear_dir_servers();
1521 routerlist_free_all();
1523 rv = tor_addr_parse(&ipv4, "127.0.0.1");
1524 tt_assert(rv == AF_INET);
1526 /* create a trusted ds without an IPv6 address and port */
1527 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
1528 tt_assert(ds);
1529 dir_server_add(ds);
1530 tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
1532 /* create a trusted ds with an IPv6 address and port */
1533 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1534 tt_assert(rv == 0);
1535 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
1536 tt_assert(ds);
1537 dir_server_add(ds);
1538 tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
1540 done:
1541 clear_dir_servers();
1542 routerlist_free_all();
1545 /* No secrets here:
1546 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1547 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1548 * with added spaces
1550 #define TEST_DIR_AUTH_LINE_START \
1551 "foobar orport=12345 " \
1552 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1553 #define TEST_DIR_AUTH_LINE_END \
1554 "1.2.3.4:54321 " \
1555 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1556 #define TEST_DIR_AUTH_IPV6_FLAG \
1557 "ipv6=[feed::beef]:9 "
1559 static void
1560 test_config_parsing_trusted_dir_server(void *arg)
1562 (void)arg;
1563 int rv = -1;
1565 /* parse a trusted dir server without an IPv6 address and port */
1566 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1567 TEST_DIR_AUTH_LINE_END,
1568 V3_DIRINFO, 1);
1569 tt_assert(rv == 0);
1571 /* parse a trusted dir server with an IPv6 address and port */
1572 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1573 TEST_DIR_AUTH_IPV6_FLAG
1574 TEST_DIR_AUTH_LINE_END,
1575 V3_DIRINFO, 1);
1576 tt_assert(rv == 0);
1578 /* Since we are only validating, there is no cleanup. */
1579 done:
1583 #undef TEST_DIR_AUTH_LINE_START
1584 #undef TEST_DIR_AUTH_LINE_END
1585 #undef TEST_DIR_AUTH_IPV6_FLAG
1587 /* No secrets here:
1588 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1590 #define TEST_DIR_FALLBACK_LINE \
1591 "1.2.3.4:54321 orport=12345 " \
1592 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1593 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1594 "ipv6=[2015:c0de::deed]:9"
1596 static void
1597 test_config_parsing_fallback_dir_server(void *arg)
1599 (void)arg;
1600 int rv = -1;
1602 /* parse a trusted dir server without an IPv6 address and port */
1603 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
1604 tt_assert(rv == 0);
1606 /* parse a trusted dir server with an IPv6 address and port */
1607 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1608 TEST_DIR_FALLBACK_IPV6_FLAG,
1610 tt_assert(rv == 0);
1612 /* Since we are only validating, there is no cleanup. */
1613 done:
1617 #undef TEST_DIR_FALLBACK_LINE
1618 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1620 static void
1621 test_config_adding_default_trusted_dir_servers(void *arg)
1623 (void)arg;
1625 clear_dir_servers();
1626 routerlist_free_all();
1628 /* Assume we only have one bridge authority */
1629 add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
1630 tt_assert(get_n_authorities(BRIDGE_DIRINFO) == 1);
1631 tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
1633 /* Assume we have eight V3 authorities */
1634 add_default_trusted_dir_authorities(V3_DIRINFO);
1635 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 8);
1636 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 9);
1638 done:
1639 clear_dir_servers();
1640 routerlist_free_all();
1643 static int n_add_default_fallback_dir_servers_known_default = 0;
1646 * This mock function is meant to replace add_default_fallback_dir_servers().
1647 * It will parse and add one known default fallback dir server,
1648 * which has a dir_port of 99.
1649 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
1650 * one every time this function is called.
1652 static void
1653 add_default_fallback_dir_servers_known_default(void)
1655 int i;
1656 const char *fallback[] = {
1657 "127.0.0.1:60099 orport=9009 "
1658 "id=0923456789012345678901234567890123456789",
1659 NULL
1661 for (i=0; fallback[i]; i++) {
1662 if (parse_dir_fallback_line(fallback[i], 0)<0) {
1663 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
1664 fallback[i]);
1667 n_add_default_fallback_dir_servers_known_default++;
1670 /* Test all the different combinations of adding dir servers */
1671 static void
1672 test_config_adding_dir_servers(void *arg)
1674 (void)arg;
1676 /* allocate options */
1677 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
1679 /* Allocate and populate configuration lines:
1681 * Use the same format as the hard-coded directories in
1682 * add_default_trusted_dir_authorities().
1683 * Zeroing the structure has the same effect as initialising to:
1684 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
1686 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
1687 test_dir_authority->key = tor_strdup("DirAuthority");
1688 test_dir_authority->value = tor_strdup(
1689 "D0 orport=9000 "
1690 "v3ident=0023456789012345678901234567890123456789 "
1691 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1694 config_line_t *test_alt_bridge_authority = tor_malloc_zero(
1695 sizeof(config_line_t));
1696 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
1697 test_alt_bridge_authority->value = tor_strdup(
1698 "B1 orport=9001 bridge "
1699 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1702 config_line_t *test_alt_dir_authority = tor_malloc_zero(
1703 sizeof(config_line_t));
1704 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
1705 test_alt_dir_authority->value = tor_strdup(
1706 "A2 orport=9002 "
1707 "v3ident=0223456789012345678901234567890123456789 "
1708 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1711 /* Use the format specified in the manual page */
1712 config_line_t *test_fallback_directory = tor_malloc_zero(
1713 sizeof(config_line_t));
1714 test_fallback_directory->key = tor_strdup("FallbackDir");
1715 test_fallback_directory->value = tor_strdup(
1716 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
1719 /* We need to know if add_default_fallback_dir_servers is called,
1720 * whatever the size of the list in fallback_dirs.inc,
1721 * so we use a version of add_default_fallback_dir_servers that adds
1722 * one known default fallback directory. */
1723 MOCK(add_default_fallback_dir_servers,
1724 add_default_fallback_dir_servers_known_default);
1726 /* There are 16 different cases, covering each combination of set/NULL for:
1727 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
1728 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
1729 * But validate_dir_servers() ensures that:
1730 * "You cannot set both DirAuthority and Alternate*Authority."
1731 * This reduces the number of cases to 10.
1733 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
1734 * So 1001 or case 9 is:
1735 * DirAuthorities set,
1736 * AlternateBridgeAuthority NULL,
1737 * AlternateDirAuthority NULL
1738 * FallbackDir set
1739 * The valid cases are cases 0-9 counting using this method, as every case
1740 * greater than or equal to 10 = 1010 is invalid.
1742 * 1. Outcome: Use Set Directory Authorities
1743 * - No Default Authorities
1744 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1745 * if they are set
1746 * Cases expected to yield this outcome:
1747 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1748 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1749 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1751 * 2. Outcome: Use Set Bridge Authority
1752 * - Use Default Non-Bridge Directory Authorities
1753 * - Use FallbackDir if it is set, otherwise use default FallbackDir
1754 * Cases expected to yield this outcome:
1755 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
1756 * AlternateBridgeAuthority is set, and
1757 * AlternateDirAuthority is NULL)
1759 * 3. Outcome: Use Set Alternate Directory Authority
1760 * - Use Default Bridge Authorities
1761 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
1762 * Cases expected to yield this outcome:
1763 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
1764 * are both NULL, but AlternateDirAuthority is set)
1766 * 4. Outcome: Use Set Custom Fallback Directory
1767 * - Use Default Bridge & Directory Authorities
1768 * Cases expected to yield this outcome:
1769 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
1770 * are all NULL, but FallbackDir is set)
1772 * 5. Outcome: Use All Defaults
1773 * - Use Default Bridge & Directory Authorities, and
1774 * Default Fallback Directories
1775 * Cases expected to yield this outcome:
1776 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
1777 * and FallbackDir are all NULL)
1781 * Find out how many default Bridge, Non-Bridge and Fallback Directories
1782 * are hard-coded into this build.
1783 * This code makes some assumptions about the implementation.
1784 * If they are wrong, one or more of cases 0-5 could fail.
1786 int n_default_alt_bridge_authority = 0;
1787 int n_default_alt_dir_authority = 0;
1788 int n_default_fallback_dir = 0;
1789 #define n_default_authorities ((n_default_alt_bridge_authority) \
1790 + (n_default_alt_dir_authority))
1792 /* Pre-Count Number of Authorities of Each Type
1793 * Use 0000: No Directory Authorities or Fallback Directories Set
1796 /* clear fallback dirs counter */
1797 n_add_default_fallback_dir_servers_known_default = 0;
1799 /* clear options*/
1800 memset(options, 0, sizeof(or_options_t));
1802 /* clear any previous dir servers:
1803 consider_adding_dir_servers() should do this anyway */
1804 clear_dir_servers();
1806 /* assign options: 0000 */
1807 options->DirAuthorities = NULL;
1808 options->AlternateBridgeAuthority = NULL;
1809 options->AlternateDirAuthority = NULL;
1810 options->FallbackDir = NULL;
1811 options->UseDefaultFallbackDirs = 1;
1813 /* parse options - ensure we always update by passing NULL old_options */
1814 consider_adding_dir_servers(options, NULL);
1816 /* check outcome */
1818 /* we must have added the default fallback dirs */
1819 tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
1821 /* we have more fallbacks than just the authorities */
1822 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1825 /* fallback_dir_servers */
1826 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1828 /* Count Bridge Authorities */
1829 SMARTLIST_FOREACH(fallback_servers,
1830 dir_server_t *,
1832 /* increment the found counter if it's a bridge auth */
1833 n_default_alt_bridge_authority +=
1834 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
1835 1 : 0)
1837 /* If we have no default bridge authority, something has gone wrong */
1838 tt_assert(n_default_alt_bridge_authority >= 1);
1840 /* Count v3 Authorities */
1841 SMARTLIST_FOREACH(fallback_servers,
1842 dir_server_t *,
1844 /* increment found counter if it's a v3 auth */
1845 n_default_alt_dir_authority +=
1846 ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
1847 1 : 0)
1849 /* If we have no default authorities, something has gone really wrong */
1850 tt_assert(n_default_alt_dir_authority >= 1);
1852 /* Calculate Fallback Directory Count */
1853 n_default_fallback_dir = (smartlist_len(fallback_servers) -
1854 n_default_alt_bridge_authority -
1855 n_default_alt_dir_authority);
1856 /* If we have a negative count, something has gone really wrong,
1857 * or some authorities aren't being added as fallback directories.
1858 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
1859 * authorities being fallback directories.) */
1860 tt_assert(n_default_fallback_dir >= 0);
1865 * 1. Outcome: Use Set Directory Authorities
1866 * - No Default Authorities
1867 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1868 * if they are set
1869 * Cases expected to yield this outcome:
1870 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1871 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1872 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1875 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
1876 AlternateDirAuthority Not Set, FallbackDir Set */
1878 /* clear fallback dirs counter */
1879 n_add_default_fallback_dir_servers_known_default = 0;
1881 /* clear options*/
1882 memset(options, 0, sizeof(or_options_t));
1884 /* clear any previous dir servers:
1885 consider_adding_dir_servers() should do this anyway */
1886 clear_dir_servers();
1888 /* assign options: 1001 */
1889 options->DirAuthorities = test_dir_authority;
1890 options->AlternateBridgeAuthority = NULL;
1891 options->AlternateDirAuthority = NULL;
1892 options->FallbackDir = test_fallback_directory;
1893 options->UseDefaultFallbackDirs = 1;
1895 /* parse options - ensure we always update by passing NULL old_options */
1896 consider_adding_dir_servers(options, NULL);
1898 /* check outcome */
1900 /* we must not have added the default fallback dirs */
1901 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
1903 /* we have more fallbacks than just the authorities */
1904 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1907 /* trusted_dir_servers */
1908 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
1909 /* D0, (No B1), (No A2) */
1910 tt_assert(smartlist_len(dir_servers) == 1);
1912 /* DirAuthority - D0 - dir_port: 60090 */
1913 int found_D0 = 0;
1914 SMARTLIST_FOREACH(dir_servers,
1915 dir_server_t *,
1917 /* increment the found counter if dir_port matches */
1918 found_D0 +=
1919 (ds->dir_port == 60090 ?
1920 1 : 0)
1922 tt_assert(found_D0 == 1);
1924 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
1925 int found_B1 = 0;
1926 SMARTLIST_FOREACH(dir_servers,
1927 dir_server_t *,
1929 /* increment the found counter if dir_port matches */
1930 found_B1 +=
1931 (ds->dir_port == 60091 ?
1932 1 : 0)
1934 tt_assert(found_B1 == 0);
1936 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
1937 int found_A2 = 0;
1938 SMARTLIST_FOREACH(dir_servers,
1939 dir_server_t *,
1941 /* increment the found counter if dir_port matches */
1942 found_A2 +=
1943 (ds->dir_port == 60092 ?
1944 1 : 0)
1946 tt_assert(found_A2 == 0);
1950 /* fallback_dir_servers */
1951 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1952 /* D0, (No B1), (No A2), Custom Fallback */
1953 tt_assert(smartlist_len(fallback_servers) == 2);
1955 /* DirAuthority - D0 - dir_port: 60090 */
1956 int found_D0 = 0;
1957 SMARTLIST_FOREACH(fallback_servers,
1958 dir_server_t *,
1960 /* increment the found counter if dir_port matches */
1961 found_D0 +=
1962 (ds->dir_port == 60090 ?
1963 1 : 0)
1965 tt_assert(found_D0 == 1);
1967 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
1968 int found_B1 = 0;
1969 SMARTLIST_FOREACH(fallback_servers,
1970 dir_server_t *,
1972 /* increment the found counter if dir_port matches */
1973 found_B1 +=
1974 (ds->dir_port == 60091 ?
1975 1 : 0)
1977 tt_assert(found_B1 == 0);
1979 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
1980 int found_A2 = 0;
1981 SMARTLIST_FOREACH(fallback_servers,
1982 dir_server_t *,
1984 /* increment the found counter if dir_port matches */
1985 found_A2 +=
1986 (ds->dir_port == 60092 ?
1987 1 : 0)
1989 tt_assert(found_A2 == 0);
1991 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
1992 int found_non_default_fallback = 0;
1993 SMARTLIST_FOREACH(fallback_servers,
1994 dir_server_t *,
1996 /* increment the found counter if dir_port matches */
1997 found_non_default_fallback +=
1998 (ds->dir_port == 60093 ?
1999 1 : 0)
2001 tt_assert(found_non_default_fallback == 1);
2003 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2004 int found_default_fallback = 0;
2005 SMARTLIST_FOREACH(fallback_servers,
2006 dir_server_t *,
2008 /* increment the found counter if dir_port matches */
2009 found_default_fallback +=
2010 (ds->dir_port == 60099 ?
2011 1 : 0)
2013 tt_assert(found_default_fallback == 0);
2017 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2019 /* clear fallback dirs counter */
2020 n_add_default_fallback_dir_servers_known_default = 0;
2022 /* clear options*/
2023 memset(options, 0, sizeof(or_options_t));
2025 /* clear any previous dir servers:
2026 consider_adding_dir_servers() should do this anyway */
2027 clear_dir_servers();
2029 /* assign options: 1000 */
2030 options->DirAuthorities = test_dir_authority;
2031 options->AlternateBridgeAuthority = NULL;
2032 options->AlternateDirAuthority = NULL;
2033 options->FallbackDir = NULL;
2034 options->UseDefaultFallbackDirs = 1;
2036 /* parse options - ensure we always update by passing NULL old_options */
2037 consider_adding_dir_servers(options, NULL);
2039 /* check outcome */
2041 /* we must not have added the default fallback dirs */
2042 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2044 /* we just have the authorities */
2045 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2048 /* trusted_dir_servers */
2049 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2050 /* D0, (No B1), (No A2) */
2051 tt_assert(smartlist_len(dir_servers) == 1);
2053 /* DirAuthority - D0 - dir_port: 60090 */
2054 int found_D0 = 0;
2055 SMARTLIST_FOREACH(dir_servers,
2056 dir_server_t *,
2058 /* increment the found counter if dir_port matches */
2059 found_D0 +=
2060 (ds->dir_port == 60090 ?
2061 1 : 0)
2063 tt_assert(found_D0 == 1);
2065 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2066 int found_B1 = 0;
2067 SMARTLIST_FOREACH(dir_servers,
2068 dir_server_t *,
2070 /* increment the found counter if dir_port matches */
2071 found_B1 +=
2072 (ds->dir_port == 60091 ?
2073 1 : 0)
2075 tt_assert(found_B1 == 0);
2077 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2078 int found_A2 = 0;
2079 SMARTLIST_FOREACH(dir_servers,
2080 dir_server_t *,
2082 /* increment the found counter if dir_port matches */
2083 found_A2 +=
2084 (ds->dir_port == 60092 ?
2085 1 : 0)
2087 tt_assert(found_A2 == 0);
2091 /* fallback_dir_servers */
2092 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2093 /* D0, (No B1), (No A2), (No Fallback) */
2094 tt_assert(smartlist_len(fallback_servers) == 1);
2096 /* DirAuthority - D0 - dir_port: 60090 */
2097 int found_D0 = 0;
2098 SMARTLIST_FOREACH(fallback_servers,
2099 dir_server_t *,
2101 /* increment the found counter if dir_port matches */
2102 found_D0 +=
2103 (ds->dir_port == 60090 ?
2104 1 : 0)
2106 tt_assert(found_D0 == 1);
2108 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2109 int found_B1 = 0;
2110 SMARTLIST_FOREACH(fallback_servers,
2111 dir_server_t *,
2113 /* increment the found counter if dir_port matches */
2114 found_B1 +=
2115 (ds->dir_port == 60091 ?
2116 1 : 0)
2118 tt_assert(found_B1 == 0);
2120 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2121 int found_A2 = 0;
2122 SMARTLIST_FOREACH(fallback_servers,
2123 dir_server_t *,
2125 /* increment the found counter if dir_port matches */
2126 found_A2 +=
2127 (ds->dir_port == 60092 ?
2128 1 : 0)
2130 tt_assert(found_A2 == 0);
2132 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2133 int found_non_default_fallback = 0;
2134 SMARTLIST_FOREACH(fallback_servers,
2135 dir_server_t *,
2137 /* increment the found counter if dir_port matches */
2138 found_non_default_fallback +=
2139 (ds->dir_port == 60093 ?
2140 1 : 0)
2142 tt_assert(found_non_default_fallback == 0);
2144 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2145 int found_default_fallback = 0;
2146 SMARTLIST_FOREACH(fallback_servers,
2147 dir_server_t *,
2149 /* increment the found counter if dir_port matches */
2150 found_default_fallback +=
2151 (ds->dir_port == 60099 ?
2152 1 : 0)
2154 tt_assert(found_default_fallback == 0);
2158 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2160 /* clear fallback dirs counter */
2161 n_add_default_fallback_dir_servers_known_default = 0;
2163 /* clear options*/
2164 memset(options, 0, sizeof(or_options_t));
2166 /* clear any previous dir servers:
2167 consider_adding_dir_servers() should do this anyway */
2168 clear_dir_servers();
2170 /* assign options: 0111 */
2171 options->DirAuthorities = NULL;
2172 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2173 options->AlternateDirAuthority = test_alt_dir_authority;
2174 options->FallbackDir = test_fallback_directory;
2175 options->UseDefaultFallbackDirs = 1;
2177 /* parse options - ensure we always update by passing NULL old_options */
2178 consider_adding_dir_servers(options, NULL);
2180 /* check outcome */
2182 /* we must not have added the default fallback dirs */
2183 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2185 /* we have more fallbacks than just the authorities */
2186 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2189 /* trusted_dir_servers */
2190 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2191 /* (No D0), B1, A2 */
2192 tt_assert(smartlist_len(dir_servers) == 2);
2194 /* (No DirAuthority) - D0 - dir_port: 60090 */
2195 int found_D0 = 0;
2196 SMARTLIST_FOREACH(dir_servers,
2197 dir_server_t *,
2199 /* increment the found counter if dir_port matches */
2200 found_D0 +=
2201 (ds->dir_port == 60090 ?
2202 1 : 0)
2204 tt_assert(found_D0 == 0);
2206 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2207 int found_B1 = 0;
2208 SMARTLIST_FOREACH(dir_servers,
2209 dir_server_t *,
2211 /* increment the found counter if dir_port matches */
2212 found_B1 +=
2213 (ds->dir_port == 60091 ?
2214 1 : 0)
2216 tt_assert(found_B1 == 1);
2218 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2219 int found_A2 = 0;
2220 SMARTLIST_FOREACH(dir_servers,
2221 dir_server_t *,
2223 /* increment the found counter if dir_port matches */
2224 found_A2 +=
2225 (ds->dir_port == 60092 ?
2226 1 : 0)
2228 tt_assert(found_A2 == 1);
2232 /* fallback_dir_servers */
2233 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2234 /* (No D0), B1, A2, Custom Fallback */
2235 tt_assert(smartlist_len(fallback_servers) == 3);
2237 /* (No DirAuthority) - D0 - dir_port: 60090 */
2238 int found_D0 = 0;
2239 SMARTLIST_FOREACH(fallback_servers,
2240 dir_server_t *,
2242 /* increment the found counter if dir_port matches */
2243 found_D0 +=
2244 (ds->dir_port == 60090 ?
2245 1 : 0)
2247 tt_assert(found_D0 == 0);
2249 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2250 int found_B1 = 0;
2251 SMARTLIST_FOREACH(fallback_servers,
2252 dir_server_t *,
2254 /* increment the found counter if dir_port matches */
2255 found_B1 +=
2256 (ds->dir_port == 60091 ?
2257 1 : 0)
2259 tt_assert(found_B1 == 1);
2261 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2262 int found_A2 = 0;
2263 SMARTLIST_FOREACH(fallback_servers,
2264 dir_server_t *,
2266 /* increment the found counter if dir_port matches */
2267 found_A2 +=
2268 (ds->dir_port == 60092 ?
2269 1 : 0)
2271 tt_assert(found_A2 == 1);
2273 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2274 int found_non_default_fallback = 0;
2275 SMARTLIST_FOREACH(fallback_servers,
2276 dir_server_t *,
2278 /* increment the found counter if dir_port matches */
2279 found_non_default_fallback +=
2280 (ds->dir_port == 60093 ?
2281 1 : 0)
2283 tt_assert(found_non_default_fallback == 1);
2285 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2286 int found_default_fallback = 0;
2287 SMARTLIST_FOREACH(fallback_servers,
2288 dir_server_t *,
2290 /* increment the found counter if dir_port matches */
2291 found_default_fallback +=
2292 (ds->dir_port == 60099 ?
2293 1 : 0)
2295 tt_assert(found_default_fallback == 0);
2299 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2300 AlternateDirAuthority Set, FallbackDir Not Set */
2302 /* clear fallback dirs counter */
2303 n_add_default_fallback_dir_servers_known_default = 0;
2305 /* clear options*/
2306 memset(options, 0, sizeof(or_options_t));
2308 /* clear any previous dir servers:
2309 consider_adding_dir_servers() should do this anyway */
2310 clear_dir_servers();
2312 /* assign options: 0110 */
2313 options->DirAuthorities = NULL;
2314 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2315 options->AlternateDirAuthority = test_alt_dir_authority;
2316 options->FallbackDir = NULL;
2317 options->UseDefaultFallbackDirs = 1;
2319 /* parse options - ensure we always update by passing NULL old_options */
2320 consider_adding_dir_servers(options, NULL);
2322 /* check outcome */
2324 /* we must not have added the default fallback dirs */
2325 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2327 /* we have more fallbacks than just the authorities */
2328 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2331 /* trusted_dir_servers */
2332 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2333 /* (No D0), B1, A2 */
2334 tt_assert(smartlist_len(dir_servers) == 2);
2336 /* (No DirAuthority) - D0 - dir_port: 60090 */
2337 int found_D0 = 0;
2338 SMARTLIST_FOREACH(dir_servers,
2339 dir_server_t *,
2341 /* increment the found counter if dir_port matches */
2342 found_D0 +=
2343 (ds->dir_port == 60090 ?
2344 1 : 0)
2346 tt_assert(found_D0 == 0);
2348 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2349 int found_B1 = 0;
2350 SMARTLIST_FOREACH(dir_servers,
2351 dir_server_t *,
2353 /* increment the found counter if dir_port matches */
2354 found_B1 +=
2355 (ds->dir_port == 60091 ?
2356 1 : 0)
2358 tt_assert(found_B1 == 1);
2360 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2361 int found_A2 = 0;
2362 SMARTLIST_FOREACH(dir_servers,
2363 dir_server_t *,
2365 /* increment the found counter if dir_port matches */
2366 found_A2 +=
2367 (ds->dir_port == 60092 ?
2368 1 : 0)
2370 tt_assert(found_A2 == 1);
2374 /* fallback_dir_servers */
2375 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2376 /* (No D0), B1, A2, (No Fallback) */
2377 tt_assert(smartlist_len(fallback_servers) == 2);
2379 /* (No DirAuthority) - D0 - dir_port: 60090 */
2380 int found_D0 = 0;
2381 SMARTLIST_FOREACH(fallback_servers,
2382 dir_server_t *,
2384 /* increment the found counter if dir_port matches */
2385 found_D0 +=
2386 (ds->dir_port == 60090 ?
2387 1 : 0)
2389 tt_assert(found_D0 == 0);
2391 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2392 int found_B1 = 0;
2393 SMARTLIST_FOREACH(fallback_servers,
2394 dir_server_t *,
2396 /* increment the found counter if dir_port matches */
2397 found_B1 +=
2398 (ds->dir_port == 60091 ?
2399 1 : 0)
2401 tt_assert(found_B1 == 1);
2403 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2404 int found_A2 = 0;
2405 SMARTLIST_FOREACH(fallback_servers,
2406 dir_server_t *,
2408 /* increment the found counter if dir_port matches */
2409 found_A2 +=
2410 (ds->dir_port == 60092 ?
2411 1 : 0)
2413 tt_assert(found_A2 == 1);
2415 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2416 int found_non_default_fallback = 0;
2417 SMARTLIST_FOREACH(fallback_servers,
2418 dir_server_t *,
2420 /* increment the found counter if dir_port matches */
2421 found_non_default_fallback +=
2422 (ds->dir_port == 60093 ?
2423 1 : 0)
2425 tt_assert(found_non_default_fallback == 0);
2427 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2428 int found_default_fallback = 0;
2429 SMARTLIST_FOREACH(fallback_servers,
2430 dir_server_t *,
2432 /* increment the found counter if dir_port matches */
2433 found_default_fallback +=
2434 (ds->dir_port == 60099 ?
2435 1 : 0)
2437 tt_assert(found_default_fallback == 0);
2442 2. Outcome: Use Set Bridge Authority
2443 - Use Default Non-Bridge Directory Authorities
2444 - Use FallbackDir if it is set, otherwise use default FallbackDir
2445 Cases expected to yield this outcome:
2446 4 & 5 (the 2 cases where DirAuthorities is NULL,
2447 AlternateBridgeAuthority is set, and
2448 AlternateDirAuthority is NULL)
2451 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2452 AlternateDirAuthority Not Set, FallbackDir Set */
2454 /* clear fallback dirs counter */
2455 n_add_default_fallback_dir_servers_known_default = 0;
2457 /* clear options*/
2458 memset(options, 0, sizeof(or_options_t));
2460 /* clear any previous dir servers:
2461 consider_adding_dir_servers() should do this anyway */
2462 clear_dir_servers();
2464 /* assign options: 0101 */
2465 options->DirAuthorities = NULL;
2466 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2467 options->AlternateDirAuthority = NULL;
2468 options->FallbackDir = test_fallback_directory;
2469 options->UseDefaultFallbackDirs = 1;
2471 /* parse options - ensure we always update by passing NULL old_options */
2472 consider_adding_dir_servers(options, NULL);
2474 /* check outcome */
2476 /* we must not have added the default fallback dirs */
2477 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2479 /* we have more fallbacks than just the authorities */
2480 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2483 /* trusted_dir_servers */
2484 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2485 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2486 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2488 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2489 int found_D0 = 0;
2490 SMARTLIST_FOREACH(dir_servers,
2491 dir_server_t *,
2493 /* increment the found counter if dir_port matches */
2494 found_D0 +=
2495 (ds->dir_port == 60090 ?
2496 1 : 0)
2498 tt_assert(found_D0 == 0);
2500 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2501 int found_B1 = 0;
2502 SMARTLIST_FOREACH(dir_servers,
2503 dir_server_t *,
2505 /* increment the found counter if dir_port matches */
2506 found_B1 +=
2507 (ds->dir_port == 60091 ?
2508 1 : 0)
2510 tt_assert(found_B1 == 1);
2512 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2513 int found_A2 = 0;
2514 SMARTLIST_FOREACH(dir_servers,
2515 dir_server_t *,
2517 /* increment the found counter if dir_port matches */
2518 found_A2 +=
2519 (ds->dir_port == 60092 ?
2520 1 : 0)
2522 tt_assert(found_A2 == 0);
2524 /* There's no easy way of checking that we have included all the
2525 * default v3 non-Bridge directory authorities, so let's assume that
2526 * if the total count above is correct, we have the right ones.
2531 /* fallback_dir_servers */
2532 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2533 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2534 * Custom Fallback */
2535 tt_assert(smartlist_len(fallback_servers) ==
2536 2 + n_default_alt_dir_authority);
2538 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2539 int found_D0 = 0;
2540 SMARTLIST_FOREACH(fallback_servers,
2541 dir_server_t *,
2543 /* increment the found counter if dir_port matches */
2544 found_D0 +=
2545 (ds->dir_port == 60090 ?
2546 1 : 0)
2548 tt_assert(found_D0 == 0);
2550 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2551 int found_B1 = 0;
2552 SMARTLIST_FOREACH(fallback_servers,
2553 dir_server_t *,
2555 /* increment the found counter if dir_port matches */
2556 found_B1 +=
2557 (ds->dir_port == 60091 ?
2558 1 : 0)
2560 tt_assert(found_B1 == 1);
2562 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2563 int found_A2 = 0;
2564 SMARTLIST_FOREACH(fallback_servers,
2565 dir_server_t *,
2567 /* increment the found counter if dir_port matches */
2568 found_A2 +=
2569 (ds->dir_port == 60092 ?
2570 1 : 0)
2572 tt_assert(found_A2 == 0);
2574 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2575 int found_non_default_fallback = 0;
2576 SMARTLIST_FOREACH(fallback_servers,
2577 dir_server_t *,
2579 /* increment the found counter if dir_port matches */
2580 found_non_default_fallback +=
2581 (ds->dir_port == 60093 ?
2582 1 : 0)
2584 tt_assert(found_non_default_fallback == 1);
2586 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2587 int found_default_fallback = 0;
2588 SMARTLIST_FOREACH(fallback_servers,
2589 dir_server_t *,
2591 /* increment the found counter if dir_port matches */
2592 found_default_fallback +=
2593 (ds->dir_port == 60099 ?
2594 1 : 0)
2596 tt_assert(found_default_fallback == 0);
2598 /* There's no easy way of checking that we have included all the
2599 * default v3 non-Bridge directory authorities, so let's assume that
2600 * if the total count above is correct, we have the right ones.
2605 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2606 AlternateDirAuthority & FallbackDir Not Set */
2608 /* clear fallback dirs counter */
2609 n_add_default_fallback_dir_servers_known_default = 0;
2611 /* clear options*/
2612 memset(options, 0, sizeof(or_options_t));
2614 /* clear any previous dir servers:
2615 consider_adding_dir_servers() should do this anyway */
2616 clear_dir_servers();
2618 /* assign options: 0100 */
2619 options->DirAuthorities = NULL;
2620 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2621 options->AlternateDirAuthority = NULL;
2622 options->FallbackDir = NULL;
2623 options->UseDefaultFallbackDirs = 1;
2625 /* parse options - ensure we always update by passing NULL old_options */
2626 consider_adding_dir_servers(options, NULL);
2628 /* check outcome */
2630 /* we must have added the default fallback dirs */
2631 tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
2633 /* we have more fallbacks than just the authorities */
2634 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2637 /* trusted_dir_servers */
2638 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2639 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2640 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2642 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2643 int found_D0 = 0;
2644 SMARTLIST_FOREACH(dir_servers,
2645 dir_server_t *,
2647 /* increment the found counter if dir_port matches */
2648 found_D0 +=
2649 (ds->dir_port == 60090 ?
2650 1 : 0)
2652 tt_assert(found_D0 == 0);
2654 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2655 int found_B1 = 0;
2656 SMARTLIST_FOREACH(dir_servers,
2657 dir_server_t *,
2659 /* increment the found counter if dir_port matches */
2660 found_B1 +=
2661 (ds->dir_port == 60091 ?
2662 1 : 0)
2664 tt_assert(found_B1 == 1);
2666 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2667 int found_A2 = 0;
2668 SMARTLIST_FOREACH(dir_servers,
2669 dir_server_t *,
2671 /* increment the found counter if dir_port matches */
2672 found_A2 +=
2673 (ds->dir_port == 60092 ?
2674 1 : 0)
2676 tt_assert(found_A2 == 0);
2678 /* There's no easy way of checking that we have included all the
2679 * default v3 non-Bridge directory authorities, so let's assume that
2680 * if the total count above is correct, we have the right ones.
2685 /* fallback_dir_servers */
2686 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2687 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2688 * Default Fallback */
2689 tt_assert(smartlist_len(fallback_servers) ==
2690 2 + n_default_alt_dir_authority);
2692 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2693 int found_D0 = 0;
2694 SMARTLIST_FOREACH(fallback_servers,
2695 dir_server_t *,
2697 /* increment the found counter if dir_port matches */
2698 found_D0 +=
2699 (ds->dir_port == 60090 ?
2700 1 : 0)
2702 tt_assert(found_D0 == 0);
2704 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2705 int found_B1 = 0;
2706 SMARTLIST_FOREACH(fallback_servers,
2707 dir_server_t *,
2709 /* increment the found counter if dir_port matches */
2710 found_B1 +=
2711 (ds->dir_port == 60091 ?
2712 1 : 0)
2714 tt_assert(found_B1 == 1);
2716 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2717 int found_A2 = 0;
2718 SMARTLIST_FOREACH(fallback_servers,
2719 dir_server_t *,
2721 /* increment the found counter if dir_port matches */
2722 found_A2 +=
2723 (ds->dir_port == 60092 ?
2724 1 : 0)
2726 tt_assert(found_A2 == 0);
2728 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2729 int found_non_default_fallback = 0;
2730 SMARTLIST_FOREACH(fallback_servers,
2731 dir_server_t *,
2733 /* increment the found counter if dir_port matches */
2734 found_non_default_fallback +=
2735 (ds->dir_port == 60093 ?
2736 1 : 0)
2738 tt_assert(found_non_default_fallback == 0);
2740 /* Default FallbackDir - No Nickname - dir_port: 60099 */
2741 int found_default_fallback = 0;
2742 SMARTLIST_FOREACH(fallback_servers,
2743 dir_server_t *,
2745 /* increment the found counter if dir_port matches */
2746 found_default_fallback +=
2747 (ds->dir_port == 60099 ?
2748 1 : 0)
2750 tt_assert(found_default_fallback == 1);
2752 /* There's no easy way of checking that we have included all the
2753 * default v3 non-Bridge directory authorities, so let's assume that
2754 * if the total count above is correct, we have the right ones.
2760 3. Outcome: Use Set Alternate Directory Authority
2761 - Use Default Bridge Authorities
2762 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2763 Cases expected to yield this outcome:
2764 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2765 are both NULL, but AlternateDirAuthority is set)
2768 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
2769 AlternateDirAuthority & FallbackDir Set */
2771 /* clear fallback dirs counter */
2772 n_add_default_fallback_dir_servers_known_default = 0;
2774 /* clear options*/
2775 memset(options, 0, sizeof(or_options_t));
2777 /* clear any previous dir servers:
2778 consider_adding_dir_servers() should do this anyway */
2779 clear_dir_servers();
2781 /* assign options: 0011 */
2782 options->DirAuthorities = NULL;
2783 options->AlternateBridgeAuthority = NULL;
2784 options->AlternateDirAuthority = test_alt_dir_authority;
2785 options->FallbackDir = test_fallback_directory;
2786 options->UseDefaultFallbackDirs = 1;
2788 /* parse options - ensure we always update by passing NULL old_options */
2789 consider_adding_dir_servers(options, NULL);
2791 /* check outcome */
2793 /* we must not have added the default fallback dirs */
2794 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2796 /* we have more fallbacks than just the authorities */
2797 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2800 /* trusted_dir_servers */
2801 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2802 /* (No D0), (No B1), Default Bridge Authorities, A2 */
2803 tt_assert(smartlist_len(dir_servers) ==
2804 1 + n_default_alt_bridge_authority);
2806 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2807 int found_D0 = 0;
2808 SMARTLIST_FOREACH(dir_servers,
2809 dir_server_t *,
2811 /* increment the found counter if dir_port matches */
2812 found_D0 +=
2813 (ds->dir_port == 60090 ?
2814 1 : 0)
2816 tt_assert(found_D0 == 0);
2818 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2819 int found_B1 = 0;
2820 SMARTLIST_FOREACH(dir_servers,
2821 dir_server_t *,
2823 /* increment the found counter if dir_port matches */
2824 found_B1 +=
2825 (ds->dir_port == 60091 ?
2826 1 : 0)
2828 tt_assert(found_B1 == 0);
2830 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2831 int found_A2 = 0;
2832 SMARTLIST_FOREACH(dir_servers,
2833 dir_server_t *,
2835 /* increment the found counter if dir_port matches */
2836 found_A2 +=
2837 (ds->dir_port == 60092 ?
2838 1 : 0)
2840 tt_assert(found_A2 == 1);
2842 /* There's no easy way of checking that we have included all the
2843 * default Bridge authorities (except for hard-coding tonga's details),
2844 * so let's assume that if the total count above is correct,
2845 * we have the right ones.
2850 /* fallback_dir_servers */
2851 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2852 /* (No D0), (No B1), Default Bridge Authorities, A2,
2853 * Custom Fallback Directory, (No Default Fallback Directories) */
2854 tt_assert(smartlist_len(fallback_servers) ==
2855 2 + n_default_alt_bridge_authority);
2857 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2858 int found_D0 = 0;
2859 SMARTLIST_FOREACH(fallback_servers,
2860 dir_server_t *,
2862 /* increment the found counter if dir_port matches */
2863 found_D0 +=
2864 (ds->dir_port == 60090 ?
2865 1 : 0)
2867 tt_assert(found_D0 == 0);
2869 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2870 int found_B1 = 0;
2871 SMARTLIST_FOREACH(fallback_servers,
2872 dir_server_t *,
2874 /* increment the found counter if dir_port matches */
2875 found_B1 +=
2876 (ds->dir_port == 60091 ?
2877 1 : 0)
2879 tt_assert(found_B1 == 0);
2881 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2882 int found_A2 = 0;
2883 SMARTLIST_FOREACH(fallback_servers,
2884 dir_server_t *,
2886 /* increment the found counter if dir_port matches */
2887 found_A2 +=
2888 (ds->dir_port == 60092 ?
2889 1 : 0)
2891 tt_assert(found_A2 == 1);
2893 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2894 int found_non_default_fallback = 0;
2895 SMARTLIST_FOREACH(fallback_servers,
2896 dir_server_t *,
2898 /* increment the found counter if dir_port matches */
2899 found_non_default_fallback +=
2900 (ds->dir_port == 60093 ?
2901 1 : 0)
2903 tt_assert(found_non_default_fallback == 1);
2905 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2906 int found_default_fallback = 0;
2907 SMARTLIST_FOREACH(fallback_servers,
2908 dir_server_t *,
2910 /* increment the found counter if dir_port matches */
2911 found_default_fallback +=
2912 (ds->dir_port == 60099 ?
2913 1 : 0)
2915 tt_assert(found_default_fallback == 0);
2917 /* There's no easy way of checking that we have included all the
2918 * default Bridge authorities (except for hard-coding tonga's details),
2919 * so let's assume that if the total count above is correct,
2920 * we have the right ones.
2925 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
2926 AlternateDirAuthority Set, FallbackDir Not Set */
2928 /* clear fallback dirs counter */
2929 n_add_default_fallback_dir_servers_known_default = 0;
2931 /* clear options*/
2932 memset(options, 0, sizeof(or_options_t));
2934 /* clear any previous dir servers:
2935 consider_adding_dir_servers() should do this anyway */
2936 clear_dir_servers();
2938 /* assign options: 0010 */
2939 options->DirAuthorities = NULL;
2940 options->AlternateBridgeAuthority = NULL;
2941 options->AlternateDirAuthority = test_alt_dir_authority;
2942 options->FallbackDir = NULL;
2943 options->UseDefaultFallbackDirs = 1;
2945 /* parse options - ensure we always update by passing NULL old_options */
2946 consider_adding_dir_servers(options, NULL);
2948 /* check outcome */
2950 /* we must not have added the default fallback dirs */
2951 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
2953 /* we just have the authorities */
2954 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2957 /* trusted_dir_servers */
2958 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2959 /* (No D0), (No B1), Default Bridge Authorities, A2,
2960 * No Default or Custom Fallback Directories */
2961 tt_assert(smartlist_len(dir_servers) ==
2962 1 + n_default_alt_bridge_authority);
2964 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2965 int found_D0 = 0;
2966 SMARTLIST_FOREACH(dir_servers,
2967 dir_server_t *,
2969 /* increment the found counter if dir_port matches */
2970 found_D0 +=
2971 (ds->dir_port == 60090 ?
2972 1 : 0)
2974 tt_assert(found_D0 == 0);
2976 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2977 int found_B1 = 0;
2978 SMARTLIST_FOREACH(dir_servers,
2979 dir_server_t *,
2981 /* increment the found counter if dir_port matches */
2982 found_B1 +=
2983 (ds->dir_port == 60091 ?
2984 1 : 0)
2986 tt_assert(found_B1 == 0);
2988 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2989 int found_A2 = 0;
2990 SMARTLIST_FOREACH(dir_servers,
2991 dir_server_t *,
2993 /* increment the found counter if dir_port matches */
2994 found_A2 +=
2995 (ds->dir_port == 60092 ?
2996 1 : 0)
2998 tt_assert(found_A2 == 1);
3000 /* There's no easy way of checking that we have included all the
3001 * default Bridge authorities (except for hard-coding tonga's details),
3002 * so let's assume that if the total count above is correct,
3003 * we have the right ones.
3008 /* fallback_dir_servers */
3009 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3010 /* (No D0), (No B1), Default Bridge Authorities, A2,
3011 * No Custom or Default Fallback Directories */
3012 tt_assert(smartlist_len(fallback_servers) ==
3013 1 + n_default_alt_bridge_authority);
3015 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3016 int found_D0 = 0;
3017 SMARTLIST_FOREACH(fallback_servers,
3018 dir_server_t *,
3020 /* increment the found counter if dir_port matches */
3021 found_D0 +=
3022 (ds->dir_port == 60090 ?
3023 1 : 0)
3025 tt_assert(found_D0 == 0);
3027 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3028 int found_B1 = 0;
3029 SMARTLIST_FOREACH(fallback_servers,
3030 dir_server_t *,
3032 /* increment the found counter if dir_port matches */
3033 found_B1 +=
3034 (ds->dir_port == 60091 ?
3035 1 : 0)
3037 tt_assert(found_B1 == 0);
3039 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3040 int found_A2 = 0;
3041 SMARTLIST_FOREACH(fallback_servers,
3042 dir_server_t *,
3044 /* increment the found counter if dir_port matches */
3045 found_A2 +=
3046 (ds->dir_port == 60092 ?
3047 1 : 0)
3049 tt_assert(found_A2 == 1);
3051 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3052 int found_non_default_fallback = 0;
3053 SMARTLIST_FOREACH(fallback_servers,
3054 dir_server_t *,
3056 /* increment the found counter if dir_port matches */
3057 found_non_default_fallback +=
3058 (ds->dir_port == 60093 ?
3059 1 : 0)
3061 tt_assert(found_non_default_fallback == 0);
3063 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3064 int found_default_fallback = 0;
3065 SMARTLIST_FOREACH(fallback_servers,
3066 dir_server_t *,
3068 /* increment the found counter if dir_port matches */
3069 found_default_fallback +=
3070 (ds->dir_port == 60099 ?
3071 1 : 0)
3073 tt_assert(found_default_fallback == 0);
3075 /* There's no easy way of checking that we have included all the
3076 * default Bridge authorities (except for hard-coding tonga's details),
3077 * so let's assume that if the total count above is correct,
3078 * we have the right ones.
3084 4. Outcome: Use Set Custom Fallback Directory
3085 - Use Default Bridge & Directory Authorities
3086 Cases expected to yield this outcome:
3087 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3088 are all NULL, but FallbackDir is set)
3091 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3092 & AlternateDirAuthority Not Set, FallbackDir Set */
3094 /* clear fallback dirs counter */
3095 n_add_default_fallback_dir_servers_known_default = 0;
3097 /* clear options*/
3098 memset(options, 0, sizeof(or_options_t));
3100 /* clear any previous dir servers:
3101 consider_adding_dir_servers() should do this anyway */
3102 clear_dir_servers();
3104 /* assign options: 0001 */
3105 options->DirAuthorities = NULL;
3106 options->AlternateBridgeAuthority = NULL;
3107 options->AlternateDirAuthority = NULL;
3108 options->FallbackDir = test_fallback_directory;
3109 options->UseDefaultFallbackDirs = 1;
3111 /* parse options - ensure we always update by passing NULL old_options */
3112 consider_adding_dir_servers(options, NULL);
3114 /* check outcome */
3116 /* we must not have added the default fallback dirs */
3117 tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
3119 /* we have more fallbacks than just the authorities */
3120 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3123 /* trusted_dir_servers */
3124 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3125 /* (No D0), (No B1), Default Bridge Authorities,
3126 * (No A2), Default v3 Directory Authorities */
3127 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3129 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3130 int found_D0 = 0;
3131 SMARTLIST_FOREACH(dir_servers,
3132 dir_server_t *,
3134 /* increment the found counter if dir_port matches */
3135 found_D0 +=
3136 (ds->dir_port == 60090 ?
3137 1 : 0)
3139 tt_assert(found_D0 == 0);
3141 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3142 int found_B1 = 0;
3143 SMARTLIST_FOREACH(dir_servers,
3144 dir_server_t *,
3146 /* increment the found counter if dir_port matches */
3147 found_B1 +=
3148 (ds->dir_port == 60091 ?
3149 1 : 0)
3151 tt_assert(found_B1 == 0);
3153 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3154 int found_A2 = 0;
3155 SMARTLIST_FOREACH(dir_servers,
3156 dir_server_t *,
3158 /* increment the found counter if dir_port matches */
3159 found_A2 +=
3160 (ds->dir_port == 60092 ?
3161 1 : 0)
3163 tt_assert(found_A2 == 0);
3165 /* There's no easy way of checking that we have included all the
3166 * default Bridge & V3 Directory authorities, so let's assume that
3167 * if the total count above is correct, we have the right ones.
3172 /* fallback_dir_servers */
3173 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3174 /* (No D0), (No B1), Default Bridge Authorities,
3175 * (No A2), Default v3 Directory Authorities,
3176 * Custom Fallback Directory, (No Default Fallback Directories) */
3177 tt_assert(smartlist_len(fallback_servers) ==
3178 1 + n_default_authorities);
3180 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3181 int found_D0 = 0;
3182 SMARTLIST_FOREACH(fallback_servers,
3183 dir_server_t *,
3185 /* increment the found counter if dir_port matches */
3186 found_D0 +=
3187 (ds->dir_port == 60090 ?
3188 1 : 0)
3190 tt_assert(found_D0 == 0);
3192 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3193 int found_B1 = 0;
3194 SMARTLIST_FOREACH(fallback_servers,
3195 dir_server_t *,
3197 /* increment the found counter if dir_port matches */
3198 found_B1 +=
3199 (ds->dir_port == 60091 ?
3200 1 : 0)
3202 tt_assert(found_B1 == 0);
3204 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3205 int found_A2 = 0;
3206 SMARTLIST_FOREACH(fallback_servers,
3207 dir_server_t *,
3209 /* increment the found counter if dir_port matches */
3210 found_A2 +=
3211 (ds->dir_port == 60092 ?
3212 1 : 0)
3214 tt_assert(found_A2 == 0);
3216 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3217 int found_non_default_fallback = 0;
3218 SMARTLIST_FOREACH(fallback_servers,
3219 dir_server_t *,
3221 /* increment the found counter if dir_port matches */
3222 found_non_default_fallback +=
3223 (ds->dir_port == 60093 ?
3224 1 : 0)
3226 tt_assert(found_non_default_fallback == 1);
3228 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3229 int found_default_fallback = 0;
3230 SMARTLIST_FOREACH(fallback_servers,
3231 dir_server_t *,
3233 /* increment the found counter if dir_port matches */
3234 found_default_fallback +=
3235 (ds->dir_port == 60099 ?
3236 1 : 0)
3238 tt_assert(found_default_fallback == 0);
3240 /* There's no easy way of checking that we have included all the
3241 * default Bridge & V3 Directory authorities, so let's assume that
3242 * if the total count above is correct, we have the right ones.
3248 5. Outcome: Use All Defaults
3249 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3250 Cases expected to yield this outcome:
3251 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3252 and FallbackDir are all NULL)
3255 /* Case 0: 0000 - All Not Set */
3257 /* clear fallback dirs counter */
3258 n_add_default_fallback_dir_servers_known_default = 0;
3260 /* clear options*/
3261 memset(options, 0, sizeof(or_options_t));
3263 /* clear any previous dir servers:
3264 consider_adding_dir_servers() should do this anyway */
3265 clear_dir_servers();
3267 /* assign options: 0001 */
3268 options->DirAuthorities = NULL;
3269 options->AlternateBridgeAuthority = NULL;
3270 options->AlternateDirAuthority = NULL;
3271 options->FallbackDir = NULL;
3272 options->UseDefaultFallbackDirs = 1;
3274 /* parse options - ensure we always update by passing NULL old_options */
3275 consider_adding_dir_servers(options, NULL);
3277 /* check outcome */
3279 /* we must have added the default fallback dirs */
3280 tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
3282 /* we have more fallbacks than just the authorities */
3283 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3286 /* trusted_dir_servers */
3287 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3288 /* (No D0), (No B1), Default Bridge Authorities,
3289 * (No A2), Default v3 Directory Authorities */
3290 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3292 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3293 int found_D0 = 0;
3294 SMARTLIST_FOREACH(dir_servers,
3295 dir_server_t *,
3297 /* increment the found counter if dir_port matches */
3298 found_D0 +=
3299 (ds->dir_port == 60090 ?
3300 1 : 0)
3302 tt_assert(found_D0 == 0);
3304 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3305 int found_B1 = 0;
3306 SMARTLIST_FOREACH(dir_servers,
3307 dir_server_t *,
3309 /* increment the found counter if dir_port matches */
3310 found_B1 +=
3311 (ds->dir_port == 60091 ?
3312 1 : 0)
3314 tt_assert(found_B1 == 0);
3316 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3317 int found_A2 = 0;
3318 SMARTLIST_FOREACH(dir_servers,
3319 dir_server_t *,
3321 /* increment the found counter if dir_port matches */
3322 found_A2 +=
3323 (ds->dir_port == 60092 ?
3324 1 : 0)
3326 tt_assert(found_A2 == 0);
3328 /* There's no easy way of checking that we have included all the
3329 * default Bridge & V3 Directory authorities, so let's assume that
3330 * if the total count above is correct, we have the right ones.
3335 /* fallback_dir_servers */
3336 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3337 /* (No D0), (No B1), Default Bridge Authorities,
3338 * (No A2), Default v3 Directory Authorities,
3339 * (No Custom Fallback Directory), Default Fallback Directories */
3340 tt_assert(smartlist_len(fallback_servers) ==
3341 n_default_authorities + n_default_fallback_dir);
3343 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3344 int found_D0 = 0;
3345 SMARTLIST_FOREACH(fallback_servers,
3346 dir_server_t *,
3348 /* increment the found counter if dir_port matches */
3349 found_D0 +=
3350 (ds->dir_port == 60090 ?
3351 1 : 0)
3353 tt_assert(found_D0 == 0);
3355 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3356 int found_B1 = 0;
3357 SMARTLIST_FOREACH(fallback_servers,
3358 dir_server_t *,
3360 /* increment the found counter if dir_port matches */
3361 found_B1 +=
3362 (ds->dir_port == 60091 ?
3363 1 : 0)
3365 tt_assert(found_B1 == 0);
3367 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3368 int found_A2 = 0;
3369 SMARTLIST_FOREACH(fallback_servers,
3370 dir_server_t *,
3372 /* increment the found counter if dir_port matches */
3373 found_A2 +=
3374 (ds->dir_port == 60092 ?
3375 1 : 0)
3377 tt_assert(found_A2 == 0);
3379 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3380 int found_non_default_fallback = 0;
3381 SMARTLIST_FOREACH(fallback_servers,
3382 dir_server_t *,
3384 /* increment the found counter if dir_port matches */
3385 found_non_default_fallback +=
3386 (ds->dir_port == 60093 ?
3387 1 : 0)
3389 tt_assert(found_non_default_fallback == 0);
3391 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3392 int found_default_fallback = 0;
3393 SMARTLIST_FOREACH(fallback_servers,
3394 dir_server_t *,
3396 /* increment the found counter if dir_port matches */
3397 found_default_fallback +=
3398 (ds->dir_port == 60099 ?
3399 1 : 0)
3401 tt_assert(found_default_fallback == 1);
3403 /* There's no easy way of checking that we have included all the
3404 * default Bridge & V3 Directory authorities, and the default
3405 * Fallback Directories, so let's assume that if the total count
3406 * above is correct, we have the right ones.
3411 done:
3412 clear_dir_servers();
3414 tor_free(test_dir_authority->key);
3415 tor_free(test_dir_authority->value);
3416 tor_free(test_dir_authority);
3418 tor_free(test_alt_dir_authority->key);
3419 tor_free(test_alt_dir_authority->value);
3420 tor_free(test_alt_dir_authority);
3422 tor_free(test_alt_bridge_authority->key);
3423 tor_free(test_alt_bridge_authority->value);
3424 tor_free(test_alt_bridge_authority);
3426 tor_free(test_fallback_directory->key);
3427 tor_free(test_fallback_directory->value);
3428 tor_free(test_fallback_directory);
3430 options->DirAuthorities = NULL;
3431 options->AlternateBridgeAuthority = NULL;
3432 options->AlternateDirAuthority = NULL;
3433 options->FallbackDir = NULL;
3434 or_options_free(options);
3436 UNMOCK(add_default_fallback_dir_servers);
3439 static void
3440 test_config_default_dir_servers(void *arg)
3442 or_options_t *opts = NULL;
3443 (void)arg;
3444 int trusted_count = 0;
3445 int fallback_count = 0;
3447 /* new set of options should stop fallback parsing */
3448 opts = tor_malloc_zero(sizeof(or_options_t));
3449 opts->UseDefaultFallbackDirs = 0;
3450 /* set old_options to NULL to force dir update */
3451 consider_adding_dir_servers(opts, NULL);
3452 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3453 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3454 or_options_free(opts);
3455 opts = NULL;
3457 /* assume a release will never go out with less than 7 authorities */
3458 tt_assert(trusted_count >= 7);
3459 /* if we disable the default fallbacks, there must not be any extra */
3460 tt_assert(fallback_count == trusted_count);
3462 opts = tor_malloc_zero(sizeof(or_options_t));
3463 opts->UseDefaultFallbackDirs = 1;
3464 consider_adding_dir_servers(opts, opts);
3465 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3466 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3467 or_options_free(opts);
3468 opts = NULL;
3470 /* assume a release will never go out with less than 7 authorities */
3471 tt_assert(trusted_count >= 7);
3472 /* XX/teor - allow for default fallbacks to be added without breaking
3473 * the unit tests. Set a minimum fallback count once the list is stable. */
3474 tt_assert(fallback_count >= trusted_count);
3476 done:
3477 or_options_free(opts);
3480 static int mock_router_pick_published_address_result = 0;
3482 static int
3483 mock_router_pick_published_address(const or_options_t *options, uint32_t *addr)
3485 (void)options;
3486 (void)addr;
3487 return mock_router_pick_published_address_result;
3490 static int mock_router_my_exit_policy_is_reject_star_result = 0;
3492 static int
3493 mock_router_my_exit_policy_is_reject_star(void)
3495 return mock_router_my_exit_policy_is_reject_star_result;
3498 static int mock_advertised_server_mode_result = 0;
3500 static int
3501 mock_advertised_server_mode(void)
3503 return mock_advertised_server_mode_result;
3506 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
3508 static const routerinfo_t *
3509 mock_router_get_my_routerinfo(void)
3511 return mock_router_get_my_routerinfo_result;
3514 static void
3515 test_config_directory_fetch(void *arg)
3517 (void)arg;
3519 /* Test Setup */
3520 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
3521 routerinfo_t routerinfo;
3522 memset(&routerinfo, 0, sizeof(routerinfo));
3523 mock_router_pick_published_address_result = -1;
3524 mock_router_my_exit_policy_is_reject_star_result = 1;
3525 mock_advertised_server_mode_result = 0;
3526 mock_router_get_my_routerinfo_result = NULL;
3527 MOCK(router_pick_published_address, mock_router_pick_published_address);
3528 MOCK(router_my_exit_policy_is_reject_star,
3529 mock_router_my_exit_policy_is_reject_star);
3530 MOCK(advertised_server_mode, mock_advertised_server_mode);
3531 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
3533 /* Clients can use multiple directory mirrors for bootstrap */
3534 memset(options, 0, sizeof(or_options_t));
3535 options->ClientOnly = 1;
3536 tt_assert(server_mode(options) == 0);
3537 tt_assert(public_server_mode(options) == 0);
3538 tt_assert(directory_fetches_from_authorities(options) == 0);
3539 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3540 == 1);
3542 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3543 memset(options, 0, sizeof(or_options_t));
3544 options->UseBridges = 1;
3545 tt_assert(server_mode(options) == 0);
3546 tt_assert(public_server_mode(options) == 0);
3547 tt_assert(directory_fetches_from_authorities(options) == 0);
3548 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3549 == 1);
3551 /* Bridge Relays (Bridges) must act like clients, and use multiple
3552 * directory mirrors for bootstrap */
3553 memset(options, 0, sizeof(or_options_t));
3554 options->BridgeRelay = 1;
3555 options->ORPort_set = 1;
3556 tt_assert(server_mode(options) == 1);
3557 tt_assert(public_server_mode(options) == 0);
3558 tt_assert(directory_fetches_from_authorities(options) == 0);
3559 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3560 == 1);
3562 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3563 * but can use multiple authorities for bootstrap */
3564 memset(options, 0, sizeof(or_options_t));
3565 options->FetchDirInfoEarly = 1;
3566 tt_assert(server_mode(options) == 0);
3567 tt_assert(public_server_mode(options) == 0);
3568 tt_assert(directory_fetches_from_authorities(options) == 1);
3569 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3570 == 1);
3572 /* OR servers only fetch the consensus from the authorities when they don't
3573 * know their own address, but never use multiple directories for bootstrap
3575 memset(options, 0, sizeof(or_options_t));
3576 options->ORPort_set = 1;
3578 mock_router_pick_published_address_result = -1;
3579 tt_assert(server_mode(options) == 1);
3580 tt_assert(public_server_mode(options) == 1);
3581 tt_assert(directory_fetches_from_authorities(options) == 1);
3582 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3583 == 0);
3585 mock_router_pick_published_address_result = 0;
3586 tt_assert(server_mode(options) == 1);
3587 tt_assert(public_server_mode(options) == 1);
3588 tt_assert(directory_fetches_from_authorities(options) == 0);
3589 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3590 == 0);
3592 /* Exit OR servers only fetch the consensus from the authorities when they
3593 * refuse unknown exits, but never use multiple directories for bootstrap
3595 memset(options, 0, sizeof(or_options_t));
3596 options->ORPort_set = 1;
3597 options->ExitRelay = 1;
3598 mock_router_pick_published_address_result = 0;
3599 mock_router_my_exit_policy_is_reject_star_result = 0;
3600 mock_advertised_server_mode_result = 1;
3601 mock_router_get_my_routerinfo_result = &routerinfo;
3603 routerinfo.supports_tunnelled_dir_requests = 1;
3605 options->RefuseUnknownExits = 1;
3606 tt_assert(server_mode(options) == 1);
3607 tt_assert(public_server_mode(options) == 1);
3608 tt_assert(directory_fetches_from_authorities(options) == 1);
3609 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3610 == 0);
3612 options->RefuseUnknownExits = 0;
3613 mock_router_pick_published_address_result = 0;
3614 tt_assert(server_mode(options) == 1);
3615 tt_assert(public_server_mode(options) == 1);
3616 tt_assert(directory_fetches_from_authorities(options) == 0);
3617 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3618 == 0);
3620 /* Dir servers fetch the consensus from the authorities, unless they are not
3621 * advertising themselves (hibernating) or have no routerinfo or are not
3622 * advertising their dirport, and never use multiple directories for
3623 * bootstrap. This only applies if they are also OR servers.
3624 * (We don't care much about the behaviour of non-OR directory servers.) */
3625 memset(options, 0, sizeof(or_options_t));
3626 options->DirPort_set = 1;
3627 options->ORPort_set = 1;
3628 options->DirCache = 1;
3629 mock_router_pick_published_address_result = 0;
3630 mock_router_my_exit_policy_is_reject_star_result = 1;
3632 mock_advertised_server_mode_result = 1;
3633 routerinfo.dir_port = 1;
3634 mock_router_get_my_routerinfo_result = &routerinfo;
3635 tt_assert(server_mode(options) == 1);
3636 tt_assert(public_server_mode(options) == 1);
3637 tt_assert(directory_fetches_from_authorities(options) == 1);
3638 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3639 == 0);
3641 mock_advertised_server_mode_result = 0;
3642 routerinfo.dir_port = 1;
3643 mock_router_get_my_routerinfo_result = &routerinfo;
3644 tt_assert(server_mode(options) == 1);
3645 tt_assert(public_server_mode(options) == 1);
3646 tt_assert(directory_fetches_from_authorities(options) == 0);
3647 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3648 == 0);
3650 mock_advertised_server_mode_result = 1;
3651 mock_router_get_my_routerinfo_result = NULL;
3652 tt_assert(server_mode(options) == 1);
3653 tt_assert(public_server_mode(options) == 1);
3654 tt_assert(directory_fetches_from_authorities(options) == 0);
3655 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3656 == 0);
3658 mock_advertised_server_mode_result = 1;
3659 routerinfo.dir_port = 0;
3660 routerinfo.supports_tunnelled_dir_requests = 0;
3661 mock_router_get_my_routerinfo_result = &routerinfo;
3662 tt_assert(server_mode(options) == 1);
3663 tt_assert(public_server_mode(options) == 1);
3664 tt_assert(directory_fetches_from_authorities(options) == 0);
3665 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3666 == 0);
3668 mock_advertised_server_mode_result = 1;
3669 routerinfo.dir_port = 1;
3670 routerinfo.supports_tunnelled_dir_requests = 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_assert(directory_fetches_from_authorities(options) == 1);
3675 tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
3676 == 0);
3678 done:
3679 tor_free(options);
3680 UNMOCK(router_pick_published_address);
3681 UNMOCK(router_get_my_routerinfo);
3682 UNMOCK(advertised_server_mode);
3683 UNMOCK(router_my_exit_policy_is_reject_star);
3686 static void
3687 test_config_default_fallback_dirs(void *arg)
3689 const char *fallback[] = {
3690 #include "../or/fallback_dirs.inc"
3691 NULL
3694 int n_included_fallback_dirs = 0;
3695 int n_added_fallback_dirs = 0;
3697 (void)arg;
3698 clear_dir_servers();
3700 while (fallback[n_included_fallback_dirs])
3701 n_included_fallback_dirs++;
3703 add_default_fallback_dir_servers();
3705 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
3707 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
3709 done:
3710 clear_dir_servers();
3713 static config_line_t *
3714 mock_config_line(const char *key, const char *val)
3716 config_line_t *config_line = tor_malloc(sizeof(config_line_t));
3717 memset(config_line, 0, sizeof(config_line_t));
3718 config_line->key = tor_strdup(key);
3719 config_line->value = tor_strdup(val);
3720 return config_line;
3723 static void
3724 test_config_parse_port_config__listenaddress(void *data)
3726 (void)data;
3727 int ret;
3728 config_line_t *config_listen_address = NULL, *config_listen_address2 = NULL,
3729 *config_listen_address3 = NULL;
3730 config_line_t *config_port1 = NULL, *config_port2 = NULL,
3731 *config_port3 = NULL, *config_port4 = NULL, *config_port5 = NULL;
3732 smartlist_t *slout = NULL;
3733 port_cfg_t *port_cfg = NULL;
3735 // Test basic invocation with no arguments
3736 ret = parse_port_config(NULL, NULL, NULL, NULL, 0, NULL, 0, 0);
3737 tt_int_op(ret, OP_EQ, 0);
3739 // Setup some test data
3740 config_listen_address = mock_config_line("DNSListenAddress", "127.0.0.1");
3741 config_listen_address2 = mock_config_line("DNSListenAddress", "x$$$:::345");
3742 config_listen_address3 = mock_config_line("DNSListenAddress",
3743 "127.0.0.1:1442");
3744 config_port1 = mock_config_line("DNSPort", "42");
3745 config_port2 = mock_config_line("DNSPort", "43");
3746 config_port1->next = config_port2;
3747 config_port3 = mock_config_line("DNSPort", "auto");
3748 config_port4 = mock_config_line("DNSPort", "55542");
3749 config_port5 = mock_config_line("DNSPort", "666777");
3751 // Test failure when we have a ListenAddress line and several
3752 // Port lines for the same portname
3753 ret = parse_port_config(NULL, config_port1, config_listen_address, "DNS", 0,
3754 NULL, 0, 0);
3756 tt_int_op(ret, OP_EQ, -1);
3758 // Test case when we have a listen address, no default port and allow
3759 // spurious listen address lines
3760 ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
3761 0, CL_PORT_ALLOW_EXTRA_LISTENADDR);
3762 tt_int_op(ret, OP_EQ, 1);
3764 // Test case when we have a listen address, no default port but doesn't
3765 // allow spurious listen address lines
3766 ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
3767 0, 0);
3768 tt_int_op(ret, OP_EQ, -1);
3770 // Test case when we have a listen address, and a port that points to auto,
3771 // should use the AUTO port
3772 slout = smartlist_new();
3773 ret = parse_port_config(slout, config_port3, config_listen_address, "DNS",
3774 0, NULL, 0, 0);
3775 tt_int_op(ret, OP_EQ, 0);
3776 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3777 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3778 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
3780 // Test when we have a listen address and a custom port
3781 ret = parse_port_config(slout, config_port4, config_listen_address, "DNS",
3782 0, NULL, 0, 0);
3783 tt_int_op(ret, OP_EQ, 0);
3784 tt_int_op(smartlist_len(slout), OP_EQ, 2);
3785 port_cfg = (port_cfg_t *)smartlist_get(slout, 1);
3786 tt_int_op(port_cfg->port, OP_EQ, 55542);
3788 // Test when we have a listen address and an invalid custom port
3789 ret = parse_port_config(slout, config_port5, config_listen_address, "DNS",
3790 0, NULL, 0, 0);
3791 tt_int_op(ret, OP_EQ, -1);
3793 // Test we get a server port configuration when asked for it
3794 ret = parse_port_config(slout, NULL, config_listen_address, "DNS", 0, NULL,
3795 123, CL_PORT_SERVER_OPTIONS);
3796 tt_int_op(ret, OP_EQ, 0);
3797 tt_int_op(smartlist_len(slout), OP_EQ, 4);
3798 port_cfg = (port_cfg_t *)smartlist_get(slout, 2);
3799 tt_int_op(port_cfg->port, OP_EQ, 123);
3800 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
3801 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
3803 // Test an invalid ListenAddress configuration
3804 ret = parse_port_config(NULL, NULL, config_listen_address2, "DNS", 0, NULL,
3805 222, 0);
3806 tt_int_op(ret, OP_EQ, -1);
3808 // Test default to the port in the listen address if available
3809 ret = parse_port_config(slout, config_port2, config_listen_address3, "DNS",
3810 0, NULL, 0, 0);
3811 tt_int_op(ret, OP_EQ, 0);
3812 tt_int_op(smartlist_len(slout), OP_EQ, 5);
3813 port_cfg = (port_cfg_t *)smartlist_get(slout, 4);
3814 tt_int_op(port_cfg->port, OP_EQ, 1442);
3816 // Test we work correctly without an out, but with a listen address
3817 // and a port
3818 ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
3819 0, NULL, 0, 0);
3820 tt_int_op(ret, OP_EQ, 0);
3822 // Test warning nonlocal control
3823 ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
3824 CONN_TYPE_CONTROL_LISTENER, NULL, 0,
3825 CL_PORT_WARN_NONLOCAL);
3826 tt_int_op(ret, OP_EQ, 0);
3828 // Test warning nonlocal ext or listener
3829 ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
3830 CONN_TYPE_EXT_OR_LISTENER, NULL, 0,
3831 CL_PORT_WARN_NONLOCAL);
3832 tt_int_op(ret, OP_EQ, 0);
3834 // Test warning nonlocal other
3835 ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
3836 0, NULL, 0, CL_PORT_WARN_NONLOCAL);
3837 tt_int_op(ret, OP_EQ, 0);
3839 // Test warning nonlocal control without an out
3840 ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
3841 CONN_TYPE_CONTROL_LISTENER, NULL, 0,
3842 CL_PORT_WARN_NONLOCAL);
3843 tt_int_op(ret, OP_EQ, 0);
3845 done:
3846 config_free_lines(config_listen_address);
3847 config_free_lines(config_listen_address2);
3848 config_free_lines(config_listen_address3);
3849 config_free_lines(config_port1);
3850 /* 2 was linked from 1. */
3851 config_free_lines(config_port3);
3852 config_free_lines(config_port4);
3853 config_free_lines(config_port5);
3854 if (slout)
3855 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3856 smartlist_free(slout);
3859 static void
3860 test_config_parse_port_config__ports__no_ports_given(void *data)
3862 (void)data;
3863 int ret;
3864 smartlist_t *slout = NULL;
3865 port_cfg_t *port_cfg = NULL;
3867 slout = smartlist_new();
3869 // Test no defaultport, no defaultaddress and no out
3870 ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 0, 0);
3871 tt_int_op(ret, OP_EQ, 0);
3873 // Test with defaultport, no defaultaddress and no out
3874 ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 42, 0);
3875 tt_int_op(ret, OP_EQ, 0);
3877 // Test no defaultport, with defaultaddress and no out
3878 ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3879 tt_int_op(ret, OP_EQ, 0);
3881 // Test with defaultport, with defaultaddress and no out
3882 ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3883 tt_int_op(ret, OP_EQ, 0);
3885 // Test no defaultport, no defaultaddress and with out
3886 ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 0, 0);
3887 tt_int_op(ret, OP_EQ, 0);
3888 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3890 // Test with defaultport, no defaultaddress and with out
3891 ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 42, 0);
3892 tt_int_op(ret, OP_EQ, 0);
3893 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3895 // Test no defaultport, with defaultaddress and with out
3896 ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3897 tt_int_op(ret, OP_EQ, 0);
3898 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3900 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3901 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3902 smartlist_clear(slout);
3903 ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3904 tt_int_op(ret, OP_EQ, 0);
3905 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3906 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3907 tt_int_op(port_cfg->port, OP_EQ, 42);
3908 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
3910 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3911 // for a unix address
3912 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3913 smartlist_clear(slout);
3914 ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "/foo/bar/unixdomain",
3915 42, CL_PORT_IS_UNIXSOCKET);
3916 tt_int_op(ret, OP_EQ, 0);
3917 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3918 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3919 tt_int_op(port_cfg->port, OP_EQ, 0);
3920 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
3921 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
3923 done:
3924 if (slout)
3925 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3926 smartlist_free(slout);
3929 static void
3930 test_config_parse_port_config__ports__ports_given(void *data)
3932 (void)data;
3933 int ret;
3934 smartlist_t *slout = NULL;
3935 port_cfg_t *port_cfg = NULL;
3936 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
3937 tor_addr_t addr;
3939 slout = smartlist_new();
3941 // Test error when encounters an invalid Port specification
3942 config_port_invalid = mock_config_line("DNSPort", "");
3943 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
3944 0, 0);
3945 tt_int_op(ret, OP_EQ, -1);
3947 // Test error when encounters an empty unix domain specification
3948 config_free_lines(config_port_invalid); config_port_invalid = NULL;
3949 config_port_invalid = mock_config_line("DNSPort", "unix:");
3950 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
3951 0, 0);
3952 tt_int_op(ret, OP_EQ, -1);
3954 // Test error when encounters a unix domain specification but the listener
3955 // doesnt support domain sockets
3956 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
3957 ret = parse_port_config(NULL, config_port_valid, NULL, "DNS",
3958 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
3959 tt_int_op(ret, OP_EQ, -1);
3961 // Test valid unix domain
3962 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3963 smartlist_clear(slout);
3964 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
3965 CONN_TYPE_AP_LISTENER, NULL, 0, 0);
3966 #ifdef _WIN32
3967 tt_int_op(ret, OP_EQ, -1);
3968 #else
3969 tt_int_op(ret, OP_EQ, 0);
3970 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3971 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3972 tt_int_op(port_cfg->port, OP_EQ, 0);
3973 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
3974 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
3975 #endif
3977 // Test failure if we have no ipv4 and no ipv6 (for unix domain sockets,
3978 // this makes no sense - it should be fixed)
3979 config_free_lines(config_port_invalid); config_port_invalid = NULL;
3980 config_port_invalid = mock_config_line("DNSPort",
3981 "unix:/tmp/foo/bar NoIPv4Traffic");
3982 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS",
3983 CONN_TYPE_AP_LISTENER, NULL, 0,
3984 CL_PORT_TAKES_HOSTNAMES);
3985 tt_int_op(ret, OP_EQ, -1);
3987 // Test success with no ipv4 but take ipv6 (for unix domain sockets, this
3988 // makes no sense - it should be fixed)
3989 config_free_lines(config_port_valid); config_port_valid = NULL;
3990 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3991 smartlist_clear(slout);
3992 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar "
3993 "NoIPv4Traffic IPv6Traffic");
3994 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
3995 CONN_TYPE_AP_LISTENER, NULL, 0,
3996 CL_PORT_TAKES_HOSTNAMES);
3997 #ifdef _WIN32
3998 tt_int_op(ret, OP_EQ, -1);
3999 #else
4000 tt_int_op(ret, OP_EQ, 0);
4001 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4002 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4003 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4004 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4005 #endif
4007 // Test success with both ipv4 and ipv6 (for unix domain sockets,
4008 // this makes no sense - it should be fixed)
4009 config_free_lines(config_port_valid); config_port_valid = NULL;
4010 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4011 smartlist_clear(slout);
4012 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar "
4013 "IPv4Traffic IPv6Traffic");
4014 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
4015 CONN_TYPE_AP_LISTENER, NULL, 0,
4016 CL_PORT_TAKES_HOSTNAMES);
4017 #ifdef _WIN32
4018 tt_int_op(ret, OP_EQ, -1);
4019 #else
4020 tt_int_op(ret, OP_EQ, 0);
4021 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4022 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4023 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4024 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4025 #endif
4027 // Test failure if we specify world writable for an IP Port
4028 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4029 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
4030 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
4031 "127.0.0.3", 0, 0);
4032 tt_int_op(ret, OP_EQ, -1);
4034 // Test failure if we specify group writable for an IP Port
4035 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4036 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
4037 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
4038 "127.0.0.3", 0, 0);
4039 tt_int_op(ret, OP_EQ, -1);
4041 // Test failure if we specify group writable for an IP Port
4042 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4043 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4044 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
4045 "127.0.0.3", 0, 0);
4046 tt_int_op(ret, OP_EQ, -1);
4048 // Test success with only a port (this will fail without a default address)
4049 config_free_lines(config_port_valid); config_port_valid = NULL;
4050 config_port_valid = mock_config_line("DNSPort", "42");
4051 ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
4052 "127.0.0.3", 0, 0);
4053 tt_int_op(ret, OP_EQ, 0);
4055 // Test success with only a port and isolate destination port
4056 config_free_lines(config_port_valid); config_port_valid = NULL;
4057 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4058 smartlist_clear(slout);
4059 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
4060 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4061 "127.0.0.3", 0, 0);
4062 tt_int_op(ret, OP_EQ, 0);
4063 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4064 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4065 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4066 ISO_DEFAULT | ISO_DESTPORT);
4068 // Test success with a negative isolate destination port, and plural
4069 config_free_lines(config_port_valid); config_port_valid = NULL;
4070 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4071 smartlist_clear(slout);
4072 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4073 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4074 "127.0.0.3", 0, 0);
4075 tt_int_op(ret, OP_EQ, 0);
4076 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4077 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4078 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4079 ISO_DEFAULT & ~ISO_DESTPORT);
4081 // Test success with isolate destination address
4082 config_free_lines(config_port_valid); config_port_valid = NULL;
4083 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4084 smartlist_clear(slout);
4085 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
4086 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4087 "127.0.0.3", 0, 0);
4088 tt_int_op(ret, OP_EQ, 0);
4089 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4090 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4091 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4092 ISO_DEFAULT | ISO_DESTADDR);
4094 // Test success with isolate socks AUTH
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("DNSPort", "42 IsolateSOCKSAuth");
4099 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4100 "127.0.0.3", 0, 0);
4101 tt_int_op(ret, OP_EQ, 0);
4102 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4103 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4104 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4105 ISO_DEFAULT | ISO_SOCKSAUTH);
4107 // Test success with isolate client protocol
4108 config_free_lines(config_port_valid); config_port_valid = NULL;
4109 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4110 smartlist_clear(slout);
4111 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
4112 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4113 "127.0.0.3", 0, 0);
4114 tt_int_op(ret, OP_EQ, 0);
4115 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4116 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4117 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4118 ISO_DEFAULT | ISO_CLIENTPROTO);
4120 // Test success with isolate client address
4121 config_free_lines(config_port_valid); config_port_valid = NULL;
4122 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4123 smartlist_clear(slout);
4124 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
4125 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4126 "127.0.0.3", 0, 0);
4127 tt_int_op(ret, OP_EQ, 0);
4128 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4129 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4130 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4131 ISO_DEFAULT | ISO_CLIENTADDR);
4133 // Test success with ignored unknown options
4134 config_free_lines(config_port_valid); config_port_valid = NULL;
4135 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4136 ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
4137 "127.0.0.3", 0, 0);
4138 tt_int_op(ret, OP_EQ, 0);
4140 // Test success with no isolate socks AUTH
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("DNSPort", "42 NoIsolateSOCKSAuth");
4145 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4146 "127.0.0.3", 0, 0);
4147 tt_int_op(ret, OP_EQ, 0);
4148 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4149 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4150 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4152 // Test success with prefer ipv6
4153 config_free_lines(config_port_valid); config_port_valid = NULL;
4154 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4155 smartlist_clear(slout);
4156 config_port_valid = mock_config_line("DNSPort", "42 IPv6Traffic PreferIPv6");
4157 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
4158 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
4159 CL_PORT_TAKES_HOSTNAMES);
4160 tt_int_op(ret, OP_EQ, 0);
4161 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4162 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4163 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
4165 // Test success with cache ipv4 DNS
4166 config_free_lines(config_port_valid); config_port_valid = NULL;
4167 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4168 smartlist_clear(slout);
4169 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
4170 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4171 "127.0.0.42", 0, 0);
4172 tt_int_op(ret, OP_EQ, 0);
4173 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4174 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4175 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4176 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4178 // Test success with cache ipv6 DNS
4179 config_free_lines(config_port_valid); config_port_valid = NULL;
4180 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4181 smartlist_clear(slout);
4182 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
4183 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4184 "127.0.0.42", 0, 0);
4185 tt_int_op(ret, OP_EQ, 0);
4186 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4187 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4188 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4189 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4191 // Test success with no cache ipv4 DNS
4192 config_free_lines(config_port_valid); config_port_valid = NULL;
4193 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4194 smartlist_clear(slout);
4195 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4196 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4197 "127.0.0.42", 0, 0);
4198 tt_int_op(ret, OP_EQ, 0);
4199 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4200 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4201 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4202 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4204 // Test success with cache DNS
4205 config_free_lines(config_port_valid); config_port_valid = NULL;
4206 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4207 smartlist_clear(slout);
4208 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
4209 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4210 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
4211 tt_int_op(ret, OP_EQ, 0);
4212 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4213 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4214 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4215 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4217 // Test success with use cached ipv4 DNS
4218 config_free_lines(config_port_valid); config_port_valid = NULL;
4219 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4220 smartlist_clear(slout);
4221 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
4222 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4223 "127.0.0.42", 0, 0);
4224 tt_int_op(ret, OP_EQ, 0);
4225 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4226 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4227 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4228 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
4230 // Test success with use cached ipv6 DNS
4231 config_free_lines(config_port_valid); config_port_valid = NULL;
4232 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4233 smartlist_clear(slout);
4234 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
4235 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4236 "127.0.0.42", 0, 0);
4237 tt_int_op(ret, OP_EQ, 0);
4238 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4239 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4240 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
4241 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4243 // Test success with use cached DNS
4244 config_free_lines(config_port_valid); config_port_valid = NULL;
4245 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4246 smartlist_clear(slout);
4247 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
4248 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4249 "127.0.0.42", 0, 0);
4250 tt_int_op(ret, OP_EQ, 0);
4251 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4252 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4253 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4254 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4256 // Test success with not preferring ipv6 automap
4257 config_free_lines(config_port_valid); config_port_valid = NULL;
4258 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4259 smartlist_clear(slout);
4260 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4261 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4262 "127.0.0.42", 0, 0);
4263 tt_int_op(ret, OP_EQ, 0);
4264 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4265 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4266 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
4268 // Test success with prefer SOCKS no auth
4269 config_free_lines(config_port_valid); config_port_valid = NULL;
4270 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4271 smartlist_clear(slout);
4272 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4273 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4274 "127.0.0.42", 0, 0);
4275 tt_int_op(ret, OP_EQ, 0);
4276 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4277 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4278 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4280 // Test failure with both a zero port and a non-zero port
4281 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4282 config_free_lines(config_port_valid); config_port_valid = NULL;
4283 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4284 smartlist_clear(slout);
4285 config_port_invalid = mock_config_line("DNSPort", "0");
4286 config_port_valid = mock_config_line("DNSPort", "42");
4287 config_port_invalid->next = config_port_valid;
4288 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4289 "127.0.0.42", 0, 0);
4290 tt_int_op(ret, OP_EQ, -1);
4292 // Test success with warn non-local control
4293 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4294 smartlist_clear(slout);
4295 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
4296 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
4297 CL_PORT_WARN_NONLOCAL);
4298 tt_int_op(ret, OP_EQ, 0);
4300 // Test success with warn non-local listener
4301 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4302 smartlist_clear(slout);
4303 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
4304 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
4305 CL_PORT_WARN_NONLOCAL);
4306 tt_int_op(ret, OP_EQ, 0);
4308 // Test success with warn non-local other
4309 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4310 smartlist_clear(slout);
4311 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4312 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4313 tt_int_op(ret, OP_EQ, 0);
4315 // Test success with warn non-local other without out
4316 ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
4317 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4318 tt_int_op(ret, OP_EQ, 0);
4320 // Test success with both ipv4 and ipv6 but without stream options
4321 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4322 config_port_valid = NULL;
4323 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4324 smartlist_clear(slout);
4325 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
4326 "IPv6Traffic");
4327 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4328 "127.0.0.44", 0,
4329 CL_PORT_TAKES_HOSTNAMES |
4330 CL_PORT_NO_STREAM_OPTIONS);
4331 tt_int_op(ret, OP_EQ, 0);
4332 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4333 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4334 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4335 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4337 // Test failure for a SessionGroup argument with invalid value
4338 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4339 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4340 smartlist_clear(slout);
4341 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
4342 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4343 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4344 tt_int_op(ret, OP_EQ, -1);
4346 // TODO: this seems wrong. Shouldn't it be the other way around?
4347 // Potential bug.
4348 // Test failure for a SessionGroup argument with valid value but with stream
4349 // options allowed
4350 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4351 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4352 smartlist_clear(slout);
4353 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
4354 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4355 "127.0.0.44", 0, 0);
4356 tt_int_op(ret, OP_EQ, -1);
4358 // Test failure for more than one SessionGroup argument
4359 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4360 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4361 smartlist_clear(slout);
4362 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
4363 "SessionGroup=321");
4364 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4365 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4366 tt_int_op(ret, OP_EQ, -1);
4368 // Test success with a sessiongroup options
4369 config_free_lines(config_port_valid); config_port_valid = NULL;
4370 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4371 smartlist_clear(slout);
4372 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
4373 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4374 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4375 tt_int_op(ret, OP_EQ, 0);
4376 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4377 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4378 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
4380 // Test success with a zero unix domain socket, and doesnt add it to out
4381 config_free_lines(config_port_valid); config_port_valid = NULL;
4382 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4383 smartlist_clear(slout);
4384 config_port_valid = mock_config_line("DNSPort", "0");
4385 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4386 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4387 tt_int_op(ret, OP_EQ, 0);
4388 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4390 // Test success with a one unix domain socket, and doesnt add it to out
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", "something");
4395 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4396 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
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->is_unix_addr, OP_EQ, 1);
4401 tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
4403 // Test success with a port of auto - it uses the default address
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", "auto");
4408 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4409 "127.0.0.46", 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->port, OP_EQ, CFG_AUTO_PORT);
4414 tor_addr_parse(&addr, "127.0.0.46");
4415 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4417 // Test success with parsing both an address and an auto port
4418 config_free_lines(config_port_valid); config_port_valid = NULL;
4419 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4420 smartlist_clear(slout);
4421 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
4422 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4423 "127.0.0.46", 0, 0);
4424 tt_int_op(ret, OP_EQ, 0);
4425 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4426 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4427 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4428 tor_addr_parse(&addr, "127.0.0.122");
4429 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4431 // Test failure when asked to parse an invalid address followed by auto
4432 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4433 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
4434 ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
4435 "127.0.0.46", 0, 0);
4436 tt_int_op(ret, OP_EQ, -1);
4438 // Test success with parsing both an address and a real port
4439 config_free_lines(config_port_valid); config_port_valid = NULL;
4440 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4441 smartlist_clear(slout);
4442 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
4443 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
4444 "127.0.0.46", 0, 0);
4445 tt_int_op(ret, OP_EQ, 0);
4446 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4447 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4448 tt_int_op(port_cfg->port, OP_EQ, 656);
4449 tor_addr_parse(&addr, "127.0.0.123");
4450 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4452 // Test failure if we can't parse anything at all
4453 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4454 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4455 smartlist_clear(slout);
4456 config_port_invalid = mock_config_line("DNSPort", "something wrong");
4457 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4458 "127.0.0.46", 0, 0);
4459 tt_int_op(ret, OP_EQ, -1);
4461 // Test failure if we find both an address, a port and an auto
4462 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4463 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4464 smartlist_clear(slout);
4465 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
4466 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
4467 "127.0.0.46", 0, 0);
4468 tt_int_op(ret, OP_EQ, -1);
4470 // Test that default to group writeable default sets group writeable for
4471 // domain socket
4472 config_free_lines(config_port_valid); config_port_valid = NULL;
4473 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4474 smartlist_clear(slout);
4475 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/somewhere");
4476 ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
4477 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
4478 CL_PORT_DFLT_GROUP_WRITABLE);
4479 #ifdef _WIN32
4480 tt_int_op(ret, OP_EQ, -1);
4481 #else
4482 tt_int_op(ret, OP_EQ, 0);
4483 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4484 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4485 tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
4486 #endif
4488 done:
4489 if (slout)
4490 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4491 smartlist_free(slout);
4492 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4493 config_free_lines(config_port_valid); config_port_valid = NULL;
4496 static void
4497 test_config_parse_port_config__ports__server_options(void *data)
4499 (void)data;
4500 int ret;
4501 smartlist_t *slout = NULL;
4502 port_cfg_t *port_cfg = NULL;
4503 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4505 slout = smartlist_new();
4507 // Test success with NoAdvertise option
4508 config_free_lines(config_port_valid); config_port_valid = NULL;
4509 config_port_valid = mock_config_line("DNSPort",
4510 "127.0.0.124:656 NoAdvertise");
4511 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
4512 CL_PORT_SERVER_OPTIONS);
4513 tt_int_op(ret, OP_EQ, 0);
4514 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4515 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4516 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
4517 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
4519 // Test success with NoListen option
4520 config_free_lines(config_port_valid); config_port_valid = NULL;
4521 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4522 smartlist_clear(slout);
4523 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
4524 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
4525 CL_PORT_SERVER_OPTIONS);
4526 tt_int_op(ret, OP_EQ, 0);
4527 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4528 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4529 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
4530 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
4532 // Test failure with both NoAdvertise and NoListen option
4533 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4534 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4535 smartlist_clear(slout);
4536 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
4537 "NoAdvertise");
4538 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
4539 0, CL_PORT_SERVER_OPTIONS);
4540 tt_int_op(ret, OP_EQ, -1);
4542 // Test success with IPv4Only
4543 config_free_lines(config_port_valid); config_port_valid = NULL;
4544 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4545 smartlist_clear(slout);
4546 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
4547 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
4548 CL_PORT_SERVER_OPTIONS);
4549 tt_int_op(ret, OP_EQ, 0);
4550 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4551 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4552 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
4553 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
4555 // Test success with IPv6Only
4556 config_free_lines(config_port_valid); config_port_valid = NULL;
4557 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4558 smartlist_clear(slout);
4559 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
4560 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
4561 CL_PORT_SERVER_OPTIONS);
4562 tt_int_op(ret, OP_EQ, 0);
4563 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4564 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4565 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
4566 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
4568 // Test failure with both IPv4Only and IPv6Only
4569 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4570 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4571 smartlist_clear(slout);
4572 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
4573 "IPv4Only");
4574 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
4575 0, CL_PORT_SERVER_OPTIONS);
4576 tt_int_op(ret, OP_EQ, -1);
4578 // Test success with invalid parameter
4579 config_free_lines(config_port_valid); config_port_valid = NULL;
4580 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4581 smartlist_clear(slout);
4582 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
4583 ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
4584 CL_PORT_SERVER_OPTIONS);
4585 tt_int_op(ret, OP_EQ, 0);
4586 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4588 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
4589 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4590 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4591 smartlist_clear(slout);
4592 config_port_invalid = mock_config_line("DNSPort",
4593 "127.0.0.124:656 IPv6Only");
4594 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
4595 0, CL_PORT_SERVER_OPTIONS);
4596 tt_int_op(ret, OP_EQ, -1);
4598 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
4599 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4600 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4601 smartlist_clear(slout);
4602 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
4603 ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
4604 0, CL_PORT_SERVER_OPTIONS);
4605 tt_int_op(ret, OP_EQ, -1);
4607 done:
4608 if (slout)
4609 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4610 smartlist_free(slout);
4611 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4612 config_free_lines(config_port_valid); config_port_valid = NULL;
4615 #define CONFIG_TEST(name, flags) \
4616 { #name, test_config_ ## name, flags, NULL, NULL }
4618 struct testcase_t config_tests[] = {
4619 CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
4620 CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
4621 CONFIG_TEST(parsing_trusted_dir_server, 0),
4622 CONFIG_TEST(parsing_fallback_dir_server, 0),
4623 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
4624 CONFIG_TEST(adding_dir_servers, TT_FORK),
4625 CONFIG_TEST(default_dir_servers, TT_FORK),
4626 CONFIG_TEST(default_fallback_dirs, 0),
4627 CONFIG_TEST(resolve_my_address, TT_FORK),
4628 CONFIG_TEST(addressmap, 0),
4629 CONFIG_TEST(parse_bridge_line, 0),
4630 CONFIG_TEST(parse_transport_options_line, 0),
4631 CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
4632 CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
4633 CONFIG_TEST(write_to_data_subdir, TT_FORK),
4634 CONFIG_TEST(fix_my_family, 0),
4635 CONFIG_TEST(directory_fetch, 0),
4636 CONFIG_TEST(parse_port_config__listenaddress, 0),
4637 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
4638 CONFIG_TEST(parse_port_config__ports__server_options, 0),
4639 CONFIG_TEST(parse_port_config__ports__ports_given, 0),
4640 END_OF_TESTCASES