test_config.c (249572B)
1 /* Copyright (c) 2001-2004, Roger Dingledine. 2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 /* See LICENSE for licensing information */ 5 6 #include "orconfig.h" 7 8 #define CONFIG_PRIVATE 9 #define RELAY_CONFIG_PRIVATE 10 #define RELAY_TRANSPORT_CONFIG_PRIVATE 11 #define PT_PRIVATE 12 #define ROUTERSET_PRIVATE 13 #include "core/or/or.h" 14 #include "lib/net/address.h" 15 #include "lib/net/resolve.h" 16 #include "feature/client/addressmap.h" 17 #include "feature/client/bridges.h" 18 #include "core/or/circuitmux_ewma.h" 19 #include "core/or/circuitbuild.h" 20 #include "app/config/config.h" 21 #include "app/config/resolve_addr.h" 22 #include "feature/relay/relay_config.h" 23 #include "feature/relay/transport_config.h" 24 #include "lib/confmgt/confmgt.h" 25 #include "core/mainloop/connection.h" 26 #include "core/or/connection_edge.h" 27 #include "test/test.h" 28 #include "core/or/connection_or.h" 29 #include "feature/control/control.h" 30 #include "core/mainloop/cpuworker.h" 31 #include "feature/dircache/dirserv.h" 32 #include "feature/dirclient/dirclient_modes.h" 33 #include "feature/dirauth/dirvote.h" 34 #include "feature/relay/dns.h" 35 #include "feature/client/entrynodes.h" 36 #include "feature/client/transports.h" 37 #include "feature/relay/ext_orport.h" 38 #include "lib/geoip/geoip.h" 39 #include "feature/hibernate/hibernate.h" 40 #include "core/mainloop/mainloop.h" 41 #include "feature/nodelist/networkstatus.h" 42 #include "feature/nodelist/nodelist.h" 43 #include "core/or/policies.h" 44 #include "feature/relay/relay_find_addr.h" 45 #include "feature/relay/router.h" 46 #include "feature/relay/routermode.h" 47 #include "feature/nodelist/dirlist.h" 48 #include "feature/nodelist/routerlist.h" 49 #include "feature/nodelist/routerset.h" 50 #include "app/config/statefile.h" 51 52 #include "test/test_helpers.h" 53 #include "test/resolve_test_helpers.h" 54 #include "test/log_test_helpers.h" 55 56 #include "feature/dirclient/dir_server_st.h" 57 #include "core/or/port_cfg_st.h" 58 #include "feature/nodelist/routerinfo_st.h" 59 60 #include "lib/fs/conffile.h" 61 #include "lib/meminfo/meminfo.h" 62 #include "lib/net/gethostname.h" 63 #include "lib/encoding/confline.h" 64 #include "lib/encoding/kvline.h" 65 66 #ifdef HAVE_UNISTD_H 67 #include <unistd.h> 68 #endif 69 #ifdef HAVE_SYS_STAT_H 70 #include <sys/stat.h> 71 #endif 72 73 static void 74 test_config_addressmap(void *arg) 75 { 76 char buf[1024]; 77 char address[256]; 78 time_t expires = TIME_MAX; 79 (void)arg; 80 81 strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid 82 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid 83 "MapAddress *.google.com *.torserver.exit\n" 84 "MapAddress *.yahoo.com *.google.com.torserver.exit\n" 85 "MapAddress *.cn.com www.cnn.com\n" 86 "MapAddress *.cnn.com www.cnn.com\n" 87 "MapAddress ex.com www.cnn.com\n" 88 "MapAddress ey.com *.cnn.com\n" 89 "MapAddress www.torproject.org 1.1.1.1\n" 90 "MapAddress other.torproject.org " 91 "this.torproject.org.otherserver.exit\n" 92 "MapAddress test.torproject.org 2.2.2.2\n" 93 "MapAddress www.google.com 3.3.3.3\n" 94 "MapAddress www.example.org 4.4.4.4\n" 95 "MapAddress 4.4.4.4 7.7.7.7\n" 96 "MapAddress 4.4.4.4 5.5.5.5\n" 97 "MapAddress www.infiniteloop.org 6.6.6.6\n" 98 "MapAddress 6.6.6.6 www.infiniteloop.org\n" 99 , sizeof(buf)); 100 101 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0); 102 config_register_addressmaps(get_options()); 103 104 /* Use old interface for now, so we don't need to rewrite the unit tests */ 105 #define addressmap_rewrite(a,s,eo,ao) \ 106 addressmap_rewrite((a),(s), ~0, (eo),(ao)) 107 108 /* MapAddress .invalidwildcard.com .torserver.exit - no match */ 109 strlcpy(address, "www.invalidwildcard.com", sizeof(address)); 110 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 111 112 /* MapAddress *invalidasterisk.com .torserver.exit - no match */ 113 strlcpy(address, "www.invalidasterisk.com", sizeof(address)); 114 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 115 116 /* Where no mapping for FQDN match on top-level domain */ 117 /* MapAddress .google.com .torserver.exit */ 118 strlcpy(address, "reader.google.com", sizeof(address)); 119 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 120 tt_str_op(address,OP_EQ, "reader.torserver.exit"); 121 122 /* MapAddress *.yahoo.com *.google.com.torserver.exit */ 123 strlcpy(address, "reader.yahoo.com", sizeof(address)); 124 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 125 tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit"); 126 127 /*MapAddress *.cnn.com www.cnn.com */ 128 strlcpy(address, "cnn.com", sizeof(address)); 129 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 130 tt_str_op(address,OP_EQ, "www.cnn.com"); 131 132 /* MapAddress .cn.com www.cnn.com */ 133 strlcpy(address, "www.cn.com", sizeof(address)); 134 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 135 tt_str_op(address,OP_EQ, "www.cnn.com"); 136 137 /* MapAddress ex.com www.cnn.com - no match */ 138 strlcpy(address, "www.ex.com", sizeof(address)); 139 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 140 141 /* MapAddress ey.com *.cnn.com - invalid expression */ 142 strlcpy(address, "ey.com", sizeof(address)); 143 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 144 145 /* Where mapping for FQDN match on FQDN */ 146 strlcpy(address, "www.google.com", sizeof(address)); 147 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 148 tt_str_op(address,OP_EQ, "3.3.3.3"); 149 150 strlcpy(address, "www.torproject.org", sizeof(address)); 151 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 152 tt_str_op(address,OP_EQ, "1.1.1.1"); 153 154 strlcpy(address, "other.torproject.org", sizeof(address)); 155 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 156 tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit"); 157 158 strlcpy(address, "test.torproject.org", sizeof(address)); 159 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 160 tt_str_op(address,OP_EQ, "2.2.2.2"); 161 162 /* Test a chain of address mappings and the order in which they were added: 163 "MapAddress www.example.org 4.4.4.4" 164 "MapAddress 4.4.4.4 7.7.7.7" 165 "MapAddress 4.4.4.4 5.5.5.5" 166 */ 167 strlcpy(address, "www.example.org", sizeof(address)); 168 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 169 tt_str_op(address,OP_EQ, "5.5.5.5"); 170 171 /* Test infinite address mapping results in no change */ 172 strlcpy(address, "www.infiniteloop.org", sizeof(address)); 173 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 174 tt_str_op(address,OP_EQ, "www.infiniteloop.org"); 175 176 /* Test we don't find false positives */ 177 strlcpy(address, "www.example.com", sizeof(address)); 178 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 179 180 /* Test top-level-domain matching a bit harder */ 181 config_free_lines(get_options_mutable()->AddressMap); 182 addressmap_clear_configured(); 183 strlcpy(buf, "MapAddress *.com *.torserver.exit\n" 184 "MapAddress *.torproject.org 1.1.1.1\n" 185 "MapAddress *.net 2.2.2.2\n" 186 , sizeof(buf)); 187 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0); 188 config_register_addressmaps(get_options()); 189 190 strlcpy(address, "www.abc.com", sizeof(address)); 191 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 192 tt_str_op(address,OP_EQ, "www.abc.torserver.exit"); 193 194 strlcpy(address, "www.def.com", sizeof(address)); 195 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 196 tt_str_op(address,OP_EQ, "www.def.torserver.exit"); 197 198 strlcpy(address, "www.torproject.org", sizeof(address)); 199 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 200 tt_str_op(address,OP_EQ, "1.1.1.1"); 201 202 strlcpy(address, "test.torproject.org", sizeof(address)); 203 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 204 tt_str_op(address,OP_EQ, "1.1.1.1"); 205 206 strlcpy(address, "torproject.net", sizeof(address)); 207 tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL)); 208 tt_str_op(address,OP_EQ, "2.2.2.2"); 209 210 /* We don't support '*' as a mapping directive */ 211 config_free_lines(get_options_mutable()->AddressMap); 212 addressmap_clear_configured(); 213 strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf)); 214 config_get_lines(buf, &(get_options_mutable()->AddressMap), 0); 215 config_register_addressmaps(get_options()); 216 217 strlcpy(address, "www.abc.com", sizeof(address)); 218 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 219 220 strlcpy(address, "www.def.net", sizeof(address)); 221 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 222 223 strlcpy(address, "www.torproject.org", sizeof(address)); 224 tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL)); 225 226 #undef addressmap_rewrite 227 228 done: 229 config_free_lines(get_options_mutable()->AddressMap); 230 get_options_mutable()->AddressMap = NULL; 231 addressmap_free_all(); 232 } 233 234 static int 235 is_private_dir(const char* path) 236 { 237 struct stat st; 238 int r = stat(path, &st); 239 if (r) { 240 return 0; 241 } 242 #if !defined (_WIN32) 243 if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) { 244 return 0; 245 } 246 #endif 247 return 1; 248 } 249 250 static void 251 test_config_check_or_create_data_subdir(void *arg) 252 { 253 or_options_t *options = get_options_mutable(); 254 char *datadir; 255 const char *subdir = "test_stats"; 256 char *subpath; 257 struct stat st; 258 int r; 259 #if !defined (_WIN32) 260 unsigned group_permission; 261 #endif 262 (void)arg; 263 264 tor_free(options->DataDirectory); 265 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0")); 266 subpath = get_datadir_fname(subdir); 267 268 #if defined (_WIN32) 269 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0); 270 #else 271 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0); 272 #endif 273 274 r = stat(subpath, &st); 275 276 // The subdirectory shouldn't exist yet, 277 // but should be created by the call to check_or_create_data_subdir. 278 tt_assert(r && (errno == ENOENT)); 279 tt_assert(!check_or_create_data_subdir(subdir)); 280 tt_assert(is_private_dir(subpath)); 281 282 // The check should return 0, if the directory already exists 283 // and is private to the user. 284 tt_assert(!check_or_create_data_subdir(subdir)); 285 286 r = stat(subpath, &st); 287 if (r) { 288 tt_abort_perror("stat"); 289 } 290 291 #if !defined (_WIN32) 292 group_permission = st.st_mode | 0070; 293 r = chmod(subpath, group_permission); 294 295 if (r) { 296 tt_abort_perror("chmod"); 297 } 298 299 // If the directory exists, but its mode is too permissive 300 // a call to check_or_create_data_subdir should reset the mode. 301 tt_assert(!is_private_dir(subpath)); 302 tt_assert(!check_or_create_data_subdir(subdir)); 303 tt_assert(is_private_dir(subpath)); 304 #endif /* !defined (_WIN32) */ 305 306 done: 307 rmdir(subpath); 308 tor_free(datadir); 309 tor_free(subpath); 310 } 311 312 static void 313 test_config_write_to_data_subdir(void *arg) 314 { 315 or_options_t* options = get_options_mutable(); 316 char *datadir; 317 char *cp = NULL; 318 const char* subdir = "test_stats"; 319 const char* fname = "test_file"; 320 const char* str = 321 "Lorem ipsum dolor sit amet, consetetur sadipscing\n" 322 "elitr, sed diam nonumy eirmod\n" 323 "tempor invidunt ut labore et dolore magna aliquyam\n" 324 "erat, sed diam voluptua.\n" 325 "At vero eos et accusam et justo duo dolores et ea\n" 326 "rebum. Stet clita kasd gubergren,\n" 327 "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n" 328 "Lorem ipsum dolor sit amet,\n" 329 "consetetur sadipscing elitr, sed diam nonumy eirmod\n" 330 "tempor invidunt ut labore et dolore\n" 331 "magna aliquyam erat, sed diam voluptua. At vero eos et\n" 332 "accusam et justo duo dolores et\n" 333 "ea rebum. Stet clita kasd gubergren, no sea takimata\n" 334 "sanctus est Lorem ipsum dolor sit amet."; 335 char* filepath = NULL; 336 (void)arg; 337 338 tor_free(options->DataDirectory); 339 datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1")); 340 filepath = get_datadir_fname2(subdir, fname); 341 342 #if defined (_WIN32) 343 tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0); 344 #else 345 tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0); 346 #endif 347 348 // Write attempt should fail, if subdirectory doesn't exist. 349 tt_assert(write_to_data_subdir(subdir, fname, str, NULL)); 350 tt_assert(! check_or_create_data_subdir(subdir)); 351 352 // Content of file after write attempt should be 353 // equal to the original string. 354 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL)); 355 cp = read_file_to_str(filepath, 0, NULL); 356 tt_str_op(cp,OP_EQ, str); 357 tor_free(cp); 358 359 // A second write operation should overwrite the old content. 360 tt_assert(!write_to_data_subdir(subdir, fname, str, NULL)); 361 cp = read_file_to_str(filepath, 0, NULL); 362 tt_str_op(cp,OP_EQ, str); 363 tor_free(cp); 364 365 done: 366 (void) unlink(filepath); 367 rmdir(options->DataDirectory); 368 tor_free(datadir); 369 tor_free(filepath); 370 tor_free(cp); 371 } 372 373 /* Test helper function: Make sure that a bridge line gets parsed 374 * properly. Also make sure that the resulting bridge_line_t structure 375 * has its fields set correctly. */ 376 static void 377 good_bridge_line_test(const char *string, const char *test_addrport, 378 const char *test_digest, const char *test_transport, 379 const smartlist_t *test_socks_args) 380 { 381 char *tmp = NULL; 382 bridge_line_t *bridge_line = parse_bridge_line(string); 383 tt_assert(bridge_line); 384 385 /* test addrport */ 386 tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port)); 387 tt_str_op(test_addrport,OP_EQ, tmp); 388 tor_free(tmp); 389 390 /* If we were asked to validate a digest, but we did not get a 391 digest after parsing, we failed. */ 392 if (test_digest && tor_digest_is_zero(bridge_line->digest)) 393 tt_abort(); 394 395 /* If we were not asked to validate a digest, and we got a digest 396 after parsing, we failed again. */ 397 if (!test_digest && !tor_digest_is_zero(bridge_line->digest)) 398 tt_abort(); 399 400 /* If we were asked to validate a digest, and we got a digest after 401 parsing, make sure it's correct. */ 402 if (test_digest) { 403 tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN)); 404 tor_strlower(tmp); 405 tt_str_op(test_digest,OP_EQ, tmp); 406 tor_free(tmp); 407 } 408 409 /* If we were asked to validate a transport name, make sure that it 410 matches with the transport name that was parsed. */ 411 if (test_transport && !bridge_line->transport_name) 412 tt_abort(); 413 if (!test_transport && bridge_line->transport_name) 414 tt_abort(); 415 if (test_transport) 416 tt_str_op(test_transport,OP_EQ, bridge_line->transport_name); 417 418 /* Validate the SOCKS argument smartlist. */ 419 if (test_socks_args && !bridge_line->socks_args) 420 tt_abort(); 421 if (!test_socks_args && bridge_line->socks_args) 422 tt_abort(); 423 if (test_socks_args) 424 tt_assert(smartlist_strings_eq(test_socks_args, 425 bridge_line->socks_args)); 426 427 done: 428 tor_free(tmp); 429 bridge_line_free(bridge_line); 430 } 431 432 /* Test helper function: Make sure that a bridge line is 433 * unparseable. */ 434 static void 435 bad_bridge_line_test(const char *string) 436 { 437 bridge_line_t *bridge_line = parse_bridge_line(string); 438 if (bridge_line) 439 TT_FAIL(("%s was supposed to fail, but it didn't.", string)); 440 tt_ptr_op(bridge_line, OP_EQ, NULL); 441 442 done: 443 bridge_line_free(bridge_line); 444 } 445 446 static void 447 test_config_parse_bridge_line(void *arg) 448 { 449 (void) arg; 450 good_bridge_line_test("192.0.2.1:4123", 451 "192.0.2.1:4123", NULL, NULL, NULL); 452 453 good_bridge_line_test("192.0.2.1", 454 "192.0.2.1:443", NULL, NULL, NULL); 455 456 good_bridge_line_test("transport [::1]", 457 "[::1]:443", NULL, "transport", NULL); 458 459 good_bridge_line_test("transport 192.0.2.1:12 " 460 "4352e58420e68f5e40bf7c74faddccd9d1349413", 461 "192.0.2.1:12", 462 "4352e58420e68f5e40bf7c74faddccd9d1349413", 463 "transport", NULL); 464 465 { 466 smartlist_t *sl_tmp = smartlist_new(); 467 smartlist_add_asprintf(sl_tmp, "twoandtwo=five"); 468 469 good_bridge_line_test("transport 192.0.2.1:12 " 470 "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five", 471 "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413", 472 "transport", sl_tmp); 473 474 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 475 smartlist_free(sl_tmp); 476 } 477 478 { 479 smartlist_t *sl_tmp = smartlist_new(); 480 smartlist_add_asprintf(sl_tmp, "twoandtwo=five"); 481 smartlist_add_asprintf(sl_tmp, "z=z"); 482 483 good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z", 484 "192.0.2.1:12", NULL, "transport", sl_tmp); 485 486 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 487 smartlist_free(sl_tmp); 488 } 489 490 { 491 smartlist_t *sl_tmp = smartlist_new(); 492 smartlist_add_asprintf(sl_tmp, "dub=come"); 493 smartlist_add_asprintf(sl_tmp, "save=me"); 494 495 good_bridge_line_test("transport 192.0.2.1:12 " 496 "4352e58420e68f5e40bf7c74faddccd9d1349666 " 497 "dub=come save=me", 498 499 "192.0.2.1:12", 500 "4352e58420e68f5e40bf7c74faddccd9d1349666", 501 "transport", sl_tmp); 502 503 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 504 smartlist_free(sl_tmp); 505 } 506 507 good_bridge_line_test("192.0.2.1:1231 " 508 "4352e58420e68f5e40bf7c74faddccd9d1349413", 509 "192.0.2.1:1231", 510 "4352e58420e68f5e40bf7c74faddccd9d1349413", 511 NULL, NULL); 512 513 /* Empty line */ 514 bad_bridge_line_test(""); 515 /* bad transport name */ 516 bad_bridge_line_test("tr$n_sp0r7 190.20.2.2"); 517 /* weird ip address */ 518 bad_bridge_line_test("a.b.c.d"); 519 /* invalid fpr */ 520 bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349"); 521 /* no k=v in the end */ 522 bad_bridge_line_test("obfs2 2.2.2.2:1231 " 523 "4352e58420e68f5e40bf7c74faddccd9d1349413 what"); 524 /* no addrport */ 525 bad_bridge_line_test("asdw"); 526 /* huge k=v value that can't fit in SOCKS fields */ 527 bad_bridge_line_test( 528 "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 529 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 530 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 531 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 532 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 533 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 534 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 536 "aa=b"); 537 } 538 539 static void 540 test_config_parse_transport_options_line(void *arg) 541 { 542 smartlist_t *options_sl = NULL, *sl_tmp = NULL; 543 544 (void) arg; 545 546 { /* too small line */ 547 options_sl = get_options_from_transport_options_line("valley", NULL); 548 tt_ptr_op(options_sl, OP_EQ, NULL); 549 } 550 551 { /* no k=v values */ 552 options_sl = get_options_from_transport_options_line("hit it!", NULL); 553 tt_ptr_op(options_sl, OP_EQ, NULL); 554 } 555 556 { /* correct line, but wrong transport specified */ 557 options_sl = 558 get_options_from_transport_options_line("trebuchet k=v", "rook"); 559 tt_ptr_op(options_sl, OP_EQ, NULL); 560 } 561 562 { /* correct -- no transport specified */ 563 sl_tmp = smartlist_new(); 564 smartlist_add_asprintf(sl_tmp, "ladi=dadi"); 565 smartlist_add_asprintf(sl_tmp, "weliketo=party"); 566 567 options_sl = 568 get_options_from_transport_options_line("rook ladi=dadi weliketo=party", 569 NULL); 570 tt_assert(options_sl); 571 tt_assert(smartlist_strings_eq(options_sl, sl_tmp)); 572 573 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 574 smartlist_free(sl_tmp); 575 sl_tmp = NULL; 576 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s)); 577 smartlist_free(options_sl); 578 options_sl = NULL; 579 } 580 581 { /* correct -- correct transport specified */ 582 sl_tmp = smartlist_new(); 583 smartlist_add_asprintf(sl_tmp, "ladi=dadi"); 584 smartlist_add_asprintf(sl_tmp, "weliketo=party"); 585 586 options_sl = 587 get_options_from_transport_options_line("rook ladi=dadi weliketo=party", 588 "rook"); 589 tt_assert(options_sl); 590 tt_assert(smartlist_strings_eq(options_sl, sl_tmp)); 591 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 592 smartlist_free(sl_tmp); 593 sl_tmp = NULL; 594 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s)); 595 smartlist_free(options_sl); 596 options_sl = NULL; 597 } 598 599 done: 600 if (options_sl) { 601 SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s)); 602 smartlist_free(options_sl); 603 } 604 if (sl_tmp) { 605 SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s)); 606 smartlist_free(sl_tmp); 607 } 608 } 609 610 /* Mocks needed for the compute_max_mem_in_queues test */ 611 static int get_total_system_memory_mock(size_t *mem_out); 612 613 static size_t total_system_memory_output = 0; 614 static int total_system_memory_return = 0; 615 616 static int 617 get_total_system_memory_mock(size_t *mem_out) 618 { 619 if (! mem_out) 620 return -1; 621 622 *mem_out = total_system_memory_output; 623 return total_system_memory_return; 624 } 625 626 /* Mocks needed for the transport plugin line test */ 627 628 static void pt_kickstart_proxy_mock(const smartlist_t *transport_list, 629 char **proxy_argv, int is_server); 630 static int transport_add_from_config_mock(const tor_addr_t *addr, 631 uint16_t port, const char *name, 632 int socks_ver); 633 static int transport_is_needed_mock(const char *transport_name); 634 635 static int pt_kickstart_proxy_mock_call_count = 0; 636 static int transport_add_from_config_mock_call_count = 0; 637 static int transport_is_needed_mock_call_count = 0; 638 static int transport_is_needed_mock_return = 0; 639 640 static void 641 pt_kickstart_proxy_mock(const smartlist_t *transport_list, 642 char **proxy_argv, int is_server) 643 { 644 (void) transport_list; 645 (void) proxy_argv; 646 (void) is_server; 647 /* XXXX check that args are as expected. */ 648 649 ++pt_kickstart_proxy_mock_call_count; 650 651 free_execve_args(proxy_argv); 652 } 653 654 static int 655 transport_add_from_config_mock(const tor_addr_t *addr, 656 uint16_t port, const char *name, 657 int socks_ver) 658 { 659 (void) addr; 660 (void) port; 661 (void) name; 662 (void) socks_ver; 663 /* XXXX check that args are as expected. */ 664 665 ++transport_add_from_config_mock_call_count; 666 667 return 0; 668 } 669 670 static int 671 transport_is_needed_mock(const char *transport_name) 672 { 673 (void) transport_name; 674 /* XXXX check that arg is as expected. */ 675 676 ++transport_is_needed_mock_call_count; 677 678 return transport_is_needed_mock_return; 679 } 680 681 static void 682 test_config_parse_tcp_proxy_line(void *arg) 683 { 684 (void)arg; 685 686 int ret; 687 char *msg = NULL; 688 or_options_t *options = get_options_mutable(); 689 690 /* Bad TCPProxy line - too short. */ 691 ret = parse_tcp_proxy_line("haproxy", options, &msg); 692 /* Return error. */ 693 tt_int_op(ret, OP_EQ, -1); 694 /* Correct error message. */ 695 tt_str_op(msg, OP_EQ, "TCPProxy has no address/port. Please fix."); 696 /* Free error message. */ 697 tor_free(msg); 698 699 /* Bad TCPProxy line - unsupported protocol. */ 700 ret = parse_tcp_proxy_line("unsupported 95.216.163.36:443", options, &msg); 701 tt_int_op(ret, OP_EQ, -1); 702 tt_str_op(msg, OP_EQ, "TCPProxy protocol is not supported. Currently the " 703 "only supported protocol is 'haproxy'. Please fix."); 704 tor_free(msg); 705 706 /* Bad TCPProxy line - unparsable address/port. */ 707 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs); 708 ret = parse_tcp_proxy_line("haproxy bogus_address!/300", options, &msg); 709 tt_int_op(ret, OP_EQ, -1); 710 tt_str_op(msg, OP_EQ, "TCPProxy address/port failed to parse or resolve. " 711 "Please fix."); 712 tor_free(msg); 713 UNMOCK(tor_addr_lookup); 714 715 /* Good TCPProxy line - ipv4. */ 716 ret = parse_tcp_proxy_line("haproxy 95.216.163.36:443", options, &msg); 717 tt_int_op(ret, OP_EQ, 0); 718 tt_ptr_op(msg, OP_EQ, NULL); 719 tt_int_op(options->TCPProxyProtocol, OP_EQ, TCP_PROXY_PROTOCOL_HAPROXY); 720 /* Correct the address. */ 721 tt_assert(tor_addr_eq_ipv4h(&options->TCPProxyAddr, 0x5fd8a324)); 722 tt_int_op(options->TCPProxyPort, OP_EQ, 443); 723 tor_free(msg); 724 725 done: 726 UNMOCK(tor_addr_lookup); 727 } 728 729 /** 730 * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config 731 * options. 732 */ 733 734 static void 735 test_config_parse_transport_plugin_line(void *arg) 736 { 737 (void)arg; 738 739 or_options_t *options = get_options_mutable(); 740 int r, tmp; 741 int old_pt_kickstart_proxy_mock_call_count; 742 int old_transport_add_from_config_mock_call_count; 743 int old_transport_is_needed_mock_call_count; 744 745 /* Bad transport lines - too short */ 746 r = pt_parse_transport_line(options, "bad", 1, 0); 747 tt_int_op(r, OP_LT, 0); 748 r = pt_parse_transport_line(options, "bad", 1, 1); 749 tt_int_op(r, OP_LT, 0); 750 r = pt_parse_transport_line(options, "bad bad", 1, 0); 751 tt_int_op(r, OP_LT, 0); 752 r = pt_parse_transport_line(options, "bad bad", 1, 1); 753 tt_int_op(r, OP_LT, 0); 754 755 /* Test transport list parsing */ 756 r = pt_parse_transport_line(options, 757 "transport_1 exec /usr/bin/fake-transport", 1, 0); 758 tt_int_op(r, OP_EQ, 0); 759 r = pt_parse_transport_line(options, 760 "transport_1 exec /usr/bin/fake-transport", 1, 1); 761 tt_int_op(r, OP_EQ, 0); 762 r = pt_parse_transport_line(options, 763 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0); 764 tt_int_op(r, OP_EQ, 0); 765 r = pt_parse_transport_line(options, 766 "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1); 767 tt_int_op(r, OP_EQ, 0); 768 /* Bad transport identifiers */ 769 r = pt_parse_transport_line(options, 770 "transport_* exec /usr/bin/fake-transport", 1, 0); 771 tt_int_op(r, OP_LT, 0); 772 r = pt_parse_transport_line(options, 773 "transport_* exec /usr/bin/fake-transport", 1, 1); 774 tt_int_op(r, OP_LT, 0); 775 776 /* Check SOCKS cases for client transport */ 777 r = pt_parse_transport_line(options, 778 "transport_1 socks4 1.2.3.4:567", 1, 0); 779 tt_int_op(r, OP_EQ, 0); 780 r = pt_parse_transport_line(options, 781 "transport_1 socks5 1.2.3.4:567", 1, 0); 782 tt_int_op(r, OP_EQ, 0); 783 /* Proxy case for server transport */ 784 r = pt_parse_transport_line(options, 785 "transport_1 proxy 1.2.3.4:567", 1, 1); 786 tt_int_op(r, OP_EQ, 0); 787 /* Multiple-transport error exit */ 788 r = pt_parse_transport_line(options, 789 "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0); 790 tt_int_op(r, OP_LT, 0); 791 r = pt_parse_transport_line(options, 792 "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1); 793 tt_int_op(r, OP_LT, 0); 794 /* No port error exit */ 795 r = pt_parse_transport_line(options, 796 "transport_1 socks5 1.2.3.4", 1, 0); 797 tt_int_op(r, OP_LT, 0); 798 r = pt_parse_transport_line(options, 799 "transport_1 proxy 1.2.3.4", 1, 1); 800 tt_int_op(r, OP_LT, 0); 801 /* Unparsable address error exit */ 802 r = pt_parse_transport_line(options, 803 "transport_1 socks5 1.2.3:6x7", 1, 0); 804 tt_int_op(r, OP_LT, 0); 805 r = pt_parse_transport_line(options, 806 "transport_1 proxy 1.2.3:6x7", 1, 1); 807 tt_int_op(r, OP_LT, 0); 808 809 /* "Strange {Client|Server}TransportPlugin field" error exit */ 810 r = pt_parse_transport_line(options, 811 "transport_1 foo bar", 1, 0); 812 tt_int_op(r, OP_LT, 0); 813 r = pt_parse_transport_line(options, 814 "transport_1 foo bar", 1, 1); 815 tt_int_op(r, OP_LT, 0); 816 817 /* No sandbox mode error exit */ 818 tmp = options->Sandbox; 819 options->Sandbox = 1; 820 r = pt_parse_transport_line(options, 821 "transport_1 exec /usr/bin/fake-transport", 1, 0); 822 tt_int_op(r, OP_LT, 0); 823 r = pt_parse_transport_line(options, 824 "transport_1 exec /usr/bin/fake-transport", 1, 1); 825 tt_int_op(r, OP_LT, 0); 826 options->Sandbox = tmp; 827 828 /* 829 * These final test cases cover code paths that only activate without 830 * validate_only, so they need mocks in place. 831 */ 832 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock); 833 old_pt_kickstart_proxy_mock_call_count = 834 pt_kickstart_proxy_mock_call_count; 835 r = pt_parse_transport_line(options, 836 "transport_1 exec /usr/bin/fake-transport", 0, 1); 837 tt_int_op(r, OP_EQ, 0); 838 tt_assert(pt_kickstart_proxy_mock_call_count == 839 old_pt_kickstart_proxy_mock_call_count + 1); 840 UNMOCK(pt_kickstart_proxy); 841 842 /* This one hits a log line in the !validate_only case only */ 843 r = pt_parse_transport_line(options, 844 "transport_1 proxy 1.2.3.4:567", 0, 1); 845 tt_int_op(r, OP_EQ, 0); 846 847 /* Check mocked client transport cases */ 848 MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock); 849 MOCK(transport_add_from_config, transport_add_from_config_mock); 850 MOCK(transport_is_needed, transport_is_needed_mock); 851 852 /* Unnecessary transport case */ 853 transport_is_needed_mock_return = 0; 854 old_pt_kickstart_proxy_mock_call_count = 855 pt_kickstart_proxy_mock_call_count; 856 old_transport_add_from_config_mock_call_count = 857 transport_add_from_config_mock_call_count; 858 old_transport_is_needed_mock_call_count = 859 transport_is_needed_mock_call_count; 860 r = pt_parse_transport_line(options, 861 "transport_1 exec /usr/bin/fake-transport", 0, 0); 862 /* Should have succeeded */ 863 tt_int_op(r, OP_EQ, 0); 864 /* transport_is_needed() should have been called */ 865 tt_assert(transport_is_needed_mock_call_count == 866 old_transport_is_needed_mock_call_count + 1); 867 /* 868 * pt_kickstart_proxy() and transport_add_from_config() should 869 * not have been called. 870 */ 871 tt_assert(pt_kickstart_proxy_mock_call_count == 872 old_pt_kickstart_proxy_mock_call_count); 873 tt_assert(transport_add_from_config_mock_call_count == 874 old_transport_add_from_config_mock_call_count); 875 876 /* Necessary transport case */ 877 transport_is_needed_mock_return = 1; 878 old_pt_kickstart_proxy_mock_call_count = 879 pt_kickstart_proxy_mock_call_count; 880 old_transport_add_from_config_mock_call_count = 881 transport_add_from_config_mock_call_count; 882 old_transport_is_needed_mock_call_count = 883 transport_is_needed_mock_call_count; 884 r = pt_parse_transport_line(options, 885 "transport_1 exec /usr/bin/fake-transport", 0, 0); 886 /* Should have succeeded */ 887 tt_int_op(r, OP_EQ, 0); 888 /* 889 * transport_is_needed() and pt_kickstart_proxy() should have been 890 * called. 891 */ 892 tt_assert(pt_kickstart_proxy_mock_call_count == 893 old_pt_kickstart_proxy_mock_call_count + 1); 894 tt_assert(transport_is_needed_mock_call_count == 895 old_transport_is_needed_mock_call_count + 1); 896 /* transport_add_from_config() should not have been called. */ 897 tt_assert(transport_add_from_config_mock_call_count == 898 old_transport_add_from_config_mock_call_count); 899 900 /* proxy case */ 901 transport_is_needed_mock_return = 1; 902 old_pt_kickstart_proxy_mock_call_count = 903 pt_kickstart_proxy_mock_call_count; 904 old_transport_add_from_config_mock_call_count = 905 transport_add_from_config_mock_call_count; 906 old_transport_is_needed_mock_call_count = 907 transport_is_needed_mock_call_count; 908 r = pt_parse_transport_line(options, 909 "transport_1 socks5 1.2.3.4:567", 0, 0); 910 /* Should have succeeded */ 911 tt_int_op(r, OP_EQ, 0); 912 /* 913 * transport_is_needed() and transport_add_from_config() should have 914 * been called. 915 */ 916 tt_assert(transport_add_from_config_mock_call_count == 917 old_transport_add_from_config_mock_call_count + 1); 918 tt_assert(transport_is_needed_mock_call_count == 919 old_transport_is_needed_mock_call_count + 1); 920 /* pt_kickstart_proxy() should not have been called. */ 921 tt_assert(pt_kickstart_proxy_mock_call_count == 922 old_pt_kickstart_proxy_mock_call_count); 923 924 /* Done with mocked client transport cases */ 925 UNMOCK(transport_is_needed); 926 UNMOCK(transport_add_from_config); 927 UNMOCK(pt_kickstart_proxy); 928 929 done: 930 /* Make sure we undo all mocks */ 931 UNMOCK(pt_kickstart_proxy); 932 UNMOCK(transport_add_from_config); 933 UNMOCK(transport_is_needed); 934 935 return; 936 } 937 938 // Tests if an options with MyFamily fingerprints missing '$' normalises 939 // them correctly and also ensure it also works with multiple fingerprints 940 static void 941 test_config_fix_my_family(void *arg) 942 { 943 char *err = NULL; 944 config_line_t *family = tor_malloc_zero(sizeof(config_line_t)); 945 family->key = tor_strdup("MyFamily"); 946 family->value = tor_strdup("$1111111111111111111111111111111111111111, " 947 "1111111111111111111111111111111111111112, " 948 "$1111111111111111111111111111111111111113"); 949 950 config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t)); 951 family2->key = tor_strdup("MyFamily"); 952 family2->value = tor_strdup("1111111111111111111111111111111111111114"); 953 954 config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t)); 955 family3->key = tor_strdup("MyFamily"); 956 family3->value = tor_strdup("$1111111111111111111111111111111111111115"); 957 958 family->next = family2; 959 family2->next = family3; 960 family3->next = NULL; 961 962 or_options_t* options = options_new(); 963 (void) arg; 964 965 options_init(options); 966 options->MyFamily_lines = family; 967 968 options_validate(NULL, options, &err) ; 969 970 if (err != NULL) { 971 TT_FAIL(("options_validate failed: %s", err)); 972 } 973 974 const char *valid[] = { "$1111111111111111111111111111111111111111", 975 "$1111111111111111111111111111111111111112", 976 "$1111111111111111111111111111111111111113", 977 "$1111111111111111111111111111111111111114", 978 "$1111111111111111111111111111111111111115" }; 979 int ret_size = 0; 980 config_line_t *ret; 981 for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) { 982 tt_str_op(ret->value, OP_EQ, valid[ret_size]); 983 ret_size++; 984 } 985 tt_int_op(ret_size, OP_EQ, 5); 986 987 done: 988 tor_free(err); 989 or_options_free(options); 990 } 991 992 static int n_hostname_01010101 = 0; 993 static const char *ret_addr_lookup_01010101[2] = { 994 "1.1.1.1", "0101::0101", 995 }; 996 997 /** This mock function is meant to replace tor_addr_lookup(). 998 * It answers with 1.1.1.1 as IP address that resulted from lookup. 999 * This function increments <b>n_hostname_01010101</b> counter by one 1000 * every time it is called. 1001 */ 1002 static int 1003 tor_addr_lookup_01010101(const char *name, uint16_t family, tor_addr_t *addr) 1004 { 1005 n_hostname_01010101++; 1006 1007 if (family == AF_INET) { 1008 if (name && addr) { 1009 int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[0]); 1010 tt_int_op(ret, OP_EQ, family); 1011 } 1012 } else if (family == AF_INET6) { 1013 if (name && addr) { 1014 int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[1]); 1015 tt_int_op(ret, OP_EQ, family); 1016 } 1017 } 1018 done: 1019 return 0; 1020 } 1021 1022 static int n_hostname_localhost = 0; 1023 1024 /** This mock function is meant to replace tor_addr_lookup(). 1025 * It answers with 127.0.0.1 as IP address that resulted from lookup. 1026 * This function increments <b>n_hostname_localhost</b> counter by one 1027 * every time it is called. 1028 */ 1029 static int 1030 tor_addr_lookup_localhost(const char *name, uint16_t family, tor_addr_t *addr) 1031 { 1032 n_hostname_localhost++; 1033 1034 if (family == AF_INET) { 1035 if (name && addr) { 1036 tor_addr_from_ipv4h(addr, 0x7f000001); 1037 } 1038 } else if (family == AF_INET6) { 1039 if (name && addr) { 1040 int ret = tor_addr_parse(addr, "::1"); 1041 tt_int_op(ret, OP_EQ, AF_INET6); 1042 } 1043 } 1044 done: 1045 return 0; 1046 } 1047 1048 static int n_hostname_failure = 0; 1049 1050 /** This mock function is meant to replace tor_addr_lookup(). 1051 * It pretends to fail by returning -1 to caller. Also, this function 1052 * increments <b>n_hostname_failure</b> every time it is called. 1053 */ 1054 static int 1055 tor_addr_lookup_failure(const char *name, uint16_t family, tor_addr_t *addr) 1056 { 1057 (void)name; 1058 (void)family; 1059 (void)addr; 1060 1061 n_hostname_failure++; 1062 1063 return -1; 1064 } 1065 1066 /** Mock function for tor_addr_lookup(). 1067 * 1068 * Depending on the given hostname and family, resolve either to IPv4 or IPv6. 1069 * 1070 * If the requested hostname family is not the same as the family provided, an 1071 * error is returned. 1072 * 1073 * Possible hostnames: 1074 * - www.torproject.org.v4 for IPv4 -> 1.1.1.1 1075 * - www.torproject.org.v6 for IPv6 -> [0101::0101] 1076 */ 1077 static int 1078 tor_addr_lookup_mixed(const char *name, uint16_t family, tor_addr_t *addr) 1079 { 1080 tt_assert(addr); 1081 tt_assert(name); 1082 1083 if (!strcmp(name, "www.torproject.org.v4")) { 1084 if (family == AF_INET) { 1085 tor_addr_from_ipv4h(addr, 0x01010101); 1086 return 0; 1087 } 1088 /* Resolving AF_INET but the asked family is different. Failure. */ 1089 return -1; 1090 } 1091 1092 if (!strcmp(name, "www.torproject.org.v6")) { 1093 if (family == AF_INET6) { 1094 int ret = tor_addr_parse(addr, "0101::0101"); 1095 tt_int_op(ret, OP_EQ, AF_INET6); 1096 return 0; 1097 } 1098 /* Resolving AF_INET6 but the asked family is not. Failure. */ 1099 return -1; 1100 } 1101 1102 done: 1103 return 0; 1104 } 1105 1106 static int n_gethostname_replacement = 0; 1107 1108 /** This mock function is meant to replace tor_gethostname(). It 1109 * responds with string "onionrouter!" as hostname. This function 1110 * increments <b>n_gethostname_replacement</b> by one every time 1111 * it is called. 1112 */ 1113 static int 1114 tor_gethostname_replacement(char *name, size_t namelen) 1115 { 1116 n_gethostname_replacement++; 1117 1118 if (name && namelen) { 1119 strlcpy(name,"onionrouter!",namelen); 1120 } 1121 1122 return 0; 1123 } 1124 1125 static int n_gethostname_localhost = 0; 1126 1127 /** This mock function is meant to replace tor_gethostname(). It 1128 * responds with string "127.0.0.1" as hostname. This function 1129 * increments <b>n_gethostname_localhost</b> by one every time 1130 * it is called. 1131 */ 1132 static int 1133 tor_gethostname_localhost(char *name, size_t namelen) 1134 { 1135 n_gethostname_localhost++; 1136 1137 if (name && namelen) { 1138 strlcpy(name,"127.0.0.1",namelen); 1139 } 1140 1141 return 0; 1142 } 1143 1144 static int n_gethostname_failure = 0; 1145 1146 /** This mock function is meant to replace tor_gethostname. 1147 * It pretends to fail by returning -1. This function increments 1148 * <b>n_gethostname_failure</b> by one every time it is called. 1149 */ 1150 static int 1151 tor_gethostname_failure(char *name, size_t namelen) 1152 { 1153 (void)name; 1154 (void)namelen; 1155 n_gethostname_failure++; 1156 1157 return -1; 1158 } 1159 1160 static int n_get_interface_address6 = 0; 1161 static sa_family_t last_address6_family; 1162 static const char *ret_get_interface_address6_08080808[2] = { 1163 "8.8.8.8", "0808::0808", 1164 }; 1165 1166 /** This mock function is meant to replace get_interface_address(). 1167 * It answers with address 8.8.8.8. This function increments 1168 * <b>n_get_interface_address</b> by one every time it is called. 1169 */ 1170 static int 1171 get_interface_address6_08080808(int severity, sa_family_t family, 1172 tor_addr_t *addr) 1173 { 1174 (void)severity; 1175 1176 n_get_interface_address6++; 1177 1178 if (family == AF_INET) { 1179 if (addr) { 1180 int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[0]); 1181 tt_int_op(ret, OP_EQ, AF_INET); 1182 } 1183 } else if (family == AF_INET6) { 1184 if (addr) { 1185 int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[1]); 1186 tt_int_op(ret, OP_EQ, AF_INET6); 1187 } 1188 } 1189 done: 1190 return 0; 1191 } 1192 1193 /** This mock function is meant to replace get_interface_address6(). 1194 * It answers with IP address 9.9.9.9 iff both of the following are true: 1195 * - <b>family</b> is AF_INET 1196 * - <b>addr</b> pointer is not NULL. 1197 * This function increments <b>n_get_interface_address6</b> by one every 1198 * time it is called. 1199 */ 1200 #if 0 1201 static int 1202 get_interface_address6_replacement(int severity, sa_family_t family, 1203 tor_addr_t *addr) 1204 { 1205 (void)severity; 1206 1207 last_address6_family = family; 1208 n_get_interface_address6++; 1209 1210 if ((family != AF_INET) || !addr) { 1211 return -1; 1212 } 1213 1214 tor_addr_from_ipv4h(addr,0x09090909); 1215 1216 return 0; 1217 } 1218 #endif /* 0 */ 1219 1220 static int n_get_interface_address6_failure = 0; 1221 1222 /** 1223 * This mock function is meant to replace get_interface_addres6(). 1224 * It will pretend to fail by return -1. 1225 * <b>n_get_interface_address6_failure</b> is incremented by one 1226 * every time this function is called and <b>last_address6_family</b> 1227 * is assigned the value of <b>family</b> argument. 1228 */ 1229 static int 1230 get_interface_address6_failure(int severity, sa_family_t family, 1231 tor_addr_t *addr) 1232 { 1233 (void)severity; 1234 (void)addr; 1235 n_get_interface_address6_failure++; 1236 last_address6_family = family; 1237 1238 return -1; 1239 } 1240 1241 /** Helper macro: to validate the returned value from find_my_address() so we 1242 * don't copy those all the time. */ 1243 #undef VALIDATE_FOUND_ADDRESS 1244 #define VALIDATE_FOUND_ADDRESS(ret, method, hostname) \ 1245 do { \ 1246 tt_int_op(retval, OP_EQ, ret); \ 1247 tt_int_op(method, OP_EQ, method_used); \ 1248 if (hostname == NULL) tt_assert(!hostname_out); \ 1249 else tt_str_op(hostname_out, OP_EQ, hostname); \ 1250 if (ret == true) { \ 1251 tt_assert(tor_addr_eq(&resolved_addr, &test_addr)); \ 1252 } \ 1253 } while (0) 1254 1255 /** Helper macro: Cleanup the address and variables used after a 1256 * find_my_address() call. */ 1257 #undef CLEANUP_FOUND_ADDRESS 1258 #define CLEANUP_FOUND_ADDRESS \ 1259 do { \ 1260 config_free_lines(options->Address); \ 1261 config_free_lines(options->ORPort_lines); \ 1262 options->AddressDisableIPv6 = 0; \ 1263 options->ORPort_set = 0; \ 1264 tor_free(options->DirAuthorities); \ 1265 tor_free(hostname_out); \ 1266 tor_addr_make_unspec(&resolved_addr); \ 1267 tor_addr_make_unspec(&test_addr); \ 1268 } while (0) 1269 1270 /** Test both IPv4 and IPv6 coexisting together in the configuration. */ 1271 static void 1272 test_config_find_my_address_mixed(void *arg) 1273 { 1274 or_options_t *options; 1275 tor_addr_t resolved_addr, test_addr; 1276 resolved_addr_method_t method_used; 1277 char *hostname_out = NULL; 1278 bool retval; 1279 1280 (void)arg; 1281 1282 options = options_new(); 1283 1284 options_init(options); 1285 1286 /* 1287 * CASE 1: Only IPv6 address. Accepted. 1288 */ 1289 config_line_append(&options->Address, "Address", 1290 "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19"); 1291 tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19"); 1292 1293 /* IPv6 address should be found and considered configured. */ 1294 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr, 1295 &method_used, &hostname_out); 1296 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1297 1298 CLEANUP_FOUND_ADDRESS; 1299 1300 /* 1301 * Case 2: IPv4 _and_ IPv6 given. Accepted. 1302 */ 1303 config_line_append(&options->Address, "Address", 1304 "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19"); 1305 config_line_append(&options->Address, "Address", "1.1.1.1"); 1306 tor_addr_parse(&test_addr, "1.1.1.1"); 1307 1308 /* IPv4 address should be found and considered configured. */ 1309 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr, 1310 &method_used, &hostname_out); 1311 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1312 1313 /* IPv6 address should be found and considered configured. */ 1314 tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19"); 1315 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr, 1316 &method_used, &hostname_out); 1317 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1318 1319 CLEANUP_FOUND_ADDRESS; 1320 1321 /* 1322 * Case 3: Two hostnames, IPv4 and IPv6. 1323 */ 1324 config_line_append(&options->Address, "Address", "www.torproject.org.v4"); 1325 config_line_append(&options->Address, "Address", "www.torproject.org.v6"); 1326 1327 /* Looks at specific hostname to learn which address family to use. */ 1328 MOCK(tor_addr_lookup, tor_addr_lookup_mixed); 1329 1330 /* IPv4 address should be found and considered resolved. */ 1331 tor_addr_parse(&test_addr, "1.1.1.1"); 1332 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr, 1333 &method_used, &hostname_out); 1334 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, 1335 "www.torproject.org.v4"); 1336 tor_free(hostname_out); 1337 1338 /* IPv6 address should be found and considered resolved. */ 1339 tor_addr_parse(&test_addr, "0101::0101"); 1340 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr, 1341 &method_used, &hostname_out); 1342 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, 1343 "www.torproject.org.v6"); 1344 1345 CLEANUP_FOUND_ADDRESS; 1346 UNMOCK(tor_addr_lookup); 1347 1348 /* 1349 * Case 4: IPv4 address and a hostname resolving to IPV6. 1350 */ 1351 config_line_append(&options->Address, "Address", "1.1.1.1"); 1352 config_line_append(&options->Address, "Address", "www.torproject.org.v6"); 1353 1354 /* Looks at specific hostname to learn which address family to use. */ 1355 MOCK(tor_addr_lookup, tor_addr_lookup_mixed); 1356 1357 /* IPv4 address should be found and configured. */ 1358 tor_addr_parse(&test_addr, "1.1.1.1"); 1359 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr, 1360 &method_used, &hostname_out); 1361 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1362 1363 /* IPv6 address should be found and considered resolved. */ 1364 tor_addr_parse(&test_addr, "0101::0101"); 1365 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr, 1366 &method_used, &hostname_out); 1367 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, 1368 "www.torproject.org.v6"); 1369 1370 CLEANUP_FOUND_ADDRESS; 1371 UNMOCK(tor_addr_lookup); 1372 1373 /* 1374 * Case 5: Hostname resolving to IPv4 and an IPv6 address. 1375 */ 1376 config_line_append(&options->Address, "Address", "0101::0101"); 1377 config_line_append(&options->Address, "Address", "www.torproject.org.v4"); 1378 1379 /* Looks at specific hostname to learn which address family to use. */ 1380 MOCK(tor_addr_lookup, tor_addr_lookup_mixed); 1381 1382 /* IPv4 address should be found and resolved. */ 1383 tor_addr_parse(&test_addr, "1.1.1.1"); 1384 retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr, 1385 &method_used, &hostname_out); 1386 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, 1387 "www.torproject.org.v4"); 1388 tor_free(hostname_out); 1389 1390 /* IPv6 address should be found and considered resolved. */ 1391 tor_addr_parse(&test_addr, "0101::0101"); 1392 retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr, 1393 &method_used, &hostname_out); 1394 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1395 CLEANUP_FOUND_ADDRESS; 1396 1397 UNMOCK(tor_addr_lookup); 1398 1399 done: 1400 config_free_lines(options->Address); 1401 or_options_free(options); 1402 tor_free(hostname_out); 1403 1404 UNMOCK(tor_addr_lookup); 1405 } 1406 1407 /** Parameters for the find_my_address() test. We test both AF_INET and 1408 * AF_INET6 but we have one interface to do so thus we run the same exact unit 1409 * tests for both without copying them. */ 1410 typedef struct find_my_address_params_t { 1411 /* Index where the mock function results are located. For instance, 1412 * tor_addr_lookup_01010101() will have its returned value depending on the 1413 * family in ret_addr_lookup_01010101[]. 1414 * 1415 * Values that can be found: 1416 * AF_INET : index 0. 1417 * AF_INET6: index 1. 1418 */ 1419 int idx; 1420 int family; 1421 const char *public_ip; 1422 const char *internal_ip; 1423 const char *orport; 1424 } find_my_address_params_t; 1425 1426 static find_my_address_params_t addr_param_v4 = { 1427 .idx = 0, 1428 .family = AF_INET, 1429 .public_ip = "128.52.128.105", 1430 .internal_ip = "127.0.0.1", 1431 }; 1432 1433 static find_my_address_params_t addr_param_v6 = { 1434 .idx = 1, 1435 .family = AF_INET6, 1436 .public_ip = "[4242::4242]", 1437 .internal_ip = "[::1]", 1438 }; 1439 1440 static void 1441 test_config_find_my_address(void *arg) 1442 { 1443 or_options_t *options; 1444 tor_addr_t resolved_addr, test_addr; 1445 resolved_addr_method_t method_used; 1446 char *hostname_out = NULL; 1447 bool retval; 1448 int prev_n_hostname_01010101; 1449 int prev_n_hostname_failure; 1450 int prev_n_hostname_localhost; 1451 int prev_n_gethostname_replacement; 1452 int prev_n_gethostname_failure; 1453 int prev_n_gethostname_localhost; 1454 int prev_n_get_interface_address6; 1455 int prev_n_get_interface_address6_failure; 1456 1457 const find_my_address_params_t *p = arg; 1458 1459 options = options_new(); 1460 options_init(options); 1461 options->PublishServerDescriptor_ = V3_DIRINFO; 1462 1463 /* 1464 * Case 0: 1465 * AddressDisableIPv6 is set. 1466 * 1467 * Only run this if we are in the IPv6 test. 1468 */ 1469 if (p->family == AF_INET6) { 1470 options->AddressDisableIPv6 = 1; 1471 /* Set a valid IPv6. However, the discovery should still fail. */ 1472 config_line_append(&options->Address, "Address", p->public_ip); 1473 tor_addr_parse(&test_addr, p->public_ip); 1474 1475 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1476 &method_used, &hostname_out); 1477 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1478 CLEANUP_FOUND_ADDRESS; 1479 } 1480 1481 /* 1482 * Case 1: 1483 * 1. Address is a valid address. 1484 * 1485 * Expected to succeed. 1486 */ 1487 config_line_append(&options->Address, "Address", p->public_ip); 1488 tor_addr_parse(&test_addr, p->public_ip); 1489 1490 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1491 &method_used, &hostname_out); 1492 1493 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1494 CLEANUP_FOUND_ADDRESS; 1495 1496 /* 1497 * Case 2: Address is a resolvable address. Expected to succeed. 1498 */ 1499 MOCK(tor_addr_lookup, tor_addr_lookup_01010101); 1500 1501 config_line_append(&options->Address, "Address", "www.torproject.org"); 1502 tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]); 1503 1504 prev_n_hostname_01010101 = n_hostname_01010101; 1505 1506 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1507 &method_used, &hostname_out); 1508 1509 tt_int_op(n_hostname_01010101, OP_EQ, ++prev_n_hostname_01010101); 1510 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, "www.torproject.org"); 1511 CLEANUP_FOUND_ADDRESS; 1512 1513 UNMOCK(tor_addr_lookup); 1514 1515 /* 1516 * Case 3: Address is a local addressi (internal). Expected to fail. 1517 */ 1518 config_line_append(&options->Address, "Address", p->internal_ip); 1519 1520 setup_full_capture_of_logs(LOG_NOTICE); 1521 1522 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1523 &method_used, &hostname_out); 1524 1525 expect_log_msg_containing("is a private IP address. Tor relays that " 1526 "use the default DirAuthorities must have " 1527 "public IP addresses."); 1528 teardown_capture_of_logs(); 1529 1530 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1531 CLEANUP_FOUND_ADDRESS; 1532 1533 /* 1534 * Case 4: Address is a local address but custom authorities. Expected to 1535 * succeed. 1536 */ 1537 config_line_append(&options->Address, "Address", p->internal_ip); 1538 options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t)); 1539 tor_addr_parse(&test_addr, p->internal_ip); 1540 1541 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1542 &method_used, &hostname_out); 1543 1544 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1545 CLEANUP_FOUND_ADDRESS; 1546 1547 /* 1548 * Case 5: Multiple address in Address. Expected to fail. 1549 */ 1550 config_line_append(&options->Address, "Address", p->public_ip); 1551 config_line_append(&options->Address, "Address", p->public_ip); 1552 1553 setup_full_capture_of_logs(LOG_NOTICE); 1554 1555 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1556 &method_used, &hostname_out); 1557 1558 expect_log_msg_containing("Found 2 Address statement of address family"); 1559 teardown_capture_of_logs(); 1560 1561 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1562 CLEANUP_FOUND_ADDRESS; 1563 1564 /* 1565 * Case 8: 1566 * 1. Address is NULL 1567 * 2. Interface address is a valid address. 1568 * 1569 * Expected to succeed. 1570 */ 1571 options->Address = NULL; 1572 tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]); 1573 1574 MOCK(get_interface_address6, get_interface_address6_08080808); 1575 1576 prev_n_get_interface_address6 = n_get_interface_address6; 1577 1578 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1579 &method_used, &hostname_out); 1580 1581 tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6); 1582 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL); 1583 CLEANUP_FOUND_ADDRESS; 1584 1585 UNMOCK(get_interface_address6); 1586 1587 /* 1588 * Case 9: 1589 * 1. Address is NULL 1590 * 2. Interface address fails to be found. 1591 * 3. Local hostname resolves to a valid address. 1592 * 1593 * Expected to succeed. 1594 */ 1595 options->Address = NULL; 1596 tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]); 1597 1598 MOCK(get_interface_address6, get_interface_address6_failure); 1599 MOCK(tor_gethostname, tor_gethostname_replacement); 1600 MOCK(tor_addr_lookup, tor_addr_lookup_01010101); 1601 1602 prev_n_get_interface_address6_failure = n_get_interface_address6_failure; 1603 prev_n_hostname_01010101 = n_hostname_01010101; 1604 prev_n_gethostname_replacement = n_gethostname_replacement; 1605 1606 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1607 &method_used, &hostname_out); 1608 1609 tt_int_op(n_get_interface_address6_failure, OP_EQ, 1610 ++prev_n_get_interface_address6_failure); 1611 tt_int_op(n_hostname_01010101, OP_EQ, 1612 ++prev_n_hostname_01010101); 1613 tt_int_op(n_gethostname_replacement, OP_EQ, 1614 ++prev_n_gethostname_replacement); 1615 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_GETHOSTNAME, "onionrouter!"); 1616 CLEANUP_FOUND_ADDRESS; 1617 1618 UNMOCK(get_interface_address6); 1619 UNMOCK(tor_gethostname); 1620 UNMOCK(tor_addr_lookup); 1621 1622 /* 1623 * Case 10: 1624 * 1. Address is NULL 1625 * 2. Interface address fails to be found. 1626 * 3. Local hostname resolves to an internal address. 1627 * 1628 * Expected to fail. 1629 */ 1630 options->Address = NULL; 1631 1632 MOCK(get_interface_address6, get_interface_address6_failure); 1633 MOCK(tor_gethostname, tor_gethostname_localhost); 1634 MOCK(tor_addr_lookup, tor_addr_lookup_localhost); 1635 1636 prev_n_get_interface_address6_failure = n_get_interface_address6_failure; 1637 prev_n_hostname_localhost = n_hostname_localhost; 1638 prev_n_gethostname_localhost = n_gethostname_localhost; 1639 1640 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1641 &method_used, &hostname_out); 1642 1643 tt_int_op(n_get_interface_address6_failure, OP_EQ, 1644 ++prev_n_get_interface_address6_failure); 1645 tt_int_op(n_hostname_localhost, OP_EQ, 1646 ++prev_n_hostname_localhost); 1647 tt_int_op(n_gethostname_localhost, OP_EQ, 1648 ++prev_n_gethostname_localhost); 1649 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1650 CLEANUP_FOUND_ADDRESS; 1651 1652 UNMOCK(get_interface_address6); 1653 UNMOCK(tor_gethostname); 1654 UNMOCK(tor_addr_lookup); 1655 1656 /* 1657 * Case 11: 1658 * 1. Address is NULL 1659 * 2. Interface address fails to be found. 1660 * 3. Local hostname fails to be found. 1661 * 1662 * Expected to fail. 1663 */ 1664 options->Address = NULL; 1665 1666 MOCK(get_interface_address6, get_interface_address6_failure); 1667 MOCK(tor_gethostname, tor_gethostname_failure); 1668 1669 prev_n_get_interface_address6_failure = n_get_interface_address6_failure; 1670 prev_n_gethostname_failure = n_gethostname_failure; 1671 1672 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1673 &method_used, &hostname_out); 1674 1675 tt_int_op(n_get_interface_address6_failure, OP_EQ, 1676 ++prev_n_get_interface_address6_failure); 1677 tt_int_op(n_gethostname_failure, OP_EQ, 1678 ++prev_n_gethostname_failure); 1679 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1680 CLEANUP_FOUND_ADDRESS; 1681 1682 UNMOCK(get_interface_address6); 1683 UNMOCK(tor_gethostname); 1684 1685 /* 1686 * Case 12: 1687 * 1. Address is NULL 1688 * 2. Interface address fails to be found. 1689 * 3. Local hostname can't be resolved. 1690 * 1691 * Expected to fail. 1692 */ 1693 options->Address = NULL; 1694 1695 MOCK(get_interface_address6, get_interface_address6_failure); 1696 MOCK(tor_gethostname, tor_gethostname_replacement); 1697 MOCK(tor_addr_lookup, tor_addr_lookup_failure); 1698 1699 prev_n_get_interface_address6_failure = n_get_interface_address6_failure; 1700 prev_n_gethostname_replacement = n_gethostname_replacement; 1701 prev_n_hostname_failure = n_hostname_failure; 1702 1703 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1704 &method_used, &hostname_out); 1705 1706 tt_int_op(n_get_interface_address6_failure, OP_EQ, 1707 ++prev_n_get_interface_address6_failure); 1708 tt_int_op(n_gethostname_replacement, OP_EQ, 1709 ++prev_n_gethostname_replacement); 1710 tt_int_op(n_hostname_failure, OP_EQ, 1711 ++prev_n_hostname_failure); 1712 VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL); 1713 CLEANUP_FOUND_ADDRESS; 1714 1715 /* 1716 * Case 13: 1717 * 1. Address is NULL. 1718 * 2. ORPort has a valid public address. 1719 */ 1720 { 1721 char *msg = NULL; 1722 int n, w, ret; 1723 char *orport_line = NULL; 1724 1725 options->Address = NULL; 1726 tor_asprintf(&orport_line, "%s:9001", p->public_ip); 1727 config_line_append(&options->ORPort_lines, "ORPort", orport_line); 1728 tor_free(orport_line); 1729 1730 if (p->family == AF_INET6) { 1731 /* XXX: Tor does _not_ allow an IPv6 only ORPort thus we need to add a 1732 * bogus IPv4 at the moment. */ 1733 config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001"); 1734 } 1735 1736 ret = parse_ports(options, 0, &msg, &n, &w); 1737 tt_int_op(ret, OP_EQ, 0); 1738 tor_addr_parse(&test_addr, p->public_ip); 1739 } 1740 1741 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1742 &method_used, &hostname_out); 1743 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED_ORPORT, NULL); 1744 CLEANUP_FOUND_ADDRESS; 1745 1746 /* 1747 * Case 14: 1748 * 1. Address is NULL. 1749 * 2. ORPort has an internal address thus fails. 1750 * 3. Interface as a valid address. 1751 */ 1752 { 1753 char *msg = NULL; 1754 int n, w, ret; 1755 char *orport_line = NULL; 1756 1757 options->Address = NULL; 1758 tor_asprintf(&orport_line, "%s:9001", p->internal_ip); 1759 config_line_append(&options->ORPort_lines, "ORPort", orport_line); 1760 tor_free(orport_line); 1761 1762 if (p->family == AF_INET6) { 1763 /* XXX: Tor does _not_ allow an IPv6 only ORPort thus we need to add a 1764 * bogus IPv4 at the moment. */ 1765 config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001"); 1766 } 1767 1768 ret = parse_ports(options, 0, &msg, &n, &w); 1769 tt_int_op(ret, OP_EQ, 0); 1770 } 1771 tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]); 1772 1773 MOCK(get_interface_address6, get_interface_address6_08080808); 1774 1775 prev_n_get_interface_address6 = n_get_interface_address6; 1776 1777 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1778 &method_used, &hostname_out); 1779 1780 tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6); 1781 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL); 1782 CLEANUP_FOUND_ADDRESS; 1783 1784 /* 1785 * Case 15: Address is a local address (internal) but we unset 1786 * PublishServerDescriptor_ so we are allowed to hold it. 1787 */ 1788 options->PublishServerDescriptor_ = NO_DIRINFO; 1789 if (p->family == AF_INET) { 1790 options->AssumeReachable = 1; 1791 } 1792 config_line_append(&options->Address, "Address", p->internal_ip); 1793 1794 tor_addr_parse(&test_addr, p->internal_ip); 1795 retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr, 1796 &method_used, &hostname_out); 1797 VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL); 1798 CLEANUP_FOUND_ADDRESS; 1799 1800 UNMOCK(get_interface_address6); 1801 UNMOCK(tor_gethostname); 1802 UNMOCK(tor_addr_lookup); 1803 1804 done: 1805 or_options_free(options); 1806 1807 UNMOCK(tor_gethostname); 1808 UNMOCK(tor_addr_lookup); 1809 UNMOCK(get_interface_address6); 1810 } 1811 1812 static void 1813 test_config_adding_trusted_dir_server(void *arg) 1814 { 1815 (void)arg; 1816 1817 const char digest[DIGEST_LEN] = ""; 1818 dir_server_t *ds = NULL; 1819 tor_addr_port_t ipv6; 1820 int rv = -1; 1821 1822 clear_dir_servers(); 1823 routerlist_free_all(); 1824 1825 /* create a trusted ds without an IPv6 address and port */ 1826 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest, 1827 NULL, V3_DIRINFO, 1.0); 1828 tt_assert(ds); 1829 dir_server_add(ds); 1830 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1); 1831 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1); 1832 1833 /* create a trusted ds with an IPv6 address and port */ 1834 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1); 1835 tt_int_op(rv, OP_EQ, 0); 1836 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest, 1837 NULL, V3_DIRINFO, 1.0); 1838 tt_assert(ds); 1839 dir_server_add(ds); 1840 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2); 1841 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2); 1842 1843 done: 1844 clear_dir_servers(); 1845 routerlist_free_all(); 1846 } 1847 1848 static void 1849 test_config_adding_fallback_dir_server(void *arg) 1850 { 1851 (void)arg; 1852 1853 const char digest[DIGEST_LEN] = ""; 1854 dir_server_t *ds = NULL; 1855 tor_addr_t ipv4; 1856 tor_addr_port_t ipv6; 1857 int rv = -1; 1858 1859 clear_dir_servers(); 1860 routerlist_free_all(); 1861 1862 rv = tor_addr_parse(&ipv4, "127.0.0.1"); 1863 tt_int_op(rv, OP_EQ, AF_INET); 1864 1865 /* create a trusted ds without an IPv6 address and port */ 1866 ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0); 1867 tt_assert(ds); 1868 dir_server_add(ds); 1869 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1); 1870 1871 /* create a trusted ds with an IPv6 address and port */ 1872 rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1); 1873 tt_int_op(rv, OP_EQ, 0); 1874 ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0); 1875 tt_assert(ds); 1876 dir_server_add(ds); 1877 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2); 1878 1879 done: 1880 clear_dir_servers(); 1881 routerlist_free_all(); 1882 } 1883 1884 /* No secrets here: 1885 * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"` 1886 * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"` 1887 * with added spaces 1888 */ 1889 #define TEST_DIR_AUTH_LINE_START \ 1890 "foobar orport=12345 " \ 1891 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 " 1892 #define TEST_DIR_AUTH_LINE_END \ 1893 "1.2.3.4:54321 " \ 1894 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17" 1895 #define TEST_DIR_AUTH_IPV6_FLAG \ 1896 "ipv6=[feed::beef]:9 " 1897 1898 static void 1899 test_config_parsing_trusted_dir_server(void *arg) 1900 { 1901 (void)arg; 1902 int rv = -1; 1903 1904 /* parse a trusted dir server without an IPv6 address and port */ 1905 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START 1906 TEST_DIR_AUTH_LINE_END, 1907 V3_DIRINFO, 1); 1908 tt_int_op(rv, OP_EQ, 0); 1909 1910 /* parse a trusted dir server with an IPv6 address and port */ 1911 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START 1912 TEST_DIR_AUTH_IPV6_FLAG 1913 TEST_DIR_AUTH_LINE_END, 1914 V3_DIRINFO, 1); 1915 tt_int_op(rv, OP_EQ, 0); 1916 1917 /* Since we are only validating, there is no cleanup. */ 1918 done: 1919 ; 1920 } 1921 1922 #undef TEST_DIR_AUTH_LINE_START 1923 #undef TEST_DIR_AUTH_LINE_END 1924 #undef TEST_DIR_AUTH_IPV6_FLAG 1925 1926 #define TEST_DIR_AUTH_LINE_START \ 1927 "foobar orport=12345 " \ 1928 "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 " 1929 #define TEST_DIR_AUTH_LINE_END_BAD_IP \ 1930 "0.256.3.4:54321 " \ 1931 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17" 1932 #define TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR \ 1933 "torproject.org:54321 " \ 1934 "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17" 1935 1936 static void 1937 test_config_parsing_invalid_dir_address(void *arg) 1938 { 1939 (void)arg; 1940 int rv; 1941 1942 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START 1943 TEST_DIR_AUTH_LINE_END_BAD_IP, 1944 V3_DIRINFO, 1); 1945 tt_int_op(rv, OP_EQ, -1); 1946 1947 rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START 1948 TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR, 1949 V3_DIRINFO, 1); 1950 tt_int_op(rv, OP_EQ, -1); 1951 1952 done: 1953 return; 1954 } 1955 1956 #undef TEST_DIR_AUTH_LINE_START 1957 #undef TEST_DIR_AUTH_LINE_END_BAD_IP 1958 #undef TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR 1959 1960 /* No secrets here: 1961 * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1` 1962 */ 1963 #define TEST_DIR_FALLBACK_LINE \ 1964 "1.2.3.4:54321 orport=12345 " \ 1965 "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 " 1966 #define TEST_DIR_FALLBACK_IPV6_FLAG \ 1967 "ipv6=[2015:c0de::deed]:9" 1968 1969 static void 1970 test_config_parsing_fallback_dir_server(void *arg) 1971 { 1972 (void)arg; 1973 int rv = -1; 1974 1975 /* parse a trusted dir server without an IPv6 address and port */ 1976 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1); 1977 tt_int_op(rv, OP_EQ, 0); 1978 1979 /* parse a trusted dir server with an IPv6 address and port */ 1980 rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE 1981 TEST_DIR_FALLBACK_IPV6_FLAG, 1982 1); 1983 tt_int_op(rv, OP_EQ, 0); 1984 1985 /* Since we are only validating, there is no cleanup. */ 1986 done: 1987 ; 1988 } 1989 1990 #undef TEST_DIR_FALLBACK_LINE 1991 #undef TEST_DIR_FALLBACK_IPV6_FLAG 1992 1993 static void 1994 test_config_adding_default_trusted_dir_servers(void *arg) 1995 { 1996 (void)arg; 1997 1998 clear_dir_servers(); 1999 routerlist_free_all(); 2000 2001 /* Assume we only have one bridge authority */ 2002 add_default_trusted_dir_authorities(BRIDGE_DIRINFO); 2003 tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1); 2004 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1); 2005 2006 /* Assume we have nine V3 authorities */ 2007 add_default_trusted_dir_authorities(V3_DIRINFO); 2008 tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9); 2009 tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10); 2010 2011 done: 2012 clear_dir_servers(); 2013 routerlist_free_all(); 2014 } 2015 2016 static int n_add_default_fallback_dir_servers_known_default = 0; 2017 2018 /** 2019 * This mock function is meant to replace add_default_fallback_dir_servers(). 2020 * It will parse and add one known default fallback dir server, 2021 * which has a dir_port of 99. 2022 * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by 2023 * one every time this function is called. 2024 */ 2025 static void 2026 add_default_fallback_dir_servers_known_default(void) 2027 { 2028 int i; 2029 const char *fallback[] = { 2030 "127.0.0.1:60099 orport=9009 " 2031 "id=0923456789012345678901234567890123456789", 2032 NULL 2033 }; 2034 for (i=0; fallback[i]; i++) { 2035 if (parse_dir_fallback_line(fallback[i], 0)<0) { 2036 log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s", 2037 fallback[i]); 2038 } 2039 } 2040 n_add_default_fallback_dir_servers_known_default++; 2041 } 2042 2043 /* Helper for test_config_adding_dir_servers(), which should be 2044 * refactored: clear the fields in the options which the options object 2045 * does not really own. */ 2046 static void 2047 ads_clear_helper(or_options_t *options) 2048 { 2049 options->DirAuthorities = NULL; 2050 options->AlternateBridgeAuthority = NULL; 2051 options->AlternateDirAuthority = NULL; 2052 options->FallbackDir = NULL; 2053 } 2054 2055 /* Test all the different combinations of adding dir servers */ 2056 static void 2057 test_config_adding_dir_servers(void *arg) 2058 { 2059 (void)arg; 2060 2061 /* allocate options */ 2062 or_options_t *options = options_new(); 2063 2064 /* Allocate and populate configuration lines: 2065 * 2066 * Use the same format as the hard-coded directories in 2067 * add_default_trusted_dir_authorities(). 2068 * Zeroing the structure has the same effect as initialising to: 2069 * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0}; 2070 */ 2071 config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t)); 2072 test_dir_authority->key = tor_strdup("DirAuthority"); 2073 test_dir_authority->value = tor_strdup( 2074 "D0 orport=9000 " 2075 "v3ident=0023456789012345678901234567890123456789 " 2076 "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789" 2077 ); 2078 2079 config_line_t *test_alt_bridge_authority = tor_malloc_zero( 2080 sizeof(config_line_t)); 2081 test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority"); 2082 test_alt_bridge_authority->value = tor_strdup( 2083 "B1 orport=9001 bridge " 2084 "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789" 2085 ); 2086 2087 config_line_t *test_alt_dir_authority = tor_malloc_zero( 2088 sizeof(config_line_t)); 2089 test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority"); 2090 test_alt_dir_authority->value = tor_strdup( 2091 "A2 orport=9002 " 2092 "v3ident=0223456789012345678901234567890123456789 " 2093 "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789" 2094 ); 2095 2096 /* Use the format specified in the manual page */ 2097 config_line_t *test_fallback_directory = tor_malloc_zero( 2098 sizeof(config_line_t)); 2099 test_fallback_directory->key = tor_strdup("FallbackDir"); 2100 test_fallback_directory->value = tor_strdup( 2101 "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789" 2102 ); 2103 2104 /* We need to know if add_default_fallback_dir_servers is called, 2105 * whatever the size of the list in fallback_dirs.inc, 2106 * so we use a version of add_default_fallback_dir_servers that adds 2107 * one known default fallback directory. */ 2108 MOCK(add_default_fallback_dir_servers, 2109 add_default_fallback_dir_servers_known_default); 2110 2111 /* There are 16 different cases, covering each combination of set/NULL for: 2112 * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority & 2113 * FallbackDir. (We always set UseDefaultFallbackDirs to 1.) 2114 * But validate_dir_servers() ensures that: 2115 * "You cannot set both DirAuthority and Alternate*Authority." 2116 * This reduces the number of cases to 10. 2117 * 2118 * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL 2119 * So 1001 or case 9 is: 2120 * DirAuthorities set, 2121 * AlternateBridgeAuthority NULL, 2122 * AlternateDirAuthority NULL 2123 * FallbackDir set 2124 * The valid cases are cases 0-9 counting using this method, as every case 2125 * greater than or equal to 10 = 1010 is invalid. 2126 * 2127 * 1. Outcome: Use Set Directory Authorities 2128 * - No Default Authorities 2129 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir 2130 * if they are set 2131 * Cases expected to yield this outcome: 2132 * 8 & 9 (the 2 valid cases where DirAuthorities is set) 2133 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and 2134 * AlternateBridgeAuthority and AlternateDirAuthority are both set) 2135 * 2136 * 2. Outcome: Use Set Bridge Authority 2137 * - Use Default Non-Bridge Directory Authorities 2138 * - Use FallbackDir if it is set, otherwise use default FallbackDir 2139 * Cases expected to yield this outcome: 2140 * 4 & 5 (the 2 cases where DirAuthorities is NULL, 2141 * AlternateBridgeAuthority is set, and 2142 * AlternateDirAuthority is NULL) 2143 * 2144 * 3. Outcome: Use Set Alternate Directory Authority 2145 * - Use Default Bridge Authorities 2146 * - Use FallbackDir if it is set, otherwise No Default Fallback Directories 2147 * Cases expected to yield this outcome: 2148 * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority 2149 * are both NULL, but AlternateDirAuthority is set) 2150 * 2151 * 4. Outcome: Use Set Custom Fallback Directory 2152 * - Use Default Bridge & Directory Authorities 2153 * Cases expected to yield this outcome: 2154 * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority 2155 * are all NULL, but FallbackDir is set) 2156 * 2157 * 5. Outcome: Use All Defaults 2158 * - Use Default Bridge & Directory Authorities, and 2159 * Default Fallback Directories 2160 * Cases expected to yield this outcome: 2161 * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority 2162 * and FallbackDir are all NULL) 2163 */ 2164 2165 /* 2166 * Find out how many default Bridge, Non-Bridge and Fallback Directories 2167 * are hard-coded into this build. 2168 * This code makes some assumptions about the implementation. 2169 * If they are wrong, one or more of cases 0-5 could fail. 2170 */ 2171 int n_default_alt_bridge_authority = 0; 2172 int n_default_alt_dir_authority = 0; 2173 int n_default_fallback_dir = 0; 2174 #define n_default_authorities ((n_default_alt_bridge_authority) \ 2175 + (n_default_alt_dir_authority)) 2176 2177 /* Pre-Count Number of Authorities of Each Type 2178 * Use 0000: No Directory Authorities or Fallback Directories Set 2179 */ 2180 { 2181 /* clear fallback dirs counter */ 2182 n_add_default_fallback_dir_servers_known_default = 0; 2183 2184 /* clear options*/ 2185 ads_clear_helper(options); 2186 or_options_free(options); 2187 options = options_new(); 2188 2189 /* clear any previous dir servers: 2190 consider_adding_dir_servers() should do this anyway */ 2191 clear_dir_servers(); 2192 2193 /* assign options: 0000 */ 2194 options->DirAuthorities = NULL; 2195 options->AlternateBridgeAuthority = NULL; 2196 options->AlternateDirAuthority = NULL; 2197 options->FallbackDir = NULL; 2198 options->UseDefaultFallbackDirs = 1; 2199 2200 /* parse options - ensure we always update by passing NULL old_options */ 2201 consider_adding_dir_servers(options, NULL); 2202 2203 /* check outcome */ 2204 2205 /* we must have added the default fallback dirs */ 2206 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1); 2207 2208 /* we have more fallbacks than just the authorities */ 2209 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 2210 2211 { 2212 /* fallback_dir_servers */ 2213 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2214 2215 /* Count Bridge Authorities */ 2216 SMARTLIST_FOREACH(fallback_servers, 2217 dir_server_t *, 2218 ds, 2219 /* increment the found counter if it's a bridge auth */ 2220 n_default_alt_bridge_authority += 2221 ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ? 2222 1 : 0) 2223 ); 2224 /* If we have no default bridge authority, something has gone wrong */ 2225 tt_int_op(n_default_alt_bridge_authority, OP_GE, 1); 2226 2227 /* Count v3 Authorities */ 2228 SMARTLIST_FOREACH(fallback_servers, 2229 dir_server_t *, 2230 ds, 2231 /* increment found counter if it's a v3 auth */ 2232 n_default_alt_dir_authority += 2233 ((ds->is_authority && (ds->type & V3_DIRINFO)) ? 2234 1 : 0) 2235 ); 2236 /* If we have no default authorities, something has gone really wrong */ 2237 tt_int_op(n_default_alt_dir_authority, OP_GE, 1); 2238 2239 /* Calculate Fallback Directory Count */ 2240 n_default_fallback_dir = (smartlist_len(fallback_servers) - 2241 n_default_alt_bridge_authority - 2242 n_default_alt_dir_authority); 2243 /* If we have a negative count, something has gone really wrong, 2244 * or some authorities aren't being added as fallback directories. 2245 * (networkstatus_consensus_can_use_extra_fallbacks depends on all 2246 * authorities being fallback directories.) */ 2247 tt_int_op(n_default_fallback_dir, OP_GE, 0); 2248 } 2249 } 2250 2251 /* 2252 * 1. Outcome: Use Set Directory Authorities 2253 * - No Default Authorities 2254 * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir 2255 * if they are set 2256 * Cases expected to yield this outcome: 2257 * 8 & 9 (the 2 valid cases where DirAuthorities is set) 2258 * 6 & 7 (the 2 cases where DirAuthorities is NULL, and 2259 * AlternateBridgeAuthority and AlternateDirAuthority are both set) 2260 */ 2261 2262 /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set, 2263 AlternateDirAuthority Not Set, FallbackDir Set */ 2264 { 2265 /* clear fallback dirs counter */ 2266 n_add_default_fallback_dir_servers_known_default = 0; 2267 2268 /* clear options*/ 2269 ads_clear_helper(options); 2270 or_options_free(options); 2271 options = options_new(); 2272 2273 /* clear any previous dir servers: 2274 consider_adding_dir_servers() should do this anyway */ 2275 clear_dir_servers(); 2276 2277 /* assign options: 1001 */ 2278 options->DirAuthorities = test_dir_authority; 2279 options->AlternateBridgeAuthority = NULL; 2280 options->AlternateDirAuthority = NULL; 2281 options->FallbackDir = test_fallback_directory; 2282 options->UseDefaultFallbackDirs = 1; 2283 2284 /* parse options - ensure we always update by passing NULL old_options */ 2285 consider_adding_dir_servers(options, NULL); 2286 2287 /* check outcome */ 2288 2289 /* we must not have added the default fallback dirs */ 2290 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 2291 2292 /* we have more fallbacks than just the authorities */ 2293 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 2294 2295 { 2296 /* trusted_dir_servers */ 2297 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 2298 /* D0, (No B1), (No A2) */ 2299 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1); 2300 2301 /* DirAuthority - D0 - dir_port: 60090 */ 2302 int found_D0 = 0; 2303 SMARTLIST_FOREACH(dir_servers, 2304 dir_server_t *, 2305 ds, 2306 /* increment the found counter if dir_port matches */ 2307 found_D0 += 2308 (ds->ipv4_dirport == 60090 ? 2309 1 : 0) 2310 ); 2311 tt_int_op(found_D0, OP_EQ, 1); 2312 2313 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 2314 int found_B1 = 0; 2315 SMARTLIST_FOREACH(dir_servers, 2316 dir_server_t *, 2317 ds, 2318 /* increment the found counter if dir_port matches */ 2319 found_B1 += 2320 (ds->ipv4_dirport == 60091 ? 2321 1 : 0) 2322 ); 2323 tt_int_op(found_B1, OP_EQ, 0); 2324 2325 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2326 int found_A2 = 0; 2327 SMARTLIST_FOREACH(dir_servers, 2328 dir_server_t *, 2329 ds, 2330 /* increment the found counter if dir_port matches */ 2331 found_A2 += 2332 (ds->ipv4_dirport == 60092 ? 2333 1 : 0) 2334 ); 2335 tt_int_op(found_A2, OP_EQ, 0); 2336 } 2337 2338 { 2339 /* fallback_dir_servers */ 2340 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2341 /* D0, (No B1), (No A2), Custom Fallback */ 2342 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2); 2343 2344 /* DirAuthority - D0 - dir_port: 60090 */ 2345 int found_D0 = 0; 2346 SMARTLIST_FOREACH(fallback_servers, 2347 dir_server_t *, 2348 ds, 2349 /* increment the found counter if dir_port matches */ 2350 found_D0 += 2351 (ds->ipv4_dirport == 60090 ? 2352 1 : 0) 2353 ); 2354 tt_int_op(found_D0, OP_EQ, 1); 2355 2356 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 2357 int found_B1 = 0; 2358 SMARTLIST_FOREACH(fallback_servers, 2359 dir_server_t *, 2360 ds, 2361 /* increment the found counter if dir_port matches */ 2362 found_B1 += 2363 (ds->ipv4_dirport == 60091 ? 2364 1 : 0) 2365 ); 2366 tt_int_op(found_B1, OP_EQ, 0); 2367 2368 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2369 int found_A2 = 0; 2370 SMARTLIST_FOREACH(fallback_servers, 2371 dir_server_t *, 2372 ds, 2373 /* increment the found counter if dir_port matches */ 2374 found_A2 += 2375 (ds->ipv4_dirport == 60092 ? 2376 1 : 0) 2377 ); 2378 tt_int_op(found_A2, OP_EQ, 0); 2379 2380 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 2381 int found_non_default_fallback = 0; 2382 SMARTLIST_FOREACH(fallback_servers, 2383 dir_server_t *, 2384 ds, 2385 /* increment the found counter if dir_port matches */ 2386 found_non_default_fallback += 2387 (ds->ipv4_dirport == 60093 ? 2388 1 : 0) 2389 ); 2390 tt_int_op(found_non_default_fallback, OP_EQ, 1); 2391 2392 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 2393 int found_default_fallback = 0; 2394 SMARTLIST_FOREACH(fallback_servers, 2395 dir_server_t *, 2396 ds, 2397 /* increment the found counter if dir_port matches */ 2398 found_default_fallback += 2399 (ds->ipv4_dirport == 60099 ? 2400 1 : 0) 2401 ); 2402 tt_int_op(found_default_fallback, OP_EQ, 0); 2403 } 2404 } 2405 2406 /* Case 8: 1000 - DirAuthorities Set, Others Not Set */ 2407 { 2408 /* clear fallback dirs counter */ 2409 n_add_default_fallback_dir_servers_known_default = 0; 2410 2411 /* clear options*/ 2412 ads_clear_helper(options); 2413 or_options_free(options); 2414 options = options_new(); 2415 2416 /* clear any previous dir servers: 2417 consider_adding_dir_servers() should do this anyway */ 2418 clear_dir_servers(); 2419 2420 /* assign options: 1000 */ 2421 options->DirAuthorities = test_dir_authority; 2422 options->AlternateBridgeAuthority = NULL; 2423 options->AlternateDirAuthority = NULL; 2424 options->FallbackDir = NULL; 2425 options->UseDefaultFallbackDirs = 1; 2426 2427 /* parse options - ensure we always update by passing NULL old_options */ 2428 consider_adding_dir_servers(options, NULL); 2429 2430 /* check outcome */ 2431 2432 /* we must not have added the default fallback dirs */ 2433 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 2434 2435 /* we just have the authorities */ 2436 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0); 2437 2438 { 2439 /* trusted_dir_servers */ 2440 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 2441 /* D0, (No B1), (No A2) */ 2442 tt_int_op(smartlist_len(dir_servers), OP_EQ, 1); 2443 2444 /* DirAuthority - D0 - dir_port: 60090 */ 2445 int found_D0 = 0; 2446 SMARTLIST_FOREACH(dir_servers, 2447 dir_server_t *, 2448 ds, 2449 /* increment the found counter if dir_port matches */ 2450 found_D0 += 2451 (ds->ipv4_dirport == 60090 ? 2452 1 : 0) 2453 ); 2454 tt_int_op(found_D0, OP_EQ, 1); 2455 2456 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 2457 int found_B1 = 0; 2458 SMARTLIST_FOREACH(dir_servers, 2459 dir_server_t *, 2460 ds, 2461 /* increment the found counter if dir_port matches */ 2462 found_B1 += 2463 (ds->ipv4_dirport == 60091 ? 2464 1 : 0) 2465 ); 2466 tt_int_op(found_B1, OP_EQ, 0); 2467 2468 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2469 int found_A2 = 0; 2470 SMARTLIST_FOREACH(dir_servers, 2471 dir_server_t *, 2472 ds, 2473 /* increment the found counter if dir_port matches */ 2474 found_A2 += 2475 (ds->ipv4_dirport == 60092 ? 2476 1 : 0) 2477 ); 2478 tt_int_op(found_A2, OP_EQ, 0); 2479 } 2480 2481 { 2482 /* fallback_dir_servers */ 2483 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2484 /* D0, (No B1), (No A2), (No Fallback) */ 2485 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1); 2486 2487 /* DirAuthority - D0 - dir_port: 60090 */ 2488 int found_D0 = 0; 2489 SMARTLIST_FOREACH(fallback_servers, 2490 dir_server_t *, 2491 ds, 2492 /* increment the found counter if dir_port matches */ 2493 found_D0 += 2494 (ds->ipv4_dirport == 60090 ? 2495 1 : 0) 2496 ); 2497 tt_int_op(found_D0, OP_EQ, 1); 2498 2499 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 2500 int found_B1 = 0; 2501 SMARTLIST_FOREACH(fallback_servers, 2502 dir_server_t *, 2503 ds, 2504 /* increment the found counter if dir_port matches */ 2505 found_B1 += 2506 (ds->ipv4_dirport == 60091 ? 2507 1 : 0) 2508 ); 2509 tt_int_op(found_B1, OP_EQ, 0); 2510 2511 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2512 int found_A2 = 0; 2513 SMARTLIST_FOREACH(fallback_servers, 2514 dir_server_t *, 2515 ds, 2516 /* increment the found counter if dir_port matches */ 2517 found_A2 += 2518 (ds->ipv4_dirport == 60092 ? 2519 1 : 0) 2520 ); 2521 tt_int_op(found_A2, OP_EQ, 0); 2522 2523 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */ 2524 int found_non_default_fallback = 0; 2525 SMARTLIST_FOREACH(fallback_servers, 2526 dir_server_t *, 2527 ds, 2528 /* increment the found counter if dir_port matches */ 2529 found_non_default_fallback += 2530 (ds->ipv4_dirport == 60093 ? 2531 1 : 0) 2532 ); 2533 tt_int_op(found_non_default_fallback, OP_EQ, 0); 2534 2535 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 2536 int found_default_fallback = 0; 2537 SMARTLIST_FOREACH(fallback_servers, 2538 dir_server_t *, 2539 ds, 2540 /* increment the found counter if dir_port matches */ 2541 found_default_fallback += 2542 (ds->ipv4_dirport == 60099 ? 2543 1 : 0) 2544 ); 2545 tt_int_op(found_default_fallback, OP_EQ, 0); 2546 } 2547 } 2548 2549 /* Case 7: 0111 - DirAuthorities Not Set, Others Set */ 2550 { 2551 /* clear fallback dirs counter */ 2552 n_add_default_fallback_dir_servers_known_default = 0; 2553 2554 /* clear options*/ 2555 ads_clear_helper(options); 2556 or_options_free(options); 2557 options = options_new(); 2558 2559 /* clear any previous dir servers: 2560 consider_adding_dir_servers() should do this anyway */ 2561 clear_dir_servers(); 2562 2563 /* assign options: 0111 */ 2564 options->DirAuthorities = NULL; 2565 options->AlternateBridgeAuthority = test_alt_bridge_authority; 2566 options->AlternateDirAuthority = test_alt_dir_authority; 2567 options->FallbackDir = test_fallback_directory; 2568 options->UseDefaultFallbackDirs = 1; 2569 2570 /* parse options - ensure we always update by passing NULL old_options */ 2571 consider_adding_dir_servers(options, NULL); 2572 2573 /* check outcome */ 2574 2575 /* we must not have added the default fallback dirs */ 2576 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 2577 2578 /* we have more fallbacks than just the authorities */ 2579 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 2580 2581 { 2582 /* trusted_dir_servers */ 2583 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 2584 /* (No D0), B1, A2 */ 2585 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2); 2586 2587 /* (No DirAuthority) - D0 - dir_port: 60090 */ 2588 int found_D0 = 0; 2589 SMARTLIST_FOREACH(dir_servers, 2590 dir_server_t *, 2591 ds, 2592 /* increment the found counter if dir_port matches */ 2593 found_D0 += 2594 (ds->ipv4_dirport == 60090 ? 2595 1 : 0) 2596 ); 2597 tt_int_op(found_D0, OP_EQ, 0); 2598 2599 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2600 int found_B1 = 0; 2601 SMARTLIST_FOREACH(dir_servers, 2602 dir_server_t *, 2603 ds, 2604 /* increment the found counter if dir_port matches */ 2605 found_B1 += 2606 (ds->ipv4_dirport == 60091 ? 2607 1 : 0) 2608 ); 2609 tt_int_op(found_B1, OP_EQ, 1); 2610 2611 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 2612 int found_A2 = 0; 2613 SMARTLIST_FOREACH(dir_servers, 2614 dir_server_t *, 2615 ds, 2616 /* increment the found counter if dir_port matches */ 2617 found_A2 += 2618 (ds->ipv4_dirport == 60092 ? 2619 1 : 0) 2620 ); 2621 tt_int_op(found_A2, OP_EQ, 1); 2622 } 2623 2624 { 2625 /* fallback_dir_servers */ 2626 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2627 /* (No D0), B1, A2, Custom Fallback */ 2628 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3); 2629 2630 /* (No DirAuthority) - D0 - dir_port: 60090 */ 2631 int found_D0 = 0; 2632 SMARTLIST_FOREACH(fallback_servers, 2633 dir_server_t *, 2634 ds, 2635 /* increment the found counter if dir_port matches */ 2636 found_D0 += 2637 (ds->ipv4_dirport == 60090 ? 2638 1 : 0) 2639 ); 2640 tt_int_op(found_D0, OP_EQ, 0); 2641 2642 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2643 int found_B1 = 0; 2644 SMARTLIST_FOREACH(fallback_servers, 2645 dir_server_t *, 2646 ds, 2647 /* increment the found counter if dir_port matches */ 2648 found_B1 += 2649 (ds->ipv4_dirport == 60091 ? 2650 1 : 0) 2651 ); 2652 tt_int_op(found_B1, OP_EQ, 1); 2653 2654 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 2655 int found_A2 = 0; 2656 SMARTLIST_FOREACH(fallback_servers, 2657 dir_server_t *, 2658 ds, 2659 /* increment the found counter if dir_port matches */ 2660 found_A2 += 2661 (ds->ipv4_dirport == 60092 ? 2662 1 : 0) 2663 ); 2664 tt_int_op(found_A2, OP_EQ, 1); 2665 2666 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 2667 int found_non_default_fallback = 0; 2668 SMARTLIST_FOREACH(fallback_servers, 2669 dir_server_t *, 2670 ds, 2671 /* increment the found counter if dir_port matches */ 2672 found_non_default_fallback += 2673 (ds->ipv4_dirport == 60093 ? 2674 1 : 0) 2675 ); 2676 tt_int_op(found_non_default_fallback, OP_EQ, 1); 2677 2678 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 2679 int found_default_fallback = 0; 2680 SMARTLIST_FOREACH(fallback_servers, 2681 dir_server_t *, 2682 ds, 2683 /* increment the found counter if dir_port matches */ 2684 found_default_fallback += 2685 (ds->ipv4_dirport == 60099 ? 2686 1 : 0) 2687 ); 2688 tt_int_op(found_default_fallback, OP_EQ, 0); 2689 } 2690 } 2691 2692 /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority & 2693 AlternateDirAuthority Set, FallbackDir Not Set */ 2694 { 2695 /* clear fallback dirs counter */ 2696 n_add_default_fallback_dir_servers_known_default = 0; 2697 2698 /* clear options*/ 2699 ads_clear_helper(options); 2700 or_options_free(options); 2701 options = options_new(); 2702 2703 /* clear any previous dir servers: 2704 consider_adding_dir_servers() should do this anyway */ 2705 clear_dir_servers(); 2706 2707 /* assign options: 0110 */ 2708 options->DirAuthorities = NULL; 2709 options->AlternateBridgeAuthority = test_alt_bridge_authority; 2710 options->AlternateDirAuthority = test_alt_dir_authority; 2711 options->FallbackDir = NULL; 2712 options->UseDefaultFallbackDirs = 1; 2713 2714 /* parse options - ensure we always update by passing NULL old_options */ 2715 consider_adding_dir_servers(options, NULL); 2716 2717 /* check outcome */ 2718 2719 /* we must not have added the default fallback dirs */ 2720 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 2721 2722 /* we have more fallbacks than just the authorities */ 2723 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0); 2724 2725 { 2726 /* trusted_dir_servers */ 2727 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 2728 /* (No D0), B1, A2 */ 2729 tt_int_op(smartlist_len(dir_servers), OP_EQ, 2); 2730 2731 /* (No DirAuthority) - D0 - dir_port: 60090 */ 2732 int found_D0 = 0; 2733 SMARTLIST_FOREACH(dir_servers, 2734 dir_server_t *, 2735 ds, 2736 /* increment the found counter if dir_port matches */ 2737 found_D0 += 2738 (ds->ipv4_dirport == 60090 ? 2739 1 : 0) 2740 ); 2741 tt_int_op(found_D0, OP_EQ, 0); 2742 2743 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2744 int found_B1 = 0; 2745 SMARTLIST_FOREACH(dir_servers, 2746 dir_server_t *, 2747 ds, 2748 /* increment the found counter if dir_port matches */ 2749 found_B1 += 2750 (ds->ipv4_dirport == 60091 ? 2751 1 : 0) 2752 ); 2753 tt_int_op(found_B1, OP_EQ, 1); 2754 2755 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 2756 int found_A2 = 0; 2757 SMARTLIST_FOREACH(dir_servers, 2758 dir_server_t *, 2759 ds, 2760 /* increment the found counter if dir_port matches */ 2761 found_A2 += 2762 (ds->ipv4_dirport == 60092 ? 2763 1 : 0) 2764 ); 2765 tt_int_op(found_A2, OP_EQ, 1); 2766 } 2767 2768 { 2769 /* fallback_dir_servers */ 2770 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2771 /* (No D0), B1, A2, (No Fallback) */ 2772 tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2); 2773 2774 /* (No DirAuthority) - D0 - dir_port: 60090 */ 2775 int found_D0 = 0; 2776 SMARTLIST_FOREACH(fallback_servers, 2777 dir_server_t *, 2778 ds, 2779 /* increment the found counter if dir_port matches */ 2780 found_D0 += 2781 (ds->ipv4_dirport == 60090 ? 2782 1 : 0) 2783 ); 2784 tt_int_op(found_D0, OP_EQ, 0); 2785 2786 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2787 int found_B1 = 0; 2788 SMARTLIST_FOREACH(fallback_servers, 2789 dir_server_t *, 2790 ds, 2791 /* increment the found counter if dir_port matches */ 2792 found_B1 += 2793 (ds->ipv4_dirport == 60091 ? 2794 1 : 0) 2795 ); 2796 tt_int_op(found_B1, OP_EQ, 1); 2797 2798 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 2799 int found_A2 = 0; 2800 SMARTLIST_FOREACH(fallback_servers, 2801 dir_server_t *, 2802 ds, 2803 /* increment the found counter if dir_port matches */ 2804 found_A2 += 2805 (ds->ipv4_dirport == 60092 ? 2806 1 : 0) 2807 ); 2808 tt_int_op(found_A2, OP_EQ, 1); 2809 2810 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */ 2811 int found_non_default_fallback = 0; 2812 SMARTLIST_FOREACH(fallback_servers, 2813 dir_server_t *, 2814 ds, 2815 /* increment the found counter if dir_port matches */ 2816 found_non_default_fallback += 2817 (ds->ipv4_dirport == 60093 ? 2818 1 : 0) 2819 ); 2820 tt_int_op(found_non_default_fallback, OP_EQ, 0); 2821 2822 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 2823 int found_default_fallback = 0; 2824 SMARTLIST_FOREACH(fallback_servers, 2825 dir_server_t *, 2826 ds, 2827 /* increment the found counter if dir_port matches */ 2828 found_default_fallback += 2829 (ds->ipv4_dirport == 60099 ? 2830 1 : 0) 2831 ); 2832 tt_int_op(found_default_fallback, OP_EQ, 0); 2833 } 2834 } 2835 2836 /* 2837 2. Outcome: Use Set Bridge Authority 2838 - Use Default Non-Bridge Directory Authorities 2839 - Use FallbackDir if it is set, otherwise use default FallbackDir 2840 Cases expected to yield this outcome: 2841 4 & 5 (the 2 cases where DirAuthorities is NULL, 2842 AlternateBridgeAuthority is set, and 2843 AlternateDirAuthority is NULL) 2844 */ 2845 2846 /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set, 2847 AlternateDirAuthority Not Set, FallbackDir Set */ 2848 { 2849 /* clear fallback dirs counter */ 2850 n_add_default_fallback_dir_servers_known_default = 0; 2851 2852 /* clear options*/ 2853 ads_clear_helper(options); 2854 or_options_free(options); 2855 options = options_new(); 2856 2857 /* clear any previous dir servers: 2858 consider_adding_dir_servers() should do this anyway */ 2859 clear_dir_servers(); 2860 2861 /* assign options: 0101 */ 2862 options->DirAuthorities = NULL; 2863 options->AlternateBridgeAuthority = test_alt_bridge_authority; 2864 options->AlternateDirAuthority = NULL; 2865 options->FallbackDir = test_fallback_directory; 2866 options->UseDefaultFallbackDirs = 1; 2867 2868 /* parse options - ensure we always update by passing NULL old_options */ 2869 consider_adding_dir_servers(options, NULL); 2870 2871 /* check outcome */ 2872 2873 /* we must not have added the default fallback dirs */ 2874 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 2875 2876 /* we have more fallbacks than just the authorities */ 2877 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 2878 2879 { 2880 /* trusted_dir_servers */ 2881 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 2882 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */ 2883 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority); 2884 2885 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 2886 int found_D0 = 0; 2887 SMARTLIST_FOREACH(dir_servers, 2888 dir_server_t *, 2889 ds, 2890 /* increment the found counter if dir_port matches */ 2891 found_D0 += 2892 (ds->ipv4_dirport == 60090 ? 2893 1 : 0) 2894 ); 2895 tt_int_op(found_D0, OP_EQ, 0); 2896 2897 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2898 int found_B1 = 0; 2899 SMARTLIST_FOREACH(dir_servers, 2900 dir_server_t *, 2901 ds, 2902 /* increment the found counter if dir_port matches */ 2903 found_B1 += 2904 (ds->ipv4_dirport == 60091 ? 2905 1 : 0) 2906 ); 2907 tt_int_op(found_B1, OP_EQ, 1); 2908 2909 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2910 int found_A2 = 0; 2911 SMARTLIST_FOREACH(dir_servers, 2912 dir_server_t *, 2913 ds, 2914 /* increment the found counter if dir_port matches */ 2915 found_A2 += 2916 (ds->ipv4_dirport == 60092 ? 2917 1 : 0) 2918 ); 2919 tt_int_op(found_A2, OP_EQ, 0); 2920 2921 /* There's no easy way of checking that we have included all the 2922 * default v3 non-Bridge directory authorities, so let's assume that 2923 * if the total count above is correct, we have the right ones. 2924 */ 2925 } 2926 2927 { 2928 /* fallback_dir_servers */ 2929 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 2930 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities, 2931 * Custom Fallback */ 2932 tt_assert(smartlist_len(fallback_servers) == 2933 2 + n_default_alt_dir_authority); 2934 2935 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 2936 int found_D0 = 0; 2937 SMARTLIST_FOREACH(fallback_servers, 2938 dir_server_t *, 2939 ds, 2940 /* increment the found counter if dir_port matches */ 2941 found_D0 += 2942 (ds->ipv4_dirport == 60090 ? 2943 1 : 0) 2944 ); 2945 tt_int_op(found_D0, OP_EQ, 0); 2946 2947 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 2948 int found_B1 = 0; 2949 SMARTLIST_FOREACH(fallback_servers, 2950 dir_server_t *, 2951 ds, 2952 /* increment the found counter if dir_port matches */ 2953 found_B1 += 2954 (ds->ipv4_dirport == 60091 ? 2955 1 : 0) 2956 ); 2957 tt_int_op(found_B1, OP_EQ, 1); 2958 2959 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 2960 int found_A2 = 0; 2961 SMARTLIST_FOREACH(fallback_servers, 2962 dir_server_t *, 2963 ds, 2964 /* increment the found counter if dir_port matches */ 2965 found_A2 += 2966 (ds->ipv4_dirport == 60092 ? 2967 1 : 0) 2968 ); 2969 tt_int_op(found_A2, OP_EQ, 0); 2970 2971 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 2972 int found_non_default_fallback = 0; 2973 SMARTLIST_FOREACH(fallback_servers, 2974 dir_server_t *, 2975 ds, 2976 /* increment the found counter if dir_port matches */ 2977 found_non_default_fallback += 2978 (ds->ipv4_dirport == 60093 ? 2979 1 : 0) 2980 ); 2981 tt_int_op(found_non_default_fallback, OP_EQ, 1); 2982 2983 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 2984 int found_default_fallback = 0; 2985 SMARTLIST_FOREACH(fallback_servers, 2986 dir_server_t *, 2987 ds, 2988 /* increment the found counter if dir_port matches */ 2989 found_default_fallback += 2990 (ds->ipv4_dirport == 60099 ? 2991 1 : 0) 2992 ); 2993 tt_int_op(found_default_fallback, OP_EQ, 0); 2994 2995 /* There's no easy way of checking that we have included all the 2996 * default v3 non-Bridge directory authorities, so let's assume that 2997 * if the total count above is correct, we have the right ones. 2998 */ 2999 } 3000 } 3001 3002 /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set, 3003 AlternateDirAuthority & FallbackDir Not Set */ 3004 { 3005 /* clear fallback dirs counter */ 3006 n_add_default_fallback_dir_servers_known_default = 0; 3007 3008 /* clear options*/ 3009 ads_clear_helper(options); 3010 or_options_free(options); 3011 options = options_new(); 3012 3013 /* clear any previous dir servers: 3014 consider_adding_dir_servers() should do this anyway */ 3015 clear_dir_servers(); 3016 3017 /* assign options: 0100 */ 3018 options->DirAuthorities = NULL; 3019 options->AlternateBridgeAuthority = test_alt_bridge_authority; 3020 options->AlternateDirAuthority = NULL; 3021 options->FallbackDir = NULL; 3022 options->UseDefaultFallbackDirs = 1; 3023 3024 /* parse options - ensure we always update by passing NULL old_options */ 3025 consider_adding_dir_servers(options, NULL); 3026 3027 /* check outcome */ 3028 3029 /* we must have added the default fallback dirs */ 3030 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1); 3031 3032 /* we have more fallbacks than just the authorities */ 3033 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 3034 3035 { 3036 /* trusted_dir_servers */ 3037 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 3038 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */ 3039 tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority); 3040 3041 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3042 int found_D0 = 0; 3043 SMARTLIST_FOREACH(dir_servers, 3044 dir_server_t *, 3045 ds, 3046 /* increment the found counter if dir_port matches */ 3047 found_D0 += 3048 (ds->ipv4_dirport == 60090 ? 3049 1 : 0) 3050 ); 3051 tt_int_op(found_D0, OP_EQ, 0); 3052 3053 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 3054 int found_B1 = 0; 3055 SMARTLIST_FOREACH(dir_servers, 3056 dir_server_t *, 3057 ds, 3058 /* increment the found counter if dir_port matches */ 3059 found_B1 += 3060 (ds->ipv4_dirport == 60091 ? 3061 1 : 0) 3062 ); 3063 tt_int_op(found_B1, OP_EQ, 1); 3064 3065 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3066 int found_A2 = 0; 3067 SMARTLIST_FOREACH(dir_servers, 3068 dir_server_t *, 3069 ds, 3070 /* increment the found counter if dir_port matches */ 3071 found_A2 += 3072 (ds->ipv4_dirport == 60092 ? 3073 1 : 0) 3074 ); 3075 tt_int_op(found_A2, OP_EQ, 0); 3076 3077 /* There's no easy way of checking that we have included all the 3078 * default v3 non-Bridge directory authorities, so let's assume that 3079 * if the total count above is correct, we have the right ones. 3080 */ 3081 } 3082 3083 { 3084 /* fallback_dir_servers */ 3085 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 3086 /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities, 3087 * Default Fallback */ 3088 tt_assert(smartlist_len(fallback_servers) == 3089 2 + n_default_alt_dir_authority); 3090 3091 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3092 int found_D0 = 0; 3093 SMARTLIST_FOREACH(fallback_servers, 3094 dir_server_t *, 3095 ds, 3096 /* increment the found counter if dir_port matches */ 3097 found_D0 += 3098 (ds->ipv4_dirport == 60090 ? 3099 1 : 0) 3100 ); 3101 tt_int_op(found_D0, OP_EQ, 0); 3102 3103 /* AlternateBridgeAuthority - B1 - dir_port: 60091 */ 3104 int found_B1 = 0; 3105 SMARTLIST_FOREACH(fallback_servers, 3106 dir_server_t *, 3107 ds, 3108 /* increment the found counter if dir_port matches */ 3109 found_B1 += 3110 (ds->ipv4_dirport == 60091 ? 3111 1 : 0) 3112 ); 3113 tt_int_op(found_B1, OP_EQ, 1); 3114 3115 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3116 int found_A2 = 0; 3117 SMARTLIST_FOREACH(fallback_servers, 3118 dir_server_t *, 3119 ds, 3120 /* increment the found counter if dir_port matches */ 3121 found_A2 += 3122 (ds->ipv4_dirport == 60092 ? 3123 1 : 0) 3124 ); 3125 tt_int_op(found_A2, OP_EQ, 0); 3126 3127 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */ 3128 int found_non_default_fallback = 0; 3129 SMARTLIST_FOREACH(fallback_servers, 3130 dir_server_t *, 3131 ds, 3132 /* increment the found counter if dir_port matches */ 3133 found_non_default_fallback += 3134 (ds->ipv4_dirport == 60093 ? 3135 1 : 0) 3136 ); 3137 tt_int_op(found_non_default_fallback, OP_EQ, 0); 3138 3139 /* Default FallbackDir - No Nickname - dir_port: 60099 */ 3140 int found_default_fallback = 0; 3141 SMARTLIST_FOREACH(fallback_servers, 3142 dir_server_t *, 3143 ds, 3144 /* increment the found counter if dir_port matches */ 3145 found_default_fallback += 3146 (ds->ipv4_dirport == 60099 ? 3147 1 : 0) 3148 ); 3149 tt_int_op(found_default_fallback, OP_EQ, 1); 3150 3151 /* There's no easy way of checking that we have included all the 3152 * default v3 non-Bridge directory authorities, so let's assume that 3153 * if the total count above is correct, we have the right ones. 3154 */ 3155 } 3156 } 3157 3158 /* 3159 3. Outcome: Use Set Alternate Directory Authority 3160 - Use Default Bridge Authorities 3161 - Use FallbackDir if it is set, otherwise No Default Fallback Directories 3162 Cases expected to yield this outcome: 3163 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority 3164 are both NULL, but AlternateDirAuthority is set) 3165 */ 3166 3167 /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set, 3168 AlternateDirAuthority & FallbackDir Set */ 3169 { 3170 /* clear fallback dirs counter */ 3171 n_add_default_fallback_dir_servers_known_default = 0; 3172 3173 /* clear options*/ 3174 ads_clear_helper(options); 3175 or_options_free(options); 3176 options = options_new(); 3177 3178 /* clear any previous dir servers: 3179 consider_adding_dir_servers() should do this anyway */ 3180 clear_dir_servers(); 3181 3182 /* assign options: 0011 */ 3183 options->DirAuthorities = NULL; 3184 options->AlternateBridgeAuthority = NULL; 3185 options->AlternateDirAuthority = test_alt_dir_authority; 3186 options->FallbackDir = test_fallback_directory; 3187 options->UseDefaultFallbackDirs = 1; 3188 3189 /* parse options - ensure we always update by passing NULL old_options */ 3190 consider_adding_dir_servers(options, NULL); 3191 3192 /* check outcome */ 3193 3194 /* we must not have added the default fallback dirs */ 3195 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 3196 3197 /* we have more fallbacks than just the authorities */ 3198 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 3199 3200 { 3201 /* trusted_dir_servers */ 3202 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 3203 /* (No D0), (No B1), Default Bridge Authorities, A2 */ 3204 tt_assert(smartlist_len(dir_servers) == 3205 1 + n_default_alt_bridge_authority); 3206 3207 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3208 int found_D0 = 0; 3209 SMARTLIST_FOREACH(dir_servers, 3210 dir_server_t *, 3211 ds, 3212 /* increment the found counter if dir_port matches */ 3213 found_D0 += 3214 (ds->ipv4_dirport == 60090 ? 3215 1 : 0) 3216 ); 3217 tt_int_op(found_D0, OP_EQ, 0); 3218 3219 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3220 int found_B1 = 0; 3221 SMARTLIST_FOREACH(dir_servers, 3222 dir_server_t *, 3223 ds, 3224 /* increment the found counter if dir_port matches */ 3225 found_B1 += 3226 (ds->ipv4_dirport == 60091 ? 3227 1 : 0) 3228 ); 3229 tt_int_op(found_B1, OP_EQ, 0); 3230 3231 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 3232 int found_A2 = 0; 3233 SMARTLIST_FOREACH(dir_servers, 3234 dir_server_t *, 3235 ds, 3236 /* increment the found counter if dir_port matches */ 3237 found_A2 += 3238 (ds->ipv4_dirport == 60092 ? 3239 1 : 0) 3240 ); 3241 tt_int_op(found_A2, OP_EQ, 1); 3242 3243 /* There's no easy way of checking that we have included all the 3244 * default Bridge authorities (except for hard-coding tonga's details), 3245 * so let's assume that if the total count above is correct, 3246 * we have the right ones. 3247 */ 3248 } 3249 3250 { 3251 /* fallback_dir_servers */ 3252 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 3253 /* (No D0), (No B1), Default Bridge Authorities, A2, 3254 * Custom Fallback Directory, (No Default Fallback Directories) */ 3255 tt_assert(smartlist_len(fallback_servers) == 3256 2 + n_default_alt_bridge_authority); 3257 3258 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3259 int found_D0 = 0; 3260 SMARTLIST_FOREACH(fallback_servers, 3261 dir_server_t *, 3262 ds, 3263 /* increment the found counter if dir_port matches */ 3264 found_D0 += 3265 (ds->ipv4_dirport == 60090 ? 3266 1 : 0) 3267 ); 3268 tt_int_op(found_D0, OP_EQ, 0); 3269 3270 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3271 int found_B1 = 0; 3272 SMARTLIST_FOREACH(fallback_servers, 3273 dir_server_t *, 3274 ds, 3275 /* increment the found counter if dir_port matches */ 3276 found_B1 += 3277 (ds->ipv4_dirport == 60091 ? 3278 1 : 0) 3279 ); 3280 tt_int_op(found_B1, OP_EQ, 0); 3281 3282 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 3283 int found_A2 = 0; 3284 SMARTLIST_FOREACH(fallback_servers, 3285 dir_server_t *, 3286 ds, 3287 /* increment the found counter if dir_port matches */ 3288 found_A2 += 3289 (ds->ipv4_dirport == 60092 ? 3290 1 : 0) 3291 ); 3292 tt_int_op(found_A2, OP_EQ, 1); 3293 3294 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 3295 int found_non_default_fallback = 0; 3296 SMARTLIST_FOREACH(fallback_servers, 3297 dir_server_t *, 3298 ds, 3299 /* increment the found counter if dir_port matches */ 3300 found_non_default_fallback += 3301 (ds->ipv4_dirport == 60093 ? 3302 1 : 0) 3303 ); 3304 tt_int_op(found_non_default_fallback, OP_EQ, 1); 3305 3306 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 3307 int found_default_fallback = 0; 3308 SMARTLIST_FOREACH(fallback_servers, 3309 dir_server_t *, 3310 ds, 3311 /* increment the found counter if dir_port matches */ 3312 found_default_fallback += 3313 (ds->ipv4_dirport == 60099 ? 3314 1 : 0) 3315 ); 3316 tt_int_op(found_default_fallback, OP_EQ, 0); 3317 3318 /* There's no easy way of checking that we have included all the 3319 * default Bridge authorities (except for hard-coding tonga's details), 3320 * so let's assume that if the total count above is correct, 3321 * we have the right ones. 3322 */ 3323 } 3324 } 3325 3326 /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set, 3327 AlternateDirAuthority Set, FallbackDir Not Set */ 3328 { 3329 /* clear fallback dirs counter */ 3330 n_add_default_fallback_dir_servers_known_default = 0; 3331 3332 /* clear options*/ 3333 ads_clear_helper(options); 3334 or_options_free(options); 3335 options = options_new(); 3336 3337 /* clear any previous dir servers: 3338 consider_adding_dir_servers() should do this anyway */ 3339 clear_dir_servers(); 3340 3341 /* assign options: 0010 */ 3342 options->DirAuthorities = NULL; 3343 options->AlternateBridgeAuthority = NULL; 3344 options->AlternateDirAuthority = test_alt_dir_authority; 3345 options->FallbackDir = NULL; 3346 options->UseDefaultFallbackDirs = 1; 3347 3348 /* parse options - ensure we always update by passing NULL old_options */ 3349 consider_adding_dir_servers(options, NULL); 3350 3351 /* check outcome */ 3352 3353 /* we must not have added the default fallback dirs */ 3354 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 3355 3356 /* we just have the authorities */ 3357 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0); 3358 3359 { 3360 /* trusted_dir_servers */ 3361 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 3362 /* (No D0), (No B1), Default Bridge Authorities, A2, 3363 * No Default or Custom Fallback Directories */ 3364 tt_assert(smartlist_len(dir_servers) == 3365 1 + n_default_alt_bridge_authority); 3366 3367 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3368 int found_D0 = 0; 3369 SMARTLIST_FOREACH(dir_servers, 3370 dir_server_t *, 3371 ds, 3372 /* increment the found counter if dir_port matches */ 3373 found_D0 += 3374 (ds->ipv4_dirport == 60090 ? 3375 1 : 0) 3376 ); 3377 tt_int_op(found_D0, OP_EQ, 0); 3378 3379 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3380 int found_B1 = 0; 3381 SMARTLIST_FOREACH(dir_servers, 3382 dir_server_t *, 3383 ds, 3384 /* increment the found counter if dir_port matches */ 3385 found_B1 += 3386 (ds->ipv4_dirport == 60091 ? 3387 1 : 0) 3388 ); 3389 tt_int_op(found_B1, OP_EQ, 0); 3390 3391 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 3392 int found_A2 = 0; 3393 SMARTLIST_FOREACH(dir_servers, 3394 dir_server_t *, 3395 ds, 3396 /* increment the found counter if dir_port matches */ 3397 found_A2 += 3398 (ds->ipv4_dirport == 60092 ? 3399 1 : 0) 3400 ); 3401 tt_int_op(found_A2, OP_EQ, 1); 3402 3403 /* There's no easy way of checking that we have included all the 3404 * default Bridge authorities (except for hard-coding tonga's details), 3405 * so let's assume that if the total count above is correct, 3406 * we have the right ones. 3407 */ 3408 } 3409 3410 { 3411 /* fallback_dir_servers */ 3412 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 3413 /* (No D0), (No B1), Default Bridge Authorities, A2, 3414 * No Custom or Default Fallback Directories */ 3415 tt_assert(smartlist_len(fallback_servers) == 3416 1 + n_default_alt_bridge_authority); 3417 3418 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3419 int found_D0 = 0; 3420 SMARTLIST_FOREACH(fallback_servers, 3421 dir_server_t *, 3422 ds, 3423 /* increment the found counter if dir_port matches */ 3424 found_D0 += 3425 (ds->ipv4_dirport == 60090 ? 3426 1 : 0) 3427 ); 3428 tt_int_op(found_D0, OP_EQ, 0); 3429 3430 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3431 int found_B1 = 0; 3432 SMARTLIST_FOREACH(fallback_servers, 3433 dir_server_t *, 3434 ds, 3435 /* increment the found counter if dir_port matches */ 3436 found_B1 += 3437 (ds->ipv4_dirport == 60091 ? 3438 1 : 0) 3439 ); 3440 tt_int_op(found_B1, OP_EQ, 0); 3441 3442 /* AlternateDirAuthority - A2 - dir_port: 60092 */ 3443 int found_A2 = 0; 3444 SMARTLIST_FOREACH(fallback_servers, 3445 dir_server_t *, 3446 ds, 3447 /* increment the found counter if dir_port matches */ 3448 found_A2 += 3449 (ds->ipv4_dirport == 60092 ? 3450 1 : 0) 3451 ); 3452 tt_int_op(found_A2, OP_EQ, 1); 3453 3454 /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */ 3455 int found_non_default_fallback = 0; 3456 SMARTLIST_FOREACH(fallback_servers, 3457 dir_server_t *, 3458 ds, 3459 /* increment the found counter if dir_port matches */ 3460 found_non_default_fallback += 3461 (ds->ipv4_dirport == 60093 ? 3462 1 : 0) 3463 ); 3464 tt_int_op(found_non_default_fallback, OP_EQ, 0); 3465 3466 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 3467 int found_default_fallback = 0; 3468 SMARTLIST_FOREACH(fallback_servers, 3469 dir_server_t *, 3470 ds, 3471 /* increment the found counter if dir_port matches */ 3472 found_default_fallback += 3473 (ds->ipv4_dirport == 60099 ? 3474 1 : 0) 3475 ); 3476 tt_int_op(found_default_fallback, OP_EQ, 0); 3477 3478 /* There's no easy way of checking that we have included all the 3479 * default Bridge authorities (except for hard-coding tonga's details), 3480 * so let's assume that if the total count above is correct, 3481 * we have the right ones. 3482 */ 3483 } 3484 } 3485 3486 /* 3487 4. Outcome: Use Set Custom Fallback Directory 3488 - Use Default Bridge & Directory Authorities 3489 Cases expected to yield this outcome: 3490 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority 3491 are all NULL, but FallbackDir is set) 3492 */ 3493 3494 /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority 3495 & AlternateDirAuthority Not Set, FallbackDir Set */ 3496 { 3497 /* clear fallback dirs counter */ 3498 n_add_default_fallback_dir_servers_known_default = 0; 3499 3500 /* clear options*/ 3501 ads_clear_helper(options); 3502 or_options_free(options); 3503 options = options_new(); 3504 3505 /* clear any previous dir servers: 3506 consider_adding_dir_servers() should do this anyway */ 3507 clear_dir_servers(); 3508 3509 /* assign options: 0001 */ 3510 options->DirAuthorities = NULL; 3511 options->AlternateBridgeAuthority = NULL; 3512 options->AlternateDirAuthority = NULL; 3513 options->FallbackDir = test_fallback_directory; 3514 options->UseDefaultFallbackDirs = 1; 3515 3516 /* parse options - ensure we always update by passing NULL old_options */ 3517 consider_adding_dir_servers(options, NULL); 3518 3519 /* check outcome */ 3520 3521 /* we must not have added the default fallback dirs */ 3522 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0); 3523 3524 /* we have more fallbacks than just the authorities */ 3525 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 3526 3527 { 3528 /* trusted_dir_servers */ 3529 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 3530 /* (No D0), (No B1), Default Bridge Authorities, 3531 * (No A2), Default v3 Directory Authorities */ 3532 tt_assert(smartlist_len(dir_servers) == n_default_authorities); 3533 3534 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3535 int found_D0 = 0; 3536 SMARTLIST_FOREACH(dir_servers, 3537 dir_server_t *, 3538 ds, 3539 /* increment the found counter if dir_port matches */ 3540 found_D0 += 3541 (ds->ipv4_dirport == 60090 ? 3542 1 : 0) 3543 ); 3544 tt_int_op(found_D0, OP_EQ, 0); 3545 3546 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3547 int found_B1 = 0; 3548 SMARTLIST_FOREACH(dir_servers, 3549 dir_server_t *, 3550 ds, 3551 /* increment the found counter if dir_port matches */ 3552 found_B1 += 3553 (ds->ipv4_dirport == 60091 ? 3554 1 : 0) 3555 ); 3556 tt_int_op(found_B1, OP_EQ, 0); 3557 3558 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3559 int found_A2 = 0; 3560 SMARTLIST_FOREACH(dir_servers, 3561 dir_server_t *, 3562 ds, 3563 /* increment the found counter if dir_port matches */ 3564 found_A2 += 3565 (ds->ipv4_dirport == 60092 ? 3566 1 : 0) 3567 ); 3568 tt_int_op(found_A2, OP_EQ, 0); 3569 3570 /* There's no easy way of checking that we have included all the 3571 * default Bridge & V3 Directory authorities, so let's assume that 3572 * if the total count above is correct, we have the right ones. 3573 */ 3574 } 3575 3576 { 3577 /* fallback_dir_servers */ 3578 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 3579 /* (No D0), (No B1), Default Bridge Authorities, 3580 * (No A2), Default v3 Directory Authorities, 3581 * Custom Fallback Directory, (No Default Fallback Directories) */ 3582 tt_assert(smartlist_len(fallback_servers) == 3583 1 + n_default_authorities); 3584 3585 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3586 int found_D0 = 0; 3587 SMARTLIST_FOREACH(fallback_servers, 3588 dir_server_t *, 3589 ds, 3590 /* increment the found counter if dir_port matches */ 3591 found_D0 += 3592 (ds->ipv4_dirport == 60090 ? 3593 1 : 0) 3594 ); 3595 tt_int_op(found_D0, OP_EQ, 0); 3596 3597 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3598 int found_B1 = 0; 3599 SMARTLIST_FOREACH(fallback_servers, 3600 dir_server_t *, 3601 ds, 3602 /* increment the found counter if dir_port matches */ 3603 found_B1 += 3604 (ds->ipv4_dirport == 60091 ? 3605 1 : 0) 3606 ); 3607 tt_int_op(found_B1, OP_EQ, 0); 3608 3609 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3610 int found_A2 = 0; 3611 SMARTLIST_FOREACH(fallback_servers, 3612 dir_server_t *, 3613 ds, 3614 /* increment the found counter if dir_port matches */ 3615 found_A2 += 3616 (ds->ipv4_dirport == 60092 ? 3617 1 : 0) 3618 ); 3619 tt_int_op(found_A2, OP_EQ, 0); 3620 3621 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 3622 int found_non_default_fallback = 0; 3623 SMARTLIST_FOREACH(fallback_servers, 3624 dir_server_t *, 3625 ds, 3626 /* increment the found counter if dir_port matches */ 3627 found_non_default_fallback += 3628 (ds->ipv4_dirport == 60093 ? 3629 1 : 0) 3630 ); 3631 tt_int_op(found_non_default_fallback, OP_EQ, 1); 3632 3633 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 3634 int found_default_fallback = 0; 3635 SMARTLIST_FOREACH(fallback_servers, 3636 dir_server_t *, 3637 ds, 3638 /* increment the found counter if dir_port matches */ 3639 found_default_fallback += 3640 (ds->ipv4_dirport == 60099 ? 3641 1 : 0) 3642 ); 3643 tt_int_op(found_default_fallback, OP_EQ, 0); 3644 3645 /* There's no easy way of checking that we have included all the 3646 * default Bridge & V3 Directory authorities, so let's assume that 3647 * if the total count above is correct, we have the right ones. 3648 */ 3649 } 3650 } 3651 3652 /* 3653 5. Outcome: Use All Defaults 3654 - Use Default Bridge & Directory Authorities, Default Fallback Directories 3655 Cases expected to yield this outcome: 3656 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority 3657 and FallbackDir are all NULL) 3658 */ 3659 3660 /* Case 0: 0000 - All Not Set */ 3661 { 3662 /* clear fallback dirs counter */ 3663 n_add_default_fallback_dir_servers_known_default = 0; 3664 3665 /* clear options*/ 3666 ads_clear_helper(options); 3667 or_options_free(options); 3668 options = options_new(); 3669 3670 /* clear any previous dir servers: 3671 consider_adding_dir_servers() should do this anyway */ 3672 clear_dir_servers(); 3673 3674 /* assign options: 0001 */ 3675 options->DirAuthorities = NULL; 3676 options->AlternateBridgeAuthority = NULL; 3677 options->AlternateDirAuthority = NULL; 3678 options->FallbackDir = NULL; 3679 options->UseDefaultFallbackDirs = 1; 3680 3681 /* parse options - ensure we always update by passing NULL old_options */ 3682 consider_adding_dir_servers(options, NULL); 3683 3684 /* check outcome */ 3685 3686 /* we must have added the default fallback dirs */ 3687 tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1); 3688 3689 /* we have more fallbacks than just the authorities */ 3690 tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1); 3691 3692 { 3693 /* trusted_dir_servers */ 3694 const smartlist_t *dir_servers = router_get_trusted_dir_servers(); 3695 /* (No D0), (No B1), Default Bridge Authorities, 3696 * (No A2), Default v3 Directory Authorities */ 3697 tt_assert(smartlist_len(dir_servers) == n_default_authorities); 3698 3699 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3700 int found_D0 = 0; 3701 SMARTLIST_FOREACH(dir_servers, 3702 dir_server_t *, 3703 ds, 3704 /* increment the found counter if dir_port matches */ 3705 found_D0 += 3706 (ds->ipv4_dirport == 60090 ? 3707 1 : 0) 3708 ); 3709 tt_int_op(found_D0, OP_EQ, 0); 3710 3711 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3712 int found_B1 = 0; 3713 SMARTLIST_FOREACH(dir_servers, 3714 dir_server_t *, 3715 ds, 3716 /* increment the found counter if dir_port matches */ 3717 found_B1 += 3718 (ds->ipv4_dirport == 60091 ? 3719 1 : 0) 3720 ); 3721 tt_int_op(found_B1, OP_EQ, 0); 3722 3723 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3724 int found_A2 = 0; 3725 SMARTLIST_FOREACH(dir_servers, 3726 dir_server_t *, 3727 ds, 3728 /* increment the found counter if dir_port matches */ 3729 found_A2 += 3730 (ds->ipv4_dirport == 60092 ? 3731 1 : 0) 3732 ); 3733 tt_int_op(found_A2, OP_EQ, 0); 3734 3735 /* There's no easy way of checking that we have included all the 3736 * default Bridge & V3 Directory authorities, so let's assume that 3737 * if the total count above is correct, we have the right ones. 3738 */ 3739 } 3740 3741 { 3742 /* fallback_dir_servers */ 3743 const smartlist_t *fallback_servers = router_get_fallback_dir_servers(); 3744 /* (No D0), (No B1), Default Bridge Authorities, 3745 * (No A2), Default v3 Directory Authorities, 3746 * (No Custom Fallback Directory), Default Fallback Directories */ 3747 tt_assert(smartlist_len(fallback_servers) == 3748 n_default_authorities + n_default_fallback_dir); 3749 3750 /* (No DirAuthorities) - D0 - dir_port: 60090 */ 3751 int found_D0 = 0; 3752 SMARTLIST_FOREACH(fallback_servers, 3753 dir_server_t *, 3754 ds, 3755 /* increment the found counter if dir_port matches */ 3756 found_D0 += 3757 (ds->ipv4_dirport == 60090 ? 3758 1 : 0) 3759 ); 3760 tt_int_op(found_D0, OP_EQ, 0); 3761 3762 /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */ 3763 int found_B1 = 0; 3764 SMARTLIST_FOREACH(fallback_servers, 3765 dir_server_t *, 3766 ds, 3767 /* increment the found counter if dir_port matches */ 3768 found_B1 += 3769 (ds->ipv4_dirport == 60091 ? 3770 1 : 0) 3771 ); 3772 tt_int_op(found_B1, OP_EQ, 0); 3773 3774 /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */ 3775 int found_A2 = 0; 3776 SMARTLIST_FOREACH(fallback_servers, 3777 dir_server_t *, 3778 ds, 3779 /* increment the found counter if dir_port matches */ 3780 found_A2 += 3781 (ds->ipv4_dirport == 60092 ? 3782 1 : 0) 3783 ); 3784 tt_int_op(found_A2, OP_EQ, 0); 3785 3786 /* Custom FallbackDir - No Nickname - dir_port: 60093 */ 3787 int found_non_default_fallback = 0; 3788 SMARTLIST_FOREACH(fallback_servers, 3789 dir_server_t *, 3790 ds, 3791 /* increment the found counter if dir_port matches */ 3792 found_non_default_fallback += 3793 (ds->ipv4_dirport == 60093 ? 3794 1 : 0) 3795 ); 3796 tt_int_op(found_non_default_fallback, OP_EQ, 0); 3797 3798 /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */ 3799 int found_default_fallback = 0; 3800 SMARTLIST_FOREACH(fallback_servers, 3801 dir_server_t *, 3802 ds, 3803 /* increment the found counter if dir_port matches */ 3804 found_default_fallback += 3805 (ds->ipv4_dirport == 60099 ? 3806 1 : 0) 3807 ); 3808 tt_int_op(found_default_fallback, OP_EQ, 1); 3809 3810 /* There's no easy way of checking that we have included all the 3811 * default Bridge & V3 Directory authorities, and the default 3812 * Fallback Directories, so let's assume that if the total count 3813 * above is correct, we have the right ones. 3814 */ 3815 } 3816 } 3817 3818 done: 3819 clear_dir_servers(); 3820 3821 tor_free(test_dir_authority->key); 3822 tor_free(test_dir_authority->value); 3823 tor_free(test_dir_authority); 3824 3825 tor_free(test_alt_dir_authority->key); 3826 tor_free(test_alt_dir_authority->value); 3827 tor_free(test_alt_dir_authority); 3828 3829 tor_free(test_alt_bridge_authority->key); 3830 tor_free(test_alt_bridge_authority->value); 3831 tor_free(test_alt_bridge_authority); 3832 3833 tor_free(test_fallback_directory->key); 3834 tor_free(test_fallback_directory->value); 3835 tor_free(test_fallback_directory); 3836 3837 ads_clear_helper(options); 3838 or_options_free(options); 3839 3840 UNMOCK(add_default_fallback_dir_servers); 3841 } 3842 3843 static void 3844 test_config_default_dir_servers(void *arg) 3845 { 3846 or_options_t *opts = NULL; 3847 (void)arg; 3848 int trusted_count = 0; 3849 int fallback_count = 0; 3850 3851 /* new set of options should stop fallback parsing */ 3852 opts = options_new(); 3853 opts->UseDefaultFallbackDirs = 0; 3854 /* set old_options to NULL to force dir update */ 3855 consider_adding_dir_servers(opts, NULL); 3856 trusted_count = smartlist_len(router_get_trusted_dir_servers()); 3857 fallback_count = smartlist_len(router_get_fallback_dir_servers()); 3858 or_options_free(opts); 3859 opts = NULL; 3860 3861 /* assume a release will never go out with less than 7 authorities */ 3862 tt_int_op(trusted_count, OP_GE, 7); 3863 /* if we disable the default fallbacks, there must not be any extra */ 3864 tt_assert(fallback_count == trusted_count); 3865 3866 opts = options_new(); 3867 opts->UseDefaultFallbackDirs = 1; 3868 consider_adding_dir_servers(opts, opts); 3869 trusted_count = smartlist_len(router_get_trusted_dir_servers()); 3870 fallback_count = smartlist_len(router_get_fallback_dir_servers()); 3871 or_options_free(opts); 3872 opts = NULL; 3873 3874 /* assume a release will never go out with less than 7 authorities */ 3875 tt_int_op(trusted_count, OP_GE, 7); 3876 /* XX/teor - allow for default fallbacks to be added without breaking 3877 * the unit tests. Set a minimum fallback count once the list is stable. */ 3878 tt_assert(fallback_count >= trusted_count); 3879 3880 done: 3881 or_options_free(opts); 3882 } 3883 3884 static bool mock_relay_find_addr_to_publish_result = true; 3885 3886 static bool 3887 mock_relay_find_addr_to_publish(const or_options_t *options, int family, 3888 int flags, tor_addr_t *addr_out) 3889 { 3890 (void) options; 3891 (void) family; 3892 (void) flags; 3893 (void) addr_out; 3894 return mock_relay_find_addr_to_publish_result; 3895 } 3896 3897 static int mock_router_my_exit_policy_is_reject_star_result = 0; 3898 3899 static int 3900 mock_router_my_exit_policy_is_reject_star(void) 3901 { 3902 return mock_router_my_exit_policy_is_reject_star_result; 3903 } 3904 3905 static int mock_advertised_server_mode_result = 0; 3906 3907 static int 3908 mock_advertised_server_mode(void) 3909 { 3910 return mock_advertised_server_mode_result; 3911 } 3912 3913 static routerinfo_t *mock_router_get_my_routerinfo_result = NULL; 3914 3915 static const routerinfo_t * 3916 mock_router_get_my_routerinfo(void) 3917 { 3918 return mock_router_get_my_routerinfo_result; 3919 } 3920 3921 static void 3922 test_config_directory_fetch(void *arg) 3923 { 3924 (void)arg; 3925 3926 /* Test Setup */ 3927 or_options_t *options = options_new(); 3928 routerinfo_t routerinfo; 3929 memset(&routerinfo, 0, sizeof(routerinfo)); 3930 mock_relay_find_addr_to_publish_result = false; 3931 mock_router_my_exit_policy_is_reject_star_result = 1; 3932 mock_advertised_server_mode_result = 0; 3933 mock_router_get_my_routerinfo_result = NULL; 3934 MOCK(relay_find_addr_to_publish, mock_relay_find_addr_to_publish); 3935 MOCK(router_my_exit_policy_is_reject_star, 3936 mock_router_my_exit_policy_is_reject_star); 3937 MOCK(advertised_server_mode, mock_advertised_server_mode); 3938 MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo); 3939 or_options_free(options); 3940 options = options_new(); 3941 3942 /* Clients can use multiple directory mirrors for bootstrap */ 3943 options->ClientOnly = 1; 3944 tt_assert(server_mode(options) == 0); 3945 tt_assert(public_server_mode(options) == 0); 3946 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 3947 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 3948 OP_EQ, 1); 3949 3950 /* Bridge Clients can use multiple directory mirrors for bootstrap */ 3951 or_options_free(options); 3952 options = options_new(); 3953 options->UseBridges = 1; 3954 tt_assert(server_mode(options) == 0); 3955 tt_assert(public_server_mode(options) == 0); 3956 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 3957 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 3958 OP_EQ, 1); 3959 3960 /* Bridge Relays (Bridges) must act like clients, and use multiple 3961 * directory mirrors for bootstrap */ 3962 or_options_free(options); 3963 options = options_new(); 3964 options->BridgeRelay = 1; 3965 options->ORPort_set = 1; 3966 tt_assert(server_mode(options) == 1); 3967 tt_assert(public_server_mode(options) == 0); 3968 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 3969 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 3970 OP_EQ, 1); 3971 3972 /* Clients set to FetchDirInfoEarly must fetch it from the authorities, 3973 * but can use multiple authorities for bootstrap */ 3974 or_options_free(options); 3975 options = options_new(); 3976 options->FetchDirInfoEarly = 1; 3977 tt_assert(server_mode(options) == 0); 3978 tt_assert(public_server_mode(options) == 0); 3979 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1); 3980 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 3981 OP_EQ, 1); 3982 3983 /* Exit OR servers only fetch the consensus from the authorities when they 3984 * refuse unknown exits, but never use multiple directories for bootstrap 3985 */ 3986 or_options_free(options); 3987 options = options_new(); 3988 options->ORPort_set = 1; 3989 options->ExitRelay = 1; 3990 mock_relay_find_addr_to_publish_result = true; 3991 mock_router_my_exit_policy_is_reject_star_result = 0; 3992 mock_advertised_server_mode_result = 1; 3993 mock_router_get_my_routerinfo_result = &routerinfo; 3994 3995 routerinfo.supports_tunnelled_dir_requests = 1; 3996 3997 options->RefuseUnknownExits = 1; 3998 tt_assert(server_mode(options) == 1); 3999 tt_assert(public_server_mode(options) == 1); 4000 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1); 4001 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4002 OP_EQ, 0); 4003 4004 options->RefuseUnknownExits = 0; 4005 mock_relay_find_addr_to_publish_result = true; 4006 tt_assert(server_mode(options) == 1); 4007 tt_assert(public_server_mode(options) == 1); 4008 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 4009 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4010 OP_EQ, 0); 4011 4012 /* Dir servers fetch the consensus from the authorities, unless they are not 4013 * advertising themselves (hibernating) or have no routerinfo or are not 4014 * advertising their dirport, and never use multiple directories for 4015 * bootstrap. This only applies if they are also OR servers. 4016 * (We don't care much about the behaviour of non-OR directory servers.) */ 4017 or_options_free(options); 4018 options = options_new(); 4019 options->DirPort_set = 1; 4020 options->ORPort_set = 1; 4021 options->DirCache = 1; 4022 mock_relay_find_addr_to_publish_result = true; 4023 mock_router_my_exit_policy_is_reject_star_result = 1; 4024 4025 mock_advertised_server_mode_result = 1; 4026 routerinfo.ipv4_dirport = 1; 4027 mock_router_get_my_routerinfo_result = &routerinfo; 4028 tt_assert(server_mode(options) == 1); 4029 tt_assert(public_server_mode(options) == 1); 4030 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1); 4031 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4032 OP_EQ, 0); 4033 4034 mock_advertised_server_mode_result = 0; 4035 routerinfo.ipv4_dirport = 1; 4036 mock_router_get_my_routerinfo_result = &routerinfo; 4037 tt_assert(server_mode(options) == 1); 4038 tt_assert(public_server_mode(options) == 1); 4039 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 4040 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4041 OP_EQ, 0); 4042 4043 mock_advertised_server_mode_result = 1; 4044 mock_router_get_my_routerinfo_result = NULL; 4045 tt_assert(server_mode(options) == 1); 4046 tt_assert(public_server_mode(options) == 1); 4047 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 4048 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4049 OP_EQ, 0); 4050 4051 mock_advertised_server_mode_result = 1; 4052 routerinfo.ipv4_dirport = 0; 4053 routerinfo.supports_tunnelled_dir_requests = 0; 4054 mock_router_get_my_routerinfo_result = &routerinfo; 4055 tt_assert(server_mode(options) == 1); 4056 tt_assert(public_server_mode(options) == 1); 4057 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0); 4058 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4059 OP_EQ, 0); 4060 4061 mock_advertised_server_mode_result = 1; 4062 routerinfo.ipv4_dirport = 1; 4063 routerinfo.supports_tunnelled_dir_requests = 1; 4064 mock_router_get_my_routerinfo_result = &routerinfo; 4065 tt_assert(server_mode(options) == 1); 4066 tt_assert(public_server_mode(options) == 1); 4067 tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1); 4068 tt_int_op(networkstatus_consensus_can_use_multiple_directories(options), 4069 OP_EQ, 0); 4070 4071 done: 4072 or_options_free(options); 4073 UNMOCK(relay_find_addr_to_publish); 4074 UNMOCK(router_get_my_routerinfo); 4075 UNMOCK(advertised_server_mode); 4076 UNMOCK(router_my_exit_policy_is_reject_star); 4077 } 4078 4079 static void 4080 test_config_default_fallback_dirs(void *arg) 4081 { 4082 const char *fallback[] = { 4083 #ifndef COCCI 4084 #include "app/config/fallback_dirs.inc" 4085 #endif 4086 NULL 4087 }; 4088 4089 int n_included_fallback_dirs = 0; 4090 int n_added_fallback_dirs = 0; 4091 4092 (void)arg; 4093 clear_dir_servers(); 4094 4095 while (fallback[n_included_fallback_dirs]) 4096 n_included_fallback_dirs++; 4097 4098 add_default_fallback_dir_servers(); 4099 4100 n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers()); 4101 4102 tt_assert(n_included_fallback_dirs == n_added_fallback_dirs); 4103 4104 done: 4105 clear_dir_servers(); 4106 } 4107 4108 static void 4109 test_config_port_cfg_line_extract_addrport(void *arg) 4110 { 4111 (void)arg; 4112 int unixy = 0; 4113 const char *rest = NULL; 4114 char *a = NULL; 4115 4116 tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0); 4117 tt_int_op(unixy, OP_EQ, 0); 4118 tt_str_op(a, OP_EQ, ""); 4119 tt_str_op(rest, OP_EQ, ""); 4120 tor_free(a); 4121 4122 tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest), 4123 OP_EQ, 0); 4124 tt_int_op(unixy, OP_EQ, 0); 4125 tt_str_op(a, OP_EQ, "hello"); 4126 tt_str_op(rest, OP_EQ, ""); 4127 tor_free(a); 4128 4129 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut", 4130 &a, &unixy, &rest), OP_EQ, 0); 4131 tt_int_op(unixy, OP_EQ, 0); 4132 tt_str_op(a, OP_EQ, "flipperwalt"); 4133 tt_str_op(rest, OP_EQ, "gersplut"); 4134 tor_free(a); 4135 4136 tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut", 4137 &a, &unixy, &rest), OP_EQ, 0); 4138 tt_int_op(unixy, OP_EQ, 0); 4139 tt_str_op(a, OP_EQ, "flipperwalt"); 4140 tt_str_op(rest, OP_EQ, "gersplut"); 4141 tor_free(a); 4142 4143 tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut", 4144 &a, &unixy, &rest), OP_EQ, 0); 4145 tt_int_op(unixy, OP_EQ, 0); 4146 tt_str_op(a, OP_EQ, "flipperwalt"); 4147 tt_str_op(rest, OP_EQ, "gersplut"); 4148 tor_free(a); 4149 4150 tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut", 4151 &a, &unixy, &rest), OP_EQ, 0); 4152 tt_int_op(unixy, OP_EQ, 1); 4153 tt_str_op(a, OP_EQ, "flipperwalt"); 4154 tt_str_op(rest, OP_EQ, "gersplut"); 4155 tor_free(a); 4156 4157 tt_int_op(port_cfg_line_extract_addrport("lolol", 4158 &a, &unixy, &rest), OP_EQ, 0); 4159 tt_int_op(unixy, OP_EQ, 0); 4160 tt_str_op(a, OP_EQ, "lolol"); 4161 tt_str_op(rest, OP_EQ, ""); 4162 tor_free(a); 4163 4164 tt_int_op(port_cfg_line_extract_addrport("unix:lolol", 4165 &a, &unixy, &rest), OP_EQ, 0); 4166 tt_int_op(unixy, OP_EQ, 1); 4167 tt_str_op(a, OP_EQ, "lolol"); 4168 tt_str_op(rest, OP_EQ, ""); 4169 tor_free(a); 4170 4171 tt_int_op(port_cfg_line_extract_addrport("unix:lolol ", 4172 &a, &unixy, &rest), OP_EQ, 0); 4173 tt_int_op(unixy, OP_EQ, 1); 4174 tt_str_op(a, OP_EQ, "lolol"); 4175 tt_str_op(rest, OP_EQ, ""); 4176 tor_free(a); 4177 4178 tt_int_op(port_cfg_line_extract_addrport(" unix:lolol", 4179 &a, &unixy, &rest), OP_EQ, 0); 4180 tt_int_op(unixy, OP_EQ, 1); 4181 tt_str_op(a, OP_EQ, "lolol"); 4182 tt_str_op(rest, OP_EQ, ""); 4183 tor_free(a); 4184 4185 tt_int_op(port_cfg_line_extract_addrport("foobar:lolol", 4186 &a, &unixy, &rest), OP_EQ, 0); 4187 tt_int_op(unixy, OP_EQ, 0); 4188 tt_str_op(a, OP_EQ, "foobar:lolol"); 4189 tt_str_op(rest, OP_EQ, ""); 4190 tor_free(a); 4191 4192 tt_int_op(port_cfg_line_extract_addrport(":lolol", 4193 &a, &unixy, &rest), OP_EQ, 0); 4194 tt_int_op(unixy, OP_EQ, 0); 4195 tt_str_op(a, OP_EQ, ":lolol"); 4196 tt_str_op(rest, OP_EQ, ""); 4197 tor_free(a); 4198 4199 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"", 4200 &a, &unixy, &rest), OP_EQ, 0); 4201 tt_int_op(unixy, OP_EQ, 1); 4202 tt_str_op(a, OP_EQ, "lolol"); 4203 tt_str_op(rest, OP_EQ, ""); 4204 tor_free(a); 4205 4206 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ", 4207 &a, &unixy, &rest), OP_EQ, 0); 4208 tt_int_op(unixy, OP_EQ, 1); 4209 tt_str_op(a, OP_EQ, "lolol"); 4210 tt_str_op(rest, OP_EQ, ""); 4211 tor_free(a); 4212 4213 tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ", 4214 &a, &unixy, &rest), OP_EQ, 0); 4215 tt_int_op(unixy, OP_EQ, 1); 4216 tt_str_op(a, OP_EQ, "lolol"); 4217 tt_str_op(rest, OP_EQ, "foo "); 4218 tor_free(a); 4219 4220 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ", 4221 &a, &unixy, &rest), OP_EQ, 0); 4222 tt_int_op(unixy, OP_EQ, 1); 4223 tt_str_op(a, OP_EQ, "lol ol"); 4224 tt_str_op(rest, OP_EQ, "foo "); 4225 tor_free(a); 4226 4227 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ", 4228 &a, &unixy, &rest), OP_EQ, 0); 4229 tt_int_op(unixy, OP_EQ, 1); 4230 tt_str_op(a, OP_EQ, "lol\" ol"); 4231 tt_str_op(rest, OP_EQ, "foo "); 4232 tor_free(a); 4233 4234 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ", 4235 &a, &unixy, &rest), OP_EQ, -1); 4236 tor_free(a); 4237 4238 tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ", 4239 &a, &unixy, &rest), OP_EQ, -1); 4240 tor_free(a); 4241 4242 done: 4243 tor_free(a); 4244 } 4245 4246 static config_line_t * 4247 mock_config_line(const char *key, const char *val) 4248 { 4249 config_line_t *config_line = tor_malloc(sizeof(config_line_t)); 4250 memset(config_line, 0, sizeof(config_line_t)); 4251 config_line->key = tor_strdup(key); 4252 config_line->value = tor_strdup(val); 4253 return config_line; 4254 } 4255 4256 static void 4257 test_config_parse_port_config__ports__no_ports_given(void *data) 4258 { 4259 (void)data; 4260 int ret; 4261 smartlist_t *slout = NULL; 4262 port_cfg_t *port_cfg = NULL; 4263 4264 slout = smartlist_new(); 4265 4266 // Test no defaultport, no defaultaddress and no out 4267 ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 0, 0); 4268 tt_int_op(ret, OP_EQ, 0); 4269 4270 // Test with defaultport, no defaultaddress and no out 4271 ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 42, 0); 4272 tt_int_op(ret, OP_EQ, 0); 4273 4274 // Test no defaultport, with defaultaddress and no out 4275 ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0); 4276 tt_int_op(ret, OP_EQ, 0); 4277 4278 // Test with defaultport, with defaultaddress and no out 4279 ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0); 4280 tt_int_op(ret, OP_EQ, 0); 4281 4282 // Test no defaultport, no defaultaddress and with out 4283 ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 0, 0); 4284 tt_int_op(ret, OP_EQ, 0); 4285 tt_int_op(smartlist_len(slout), OP_EQ, 0); 4286 4287 // Test with defaultport, no defaultaddress and with out 4288 ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 42, 0); 4289 tt_int_op(ret, OP_EQ, 0); 4290 tt_int_op(smartlist_len(slout), OP_EQ, 0); 4291 4292 // Test no defaultport, with defaultaddress and with out 4293 ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0); 4294 tt_int_op(ret, OP_EQ, 0); 4295 tt_int_op(smartlist_len(slout), OP_EQ, 0); 4296 4297 // Test with defaultport, with defaultaddress and out, adds a new port cfg 4298 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4299 smartlist_clear(slout); 4300 ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0); 4301 tt_int_op(ret, OP_EQ, 0); 4302 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4303 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4304 tt_int_op(port_cfg->port, OP_EQ, 42); 4305 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0); 4306 4307 // Test with defaultport, with defaultaddress and out, adds a new port cfg 4308 // for a unix address 4309 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4310 smartlist_clear(slout); 4311 ret = port_parse_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain", 4312 42, CL_PORT_IS_UNIXSOCKET); 4313 tt_int_op(ret, OP_EQ, 0); 4314 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4315 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4316 tt_int_op(port_cfg->port, OP_EQ, 0); 4317 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1); 4318 tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain"); 4319 4320 done: 4321 if (slout) 4322 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4323 smartlist_free(slout); 4324 } 4325 4326 static void 4327 test_config_parse_port_config__ports__ports_given(void *data) 4328 { 4329 (void)data; 4330 int ret; 4331 smartlist_t *slout = NULL; 4332 port_cfg_t *port_cfg = NULL; 4333 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL; 4334 tor_addr_t addr; 4335 4336 slout = smartlist_new(); 4337 4338 mock_hostname_resolver(); 4339 4340 // Test error when encounters an invalid Port specification 4341 config_port_invalid = mock_config_line("DNSPort", ""); 4342 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL, 4343 0, 0); 4344 tt_int_op(ret, OP_EQ, -1); 4345 4346 // Test error when encounters an empty unix domain specification 4347 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4348 config_port_invalid = mock_config_line("DNSPort", "unix:"); 4349 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL, 4350 0, 0); 4351 tt_int_op(ret, OP_EQ, -1); 4352 4353 // Test error when encounters a unix domain specification but the listener 4354 // doesn't support domain sockets 4355 config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar"); 4356 ret = port_parse_config(NULL, config_port_valid, "DNS", 4357 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0); 4358 tt_int_op(ret, OP_EQ, -1); 4359 4360 // Test valid unix domain 4361 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4362 smartlist_clear(slout); 4363 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4364 CONN_TYPE_AP_LISTENER, NULL, 0, 0); 4365 #ifdef _WIN32 4366 tt_int_op(ret, OP_EQ, -1); 4367 #else 4368 tt_int_op(ret, OP_EQ, 0); 4369 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4370 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4371 tt_int_op(port_cfg->port, OP_EQ, 0); 4372 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1); 4373 tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar"); 4374 /* Test entry port defaults as initialised in port_parse_config */ 4375 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1); 4376 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1); 4377 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1); 4378 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0); 4379 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1); 4380 #endif /* defined(_WIN32) */ 4381 4382 // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only) 4383 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4384 config_port_invalid = mock_config_line("SOCKSPort", 4385 "unix:/tmp/foo/bar NoIPv4Traffic " 4386 "NoIPv6Traffic " 4387 "NoOnionTraffic"); 4388 ret = port_parse_config(NULL, config_port_invalid, "SOCKS", 4389 CONN_TYPE_AP_LISTENER, NULL, 0, 4390 CL_PORT_TAKES_HOSTNAMES); 4391 tt_int_op(ret, OP_EQ, -1); 4392 4393 // Test failure if we have no DNS and we're a DNSPort 4394 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4395 config_port_invalid = mock_config_line("DNSPort", 4396 "127.0.0.1:80 NoDNSRequest"); 4397 ret = port_parse_config(NULL, config_port_invalid, "DNS", 4398 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 4399 CL_PORT_TAKES_HOSTNAMES); 4400 tt_int_op(ret, OP_EQ, -1); 4401 4402 // If we're a DNSPort, DNS only is ok 4403 // Use a port because DNSPort doesn't support sockets 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", "127.0.0.1:80 " 4408 "NoIPv6Traffic " 4409 "NoIPv4Traffic NoOnionTraffic"); 4410 ret = port_parse_config(slout, config_port_valid, "DNS", 4411 CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 4412 CL_PORT_TAKES_HOSTNAMES); 4413 tt_int_op(ret, OP_EQ, 0); 4414 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4415 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4416 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1); 4417 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0); 4418 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0); 4419 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0); 4420 4421 // Test failure if we have DNS but no ipv4 and no ipv6 4422 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4423 config_port_invalid = mock_config_line("SOCKSPort", 4424 "NoIPv6Traffic " 4425 "unix:/tmp/foo/bar NoIPv4Traffic"); 4426 ret = port_parse_config(NULL, config_port_invalid, "SOCKS", 4427 CONN_TYPE_AP_LISTENER, NULL, 0, 4428 CL_PORT_TAKES_HOSTNAMES); 4429 tt_int_op(ret, OP_EQ, -1); 4430 4431 // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate 4432 // options) 4433 config_free_lines(config_port_valid); config_port_valid = NULL; 4434 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4435 smartlist_clear(slout); 4436 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar " 4437 "NoIPv6Traffic " 4438 "NoDNSRequest NoIPv4Traffic"); 4439 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4440 CONN_TYPE_AP_LISTENER, NULL, 0, 4441 CL_PORT_TAKES_HOSTNAMES); 4442 #ifdef _WIN32 4443 tt_int_op(ret, OP_EQ, -1); 4444 #else 4445 tt_int_op(ret, OP_EQ, 0); 4446 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4447 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4448 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0); 4449 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0); 4450 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0); 4451 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1); 4452 #endif /* defined(_WIN32) */ 4453 4454 // Test success with quoted unix: address. 4455 config_free_lines(config_port_valid); config_port_valid = NULL; 4456 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4457 smartlist_clear(slout); 4458 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" " 4459 "NoIPv6Traffic " 4460 "NoDNSRequest NoIPv4Traffic"); 4461 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4462 CONN_TYPE_AP_LISTENER, NULL, 0, 4463 CL_PORT_TAKES_HOSTNAMES); 4464 #ifdef _WIN32 4465 tt_int_op(ret, OP_EQ, -1); 4466 #else 4467 tt_int_op(ret, OP_EQ, 0); 4468 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4469 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4470 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0); 4471 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0); 4472 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0); 4473 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1); 4474 #endif /* defined(_WIN32) */ 4475 4476 // Test failure with broken quoted unix: address. 4477 config_free_lines(config_port_valid); config_port_valid = NULL; 4478 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4479 smartlist_clear(slout); 4480 config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar " 4481 "NoIPv6Traffic " 4482 "NoDNSRequest NoIPv4Traffic"); 4483 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4484 CONN_TYPE_AP_LISTENER, NULL, 0, 4485 CL_PORT_TAKES_HOSTNAMES); 4486 tt_int_op(ret, OP_EQ, -1); 4487 4488 // Test failure with empty quoted unix: address. 4489 config_free_lines(config_port_valid); config_port_valid = NULL; 4490 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4491 smartlist_clear(slout); 4492 config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" " 4493 "NoIPv6Traffic " 4494 "NoDNSRequest NoIPv4Traffic"); 4495 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4496 CONN_TYPE_AP_LISTENER, NULL, 0, 4497 CL_PORT_TAKES_HOSTNAMES); 4498 tt_int_op(ret, OP_EQ, -1); 4499 4500 // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6) 4501 config_free_lines(config_port_valid); config_port_valid = NULL; 4502 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4503 smartlist_clear(slout); 4504 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar " 4505 "OnionTrafficOnly"); 4506 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4507 CONN_TYPE_AP_LISTENER, NULL, 0, 4508 CL_PORT_TAKES_HOSTNAMES); 4509 #ifdef _WIN32 4510 tt_int_op(ret, OP_EQ, -1); 4511 #else 4512 tt_int_op(ret, OP_EQ, 0); 4513 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4514 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4515 tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0); 4516 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0); 4517 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0); 4518 tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1); 4519 #endif /* defined(_WIN32) */ 4520 4521 // Test success with no ipv4 but take ipv6 4522 config_free_lines(config_port_valid); config_port_valid = NULL; 4523 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4524 smartlist_clear(slout); 4525 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar " 4526 "NoIPv4Traffic IPv6Traffic"); 4527 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4528 CONN_TYPE_AP_LISTENER, NULL, 0, 4529 CL_PORT_TAKES_HOSTNAMES); 4530 #ifdef _WIN32 4531 tt_int_op(ret, OP_EQ, -1); 4532 #else 4533 tt_int_op(ret, OP_EQ, 0); 4534 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4535 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4536 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0); 4537 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1); 4538 #endif /* defined(_WIN32) */ 4539 4540 // Test success with both ipv4 and ipv6 4541 config_free_lines(config_port_valid); config_port_valid = NULL; 4542 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4543 smartlist_clear(slout); 4544 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar " 4545 "IPv4Traffic IPv6Traffic"); 4546 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4547 CONN_TYPE_AP_LISTENER, NULL, 0, 4548 CL_PORT_TAKES_HOSTNAMES); 4549 #ifdef _WIN32 4550 tt_int_op(ret, OP_EQ, -1); 4551 #else 4552 tt_int_op(ret, OP_EQ, 0); 4553 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4554 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4555 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1); 4556 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1); 4557 #endif /* defined(_WIN32) */ 4558 4559 // Test failure if we specify world writable for an IP Port 4560 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4561 config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable"); 4562 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, 4563 "127.0.0.3", 0, 0); 4564 tt_int_op(ret, OP_EQ, -1); 4565 4566 // Test failure if we specify group writable for an IP Port 4567 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4568 config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable"); 4569 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, 4570 "127.0.0.3", 0, 0); 4571 tt_int_op(ret, OP_EQ, -1); 4572 4573 // Test failure if we specify group writable for an IP Port 4574 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4575 config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck"); 4576 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, 4577 "127.0.0.3", 0, 0); 4578 tt_int_op(ret, OP_EQ, -1); 4579 4580 // Test success with only a port (this will fail without a default address) 4581 config_free_lines(config_port_valid); config_port_valid = NULL; 4582 config_port_valid = mock_config_line("DNSPort", "42"); 4583 ret = port_parse_config(NULL, config_port_valid, "DNS", 0, 4584 "127.0.0.3", 0, 0); 4585 tt_int_op(ret, OP_EQ, 0); 4586 4587 // Test success with only a port and isolate destination port 4588 config_free_lines(config_port_valid); config_port_valid = NULL; 4589 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4590 smartlist_clear(slout); 4591 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort"); 4592 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4593 "127.0.0.3", 0, 0); 4594 tt_int_op(ret, OP_EQ, 0); 4595 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4596 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4597 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4598 ISO_DEFAULT | ISO_DESTPORT); 4599 4600 // Test success with a negative isolate destination port, and plural 4601 config_free_lines(config_port_valid); config_port_valid = NULL; 4602 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4603 smartlist_clear(slout); 4604 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts"); 4605 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4606 "127.0.0.3", 0, 0); 4607 tt_int_op(ret, OP_EQ, 0); 4608 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4609 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4610 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4611 ISO_DEFAULT & ~ISO_DESTPORT); 4612 4613 // Test success with isolate destination address 4614 config_free_lines(config_port_valid); config_port_valid = NULL; 4615 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4616 smartlist_clear(slout); 4617 config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr"); 4618 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4619 "127.0.0.3", 0, 0); 4620 tt_int_op(ret, OP_EQ, 0); 4621 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4622 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4623 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4624 ISO_DEFAULT | ISO_DESTADDR); 4625 4626 // Test success with isolate socks AUTH 4627 config_free_lines(config_port_valid); config_port_valid = NULL; 4628 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4629 smartlist_clear(slout); 4630 config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth"); 4631 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4632 "127.0.0.3", 0, 0); 4633 tt_int_op(ret, OP_EQ, 0); 4634 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4635 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4636 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4637 ISO_DEFAULT | ISO_SOCKSAUTH); 4638 4639 // Test success with isolate client protocol 4640 config_free_lines(config_port_valid); config_port_valid = NULL; 4641 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4642 smartlist_clear(slout); 4643 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol"); 4644 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4645 "127.0.0.3", 0, 0); 4646 tt_int_op(ret, OP_EQ, 0); 4647 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4648 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4649 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4650 ISO_DEFAULT | ISO_CLIENTPROTO); 4651 4652 // Test success with isolate client address 4653 config_free_lines(config_port_valid); config_port_valid = NULL; 4654 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4655 smartlist_clear(slout); 4656 config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr"); 4657 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4658 "127.0.0.3", 0, 0); 4659 tt_int_op(ret, OP_EQ, 0); 4660 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4661 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4662 tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ, 4663 ISO_DEFAULT | ISO_CLIENTADDR); 4664 4665 // Test success with ignored unknown options 4666 config_free_lines(config_port_valid); config_port_valid = NULL; 4667 config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist"); 4668 ret = port_parse_config(NULL, config_port_valid, "DNS", 0, 4669 "127.0.0.3", 0, 0); 4670 tt_int_op(ret, OP_EQ, 0); 4671 4672 // Test success with no isolate socks AUTH 4673 config_free_lines(config_port_valid); config_port_valid = NULL; 4674 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4675 smartlist_clear(slout); 4676 config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth"); 4677 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4678 "127.0.0.3", 0, 0); 4679 tt_int_op(ret, OP_EQ, 0); 4680 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4681 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4682 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1); 4683 4684 // Test success with prefer ipv6 4685 config_free_lines(config_port_valid); config_port_valid = NULL; 4686 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4687 smartlist_clear(slout); 4688 config_port_valid = mock_config_line("SOCKSPort", 4689 "42 IPv6Traffic PreferIPv6"); 4690 ret = port_parse_config(slout, config_port_valid, "SOCKS", 4691 CONN_TYPE_AP_LISTENER, "127.0.0.42", 0, 4692 CL_PORT_TAKES_HOSTNAMES); 4693 tt_int_op(ret, OP_EQ, 0); 4694 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4695 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4696 tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1); 4697 4698 // Test success with cache ipv4 DNS 4699 config_free_lines(config_port_valid); config_port_valid = NULL; 4700 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4701 smartlist_clear(slout); 4702 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS"); 4703 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4704 "127.0.0.42", 0, 0); 4705 tt_int_op(ret, OP_EQ, 0); 4706 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4707 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4708 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1); 4709 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0); 4710 4711 // Test success with cache ipv6 DNS 4712 config_free_lines(config_port_valid); config_port_valid = NULL; 4713 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4714 smartlist_clear(slout); 4715 config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS"); 4716 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4717 "127.0.0.42", 0, 0); 4718 tt_int_op(ret, OP_EQ, 0); 4719 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4720 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4721 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0); 4722 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1); 4723 4724 // Test success with no cache ipv4 DNS 4725 config_free_lines(config_port_valid); config_port_valid = NULL; 4726 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4727 smartlist_clear(slout); 4728 config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS"); 4729 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4730 "127.0.0.42", 0, 0); 4731 tt_int_op(ret, OP_EQ, 0); 4732 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4733 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4734 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0); 4735 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0); 4736 4737 // Test success with cache DNS 4738 config_free_lines(config_port_valid); config_port_valid = NULL; 4739 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4740 smartlist_clear(slout); 4741 config_port_valid = mock_config_line("DNSPort", "42 CacheDNS"); 4742 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4743 "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES); 4744 tt_int_op(ret, OP_EQ, 0); 4745 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4746 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4747 tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1); 4748 tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1); 4749 4750 // Test success with use cached ipv4 DNS 4751 config_free_lines(config_port_valid); config_port_valid = NULL; 4752 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4753 smartlist_clear(slout); 4754 config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache"); 4755 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4756 "127.0.0.42", 0, 0); 4757 tt_int_op(ret, OP_EQ, 0); 4758 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4759 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4760 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1); 4761 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0); 4762 4763 // Test success with use cached ipv6 DNS 4764 config_free_lines(config_port_valid); config_port_valid = NULL; 4765 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4766 smartlist_clear(slout); 4767 config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache"); 4768 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4769 "127.0.0.42", 0, 0); 4770 tt_int_op(ret, OP_EQ, 0); 4771 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4772 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4773 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0); 4774 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1); 4775 4776 // Test success with use cached DNS 4777 config_free_lines(config_port_valid); config_port_valid = NULL; 4778 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4779 smartlist_clear(slout); 4780 config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache"); 4781 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4782 "127.0.0.42", 0, 0); 4783 tt_int_op(ret, OP_EQ, 0); 4784 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4785 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4786 tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1); 4787 tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1); 4788 4789 // Test success with not preferring ipv6 automap 4790 config_free_lines(config_port_valid); config_port_valid = NULL; 4791 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4792 smartlist_clear(slout); 4793 config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap"); 4794 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4795 "127.0.0.42", 0, 0); 4796 tt_int_op(ret, OP_EQ, 0); 4797 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4798 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4799 tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0); 4800 4801 // Test success with prefer SOCKS no auth 4802 config_free_lines(config_port_valid); config_port_valid = NULL; 4803 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4804 smartlist_clear(slout); 4805 config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth"); 4806 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4807 "127.0.0.42", 0, 0); 4808 tt_int_op(ret, OP_EQ, 0); 4809 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4810 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4811 tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1); 4812 4813 // Test failure with both a zero port and a non-zero port 4814 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4815 config_free_lines(config_port_valid); config_port_valid = NULL; 4816 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4817 smartlist_clear(slout); 4818 config_port_invalid = mock_config_line("DNSPort", "0"); 4819 config_port_valid = mock_config_line("DNSPort", "42"); 4820 config_port_invalid->next = config_port_valid; 4821 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 4822 "127.0.0.42", 0, 0); 4823 tt_int_op(ret, OP_EQ, -1); 4824 4825 // Test success with warn non-local control 4826 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4827 smartlist_clear(slout); 4828 ret = port_parse_config(slout, config_port_valid, "Control", 4829 CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0, 4830 CL_PORT_WARN_NONLOCAL); 4831 tt_int_op(ret, OP_EQ, 0); 4832 4833 // Test success with warn non-local listener 4834 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4835 smartlist_clear(slout); 4836 ret = port_parse_config(slout, config_port_valid, "ExtOR", 4837 CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0, 4838 CL_PORT_WARN_NONLOCAL); 4839 tt_int_op(ret, OP_EQ, 0); 4840 4841 // Test success with warn non-local other 4842 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4843 smartlist_clear(slout); 4844 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4845 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL); 4846 tt_int_op(ret, OP_EQ, 0); 4847 4848 // Test success with warn non-local other without out 4849 ret = port_parse_config(NULL, config_port_valid, "DNS", 0, 4850 "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL); 4851 tt_int_op(ret, OP_EQ, 0); 4852 4853 // Test success with both ipv4 and ipv6 but without stream options 4854 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4855 config_port_valid = NULL; 4856 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4857 smartlist_clear(slout); 4858 config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic " 4859 "IPv6Traffic"); 4860 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4861 "127.0.0.44", 0, 4862 CL_PORT_TAKES_HOSTNAMES | 4863 CL_PORT_NO_STREAM_OPTIONS); 4864 tt_int_op(ret, OP_EQ, 0); 4865 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4866 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4867 tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1); 4868 tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1); 4869 4870 // Test failure for a SessionGroup argument with invalid value 4871 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4872 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4873 smartlist_clear(slout); 4874 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid"); 4875 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 4876 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS); 4877 tt_int_op(ret, OP_EQ, -1); 4878 4879 // Test failure for a SessionGroup argument with valid value but with no 4880 // stream options allowed 4881 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4882 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4883 smartlist_clear(slout); 4884 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123"); 4885 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 4886 "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS); 4887 tt_int_op(ret, OP_EQ, -1); 4888 4889 // Test failure for more than one SessionGroup argument 4890 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4891 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4892 smartlist_clear(slout); 4893 config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 " 4894 "SessionGroup=321"); 4895 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 4896 "127.0.0.44", 0, 0); 4897 tt_int_op(ret, OP_EQ, -1); 4898 4899 // Test success with a sessiongroup options 4900 config_free_lines(config_port_valid); config_port_valid = NULL; 4901 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4902 smartlist_clear(slout); 4903 config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122"); 4904 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4905 "127.0.0.44", 0, 0); 4906 tt_int_op(ret, OP_EQ, 0); 4907 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4908 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4909 tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122); 4910 4911 // Test success with a zero unix domain socket, and doesn't add it to out 4912 config_free_lines(config_port_valid); config_port_valid = NULL; 4913 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4914 smartlist_clear(slout); 4915 config_port_valid = mock_config_line("DNSPort", "0"); 4916 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4917 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET); 4918 tt_int_op(ret, OP_EQ, 0); 4919 tt_int_op(smartlist_len(slout), OP_EQ, 0); 4920 4921 // Test success with a one unix domain socket, and doesn't add it to out 4922 config_free_lines(config_port_valid); config_port_valid = NULL; 4923 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4924 smartlist_clear(slout); 4925 config_port_valid = mock_config_line("DNSPort", "something"); 4926 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4927 "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET); 4928 tt_int_op(ret, OP_EQ, 0); 4929 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4930 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4931 tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1); 4932 tt_str_op(port_cfg->unix_addr, OP_EQ, "something"); 4933 4934 // Test success with a port of auto - it uses the default address 4935 config_free_lines(config_port_valid); config_port_valid = NULL; 4936 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4937 smartlist_clear(slout); 4938 config_port_valid = mock_config_line("DNSPort", "auto"); 4939 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4940 "127.0.0.46", 0, 0); 4941 tt_int_op(ret, OP_EQ, 0); 4942 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4943 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4944 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT); 4945 tor_addr_parse(&addr, "127.0.0.46"); 4946 tt_assert(tor_addr_eq(&port_cfg->addr, &addr)); 4947 4948 // Test success with a port of auto in mixed case 4949 config_free_lines(config_port_valid); config_port_valid = NULL; 4950 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4951 smartlist_clear(slout); 4952 config_port_valid = mock_config_line("DNSPort", "AuTo"); 4953 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4954 "127.0.0.46", 0, 0); 4955 tt_int_op(ret, OP_EQ, 0); 4956 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4957 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4958 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT); 4959 tor_addr_parse(&addr, "127.0.0.46"); 4960 tt_assert(tor_addr_eq(&port_cfg->addr, &addr)); 4961 4962 // Test success with parsing both an address and an auto port 4963 config_free_lines(config_port_valid); config_port_valid = NULL; 4964 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4965 smartlist_clear(slout); 4966 config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto"); 4967 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4968 "127.0.0.46", 0, 0); 4969 tt_int_op(ret, OP_EQ, 0); 4970 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4971 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4972 tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT); 4973 tor_addr_parse(&addr, "127.0.0.122"); 4974 tt_assert(tor_addr_eq(&port_cfg->addr, &addr)); 4975 4976 // Test failure when asked to parse an invalid address followed by auto 4977 config_free_lines(config_port_invalid); config_port_invalid = NULL; 4978 config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto"); 4979 MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs); 4980 ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, 4981 "127.0.0.46", 0, 0); 4982 UNMOCK(tor_addr_lookup); 4983 tt_int_op(ret, OP_EQ, -1); 4984 4985 // Test success with parsing both an address and a real port 4986 config_free_lines(config_port_valid); config_port_valid = NULL; 4987 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 4988 smartlist_clear(slout); 4989 config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656"); 4990 ret = port_parse_config(slout, config_port_valid, "DNS", 0, 4991 "127.0.0.46", 0, 0); 4992 tt_int_op(ret, OP_EQ, 0); 4993 tt_int_op(smartlist_len(slout), OP_EQ, 1); 4994 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 4995 tt_int_op(port_cfg->port, OP_EQ, 656); 4996 tor_addr_parse(&addr, "127.0.0.123"); 4997 tt_assert(tor_addr_eq(&port_cfg->addr, &addr)); 4998 4999 // Test failure if we can't parse anything at all 5000 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5001 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5002 smartlist_clear(slout); 5003 config_port_invalid = mock_config_line("DNSPort", "something wrong"); 5004 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 5005 "127.0.0.46", 0, 0); 5006 tt_int_op(ret, OP_EQ, -1); 5007 5008 // Test failure if we find both an address, a port and an auto 5009 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5010 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5011 smartlist_clear(slout); 5012 config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto"); 5013 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, 5014 "127.0.0.46", 0, 0); 5015 tt_int_op(ret, OP_EQ, -1); 5016 5017 // Test that default to group writeable default sets group writeable for 5018 // domain socket 5019 config_free_lines(config_port_valid); config_port_valid = NULL; 5020 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5021 smartlist_clear(slout); 5022 config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere"); 5023 ret = port_parse_config(slout, config_port_valid, "SOCKS", 5024 CONN_TYPE_AP_LISTENER, "127.0.0.46", 0, 5025 CL_PORT_DFLT_GROUP_WRITABLE); 5026 #ifdef _WIN32 5027 tt_int_op(ret, OP_EQ, -1); 5028 #else 5029 tt_int_op(ret, OP_EQ, 0); 5030 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5031 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 5032 tt_int_op(port_cfg->is_group_writable, OP_EQ, 1); 5033 #endif /* defined(_WIN32) */ 5034 5035 done: 5036 unmock_hostname_resolver(); 5037 if (slout) 5038 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5039 smartlist_free(slout); 5040 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5041 config_free_lines(config_port_valid); config_port_valid = NULL; 5042 } 5043 5044 static void 5045 test_config_parse_port_config__ports__server_options(void *data) 5046 { 5047 (void)data; 5048 int ret; 5049 smartlist_t *slout = NULL; 5050 port_cfg_t *port_cfg = NULL; 5051 config_line_t *config_port_invalid = NULL, *config_port_valid = NULL; 5052 5053 slout = smartlist_new(); 5054 5055 // Test success with NoAdvertise option 5056 config_free_lines(config_port_valid); config_port_valid = NULL; 5057 config_port_valid = mock_config_line("DNSPort", 5058 "127.0.0.124:656 NoAdvertise"); 5059 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0, 5060 CL_PORT_SERVER_OPTIONS); 5061 tt_int_op(ret, OP_EQ, 0); 5062 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5063 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 5064 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1); 5065 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0); 5066 5067 // Test success with NoListen option 5068 config_free_lines(config_port_valid); config_port_valid = NULL; 5069 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5070 smartlist_clear(slout); 5071 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen"); 5072 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0, 5073 CL_PORT_SERVER_OPTIONS); 5074 tt_int_op(ret, OP_EQ, 0); 5075 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5076 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 5077 tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0); 5078 tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1); 5079 5080 // Test failure with both NoAdvertise and NoListen option 5081 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5082 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5083 smartlist_clear(slout); 5084 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen " 5085 "NoAdvertise"); 5086 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL, 5087 0, CL_PORT_SERVER_OPTIONS); 5088 tt_int_op(ret, OP_EQ, -1); 5089 5090 // Test success with IPv4Only 5091 config_free_lines(config_port_valid); config_port_valid = NULL; 5092 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5093 smartlist_clear(slout); 5094 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only"); 5095 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0, 5096 CL_PORT_SERVER_OPTIONS); 5097 tt_int_op(ret, OP_EQ, 0); 5098 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5099 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 5100 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1); 5101 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0); 5102 5103 // Test success with IPv6Only 5104 config_free_lines(config_port_valid); config_port_valid = NULL; 5105 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5106 smartlist_clear(slout); 5107 config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only"); 5108 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0, 5109 CL_PORT_SERVER_OPTIONS); 5110 tt_int_op(ret, OP_EQ, 0); 5111 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5112 port_cfg = (port_cfg_t *)smartlist_get(slout, 0); 5113 tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0); 5114 tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1); 5115 5116 // Test failure with both IPv4Only and IPv6Only 5117 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5118 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5119 smartlist_clear(slout); 5120 config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only " 5121 "IPv4Only"); 5122 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL, 5123 0, CL_PORT_SERVER_OPTIONS); 5124 tt_int_op(ret, OP_EQ, -1); 5125 5126 // Test success with invalid parameter 5127 config_free_lines(config_port_valid); config_port_valid = NULL; 5128 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5129 smartlist_clear(slout); 5130 config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown"); 5131 ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0, 5132 CL_PORT_SERVER_OPTIONS); 5133 tt_int_op(ret, OP_EQ, 0); 5134 tt_int_op(smartlist_len(slout), OP_EQ, 1); 5135 5136 // Test failure when asked to bind only to ipv6 but gets an ipv4 address 5137 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5138 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5139 smartlist_clear(slout); 5140 config_port_invalid = mock_config_line("DNSPort", 5141 "127.0.0.124:656 IPv6Only"); 5142 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL, 5143 0, CL_PORT_SERVER_OPTIONS); 5144 tt_int_op(ret, OP_EQ, -1); 5145 5146 // Test failure when asked to bind only to ipv4 but gets an ipv6 address 5147 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5148 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5149 smartlist_clear(slout); 5150 config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only"); 5151 ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL, 5152 0, CL_PORT_SERVER_OPTIONS); 5153 tt_int_op(ret, OP_EQ, -1); 5154 5155 // Check for failure with empty unix: address. 5156 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5157 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5158 smartlist_clear(slout); 5159 config_port_invalid = mock_config_line("ORPort", "unix:\"\""); 5160 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, NULL, 5161 0, CL_PORT_SERVER_OPTIONS); 5162 tt_int_op(ret, OP_EQ, -1); 5163 5164 /* Default address is IPv4 but pass IPv6Only flag. Should be ignored. */ 5165 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5166 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5167 smartlist_clear(slout); 5168 config_port_invalid = mock_config_line("ORPort", "9050 IPv6Only"); 5169 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, 5170 "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS); 5171 tt_int_op(ret, OP_EQ, 0); 5172 5173 /* Default address is IPv6 but pass IPv4Only flag. Should be ignored. */ 5174 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5175 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5176 smartlist_clear(slout); 5177 config_port_invalid = mock_config_line("ORPort", "9050 IPv4Only"); 5178 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, 5179 "[::]", 0, CL_PORT_SERVER_OPTIONS); 5180 tt_int_op(ret, OP_EQ, 0); 5181 5182 /* Explicit address is IPv6 but pass IPv4Only flag. Should error. */ 5183 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5184 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5185 smartlist_clear(slout); 5186 config_port_invalid = mock_config_line("ORPort", 5187 "[4242::4242]:9050 IPv4Only"); 5188 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, 5189 "[::]", 0, CL_PORT_SERVER_OPTIONS); 5190 tt_int_op(ret, OP_EQ, -1); 5191 5192 /* Explicit address is IPv4 but pass IPv6Only flag. Should error. */ 5193 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5194 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5195 smartlist_clear(slout); 5196 config_port_invalid = mock_config_line("ORPort", 5197 "1.2.3.4:9050 IPv6Only"); 5198 ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, 5199 "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS); 5200 tt_int_op(ret, OP_EQ, -1); 5201 5202 done: 5203 if (slout) 5204 SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf)); 5205 smartlist_free(slout); 5206 config_free_lines(config_port_invalid); config_port_invalid = NULL; 5207 config_free_lines(config_port_valid); config_port_valid = NULL; 5208 } 5209 5210 static void 5211 test_config_get_first_advertised(void *data) 5212 { 5213 (void)data; 5214 int r, w=0, n=0; 5215 char *msg=NULL; 5216 or_options_t *opts = options_new(); 5217 int port; 5218 const tor_addr_t *addr; 5219 5220 // no ports are configured? We get NULL. 5221 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER, 5222 AF_INET); 5223 tt_int_op(port, OP_EQ, 0); 5224 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER, 5225 AF_INET); 5226 tt_ptr_op(addr, OP_EQ, NULL); 5227 5228 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER, 5229 AF_INET6); 5230 tt_int_op(port, OP_EQ, 0); 5231 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER, 5232 AF_INET6); 5233 tt_ptr_op(addr, OP_EQ, NULL); 5234 5235 config_line_append(&opts->ORPort_lines, "ORPort", "[1234::5678]:8080"); 5236 config_line_append(&opts->ORPort_lines, "ORPort", 5237 "1.2.3.4:9999 noadvertise"); 5238 config_line_append(&opts->ORPort_lines, "ORPort", 5239 "5.6.7.8:9911 nolisten"); 5240 5241 r = parse_ports(opts, 0, &msg, &n, &w); 5242 tt_assert(r == 0); 5243 5244 // UNSPEC gets us nothing. 5245 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER, 5246 AF_UNSPEC); 5247 tt_int_op(port, OP_EQ, 0); 5248 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER, 5249 AF_UNSPEC); 5250 tt_ptr_op(addr, OP_EQ, NULL); 5251 5252 // Try AF_INET. 5253 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER, 5254 AF_INET); 5255 tt_int_op(port, OP_EQ, 9911); 5256 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER, 5257 AF_INET); 5258 tt_ptr_op(addr, OP_NE, NULL); 5259 tt_str_op(fmt_addrport(addr,port), OP_EQ, "5.6.7.8:9911"); 5260 5261 // Try AF_INET6 5262 port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER, 5263 AF_INET6); 5264 tt_int_op(port, OP_EQ, 8080); 5265 addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER, 5266 AF_INET6); 5267 tt_ptr_op(addr, OP_NE, NULL); 5268 tt_str_op(fmt_addrport(addr,port), OP_EQ, "[1234::5678]:8080"); 5269 5270 done: 5271 or_options_free(opts); 5272 config_free_all(); 5273 } 5274 5275 static void 5276 test_config_parse_log_severity(void *data) 5277 { 5278 int ret; 5279 const char *severity_log_lines[] = { 5280 "debug file /tmp/debug.log", 5281 "debug\tfile /tmp/debug.log", 5282 "[handshake]debug [~net,~mm]info notice stdout", 5283 "[handshake]debug\t[~net,~mm]info\tnotice\tstdout", 5284 NULL 5285 }; 5286 int i; 5287 log_severity_list_t *severity; 5288 5289 (void) data; 5290 5291 severity = tor_malloc(sizeof(log_severity_list_t)); 5292 for (i = 0; severity_log_lines[i]; i++) { 5293 memset(severity, 0, sizeof(log_severity_list_t)); 5294 ret = parse_log_severity_config(&severity_log_lines[i], severity); 5295 tt_int_op(ret, OP_EQ, 0); 5296 } 5297 5298 done: 5299 tor_free(severity); 5300 } 5301 5302 static void 5303 test_config_include_limit(void *data) 5304 { 5305 (void)data; 5306 5307 config_line_t *result = NULL; 5308 char *torrc_path = NULL; 5309 char *dir = tor_strdup(get_fname("test_include_limit")); 5310 tt_ptr_op(dir, OP_NE, NULL); 5311 5312 #ifdef _WIN32 5313 tt_int_op(mkdir(dir), OP_EQ, 0); 5314 #else 5315 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5316 #endif 5317 5318 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir); 5319 char torrc_contents[1000]; 5320 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s", 5321 torrc_path); 5322 tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0); 5323 5324 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL), 5325 OP_EQ, -1); 5326 5327 done: 5328 config_free_lines(result); 5329 tor_free(torrc_path); 5330 tor_free(dir); 5331 } 5332 5333 static void 5334 test_config_include_does_not_exist(void *data) 5335 { 5336 (void)data; 5337 5338 config_line_t *result = NULL; 5339 char *dir = tor_strdup(get_fname("test_include_does_not_exist")); 5340 char *missing_path = NULL; 5341 tt_ptr_op(dir, OP_NE, NULL); 5342 5343 #ifdef _WIN32 5344 tt_int_op(mkdir(dir), OP_EQ, 0); 5345 #else 5346 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5347 #endif 5348 5349 tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir); 5350 char torrc_contents[1000]; 5351 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s", 5352 missing_path); 5353 5354 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL), 5355 OP_EQ, -1); 5356 5357 done: 5358 config_free_lines(result); 5359 tor_free(dir); 5360 tor_free(missing_path); 5361 } 5362 5363 static void 5364 test_config_include_error_in_included_file(void *data) 5365 { 5366 (void)data; 5367 config_line_t *result = NULL; 5368 5369 char *dir = tor_strdup(get_fname("test_error_in_included_file")); 5370 char *invalid_path = NULL; 5371 tt_ptr_op(dir, OP_NE, NULL); 5372 5373 #ifdef _WIN32 5374 tt_int_op(mkdir(dir), OP_EQ, 0); 5375 #else 5376 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5377 #endif 5378 5379 tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir); 5380 tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0); 5381 5382 char torrc_contents[1000]; 5383 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s", 5384 invalid_path); 5385 5386 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL), 5387 OP_EQ, -1); 5388 5389 done: 5390 config_free_lines(result); 5391 tor_free(dir); 5392 tor_free(invalid_path); 5393 } 5394 5395 static void 5396 test_config_include_empty_file_folder(void *data) 5397 { 5398 (void)data; 5399 config_line_t *result = NULL; 5400 5401 char *folder_path = NULL; 5402 char *file_path = NULL; 5403 char *dir = tor_strdup(get_fname("test_include_empty_file_folder")); 5404 tt_ptr_op(dir, OP_NE, NULL); 5405 5406 #ifdef _WIN32 5407 tt_int_op(mkdir(dir), OP_EQ, 0); 5408 #else 5409 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5410 #endif 5411 5412 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir); 5413 #ifdef _WIN32 5414 tt_int_op(mkdir(folder_path), OP_EQ, 0); 5415 #else 5416 tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0); 5417 #endif 5418 tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir); 5419 tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0); 5420 5421 char torrc_contents[1000]; 5422 tor_snprintf(torrc_contents, sizeof(torrc_contents), 5423 "%%include %s\n" 5424 "%%include %s\n", 5425 folder_path, file_path); 5426 5427 int include_used; 5428 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used, 5429 NULL), OP_EQ, 0); 5430 tt_ptr_op(result, OP_EQ, NULL); 5431 tt_int_op(include_used, OP_EQ, 1); 5432 5433 done: 5434 config_free_lines(result); 5435 tor_free(folder_path); 5436 tor_free(file_path); 5437 tor_free(dir); 5438 } 5439 5440 #ifndef _WIN32 5441 static void 5442 test_config_include_no_permission(void *data) 5443 { 5444 (void)data; 5445 config_line_t *result = NULL; 5446 5447 char *folder_path = NULL; 5448 char *dir = NULL; 5449 if (geteuid() == 0) 5450 tt_skip(); 5451 5452 dir = tor_strdup(get_fname("test_include_forbidden_folder")); 5453 tt_ptr_op(dir, OP_NE, NULL); 5454 5455 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5456 5457 tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir); 5458 tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0); 5459 5460 char torrc_contents[1000]; 5461 tor_snprintf(torrc_contents, sizeof(torrc_contents), 5462 "%%include %s\n", 5463 folder_path); 5464 5465 int include_used; 5466 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, 5467 &include_used, NULL), 5468 OP_EQ, -1); 5469 tt_ptr_op(result, OP_EQ, NULL); 5470 5471 done: 5472 config_free_lines(result); 5473 tor_free(folder_path); 5474 if (dir) 5475 chmod(dir, 0700); 5476 tor_free(dir); 5477 } 5478 #endif /* !defined(_WIN32) */ 5479 5480 static void 5481 test_config_include_recursion_before_after(void *data) 5482 { 5483 (void)data; 5484 5485 config_line_t *result = NULL; 5486 char *torrc_path = NULL; 5487 char *dir = tor_strdup(get_fname("test_include_recursion_before_after")); 5488 tt_ptr_op(dir, OP_NE, NULL); 5489 5490 #ifdef _WIN32 5491 tt_int_op(mkdir(dir), OP_EQ, 0); 5492 #else 5493 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5494 #endif 5495 5496 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir); 5497 5498 char file_contents[1000]; 5499 const int limit = MAX_INCLUDE_RECURSION_LEVEL; 5500 int i; 5501 // Loop backwards so file_contents has the contents of the first file by the 5502 // end of the loop 5503 for (i = limit; i > 0; i--) { 5504 if (i < limit) { 5505 tor_snprintf(file_contents, sizeof(file_contents), 5506 "Test %d\n" 5507 "%%include %s%d\n" 5508 "Test %d\n", 5509 i, torrc_path, i + 1, 2 * limit - i); 5510 } else { 5511 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i); 5512 } 5513 5514 if (i > 1) { 5515 char *file_path = NULL; 5516 tor_asprintf(&file_path, "%s%d", torrc_path, i); 5517 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0); 5518 tor_free(file_path); 5519 } 5520 } 5521 5522 int include_used; 5523 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used, 5524 NULL), OP_EQ, 0); 5525 tt_ptr_op(result, OP_NE, NULL); 5526 tt_int_op(include_used, OP_EQ, 1); 5527 5528 int len = 0; 5529 config_line_t *next; 5530 for (next = result; next != NULL; next = next->next) { 5531 char expected[10]; 5532 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 5533 tt_str_op(next->key, OP_EQ, "Test"); 5534 tt_str_op(next->value, OP_EQ, expected); 5535 len++; 5536 } 5537 tt_int_op(len, OP_EQ, 2 * limit - 1); 5538 5539 done: 5540 config_free_lines(result); 5541 tor_free(dir); 5542 tor_free(torrc_path); 5543 } 5544 5545 static void 5546 test_config_include_recursion_after_only(void *data) 5547 { 5548 (void)data; 5549 5550 config_line_t *result = NULL; 5551 char *torrc_path = NULL; 5552 char *dir = tor_strdup(get_fname("test_include_recursion_after_only")); 5553 tt_ptr_op(dir, OP_NE, NULL); 5554 5555 #ifdef _WIN32 5556 tt_int_op(mkdir(dir), OP_EQ, 0); 5557 #else 5558 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5559 #endif 5560 5561 tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir); 5562 5563 char file_contents[1000]; 5564 const int limit = MAX_INCLUDE_RECURSION_LEVEL; 5565 int i; 5566 // Loop backwards so file_contents has the contents of the first file by the 5567 // end of the loop 5568 for (i = limit; i > 0; i--) { 5569 int n = (i - limit - 1) * -1; 5570 if (i < limit) { 5571 tor_snprintf(file_contents, sizeof(file_contents), 5572 "%%include %s%d\n" 5573 "Test %d\n", 5574 torrc_path, i + 1, n); 5575 } else { 5576 tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n); 5577 } 5578 5579 if (i > 1) { 5580 char *file_path = NULL; 5581 tor_asprintf(&file_path, "%s%d", torrc_path, i); 5582 tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0); 5583 tor_free(file_path); 5584 } 5585 } 5586 5587 int include_used; 5588 tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used, 5589 NULL), OP_EQ, 0); 5590 tt_ptr_op(result, OP_NE, NULL); 5591 tt_int_op(include_used, OP_EQ, 1); 5592 5593 int len = 0; 5594 config_line_t *next; 5595 for (next = result; next != NULL; next = next->next) { 5596 char expected[10]; 5597 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 5598 tt_str_op(next->key, OP_EQ, "Test"); 5599 tt_str_op(next->value, OP_EQ, expected); 5600 len++; 5601 } 5602 tt_int_op(len, OP_EQ, limit); 5603 5604 done: 5605 config_free_lines(result); 5606 tor_free(dir); 5607 tor_free(torrc_path); 5608 } 5609 5610 static void 5611 test_config_include_folder_order(void *data) 5612 { 5613 (void)data; 5614 5615 config_line_t *result = NULL; 5616 char *torrcd = NULL; 5617 char *path = NULL; 5618 char *path2 = NULL; 5619 char *dir = tor_strdup(get_fname("test_include_folder_order")); 5620 tt_ptr_op(dir, OP_NE, NULL); 5621 5622 #ifdef _WIN32 5623 tt_int_op(mkdir(dir), OP_EQ, 0); 5624 #else 5625 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5626 #endif 5627 5628 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d"); 5629 5630 #ifdef _WIN32 5631 tt_int_op(mkdir(torrcd), OP_EQ, 0); 5632 #else 5633 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0); 5634 #endif 5635 5636 // test that files in subfolders are ignored 5637 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder"); 5638 5639 #ifdef _WIN32 5640 tt_int_op(mkdir(path), OP_EQ, 0); 5641 #else 5642 tt_int_op(mkdir(path, 0700), OP_EQ, 0); 5643 #endif 5644 5645 tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore"); 5646 tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0); 5647 tor_free(path); 5648 5649 // test that files starting with . are ignored 5650 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot"); 5651 tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0); 5652 tor_free(path); 5653 5654 // test file order 5655 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st"); 5656 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0); 5657 tor_free(path); 5658 5659 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd"); 5660 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0); 5661 tor_free(path); 5662 5663 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd"); 5664 tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0); 5665 tor_free(path); 5666 5667 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th"); 5668 tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0); 5669 tor_free(path); 5670 5671 char torrc_contents[1000]; 5672 tor_snprintf(torrc_contents, sizeof(torrc_contents), 5673 "%%include %s\n", 5674 torrcd); 5675 5676 int include_used; 5677 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 5678 NULL), OP_EQ, 0); 5679 tt_ptr_op(result, OP_NE, NULL); 5680 tt_int_op(include_used, OP_EQ, 1); 5681 5682 int len = 0; 5683 config_line_t *next; 5684 for (next = result; next != NULL; next = next->next) { 5685 char expected[10]; 5686 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 5687 tt_str_op(next->key, OP_EQ, "Test"); 5688 tt_str_op(next->value, OP_EQ, expected); 5689 len++; 5690 } 5691 tt_int_op(len, OP_EQ, 4); 5692 5693 done: 5694 config_free_lines(result); 5695 tor_free(torrcd); 5696 tor_free(path); 5697 tor_free(path2); 5698 tor_free(dir); 5699 } 5700 5701 static void 5702 test_config_include_blank_file_last(void *data) 5703 { 5704 (void)data; 5705 5706 config_line_t *result = NULL; 5707 char *torrcd = NULL; 5708 char *path = NULL; 5709 char *dir = tor_strdup(get_fname("test_include_blank_file_last")); 5710 tt_ptr_op(dir, OP_NE, NULL); 5711 5712 #ifdef _WIN32 5713 tt_int_op(mkdir(dir), OP_EQ, 0); 5714 #else 5715 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5716 #endif 5717 5718 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d"); 5719 5720 #ifdef _WIN32 5721 tt_int_op(mkdir(torrcd), OP_EQ, 0); 5722 #else 5723 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0); 5724 #endif 5725 5726 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_1st"); 5727 tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0); 5728 tor_free(path); 5729 5730 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "bb_2nd"); 5731 tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0); 5732 tor_free(path); 5733 5734 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "cc_comment"); 5735 tt_int_op(write_str_to_file(path, "# comment only\n", 0), OP_EQ, 0); 5736 tor_free(path); 5737 5738 char torrc_contents[1000]; 5739 tor_snprintf(torrc_contents, sizeof(torrc_contents), 5740 "%%include %s\n" 5741 "Test 3\n", 5742 torrcd); 5743 5744 int include_used; 5745 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 5746 NULL), OP_EQ, 0); 5747 tt_ptr_op(result, OP_NE, NULL); 5748 tt_int_op(include_used, OP_EQ, 1); 5749 5750 int len = 0; 5751 config_line_t *next; 5752 for (next = result; next != NULL; next = next->next) { 5753 char expected[10]; 5754 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 5755 tt_str_op(next->key, OP_EQ, "Test"); 5756 tt_str_op(next->value, OP_EQ, expected); 5757 len++; 5758 } 5759 tt_int_op(len, OP_EQ, 3); 5760 5761 done: 5762 config_free_lines(result); 5763 tor_free(torrcd); 5764 tor_free(path); 5765 tor_free(dir); 5766 } 5767 5768 static void 5769 test_config_include_path_syntax(void *data) 5770 { 5771 (void)data; 5772 5773 config_line_t *result = NULL; 5774 char *dir = tor_strdup(get_fname("test_include_path_syntax")); 5775 char *esc_dir = NULL, *dir_with_pathsep = NULL, 5776 *esc_dir_with_pathsep = NULL, *torrc_contents = NULL; 5777 tt_ptr_op(dir, OP_NE, NULL); 5778 5779 #ifdef _WIN32 5780 tt_int_op(mkdir(dir), OP_EQ, 0); 5781 #else 5782 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5783 #endif 5784 5785 esc_dir = esc_for_log(dir); 5786 tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR); 5787 esc_dir_with_pathsep = esc_for_log(dir_with_pathsep); 5788 5789 tor_asprintf(&torrc_contents, 5790 "%%include %s\n" 5791 "%%include %s%s \n" // space to avoid suppressing newline 5792 "%%include %s\n", 5793 esc_dir, 5794 dir, PATH_SEPARATOR, 5795 esc_dir_with_pathsep); 5796 5797 int include_used; 5798 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used, 5799 NULL), OP_EQ, 0); 5800 tt_ptr_op(result, OP_EQ, NULL); 5801 tt_int_op(include_used, OP_EQ, 1); 5802 5803 done: 5804 config_free_lines(result); 5805 tor_free(dir); 5806 tor_free(torrc_contents); 5807 tor_free(esc_dir); 5808 tor_free(dir_with_pathsep); 5809 tor_free(esc_dir_with_pathsep); 5810 } 5811 5812 static void 5813 test_config_include_not_processed(void *data) 5814 { 5815 (void)data; 5816 5817 char torrc_contents[1000] = "%include does_not_exist\n"; 5818 config_line_t *result = NULL; 5819 tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0); 5820 tt_ptr_op(result, OP_NE, NULL); 5821 5822 int len = 0; 5823 config_line_t *next; 5824 for (next = result; next != NULL; next = next->next) { 5825 tt_str_op(next->key, OP_EQ, "%include"); 5826 tt_str_op(next->value, OP_EQ, "does_not_exist"); 5827 len++; 5828 } 5829 tt_int_op(len, OP_EQ, 1); 5830 5831 done: 5832 config_free_lines(result); 5833 } 5834 5835 static void 5836 test_config_include_has_include(void *data) 5837 { 5838 (void)data; 5839 5840 config_line_t *result = NULL; 5841 char *dir = tor_strdup(get_fname("test_include_has_include")); 5842 tt_ptr_op(dir, OP_NE, NULL); 5843 5844 #ifdef _WIN32 5845 tt_int_op(mkdir(dir), OP_EQ, 0); 5846 #else 5847 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5848 #endif 5849 5850 char torrc_contents[1000] = "Test 1\n"; 5851 int include_used; 5852 5853 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used, 5854 NULL), OP_EQ, 0); 5855 tt_int_op(include_used, OP_EQ, 0); 5856 config_free_lines(result); 5857 5858 tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir); 5859 tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used, 5860 NULL), OP_EQ, 0); 5861 tt_int_op(include_used, OP_EQ, 1); 5862 5863 done: 5864 config_free_lines(result); 5865 tor_free(dir); 5866 } 5867 5868 static void 5869 test_config_include_flag_both_without(void *data) 5870 { 5871 (void)data; 5872 5873 char *errmsg = NULL; 5874 char conf_empty[1000]; 5875 tor_snprintf(conf_empty, sizeof(conf_empty), 5876 "DataDirectory %s\n", 5877 get_fname(NULL)); 5878 // test with defaults-torrc and torrc without include 5879 int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS, 5880 NULL, &errmsg); 5881 tt_int_op(ret, OP_EQ, 0); 5882 5883 const or_options_t *options = get_options(); 5884 tt_int_op(options->IncludeUsed, OP_EQ, 0); 5885 5886 done: 5887 tor_free(errmsg); 5888 config_free_all(); 5889 } 5890 5891 static void 5892 test_config_include_flag_torrc_only(void *data) 5893 { 5894 (void)data; 5895 5896 char *errmsg = NULL; 5897 char *path = NULL; 5898 char *dir = tor_strdup(get_fname("test_include_flag_torrc_only")); 5899 tt_ptr_op(dir, OP_NE, NULL); 5900 5901 #ifdef _WIN32 5902 tt_int_op(mkdir(dir), OP_EQ, 0); 5903 #else 5904 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5905 #endif 5906 5907 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy"); 5908 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0); 5909 5910 char conf_empty[1000]; 5911 tor_snprintf(conf_empty, sizeof(conf_empty), 5912 "DataDirectory %s\n", 5913 get_fname(NULL)); 5914 char conf_include[1000]; 5915 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path); 5916 5917 // test with defaults-torrc without include and torrc with include 5918 int ret = options_init_from_string(conf_empty, conf_include, 5919 CMD_RUN_UNITTESTS, NULL, &errmsg); 5920 tt_int_op(ret, OP_EQ, 0); 5921 5922 const or_options_t *options = get_options(); 5923 tt_int_op(options->IncludeUsed, OP_EQ, 1); 5924 5925 done: 5926 tor_free(errmsg); 5927 tor_free(path); 5928 tor_free(dir); 5929 config_free_all(); 5930 } 5931 5932 static void 5933 test_config_include_flag_defaults_only(void *data) 5934 { 5935 (void)data; 5936 5937 char *errmsg = NULL; 5938 char *path = NULL; 5939 char *dir = tor_strdup(get_fname("test_include_flag_defaults_only")); 5940 tt_ptr_op(dir, OP_NE, NULL); 5941 5942 #ifdef _WIN32 5943 tt_int_op(mkdir(dir), OP_EQ, 0); 5944 #else 5945 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5946 #endif 5947 5948 tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy"); 5949 tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0); 5950 5951 char conf_empty[1000]; 5952 tor_snprintf(conf_empty, sizeof(conf_empty), 5953 "DataDirectory %s\n", 5954 get_fname(NULL)); 5955 char conf_include[1000]; 5956 tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path); 5957 5958 // test with defaults-torrc with include and torrc without include 5959 int ret = options_init_from_string(conf_include, conf_empty, 5960 CMD_RUN_UNITTESTS, NULL, &errmsg); 5961 tt_int_op(ret, OP_EQ, 0); 5962 5963 const or_options_t *options = get_options(); 5964 tt_int_op(options->IncludeUsed, OP_EQ, 0); 5965 5966 done: 5967 tor_free(errmsg); 5968 tor_free(path); 5969 tor_free(dir); 5970 config_free_all(); 5971 } 5972 5973 static void 5974 test_config_include_wildcards(void *data) 5975 { 5976 (void)data; 5977 5978 char *temp = NULL, *folder = NULL; 5979 config_line_t *result = NULL; 5980 char *dir = tor_strdup(get_fname("test_include_wildcards")); 5981 tt_ptr_op(dir, OP_NE, NULL); 5982 5983 #ifdef _WIN32 5984 tt_int_op(mkdir(dir), OP_EQ, 0); 5985 #else 5986 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 5987 #endif 5988 5989 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "01_one.conf"); 5990 tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0); 5991 tor_free(temp); 5992 5993 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "02_two.conf"); 5994 tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0); 5995 tor_free(temp); 5996 5997 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "aa_three.conf"); 5998 tt_int_op(write_str_to_file(temp, "Test 3\n", 0), OP_EQ, 0); 5999 tor_free(temp); 6000 6001 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "foo"); 6002 tt_int_op(write_str_to_file(temp, "Test 6\n", 0), OP_EQ, 0); 6003 tor_free(temp); 6004 6005 tor_asprintf(&folder, "%s"PATH_SEPARATOR"%s", dir, "folder"); 6006 6007 #ifdef _WIN32 6008 tt_int_op(mkdir(folder), OP_EQ, 0); 6009 #else 6010 tt_int_op(mkdir(folder, 0700), OP_EQ, 0); 6011 #endif 6012 6013 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "04_four.conf"); 6014 tt_int_op(write_str_to_file(temp, "Test 4\n", 0), OP_EQ, 0); 6015 tor_free(temp); 6016 6017 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "05_five.conf"); 6018 tt_int_op(write_str_to_file(temp, "Test 5\n", 0), OP_EQ, 0); 6019 tor_free(temp); 6020 6021 char torrc_contents[1000]; 6022 int include_used; 6023 6024 // test pattern that matches no file 6025 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6026 "%%include %s"PATH_SEPARATOR"not-exist*\n", 6027 dir); 6028 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6029 NULL), OP_EQ, 0); 6030 tt_ptr_op(result, OP_EQ, NULL); 6031 tt_int_op(include_used, OP_EQ, 1); 6032 config_free_lines(result); 6033 6034 #ifndef _WIN32 6035 // test wildcard escaping 6036 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6037 "%%include %s"PATH_SEPARATOR"\\*\n", 6038 dir); 6039 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6040 NULL), OP_EQ, -1); 6041 tt_ptr_op(result, OP_EQ, NULL); 6042 tt_int_op(include_used, OP_EQ, 1); 6043 config_free_lines(result); 6044 #endif /* !defined(_WIN32) */ 6045 6046 // test pattern *.conf 6047 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6048 "%%include %s"PATH_SEPARATOR"*.conf\n", 6049 dir); 6050 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6051 NULL), OP_EQ, 0); 6052 tt_ptr_op(result, OP_NE, NULL); 6053 tt_int_op(include_used, OP_EQ, 1); 6054 6055 int len = 0; 6056 config_line_t *next; 6057 char expected[10]; 6058 for (next = result; next != NULL; next = next->next) { 6059 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 6060 tt_str_op(next->key, OP_EQ, "Test"); 6061 tt_str_op(next->value, OP_EQ, expected); 6062 len++; 6063 } 6064 tt_int_op(len, OP_EQ, 3); 6065 config_free_lines(result); 6066 6067 // test pattern that matches folder and files 6068 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6069 "%%include %s"PATH_SEPARATOR"*\n", 6070 dir); 6071 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6072 NULL), OP_EQ, 0); 6073 tt_ptr_op(result, OP_NE, NULL); 6074 tt_int_op(include_used, OP_EQ, 1); 6075 6076 len = 0; 6077 for (next = result; next != NULL; next = next->next) { 6078 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 6079 tt_str_op(next->key, OP_EQ, "Test"); 6080 tt_str_op(next->value, OP_EQ, expected); 6081 len++; 6082 } 6083 tt_int_op(len, OP_EQ, 6); 6084 config_free_lines(result); 6085 6086 // test pattern ending in PATH_SEPARATOR, test linux path separator 6087 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6088 "%%include %s/f*/\n", 6089 dir); 6090 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6091 NULL), OP_EQ, 0); 6092 tt_ptr_op(result, OP_NE, NULL); 6093 tt_int_op(include_used, OP_EQ, 1); 6094 6095 len = 0; 6096 for (next = result; next != NULL; next = next->next) { 6097 tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3); 6098 tt_str_op(next->key, OP_EQ, "Test"); 6099 tt_str_op(next->value, OP_EQ, expected); 6100 len++; 6101 } 6102 tt_int_op(len, OP_EQ, 2); 6103 config_free_lines(result); 6104 6105 // test pattern with wildcards in folder and file 6106 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6107 "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*.conf\n", 6108 dir); 6109 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6110 NULL), OP_EQ, 0); 6111 tt_ptr_op(result, OP_NE, NULL); 6112 tt_int_op(include_used, OP_EQ, 1); 6113 6114 len = 0; 6115 for (next = result; next != NULL; next = next->next) { 6116 tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3); 6117 tt_str_op(next->key, OP_EQ, "Test"); 6118 tt_str_op(next->value, OP_EQ, expected); 6119 len++; 6120 } 6121 tt_int_op(len, OP_EQ, 2); 6122 config_free_lines(result); 6123 6124 done: 6125 config_free_lines(result); 6126 tor_free(folder); 6127 tor_free(temp); 6128 tor_free(dir); 6129 } 6130 6131 static void 6132 test_config_include_hidden(void *data) 6133 { 6134 (void)data; 6135 6136 char *temp = NULL, *folder = NULL; 6137 config_line_t *result = NULL; 6138 char *dir = tor_strdup(get_fname("test_include_hidden")); 6139 tt_ptr_op(dir, OP_NE, NULL); 6140 6141 #ifdef _WIN32 6142 tt_int_op(mkdir(dir), OP_EQ, 0); 6143 #else 6144 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 6145 #endif 6146 6147 tor_asprintf(&folder, "%s"PATH_SEPARATOR"%s", dir, ".dotdir"); 6148 6149 #ifdef _WIN32 6150 tt_int_op(mkdir(folder), OP_EQ, 0); 6151 #else 6152 tt_int_op(mkdir(folder, 0700), OP_EQ, 0); 6153 #endif 6154 6155 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, ".dotfile"); 6156 tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0); 6157 tor_free(temp); 6158 6159 tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "file"); 6160 tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0); 6161 tor_free(temp); 6162 6163 char torrc_contents[1000]; 6164 int include_used; 6165 int len = 0; 6166 config_line_t *next; 6167 char expected[10]; 6168 6169 // test wildcards do not expand to dot folders (except for windows) 6170 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6171 "%%include %s"PATH_SEPARATOR"*\n", 6172 dir); 6173 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6174 NULL), OP_EQ, 0); 6175 tt_int_op(include_used, OP_EQ, 1); 6176 #ifdef _WIN32 // wildcard expansion includes dot files on Windows 6177 for (next = result; next != NULL; next = next->next) { 6178 tor_snprintf(expected, sizeof(expected), "%d", len + 2); 6179 tt_str_op(next->key, OP_EQ, "Test"); 6180 tt_str_op(next->value, OP_EQ, expected); 6181 len++; 6182 } 6183 tt_int_op(len, OP_EQ, 1); 6184 #else /* !defined(_WIN32) */ 6185 tt_ptr_op(result, OP_EQ, NULL); 6186 #endif /* defined(_WIN32) */ 6187 config_free_lines(result); 6188 6189 // test wildcards match hidden folders when explicitly in the pattern 6190 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6191 "%%include %s"PATH_SEPARATOR".*\n", 6192 dir); 6193 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6194 NULL), OP_EQ, 0); 6195 tt_ptr_op(result, OP_NE, NULL); 6196 tt_int_op(include_used, OP_EQ, 1); 6197 6198 len = 0; 6199 for (next = result; next != NULL; next = next->next) { 6200 tor_snprintf(expected, sizeof(expected), "%d", len + 2); 6201 tt_str_op(next->key, OP_EQ, "Test"); 6202 tt_str_op(next->value, OP_EQ, expected); 6203 len++; 6204 } 6205 tt_int_op(len, OP_EQ, 1); 6206 config_free_lines(result); 6207 6208 // test hidden dir when explicitly included 6209 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6210 "%%include %s"PATH_SEPARATOR".dotdir\n", 6211 dir); 6212 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6213 NULL), OP_EQ, 0); 6214 tt_ptr_op(result, OP_NE, NULL); 6215 tt_int_op(include_used, OP_EQ, 1); 6216 6217 len = 0; 6218 for (next = result; next != NULL; next = next->next) { 6219 tor_snprintf(expected, sizeof(expected), "%d", len + 2); 6220 tt_str_op(next->key, OP_EQ, "Test"); 6221 tt_str_op(next->value, OP_EQ, expected); 6222 len++; 6223 } 6224 tt_int_op(len, OP_EQ, 1); 6225 config_free_lines(result); 6226 6227 // test hidden file when explicitly included 6228 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6229 "%%include %s"PATH_SEPARATOR".dotdir"PATH_SEPARATOR".dotfile\n", 6230 dir); 6231 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6232 NULL), OP_EQ, 0); 6233 tt_ptr_op(result, OP_NE, NULL); 6234 tt_int_op(include_used, OP_EQ, 1); 6235 6236 len = 0; 6237 for (next = result; next != NULL; next = next->next) { 6238 tor_snprintf(expected, sizeof(expected), "%d", len + 1); 6239 tt_str_op(next->key, OP_EQ, "Test"); 6240 tt_str_op(next->value, OP_EQ, expected); 6241 len++; 6242 } 6243 tt_int_op(len, OP_EQ, 1); 6244 config_free_lines(result); 6245 6246 done: 6247 config_free_lines(result); 6248 tor_free(folder); 6249 tor_free(temp); 6250 tor_free(dir); 6251 } 6252 6253 static void 6254 test_config_dup_and_filter(void *arg) 6255 { 6256 (void)arg; 6257 /* Test normal input. */ 6258 config_line_t *line = NULL; 6259 config_line_append(&line, "abc", "def"); 6260 config_line_append(&line, "ghi", "jkl"); 6261 config_line_append(&line, "ABCD", "mno"); 6262 6263 config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc"); 6264 tt_ptr_op(line_dup, OP_NE, NULL); 6265 tt_ptr_op(line_dup->next, OP_NE, NULL); 6266 tt_ptr_op(line_dup->next->next, OP_EQ, NULL); 6267 6268 tt_str_op(line_dup->key, OP_EQ, "abc"); 6269 tt_str_op(line_dup->value, OP_EQ, "def"); 6270 tt_str_op(line_dup->next->key, OP_EQ, "ABCD"); 6271 tt_str_op(line_dup->next->value, OP_EQ, "mno"); 6272 6273 /* empty output */ 6274 config_free_lines(line_dup); 6275 line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf"); 6276 tt_ptr_op(line_dup, OP_EQ, NULL); 6277 6278 /* empty input */ 6279 config_free_lines(line_dup); 6280 line_dup = config_lines_dup_and_filter(NULL, "abc"); 6281 tt_ptr_op(line_dup, OP_EQ, NULL); 6282 6283 done: 6284 config_free_lines(line); 6285 config_free_lines(line_dup); 6286 } 6287 6288 /* If we're not configured to be a bridge, but we set 6289 * BridgeDistribution, then options_validate () should return -1. */ 6290 static void 6291 test_config_check_bridge_distribution_setting_not_a_bridge(void *arg) 6292 { 6293 or_options_t* options = get_options_mutable(); 6294 or_options_t* old_options = options; 6295 char* message = NULL; 6296 int ret; 6297 6298 (void)arg; 6299 6300 options->BridgeRelay = 0; 6301 options->BridgeDistribution = (char*)("https"); 6302 6303 ret = options_validate(old_options, options, &message); 6304 6305 tt_int_op(ret, OP_EQ, -1); 6306 tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you " 6307 "didn't set BridgeRelay!"); 6308 done: 6309 tor_free(message); 6310 options->BridgeDistribution = NULL; 6311 } 6312 6313 /* If the BridgeDistribution setting was valid, 0 should be returned. */ 6314 static void 6315 test_config_check_bridge_distribution_setting_valid(void *arg) 6316 { 6317 (void)arg; 6318 6319 // Check all the possible values we support right now. 6320 tt_int_op(check_bridge_distribution_setting("none"), OP_EQ, 0); 6321 tt_int_op(check_bridge_distribution_setting("any"), OP_EQ, 0); 6322 tt_int_op(check_bridge_distribution_setting("https"), OP_EQ, 0); 6323 tt_int_op(check_bridge_distribution_setting("email"), OP_EQ, 0); 6324 tt_int_op(check_bridge_distribution_setting("settings"), OP_EQ, 0); 6325 6326 // Check all the possible values we support right now with weird casing. 6327 tt_int_op(check_bridge_distribution_setting("NoNe"), OP_EQ, 0); 6328 tt_int_op(check_bridge_distribution_setting("anY"), OP_EQ, 0); 6329 tt_int_op(check_bridge_distribution_setting("hTTps"), OP_EQ, 0); 6330 tt_int_op(check_bridge_distribution_setting("emAIl"), OP_EQ, 0); 6331 tt_int_op(check_bridge_distribution_setting("setTIngS"), OP_EQ, 0); 6332 6333 // Invalid values. 6334 tt_int_op(check_bridge_distribution_setting("x\rx"), OP_EQ, -1); 6335 tt_int_op(check_bridge_distribution_setting("x\nx"), OP_EQ, -1); 6336 tt_int_op(check_bridge_distribution_setting("\t\t\t"), OP_EQ, -1); 6337 6338 done: 6339 return; 6340 } 6341 6342 /* If the BridgeDistribution setting was invalid, -1 should be returned. */ 6343 static void 6344 test_config_check_bridge_distribution_setting_invalid(void *arg) 6345 { 6346 int ret = check_bridge_distribution_setting("hyphens-are-allowed"); 6347 6348 (void)arg; 6349 6350 tt_int_op(ret, OP_EQ, 0); 6351 6352 ret = check_bridge_distribution_setting("asterisks*are*forbidden"); 6353 6354 tt_int_op(ret, OP_EQ, -1); 6355 done: 6356 return; 6357 } 6358 6359 /* If the BridgeDistribution setting was unrecognised, a warning should be 6360 * logged and 0 should be returned. */ 6361 static void 6362 test_config_check_bridge_distribution_setting_unrecognised(void *arg) 6363 { 6364 int ret = check_bridge_distribution_setting("unicorn"); 6365 6366 (void)arg; 6367 6368 tt_int_op(ret, OP_EQ, 0); 6369 done: 6370 return; 6371 } 6372 6373 static void 6374 test_config_include_opened_file_list(void *data) 6375 { 6376 (void)data; 6377 6378 config_line_t *result = NULL; 6379 smartlist_t *opened_files = smartlist_new(); 6380 char *torrcd = NULL; 6381 char *subfolder = NULL; 6382 char *in_subfolder = NULL; 6383 char *empty = NULL; 6384 char *file = NULL; 6385 char *dot = NULL; 6386 char *dir = tor_strdup(get_fname("test_include_opened_file_list")); 6387 tt_ptr_op(dir, OP_NE, NULL); 6388 6389 #ifdef _WIN32 6390 tt_int_op(mkdir(dir), OP_EQ, 0); 6391 #else 6392 tt_int_op(mkdir(dir, 0700), OP_EQ, 0); 6393 #endif 6394 6395 tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d"); 6396 6397 #ifdef _WIN32 6398 tt_int_op(mkdir(torrcd), OP_EQ, 0); 6399 #else 6400 tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0); 6401 #endif 6402 6403 tor_asprintf(&subfolder, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder"); 6404 6405 #ifdef _WIN32 6406 tt_int_op(mkdir(subfolder), OP_EQ, 0); 6407 #else 6408 tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0); 6409 #endif 6410 6411 tor_asprintf(&in_subfolder, "%s"PATH_SEPARATOR"%s", subfolder, 6412 "01_file_in_subfolder"); 6413 tt_int_op(write_str_to_file(in_subfolder, "Test 1\n", 0), OP_EQ, 0); 6414 6415 tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty"); 6416 tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0); 6417 6418 tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file"); 6419 tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0); 6420 6421 tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot"); 6422 tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0); 6423 6424 char torrc_contents[1000]; 6425 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6426 "%%include %s\n", 6427 torrcd); 6428 6429 int include_used; 6430 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6431 opened_files), OP_EQ, 0); 6432 tt_ptr_op(result, OP_NE, NULL); 6433 tt_int_op(include_used, OP_EQ, 1); 6434 6435 tt_int_op(smartlist_len(opened_files), OP_EQ, 4); 6436 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1); 6437 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1); 6438 // files inside subfolders are not opened, only the subfolder is opened 6439 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1); 6440 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1); 6441 // dot files are not opened as we ignore them when we get their name from 6442 // their parent folder 6443 6444 // test with wildcards 6445 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f)); 6446 smartlist_clear(opened_files); 6447 config_free_lines(result); 6448 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6449 "%%include %s"PATH_SEPARATOR"*\n", 6450 torrcd); 6451 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6452 opened_files), OP_EQ, 0); 6453 tt_ptr_op(result, OP_NE, NULL); 6454 tt_int_op(include_used, OP_EQ, 1); 6455 6456 #ifdef _WIN32 6457 tt_int_op(smartlist_len(opened_files), OP_EQ, 6); 6458 #else 6459 tt_int_op(smartlist_len(opened_files), OP_EQ, 5); 6460 #endif 6461 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1); 6462 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1); 6463 // * will match the subfolder inside torrc.d, so it will be included 6464 tt_int_op(smartlist_contains_string(opened_files, in_subfolder), OP_EQ, 1); 6465 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1); 6466 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1); 6467 #ifdef _WIN32 6468 // * matches the dot file on Windows 6469 tt_int_op(smartlist_contains_string(opened_files, dot), OP_EQ, 1); 6470 #endif 6471 6472 // test with wildcards in folder and file 6473 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f)); 6474 smartlist_clear(opened_files); 6475 config_free_lines(result); 6476 tor_snprintf(torrc_contents, sizeof(torrc_contents), 6477 "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*\n", 6478 torrcd); 6479 tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, 6480 opened_files), OP_EQ, 0); 6481 tt_ptr_op(result, OP_NE, NULL); 6482 tt_int_op(include_used, OP_EQ, 1); 6483 6484 #ifdef _WIN32 6485 tt_int_op(smartlist_len(opened_files), OP_EQ, 6); 6486 #else 6487 tt_int_op(smartlist_len(opened_files), OP_EQ, 5); 6488 #endif 6489 tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1); 6490 tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1); 6491 tt_int_op(smartlist_contains_string(opened_files, in_subfolder), OP_EQ, 1); 6492 // stat is called on the following files, so they count as opened 6493 tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1); 6494 tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1); 6495 #ifdef _WIN32 6496 // * matches the dot file on Windows 6497 tt_int_op(smartlist_contains_string(opened_files, dot), OP_EQ, 1); 6498 #endif 6499 6500 done: 6501 SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f)); 6502 smartlist_free(opened_files); 6503 config_free_lines(result); 6504 tor_free(torrcd); 6505 tor_free(subfolder); 6506 tor_free(in_subfolder); 6507 tor_free(empty); 6508 tor_free(file); 6509 tor_free(dot); 6510 tor_free(dir); 6511 } 6512 6513 static void 6514 test_config_compute_max_mem_in_queues(void *data) 6515 { 6516 #define GIGABYTE(x) (UINT64_C(x) << 30) 6517 #define MEGABYTE(x) (UINT64_C(x) << 20) 6518 (void)data; 6519 MOCK(get_total_system_memory, get_total_system_memory_mock); 6520 6521 /* We are unable to detect the amount of memory on the system. Tor will try 6522 * to use some sensible default values for 64-bit and 32-bit systems. */ 6523 total_system_memory_return = -1; 6524 6525 #if SIZEOF_VOID_P >= 8 6526 /* We are on a 64-bit system. */ 6527 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8)); 6528 #else 6529 /* We are on a 32-bit system. */ 6530 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1)); 6531 #endif /* SIZEOF_VOID_P >= 8 */ 6532 6533 /* We are able to detect the amount of RAM on the system. */ 6534 total_system_memory_return = 0; 6535 6536 /* We are running on a system with one gigabyte of RAM. */ 6537 total_system_memory_output = GIGABYTE(1); 6538 6539 /* We have 0.75 * RAM available. */ 6540 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, 6541 3 * (GIGABYTE(1) / 4)); 6542 6543 /* We are running on a tiny machine with 256 MB of RAM. */ 6544 total_system_memory_output = MEGABYTE(256); 6545 6546 /* We will now enforce a minimum of 256 MB of RAM available for the 6547 * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192 6548 * MB available. */ 6549 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256)); 6550 6551 #if SIZEOF_SIZE_T > 4 6552 /* We are running on a machine with 8 GB of RAM. */ 6553 total_system_memory_output = GIGABYTE(8); 6554 6555 /* We will have 0.4 * RAM available. */ 6556 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, 6557 2 * (GIGABYTE(8) / 5)); 6558 6559 /* We are running on a machine with 16 GB of RAM. */ 6560 total_system_memory_output = GIGABYTE(16); 6561 6562 /* We will have 0.4 * RAM available. */ 6563 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, 6564 2 * (GIGABYTE(16) / 5)); 6565 6566 /* We are running on a machine with 32 GB of RAM. */ 6567 total_system_memory_output = GIGABYTE(32); 6568 6569 /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */ 6570 tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, 6571 MAX_DEFAULT_MEMORY_QUEUE_SIZE); 6572 #endif /* SIZEOF_SIZE_T > 4 */ 6573 6574 done: 6575 UNMOCK(get_total_system_memory); 6576 6577 #undef GIGABYTE 6578 #undef MEGABYTE 6579 } 6580 6581 static void 6582 test_config_extended_fmt(void *arg) 6583 { 6584 (void)arg; 6585 config_line_t *lines = NULL, *lp; 6586 const char string1[] = 6587 "thing1 is here\n" 6588 "+thing2 is over here\n" 6589 "/thing3\n" 6590 "/thing4 is back here\n"; 6591 6592 /* Try with the "extended" flag disabled. */ 6593 int r = config_get_lines(string1, &lines, 0); 6594 tt_int_op(r, OP_EQ, 0); 6595 lp = lines; 6596 tt_ptr_op(lp, OP_NE, NULL); 6597 tt_str_op(lp->key, OP_EQ, "thing1"); 6598 tt_str_op(lp->value, OP_EQ, "is here"); 6599 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL); 6600 lp = lp->next; 6601 tt_ptr_op(lp, OP_NE, NULL); 6602 tt_str_op(lp->key, OP_EQ, "+thing2"); 6603 tt_str_op(lp->value, OP_EQ, "is over here"); 6604 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL); 6605 lp = lp->next; 6606 tt_ptr_op(lp, OP_NE, NULL); 6607 tt_str_op(lp->key, OP_EQ, "/thing3"); 6608 tt_str_op(lp->value, OP_EQ, ""); 6609 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL); 6610 lp = lp->next; 6611 tt_ptr_op(lp, OP_NE, NULL); 6612 tt_str_op(lp->key, OP_EQ, "/thing4"); 6613 tt_str_op(lp->value, OP_EQ, "is back here"); 6614 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL); 6615 lp = lp->next; 6616 tt_assert(!lp); 6617 config_free_lines(lines); 6618 6619 /* Try with the "extended" flag enabled. */ 6620 r = config_get_lines(string1, &lines, 1); 6621 tt_int_op(r, OP_EQ, 0); 6622 lp = lines; 6623 tt_ptr_op(lp, OP_NE, NULL); 6624 tt_str_op(lp->key, OP_EQ, "thing1"); 6625 tt_str_op(lp->value, OP_EQ, "is here"); 6626 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL); 6627 lp = lp->next; 6628 tt_ptr_op(lp, OP_NE, NULL); 6629 tt_str_op(lp->key, OP_EQ, "thing2"); 6630 tt_str_op(lp->value, OP_EQ, "is over here"); 6631 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_APPEND); 6632 lp = lp->next; 6633 tt_ptr_op(lp, OP_NE, NULL); 6634 tt_str_op(lp->key, OP_EQ, "thing3"); 6635 tt_str_op(lp->value, OP_EQ, ""); 6636 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR); 6637 lp = lp->next; 6638 tt_ptr_op(lp, OP_NE, NULL); 6639 tt_str_op(lp->key, OP_EQ, "thing4"); 6640 tt_str_op(lp->value, OP_EQ, ""); 6641 tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR); 6642 lp = lp->next; 6643 tt_assert(!lp); 6644 6645 done: 6646 config_free_lines(lines); 6647 } 6648 6649 static void 6650 test_config_kvline_parse(void *arg) 6651 { 6652 (void)arg; 6653 6654 config_line_t *lines = NULL; 6655 char *enc = NULL; 6656 6657 lines = kvline_parse("A=B CD=EF", 0); 6658 tt_assert(lines); 6659 tt_str_op(lines->key, OP_EQ, "A"); 6660 tt_str_op(lines->value, OP_EQ, "B"); 6661 tt_str_op(lines->next->key, OP_EQ, "CD"); 6662 tt_str_op(lines->next->value, OP_EQ, "EF"); 6663 enc = kvline_encode(lines, 0); 6664 tt_str_op(enc, OP_EQ, "A=B CD=EF"); 6665 tor_free(enc); 6666 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS); 6667 tt_str_op(enc, OP_EQ, "A=B CD=EF"); 6668 tor_free(enc); 6669 config_free_lines(lines); 6670 6671 lines = kvline_parse("AB CDE=F", 0); 6672 tt_assert(! lines); 6673 6674 lines = kvline_parse("AB CDE=F", KV_OMIT_KEYS); 6675 tt_assert(lines); 6676 tt_str_op(lines->key, OP_EQ, ""); 6677 tt_str_op(lines->value, OP_EQ, "AB"); 6678 tt_str_op(lines->next->key, OP_EQ, "CDE"); 6679 tt_str_op(lines->next->value, OP_EQ, "F"); 6680 tt_assert(lines); 6681 enc = kvline_encode(lines, 0); 6682 tt_assert(!enc); 6683 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS); 6684 tt_str_op(enc, OP_EQ, "AB CDE=F"); 6685 tor_free(enc); 6686 config_free_lines(lines); 6687 6688 lines = kvline_parse("AB=C CDE=\"F G\"", 0); 6689 tt_assert(!lines); 6690 6691 lines = kvline_parse("AB=C CDE=\"F G\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS); 6692 tt_assert(lines); 6693 tt_str_op(lines->key, OP_EQ, "AB"); 6694 tt_str_op(lines->value, OP_EQ, "C"); 6695 tt_str_op(lines->next->key, OP_EQ, "CDE"); 6696 tt_str_op(lines->next->value, OP_EQ, "F G"); 6697 tt_str_op(lines->next->next->key, OP_EQ, ""); 6698 tt_str_op(lines->next->next->value, OP_EQ, "GHI"); 6699 enc = kvline_encode(lines, 0); 6700 tt_assert(!enc); 6701 enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS); 6702 tt_str_op(enc, OP_EQ, "AB=C CDE=\"F G\" GHI"); 6703 tor_free(enc); 6704 config_free_lines(lines); 6705 6706 lines = kvline_parse("A\"B=C CDE=\"F\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS); 6707 tt_assert(! lines); 6708 6709 lines = kvline_parse("AB=", KV_QUOTED); 6710 tt_assert(lines); 6711 tt_str_op(lines->key, OP_EQ, "AB"); 6712 tt_str_op(lines->value, OP_EQ, ""); 6713 config_free_lines(lines); 6714 6715 lines = kvline_parse("AB=", 0); 6716 tt_assert(lines); 6717 tt_str_op(lines->key, OP_EQ, "AB"); 6718 tt_str_op(lines->value, OP_EQ, ""); 6719 config_free_lines(lines); 6720 6721 lines = kvline_parse("AB=", KV_OMIT_VALS); 6722 tt_assert(lines); 6723 tt_str_op(lines->key, OP_EQ, "AB"); 6724 tt_str_op(lines->value, OP_EQ, ""); 6725 config_free_lines(lines); 6726 6727 lines = kvline_parse(" AB ", KV_OMIT_VALS); 6728 tt_assert(lines); 6729 tt_str_op(lines->key, OP_EQ, "AB"); 6730 tt_str_op(lines->value, OP_EQ, ""); 6731 config_free_lines(lines); 6732 6733 lines = kvline_parse("AB", KV_OMIT_VALS); 6734 tt_assert(lines); 6735 tt_str_op(lines->key, OP_EQ, "AB"); 6736 tt_str_op(lines->value, OP_EQ, ""); 6737 enc = kvline_encode(lines, KV_OMIT_VALS); 6738 tt_str_op(enc, OP_EQ, "AB"); 6739 tor_free(enc); 6740 config_free_lines(lines); 6741 6742 lines = kvline_parse("AB=CD", KV_OMIT_VALS); 6743 tt_assert(lines); 6744 tt_str_op(lines->key, OP_EQ, "AB"); 6745 tt_str_op(lines->value, OP_EQ, "CD"); 6746 enc = kvline_encode(lines, KV_OMIT_VALS); 6747 tt_str_op(enc, OP_EQ, "AB=CD"); 6748 tor_free(enc); 6749 config_free_lines(lines); 6750 6751 lines = kvline_parse("AB=CD DE FGH=I", KV_OMIT_VALS); 6752 tt_assert(lines); 6753 tt_str_op(lines->key, OP_EQ, "AB"); 6754 tt_str_op(lines->value, OP_EQ, "CD"); 6755 tt_str_op(lines->next->key, OP_EQ, "DE"); 6756 tt_str_op(lines->next->value, OP_EQ, ""); 6757 tt_str_op(lines->next->next->key, OP_EQ, "FGH"); 6758 tt_str_op(lines->next->next->value, OP_EQ, "I"); 6759 enc = kvline_encode(lines, KV_OMIT_VALS); 6760 tt_str_op(enc, OP_EQ, "AB=CD DE FGH=I"); 6761 tor_free(enc); 6762 config_free_lines(lines); 6763 6764 lines = kvline_parse("AB=\"CD E\" DE FGH=\"I\"", KV_OMIT_VALS|KV_QUOTED); 6765 tt_assert(lines); 6766 tt_str_op(lines->key, OP_EQ, "AB"); 6767 tt_str_op(lines->value, OP_EQ, "CD E"); 6768 tt_str_op(lines->next->key, OP_EQ, "DE"); 6769 tt_str_op(lines->next->value, OP_EQ, ""); 6770 tt_str_op(lines->next->next->key, OP_EQ, "FGH"); 6771 tt_str_op(lines->next->next->value, OP_EQ, "I"); 6772 enc = kvline_encode(lines, KV_OMIT_VALS|KV_QUOTED); 6773 tt_str_op(enc, OP_EQ, "AB=\"CD E\" DE FGH=I"); 6774 tor_free(enc); 6775 config_free_lines(lines); 6776 6777 lines = kvline_parse("AB=CD \"EF=GH\"", KV_OMIT_KEYS|KV_QUOTED); 6778 tt_assert(lines); 6779 tt_str_op(lines->key, OP_EQ, "AB"); 6780 tt_str_op(lines->value, OP_EQ, "CD"); 6781 tt_str_op(lines->next->key, OP_EQ, ""); 6782 tt_str_op(lines->next->value, OP_EQ, "EF=GH"); 6783 enc = kvline_encode(lines, KV_OMIT_KEYS); 6784 tt_assert(!enc); 6785 enc = kvline_encode(lines, KV_OMIT_KEYS|KV_QUOTED); 6786 tt_assert(enc); 6787 tt_str_op(enc, OP_EQ, "AB=CD \"EF=GH\""); 6788 tor_free(enc); 6789 config_free_lines(lines); 6790 6791 lines = tor_malloc_zero(sizeof(*lines)); 6792 lines->key = tor_strdup("A=B"); 6793 lines->value = tor_strdup("CD"); 6794 enc = kvline_encode(lines, 0); 6795 tt_assert(!enc); 6796 config_free_lines(lines); 6797 6798 config_line_append(&lines, "A", "B C"); 6799 enc = kvline_encode(lines, 0); 6800 tt_assert(!enc); 6801 enc = kvline_encode(lines, KV_RAW); 6802 tt_assert(enc); 6803 tt_str_op(enc, OP_EQ, "A=B C"); 6804 6805 done: 6806 config_free_lines(lines); 6807 tor_free(enc); 6808 } 6809 6810 static void 6811 test_config_getinfo_config_names(void *arg) 6812 { 6813 (void)arg; 6814 char *answer = NULL; 6815 const char *error = NULL; 6816 int rv; 6817 6818 rv = getinfo_helper_config(NULL, "config/names", &answer, &error); 6819 tt_int_op(rv, OP_EQ, 0); 6820 tt_ptr_op(error, OP_EQ, NULL); 6821 6822 // ContactInfo should be listed. 6823 tt_assert(strstr(answer, "\nContactInfo String\n")); 6824 6825 // V1AuthoritativeDirectory should not be listed, since it is obsolete. 6826 tt_assert(! strstr(answer, "V1AuthoritativeDirectory")); 6827 6828 // ___UsingTestNetworkDefaults should not be listed, since it is invisible. 6829 tt_assert(! strstr(answer, "UsingTestNetworkDefaults")); 6830 6831 done: 6832 tor_free(answer); 6833 } 6834 6835 static void 6836 test_config_duplicate_orports(void *arg) 6837 { 6838 (void)arg; 6839 6840 config_line_t *config_port = NULL; 6841 smartlist_t *ports = smartlist_new(); 6842 6843 // Pretend that the user has specified an implicit 0.0.0.0:9050, an implicit 6844 // [::]:9050, and an explicit on [::1]:9050. 6845 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries. 6846 config_line_append(&config_port, "ORPort", "[::1]:9050"); 6847 6848 // Parse IPv4, then IPv6. 6849 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0", 6850 0, CL_PORT_SERVER_OPTIONS); 6851 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]", 6852 0, CL_PORT_SERVER_OPTIONS); 6853 6854 /* There should be 4 ports at this point that is: 6855 * - 0.0.0.0:9050 6856 * - [::]:9050 6857 * - [::1]:9050 6858 * - [::1]:9050 6859 */ 6860 tt_int_op(smartlist_len(ports), OP_EQ, 4); 6861 6862 /* This will remove the [::] and the extra [::1]. */ 6863 remove_duplicate_orports(ports); 6864 6865 tt_int_op(smartlist_len(ports), OP_EQ, 2); 6866 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ, 6867 "ORPort 9050"); 6868 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ, 6869 "ORPort [::1]:9050"); 6870 6871 /* Reset. Test different ORPort value. */ 6872 SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p)); 6873 smartlist_free(ports); 6874 config_free_lines(config_port); 6875 config_port = NULL; 6876 ports = smartlist_new(); 6877 6878 /* Implicit port and then specific IPv6 addresses but more than one. */ 6879 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries. 6880 config_line_append(&config_port, "ORPort", "[4242::1]:9051"); 6881 config_line_append(&config_port, "ORPort", "[4242::2]:9051"); 6882 6883 // Parse IPv4, then IPv6. 6884 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0", 6885 0, CL_PORT_SERVER_OPTIONS); 6886 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]", 6887 0, CL_PORT_SERVER_OPTIONS); 6888 6889 /* There should be 6 ports at this point that is: 6890 * - 0.0.0.0:9050 6891 * - [::]:9050 6892 * - [4242::1]:9051 6893 * - [4242::1]:9051 6894 * - [4242::2]:9051 6895 * - [4242::2]:9051 6896 */ 6897 tt_int_op(smartlist_len(ports), OP_EQ, 6); 6898 6899 /* This will remove the [::] and the duplicates. */ 6900 remove_duplicate_orports(ports); 6901 6902 /* We have four address here, 1 IPv4 on 9050, IPv6 on 9050, IPv6 on 9051 and 6903 * a different IPv6 on 9051. */ 6904 tt_int_op(smartlist_len(ports), OP_EQ, 4); 6905 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ, 6906 "ORPort 9050"); 6907 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ, 6908 "ORPort [4242::1]:9051"); 6909 tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ, 6910 "ORPort [4242::2]:9051"); 6911 tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ, 6912 "ORPort 9050"); 6913 6914 /* Reset. Test different ORPort value. */ 6915 SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p)); 6916 smartlist_free(ports); 6917 config_free_lines(config_port); 6918 config_port = NULL; 6919 ports = smartlist_new(); 6920 6921 /* Three different ports. */ 6922 config_line_append(&config_port, "ORPort", "9050"); // two implicit entries. 6923 config_line_append(&config_port, "ORPort", "[4242::1]:9051"); 6924 config_line_append(&config_port, "ORPort", "[4242::2]:9052"); 6925 6926 // Parse IPv4, then IPv6. 6927 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0", 6928 0, CL_PORT_SERVER_OPTIONS); 6929 port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "[::]", 6930 0, CL_PORT_SERVER_OPTIONS); 6931 6932 /* There should be 6 ports at this point that is: 6933 * - 0.0.0.0:9050 6934 * - [::]:9050 6935 * - [4242::1]:9051 6936 * - [4242::1]:9051 6937 * - [4242::2]:9052 6938 * - [4242::2]:9052 6939 */ 6940 tt_int_op(smartlist_len(ports), OP_EQ, 6); 6941 6942 /* This will remove the [::] and the duplicates. */ 6943 remove_duplicate_orports(ports); 6944 6945 /* We have four address here, 1 IPv4 on 9050, IPv6 on 9050, IPv6 on 9051 and 6946 * IPv6 on 9052. */ 6947 tt_int_op(smartlist_len(ports), OP_EQ, 4); 6948 tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ, 6949 "ORPort 9050"); 6950 tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ, 6951 "ORPort [4242::1]:9051"); 6952 tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ, 6953 "ORPort [4242::2]:9052"); 6954 tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ, 6955 "ORPort 9050"); 6956 6957 done: 6958 SMARTLIST_FOREACH(ports,port_cfg_t *,pf,port_cfg_free(pf)); 6959 smartlist_free(ports); 6960 config_free_lines(config_port); 6961 } 6962 6963 static void 6964 test_config_multifamily_port(void *arg) 6965 { 6966 (void) arg; 6967 6968 config_line_t *config_port = NULL; 6969 smartlist_t *ports = smartlist_new(); 6970 6971 config_line_append(&config_port, "SocksPort", "9050"); 6972 config_line_append(&config_port, "SocksPort", "[::1]:9050"); 6973 6974 // Parse IPv4, then IPv6. 6975 port_parse_config(ports, config_port, "SOCKS", CONN_TYPE_AP_LISTENER, 6976 "0.0.0.0", 9050, 0); 6977 6978 /* There should be 2 ports at this point that is: 6979 * - 0.0.0.0:9050 6980 * - [::1]:9050 6981 */ 6982 tt_int_op(smartlist_len(ports), OP_EQ, 2); 6983 6984 done: 6985 SMARTLIST_FOREACH(ports, port_cfg_t *, cfg, port_cfg_free(cfg)); 6986 smartlist_free(ports); 6987 config_free_lines(config_port); 6988 } 6989 6990 #ifndef COCCI 6991 #define CONFIG_TEST(name, flags) \ 6992 { #name, test_config_ ## name, flags, NULL, NULL } 6993 6994 #define CONFIG_TEST_SETUP(suffix, name, flags, setup, setup_data) \ 6995 { #name#suffix, test_config_ ## name, flags, setup, setup_data } 6996 #endif /* !defined(COCCI) */ 6997 6998 struct testcase_t config_tests[] = { 6999 CONFIG_TEST(adding_trusted_dir_server, TT_FORK), 7000 CONFIG_TEST(adding_fallback_dir_server, TT_FORK), 7001 CONFIG_TEST(parsing_trusted_dir_server, 0), 7002 CONFIG_TEST(parsing_invalid_dir_address, 0), 7003 CONFIG_TEST(parsing_fallback_dir_server, 0), 7004 CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK), 7005 CONFIG_TEST(adding_dir_servers, TT_FORK), 7006 CONFIG_TEST(default_dir_servers, TT_FORK), 7007 CONFIG_TEST(default_fallback_dirs, 0), 7008 CONFIG_TEST_SETUP(_v4, find_my_address, TT_FORK, 7009 &passthrough_setup, &addr_param_v4), 7010 CONFIG_TEST_SETUP(_v6, find_my_address, TT_FORK, 7011 &passthrough_setup, &addr_param_v6), 7012 CONFIG_TEST(find_my_address_mixed, TT_FORK), 7013 CONFIG_TEST(addressmap, 0), 7014 CONFIG_TEST(parse_bridge_line, 0), 7015 CONFIG_TEST(parse_transport_options_line, 0), 7016 CONFIG_TEST(parse_transport_plugin_line, TT_FORK), 7017 CONFIG_TEST(parse_tcp_proxy_line, TT_FORK), 7018 CONFIG_TEST(check_or_create_data_subdir, TT_FORK), 7019 CONFIG_TEST(write_to_data_subdir, TT_FORK), 7020 CONFIG_TEST(fix_my_family, 0), 7021 CONFIG_TEST(directory_fetch, 0), 7022 CONFIG_TEST(port_cfg_line_extract_addrport, 0), 7023 CONFIG_TEST(parse_port_config__ports__no_ports_given, 0), 7024 CONFIG_TEST(parse_port_config__ports__server_options, 0), 7025 CONFIG_TEST(parse_port_config__ports__ports_given, 0), 7026 CONFIG_TEST(get_first_advertised, TT_FORK), 7027 CONFIG_TEST(parse_log_severity, 0), 7028 CONFIG_TEST(include_limit, 0), 7029 CONFIG_TEST(include_does_not_exist, 0), 7030 CONFIG_TEST(include_error_in_included_file, 0), 7031 CONFIG_TEST(include_empty_file_folder, 0), 7032 #ifndef _WIN32 7033 CONFIG_TEST(include_no_permission, 0), 7034 #endif 7035 CONFIG_TEST(include_recursion_before_after, 0), 7036 CONFIG_TEST(include_recursion_after_only, 0), 7037 CONFIG_TEST(include_folder_order, 0), 7038 CONFIG_TEST(include_blank_file_last, 0), 7039 CONFIG_TEST(include_path_syntax, 0), 7040 CONFIG_TEST(include_not_processed, 0), 7041 CONFIG_TEST(include_has_include, 0), 7042 CONFIG_TEST(include_flag_both_without, TT_FORK), 7043 CONFIG_TEST(include_flag_torrc_only, TT_FORK), 7044 CONFIG_TEST(include_flag_defaults_only, TT_FORK), 7045 CONFIG_TEST(include_wildcards, 0), 7046 CONFIG_TEST(include_hidden, 0), 7047 CONFIG_TEST(dup_and_filter, 0), 7048 CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK), 7049 CONFIG_TEST(check_bridge_distribution_setting_valid, 0), 7050 CONFIG_TEST(check_bridge_distribution_setting_invalid, 0), 7051 CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0), 7052 CONFIG_TEST(include_opened_file_list, 0), 7053 CONFIG_TEST(compute_max_mem_in_queues, 0), 7054 CONFIG_TEST(extended_fmt, 0), 7055 CONFIG_TEST(kvline_parse, 0), 7056 CONFIG_TEST(getinfo_config_names, 0), 7057 CONFIG_TEST(duplicate_orports, 0), 7058 CONFIG_TEST(multifamily_port, 0), 7059 END_OF_TESTCASES 7060 };