Extract the non-stats part of geoip into a new src/lib/geoip.
[tor.git] / src / test / test_config.c
blobdae4d8376694704d5ee909ea969b379f912a9bc4
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2018, 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 "core/or/or.h"
12 #include "lib/net/address.h"
13 #include "lib/net/resolve.h"
14 #include "feature/client/addressmap.h"
15 #include "feature/client/bridges.h"
16 #include "core/or/circuitmux_ewma.h"
17 #include "core/or/circuitbuild.h"
18 #include "app/config/config.h"
19 #include "app/config/confparse.h"
20 #include "core/mainloop/connection.h"
21 #include "core/or/connection_edge.h"
22 #include "test/test.h"
23 #include "core/or/connection_or.h"
24 #include "feature/control/control.h"
25 #include "core/mainloop/cpuworker.h"
26 #include "feature/dircache/dirserv.h"
27 #include "feature/dirauth/dirvote.h"
28 #include "feature/relay/dns.h"
29 #include "feature/client/entrynodes.h"
30 #include "feature/client/transports.h"
31 #include "feature/relay/ext_orport.h"
32 #include "lib/geoip/geoip.h"
33 #include "feature/hibernate/hibernate.h"
34 #include "core/mainloop/mainloop.h"
35 #include "feature/nodelist/networkstatus.h"
36 #include "feature/nodelist/nodelist.h"
37 #include "core/or/policies.h"
38 #include "feature/rend/rendclient.h"
39 #include "feature/rend/rendservice.h"
40 #include "feature/relay/router.h"
41 #include "feature/relay/routermode.h"
42 #include "feature/nodelist/dirlist.h"
43 #include "feature/nodelist/routerlist.h"
44 #include "feature/nodelist/routerset.h"
45 #include "app/config/statefile.h"
47 #include "test/test_helpers.h"
49 #include "feature/dirclient/dir_server_st.h"
50 #include "core/or/port_cfg_st.h"
51 #include "feature/nodelist/routerinfo_st.h"
53 #include "lib/fs/conffile.h"
54 #include "lib/meminfo/meminfo.h"
55 #include "lib/net/gethostname.h"
56 #include "lib/encoding/confline.h"
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61 #ifdef HAVE_SYS_STAT_H
62 #include <sys/stat.h>
63 #endif
65 static void
66 test_config_addressmap(void *arg)
68 char buf[1024];
69 char address[256];
70 time_t expires = TIME_MAX;
71 (void)arg;
73 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
74 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
75 "MapAddress *.google.com *.torserver.exit\n"
76 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
77 "MapAddress *.cn.com www.cnn.com\n"
78 "MapAddress *.cnn.com www.cnn.com\n"
79 "MapAddress ex.com www.cnn.com\n"
80 "MapAddress ey.com *.cnn.com\n"
81 "MapAddress www.torproject.org 1.1.1.1\n"
82 "MapAddress other.torproject.org "
83 "this.torproject.org.otherserver.exit\n"
84 "MapAddress test.torproject.org 2.2.2.2\n"
85 "MapAddress www.google.com 3.3.3.3\n"
86 "MapAddress www.example.org 4.4.4.4\n"
87 "MapAddress 4.4.4.4 7.7.7.7\n"
88 "MapAddress 4.4.4.4 5.5.5.5\n"
89 "MapAddress www.infiniteloop.org 6.6.6.6\n"
90 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
91 , sizeof(buf));
93 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
94 config_register_addressmaps(get_options());
96 /* Use old interface for now, so we don't need to rewrite the unit tests */
97 #define addressmap_rewrite(a,s,eo,ao) \
98 addressmap_rewrite((a),(s), ~0, (eo),(ao))
100 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
101 strlcpy(address, "www.invalidwildcard.com", sizeof(address));
102 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
104 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
105 strlcpy(address, "www.invalidasterisk.com", sizeof(address));
106 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
108 /* Where no mapping for FQDN match on top-level domain */
109 /* MapAddress .google.com .torserver.exit */
110 strlcpy(address, "reader.google.com", sizeof(address));
111 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
112 tt_str_op(address,OP_EQ, "reader.torserver.exit");
114 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
115 strlcpy(address, "reader.yahoo.com", sizeof(address));
116 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
117 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
119 /*MapAddress *.cnn.com www.cnn.com */
120 strlcpy(address, "cnn.com", sizeof(address));
121 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
122 tt_str_op(address,OP_EQ, "www.cnn.com");
124 /* MapAddress .cn.com www.cnn.com */
125 strlcpy(address, "www.cn.com", sizeof(address));
126 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
127 tt_str_op(address,OP_EQ, "www.cnn.com");
129 /* MapAddress ex.com www.cnn.com - no match */
130 strlcpy(address, "www.ex.com", sizeof(address));
131 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
133 /* MapAddress ey.com *.cnn.com - invalid expression */
134 strlcpy(address, "ey.com", sizeof(address));
135 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
137 /* Where mapping for FQDN match on FQDN */
138 strlcpy(address, "www.google.com", sizeof(address));
139 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
140 tt_str_op(address,OP_EQ, "3.3.3.3");
142 strlcpy(address, "www.torproject.org", sizeof(address));
143 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
144 tt_str_op(address,OP_EQ, "1.1.1.1");
146 strlcpy(address, "other.torproject.org", sizeof(address));
147 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
148 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
150 strlcpy(address, "test.torproject.org", sizeof(address));
151 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
152 tt_str_op(address,OP_EQ, "2.2.2.2");
154 /* Test a chain of address mappings and the order in which they were added:
155 "MapAddress www.example.org 4.4.4.4"
156 "MapAddress 4.4.4.4 7.7.7.7"
157 "MapAddress 4.4.4.4 5.5.5.5"
159 strlcpy(address, "www.example.org", sizeof(address));
160 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
161 tt_str_op(address,OP_EQ, "5.5.5.5");
163 /* Test infinite address mapping results in no change */
164 strlcpy(address, "www.infiniteloop.org", sizeof(address));
165 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
166 tt_str_op(address,OP_EQ, "www.infiniteloop.org");
168 /* Test we don't find false positives */
169 strlcpy(address, "www.example.com", sizeof(address));
170 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
172 /* Test top-level-domain matching a bit harder */
173 config_free_lines(get_options_mutable()->AddressMap);
174 addressmap_clear_configured();
175 strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
176 "MapAddress *.torproject.org 1.1.1.1\n"
177 "MapAddress *.net 2.2.2.2\n"
178 , sizeof(buf));
179 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
180 config_register_addressmaps(get_options());
182 strlcpy(address, "www.abc.com", sizeof(address));
183 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
184 tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
186 strlcpy(address, "www.def.com", sizeof(address));
187 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
188 tt_str_op(address,OP_EQ, "www.def.torserver.exit");
190 strlcpy(address, "www.torproject.org", sizeof(address));
191 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
192 tt_str_op(address,OP_EQ, "1.1.1.1");
194 strlcpy(address, "test.torproject.org", sizeof(address));
195 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
196 tt_str_op(address,OP_EQ, "1.1.1.1");
198 strlcpy(address, "torproject.net", sizeof(address));
199 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
200 tt_str_op(address,OP_EQ, "2.2.2.2");
202 /* We don't support '*' as a mapping directive */
203 config_free_lines(get_options_mutable()->AddressMap);
204 addressmap_clear_configured();
205 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
206 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
207 config_register_addressmaps(get_options());
209 strlcpy(address, "www.abc.com", sizeof(address));
210 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
212 strlcpy(address, "www.def.net", sizeof(address));
213 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
215 strlcpy(address, "www.torproject.org", sizeof(address));
216 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
218 #undef addressmap_rewrite
220 done:
221 config_free_lines(get_options_mutable()->AddressMap);
222 get_options_mutable()->AddressMap = NULL;
223 addressmap_free_all();
226 static int
227 is_private_dir(const char* path)
229 struct stat st;
230 int r = stat(path, &st);
231 if (r) {
232 return 0;
234 #if !defined (_WIN32)
235 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
236 return 0;
238 #endif
239 return 1;
242 static void
243 test_config_check_or_create_data_subdir(void *arg)
245 or_options_t *options = get_options_mutable();
246 char *datadir;
247 const char *subdir = "test_stats";
248 char *subpath;
249 struct stat st;
250 int r;
251 #if !defined (_WIN32)
252 unsigned group_permission;
253 #endif
254 (void)arg;
256 tor_free(options->DataDirectory);
257 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
258 subpath = get_datadir_fname(subdir);
260 #if defined (_WIN32)
261 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
262 #else
263 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
264 #endif
266 r = stat(subpath, &st);
268 // The subdirectory shouldn't exist yet,
269 // but should be created by the call to check_or_create_data_subdir.
270 tt_assert(r && (errno == ENOENT));
271 tt_assert(!check_or_create_data_subdir(subdir));
272 tt_assert(is_private_dir(subpath));
274 // The check should return 0, if the directory already exists
275 // and is private to the user.
276 tt_assert(!check_or_create_data_subdir(subdir));
278 r = stat(subpath, &st);
279 if (r) {
280 tt_abort_perror("stat");
283 #if !defined (_WIN32)
284 group_permission = st.st_mode | 0070;
285 r = chmod(subpath, group_permission);
287 if (r) {
288 tt_abort_perror("chmod");
291 // If the directory exists, but its mode is too permissive
292 // a call to check_or_create_data_subdir should reset the mode.
293 tt_assert(!is_private_dir(subpath));
294 tt_assert(!check_or_create_data_subdir(subdir));
295 tt_assert(is_private_dir(subpath));
296 #endif /* !defined (_WIN32) */
298 done:
299 rmdir(subpath);
300 tor_free(datadir);
301 tor_free(subpath);
304 static void
305 test_config_write_to_data_subdir(void *arg)
307 or_options_t* options = get_options_mutable();
308 char *datadir;
309 char *cp = NULL;
310 const char* subdir = "test_stats";
311 const char* fname = "test_file";
312 const char* str =
313 "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
314 "elitr, sed diam nonumy eirmod\n"
315 "tempor invidunt ut labore et dolore magna aliquyam\n"
316 "erat, sed diam voluptua.\n"
317 "At vero eos et accusam et justo duo dolores et ea\n"
318 "rebum. Stet clita kasd gubergren,\n"
319 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
320 "Lorem ipsum dolor sit amet,\n"
321 "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
322 "tempor invidunt ut labore et dolore\n"
323 "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
324 "accusam et justo duo dolores et\n"
325 "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
326 "sanctus est Lorem ipsum dolor sit amet.";
327 char* filepath = NULL;
328 (void)arg;
330 tor_free(options->DataDirectory);
331 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
332 filepath = get_datadir_fname2(subdir, fname);
334 #if defined (_WIN32)
335 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
336 #else
337 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
338 #endif
340 // Write attempt should fail, if subdirectory doesn't exist.
341 tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
342 tt_assert(! check_or_create_data_subdir(subdir));
344 // Content of file after write attempt should be
345 // equal to the original string.
346 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
347 cp = read_file_to_str(filepath, 0, NULL);
348 tt_str_op(cp,OP_EQ, str);
349 tor_free(cp);
351 // A second write operation should overwrite the old content.
352 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
353 cp = read_file_to_str(filepath, 0, NULL);
354 tt_str_op(cp,OP_EQ, str);
355 tor_free(cp);
357 done:
358 (void) unlink(filepath);
359 rmdir(options->DataDirectory);
360 tor_free(datadir);
361 tor_free(filepath);
362 tor_free(cp);
365 /* Test helper function: Make sure that a bridge line gets parsed
366 * properly. Also make sure that the resulting bridge_line_t structure
367 * has its fields set correctly. */
368 static void
369 good_bridge_line_test(const char *string, const char *test_addrport,
370 const char *test_digest, const char *test_transport,
371 const smartlist_t *test_socks_args)
373 char *tmp = NULL;
374 bridge_line_t *bridge_line = parse_bridge_line(string);
375 tt_assert(bridge_line);
377 /* test addrport */
378 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
379 tt_str_op(test_addrport,OP_EQ, tmp);
380 tor_free(tmp);
382 /* If we were asked to validate a digest, but we did not get a
383 digest after parsing, we failed. */
384 if (test_digest && tor_digest_is_zero(bridge_line->digest))
385 tt_abort();
387 /* If we were not asked to validate a digest, and we got a digest
388 after parsing, we failed again. */
389 if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
390 tt_abort();
392 /* If we were asked to validate a digest, and we got a digest after
393 parsing, make sure it's correct. */
394 if (test_digest) {
395 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
396 tor_strlower(tmp);
397 tt_str_op(test_digest,OP_EQ, tmp);
398 tor_free(tmp);
401 /* If we were asked to validate a transport name, make sure tha it
402 matches with the transport name that was parsed. */
403 if (test_transport && !bridge_line->transport_name)
404 tt_abort();
405 if (!test_transport && bridge_line->transport_name)
406 tt_abort();
407 if (test_transport)
408 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
410 /* Validate the SOCKS argument smartlist. */
411 if (test_socks_args && !bridge_line->socks_args)
412 tt_abort();
413 if (!test_socks_args && bridge_line->socks_args)
414 tt_abort();
415 if (test_socks_args)
416 tt_assert(smartlist_strings_eq(test_socks_args,
417 bridge_line->socks_args));
419 done:
420 tor_free(tmp);
421 bridge_line_free(bridge_line);
424 /* Test helper function: Make sure that a bridge line is
425 * unparseable. */
426 static void
427 bad_bridge_line_test(const char *string)
429 bridge_line_t *bridge_line = parse_bridge_line(string);
430 if (bridge_line)
431 TT_FAIL(("%s was supposed to fail, but it didn't.", string));
432 tt_ptr_op(bridge_line, OP_EQ, NULL);
434 done:
435 bridge_line_free(bridge_line);
438 static void
439 test_config_parse_bridge_line(void *arg)
441 (void) arg;
442 good_bridge_line_test("192.0.2.1:4123",
443 "192.0.2.1:4123", NULL, NULL, NULL);
445 good_bridge_line_test("192.0.2.1",
446 "192.0.2.1:443", NULL, NULL, NULL);
448 good_bridge_line_test("transport [::1]",
449 "[::1]:443", NULL, "transport", NULL);
451 good_bridge_line_test("transport 192.0.2.1:12 "
452 "4352e58420e68f5e40bf7c74faddccd9d1349413",
453 "192.0.2.1:12",
454 "4352e58420e68f5e40bf7c74faddccd9d1349413",
455 "transport", NULL);
458 smartlist_t *sl_tmp = smartlist_new();
459 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
461 good_bridge_line_test("transport 192.0.2.1:12 "
462 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
463 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
464 "transport", sl_tmp);
466 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
467 smartlist_free(sl_tmp);
471 smartlist_t *sl_tmp = smartlist_new();
472 smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
473 smartlist_add_asprintf(sl_tmp, "z=z");
475 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
476 "192.0.2.1:12", NULL, "transport", sl_tmp);
478 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
479 smartlist_free(sl_tmp);
483 smartlist_t *sl_tmp = smartlist_new();
484 smartlist_add_asprintf(sl_tmp, "dub=come");
485 smartlist_add_asprintf(sl_tmp, "save=me");
487 good_bridge_line_test("transport 192.0.2.1:12 "
488 "4352e58420e68f5e40bf7c74faddccd9d1349666 "
489 "dub=come save=me",
491 "192.0.2.1:12",
492 "4352e58420e68f5e40bf7c74faddccd9d1349666",
493 "transport", sl_tmp);
495 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
496 smartlist_free(sl_tmp);
499 good_bridge_line_test("192.0.2.1:1231 "
500 "4352e58420e68f5e40bf7c74faddccd9d1349413",
501 "192.0.2.1:1231",
502 "4352e58420e68f5e40bf7c74faddccd9d1349413",
503 NULL, NULL);
505 /* Empty line */
506 bad_bridge_line_test("");
507 /* bad transport name */
508 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
509 /* weird ip address */
510 bad_bridge_line_test("a.b.c.d");
511 /* invalid fpr */
512 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
513 /* no k=v in the end */
514 bad_bridge_line_test("obfs2 2.2.2.2:1231 "
515 "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
516 /* no addrport */
517 bad_bridge_line_test("asdw");
518 /* huge k=v value that can't fit in SOCKS fields */
519 bad_bridge_line_test(
520 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
521 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
522 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
524 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
525 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
526 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
527 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
528 "aa=b");
531 static void
532 test_config_parse_transport_options_line(void *arg)
534 smartlist_t *options_sl = NULL, *sl_tmp = NULL;
536 (void) arg;
538 { /* too small line */
539 options_sl = get_options_from_transport_options_line("valley", NULL);
540 tt_ptr_op(options_sl, OP_EQ, NULL);
543 { /* no k=v values */
544 options_sl = get_options_from_transport_options_line("hit it!", NULL);
545 tt_ptr_op(options_sl, OP_EQ, NULL);
548 { /* correct line, but wrong transport specified */
549 options_sl =
550 get_options_from_transport_options_line("trebuchet k=v", "rook");
551 tt_ptr_op(options_sl, OP_EQ, NULL);
554 { /* correct -- no transport specified */
555 sl_tmp = smartlist_new();
556 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
557 smartlist_add_asprintf(sl_tmp, "weliketo=party");
559 options_sl =
560 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
561 NULL);
562 tt_assert(options_sl);
563 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
565 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
566 smartlist_free(sl_tmp);
567 sl_tmp = NULL;
568 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
569 smartlist_free(options_sl);
570 options_sl = NULL;
573 { /* correct -- correct transport specified */
574 sl_tmp = smartlist_new();
575 smartlist_add_asprintf(sl_tmp, "ladi=dadi");
576 smartlist_add_asprintf(sl_tmp, "weliketo=party");
578 options_sl =
579 get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
580 "rook");
581 tt_assert(options_sl);
582 tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
583 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
584 smartlist_free(sl_tmp);
585 sl_tmp = NULL;
586 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
587 smartlist_free(options_sl);
588 options_sl = NULL;
591 done:
592 if (options_sl) {
593 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
594 smartlist_free(options_sl);
596 if (sl_tmp) {
597 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
598 smartlist_free(sl_tmp);
602 /* Mocks needed for the compute_max_mem_in_queues test */
603 static int get_total_system_memory_mock(size_t *mem_out);
605 static size_t total_system_memory_output = 0;
606 static int total_system_memory_return = 0;
608 static int
609 get_total_system_memory_mock(size_t *mem_out)
611 if (! mem_out)
612 return -1;
614 *mem_out = total_system_memory_output;
615 return total_system_memory_return;
618 /* Mocks needed for the transport plugin line test */
620 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
621 char **proxy_argv, int is_server);
622 static int transport_add_from_config_mock(const tor_addr_t *addr,
623 uint16_t port, const char *name,
624 int socks_ver);
625 static int transport_is_needed_mock(const char *transport_name);
627 static int pt_kickstart_proxy_mock_call_count = 0;
628 static int transport_add_from_config_mock_call_count = 0;
629 static int transport_is_needed_mock_call_count = 0;
630 static int transport_is_needed_mock_return = 0;
632 static void
633 pt_kickstart_proxy_mock(const smartlist_t *transport_list,
634 char **proxy_argv, int is_server)
636 (void) transport_list;
637 (void) proxy_argv;
638 (void) is_server;
639 /* XXXX check that args are as expected. */
641 ++pt_kickstart_proxy_mock_call_count;
643 free_execve_args(proxy_argv);
646 static int
647 transport_add_from_config_mock(const tor_addr_t *addr,
648 uint16_t port, const char *name,
649 int socks_ver)
651 (void) addr;
652 (void) port;
653 (void) name;
654 (void) socks_ver;
655 /* XXXX check that args are as expected. */
657 ++transport_add_from_config_mock_call_count;
659 return 0;
662 static int
663 transport_is_needed_mock(const char *transport_name)
665 (void) transport_name;
666 /* XXXX check that arg is as expected. */
668 ++transport_is_needed_mock_call_count;
670 return transport_is_needed_mock_return;
674 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
675 * options.
678 static void
679 test_config_parse_transport_plugin_line(void *arg)
681 (void)arg;
683 or_options_t *options = get_options_mutable();
684 int r, tmp;
685 int old_pt_kickstart_proxy_mock_call_count;
686 int old_transport_add_from_config_mock_call_count;
687 int old_transport_is_needed_mock_call_count;
689 /* Bad transport lines - too short */
690 r = parse_transport_line(options, "bad", 1, 0);
691 tt_int_op(r, OP_LT, 0);
692 r = parse_transport_line(options, "bad", 1, 1);
693 tt_int_op(r, OP_LT, 0);
694 r = parse_transport_line(options, "bad bad", 1, 0);
695 tt_int_op(r, OP_LT, 0);
696 r = parse_transport_line(options, "bad bad", 1, 1);
697 tt_int_op(r, OP_LT, 0);
699 /* Test transport list parsing */
700 r = parse_transport_line(options,
701 "transport_1 exec /usr/bin/fake-transport", 1, 0);
702 tt_int_op(r, OP_EQ, 0);
703 r = parse_transport_line(options,
704 "transport_1 exec /usr/bin/fake-transport", 1, 1);
705 tt_int_op(r, OP_EQ, 0);
706 r = parse_transport_line(options,
707 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
708 tt_int_op(r, OP_EQ, 0);
709 r = parse_transport_line(options,
710 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
711 tt_int_op(r, OP_EQ, 0);
712 /* Bad transport identifiers */
713 r = parse_transport_line(options,
714 "transport_* exec /usr/bin/fake-transport", 1, 0);
715 tt_int_op(r, OP_LT, 0);
716 r = parse_transport_line(options,
717 "transport_* exec /usr/bin/fake-transport", 1, 1);
718 tt_int_op(r, OP_LT, 0);
720 /* Check SOCKS cases for client transport */
721 r = parse_transport_line(options,
722 "transport_1 socks4 1.2.3.4:567", 1, 0);
723 tt_int_op(r, OP_EQ, 0);
724 r = parse_transport_line(options,
725 "transport_1 socks5 1.2.3.4:567", 1, 0);
726 tt_int_op(r, OP_EQ, 0);
727 /* Proxy case for server transport */
728 r = parse_transport_line(options,
729 "transport_1 proxy 1.2.3.4:567", 1, 1);
730 tt_int_op(r, OP_EQ, 0);
731 /* Multiple-transport error exit */
732 r = parse_transport_line(options,
733 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
734 tt_int_op(r, OP_LT, 0);
735 r = parse_transport_line(options,
736 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
737 tt_int_op(r, OP_LT, 0);
738 /* No port error exit */
739 r = parse_transport_line(options,
740 "transport_1 socks5 1.2.3.4", 1, 0);
741 tt_int_op(r, OP_LT, 0);
742 r = parse_transport_line(options,
743 "transport_1 proxy 1.2.3.4", 1, 1);
744 tt_int_op(r, OP_LT, 0);
745 /* Unparsable address error exit */
746 r = parse_transport_line(options,
747 "transport_1 socks5 1.2.3:6x7", 1, 0);
748 tt_int_op(r, OP_LT, 0);
749 r = parse_transport_line(options,
750 "transport_1 proxy 1.2.3:6x7", 1, 1);
751 tt_int_op(r, OP_LT, 0);
753 /* "Strange {Client|Server}TransportPlugin field" error exit */
754 r = parse_transport_line(options,
755 "transport_1 foo bar", 1, 0);
756 tt_int_op(r, OP_LT, 0);
757 r = parse_transport_line(options,
758 "transport_1 foo bar", 1, 1);
759 tt_int_op(r, OP_LT, 0);
761 /* No sandbox mode error exit */
762 tmp = options->Sandbox;
763 options->Sandbox = 1;
764 r = parse_transport_line(options,
765 "transport_1 exec /usr/bin/fake-transport", 1, 0);
766 tt_int_op(r, OP_LT, 0);
767 r = parse_transport_line(options,
768 "transport_1 exec /usr/bin/fake-transport", 1, 1);
769 tt_int_op(r, OP_LT, 0);
770 options->Sandbox = tmp;
773 * These final test cases cover code paths that only activate without
774 * validate_only, so they need mocks in place.
776 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
777 old_pt_kickstart_proxy_mock_call_count =
778 pt_kickstart_proxy_mock_call_count;
779 r = parse_transport_line(options,
780 "transport_1 exec /usr/bin/fake-transport", 0, 1);
781 tt_int_op(r, OP_EQ, 0);
782 tt_assert(pt_kickstart_proxy_mock_call_count ==
783 old_pt_kickstart_proxy_mock_call_count + 1);
784 UNMOCK(pt_kickstart_proxy);
786 /* This one hits a log line in the !validate_only case only */
787 r = parse_transport_line(options,
788 "transport_1 proxy 1.2.3.4:567", 0, 1);
789 tt_int_op(r, OP_EQ, 0);
791 /* Check mocked client transport cases */
792 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
793 MOCK(transport_add_from_config, transport_add_from_config_mock);
794 MOCK(transport_is_needed, transport_is_needed_mock);
796 /* Unnecessary transport case */
797 transport_is_needed_mock_return = 0;
798 old_pt_kickstart_proxy_mock_call_count =
799 pt_kickstart_proxy_mock_call_count;
800 old_transport_add_from_config_mock_call_count =
801 transport_add_from_config_mock_call_count;
802 old_transport_is_needed_mock_call_count =
803 transport_is_needed_mock_call_count;
804 r = parse_transport_line(options,
805 "transport_1 exec /usr/bin/fake-transport", 0, 0);
806 /* Should have succeeded */
807 tt_int_op(r, OP_EQ, 0);
808 /* transport_is_needed() should have been called */
809 tt_assert(transport_is_needed_mock_call_count ==
810 old_transport_is_needed_mock_call_count + 1);
812 * pt_kickstart_proxy() and transport_add_from_config() should
813 * not have been called.
815 tt_assert(pt_kickstart_proxy_mock_call_count ==
816 old_pt_kickstart_proxy_mock_call_count);
817 tt_assert(transport_add_from_config_mock_call_count ==
818 old_transport_add_from_config_mock_call_count);
820 /* Necessary transport case */
821 transport_is_needed_mock_return = 1;
822 old_pt_kickstart_proxy_mock_call_count =
823 pt_kickstart_proxy_mock_call_count;
824 old_transport_add_from_config_mock_call_count =
825 transport_add_from_config_mock_call_count;
826 old_transport_is_needed_mock_call_count =
827 transport_is_needed_mock_call_count;
828 r = parse_transport_line(options,
829 "transport_1 exec /usr/bin/fake-transport", 0, 0);
830 /* Should have succeeded */
831 tt_int_op(r, OP_EQ, 0);
833 * transport_is_needed() and pt_kickstart_proxy() should have been
834 * called.
836 tt_assert(pt_kickstart_proxy_mock_call_count ==
837 old_pt_kickstart_proxy_mock_call_count + 1);
838 tt_assert(transport_is_needed_mock_call_count ==
839 old_transport_is_needed_mock_call_count + 1);
840 /* transport_add_from_config() should not have been called. */
841 tt_assert(transport_add_from_config_mock_call_count ==
842 old_transport_add_from_config_mock_call_count);
844 /* proxy case */
845 transport_is_needed_mock_return = 1;
846 old_pt_kickstart_proxy_mock_call_count =
847 pt_kickstart_proxy_mock_call_count;
848 old_transport_add_from_config_mock_call_count =
849 transport_add_from_config_mock_call_count;
850 old_transport_is_needed_mock_call_count =
851 transport_is_needed_mock_call_count;
852 r = parse_transport_line(options,
853 "transport_1 socks5 1.2.3.4:567", 0, 0);
854 /* Should have succeeded */
855 tt_int_op(r, OP_EQ, 0);
857 * transport_is_needed() and transport_add_from_config() should have
858 * been called.
860 tt_assert(transport_add_from_config_mock_call_count ==
861 old_transport_add_from_config_mock_call_count + 1);
862 tt_assert(transport_is_needed_mock_call_count ==
863 old_transport_is_needed_mock_call_count + 1);
864 /* pt_kickstart_proxy() should not have been called. */
865 tt_assert(pt_kickstart_proxy_mock_call_count ==
866 old_pt_kickstart_proxy_mock_call_count);
868 /* Done with mocked client transport cases */
869 UNMOCK(transport_is_needed);
870 UNMOCK(transport_add_from_config);
871 UNMOCK(pt_kickstart_proxy);
873 done:
874 /* Make sure we undo all mocks */
875 UNMOCK(pt_kickstart_proxy);
876 UNMOCK(transport_add_from_config);
877 UNMOCK(transport_is_needed);
879 return;
882 // Tests if an options with MyFamily fingerprints missing '$' normalises
883 // them correctly and also ensure it also works with multiple fingerprints
884 static void
885 test_config_fix_my_family(void *arg)
887 char *err = NULL;
888 config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
889 family->key = tor_strdup("MyFamily");
890 family->value = tor_strdup("$1111111111111111111111111111111111111111, "
891 "1111111111111111111111111111111111111112, "
892 "$1111111111111111111111111111111111111113");
894 config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
895 family2->key = tor_strdup("MyFamily");
896 family2->value = tor_strdup("1111111111111111111111111111111111111114");
898 config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
899 family3->key = tor_strdup("MyFamily");
900 family3->value = tor_strdup("$1111111111111111111111111111111111111115");
902 family->next = family2;
903 family2->next = family3;
904 family3->next = NULL;
906 or_options_t* options = options_new();
907 or_options_t* defaults = options_new();
908 (void) arg;
910 options_init(options);
911 options_init(defaults);
912 options->MyFamily_lines = family;
914 options_validate(NULL, options, defaults, 0, &err) ;
916 if (err != NULL) {
917 TT_FAIL(("options_validate failed: %s", err));
920 const char *valid[] = { "$1111111111111111111111111111111111111111",
921 "$1111111111111111111111111111111111111112",
922 "$1111111111111111111111111111111111111113",
923 "$1111111111111111111111111111111111111114",
924 "$1111111111111111111111111111111111111115" };
925 int ret_size = 0;
926 config_line_t *ret;
927 for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
928 tt_str_op(ret->value, OP_EQ, valid[ret_size]);
929 ret_size++;
931 tt_int_op(ret_size, OP_EQ, 5);
933 done:
934 tor_free(err);
935 or_options_free(options);
936 or_options_free(defaults);
939 static int n_hostname_01010101 = 0;
941 /** This mock function is meant to replace tor_lookup_hostname().
942 * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
943 * This function increments <b>n_hostname_01010101</b> counter by one
944 * every time it is called.
946 static int
947 tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
949 n_hostname_01010101++;
951 if (name && addr) {
952 *addr = ntohl(0x01010101);
955 return 0;
958 static int n_hostname_localhost = 0;
960 /** This mock function is meant to replace tor_lookup_hostname().
961 * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
962 * This function increments <b>n_hostname_localhost</b> counter by one
963 * every time it is called.
965 static int
966 tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
968 n_hostname_localhost++;
970 if (name && addr) {
971 *addr = 0x7f000001;
974 return 0;
977 static int n_hostname_failure = 0;
979 /** This mock function is meant to replace tor_lookup_hostname().
980 * It pretends to fail by returning -1 to caller. Also, this function
981 * increments <b>n_hostname_failure</b> every time it is called.
983 static int
984 tor_lookup_hostname_failure(const char *name, uint32_t *addr)
986 (void)name;
987 (void)addr;
989 n_hostname_failure++;
991 return -1;
994 static int n_gethostname_replacement = 0;
996 /** This mock function is meant to replace tor_gethostname(). It
997 * responds with string "onionrouter!" as hostname. This function
998 * increments <b>n_gethostname_replacement</b> by one every time
999 * it is called.
1001 static int
1002 tor_gethostname_replacement(char *name, size_t namelen)
1004 n_gethostname_replacement++;
1006 if (name && namelen) {
1007 strlcpy(name,"onionrouter!",namelen);
1010 return 0;
1013 static int n_gethostname_localhost = 0;
1015 /** This mock function is meant to replace tor_gethostname(). It
1016 * responds with string "127.0.0.1" as hostname. This function
1017 * increments <b>n_gethostname_localhost</b> by one every time
1018 * it is called.
1020 static int
1021 tor_gethostname_localhost(char *name, size_t namelen)
1023 n_gethostname_localhost++;
1025 if (name && namelen) {
1026 strlcpy(name,"127.0.0.1",namelen);
1029 return 0;
1032 static int n_gethostname_failure = 0;
1034 /** This mock function is meant to replace tor_gethostname.
1035 * It pretends to fail by returning -1. This function increments
1036 * <b>n_gethostname_failure</b> by one every time it is called.
1038 static int
1039 tor_gethostname_failure(char *name, size_t namelen)
1041 (void)name;
1042 (void)namelen;
1043 n_gethostname_failure++;
1045 return -1;
1048 static int n_get_interface_address = 0;
1050 /** This mock function is meant to replace get_interface_address().
1051 * It answers with address 8.8.8.8. This function increments
1052 * <b>n_get_interface_address</b> by one every time it is called.
1054 static int
1055 get_interface_address_08080808(int severity, uint32_t *addr)
1057 (void)severity;
1059 n_get_interface_address++;
1061 if (addr) {
1062 *addr = ntohl(0x08080808);
1065 return 0;
1068 static int n_get_interface_address6 = 0;
1069 static sa_family_t last_address6_family;
1071 /** This mock function is meant to replace get_interface_address6().
1072 * It answers with IP address 9.9.9.9 iff both of the following are true:
1073 * - <b>family</b> is AF_INET
1074 * - <b>addr</b> pointer is not NULL.
1075 * This function increments <b>n_get_interface_address6</b> by one every
1076 * time it is called.
1078 static int
1079 get_interface_address6_replacement(int severity, sa_family_t family,
1080 tor_addr_t *addr)
1082 (void)severity;
1084 last_address6_family = family;
1085 n_get_interface_address6++;
1087 if ((family != AF_INET) || !addr) {
1088 return -1;
1091 tor_addr_from_ipv4h(addr,0x09090909);
1093 return 0;
1096 static int n_get_interface_address_failure = 0;
1099 * This mock function is meant to replace get_interface_address().
1100 * It pretends to fail getting interface address by returning -1.
1101 * <b>n_get_interface_address_failure</b> is incremented by one
1102 * every time this function is called.
1104 static int
1105 get_interface_address_failure(int severity, uint32_t *addr)
1107 (void)severity;
1108 (void)addr;
1110 n_get_interface_address_failure++;
1112 return -1;
1115 static int n_get_interface_address6_failure = 0;
1118 * This mock function is meant to replace get_interface_addres6().
1119 * It will pretend to fail by return -1.
1120 * <b>n_get_interface_address6_failure</b> is incremented by one
1121 * every time this function is called and <b>last_address6_family</b>
1122 * is assigned the value of <b>family</b> argument.
1124 static int
1125 get_interface_address6_failure(int severity, sa_family_t family,
1126 tor_addr_t *addr)
1128 (void)severity;
1129 (void)addr;
1130 n_get_interface_address6_failure++;
1131 last_address6_family = family;
1133 return -1;
1136 static void
1137 test_config_resolve_my_address(void *arg)
1139 or_options_t *options;
1140 uint32_t resolved_addr;
1141 const char *method_used;
1142 char *hostname_out = NULL;
1143 int retval;
1144 int prev_n_hostname_01010101;
1145 int prev_n_hostname_localhost;
1146 int prev_n_hostname_failure;
1147 int prev_n_gethostname_replacement;
1148 int prev_n_gethostname_failure;
1149 int prev_n_gethostname_localhost;
1150 int prev_n_get_interface_address;
1151 int prev_n_get_interface_address_failure;
1152 int prev_n_get_interface_address6;
1153 int prev_n_get_interface_address6_failure;
1155 (void)arg;
1157 options = options_new();
1159 options_init(options);
1162 * CASE 1:
1163 * If options->Address is a valid IPv4 address string, we want
1164 * the corresponding address to be parsed and returned.
1167 options->Address = tor_strdup("128.52.128.105");
1169 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1170 &method_used,&hostname_out);
1172 tt_want(retval == 0);
1173 tt_want_str_op(method_used,OP_EQ,"CONFIGURED");
1174 tt_want(hostname_out == NULL);
1175 tt_assert(resolved_addr == 0x80348069);
1177 tor_free(options->Address);
1180 * CASE 2:
1181 * If options->Address is a valid DNS address, we want resolve_my_address()
1182 * function to ask tor_lookup_hostname() for help with resolving it
1183 * and return the address that was resolved (in host order).
1186 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1188 tor_free(options->Address);
1189 options->Address = tor_strdup("www.torproject.org");
1191 prev_n_hostname_01010101 = n_hostname_01010101;
1193 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1194 &method_used,&hostname_out);
1196 tt_want(retval == 0);
1197 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1198 tt_want_str_op(method_used,OP_EQ,"RESOLVED");
1199 tt_want_str_op(hostname_out,OP_EQ,"www.torproject.org");
1200 tt_assert(resolved_addr == 0x01010101);
1202 UNMOCK(tor_lookup_hostname);
1204 tor_free(options->Address);
1205 tor_free(hostname_out);
1208 * CASE 3:
1209 * Given that options->Address is NULL, we want resolve_my_address()
1210 * to try and use tor_gethostname() to get hostname AND use
1211 * tor_lookup_hostname() to get IP address.
1214 resolved_addr = 0;
1215 tor_free(options->Address);
1216 options->Address = NULL;
1218 MOCK(tor_gethostname,tor_gethostname_replacement);
1219 MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
1221 prev_n_gethostname_replacement = n_gethostname_replacement;
1222 prev_n_hostname_01010101 = n_hostname_01010101;
1224 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1225 &method_used,&hostname_out);
1227 tt_want(retval == 0);
1228 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1229 tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
1230 tt_want_str_op(method_used,OP_EQ,"GETHOSTNAME");
1231 tt_want_str_op(hostname_out,OP_EQ,"onionrouter!");
1232 tt_assert(resolved_addr == 0x01010101);
1234 UNMOCK(tor_gethostname);
1235 UNMOCK(tor_lookup_hostname);
1237 tor_free(hostname_out);
1240 * CASE 4:
1241 * Given that options->Address is a local host address, we want
1242 * resolve_my_address() function to fail.
1245 resolved_addr = 0;
1246 tor_free(options->Address);
1247 options->Address = tor_strdup("127.0.0.1");
1249 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1250 &method_used,&hostname_out);
1252 tt_want(resolved_addr == 0);
1253 tt_int_op(retval, OP_EQ, -1);
1255 tor_free(options->Address);
1256 tor_free(hostname_out);
1259 * CASE 5:
1260 * We want resolve_my_address() to fail if DNS address in options->Address
1261 * cannot be resolved.
1264 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1266 prev_n_hostname_failure = n_hostname_failure;
1268 tor_free(options->Address);
1269 options->Address = tor_strdup("www.tor-project.org");
1271 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1272 &method_used,&hostname_out);
1274 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1275 tt_int_op(retval, OP_EQ, -1);
1277 UNMOCK(tor_lookup_hostname);
1279 tor_free(options->Address);
1280 tor_free(hostname_out);
1283 * CASE 6:
1284 * If options->Address is NULL AND gettting local hostname fails, we want
1285 * resolve_my_address() to fail as well.
1288 MOCK(tor_gethostname,tor_gethostname_failure);
1290 prev_n_gethostname_failure = n_gethostname_failure;
1292 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1293 &method_used,&hostname_out);
1295 tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
1296 tt_int_op(retval, OP_EQ, -1);
1298 UNMOCK(tor_gethostname);
1299 tor_free(hostname_out);
1302 * CASE 7:
1303 * We want resolve_my_address() to try and get network interface address via
1304 * get_interface_address() if hostname returned by tor_gethostname() cannot be
1305 * resolved into IP address.
1308 MOCK(tor_gethostname,tor_gethostname_replacement);
1309 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1310 MOCK(get_interface_address,get_interface_address_08080808);
1312 prev_n_gethostname_replacement = n_gethostname_replacement;
1313 prev_n_get_interface_address = n_get_interface_address;
1315 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1316 &method_used,&hostname_out);
1318 tt_want(retval == 0);
1319 tt_want_int_op(n_gethostname_replacement, OP_EQ,
1320 prev_n_gethostname_replacement + 1);
1321 tt_want_int_op(n_get_interface_address, OP_EQ,
1322 prev_n_get_interface_address + 1);
1323 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1324 tt_want(hostname_out == NULL);
1325 tt_assert(resolved_addr == 0x08080808);
1327 UNMOCK(get_interface_address);
1328 tor_free(hostname_out);
1331 * CASE 8:
1332 * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
1333 * is unresolvable. We want resolve_my_address to fail if
1334 * get_interface_address() fails.
1337 MOCK(get_interface_address,get_interface_address_failure);
1339 prev_n_get_interface_address_failure = n_get_interface_address_failure;
1340 prev_n_gethostname_replacement = n_gethostname_replacement;
1342 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1343 &method_used,&hostname_out);
1345 tt_want(n_get_interface_address_failure ==
1346 prev_n_get_interface_address_failure + 1);
1347 tt_want(n_gethostname_replacement ==
1348 prev_n_gethostname_replacement + 1);
1349 tt_int_op(retval, OP_EQ, -1);
1351 UNMOCK(get_interface_address);
1352 tor_free(hostname_out);
1355 * CASE 9:
1356 * Given that options->Address is NULL AND tor_lookup_hostname()
1357 * fails AND hostname returned by gethostname() resolves
1358 * to local IP address, we want resolve_my_address() function to
1359 * call get_interface_address6(.,AF_INET,.) and return IP address
1360 * the latter function has found.
1363 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1364 MOCK(tor_gethostname,tor_gethostname_replacement);
1365 MOCK(get_interface_address6,get_interface_address6_replacement);
1367 prev_n_gethostname_replacement = n_gethostname_replacement;
1368 prev_n_hostname_failure = n_hostname_failure;
1369 prev_n_get_interface_address6 = n_get_interface_address6;
1371 retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
1372 &method_used,&hostname_out);
1374 tt_want(last_address6_family == AF_INET);
1375 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1376 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1377 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1378 tt_want(retval == 0);
1379 tt_want_str_op(method_used,OP_EQ,"INTERFACE");
1380 tt_assert(resolved_addr == 0x09090909);
1382 UNMOCK(tor_lookup_hostname);
1383 UNMOCK(tor_gethostname);
1384 UNMOCK(get_interface_address6);
1386 tor_free(hostname_out);
1389 * CASE 10: We want resolve_my_address() to fail if all of the following
1390 * are true:
1391 * 1. options->Address is not NULL
1392 * 2. ... but it cannot be converted to struct in_addr by
1393 * tor_inet_aton()
1394 * 3. ... and tor_lookup_hostname() fails to resolve the
1395 * options->Address
1398 MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
1400 prev_n_hostname_failure = n_hostname_failure;
1402 tor_free(options->Address);
1403 options->Address = tor_strdup("some_hostname");
1405 retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
1406 &method_used,&hostname_out);
1408 tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
1409 tt_int_op(retval, OP_EQ, -1);
1411 UNMOCK(tor_gethostname);
1412 UNMOCK(tor_lookup_hostname);
1414 tor_free(hostname_out);
1417 * CASE 11:
1418 * Suppose the following sequence of events:
1419 * 1. options->Address is NULL
1420 * 2. tor_gethostname() succeeds to get hostname of machine Tor
1421 * if running on.
1422 * 3. Hostname from previous step cannot be converted to
1423 * address by using tor_inet_aton() function.
1424 * 4. However, tor_lookup_hostname() succeeds in resolving the
1425 * hostname from step 2.
1426 * 5. Unfortunately, tor_addr_is_internal() deems this address
1427 * to be internal.
1428 * 6. get_interface_address6(.,AF_INET,.) returns non-internal
1429 * IPv4
1431 * We want resolve_my_addr() to succeed with method "INTERFACE"
1432 * and address from step 6.
1435 tor_free(options->Address);
1436 options->Address = NULL;
1438 MOCK(tor_gethostname,tor_gethostname_replacement);
1439 MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
1440 MOCK(get_interface_address6,get_interface_address6_replacement);
1442 prev_n_gethostname_replacement = n_gethostname_replacement;
1443 prev_n_hostname_localhost = n_hostname_localhost;
1444 prev_n_get_interface_address6 = n_get_interface_address6;
1446 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1447 &method_used,&hostname_out);
1449 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1450 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1451 tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
1453 tt_str_op(method_used,OP_EQ,"INTERFACE");
1454 tt_ptr_op(hostname_out, OP_EQ, NULL);
1455 tt_int_op(retval, OP_EQ, 0);
1458 * CASE 11b:
1459 * 1-5 as above.
1460 * 6. get_interface_address6() fails.
1462 * In this subcase, we want resolve_my_address() to fail.
1465 UNMOCK(get_interface_address6);
1466 MOCK(get_interface_address6,get_interface_address6_failure);
1468 prev_n_gethostname_replacement = n_gethostname_replacement;
1469 prev_n_hostname_localhost = n_hostname_localhost;
1470 prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
1472 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1473 &method_used,&hostname_out);
1475 tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
1476 tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
1477 tt_want(n_get_interface_address6_failure ==
1478 prev_n_get_interface_address6_failure + 1);
1480 tt_int_op(retval, OP_EQ, -1);
1482 UNMOCK(tor_gethostname);
1483 UNMOCK(tor_lookup_hostname);
1484 UNMOCK(get_interface_address6);
1486 /* CASE 12:
1487 * Suppose the following happens:
1488 * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
1489 * 2. tor_gethostname() succeeds in getting hostname of a machine ...
1490 * 3. ... which is successfully parsed by tor_inet_aton() ...
1491 * 4. into IPv4 address that tor_addr_is_inernal() considers to be
1492 * internal.
1494 * In this case, we want resolve_my_address() to fail.
1497 tor_free(options->Address);
1498 options->Address = NULL;
1499 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
1501 MOCK(tor_gethostname,tor_gethostname_localhost);
1503 prev_n_gethostname_localhost = n_gethostname_localhost;
1505 retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
1506 &method_used,&hostname_out);
1508 tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
1509 tt_int_op(retval, OP_EQ, -1);
1511 UNMOCK(tor_gethostname);
1513 done:
1514 tor_free(options->Address);
1515 tor_free(options->DirAuthorities);
1516 or_options_free(options);
1517 tor_free(hostname_out);
1519 UNMOCK(tor_gethostname);
1520 UNMOCK(tor_lookup_hostname);
1521 UNMOCK(get_interface_address);
1522 UNMOCK(get_interface_address6);
1523 UNMOCK(tor_gethostname);
1526 static void
1527 test_config_adding_trusted_dir_server(void *arg)
1529 (void)arg;
1531 const char digest[DIGEST_LEN] = "";
1532 dir_server_t *ds = NULL;
1533 tor_addr_port_t ipv6;
1534 int rv = -1;
1536 clear_dir_servers();
1537 routerlist_free_all();
1539 /* create a trusted ds without an IPv6 address and port */
1540 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1541 NULL, V3_DIRINFO, 1.0);
1542 tt_assert(ds);
1543 dir_server_add(ds);
1544 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
1545 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1547 /* create a trusted ds with an IPv6 address and port */
1548 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1549 tt_int_op(rv, OP_EQ, 0);
1550 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
1551 NULL, V3_DIRINFO, 1.0);
1552 tt_assert(ds);
1553 dir_server_add(ds);
1554 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
1555 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1557 done:
1558 clear_dir_servers();
1559 routerlist_free_all();
1562 static void
1563 test_config_adding_fallback_dir_server(void *arg)
1565 (void)arg;
1567 const char digest[DIGEST_LEN] = "";
1568 dir_server_t *ds = NULL;
1569 tor_addr_t ipv4;
1570 tor_addr_port_t ipv6;
1571 int rv = -1;
1573 clear_dir_servers();
1574 routerlist_free_all();
1576 rv = tor_addr_parse(&ipv4, "127.0.0.1");
1577 tt_int_op(rv, OP_EQ, AF_INET);
1579 /* create a trusted ds without an IPv6 address and port */
1580 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
1581 tt_assert(ds);
1582 dir_server_add(ds);
1583 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1585 /* create a trusted ds with an IPv6 address and port */
1586 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
1587 tt_int_op(rv, OP_EQ, 0);
1588 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
1589 tt_assert(ds);
1590 dir_server_add(ds);
1591 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
1593 done:
1594 clear_dir_servers();
1595 routerlist_free_all();
1598 /* No secrets here:
1599 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1600 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
1601 * with added spaces
1603 #define TEST_DIR_AUTH_LINE_START \
1604 "foobar orport=12345 " \
1605 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1606 #define TEST_DIR_AUTH_LINE_END \
1607 "1.2.3.4:54321 " \
1608 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1609 #define TEST_DIR_AUTH_IPV6_FLAG \
1610 "ipv6=[feed::beef]:9 "
1612 static void
1613 test_config_parsing_trusted_dir_server(void *arg)
1615 (void)arg;
1616 int rv = -1;
1618 /* parse a trusted dir server without an IPv6 address and port */
1619 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1620 TEST_DIR_AUTH_LINE_END,
1621 V3_DIRINFO, 1);
1622 tt_int_op(rv, OP_EQ, 0);
1624 /* parse a trusted dir server with an IPv6 address and port */
1625 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1626 TEST_DIR_AUTH_IPV6_FLAG
1627 TEST_DIR_AUTH_LINE_END,
1628 V3_DIRINFO, 1);
1629 tt_int_op(rv, OP_EQ, 0);
1631 /* Since we are only validating, there is no cleanup. */
1632 done:
1636 #undef TEST_DIR_AUTH_LINE_START
1637 #undef TEST_DIR_AUTH_LINE_END
1638 #undef TEST_DIR_AUTH_IPV6_FLAG
1640 #define TEST_DIR_AUTH_LINE_START \
1641 "foobar orport=12345 " \
1642 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
1643 #define TEST_DIR_AUTH_LINE_END_BAD_IP \
1644 "0.256.3.4:54321 " \
1645 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1646 #define TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR \
1647 "torproject.org:54321 " \
1648 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
1650 static void
1651 test_config_parsing_invalid_dir_address(void *arg)
1653 (void)arg;
1654 int rv;
1656 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1657 TEST_DIR_AUTH_LINE_END_BAD_IP,
1658 V3_DIRINFO, 1);
1659 tt_int_op(rv, OP_EQ, -1);
1661 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
1662 TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR,
1663 V3_DIRINFO, 1);
1664 tt_int_op(rv, OP_EQ, -1);
1666 done:
1667 return;
1670 #undef TEST_DIR_AUTH_LINE_START
1671 #undef TEST_DIR_AUTH_LINE_END_BAD_IP
1672 #undef TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR
1674 /* No secrets here:
1675 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
1677 #define TEST_DIR_FALLBACK_LINE \
1678 "1.2.3.4:54321 orport=12345 " \
1679 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
1680 #define TEST_DIR_FALLBACK_IPV6_FLAG \
1681 "ipv6=[2015:c0de::deed]:9"
1683 static void
1684 test_config_parsing_fallback_dir_server(void *arg)
1686 (void)arg;
1687 int rv = -1;
1689 /* parse a trusted dir server without an IPv6 address and port */
1690 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
1691 tt_int_op(rv, OP_EQ, 0);
1693 /* parse a trusted dir server with an IPv6 address and port */
1694 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
1695 TEST_DIR_FALLBACK_IPV6_FLAG,
1697 tt_int_op(rv, OP_EQ, 0);
1699 /* Since we are only validating, there is no cleanup. */
1700 done:
1704 #undef TEST_DIR_FALLBACK_LINE
1705 #undef TEST_DIR_FALLBACK_IPV6_FLAG
1707 static void
1708 test_config_adding_default_trusted_dir_servers(void *arg)
1710 (void)arg;
1712 clear_dir_servers();
1713 routerlist_free_all();
1715 /* Assume we only have one bridge authority */
1716 add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
1717 tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
1718 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
1720 /* Assume we have eight V3 authorities */
1721 add_default_trusted_dir_authorities(V3_DIRINFO);
1722 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
1723 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
1725 done:
1726 clear_dir_servers();
1727 routerlist_free_all();
1730 static int n_add_default_fallback_dir_servers_known_default = 0;
1733 * This mock function is meant to replace add_default_fallback_dir_servers().
1734 * It will parse and add one known default fallback dir server,
1735 * which has a dir_port of 99.
1736 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
1737 * one every time this function is called.
1739 static void
1740 add_default_fallback_dir_servers_known_default(void)
1742 int i;
1743 const char *fallback[] = {
1744 "127.0.0.1:60099 orport=9009 "
1745 "id=0923456789012345678901234567890123456789",
1746 NULL
1748 for (i=0; fallback[i]; i++) {
1749 if (parse_dir_fallback_line(fallback[i], 0)<0) {
1750 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
1751 fallback[i]);
1754 n_add_default_fallback_dir_servers_known_default++;
1757 /* Test all the different combinations of adding dir servers */
1758 static void
1759 test_config_adding_dir_servers(void *arg)
1761 (void)arg;
1763 /* allocate options */
1764 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
1766 /* Allocate and populate configuration lines:
1768 * Use the same format as the hard-coded directories in
1769 * add_default_trusted_dir_authorities().
1770 * Zeroing the structure has the same effect as initialising to:
1771 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
1773 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
1774 test_dir_authority->key = tor_strdup("DirAuthority");
1775 test_dir_authority->value = tor_strdup(
1776 "D0 orport=9000 "
1777 "v3ident=0023456789012345678901234567890123456789 "
1778 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1781 config_line_t *test_alt_bridge_authority = tor_malloc_zero(
1782 sizeof(config_line_t));
1783 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
1784 test_alt_bridge_authority->value = tor_strdup(
1785 "B1 orport=9001 bridge "
1786 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1789 config_line_t *test_alt_dir_authority = tor_malloc_zero(
1790 sizeof(config_line_t));
1791 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
1792 test_alt_dir_authority->value = tor_strdup(
1793 "A2 orport=9002 "
1794 "v3ident=0223456789012345678901234567890123456789 "
1795 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
1798 /* Use the format specified in the manual page */
1799 config_line_t *test_fallback_directory = tor_malloc_zero(
1800 sizeof(config_line_t));
1801 test_fallback_directory->key = tor_strdup("FallbackDir");
1802 test_fallback_directory->value = tor_strdup(
1803 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
1806 /* We need to know if add_default_fallback_dir_servers is called,
1807 * whatever the size of the list in fallback_dirs.inc,
1808 * so we use a version of add_default_fallback_dir_servers that adds
1809 * one known default fallback directory. */
1810 MOCK(add_default_fallback_dir_servers,
1811 add_default_fallback_dir_servers_known_default);
1813 /* There are 16 different cases, covering each combination of set/NULL for:
1814 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
1815 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
1816 * But validate_dir_servers() ensures that:
1817 * "You cannot set both DirAuthority and Alternate*Authority."
1818 * This reduces the number of cases to 10.
1820 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
1821 * So 1001 or case 9 is:
1822 * DirAuthorities set,
1823 * AlternateBridgeAuthority NULL,
1824 * AlternateDirAuthority NULL
1825 * FallbackDir set
1826 * The valid cases are cases 0-9 counting using this method, as every case
1827 * greater than or equal to 10 = 1010 is invalid.
1829 * 1. Outcome: Use Set Directory Authorities
1830 * - No Default Authorities
1831 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1832 * if they are set
1833 * Cases expected to yield this outcome:
1834 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1835 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1836 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1838 * 2. Outcome: Use Set Bridge Authority
1839 * - Use Default Non-Bridge Directory Authorities
1840 * - Use FallbackDir if it is set, otherwise use default FallbackDir
1841 * Cases expected to yield this outcome:
1842 * 4 & 5 (the 2 cases where DirAuthorities is NULL,
1843 * AlternateBridgeAuthority is set, and
1844 * AlternateDirAuthority is NULL)
1846 * 3. Outcome: Use Set Alternate Directory Authority
1847 * - Use Default Bridge Authorities
1848 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
1849 * Cases expected to yield this outcome:
1850 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
1851 * are both NULL, but AlternateDirAuthority is set)
1853 * 4. Outcome: Use Set Custom Fallback Directory
1854 * - Use Default Bridge & Directory Authorities
1855 * Cases expected to yield this outcome:
1856 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
1857 * are all NULL, but FallbackDir is set)
1859 * 5. Outcome: Use All Defaults
1860 * - Use Default Bridge & Directory Authorities, and
1861 * Default Fallback Directories
1862 * Cases expected to yield this outcome:
1863 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
1864 * and FallbackDir are all NULL)
1868 * Find out how many default Bridge, Non-Bridge and Fallback Directories
1869 * are hard-coded into this build.
1870 * This code makes some assumptions about the implementation.
1871 * If they are wrong, one or more of cases 0-5 could fail.
1873 int n_default_alt_bridge_authority = 0;
1874 int n_default_alt_dir_authority = 0;
1875 int n_default_fallback_dir = 0;
1876 #define n_default_authorities ((n_default_alt_bridge_authority) \
1877 + (n_default_alt_dir_authority))
1879 /* Pre-Count Number of Authorities of Each Type
1880 * Use 0000: No Directory Authorities or Fallback Directories Set
1883 /* clear fallback dirs counter */
1884 n_add_default_fallback_dir_servers_known_default = 0;
1886 /* clear options*/
1887 memset(options, 0, sizeof(or_options_t));
1889 /* clear any previous dir servers:
1890 consider_adding_dir_servers() should do this anyway */
1891 clear_dir_servers();
1893 /* assign options: 0000 */
1894 options->DirAuthorities = NULL;
1895 options->AlternateBridgeAuthority = NULL;
1896 options->AlternateDirAuthority = NULL;
1897 options->FallbackDir = NULL;
1898 options->UseDefaultFallbackDirs = 1;
1900 /* parse options - ensure we always update by passing NULL old_options */
1901 consider_adding_dir_servers(options, NULL);
1903 /* check outcome */
1905 /* we must have added the default fallback dirs */
1906 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
1908 /* we have more fallbacks than just the authorities */
1909 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1912 /* fallback_dir_servers */
1913 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
1915 /* Count Bridge Authorities */
1916 SMARTLIST_FOREACH(fallback_servers,
1917 dir_server_t *,
1919 /* increment the found counter if it's a bridge auth */
1920 n_default_alt_bridge_authority +=
1921 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
1922 1 : 0)
1924 /* If we have no default bridge authority, something has gone wrong */
1925 tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
1927 /* Count v3 Authorities */
1928 SMARTLIST_FOREACH(fallback_servers,
1929 dir_server_t *,
1931 /* increment found counter if it's a v3 auth */
1932 n_default_alt_dir_authority +=
1933 ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
1934 1 : 0)
1936 /* If we have no default authorities, something has gone really wrong */
1937 tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
1939 /* Calculate Fallback Directory Count */
1940 n_default_fallback_dir = (smartlist_len(fallback_servers) -
1941 n_default_alt_bridge_authority -
1942 n_default_alt_dir_authority);
1943 /* If we have a negative count, something has gone really wrong,
1944 * or some authorities aren't being added as fallback directories.
1945 * (networkstatus_consensus_can_use_extra_fallbacks depends on all
1946 * authorities being fallback directories.) */
1947 tt_int_op(n_default_fallback_dir, OP_GE, 0);
1952 * 1. Outcome: Use Set Directory Authorities
1953 * - No Default Authorities
1954 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
1955 * if they are set
1956 * Cases expected to yield this outcome:
1957 * 8 & 9 (the 2 valid cases where DirAuthorities is set)
1958 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
1959 * AlternateBridgeAuthority and AlternateDirAuthority are both set)
1962 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
1963 AlternateDirAuthority Not Set, FallbackDir Set */
1965 /* clear fallback dirs counter */
1966 n_add_default_fallback_dir_servers_known_default = 0;
1968 /* clear options*/
1969 memset(options, 0, sizeof(or_options_t));
1971 /* clear any previous dir servers:
1972 consider_adding_dir_servers() should do this anyway */
1973 clear_dir_servers();
1975 /* assign options: 1001 */
1976 options->DirAuthorities = test_dir_authority;
1977 options->AlternateBridgeAuthority = NULL;
1978 options->AlternateDirAuthority = NULL;
1979 options->FallbackDir = test_fallback_directory;
1980 options->UseDefaultFallbackDirs = 1;
1982 /* parse options - ensure we always update by passing NULL old_options */
1983 consider_adding_dir_servers(options, NULL);
1985 /* check outcome */
1987 /* we must not have added the default fallback dirs */
1988 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
1990 /* we have more fallbacks than just the authorities */
1991 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
1994 /* trusted_dir_servers */
1995 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
1996 /* D0, (No B1), (No A2) */
1997 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
1999 /* DirAuthority - D0 - dir_port: 60090 */
2000 int found_D0 = 0;
2001 SMARTLIST_FOREACH(dir_servers,
2002 dir_server_t *,
2004 /* increment the found counter if dir_port matches */
2005 found_D0 +=
2006 (ds->dir_port == 60090 ?
2007 1 : 0)
2009 tt_int_op(found_D0, OP_EQ, 1);
2011 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2012 int found_B1 = 0;
2013 SMARTLIST_FOREACH(dir_servers,
2014 dir_server_t *,
2016 /* increment the found counter if dir_port matches */
2017 found_B1 +=
2018 (ds->dir_port == 60091 ?
2019 1 : 0)
2021 tt_int_op(found_B1, OP_EQ, 0);
2023 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2024 int found_A2 = 0;
2025 SMARTLIST_FOREACH(dir_servers,
2026 dir_server_t *,
2028 /* increment the found counter if dir_port matches */
2029 found_A2 +=
2030 (ds->dir_port == 60092 ?
2031 1 : 0)
2033 tt_int_op(found_A2, OP_EQ, 0);
2037 /* fallback_dir_servers */
2038 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2039 /* D0, (No B1), (No A2), Custom Fallback */
2040 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2042 /* DirAuthority - D0 - dir_port: 60090 */
2043 int found_D0 = 0;
2044 SMARTLIST_FOREACH(fallback_servers,
2045 dir_server_t *,
2047 /* increment the found counter if dir_port matches */
2048 found_D0 +=
2049 (ds->dir_port == 60090 ?
2050 1 : 0)
2052 tt_int_op(found_D0, OP_EQ, 1);
2054 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2055 int found_B1 = 0;
2056 SMARTLIST_FOREACH(fallback_servers,
2057 dir_server_t *,
2059 /* increment the found counter if dir_port matches */
2060 found_B1 +=
2061 (ds->dir_port == 60091 ?
2062 1 : 0)
2064 tt_int_op(found_B1, OP_EQ, 0);
2066 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2067 int found_A2 = 0;
2068 SMARTLIST_FOREACH(fallback_servers,
2069 dir_server_t *,
2071 /* increment the found counter if dir_port matches */
2072 found_A2 +=
2073 (ds->dir_port == 60092 ?
2074 1 : 0)
2076 tt_int_op(found_A2, OP_EQ, 0);
2078 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2079 int found_non_default_fallback = 0;
2080 SMARTLIST_FOREACH(fallback_servers,
2081 dir_server_t *,
2083 /* increment the found counter if dir_port matches */
2084 found_non_default_fallback +=
2085 (ds->dir_port == 60093 ?
2086 1 : 0)
2088 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2090 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2091 int found_default_fallback = 0;
2092 SMARTLIST_FOREACH(fallback_servers,
2093 dir_server_t *,
2095 /* increment the found counter if dir_port matches */
2096 found_default_fallback +=
2097 (ds->dir_port == 60099 ?
2098 1 : 0)
2100 tt_int_op(found_default_fallback, OP_EQ, 0);
2104 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
2106 /* clear fallback dirs counter */
2107 n_add_default_fallback_dir_servers_known_default = 0;
2109 /* clear options*/
2110 memset(options, 0, sizeof(or_options_t));
2112 /* clear any previous dir servers:
2113 consider_adding_dir_servers() should do this anyway */
2114 clear_dir_servers();
2116 /* assign options: 1000 */
2117 options->DirAuthorities = test_dir_authority;
2118 options->AlternateBridgeAuthority = NULL;
2119 options->AlternateDirAuthority = NULL;
2120 options->FallbackDir = NULL;
2121 options->UseDefaultFallbackDirs = 1;
2123 /* parse options - ensure we always update by passing NULL old_options */
2124 consider_adding_dir_servers(options, NULL);
2126 /* check outcome */
2128 /* we must not have added the default fallback dirs */
2129 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2131 /* we just have the authorities */
2132 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2135 /* trusted_dir_servers */
2136 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2137 /* D0, (No B1), (No A2) */
2138 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
2140 /* DirAuthority - D0 - dir_port: 60090 */
2141 int found_D0 = 0;
2142 SMARTLIST_FOREACH(dir_servers,
2143 dir_server_t *,
2145 /* increment the found counter if dir_port matches */
2146 found_D0 +=
2147 (ds->dir_port == 60090 ?
2148 1 : 0)
2150 tt_int_op(found_D0, OP_EQ, 1);
2152 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2153 int found_B1 = 0;
2154 SMARTLIST_FOREACH(dir_servers,
2155 dir_server_t *,
2157 /* increment the found counter if dir_port matches */
2158 found_B1 +=
2159 (ds->dir_port == 60091 ?
2160 1 : 0)
2162 tt_int_op(found_B1, OP_EQ, 0);
2164 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2165 int found_A2 = 0;
2166 SMARTLIST_FOREACH(dir_servers,
2167 dir_server_t *,
2169 /* increment the found counter if dir_port matches */
2170 found_A2 +=
2171 (ds->dir_port == 60092 ?
2172 1 : 0)
2174 tt_int_op(found_A2, OP_EQ, 0);
2178 /* fallback_dir_servers */
2179 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2180 /* D0, (No B1), (No A2), (No Fallback) */
2181 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
2183 /* DirAuthority - D0 - dir_port: 60090 */
2184 int found_D0 = 0;
2185 SMARTLIST_FOREACH(fallback_servers,
2186 dir_server_t *,
2188 /* increment the found counter if dir_port matches */
2189 found_D0 +=
2190 (ds->dir_port == 60090 ?
2191 1 : 0)
2193 tt_int_op(found_D0, OP_EQ, 1);
2195 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2196 int found_B1 = 0;
2197 SMARTLIST_FOREACH(fallback_servers,
2198 dir_server_t *,
2200 /* increment the found counter if dir_port matches */
2201 found_B1 +=
2202 (ds->dir_port == 60091 ?
2203 1 : 0)
2205 tt_int_op(found_B1, OP_EQ, 0);
2207 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2208 int found_A2 = 0;
2209 SMARTLIST_FOREACH(fallback_servers,
2210 dir_server_t *,
2212 /* increment the found counter if dir_port matches */
2213 found_A2 +=
2214 (ds->dir_port == 60092 ?
2215 1 : 0)
2217 tt_int_op(found_A2, OP_EQ, 0);
2219 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2220 int found_non_default_fallback = 0;
2221 SMARTLIST_FOREACH(fallback_servers,
2222 dir_server_t *,
2224 /* increment the found counter if dir_port matches */
2225 found_non_default_fallback +=
2226 (ds->dir_port == 60093 ?
2227 1 : 0)
2229 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2231 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2232 int found_default_fallback = 0;
2233 SMARTLIST_FOREACH(fallback_servers,
2234 dir_server_t *,
2236 /* increment the found counter if dir_port matches */
2237 found_default_fallback +=
2238 (ds->dir_port == 60099 ?
2239 1 : 0)
2241 tt_int_op(found_default_fallback, OP_EQ, 0);
2245 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
2247 /* clear fallback dirs counter */
2248 n_add_default_fallback_dir_servers_known_default = 0;
2250 /* clear options*/
2251 memset(options, 0, sizeof(or_options_t));
2253 /* clear any previous dir servers:
2254 consider_adding_dir_servers() should do this anyway */
2255 clear_dir_servers();
2257 /* assign options: 0111 */
2258 options->DirAuthorities = NULL;
2259 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2260 options->AlternateDirAuthority = test_alt_dir_authority;
2261 options->FallbackDir = test_fallback_directory;
2262 options->UseDefaultFallbackDirs = 1;
2264 /* parse options - ensure we always update by passing NULL old_options */
2265 consider_adding_dir_servers(options, NULL);
2267 /* check outcome */
2269 /* we must not have added the default fallback dirs */
2270 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2272 /* we have more fallbacks than just the authorities */
2273 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2276 /* trusted_dir_servers */
2277 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2278 /* (No D0), B1, A2 */
2279 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2281 /* (No DirAuthority) - D0 - dir_port: 60090 */
2282 int found_D0 = 0;
2283 SMARTLIST_FOREACH(dir_servers,
2284 dir_server_t *,
2286 /* increment the found counter if dir_port matches */
2287 found_D0 +=
2288 (ds->dir_port == 60090 ?
2289 1 : 0)
2291 tt_int_op(found_D0, OP_EQ, 0);
2293 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2294 int found_B1 = 0;
2295 SMARTLIST_FOREACH(dir_servers,
2296 dir_server_t *,
2298 /* increment the found counter if dir_port matches */
2299 found_B1 +=
2300 (ds->dir_port == 60091 ?
2301 1 : 0)
2303 tt_int_op(found_B1, OP_EQ, 1);
2305 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2306 int found_A2 = 0;
2307 SMARTLIST_FOREACH(dir_servers,
2308 dir_server_t *,
2310 /* increment the found counter if dir_port matches */
2311 found_A2 +=
2312 (ds->dir_port == 60092 ?
2313 1 : 0)
2315 tt_int_op(found_A2, OP_EQ, 1);
2319 /* fallback_dir_servers */
2320 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2321 /* (No D0), B1, A2, Custom Fallback */
2322 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
2324 /* (No DirAuthority) - D0 - dir_port: 60090 */
2325 int found_D0 = 0;
2326 SMARTLIST_FOREACH(fallback_servers,
2327 dir_server_t *,
2329 /* increment the found counter if dir_port matches */
2330 found_D0 +=
2331 (ds->dir_port == 60090 ?
2332 1 : 0)
2334 tt_int_op(found_D0, OP_EQ, 0);
2336 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2337 int found_B1 = 0;
2338 SMARTLIST_FOREACH(fallback_servers,
2339 dir_server_t *,
2341 /* increment the found counter if dir_port matches */
2342 found_B1 +=
2343 (ds->dir_port == 60091 ?
2344 1 : 0)
2346 tt_int_op(found_B1, OP_EQ, 1);
2348 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2349 int found_A2 = 0;
2350 SMARTLIST_FOREACH(fallback_servers,
2351 dir_server_t *,
2353 /* increment the found counter if dir_port matches */
2354 found_A2 +=
2355 (ds->dir_port == 60092 ?
2356 1 : 0)
2358 tt_int_op(found_A2, OP_EQ, 1);
2360 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2361 int found_non_default_fallback = 0;
2362 SMARTLIST_FOREACH(fallback_servers,
2363 dir_server_t *,
2365 /* increment the found counter if dir_port matches */
2366 found_non_default_fallback +=
2367 (ds->dir_port == 60093 ?
2368 1 : 0)
2370 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2372 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2373 int found_default_fallback = 0;
2374 SMARTLIST_FOREACH(fallback_servers,
2375 dir_server_t *,
2377 /* increment the found counter if dir_port matches */
2378 found_default_fallback +=
2379 (ds->dir_port == 60099 ?
2380 1 : 0)
2382 tt_int_op(found_default_fallback, OP_EQ, 0);
2386 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
2387 AlternateDirAuthority Set, FallbackDir Not Set */
2389 /* clear fallback dirs counter */
2390 n_add_default_fallback_dir_servers_known_default = 0;
2392 /* clear options*/
2393 memset(options, 0, sizeof(or_options_t));
2395 /* clear any previous dir servers:
2396 consider_adding_dir_servers() should do this anyway */
2397 clear_dir_servers();
2399 /* assign options: 0110 */
2400 options->DirAuthorities = NULL;
2401 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2402 options->AlternateDirAuthority = test_alt_dir_authority;
2403 options->FallbackDir = NULL;
2404 options->UseDefaultFallbackDirs = 1;
2406 /* parse options - ensure we always update by passing NULL old_options */
2407 consider_adding_dir_servers(options, NULL);
2409 /* check outcome */
2411 /* we must not have added the default fallback dirs */
2412 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2414 /* we have more fallbacks than just the authorities */
2415 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
2418 /* trusted_dir_servers */
2419 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2420 /* (No D0), B1, A2 */
2421 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
2423 /* (No DirAuthority) - D0 - dir_port: 60090 */
2424 int found_D0 = 0;
2425 SMARTLIST_FOREACH(dir_servers,
2426 dir_server_t *,
2428 /* increment the found counter if dir_port matches */
2429 found_D0 +=
2430 (ds->dir_port == 60090 ?
2431 1 : 0)
2433 tt_int_op(found_D0, OP_EQ, 0);
2435 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2436 int found_B1 = 0;
2437 SMARTLIST_FOREACH(dir_servers,
2438 dir_server_t *,
2440 /* increment the found counter if dir_port matches */
2441 found_B1 +=
2442 (ds->dir_port == 60091 ?
2443 1 : 0)
2445 tt_int_op(found_B1, OP_EQ, 1);
2447 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2448 int found_A2 = 0;
2449 SMARTLIST_FOREACH(dir_servers,
2450 dir_server_t *,
2452 /* increment the found counter if dir_port matches */
2453 found_A2 +=
2454 (ds->dir_port == 60092 ?
2455 1 : 0)
2457 tt_int_op(found_A2, OP_EQ, 1);
2461 /* fallback_dir_servers */
2462 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2463 /* (No D0), B1, A2, (No Fallback) */
2464 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
2466 /* (No DirAuthority) - D0 - dir_port: 60090 */
2467 int found_D0 = 0;
2468 SMARTLIST_FOREACH(fallback_servers,
2469 dir_server_t *,
2471 /* increment the found counter if dir_port matches */
2472 found_D0 +=
2473 (ds->dir_port == 60090 ?
2474 1 : 0)
2476 tt_int_op(found_D0, OP_EQ, 0);
2478 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2479 int found_B1 = 0;
2480 SMARTLIST_FOREACH(fallback_servers,
2481 dir_server_t *,
2483 /* increment the found counter if dir_port matches */
2484 found_B1 +=
2485 (ds->dir_port == 60091 ?
2486 1 : 0)
2488 tt_int_op(found_B1, OP_EQ, 1);
2490 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2491 int found_A2 = 0;
2492 SMARTLIST_FOREACH(fallback_servers,
2493 dir_server_t *,
2495 /* increment the found counter if dir_port matches */
2496 found_A2 +=
2497 (ds->dir_port == 60092 ?
2498 1 : 0)
2500 tt_int_op(found_A2, OP_EQ, 1);
2502 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2503 int found_non_default_fallback = 0;
2504 SMARTLIST_FOREACH(fallback_servers,
2505 dir_server_t *,
2507 /* increment the found counter if dir_port matches */
2508 found_non_default_fallback +=
2509 (ds->dir_port == 60093 ?
2510 1 : 0)
2512 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2514 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2515 int found_default_fallback = 0;
2516 SMARTLIST_FOREACH(fallback_servers,
2517 dir_server_t *,
2519 /* increment the found counter if dir_port matches */
2520 found_default_fallback +=
2521 (ds->dir_port == 60099 ?
2522 1 : 0)
2524 tt_int_op(found_default_fallback, OP_EQ, 0);
2529 2. Outcome: Use Set Bridge Authority
2530 - Use Default Non-Bridge Directory Authorities
2531 - Use FallbackDir if it is set, otherwise use default FallbackDir
2532 Cases expected to yield this outcome:
2533 4 & 5 (the 2 cases where DirAuthorities is NULL,
2534 AlternateBridgeAuthority is set, and
2535 AlternateDirAuthority is NULL)
2538 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2539 AlternateDirAuthority Not Set, FallbackDir Set */
2541 /* clear fallback dirs counter */
2542 n_add_default_fallback_dir_servers_known_default = 0;
2544 /* clear options*/
2545 memset(options, 0, sizeof(or_options_t));
2547 /* clear any previous dir servers:
2548 consider_adding_dir_servers() should do this anyway */
2549 clear_dir_servers();
2551 /* assign options: 0101 */
2552 options->DirAuthorities = NULL;
2553 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2554 options->AlternateDirAuthority = NULL;
2555 options->FallbackDir = test_fallback_directory;
2556 options->UseDefaultFallbackDirs = 1;
2558 /* parse options - ensure we always update by passing NULL old_options */
2559 consider_adding_dir_servers(options, NULL);
2561 /* check outcome */
2563 /* we must not have added the default fallback dirs */
2564 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2566 /* we have more fallbacks than just the authorities */
2567 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2570 /* trusted_dir_servers */
2571 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2572 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2573 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2575 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2576 int found_D0 = 0;
2577 SMARTLIST_FOREACH(dir_servers,
2578 dir_server_t *,
2580 /* increment the found counter if dir_port matches */
2581 found_D0 +=
2582 (ds->dir_port == 60090 ?
2583 1 : 0)
2585 tt_int_op(found_D0, OP_EQ, 0);
2587 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2588 int found_B1 = 0;
2589 SMARTLIST_FOREACH(dir_servers,
2590 dir_server_t *,
2592 /* increment the found counter if dir_port matches */
2593 found_B1 +=
2594 (ds->dir_port == 60091 ?
2595 1 : 0)
2597 tt_int_op(found_B1, OP_EQ, 1);
2599 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2600 int found_A2 = 0;
2601 SMARTLIST_FOREACH(dir_servers,
2602 dir_server_t *,
2604 /* increment the found counter if dir_port matches */
2605 found_A2 +=
2606 (ds->dir_port == 60092 ?
2607 1 : 0)
2609 tt_int_op(found_A2, OP_EQ, 0);
2611 /* There's no easy way of checking that we have included all the
2612 * default v3 non-Bridge directory authorities, so let's assume that
2613 * if the total count above is correct, we have the right ones.
2618 /* fallback_dir_servers */
2619 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2620 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2621 * Custom Fallback */
2622 tt_assert(smartlist_len(fallback_servers) ==
2623 2 + n_default_alt_dir_authority);
2625 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2626 int found_D0 = 0;
2627 SMARTLIST_FOREACH(fallback_servers,
2628 dir_server_t *,
2630 /* increment the found counter if dir_port matches */
2631 found_D0 +=
2632 (ds->dir_port == 60090 ?
2633 1 : 0)
2635 tt_int_op(found_D0, OP_EQ, 0);
2637 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2638 int found_B1 = 0;
2639 SMARTLIST_FOREACH(fallback_servers,
2640 dir_server_t *,
2642 /* increment the found counter if dir_port matches */
2643 found_B1 +=
2644 (ds->dir_port == 60091 ?
2645 1 : 0)
2647 tt_int_op(found_B1, OP_EQ, 1);
2649 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2650 int found_A2 = 0;
2651 SMARTLIST_FOREACH(fallback_servers,
2652 dir_server_t *,
2654 /* increment the found counter if dir_port matches */
2655 found_A2 +=
2656 (ds->dir_port == 60092 ?
2657 1 : 0)
2659 tt_int_op(found_A2, OP_EQ, 0);
2661 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2662 int found_non_default_fallback = 0;
2663 SMARTLIST_FOREACH(fallback_servers,
2664 dir_server_t *,
2666 /* increment the found counter if dir_port matches */
2667 found_non_default_fallback +=
2668 (ds->dir_port == 60093 ?
2669 1 : 0)
2671 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2673 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2674 int found_default_fallback = 0;
2675 SMARTLIST_FOREACH(fallback_servers,
2676 dir_server_t *,
2678 /* increment the found counter if dir_port matches */
2679 found_default_fallback +=
2680 (ds->dir_port == 60099 ?
2681 1 : 0)
2683 tt_int_op(found_default_fallback, OP_EQ, 0);
2685 /* There's no easy way of checking that we have included all the
2686 * default v3 non-Bridge directory authorities, so let's assume that
2687 * if the total count above is correct, we have the right ones.
2692 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
2693 AlternateDirAuthority & FallbackDir Not Set */
2695 /* clear fallback dirs counter */
2696 n_add_default_fallback_dir_servers_known_default = 0;
2698 /* clear options*/
2699 memset(options, 0, sizeof(or_options_t));
2701 /* clear any previous dir servers:
2702 consider_adding_dir_servers() should do this anyway */
2703 clear_dir_servers();
2705 /* assign options: 0100 */
2706 options->DirAuthorities = NULL;
2707 options->AlternateBridgeAuthority = test_alt_bridge_authority;
2708 options->AlternateDirAuthority = NULL;
2709 options->FallbackDir = NULL;
2710 options->UseDefaultFallbackDirs = 1;
2712 /* parse options - ensure we always update by passing NULL old_options */
2713 consider_adding_dir_servers(options, NULL);
2715 /* check outcome */
2717 /* we must have added the default fallback dirs */
2718 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
2720 /* we have more fallbacks than just the authorities */
2721 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2724 /* trusted_dir_servers */
2725 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2726 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
2727 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
2729 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2730 int found_D0 = 0;
2731 SMARTLIST_FOREACH(dir_servers,
2732 dir_server_t *,
2734 /* increment the found counter if dir_port matches */
2735 found_D0 +=
2736 (ds->dir_port == 60090 ?
2737 1 : 0)
2739 tt_int_op(found_D0, OP_EQ, 0);
2741 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2742 int found_B1 = 0;
2743 SMARTLIST_FOREACH(dir_servers,
2744 dir_server_t *,
2746 /* increment the found counter if dir_port matches */
2747 found_B1 +=
2748 (ds->dir_port == 60091 ?
2749 1 : 0)
2751 tt_int_op(found_B1, OP_EQ, 1);
2753 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2754 int found_A2 = 0;
2755 SMARTLIST_FOREACH(dir_servers,
2756 dir_server_t *,
2758 /* increment the found counter if dir_port matches */
2759 found_A2 +=
2760 (ds->dir_port == 60092 ?
2761 1 : 0)
2763 tt_int_op(found_A2, OP_EQ, 0);
2765 /* There's no easy way of checking that we have included all the
2766 * default v3 non-Bridge directory authorities, so let's assume that
2767 * if the total count above is correct, we have the right ones.
2772 /* fallback_dir_servers */
2773 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2774 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
2775 * Default Fallback */
2776 tt_assert(smartlist_len(fallback_servers) ==
2777 2 + n_default_alt_dir_authority);
2779 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2780 int found_D0 = 0;
2781 SMARTLIST_FOREACH(fallback_servers,
2782 dir_server_t *,
2784 /* increment the found counter if dir_port matches */
2785 found_D0 +=
2786 (ds->dir_port == 60090 ?
2787 1 : 0)
2789 tt_int_op(found_D0, OP_EQ, 0);
2791 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
2792 int found_B1 = 0;
2793 SMARTLIST_FOREACH(fallback_servers,
2794 dir_server_t *,
2796 /* increment the found counter if dir_port matches */
2797 found_B1 +=
2798 (ds->dir_port == 60091 ?
2799 1 : 0)
2801 tt_int_op(found_B1, OP_EQ, 1);
2803 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
2804 int found_A2 = 0;
2805 SMARTLIST_FOREACH(fallback_servers,
2806 dir_server_t *,
2808 /* increment the found counter if dir_port matches */
2809 found_A2 +=
2810 (ds->dir_port == 60092 ?
2811 1 : 0)
2813 tt_int_op(found_A2, OP_EQ, 0);
2815 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
2816 int found_non_default_fallback = 0;
2817 SMARTLIST_FOREACH(fallback_servers,
2818 dir_server_t *,
2820 /* increment the found counter if dir_port matches */
2821 found_non_default_fallback +=
2822 (ds->dir_port == 60093 ?
2823 1 : 0)
2825 tt_int_op(found_non_default_fallback, OP_EQ, 0);
2827 /* Default FallbackDir - No Nickname - dir_port: 60099 */
2828 int found_default_fallback = 0;
2829 SMARTLIST_FOREACH(fallback_servers,
2830 dir_server_t *,
2832 /* increment the found counter if dir_port matches */
2833 found_default_fallback +=
2834 (ds->dir_port == 60099 ?
2835 1 : 0)
2837 tt_int_op(found_default_fallback, OP_EQ, 1);
2839 /* There's no easy way of checking that we have included all the
2840 * default v3 non-Bridge directory authorities, so let's assume that
2841 * if the total count above is correct, we have the right ones.
2847 3. Outcome: Use Set Alternate Directory Authority
2848 - Use Default Bridge Authorities
2849 - Use FallbackDir if it is set, otherwise No Default Fallback Directories
2850 Cases expected to yield this outcome:
2851 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
2852 are both NULL, but AlternateDirAuthority is set)
2855 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
2856 AlternateDirAuthority & FallbackDir Set */
2858 /* clear fallback dirs counter */
2859 n_add_default_fallback_dir_servers_known_default = 0;
2861 /* clear options*/
2862 memset(options, 0, sizeof(or_options_t));
2864 /* clear any previous dir servers:
2865 consider_adding_dir_servers() should do this anyway */
2866 clear_dir_servers();
2868 /* assign options: 0011 */
2869 options->DirAuthorities = NULL;
2870 options->AlternateBridgeAuthority = NULL;
2871 options->AlternateDirAuthority = test_alt_dir_authority;
2872 options->FallbackDir = test_fallback_directory;
2873 options->UseDefaultFallbackDirs = 1;
2875 /* parse options - ensure we always update by passing NULL old_options */
2876 consider_adding_dir_servers(options, NULL);
2878 /* check outcome */
2880 /* we must not have added the default fallback dirs */
2881 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
2883 /* we have more fallbacks than just the authorities */
2884 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
2887 /* trusted_dir_servers */
2888 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
2889 /* (No D0), (No B1), Default Bridge Authorities, A2 */
2890 tt_assert(smartlist_len(dir_servers) ==
2891 1 + n_default_alt_bridge_authority);
2893 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2894 int found_D0 = 0;
2895 SMARTLIST_FOREACH(dir_servers,
2896 dir_server_t *,
2898 /* increment the found counter if dir_port matches */
2899 found_D0 +=
2900 (ds->dir_port == 60090 ?
2901 1 : 0)
2903 tt_int_op(found_D0, OP_EQ, 0);
2905 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2906 int found_B1 = 0;
2907 SMARTLIST_FOREACH(dir_servers,
2908 dir_server_t *,
2910 /* increment the found counter if dir_port matches */
2911 found_B1 +=
2912 (ds->dir_port == 60091 ?
2913 1 : 0)
2915 tt_int_op(found_B1, OP_EQ, 0);
2917 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2918 int found_A2 = 0;
2919 SMARTLIST_FOREACH(dir_servers,
2920 dir_server_t *,
2922 /* increment the found counter if dir_port matches */
2923 found_A2 +=
2924 (ds->dir_port == 60092 ?
2925 1 : 0)
2927 tt_int_op(found_A2, OP_EQ, 1);
2929 /* There's no easy way of checking that we have included all the
2930 * default Bridge authorities (except for hard-coding tonga's details),
2931 * so let's assume that if the total count above is correct,
2932 * we have the right ones.
2937 /* fallback_dir_servers */
2938 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
2939 /* (No D0), (No B1), Default Bridge Authorities, A2,
2940 * Custom Fallback Directory, (No Default Fallback Directories) */
2941 tt_assert(smartlist_len(fallback_servers) ==
2942 2 + n_default_alt_bridge_authority);
2944 /* (No DirAuthorities) - D0 - dir_port: 60090 */
2945 int found_D0 = 0;
2946 SMARTLIST_FOREACH(fallback_servers,
2947 dir_server_t *,
2949 /* increment the found counter if dir_port matches */
2950 found_D0 +=
2951 (ds->dir_port == 60090 ?
2952 1 : 0)
2954 tt_int_op(found_D0, OP_EQ, 0);
2956 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
2957 int found_B1 = 0;
2958 SMARTLIST_FOREACH(fallback_servers,
2959 dir_server_t *,
2961 /* increment the found counter if dir_port matches */
2962 found_B1 +=
2963 (ds->dir_port == 60091 ?
2964 1 : 0)
2966 tt_int_op(found_B1, OP_EQ, 0);
2968 /* AlternateDirAuthority - A2 - dir_port: 60092 */
2969 int found_A2 = 0;
2970 SMARTLIST_FOREACH(fallback_servers,
2971 dir_server_t *,
2973 /* increment the found counter if dir_port matches */
2974 found_A2 +=
2975 (ds->dir_port == 60092 ?
2976 1 : 0)
2978 tt_int_op(found_A2, OP_EQ, 1);
2980 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
2981 int found_non_default_fallback = 0;
2982 SMARTLIST_FOREACH(fallback_servers,
2983 dir_server_t *,
2985 /* increment the found counter if dir_port matches */
2986 found_non_default_fallback +=
2987 (ds->dir_port == 60093 ?
2988 1 : 0)
2990 tt_int_op(found_non_default_fallback, OP_EQ, 1);
2992 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
2993 int found_default_fallback = 0;
2994 SMARTLIST_FOREACH(fallback_servers,
2995 dir_server_t *,
2997 /* increment the found counter if dir_port matches */
2998 found_default_fallback +=
2999 (ds->dir_port == 60099 ?
3000 1 : 0)
3002 tt_int_op(found_default_fallback, OP_EQ, 0);
3004 /* There's no easy way of checking that we have included all the
3005 * default Bridge authorities (except for hard-coding tonga's details),
3006 * so let's assume that if the total count above is correct,
3007 * we have the right ones.
3012 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
3013 AlternateDirAuthority Set, FallbackDir Not Set */
3015 /* clear fallback dirs counter */
3016 n_add_default_fallback_dir_servers_known_default = 0;
3018 /* clear options*/
3019 memset(options, 0, sizeof(or_options_t));
3021 /* clear any previous dir servers:
3022 consider_adding_dir_servers() should do this anyway */
3023 clear_dir_servers();
3025 /* assign options: 0010 */
3026 options->DirAuthorities = NULL;
3027 options->AlternateBridgeAuthority = NULL;
3028 options->AlternateDirAuthority = test_alt_dir_authority;
3029 options->FallbackDir = NULL;
3030 options->UseDefaultFallbackDirs = 1;
3032 /* parse options - ensure we always update by passing NULL old_options */
3033 consider_adding_dir_servers(options, NULL);
3035 /* check outcome */
3037 /* we must not have added the default fallback dirs */
3038 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3040 /* we just have the authorities */
3041 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
3044 /* trusted_dir_servers */
3045 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3046 /* (No D0), (No B1), Default Bridge Authorities, A2,
3047 * No Default or Custom Fallback Directories */
3048 tt_assert(smartlist_len(dir_servers) ==
3049 1 + n_default_alt_bridge_authority);
3051 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3052 int found_D0 = 0;
3053 SMARTLIST_FOREACH(dir_servers,
3054 dir_server_t *,
3056 /* increment the found counter if dir_port matches */
3057 found_D0 +=
3058 (ds->dir_port == 60090 ?
3059 1 : 0)
3061 tt_int_op(found_D0, OP_EQ, 0);
3063 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3064 int found_B1 = 0;
3065 SMARTLIST_FOREACH(dir_servers,
3066 dir_server_t *,
3068 /* increment the found counter if dir_port matches */
3069 found_B1 +=
3070 (ds->dir_port == 60091 ?
3071 1 : 0)
3073 tt_int_op(found_B1, OP_EQ, 0);
3075 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3076 int found_A2 = 0;
3077 SMARTLIST_FOREACH(dir_servers,
3078 dir_server_t *,
3080 /* increment the found counter if dir_port matches */
3081 found_A2 +=
3082 (ds->dir_port == 60092 ?
3083 1 : 0)
3085 tt_int_op(found_A2, OP_EQ, 1);
3087 /* There's no easy way of checking that we have included all the
3088 * default Bridge authorities (except for hard-coding tonga's details),
3089 * so let's assume that if the total count above is correct,
3090 * we have the right ones.
3095 /* fallback_dir_servers */
3096 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3097 /* (No D0), (No B1), Default Bridge Authorities, A2,
3098 * No Custom or Default Fallback Directories */
3099 tt_assert(smartlist_len(fallback_servers) ==
3100 1 + n_default_alt_bridge_authority);
3102 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3103 int found_D0 = 0;
3104 SMARTLIST_FOREACH(fallback_servers,
3105 dir_server_t *,
3107 /* increment the found counter if dir_port matches */
3108 found_D0 +=
3109 (ds->dir_port == 60090 ?
3110 1 : 0)
3112 tt_int_op(found_D0, OP_EQ, 0);
3114 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3115 int found_B1 = 0;
3116 SMARTLIST_FOREACH(fallback_servers,
3117 dir_server_t *,
3119 /* increment the found counter if dir_port matches */
3120 found_B1 +=
3121 (ds->dir_port == 60091 ?
3122 1 : 0)
3124 tt_int_op(found_B1, OP_EQ, 0);
3126 /* AlternateDirAuthority - A2 - dir_port: 60092 */
3127 int found_A2 = 0;
3128 SMARTLIST_FOREACH(fallback_servers,
3129 dir_server_t *,
3131 /* increment the found counter if dir_port matches */
3132 found_A2 +=
3133 (ds->dir_port == 60092 ?
3134 1 : 0)
3136 tt_int_op(found_A2, OP_EQ, 1);
3138 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
3139 int found_non_default_fallback = 0;
3140 SMARTLIST_FOREACH(fallback_servers,
3141 dir_server_t *,
3143 /* increment the found counter if dir_port matches */
3144 found_non_default_fallback +=
3145 (ds->dir_port == 60093 ?
3146 1 : 0)
3148 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3150 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3151 int found_default_fallback = 0;
3152 SMARTLIST_FOREACH(fallback_servers,
3153 dir_server_t *,
3155 /* increment the found counter if dir_port matches */
3156 found_default_fallback +=
3157 (ds->dir_port == 60099 ?
3158 1 : 0)
3160 tt_int_op(found_default_fallback, OP_EQ, 0);
3162 /* There's no easy way of checking that we have included all the
3163 * default Bridge authorities (except for hard-coding tonga's details),
3164 * so let's assume that if the total count above is correct,
3165 * we have the right ones.
3171 4. Outcome: Use Set Custom Fallback Directory
3172 - Use Default Bridge & Directory Authorities
3173 Cases expected to yield this outcome:
3174 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
3175 are all NULL, but FallbackDir is set)
3178 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
3179 & AlternateDirAuthority Not Set, FallbackDir Set */
3181 /* clear fallback dirs counter */
3182 n_add_default_fallback_dir_servers_known_default = 0;
3184 /* clear options*/
3185 memset(options, 0, sizeof(or_options_t));
3187 /* clear any previous dir servers:
3188 consider_adding_dir_servers() should do this anyway */
3189 clear_dir_servers();
3191 /* assign options: 0001 */
3192 options->DirAuthorities = NULL;
3193 options->AlternateBridgeAuthority = NULL;
3194 options->AlternateDirAuthority = NULL;
3195 options->FallbackDir = test_fallback_directory;
3196 options->UseDefaultFallbackDirs = 1;
3198 /* parse options - ensure we always update by passing NULL old_options */
3199 consider_adding_dir_servers(options, NULL);
3201 /* check outcome */
3203 /* we must not have added the default fallback dirs */
3204 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
3206 /* we have more fallbacks than just the authorities */
3207 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3210 /* trusted_dir_servers */
3211 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3212 /* (No D0), (No B1), Default Bridge Authorities,
3213 * (No A2), Default v3 Directory Authorities */
3214 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3216 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3217 int found_D0 = 0;
3218 SMARTLIST_FOREACH(dir_servers,
3219 dir_server_t *,
3221 /* increment the found counter if dir_port matches */
3222 found_D0 +=
3223 (ds->dir_port == 60090 ?
3224 1 : 0)
3226 tt_int_op(found_D0, OP_EQ, 0);
3228 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3229 int found_B1 = 0;
3230 SMARTLIST_FOREACH(dir_servers,
3231 dir_server_t *,
3233 /* increment the found counter if dir_port matches */
3234 found_B1 +=
3235 (ds->dir_port == 60091 ?
3236 1 : 0)
3238 tt_int_op(found_B1, OP_EQ, 0);
3240 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3241 int found_A2 = 0;
3242 SMARTLIST_FOREACH(dir_servers,
3243 dir_server_t *,
3245 /* increment the found counter if dir_port matches */
3246 found_A2 +=
3247 (ds->dir_port == 60092 ?
3248 1 : 0)
3250 tt_int_op(found_A2, OP_EQ, 0);
3252 /* There's no easy way of checking that we have included all the
3253 * default Bridge & V3 Directory authorities, so let's assume that
3254 * if the total count above is correct, we have the right ones.
3259 /* fallback_dir_servers */
3260 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3261 /* (No D0), (No B1), Default Bridge Authorities,
3262 * (No A2), Default v3 Directory Authorities,
3263 * Custom Fallback Directory, (No Default Fallback Directories) */
3264 tt_assert(smartlist_len(fallback_servers) ==
3265 1 + n_default_authorities);
3267 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3268 int found_D0 = 0;
3269 SMARTLIST_FOREACH(fallback_servers,
3270 dir_server_t *,
3272 /* increment the found counter if dir_port matches */
3273 found_D0 +=
3274 (ds->dir_port == 60090 ?
3275 1 : 0)
3277 tt_int_op(found_D0, OP_EQ, 0);
3279 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3280 int found_B1 = 0;
3281 SMARTLIST_FOREACH(fallback_servers,
3282 dir_server_t *,
3284 /* increment the found counter if dir_port matches */
3285 found_B1 +=
3286 (ds->dir_port == 60091 ?
3287 1 : 0)
3289 tt_int_op(found_B1, OP_EQ, 0);
3291 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3292 int found_A2 = 0;
3293 SMARTLIST_FOREACH(fallback_servers,
3294 dir_server_t *,
3296 /* increment the found counter if dir_port matches */
3297 found_A2 +=
3298 (ds->dir_port == 60092 ?
3299 1 : 0)
3301 tt_int_op(found_A2, OP_EQ, 0);
3303 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3304 int found_non_default_fallback = 0;
3305 SMARTLIST_FOREACH(fallback_servers,
3306 dir_server_t *,
3308 /* increment the found counter if dir_port matches */
3309 found_non_default_fallback +=
3310 (ds->dir_port == 60093 ?
3311 1 : 0)
3313 tt_int_op(found_non_default_fallback, OP_EQ, 1);
3315 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3316 int found_default_fallback = 0;
3317 SMARTLIST_FOREACH(fallback_servers,
3318 dir_server_t *,
3320 /* increment the found counter if dir_port matches */
3321 found_default_fallback +=
3322 (ds->dir_port == 60099 ?
3323 1 : 0)
3325 tt_int_op(found_default_fallback, OP_EQ, 0);
3327 /* There's no easy way of checking that we have included all the
3328 * default Bridge & V3 Directory authorities, so let's assume that
3329 * if the total count above is correct, we have the right ones.
3335 5. Outcome: Use All Defaults
3336 - Use Default Bridge & Directory Authorities, Default Fallback Directories
3337 Cases expected to yield this outcome:
3338 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
3339 and FallbackDir are all NULL)
3342 /* Case 0: 0000 - All Not Set */
3344 /* clear fallback dirs counter */
3345 n_add_default_fallback_dir_servers_known_default = 0;
3347 /* clear options*/
3348 memset(options, 0, sizeof(or_options_t));
3350 /* clear any previous dir servers:
3351 consider_adding_dir_servers() should do this anyway */
3352 clear_dir_servers();
3354 /* assign options: 0001 */
3355 options->DirAuthorities = NULL;
3356 options->AlternateBridgeAuthority = NULL;
3357 options->AlternateDirAuthority = NULL;
3358 options->FallbackDir = NULL;
3359 options->UseDefaultFallbackDirs = 1;
3361 /* parse options - ensure we always update by passing NULL old_options */
3362 consider_adding_dir_servers(options, NULL);
3364 /* check outcome */
3366 /* we must have added the default fallback dirs */
3367 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
3369 /* we have more fallbacks than just the authorities */
3370 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
3373 /* trusted_dir_servers */
3374 const smartlist_t *dir_servers = router_get_trusted_dir_servers();
3375 /* (No D0), (No B1), Default Bridge Authorities,
3376 * (No A2), Default v3 Directory Authorities */
3377 tt_assert(smartlist_len(dir_servers) == n_default_authorities);
3379 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3380 int found_D0 = 0;
3381 SMARTLIST_FOREACH(dir_servers,
3382 dir_server_t *,
3384 /* increment the found counter if dir_port matches */
3385 found_D0 +=
3386 (ds->dir_port == 60090 ?
3387 1 : 0)
3389 tt_int_op(found_D0, OP_EQ, 0);
3391 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3392 int found_B1 = 0;
3393 SMARTLIST_FOREACH(dir_servers,
3394 dir_server_t *,
3396 /* increment the found counter if dir_port matches */
3397 found_B1 +=
3398 (ds->dir_port == 60091 ?
3399 1 : 0)
3401 tt_int_op(found_B1, OP_EQ, 0);
3403 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3404 int found_A2 = 0;
3405 SMARTLIST_FOREACH(dir_servers,
3406 dir_server_t *,
3408 /* increment the found counter if dir_port matches */
3409 found_A2 +=
3410 (ds->dir_port == 60092 ?
3411 1 : 0)
3413 tt_int_op(found_A2, OP_EQ, 0);
3415 /* There's no easy way of checking that we have included all the
3416 * default Bridge & V3 Directory authorities, so let's assume that
3417 * if the total count above is correct, we have the right ones.
3422 /* fallback_dir_servers */
3423 const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
3424 /* (No D0), (No B1), Default Bridge Authorities,
3425 * (No A2), Default v3 Directory Authorities,
3426 * (No Custom Fallback Directory), Default Fallback Directories */
3427 tt_assert(smartlist_len(fallback_servers) ==
3428 n_default_authorities + n_default_fallback_dir);
3430 /* (No DirAuthorities) - D0 - dir_port: 60090 */
3431 int found_D0 = 0;
3432 SMARTLIST_FOREACH(fallback_servers,
3433 dir_server_t *,
3435 /* increment the found counter if dir_port matches */
3436 found_D0 +=
3437 (ds->dir_port == 60090 ?
3438 1 : 0)
3440 tt_int_op(found_D0, OP_EQ, 0);
3442 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
3443 int found_B1 = 0;
3444 SMARTLIST_FOREACH(fallback_servers,
3445 dir_server_t *,
3447 /* increment the found counter if dir_port matches */
3448 found_B1 +=
3449 (ds->dir_port == 60091 ?
3450 1 : 0)
3452 tt_int_op(found_B1, OP_EQ, 0);
3454 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
3455 int found_A2 = 0;
3456 SMARTLIST_FOREACH(fallback_servers,
3457 dir_server_t *,
3459 /* increment the found counter if dir_port matches */
3460 found_A2 +=
3461 (ds->dir_port == 60092 ?
3462 1 : 0)
3464 tt_int_op(found_A2, OP_EQ, 0);
3466 /* Custom FallbackDir - No Nickname - dir_port: 60093 */
3467 int found_non_default_fallback = 0;
3468 SMARTLIST_FOREACH(fallback_servers,
3469 dir_server_t *,
3471 /* increment the found counter if dir_port matches */
3472 found_non_default_fallback +=
3473 (ds->dir_port == 60093 ?
3474 1 : 0)
3476 tt_int_op(found_non_default_fallback, OP_EQ, 0);
3478 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
3479 int found_default_fallback = 0;
3480 SMARTLIST_FOREACH(fallback_servers,
3481 dir_server_t *,
3483 /* increment the found counter if dir_port matches */
3484 found_default_fallback +=
3485 (ds->dir_port == 60099 ?
3486 1 : 0)
3488 tt_int_op(found_default_fallback, OP_EQ, 1);
3490 /* There's no easy way of checking that we have included all the
3491 * default Bridge & V3 Directory authorities, and the default
3492 * Fallback Directories, so let's assume that if the total count
3493 * above is correct, we have the right ones.
3498 done:
3499 clear_dir_servers();
3501 tor_free(test_dir_authority->key);
3502 tor_free(test_dir_authority->value);
3503 tor_free(test_dir_authority);
3505 tor_free(test_alt_dir_authority->key);
3506 tor_free(test_alt_dir_authority->value);
3507 tor_free(test_alt_dir_authority);
3509 tor_free(test_alt_bridge_authority->key);
3510 tor_free(test_alt_bridge_authority->value);
3511 tor_free(test_alt_bridge_authority);
3513 tor_free(test_fallback_directory->key);
3514 tor_free(test_fallback_directory->value);
3515 tor_free(test_fallback_directory);
3517 options->DirAuthorities = NULL;
3518 options->AlternateBridgeAuthority = NULL;
3519 options->AlternateDirAuthority = NULL;
3520 options->FallbackDir = NULL;
3521 or_options_free(options);
3523 UNMOCK(add_default_fallback_dir_servers);
3526 static void
3527 test_config_default_dir_servers(void *arg)
3529 or_options_t *opts = NULL;
3530 (void)arg;
3531 int trusted_count = 0;
3532 int fallback_count = 0;
3534 /* new set of options should stop fallback parsing */
3535 opts = tor_malloc_zero(sizeof(or_options_t));
3536 opts->UseDefaultFallbackDirs = 0;
3537 /* set old_options to NULL to force dir update */
3538 consider_adding_dir_servers(opts, NULL);
3539 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3540 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3541 or_options_free(opts);
3542 opts = NULL;
3544 /* assume a release will never go out with less than 7 authorities */
3545 tt_int_op(trusted_count, OP_GE, 7);
3546 /* if we disable the default fallbacks, there must not be any extra */
3547 tt_assert(fallback_count == trusted_count);
3549 opts = tor_malloc_zero(sizeof(or_options_t));
3550 opts->UseDefaultFallbackDirs = 1;
3551 consider_adding_dir_servers(opts, opts);
3552 trusted_count = smartlist_len(router_get_trusted_dir_servers());
3553 fallback_count = smartlist_len(router_get_fallback_dir_servers());
3554 or_options_free(opts);
3555 opts = NULL;
3557 /* assume a release will never go out with less than 7 authorities */
3558 tt_int_op(trusted_count, OP_GE, 7);
3559 /* XX/teor - allow for default fallbacks to be added without breaking
3560 * the unit tests. Set a minimum fallback count once the list is stable. */
3561 tt_assert(fallback_count >= trusted_count);
3563 done:
3564 or_options_free(opts);
3567 static int mock_router_pick_published_address_result = 0;
3569 static int
3570 mock_router_pick_published_address(const or_options_t *options,
3571 uint32_t *addr, int cache_only)
3573 (void)options;
3574 (void)addr;
3575 (void)cache_only;
3576 return mock_router_pick_published_address_result;
3579 static int mock_router_my_exit_policy_is_reject_star_result = 0;
3581 static int
3582 mock_router_my_exit_policy_is_reject_star(void)
3584 return mock_router_my_exit_policy_is_reject_star_result;
3587 static int mock_advertised_server_mode_result = 0;
3589 static int
3590 mock_advertised_server_mode(void)
3592 return mock_advertised_server_mode_result;
3595 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
3597 static const routerinfo_t *
3598 mock_router_get_my_routerinfo(void)
3600 return mock_router_get_my_routerinfo_result;
3603 static void
3604 test_config_directory_fetch(void *arg)
3606 (void)arg;
3608 /* Test Setup */
3609 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
3610 routerinfo_t routerinfo;
3611 memset(&routerinfo, 0, sizeof(routerinfo));
3612 mock_router_pick_published_address_result = -1;
3613 mock_router_my_exit_policy_is_reject_star_result = 1;
3614 mock_advertised_server_mode_result = 0;
3615 mock_router_get_my_routerinfo_result = NULL;
3616 MOCK(router_pick_published_address, mock_router_pick_published_address);
3617 MOCK(router_my_exit_policy_is_reject_star,
3618 mock_router_my_exit_policy_is_reject_star);
3619 MOCK(advertised_server_mode, mock_advertised_server_mode);
3620 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
3622 /* Clients can use multiple directory mirrors for bootstrap */
3623 memset(options, 0, sizeof(or_options_t));
3624 options->ClientOnly = 1;
3625 tt_assert(server_mode(options) == 0);
3626 tt_assert(public_server_mode(options) == 0);
3627 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3628 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3629 OP_EQ, 1);
3631 /* Bridge Clients can use multiple directory mirrors for bootstrap */
3632 memset(options, 0, sizeof(or_options_t));
3633 options->UseBridges = 1;
3634 tt_assert(server_mode(options) == 0);
3635 tt_assert(public_server_mode(options) == 0);
3636 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3637 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3638 OP_EQ, 1);
3640 /* Bridge Relays (Bridges) must act like clients, and use multiple
3641 * directory mirrors for bootstrap */
3642 memset(options, 0, sizeof(or_options_t));
3643 options->BridgeRelay = 1;
3644 options->ORPort_set = 1;
3645 tt_assert(server_mode(options) == 1);
3646 tt_assert(public_server_mode(options) == 0);
3647 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3648 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3649 OP_EQ, 1);
3651 /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
3652 * but can use multiple authorities for bootstrap */
3653 memset(options, 0, sizeof(or_options_t));
3654 options->FetchDirInfoEarly = 1;
3655 tt_assert(server_mode(options) == 0);
3656 tt_assert(public_server_mode(options) == 0);
3657 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3658 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3659 OP_EQ, 1);
3661 /* OR servers only fetch the consensus from the authorities when they don't
3662 * know their own address, but never use multiple directories for bootstrap
3664 memset(options, 0, sizeof(or_options_t));
3665 options->ORPort_set = 1;
3667 mock_router_pick_published_address_result = -1;
3668 tt_assert(server_mode(options) == 1);
3669 tt_assert(public_server_mode(options) == 1);
3670 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3671 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3672 OP_EQ, 0);
3674 mock_router_pick_published_address_result = 0;
3675 tt_assert(server_mode(options) == 1);
3676 tt_assert(public_server_mode(options) == 1);
3677 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3678 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3679 OP_EQ, 0);
3681 /* Exit OR servers only fetch the consensus from the authorities when they
3682 * refuse unknown exits, but never use multiple directories for bootstrap
3684 memset(options, 0, sizeof(or_options_t));
3685 options->ORPort_set = 1;
3686 options->ExitRelay = 1;
3687 mock_router_pick_published_address_result = 0;
3688 mock_router_my_exit_policy_is_reject_star_result = 0;
3689 mock_advertised_server_mode_result = 1;
3690 mock_router_get_my_routerinfo_result = &routerinfo;
3692 routerinfo.supports_tunnelled_dir_requests = 1;
3694 options->RefuseUnknownExits = 1;
3695 tt_assert(server_mode(options) == 1);
3696 tt_assert(public_server_mode(options) == 1);
3697 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3698 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3699 OP_EQ, 0);
3701 options->RefuseUnknownExits = 0;
3702 mock_router_pick_published_address_result = 0;
3703 tt_assert(server_mode(options) == 1);
3704 tt_assert(public_server_mode(options) == 1);
3705 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3706 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3707 OP_EQ, 0);
3709 /* Dir servers fetch the consensus from the authorities, unless they are not
3710 * advertising themselves (hibernating) or have no routerinfo or are not
3711 * advertising their dirport, and never use multiple directories for
3712 * bootstrap. This only applies if they are also OR servers.
3713 * (We don't care much about the behaviour of non-OR directory servers.) */
3714 memset(options, 0, sizeof(or_options_t));
3715 options->DirPort_set = 1;
3716 options->ORPort_set = 1;
3717 options->DirCache = 1;
3718 mock_router_pick_published_address_result = 0;
3719 mock_router_my_exit_policy_is_reject_star_result = 1;
3721 mock_advertised_server_mode_result = 1;
3722 routerinfo.dir_port = 1;
3723 mock_router_get_my_routerinfo_result = &routerinfo;
3724 tt_assert(server_mode(options) == 1);
3725 tt_assert(public_server_mode(options) == 1);
3726 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3727 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3728 OP_EQ, 0);
3730 mock_advertised_server_mode_result = 0;
3731 routerinfo.dir_port = 1;
3732 mock_router_get_my_routerinfo_result = &routerinfo;
3733 tt_assert(server_mode(options) == 1);
3734 tt_assert(public_server_mode(options) == 1);
3735 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3736 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3737 OP_EQ, 0);
3739 mock_advertised_server_mode_result = 1;
3740 mock_router_get_my_routerinfo_result = NULL;
3741 tt_assert(server_mode(options) == 1);
3742 tt_assert(public_server_mode(options) == 1);
3743 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3744 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3745 OP_EQ, 0);
3747 mock_advertised_server_mode_result = 1;
3748 routerinfo.dir_port = 0;
3749 routerinfo.supports_tunnelled_dir_requests = 0;
3750 mock_router_get_my_routerinfo_result = &routerinfo;
3751 tt_assert(server_mode(options) == 1);
3752 tt_assert(public_server_mode(options) == 1);
3753 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
3754 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3755 OP_EQ, 0);
3757 mock_advertised_server_mode_result = 1;
3758 routerinfo.dir_port = 1;
3759 routerinfo.supports_tunnelled_dir_requests = 1;
3760 mock_router_get_my_routerinfo_result = &routerinfo;
3761 tt_assert(server_mode(options) == 1);
3762 tt_assert(public_server_mode(options) == 1);
3763 tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
3764 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
3765 OP_EQ, 0);
3767 done:
3768 tor_free(options);
3769 UNMOCK(router_pick_published_address);
3770 UNMOCK(router_get_my_routerinfo);
3771 UNMOCK(advertised_server_mode);
3772 UNMOCK(router_my_exit_policy_is_reject_star);
3775 static void
3776 test_config_default_fallback_dirs(void *arg)
3778 const char *fallback[] = {
3779 #include "app/config/fallback_dirs.inc"
3780 NULL
3783 int n_included_fallback_dirs = 0;
3784 int n_added_fallback_dirs = 0;
3786 (void)arg;
3787 clear_dir_servers();
3789 while (fallback[n_included_fallback_dirs])
3790 n_included_fallback_dirs++;
3792 add_default_fallback_dir_servers();
3794 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
3796 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
3798 done:
3799 clear_dir_servers();
3802 static void
3803 test_config_port_cfg_line_extract_addrport(void *arg)
3805 (void)arg;
3806 int unixy = 0;
3807 const char *rest = NULL;
3808 char *a = NULL;
3810 tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
3811 tt_int_op(unixy, OP_EQ, 0);
3812 tt_str_op(a, OP_EQ, "");
3813 tt_str_op(rest, OP_EQ, "");
3814 tor_free(a);
3816 tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
3817 OP_EQ, 0);
3818 tt_int_op(unixy, OP_EQ, 0);
3819 tt_str_op(a, OP_EQ, "hello");
3820 tt_str_op(rest, OP_EQ, "");
3821 tor_free(a);
3823 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
3824 &a, &unixy, &rest), OP_EQ, 0);
3825 tt_int_op(unixy, OP_EQ, 0);
3826 tt_str_op(a, OP_EQ, "flipperwalt");
3827 tt_str_op(rest, OP_EQ, "gersplut");
3828 tor_free(a);
3830 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
3831 &a, &unixy, &rest), OP_EQ, 0);
3832 tt_int_op(unixy, OP_EQ, 0);
3833 tt_str_op(a, OP_EQ, "flipperwalt");
3834 tt_str_op(rest, OP_EQ, "gersplut");
3835 tor_free(a);
3837 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
3838 &a, &unixy, &rest), OP_EQ, 0);
3839 tt_int_op(unixy, OP_EQ, 0);
3840 tt_str_op(a, OP_EQ, "flipperwalt");
3841 tt_str_op(rest, OP_EQ, "gersplut");
3842 tor_free(a);
3844 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
3845 &a, &unixy, &rest), OP_EQ, 0);
3846 tt_int_op(unixy, OP_EQ, 1);
3847 tt_str_op(a, OP_EQ, "flipperwalt");
3848 tt_str_op(rest, OP_EQ, "gersplut");
3849 tor_free(a);
3851 tt_int_op(port_cfg_line_extract_addrport("lolol",
3852 &a, &unixy, &rest), OP_EQ, 0);
3853 tt_int_op(unixy, OP_EQ, 0);
3854 tt_str_op(a, OP_EQ, "lolol");
3855 tt_str_op(rest, OP_EQ, "");
3856 tor_free(a);
3858 tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
3859 &a, &unixy, &rest), OP_EQ, 0);
3860 tt_int_op(unixy, OP_EQ, 1);
3861 tt_str_op(a, OP_EQ, "lolol");
3862 tt_str_op(rest, OP_EQ, "");
3863 tor_free(a);
3865 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
3866 &a, &unixy, &rest), OP_EQ, 0);
3867 tt_int_op(unixy, OP_EQ, 1);
3868 tt_str_op(a, OP_EQ, "lolol");
3869 tt_str_op(rest, OP_EQ, "");
3870 tor_free(a);
3872 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
3873 &a, &unixy, &rest), OP_EQ, 0);
3874 tt_int_op(unixy, OP_EQ, 1);
3875 tt_str_op(a, OP_EQ, "lolol");
3876 tt_str_op(rest, OP_EQ, "");
3877 tor_free(a);
3879 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
3880 &a, &unixy, &rest), OP_EQ, 0);
3881 tt_int_op(unixy, OP_EQ, 0);
3882 tt_str_op(a, OP_EQ, "foobar:lolol");
3883 tt_str_op(rest, OP_EQ, "");
3884 tor_free(a);
3886 tt_int_op(port_cfg_line_extract_addrport(":lolol",
3887 &a, &unixy, &rest), OP_EQ, 0);
3888 tt_int_op(unixy, OP_EQ, 0);
3889 tt_str_op(a, OP_EQ, ":lolol");
3890 tt_str_op(rest, OP_EQ, "");
3891 tor_free(a);
3893 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
3894 &a, &unixy, &rest), OP_EQ, 0);
3895 tt_int_op(unixy, OP_EQ, 1);
3896 tt_str_op(a, OP_EQ, "lolol");
3897 tt_str_op(rest, OP_EQ, "");
3898 tor_free(a);
3900 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
3901 &a, &unixy, &rest), OP_EQ, 0);
3902 tt_int_op(unixy, OP_EQ, 1);
3903 tt_str_op(a, OP_EQ, "lolol");
3904 tt_str_op(rest, OP_EQ, "");
3905 tor_free(a);
3907 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
3908 &a, &unixy, &rest), OP_EQ, 0);
3909 tt_int_op(unixy, OP_EQ, 1);
3910 tt_str_op(a, OP_EQ, "lolol");
3911 tt_str_op(rest, OP_EQ, "foo ");
3912 tor_free(a);
3914 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
3915 &a, &unixy, &rest), OP_EQ, 0);
3916 tt_int_op(unixy, OP_EQ, 1);
3917 tt_str_op(a, OP_EQ, "lol ol");
3918 tt_str_op(rest, OP_EQ, "foo ");
3919 tor_free(a);
3921 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
3922 &a, &unixy, &rest), OP_EQ, 0);
3923 tt_int_op(unixy, OP_EQ, 1);
3924 tt_str_op(a, OP_EQ, "lol\" ol");
3925 tt_str_op(rest, OP_EQ, "foo ");
3926 tor_free(a);
3928 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
3929 &a, &unixy, &rest), OP_EQ, -1);
3930 tor_free(a);
3932 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
3933 &a, &unixy, &rest), OP_EQ, -1);
3934 tor_free(a);
3936 done:
3937 tor_free(a);
3940 static config_line_t *
3941 mock_config_line(const char *key, const char *val)
3943 config_line_t *config_line = tor_malloc(sizeof(config_line_t));
3944 memset(config_line, 0, sizeof(config_line_t));
3945 config_line->key = tor_strdup(key);
3946 config_line->value = tor_strdup(val);
3947 return config_line;
3950 static void
3951 test_config_parse_port_config__ports__no_ports_given(void *data)
3953 (void)data;
3954 int ret;
3955 smartlist_t *slout = NULL;
3956 port_cfg_t *port_cfg = NULL;
3958 slout = smartlist_new();
3960 // Test no defaultport, no defaultaddress and no out
3961 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
3962 tt_int_op(ret, OP_EQ, 0);
3964 // Test with defaultport, no defaultaddress and no out
3965 ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
3966 tt_int_op(ret, OP_EQ, 0);
3968 // Test no defaultport, with defaultaddress and no out
3969 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3970 tt_int_op(ret, OP_EQ, 0);
3972 // Test with defaultport, with defaultaddress and no out
3973 ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3974 tt_int_op(ret, OP_EQ, 0);
3976 // Test no defaultport, no defaultaddress and with out
3977 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 0, 0);
3978 tt_int_op(ret, OP_EQ, 0);
3979 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3981 // Test with defaultport, no defaultaddress and with out
3982 ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 42, 0);
3983 tt_int_op(ret, OP_EQ, 0);
3984 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3986 // Test no defaultport, with defaultaddress and with out
3987 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
3988 tt_int_op(ret, OP_EQ, 0);
3989 tt_int_op(smartlist_len(slout), OP_EQ, 0);
3991 // Test with defaultport, with defaultaddress and out, adds a new port cfg
3992 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
3993 smartlist_clear(slout);
3994 ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
3995 tt_int_op(ret, OP_EQ, 0);
3996 tt_int_op(smartlist_len(slout), OP_EQ, 1);
3997 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
3998 tt_int_op(port_cfg->port, OP_EQ, 42);
3999 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
4001 // Test with defaultport, with defaultaddress and out, adds a new port cfg
4002 // for a unix address
4003 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4004 smartlist_clear(slout);
4005 ret = parse_port_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
4006 42, CL_PORT_IS_UNIXSOCKET);
4007 tt_int_op(ret, OP_EQ, 0);
4008 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4009 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4010 tt_int_op(port_cfg->port, OP_EQ, 0);
4011 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4012 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
4014 done:
4015 if (slout)
4016 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4017 smartlist_free(slout);
4020 static void
4021 test_config_parse_port_config__ports__ports_given(void *data)
4023 (void)data;
4024 int ret;
4025 smartlist_t *slout = NULL;
4026 port_cfg_t *port_cfg = NULL;
4027 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4028 tor_addr_t addr;
4030 slout = smartlist_new();
4032 // Test error when encounters an invalid Port specification
4033 config_port_invalid = mock_config_line("DNSPort", "");
4034 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
4035 0, 0);
4036 tt_int_op(ret, OP_EQ, -1);
4038 // Test error when encounters an empty unix domain specification
4039 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4040 config_port_invalid = mock_config_line("DNSPort", "unix:");
4041 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
4042 0, 0);
4043 tt_int_op(ret, OP_EQ, -1);
4045 // Test error when encounters a unix domain specification but the listener
4046 // doesn't support domain sockets
4047 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
4048 ret = parse_port_config(NULL, config_port_valid, "DNS",
4049 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
4050 tt_int_op(ret, OP_EQ, -1);
4052 // Test valid unix domain
4053 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4054 smartlist_clear(slout);
4055 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4056 CONN_TYPE_AP_LISTENER, NULL, 0, 0);
4057 #ifdef _WIN32
4058 tt_int_op(ret, OP_EQ, -1);
4059 #else
4060 tt_int_op(ret, OP_EQ, 0);
4061 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4062 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4063 tt_int_op(port_cfg->port, OP_EQ, 0);
4064 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4065 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
4066 /* Test entry port defaults as initialised in parse_port_config */
4067 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4068 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4069 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4070 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4071 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
4072 #endif /* defined(_WIN32) */
4074 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
4075 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4076 config_port_invalid = mock_config_line("SOCKSPort",
4077 "unix:/tmp/foo/bar NoIPv4Traffic "
4078 "NoIPv6Traffic "
4079 "NoOnionTraffic");
4080 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4081 CONN_TYPE_AP_LISTENER, NULL, 0,
4082 CL_PORT_TAKES_HOSTNAMES);
4083 tt_int_op(ret, OP_EQ, -1);
4085 // Test failure if we have no DNS and we're a DNSPort
4086 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4087 config_port_invalid = mock_config_line("DNSPort",
4088 "127.0.0.1:80 NoDNSRequest");
4089 ret = parse_port_config(NULL, config_port_invalid, "DNS",
4090 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4091 CL_PORT_TAKES_HOSTNAMES);
4092 tt_int_op(ret, OP_EQ, -1);
4094 // If we're a DNSPort, DNS only is ok
4095 // Use a port because DNSPort doesn't support sockets
4096 config_free_lines(config_port_valid); config_port_valid = NULL;
4097 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4098 smartlist_clear(slout);
4099 config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
4100 "NoIPv6Traffic "
4101 "NoIPv4Traffic NoOnionTraffic");
4102 ret = parse_port_config(slout, config_port_valid, "DNS",
4103 CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
4104 CL_PORT_TAKES_HOSTNAMES);
4105 tt_int_op(ret, OP_EQ, 0);
4106 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4107 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4108 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
4109 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4110 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4111 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
4113 // Test failure if we have DNS but no ipv4 and no ipv6
4114 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4115 config_port_invalid = mock_config_line("SOCKSPort",
4116 "NoIPv6Traffic "
4117 "unix:/tmp/foo/bar NoIPv4Traffic");
4118 ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
4119 CONN_TYPE_AP_LISTENER, NULL, 0,
4120 CL_PORT_TAKES_HOSTNAMES);
4121 tt_int_op(ret, OP_EQ, -1);
4123 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
4124 // options)
4125 config_free_lines(config_port_valid); config_port_valid = NULL;
4126 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4127 smartlist_clear(slout);
4128 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4129 "NoIPv6Traffic "
4130 "NoDNSRequest NoIPv4Traffic");
4131 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4132 CONN_TYPE_AP_LISTENER, NULL, 0,
4133 CL_PORT_TAKES_HOSTNAMES);
4134 #ifdef _WIN32
4135 tt_int_op(ret, OP_EQ, -1);
4136 #else
4137 tt_int_op(ret, OP_EQ, 0);
4138 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4139 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4140 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4141 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4142 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4143 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4144 #endif /* defined(_WIN32) */
4146 // Test success with quoted unix: address.
4147 config_free_lines(config_port_valid); config_port_valid = NULL;
4148 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4149 smartlist_clear(slout);
4150 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
4151 "NoIPv6Traffic "
4152 "NoDNSRequest NoIPv4Traffic");
4153 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4154 CONN_TYPE_AP_LISTENER, NULL, 0,
4155 CL_PORT_TAKES_HOSTNAMES);
4156 #ifdef _WIN32
4157 tt_int_op(ret, OP_EQ, -1);
4158 #else
4159 tt_int_op(ret, OP_EQ, 0);
4160 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4161 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4162 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4163 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4164 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4165 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4166 #endif /* defined(_WIN32) */
4168 // Test failure with broken quoted unix: address.
4169 config_free_lines(config_port_valid); config_port_valid = NULL;
4170 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4171 smartlist_clear(slout);
4172 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
4173 "NoIPv6Traffic "
4174 "NoDNSRequest NoIPv4Traffic");
4175 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4176 CONN_TYPE_AP_LISTENER, NULL, 0,
4177 CL_PORT_TAKES_HOSTNAMES);
4178 tt_int_op(ret, OP_EQ, -1);
4180 // Test failure with empty quoted unix: address.
4181 config_free_lines(config_port_valid); config_port_valid = NULL;
4182 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4183 smartlist_clear(slout);
4184 config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
4185 "NoIPv6Traffic "
4186 "NoDNSRequest NoIPv4Traffic");
4187 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4188 CONN_TYPE_AP_LISTENER, NULL, 0,
4189 CL_PORT_TAKES_HOSTNAMES);
4190 tt_int_op(ret, OP_EQ, -1);
4192 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
4193 config_free_lines(config_port_valid); config_port_valid = NULL;
4194 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4195 smartlist_clear(slout);
4196 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4197 "OnionTrafficOnly");
4198 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4199 CONN_TYPE_AP_LISTENER, NULL, 0,
4200 CL_PORT_TAKES_HOSTNAMES);
4201 #ifdef _WIN32
4202 tt_int_op(ret, OP_EQ, -1);
4203 #else
4204 tt_int_op(ret, OP_EQ, 0);
4205 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4206 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4207 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
4208 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4209 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
4210 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
4211 #endif /* defined(_WIN32) */
4213 // Test success with no ipv4 but take ipv6
4214 config_free_lines(config_port_valid); config_port_valid = NULL;
4215 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4216 smartlist_clear(slout);
4217 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4218 "NoIPv4Traffic IPv6Traffic");
4219 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4220 CONN_TYPE_AP_LISTENER, NULL, 0,
4221 CL_PORT_TAKES_HOSTNAMES);
4222 #ifdef _WIN32
4223 tt_int_op(ret, OP_EQ, -1);
4224 #else
4225 tt_int_op(ret, OP_EQ, 0);
4226 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4227 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4228 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
4229 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4230 #endif /* defined(_WIN32) */
4232 // Test success with both ipv4 and ipv6
4233 config_free_lines(config_port_valid); config_port_valid = NULL;
4234 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4235 smartlist_clear(slout);
4236 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
4237 "IPv4Traffic IPv6Traffic");
4238 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4239 CONN_TYPE_AP_LISTENER, NULL, 0,
4240 CL_PORT_TAKES_HOSTNAMES);
4241 #ifdef _WIN32
4242 tt_int_op(ret, OP_EQ, -1);
4243 #else
4244 tt_int_op(ret, OP_EQ, 0);
4245 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4246 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4247 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4248 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4249 #endif /* defined(_WIN32) */
4251 // Test failure if we specify world writable for an IP Port
4252 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4253 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
4254 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4255 "127.0.0.3", 0, 0);
4256 tt_int_op(ret, OP_EQ, -1);
4258 // Test failure if we specify group writable for an IP Port
4259 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4260 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
4261 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4262 "127.0.0.3", 0, 0);
4263 tt_int_op(ret, OP_EQ, -1);
4265 // Test failure if we specify group writable for an IP Port
4266 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4267 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
4268 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4269 "127.0.0.3", 0, 0);
4270 tt_int_op(ret, OP_EQ, -1);
4272 // Test success with only a port (this will fail without a default address)
4273 config_free_lines(config_port_valid); config_port_valid = NULL;
4274 config_port_valid = mock_config_line("DNSPort", "42");
4275 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4276 "127.0.0.3", 0, 0);
4277 tt_int_op(ret, OP_EQ, 0);
4279 // Test success with only a port and isolate destination port
4280 config_free_lines(config_port_valid); config_port_valid = NULL;
4281 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4282 smartlist_clear(slout);
4283 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
4284 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4285 "127.0.0.3", 0, 0);
4286 tt_int_op(ret, OP_EQ, 0);
4287 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4288 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4289 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4290 ISO_DEFAULT | ISO_DESTPORT);
4292 // Test success with a negative isolate destination port, and plural
4293 config_free_lines(config_port_valid); config_port_valid = NULL;
4294 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4295 smartlist_clear(slout);
4296 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
4297 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4298 "127.0.0.3", 0, 0);
4299 tt_int_op(ret, OP_EQ, 0);
4300 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4301 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4302 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4303 ISO_DEFAULT & ~ISO_DESTPORT);
4305 // Test success with isolate destination address
4306 config_free_lines(config_port_valid); config_port_valid = NULL;
4307 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4308 smartlist_clear(slout);
4309 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
4310 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4311 "127.0.0.3", 0, 0);
4312 tt_int_op(ret, OP_EQ, 0);
4313 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4314 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4315 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4316 ISO_DEFAULT | ISO_DESTADDR);
4318 // Test success with isolate socks AUTH
4319 config_free_lines(config_port_valid); config_port_valid = NULL;
4320 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4321 smartlist_clear(slout);
4322 config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
4323 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4324 "127.0.0.3", 0, 0);
4325 tt_int_op(ret, OP_EQ, 0);
4326 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4327 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4328 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4329 ISO_DEFAULT | ISO_SOCKSAUTH);
4331 // Test success with isolate client protocol
4332 config_free_lines(config_port_valid); config_port_valid = NULL;
4333 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4334 smartlist_clear(slout);
4335 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
4336 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4337 "127.0.0.3", 0, 0);
4338 tt_int_op(ret, OP_EQ, 0);
4339 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4340 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4341 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4342 ISO_DEFAULT | ISO_CLIENTPROTO);
4344 // Test success with isolate client address
4345 config_free_lines(config_port_valid); config_port_valid = NULL;
4346 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4347 smartlist_clear(slout);
4348 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
4349 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4350 "127.0.0.3", 0, 0);
4351 tt_int_op(ret, OP_EQ, 0);
4352 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4353 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4354 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
4355 ISO_DEFAULT | ISO_CLIENTADDR);
4357 // Test success with ignored unknown options
4358 config_free_lines(config_port_valid); config_port_valid = NULL;
4359 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
4360 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4361 "127.0.0.3", 0, 0);
4362 tt_int_op(ret, OP_EQ, 0);
4364 // Test success with no isolate socks AUTH
4365 config_free_lines(config_port_valid); config_port_valid = NULL;
4366 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4367 smartlist_clear(slout);
4368 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
4369 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4370 "127.0.0.3", 0, 0);
4371 tt_int_op(ret, OP_EQ, 0);
4372 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4373 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4374 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4376 // Test success with prefer ipv6
4377 config_free_lines(config_port_valid); config_port_valid = NULL;
4378 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4379 smartlist_clear(slout);
4380 config_port_valid = mock_config_line("SOCKSPort",
4381 "42 IPv6Traffic PreferIPv6");
4382 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4383 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
4384 CL_PORT_TAKES_HOSTNAMES);
4385 tt_int_op(ret, OP_EQ, 0);
4386 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4387 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4388 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
4390 // Test success with cache ipv4 DNS
4391 config_free_lines(config_port_valid); config_port_valid = NULL;
4392 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4393 smartlist_clear(slout);
4394 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
4395 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4396 "127.0.0.42", 0, 0);
4397 tt_int_op(ret, OP_EQ, 0);
4398 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4399 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4400 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4401 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4403 // Test success with cache ipv6 DNS
4404 config_free_lines(config_port_valid); config_port_valid = NULL;
4405 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4406 smartlist_clear(slout);
4407 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
4408 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4409 "127.0.0.42", 0, 0);
4410 tt_int_op(ret, OP_EQ, 0);
4411 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4412 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4413 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4414 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4416 // Test success with no cache ipv4 DNS
4417 config_free_lines(config_port_valid); config_port_valid = NULL;
4418 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4419 smartlist_clear(slout);
4420 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
4421 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4422 "127.0.0.42", 0, 0);
4423 tt_int_op(ret, OP_EQ, 0);
4424 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4425 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4426 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
4427 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
4429 // Test success with cache DNS
4430 config_free_lines(config_port_valid); config_port_valid = NULL;
4431 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4432 smartlist_clear(slout);
4433 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
4434 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4435 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
4436 tt_int_op(ret, OP_EQ, 0);
4437 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4438 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4439 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
4440 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
4442 // Test success with use cached ipv4 DNS
4443 config_free_lines(config_port_valid); config_port_valid = NULL;
4444 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4445 smartlist_clear(slout);
4446 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
4447 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4448 "127.0.0.42", 0, 0);
4449 tt_int_op(ret, OP_EQ, 0);
4450 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4451 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4452 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4453 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
4455 // Test success with use cached ipv6 DNS
4456 config_free_lines(config_port_valid); config_port_valid = NULL;
4457 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4458 smartlist_clear(slout);
4459 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
4460 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4461 "127.0.0.42", 0, 0);
4462 tt_int_op(ret, OP_EQ, 0);
4463 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4464 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4465 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
4466 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4468 // Test success with use cached DNS
4469 config_free_lines(config_port_valid); config_port_valid = NULL;
4470 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4471 smartlist_clear(slout);
4472 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
4473 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4474 "127.0.0.42", 0, 0);
4475 tt_int_op(ret, OP_EQ, 0);
4476 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4477 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4478 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
4479 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
4481 // Test success with not preferring ipv6 automap
4482 config_free_lines(config_port_valid); config_port_valid = NULL;
4483 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4484 smartlist_clear(slout);
4485 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
4486 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4487 "127.0.0.42", 0, 0);
4488 tt_int_op(ret, OP_EQ, 0);
4489 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4490 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4491 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
4493 // Test success with prefer SOCKS no auth
4494 config_free_lines(config_port_valid); config_port_valid = NULL;
4495 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4496 smartlist_clear(slout);
4497 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
4498 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4499 "127.0.0.42", 0, 0);
4500 tt_int_op(ret, OP_EQ, 0);
4501 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4502 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4503 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
4505 // Test failure with both a zero port and a non-zero port
4506 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4507 config_free_lines(config_port_valid); config_port_valid = NULL;
4508 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4509 smartlist_clear(slout);
4510 config_port_invalid = mock_config_line("DNSPort", "0");
4511 config_port_valid = mock_config_line("DNSPort", "42");
4512 config_port_invalid->next = config_port_valid;
4513 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4514 "127.0.0.42", 0, 0);
4515 tt_int_op(ret, OP_EQ, -1);
4517 // Test success with warn non-local control
4518 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4519 smartlist_clear(slout);
4520 ret = parse_port_config(slout, config_port_valid, "Control",
4521 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
4522 CL_PORT_WARN_NONLOCAL);
4523 tt_int_op(ret, OP_EQ, 0);
4525 // Test success with warn non-local listener
4526 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4527 smartlist_clear(slout);
4528 ret = parse_port_config(slout, config_port_valid, "ExtOR",
4529 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
4530 CL_PORT_WARN_NONLOCAL);
4531 tt_int_op(ret, OP_EQ, 0);
4533 // Test success with warn non-local other
4534 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4535 smartlist_clear(slout);
4536 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4537 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4538 tt_int_op(ret, OP_EQ, 0);
4540 // Test success with warn non-local other without out
4541 ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
4542 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
4543 tt_int_op(ret, OP_EQ, 0);
4545 // Test success with both ipv4 and ipv6 but without stream options
4546 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4547 config_port_valid = NULL;
4548 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4549 smartlist_clear(slout);
4550 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
4551 "IPv6Traffic");
4552 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4553 "127.0.0.44", 0,
4554 CL_PORT_TAKES_HOSTNAMES |
4555 CL_PORT_NO_STREAM_OPTIONS);
4556 tt_int_op(ret, OP_EQ, 0);
4557 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4558 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4559 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
4560 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
4562 // Test failure for a SessionGroup argument with invalid value
4563 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4564 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4565 smartlist_clear(slout);
4566 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
4567 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4568 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4569 tt_int_op(ret, OP_EQ, -1);
4571 // TODO: this seems wrong. Shouldn't it be the other way around?
4572 // Potential bug.
4573 // Test failure for a SessionGroup argument with valid value but with stream
4574 // options allowed
4575 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4576 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4577 smartlist_clear(slout);
4578 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
4579 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4580 "127.0.0.44", 0, 0);
4581 tt_int_op(ret, OP_EQ, -1);
4583 // Test failure for more than one SessionGroup argument
4584 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4585 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4586 smartlist_clear(slout);
4587 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
4588 "SessionGroup=321");
4589 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4590 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4591 tt_int_op(ret, OP_EQ, -1);
4593 // Test success with a sessiongroup options
4594 config_free_lines(config_port_valid); config_port_valid = NULL;
4595 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4596 smartlist_clear(slout);
4597 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
4598 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4599 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
4600 tt_int_op(ret, OP_EQ, 0);
4601 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4602 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4603 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
4605 // Test success with a zero unix domain socket, and doesnt add it to out
4606 config_free_lines(config_port_valid); config_port_valid = NULL;
4607 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4608 smartlist_clear(slout);
4609 config_port_valid = mock_config_line("DNSPort", "0");
4610 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4611 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4612 tt_int_op(ret, OP_EQ, 0);
4613 tt_int_op(smartlist_len(slout), OP_EQ, 0);
4615 // Test success with a one unix domain socket, and doesnt add it to out
4616 config_free_lines(config_port_valid); config_port_valid = NULL;
4617 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4618 smartlist_clear(slout);
4619 config_port_valid = mock_config_line("DNSPort", "something");
4620 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4621 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
4622 tt_int_op(ret, OP_EQ, 0);
4623 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4624 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4625 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
4626 tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
4628 // Test success with a port of auto - it uses the default address
4629 config_free_lines(config_port_valid); config_port_valid = NULL;
4630 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4631 smartlist_clear(slout);
4632 config_port_valid = mock_config_line("DNSPort", "auto");
4633 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4634 "127.0.0.46", 0, 0);
4635 tt_int_op(ret, OP_EQ, 0);
4636 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4637 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4638 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4639 tor_addr_parse(&addr, "127.0.0.46");
4640 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4642 // Test success with a port of auto in mixed case
4643 config_free_lines(config_port_valid); config_port_valid = NULL;
4644 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4645 smartlist_clear(slout);
4646 config_port_valid = mock_config_line("DNSPort", "AuTo");
4647 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4648 "127.0.0.46", 0, 0);
4649 tt_int_op(ret, OP_EQ, 0);
4650 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4651 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4652 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4653 tor_addr_parse(&addr, "127.0.0.46");
4654 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4656 // Test success with parsing both an address and an auto port
4657 config_free_lines(config_port_valid); config_port_valid = NULL;
4658 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4659 smartlist_clear(slout);
4660 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
4661 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4662 "127.0.0.46", 0, 0);
4663 tt_int_op(ret, OP_EQ, 0);
4664 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4665 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4666 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
4667 tor_addr_parse(&addr, "127.0.0.122");
4668 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4670 // Test failure when asked to parse an invalid address followed by auto
4671 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4672 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
4673 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
4674 ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
4675 "127.0.0.46", 0, 0);
4676 UNMOCK(tor_addr_lookup);
4677 tt_int_op(ret, OP_EQ, -1);
4679 // Test success with parsing both an address and a real port
4680 config_free_lines(config_port_valid); config_port_valid = NULL;
4681 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4682 smartlist_clear(slout);
4683 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
4684 ret = parse_port_config(slout, config_port_valid, "DNS", 0,
4685 "127.0.0.46", 0, 0);
4686 tt_int_op(ret, OP_EQ, 0);
4687 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4688 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4689 tt_int_op(port_cfg->port, OP_EQ, 656);
4690 tor_addr_parse(&addr, "127.0.0.123");
4691 tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
4693 // Test failure if we can't parse anything at all
4694 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4695 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4696 smartlist_clear(slout);
4697 config_port_invalid = mock_config_line("DNSPort", "something wrong");
4698 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4699 "127.0.0.46", 0, 0);
4700 tt_int_op(ret, OP_EQ, -1);
4702 // Test failure if we find both an address, a port and an auto
4703 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4704 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4705 smartlist_clear(slout);
4706 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
4707 ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
4708 "127.0.0.46", 0, 0);
4709 tt_int_op(ret, OP_EQ, -1);
4711 // Test that default to group writeable default sets group writeable for
4712 // domain socket
4713 config_free_lines(config_port_valid); config_port_valid = NULL;
4714 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4715 smartlist_clear(slout);
4716 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
4717 ret = parse_port_config(slout, config_port_valid, "SOCKS",
4718 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
4719 CL_PORT_DFLT_GROUP_WRITABLE);
4720 #ifdef _WIN32
4721 tt_int_op(ret, OP_EQ, -1);
4722 #else
4723 tt_int_op(ret, OP_EQ, 0);
4724 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4725 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4726 tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
4727 #endif /* defined(_WIN32) */
4729 done:
4730 if (slout)
4731 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4732 smartlist_free(slout);
4733 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4734 config_free_lines(config_port_valid); config_port_valid = NULL;
4737 static void
4738 test_config_parse_port_config__ports__server_options(void *data)
4740 (void)data;
4741 int ret;
4742 smartlist_t *slout = NULL;
4743 port_cfg_t *port_cfg = NULL;
4744 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
4746 slout = smartlist_new();
4748 // Test success with NoAdvertise option
4749 config_free_lines(config_port_valid); config_port_valid = NULL;
4750 config_port_valid = mock_config_line("DNSPort",
4751 "127.0.0.124:656 NoAdvertise");
4752 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4753 CL_PORT_SERVER_OPTIONS);
4754 tt_int_op(ret, OP_EQ, 0);
4755 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4756 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4757 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
4758 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
4760 // Test success with NoListen option
4761 config_free_lines(config_port_valid); config_port_valid = NULL;
4762 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4763 smartlist_clear(slout);
4764 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
4765 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4766 CL_PORT_SERVER_OPTIONS);
4767 tt_int_op(ret, OP_EQ, 0);
4768 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4769 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4770 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
4771 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
4773 // Test failure with both NoAdvertise and NoListen option
4774 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4775 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4776 smartlist_clear(slout);
4777 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
4778 "NoAdvertise");
4779 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4780 0, CL_PORT_SERVER_OPTIONS);
4781 tt_int_op(ret, OP_EQ, -1);
4783 // Test success with IPv4Only
4784 config_free_lines(config_port_valid); config_port_valid = NULL;
4785 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4786 smartlist_clear(slout);
4787 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
4788 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4789 CL_PORT_SERVER_OPTIONS);
4790 tt_int_op(ret, OP_EQ, 0);
4791 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4792 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4793 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
4794 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
4796 // Test success with IPv6Only
4797 config_free_lines(config_port_valid); config_port_valid = NULL;
4798 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4799 smartlist_clear(slout);
4800 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
4801 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4802 CL_PORT_SERVER_OPTIONS);
4803 tt_int_op(ret, OP_EQ, 0);
4804 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4805 port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
4806 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
4807 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
4809 // Test failure with both IPv4Only and IPv6Only
4810 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4811 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4812 smartlist_clear(slout);
4813 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
4814 "IPv4Only");
4815 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4816 0, CL_PORT_SERVER_OPTIONS);
4817 tt_int_op(ret, OP_EQ, -1);
4819 // Test success with invalid parameter
4820 config_free_lines(config_port_valid); config_port_valid = NULL;
4821 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4822 smartlist_clear(slout);
4823 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
4824 ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
4825 CL_PORT_SERVER_OPTIONS);
4826 tt_int_op(ret, OP_EQ, 0);
4827 tt_int_op(smartlist_len(slout), OP_EQ, 1);
4829 // Test failure when asked to bind only to ipv6 but gets an ipv4 address
4830 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4831 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4832 smartlist_clear(slout);
4833 config_port_invalid = mock_config_line("DNSPort",
4834 "127.0.0.124:656 IPv6Only");
4835 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4836 0, CL_PORT_SERVER_OPTIONS);
4837 tt_int_op(ret, OP_EQ, -1);
4839 // Test failure when asked to bind only to ipv4 but gets an ipv6 address
4840 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4841 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4842 smartlist_clear(slout);
4843 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
4844 ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
4845 0, CL_PORT_SERVER_OPTIONS);
4846 tt_int_op(ret, OP_EQ, -1);
4848 // Check for failure with empty unix: address.
4849 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4850 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4851 smartlist_clear(slout);
4852 config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
4853 ret = parse_port_config(slout, config_port_invalid, "ORPort", 0, NULL,
4854 0, CL_PORT_SERVER_OPTIONS);
4855 tt_int_op(ret, OP_EQ, -1);
4857 done:
4858 if (slout)
4859 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
4860 smartlist_free(slout);
4861 config_free_lines(config_port_invalid); config_port_invalid = NULL;
4862 config_free_lines(config_port_valid); config_port_valid = NULL;
4865 static void
4866 test_config_parse_log_severity(void *data)
4868 int ret;
4869 const char *severity_log_lines[] = {
4870 "debug file /tmp/debug.log",
4871 "debug\tfile /tmp/debug.log",
4872 "[handshake]debug [~net,~mm]info notice stdout",
4873 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
4874 NULL
4876 int i;
4877 log_severity_list_t *severity;
4879 (void) data;
4881 severity = tor_malloc(sizeof(log_severity_list_t));
4882 for (i = 0; severity_log_lines[i]; i++) {
4883 memset(severity, 0, sizeof(log_severity_list_t));
4884 ret = parse_log_severity_config(&severity_log_lines[i], severity);
4885 tt_int_op(ret, OP_EQ, 0);
4888 done:
4889 tor_free(severity);
4892 static void
4893 test_config_include_limit(void *data)
4895 (void)data;
4897 config_line_t *result = NULL;
4898 char *torrc_path = NULL;
4899 char *dir = tor_strdup(get_fname("test_include_limit"));
4900 tt_ptr_op(dir, OP_NE, NULL);
4902 #ifdef _WIN32
4903 tt_int_op(mkdir(dir), OP_EQ, 0);
4904 #else
4905 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4906 #endif
4908 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
4909 char torrc_contents[1000];
4910 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4911 torrc_path);
4912 tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
4914 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4915 OP_EQ, -1);
4917 done:
4918 config_free_lines(result);
4919 tor_free(torrc_path);
4920 tor_free(dir);
4923 static void
4924 test_config_include_does_not_exist(void *data)
4926 (void)data;
4928 config_line_t *result = NULL;
4929 char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
4930 char *missing_path = NULL;
4931 tt_ptr_op(dir, OP_NE, NULL);
4933 #ifdef _WIN32
4934 tt_int_op(mkdir(dir), OP_EQ, 0);
4935 #else
4936 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4937 #endif
4939 tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
4940 char torrc_contents[1000];
4941 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4942 missing_path);
4944 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4945 OP_EQ, -1);
4947 done:
4948 config_free_lines(result);
4949 tor_free(dir);
4950 tor_free(missing_path);
4953 static void
4954 test_config_include_error_in_included_file(void *data)
4956 (void)data;
4957 config_line_t *result = NULL;
4959 char *dir = tor_strdup(get_fname("test_error_in_included_file"));
4960 char *invalid_path = NULL;
4961 tt_ptr_op(dir, OP_NE, NULL);
4963 #ifdef _WIN32
4964 tt_int_op(mkdir(dir), OP_EQ, 0);
4965 #else
4966 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
4967 #endif
4969 tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
4970 tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
4972 char torrc_contents[1000];
4973 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
4974 invalid_path);
4976 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
4977 OP_EQ, -1);
4979 done:
4980 config_free_lines(result);
4981 tor_free(dir);
4982 tor_free(invalid_path);
4985 static void
4986 test_config_include_empty_file_folder(void *data)
4988 (void)data;
4989 config_line_t *result = NULL;
4991 char *folder_path = NULL;
4992 char *file_path = NULL;
4993 char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
4994 tt_ptr_op(dir, OP_NE, NULL);
4996 #ifdef _WIN32
4997 tt_int_op(mkdir(dir), OP_EQ, 0);
4998 #else
4999 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5000 #endif
5002 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir);
5003 #ifdef _WIN32
5004 tt_int_op(mkdir(folder_path), OP_EQ, 0);
5005 #else
5006 tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
5007 #endif
5008 tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
5009 tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
5011 char torrc_contents[1000];
5012 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5013 "%%include %s\n"
5014 "%%include %s\n",
5015 folder_path, file_path);
5017 int include_used;
5018 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5019 NULL), OP_EQ, 0);
5020 tt_ptr_op(result, OP_EQ, NULL);
5021 tt_int_op(include_used, OP_EQ, 1);
5023 done:
5024 config_free_lines(result);
5025 tor_free(folder_path);
5026 tor_free(file_path);
5027 tor_free(dir);
5030 #ifndef _WIN32
5031 static void
5032 test_config_include_no_permission(void *data)
5034 (void)data;
5035 config_line_t *result = NULL;
5037 char *folder_path = NULL;
5038 char *dir = NULL;
5039 if (geteuid() == 0)
5040 tt_skip();
5042 dir = tor_strdup(get_fname("test_include_forbidden_folder"));
5043 tt_ptr_op(dir, OP_NE, NULL);
5045 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5047 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
5048 tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
5050 char torrc_contents[1000];
5051 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5052 "%%include %s\n",
5053 folder_path);
5055 int include_used;
5056 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
5057 &include_used, NULL),
5058 OP_EQ, -1);
5059 tt_ptr_op(result, OP_EQ, NULL);
5061 done:
5062 config_free_lines(result);
5063 tor_free(folder_path);
5064 if (dir)
5065 chmod(dir, 0700);
5066 tor_free(dir);
5068 #endif
5070 static void
5071 test_config_include_recursion_before_after(void *data)
5073 (void)data;
5075 config_line_t *result = NULL;
5076 char *torrc_path = NULL;
5077 char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
5078 tt_ptr_op(dir, OP_NE, NULL);
5080 #ifdef _WIN32
5081 tt_int_op(mkdir(dir), OP_EQ, 0);
5082 #else
5083 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5084 #endif
5086 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5088 char file_contents[1000];
5089 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5090 int i;
5091 // Loop backwards so file_contents has the contents of the first file by the
5092 // end of the loop
5093 for (i = limit; i > 0; i--) {
5094 if (i < limit) {
5095 tor_snprintf(file_contents, sizeof(file_contents),
5096 "Test %d\n"
5097 "%%include %s%d\n"
5098 "Test %d\n",
5099 i, torrc_path, i + 1, 2 * limit - i);
5100 } else {
5101 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
5104 if (i > 1) {
5105 char *file_path = NULL;
5106 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5107 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5108 tor_free(file_path);
5112 int include_used;
5113 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5114 NULL), OP_EQ, 0);
5115 tt_ptr_op(result, OP_NE, NULL);
5116 tt_int_op(include_used, OP_EQ, 1);
5118 int len = 0;
5119 config_line_t *next;
5120 for (next = result; next != NULL; next = next->next) {
5121 char expected[10];
5122 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5123 tt_str_op(next->key, OP_EQ, "Test");
5124 tt_str_op(next->value, OP_EQ, expected);
5125 len++;
5127 tt_int_op(len, OP_EQ, 2 * limit - 1);
5129 done:
5130 config_free_lines(result);
5131 tor_free(dir);
5132 tor_free(torrc_path);
5135 static void
5136 test_config_include_recursion_after_only(void *data)
5138 (void)data;
5140 config_line_t *result = NULL;
5141 char *torrc_path = NULL;
5142 char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
5143 tt_ptr_op(dir, OP_NE, NULL);
5145 #ifdef _WIN32
5146 tt_int_op(mkdir(dir), OP_EQ, 0);
5147 #else
5148 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5149 #endif
5151 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
5153 char file_contents[1000];
5154 const int limit = MAX_INCLUDE_RECURSION_LEVEL;
5155 int i;
5156 // Loop backwards so file_contents has the contents of the first file by the
5157 // end of the loop
5158 for (i = limit; i > 0; i--) {
5159 int n = (i - limit - 1) * -1;
5160 if (i < limit) {
5161 tor_snprintf(file_contents, sizeof(file_contents),
5162 "%%include %s%d\n"
5163 "Test %d\n",
5164 torrc_path, i + 1, n);
5165 } else {
5166 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
5169 if (i > 1) {
5170 char *file_path = NULL;
5171 tor_asprintf(&file_path, "%s%d", torrc_path, i);
5172 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
5173 tor_free(file_path);
5177 int include_used;
5178 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
5179 NULL), OP_EQ, 0);
5180 tt_ptr_op(result, OP_NE, NULL);
5181 tt_int_op(include_used, OP_EQ, 1);
5183 int len = 0;
5184 config_line_t *next;
5185 for (next = result; next != NULL; next = next->next) {
5186 char expected[10];
5187 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5188 tt_str_op(next->key, OP_EQ, "Test");
5189 tt_str_op(next->value, OP_EQ, expected);
5190 len++;
5192 tt_int_op(len, OP_EQ, limit);
5194 done:
5195 config_free_lines(result);
5196 tor_free(dir);
5197 tor_free(torrc_path);
5200 static void
5201 test_config_include_folder_order(void *data)
5203 (void)data;
5205 config_line_t *result = NULL;
5206 char *torrcd = NULL;
5207 char *path = NULL;
5208 char *path2 = NULL;
5209 char *dir = tor_strdup(get_fname("test_include_folder_order"));
5210 tt_ptr_op(dir, OP_NE, NULL);
5212 #ifdef _WIN32
5213 tt_int_op(mkdir(dir), OP_EQ, 0);
5214 #else
5215 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5216 #endif
5218 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5220 #ifdef _WIN32
5221 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5222 #else
5223 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5224 #endif
5226 // test that files in subfolders are ignored
5227 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5229 #ifdef _WIN32
5230 tt_int_op(mkdir(path), OP_EQ, 0);
5231 #else
5232 tt_int_op(mkdir(path, 0700), OP_EQ, 0);
5233 #endif
5235 tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
5236 tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
5237 tor_free(path);
5239 // test that files starting with . are ignored
5240 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5241 tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
5242 tor_free(path);
5244 // test file order
5245 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
5246 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5247 tor_free(path);
5249 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
5250 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
5251 tor_free(path);
5253 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
5254 tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
5255 tor_free(path);
5257 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
5258 tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
5259 tor_free(path);
5261 char torrc_contents[1000];
5262 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5263 "%%include %s\n",
5264 torrcd);
5266 int include_used;
5267 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5268 NULL), OP_EQ, 0);
5269 tt_ptr_op(result, OP_NE, NULL);
5270 tt_int_op(include_used, OP_EQ, 1);
5272 int len = 0;
5273 config_line_t *next;
5274 for (next = result; next != NULL; next = next->next) {
5275 char expected[10];
5276 tor_snprintf(expected, sizeof(expected), "%d", len + 1);
5277 tt_str_op(next->key, OP_EQ, "Test");
5278 tt_str_op(next->value, OP_EQ, expected);
5279 len++;
5281 tt_int_op(len, OP_EQ, 4);
5283 done:
5284 config_free_lines(result);
5285 tor_free(torrcd);
5286 tor_free(path);
5287 tor_free(path2);
5288 tor_free(dir);
5291 static void
5292 test_config_include_path_syntax(void *data)
5294 (void)data;
5296 config_line_t *result = NULL;
5297 char *dir = tor_strdup(get_fname("test_include_path_syntax"));
5298 char *esc_dir = NULL, *dir_with_pathsep = NULL,
5299 *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
5300 tt_ptr_op(dir, OP_NE, NULL);
5302 #ifdef _WIN32
5303 tt_int_op(mkdir(dir), OP_EQ, 0);
5304 #else
5305 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5306 #endif
5308 esc_dir = esc_for_log(dir);
5309 tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
5310 esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
5312 tor_asprintf(&torrc_contents,
5313 "%%include %s\n"
5314 "%%include %s%s \n" // space to avoid suppressing newline
5315 "%%include %s\n",
5316 esc_dir,
5317 dir, PATH_SEPARATOR,
5318 esc_dir_with_pathsep);
5320 int include_used;
5321 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5322 NULL), OP_EQ, 0);
5323 tt_ptr_op(result, OP_EQ, NULL);
5324 tt_int_op(include_used, OP_EQ, 1);
5326 done:
5327 config_free_lines(result);
5328 tor_free(dir);
5329 tor_free(torrc_contents);
5330 tor_free(esc_dir);
5331 tor_free(dir_with_pathsep);
5332 tor_free(esc_dir_with_pathsep);
5335 static void
5336 test_config_include_not_processed(void *data)
5338 (void)data;
5340 char torrc_contents[1000] = "%include does_not_exist\n";
5341 config_line_t *result = NULL;
5342 tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
5343 tt_ptr_op(result, OP_NE, NULL);
5345 int len = 0;
5346 config_line_t *next;
5347 for (next = result; next != NULL; next = next->next) {
5348 tt_str_op(next->key, OP_EQ, "%include");
5349 tt_str_op(next->value, OP_EQ, "does_not_exist");
5350 len++;
5352 tt_int_op(len, OP_EQ, 1);
5354 done:
5355 config_free_lines(result);
5358 static void
5359 test_config_include_has_include(void *data)
5361 (void)data;
5363 config_line_t *result = NULL;
5364 char *dir = tor_strdup(get_fname("test_include_has_include"));
5365 tt_ptr_op(dir, OP_NE, NULL);
5367 #ifdef _WIN32
5368 tt_int_op(mkdir(dir), OP_EQ, 0);
5369 #else
5370 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5371 #endif
5373 char torrc_contents[1000] = "Test 1\n";
5374 int include_used;
5376 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5377 NULL), OP_EQ, 0);
5378 tt_int_op(include_used, OP_EQ, 0);
5379 config_free_lines(result);
5381 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
5382 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
5383 NULL), OP_EQ, 0);
5384 tt_int_op(include_used, OP_EQ, 1);
5386 done:
5387 config_free_lines(result);
5388 tor_free(dir);
5391 static void
5392 test_config_include_flag_both_without(void *data)
5394 (void)data;
5396 char *errmsg = NULL;
5397 char conf_empty[1000];
5398 tor_snprintf(conf_empty, sizeof(conf_empty),
5399 "DataDirectory %s\n",
5400 get_fname(NULL));
5401 // test with defaults-torrc and torrc without include
5402 int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
5403 NULL, &errmsg);
5404 tt_int_op(ret, OP_EQ, 0);
5406 const or_options_t *options = get_options();
5407 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5409 done:
5410 tor_free(errmsg);
5413 static void
5414 test_config_include_flag_torrc_only(void *data)
5416 (void)data;
5418 char *errmsg = NULL;
5419 char *path = NULL;
5420 char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
5421 tt_ptr_op(dir, OP_NE, NULL);
5423 #ifdef _WIN32
5424 tt_int_op(mkdir(dir), OP_EQ, 0);
5425 #else
5426 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5427 #endif
5429 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5430 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5432 char conf_empty[1000];
5433 tor_snprintf(conf_empty, sizeof(conf_empty),
5434 "DataDirectory %s\n",
5435 get_fname(NULL));
5436 char conf_include[1000];
5437 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5439 // test with defaults-torrc without include and torrc with include
5440 int ret = options_init_from_string(conf_empty, conf_include,
5441 CMD_RUN_UNITTESTS, NULL, &errmsg);
5442 tt_int_op(ret, OP_EQ, 0);
5444 const or_options_t *options = get_options();
5445 tt_int_op(options->IncludeUsed, OP_EQ, 1);
5447 done:
5448 tor_free(errmsg);
5449 tor_free(path);
5450 tor_free(dir);
5453 static void
5454 test_config_include_flag_defaults_only(void *data)
5456 (void)data;
5458 char *errmsg = NULL;
5459 char *path = NULL;
5460 char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
5461 tt_ptr_op(dir, OP_NE, NULL);
5463 #ifdef _WIN32
5464 tt_int_op(mkdir(dir), OP_EQ, 0);
5465 #else
5466 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5467 #endif
5469 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
5470 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
5472 char conf_empty[1000];
5473 tor_snprintf(conf_empty, sizeof(conf_empty),
5474 "DataDirectory %s\n",
5475 get_fname(NULL));
5476 char conf_include[1000];
5477 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
5479 // test with defaults-torrc with include and torrc without include
5480 int ret = options_init_from_string(conf_include, conf_empty,
5481 CMD_RUN_UNITTESTS, NULL, &errmsg);
5482 tt_int_op(ret, OP_EQ, 0);
5484 const or_options_t *options = get_options();
5485 tt_int_op(options->IncludeUsed, OP_EQ, 0);
5487 done:
5488 tor_free(errmsg);
5489 tor_free(path);
5490 tor_free(dir);
5493 static void
5494 test_config_dup_and_filter(void *arg)
5496 (void)arg;
5497 /* Test normal input. */
5498 config_line_t *line = NULL;
5499 config_line_append(&line, "abc", "def");
5500 config_line_append(&line, "ghi", "jkl");
5501 config_line_append(&line, "ABCD", "mno");
5503 config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
5504 tt_ptr_op(line_dup, OP_NE, NULL);
5505 tt_ptr_op(line_dup->next, OP_NE, NULL);
5506 tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
5508 tt_str_op(line_dup->key, OP_EQ, "abc");
5509 tt_str_op(line_dup->value, OP_EQ, "def");
5510 tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
5511 tt_str_op(line_dup->next->value, OP_EQ, "mno");
5513 /* empty output */
5514 config_free_lines(line_dup);
5515 line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
5516 tt_ptr_op(line_dup, OP_EQ, NULL);
5518 /* empty input */
5519 config_free_lines(line_dup);
5520 line_dup = config_lines_dup_and_filter(NULL, "abc");
5521 tt_ptr_op(line_dup, OP_EQ, NULL);
5523 done:
5524 config_free_lines(line);
5525 config_free_lines(line_dup);
5528 /* If we're not configured to be a bridge, but we set
5529 * BridgeDistribution, then options_validate () should return -1. */
5530 static void
5531 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
5533 or_options_t* options = get_options_mutable();
5534 or_options_t* old_options = options;
5535 or_options_t* default_options = options;
5536 char* message = NULL;
5537 int ret;
5539 (void)arg;
5541 options->BridgeRelay = 0;
5542 options->BridgeDistribution = (char*)("https");
5544 ret = options_validate(old_options, options, default_options, 0, &message);
5546 tt_int_op(ret, OP_EQ, -1);
5547 tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
5548 "didn't set BridgeRelay!");
5549 done:
5550 tor_free(message);
5551 options->BridgeDistribution = NULL;
5554 /* If the BridgeDistribution setting was valid, 0 should be returned. */
5555 static void
5556 test_config_check_bridge_distribution_setting_valid(void *arg)
5558 int ret = check_bridge_distribution_setting("https");
5560 (void)arg;
5562 tt_int_op(ret, OP_EQ, 0);
5563 done:
5564 return;
5567 /* If the BridgeDistribution setting was invalid, -1 should be returned. */
5568 static void
5569 test_config_check_bridge_distribution_setting_invalid(void *arg)
5571 int ret = check_bridge_distribution_setting("hyphens-are-allowed");
5573 (void)arg;
5575 tt_int_op(ret, OP_EQ, 0);
5577 ret = check_bridge_distribution_setting("asterisks*are*forbidden");
5579 tt_int_op(ret, OP_EQ, -1);
5580 done:
5581 return;
5584 /* If the BridgeDistribution setting was unrecognised, a warning should be
5585 * logged and 0 should be returned. */
5586 static void
5587 test_config_check_bridge_distribution_setting_unrecognised(void *arg)
5589 int ret = check_bridge_distribution_setting("unicorn");
5591 (void)arg;
5593 tt_int_op(ret, OP_EQ, 0);
5594 done:
5595 return;
5598 static void
5599 test_config_include_opened_file_list(void *data)
5601 (void)data;
5603 config_line_t *result = NULL;
5604 smartlist_t *opened_files = smartlist_new();
5605 char *torrcd = NULL;
5606 char *subfolder = NULL;
5607 char *path = NULL;
5608 char *empty = NULL;
5609 char *file = NULL;
5610 char *dot = NULL;
5611 char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
5612 tt_ptr_op(dir, OP_NE, NULL);
5614 #ifdef _WIN32
5615 tt_int_op(mkdir(dir), OP_EQ, 0);
5616 #else
5617 tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
5618 #endif
5620 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
5622 #ifdef _WIN32
5623 tt_int_op(mkdir(torrcd), OP_EQ, 0);
5624 #else
5625 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
5626 #endif
5628 tor_asprintf(&subfolder, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
5630 #ifdef _WIN32
5631 tt_int_op(mkdir(subfolder), OP_EQ, 0);
5632 #else
5633 tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
5634 #endif
5636 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", subfolder,
5637 "01_file_in_subfolder");
5638 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
5640 tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty");
5641 tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
5643 tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file");
5644 tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
5646 tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
5647 tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
5649 char torrc_contents[1000];
5650 tor_snprintf(torrc_contents, sizeof(torrc_contents),
5651 "%%include %s\n",
5652 torrcd);
5654 int include_used;
5655 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
5656 opened_files), OP_EQ, 0);
5657 tt_ptr_op(result, OP_NE, NULL);
5658 tt_int_op(include_used, OP_EQ, 1);
5660 tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
5661 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
5662 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
5663 // files inside subfolders are not opended, only the subfolder is opened
5664 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
5665 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
5666 // dot files are not opened as we ignore them when we get their name from
5667 // their parent folder
5669 done:
5670 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
5671 smartlist_free(opened_files);
5672 config_free_lines(result);
5673 tor_free(torrcd);
5674 tor_free(subfolder);
5675 tor_free(path);
5676 tor_free(empty);
5677 tor_free(file);
5678 tor_free(dot);
5679 tor_free(dir);
5682 static void
5683 test_config_compute_max_mem_in_queues(void *data)
5685 #define GIGABYTE(x) (UINT64_C(x) << 30)
5686 #define MEGABYTE(x) (UINT64_C(x) << 20)
5687 (void)data;
5688 MOCK(get_total_system_memory, get_total_system_memory_mock);
5690 /* We are unable to detect the amount of memory on the system. Tor will try
5691 * to use some sensible default values for 64-bit and 32-bit systems. */
5692 total_system_memory_return = -1;
5694 #if SIZEOF_VOID_P >= 8
5695 /* We are on a 64-bit system. */
5696 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
5697 #else
5698 /* We are on a 32-bit system. */
5699 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
5700 #endif
5702 /* We are able to detect the amount of RAM on the system. */
5703 total_system_memory_return = 0;
5705 /* We are running on a system with one gigabyte of RAM. */
5706 total_system_memory_output = GIGABYTE(1);
5708 /* We have 0.75 * RAM available. */
5709 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5710 3 * (GIGABYTE(1) / 4));
5712 /* We are running on a tiny machine with 256 MB of RAM. */
5713 total_system_memory_output = MEGABYTE(256);
5715 /* We will now enforce a minimum of 256 MB of RAM available for the
5716 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
5717 * MB available. */
5718 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
5720 #if SIZEOF_SIZE_T > 4
5721 /* We are running on a machine with 8 GB of RAM. */
5722 total_system_memory_output = GIGABYTE(8);
5724 /* We will have 0.4 * RAM available. */
5725 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5726 2 * (GIGABYTE(8) / 5));
5728 /* We are running on a machine with 16 GB of RAM. */
5729 total_system_memory_output = GIGABYTE(16);
5731 /* We will have 0.4 * RAM available. */
5732 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5733 2 * (GIGABYTE(16) / 5));
5735 /* We are running on a machine with 32 GB of RAM. */
5736 total_system_memory_output = GIGABYTE(32);
5738 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
5739 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
5740 MAX_DEFAULT_MEMORY_QUEUE_SIZE);
5741 #endif
5743 done:
5744 UNMOCK(get_total_system_memory);
5746 #undef GIGABYTE
5747 #undef MEGABYTE
5750 static void
5751 test_config_extended_fmt(void *arg)
5753 (void)arg;
5754 config_line_t *lines = NULL, *lp;
5755 const char string1[] =
5756 "thing1 is here\n"
5757 "+thing2 is over here\n"
5758 "/thing3\n"
5759 "/thing4 is back here\n";
5761 /* Try with the "extended" flag disabled. */
5762 int r = config_get_lines(string1, &lines, 0);
5763 tt_int_op(r, OP_EQ, 0);
5764 lp = lines;
5765 tt_ptr_op(lp, OP_NE, NULL);
5766 tt_str_op(lp->key, OP_EQ, "thing1");
5767 tt_str_op(lp->value, OP_EQ, "is here");
5768 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
5769 lp = lp->next;
5770 tt_ptr_op(lp, OP_NE, NULL);
5771 tt_str_op(lp->key, OP_EQ, "+thing2");
5772 tt_str_op(lp->value, OP_EQ, "is over here");
5773 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
5774 lp = lp->next;
5775 tt_ptr_op(lp, OP_NE, NULL);
5776 tt_str_op(lp->key, OP_EQ, "/thing3");
5777 tt_str_op(lp->value, OP_EQ, "");
5778 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
5779 lp = lp->next;
5780 tt_ptr_op(lp, OP_NE, NULL);
5781 tt_str_op(lp->key, OP_EQ, "/thing4");
5782 tt_str_op(lp->value, OP_EQ, "is back here");
5783 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
5784 lp = lp->next;
5785 config_free_lines(lines);
5787 /* Try with the "extended" flag enabled. */
5788 r = config_get_lines(string1, &lines, 1);
5789 tt_int_op(r, OP_EQ, 0);
5790 lp = lines;
5791 tt_ptr_op(lp, OP_NE, NULL);
5792 tt_str_op(lp->key, OP_EQ, "thing1");
5793 tt_str_op(lp->value, OP_EQ, "is here");
5794 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
5795 lp = lp->next;
5796 tt_ptr_op(lp, OP_NE, NULL);
5797 tt_str_op(lp->key, OP_EQ, "thing2");
5798 tt_str_op(lp->value, OP_EQ, "is over here");
5799 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_APPEND);
5800 lp = lp->next;
5801 tt_ptr_op(lp, OP_NE, NULL);
5802 tt_str_op(lp->key, OP_EQ, "thing3");
5803 tt_str_op(lp->value, OP_EQ, "");
5804 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
5805 lp = lp->next;
5806 tt_ptr_op(lp, OP_NE, NULL);
5807 tt_str_op(lp->key, OP_EQ, "thing4");
5808 tt_str_op(lp->value, OP_EQ, "");
5809 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
5810 lp = lp->next;
5812 done:
5813 config_free_lines(lines);
5816 #define CONFIG_TEST(name, flags) \
5817 { #name, test_config_ ## name, flags, NULL, NULL }
5819 struct testcase_t config_tests[] = {
5820 CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
5821 CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
5822 CONFIG_TEST(parsing_trusted_dir_server, 0),
5823 CONFIG_TEST(parsing_invalid_dir_address, 0),
5824 CONFIG_TEST(parsing_fallback_dir_server, 0),
5825 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
5826 CONFIG_TEST(adding_dir_servers, TT_FORK),
5827 CONFIG_TEST(default_dir_servers, TT_FORK),
5828 CONFIG_TEST(default_fallback_dirs, 0),
5829 CONFIG_TEST(resolve_my_address, TT_FORK),
5830 CONFIG_TEST(addressmap, 0),
5831 CONFIG_TEST(parse_bridge_line, 0),
5832 CONFIG_TEST(parse_transport_options_line, 0),
5833 CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
5834 CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
5835 CONFIG_TEST(write_to_data_subdir, TT_FORK),
5836 CONFIG_TEST(fix_my_family, 0),
5837 CONFIG_TEST(directory_fetch, 0),
5838 CONFIG_TEST(port_cfg_line_extract_addrport, 0),
5839 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
5840 CONFIG_TEST(parse_port_config__ports__server_options, 0),
5841 CONFIG_TEST(parse_port_config__ports__ports_given, 0),
5842 CONFIG_TEST(parse_log_severity, 0),
5843 CONFIG_TEST(include_limit, 0),
5844 CONFIG_TEST(include_does_not_exist, 0),
5845 CONFIG_TEST(include_error_in_included_file, 0),
5846 CONFIG_TEST(include_empty_file_folder, 0),
5847 #ifndef _WIN32
5848 CONFIG_TEST(include_no_permission, 0),
5849 #endif
5850 CONFIG_TEST(include_recursion_before_after, 0),
5851 CONFIG_TEST(include_recursion_after_only, 0),
5852 CONFIG_TEST(include_folder_order, 0),
5853 CONFIG_TEST(include_path_syntax, 0),
5854 CONFIG_TEST(include_not_processed, 0),
5855 CONFIG_TEST(include_has_include, 0),
5856 CONFIG_TEST(include_flag_both_without, TT_FORK),
5857 CONFIG_TEST(include_flag_torrc_only, TT_FORK),
5858 CONFIG_TEST(include_flag_defaults_only, TT_FORK),
5859 CONFIG_TEST(dup_and_filter, 0),
5860 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
5861 CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
5862 CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
5863 CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
5864 CONFIG_TEST(include_opened_file_list, 0),
5865 CONFIG_TEST(compute_max_mem_in_queues, 0),
5866 CONFIG_TEST(extended_fmt, 0),
5867 END_OF_TESTCASES