LCOV - code coverage report
Current view: top level - test - test_config.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 3244 3268 99.3 %
Date: 2021-11-24 03:28:48 Functions: 77 78 98.7 %

          Line data    Source code
       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           1 : test_config_addressmap(void *arg)
      75             : {
      76           1 :   char buf[1024];
      77           1 :   char address[256];
      78           1 :   time_t expires = TIME_MAX;
      79           1 :   (void)arg;
      80             : 
      81           1 :   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           1 :   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
     102           1 :   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           1 :   strlcpy(address, "www.invalidwildcard.com", sizeof(address));
     110           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     111             : 
     112             :   /* MapAddress *invalidasterisk.com .torserver.exit  - no match */
     113           1 :   strlcpy(address, "www.invalidasterisk.com", sizeof(address));
     114           1 :   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           1 :   strlcpy(address, "reader.google.com", sizeof(address));
     119           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     120           1 :   tt_str_op(address,OP_EQ, "reader.torserver.exit");
     121             : 
     122             :   /* MapAddress *.yahoo.com *.google.com.torserver.exit */
     123           1 :   strlcpy(address, "reader.yahoo.com", sizeof(address));
     124           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     125           1 :   tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
     126             : 
     127             :   /*MapAddress *.cnn.com www.cnn.com */
     128           1 :   strlcpy(address, "cnn.com", sizeof(address));
     129           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     130           1 :   tt_str_op(address,OP_EQ, "www.cnn.com");
     131             : 
     132             :   /* MapAddress .cn.com www.cnn.com */
     133           1 :   strlcpy(address, "www.cn.com", sizeof(address));
     134           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     135           1 :   tt_str_op(address,OP_EQ, "www.cnn.com");
     136             : 
     137             :   /* MapAddress ex.com www.cnn.com  - no match */
     138           1 :   strlcpy(address, "www.ex.com", sizeof(address));
     139           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     140             : 
     141             :   /* MapAddress ey.com *.cnn.com - invalid expression */
     142           1 :   strlcpy(address, "ey.com", sizeof(address));
     143           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     144             : 
     145             :   /* Where mapping for FQDN match on FQDN */
     146           1 :   strlcpy(address, "www.google.com", sizeof(address));
     147           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     148           1 :   tt_str_op(address,OP_EQ, "3.3.3.3");
     149             : 
     150           1 :   strlcpy(address, "www.torproject.org", sizeof(address));
     151           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     152           1 :   tt_str_op(address,OP_EQ, "1.1.1.1");
     153             : 
     154           1 :   strlcpy(address, "other.torproject.org", sizeof(address));
     155           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     156           1 :   tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
     157             : 
     158           1 :   strlcpy(address, "test.torproject.org", sizeof(address));
     159           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     160           1 :   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           1 :   strlcpy(address, "www.example.org", sizeof(address));
     168           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     169           1 :   tt_str_op(address,OP_EQ, "5.5.5.5");
     170             : 
     171             :   /* Test infinite address mapping results in no change */
     172           1 :   strlcpy(address, "www.infiniteloop.org", sizeof(address));
     173           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     174           1 :   tt_str_op(address,OP_EQ, "www.infiniteloop.org");
     175             : 
     176             :   /* Test we don't find false positives */
     177           1 :   strlcpy(address, "www.example.com", sizeof(address));
     178           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     179             : 
     180             :   /* Test top-level-domain matching a bit harder */
     181           1 :   config_free_lines(get_options_mutable()->AddressMap);
     182           1 :   addressmap_clear_configured();
     183           1 :   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           1 :   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
     188           1 :   config_register_addressmaps(get_options());
     189             : 
     190           1 :   strlcpy(address, "www.abc.com", sizeof(address));
     191           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     192           1 :   tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
     193             : 
     194           1 :   strlcpy(address, "www.def.com", sizeof(address));
     195           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     196           1 :   tt_str_op(address,OP_EQ, "www.def.torserver.exit");
     197             : 
     198           1 :   strlcpy(address, "www.torproject.org", sizeof(address));
     199           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     200           1 :   tt_str_op(address,OP_EQ, "1.1.1.1");
     201             : 
     202           1 :   strlcpy(address, "test.torproject.org", sizeof(address));
     203           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     204           1 :   tt_str_op(address,OP_EQ, "1.1.1.1");
     205             : 
     206           1 :   strlcpy(address, "torproject.net", sizeof(address));
     207           1 :   tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
     208           1 :   tt_str_op(address,OP_EQ, "2.2.2.2");
     209             : 
     210             :   /* We don't support '*' as a mapping directive */
     211           1 :   config_free_lines(get_options_mutable()->AddressMap);
     212           1 :   addressmap_clear_configured();
     213           1 :   strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
     214           1 :   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
     215           1 :   config_register_addressmaps(get_options());
     216             : 
     217           1 :   strlcpy(address, "www.abc.com", sizeof(address));
     218           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     219             : 
     220           1 :   strlcpy(address, "www.def.net", sizeof(address));
     221           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     222             : 
     223           1 :   strlcpy(address, "www.torproject.org", sizeof(address));
     224           1 :   tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
     225             : 
     226             : #undef addressmap_rewrite
     227             : 
     228           1 :  done:
     229           1 :   config_free_lines(get_options_mutable()->AddressMap);
     230           1 :   get_options_mutable()->AddressMap = NULL;
     231           1 :   addressmap_free_all();
     232           1 : }
     233             : 
     234             : static int
     235           3 : is_private_dir(const char* path)
     236             : {
     237           3 :   struct stat st;
     238           3 :   int r = stat(path, &st);
     239           3 :   if (r) {
     240             :     return 0;
     241             :   }
     242             : #if !defined (_WIN32)
     243           3 :   if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
     244           1 :     return 0;
     245             :   }
     246             : #endif
     247             :   return 1;
     248             : }
     249             : 
     250             : static void
     251           1 : test_config_check_or_create_data_subdir(void *arg)
     252             : {
     253           1 :   or_options_t *options = get_options_mutable();
     254           1 :   char *datadir;
     255           1 :   const char *subdir = "test_stats";
     256           1 :   char *subpath;
     257           1 :   struct stat st;
     258           1 :   int r;
     259             : #if !defined (_WIN32)
     260           1 :   unsigned group_permission;
     261             : #endif
     262           1 :   (void)arg;
     263             : 
     264           1 :   tor_free(options->DataDirectory);
     265           1 :   datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
     266           1 :   subpath = get_datadir_fname(subdir);
     267             : 
     268             : #if defined (_WIN32)
     269             :   tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
     270             : #else
     271           1 :   tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
     272             : #endif
     273             : 
     274           1 :   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           1 :   tt_assert(r && (errno == ENOENT));
     279           1 :   tt_assert(!check_or_create_data_subdir(subdir));
     280           1 :   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           1 :   tt_assert(!check_or_create_data_subdir(subdir));
     285             : 
     286           1 :   r = stat(subpath, &st);
     287           1 :   if (r) {
     288           0 :     tt_abort_perror("stat");
     289             :   }
     290             : 
     291             : #if !defined (_WIN32)
     292           1 :   group_permission = st.st_mode | 0070;
     293           1 :   r = chmod(subpath, group_permission);
     294             : 
     295           1 :   if (r) {
     296           0 :     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           1 :   tt_assert(!is_private_dir(subpath));
     302           1 :   tt_assert(!check_or_create_data_subdir(subdir));
     303           1 :   tt_assert(is_private_dir(subpath));
     304             : #endif /* !defined (_WIN32) */
     305             : 
     306           1 :  done:
     307           1 :   rmdir(subpath);
     308           1 :   tor_free(datadir);
     309           1 :   tor_free(subpath);
     310           1 : }
     311             : 
     312             : static void
     313           1 : test_config_write_to_data_subdir(void *arg)
     314             : {
     315           1 :   or_options_t* options = get_options_mutable();
     316           1 :   char *datadir;
     317           1 :   char *cp = NULL;
     318           1 :   const char* subdir = "test_stats";
     319           1 :   const char* fname = "test_file";
     320           1 :   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           1 :   char* filepath = NULL;
     336           1 :   (void)arg;
     337             : 
     338           1 :   tor_free(options->DataDirectory);
     339           1 :   datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
     340           1 :   filepath = get_datadir_fname2(subdir, fname);
     341             : 
     342             : #if defined (_WIN32)
     343             :   tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
     344             : #else
     345           1 :   tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
     346             : #endif
     347             : 
     348             :   // Write attempt should fail, if subdirectory doesn't exist.
     349           1 :   tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
     350           1 :   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           1 :   tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
     355           1 :   cp = read_file_to_str(filepath, 0, NULL);
     356           1 :   tt_str_op(cp,OP_EQ, str);
     357           1 :   tor_free(cp);
     358             : 
     359             :   // A second write operation should overwrite the old content.
     360           1 :   tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
     361           1 :   cp = read_file_to_str(filepath, 0, NULL);
     362           1 :   tt_str_op(cp,OP_EQ, str);
     363           1 :   tor_free(cp);
     364             : 
     365           1 :  done:
     366           1 :   (void) unlink(filepath);
     367           1 :   rmdir(options->DataDirectory);
     368           1 :   tor_free(datadir);
     369           1 :   tor_free(filepath);
     370           1 :   tor_free(cp);
     371           1 : }
     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           8 : 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           8 :   char *tmp = NULL;
     382           8 :   bridge_line_t *bridge_line = parse_bridge_line(string);
     383           8 :   tt_assert(bridge_line);
     384             : 
     385             :   /* test addrport */
     386           8 :   tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
     387           8 :   tt_str_op(test_addrport,OP_EQ, tmp);
     388           8 :   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           8 :   if (test_digest && tor_digest_is_zero(bridge_line->digest))
     393           0 :     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           8 :   if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
     398           0 :     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           8 :   if (test_digest) {
     403           4 :     tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
     404           4 :     tor_strlower(tmp);
     405           4 :     tt_str_op(test_digest,OP_EQ, tmp);
     406           4 :     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           8 :   if (test_transport && !bridge_line->transport_name)
     412           0 :     tt_abort();
     413           8 :   if (!test_transport && bridge_line->transport_name)
     414           0 :     tt_abort();
     415           8 :   if (test_transport)
     416           5 :     tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
     417             : 
     418             :   /* Validate the SOCKS argument smartlist. */
     419           8 :   if (test_socks_args && !bridge_line->socks_args)
     420           0 :     tt_abort();
     421           8 :   if (!test_socks_args && bridge_line->socks_args)
     422           0 :     tt_abort();
     423           8 :   if (test_socks_args)
     424           3 :     tt_assert(smartlist_strings_eq(test_socks_args,
     425             :                                      bridge_line->socks_args));
     426             : 
     427           8 :  done:
     428           8 :   tor_free(tmp);
     429           8 :   bridge_line_free(bridge_line);
     430           8 : }
     431             : 
     432             : /* Test helper function: Make sure that a bridge line is
     433             :  * unparseable. */
     434             : static void
     435           7 : bad_bridge_line_test(const char *string)
     436             : {
     437           7 :   bridge_line_t *bridge_line = parse_bridge_line(string);
     438           7 :   if (bridge_line)
     439           0 :     TT_FAIL(("%s was supposed to fail, but it didn't.", string));
     440           7 :   tt_ptr_op(bridge_line, OP_EQ, NULL);
     441             : 
     442           7 :  done:
     443           7 :   bridge_line_free(bridge_line);
     444           7 : }
     445             : 
     446             : static void
     447           1 : test_config_parse_bridge_line(void *arg)
     448             : {
     449           1 :   (void) arg;
     450           1 :   good_bridge_line_test("192.0.2.1:4123",
     451             :                         "192.0.2.1:4123", NULL, NULL, NULL);
     452             : 
     453           1 :   good_bridge_line_test("192.0.2.1",
     454             :                         "192.0.2.1:443", NULL, NULL, NULL);
     455             : 
     456           1 :   good_bridge_line_test("transport [::1]",
     457             :                         "[::1]:443", NULL, "transport", NULL);
     458             : 
     459           1 :   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           1 :     smartlist_t *sl_tmp = smartlist_new();
     467           1 :     smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
     468             : 
     469           1 :     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           2 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     475           1 :     smartlist_free(sl_tmp);
     476             :   }
     477             : 
     478             :   {
     479           1 :     smartlist_t *sl_tmp = smartlist_new();
     480           1 :     smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
     481           1 :     smartlist_add_asprintf(sl_tmp, "z=z");
     482             : 
     483           1 :     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           3 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     487           1 :     smartlist_free(sl_tmp);
     488             :   }
     489             : 
     490             :   {
     491           1 :     smartlist_t *sl_tmp = smartlist_new();
     492           1 :     smartlist_add_asprintf(sl_tmp, "dub=come");
     493           1 :     smartlist_add_asprintf(sl_tmp, "save=me");
     494             : 
     495           1 :     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           3 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     504           1 :     smartlist_free(sl_tmp);
     505             :   }
     506             : 
     507           1 :   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           1 :   bad_bridge_line_test("");
     515             :   /* bad transport name */
     516           1 :   bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
     517             :   /* weird ip address */
     518           1 :   bad_bridge_line_test("a.b.c.d");
     519             :   /* invalid fpr */
     520           1 :   bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
     521             :   /* no k=v in the end */
     522           1 :   bad_bridge_line_test("obfs2 2.2.2.2:1231 "
     523             :                        "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
     524             :   /* no addrport */
     525           1 :   bad_bridge_line_test("asdw");
     526             :   /* huge k=v value that can't fit in SOCKS fields */
     527           1 :   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           1 : }
     538             : 
     539             : static void
     540           1 : test_config_parse_transport_options_line(void *arg)
     541             : {
     542           1 :   smartlist_t *options_sl = NULL, *sl_tmp = NULL;
     543             : 
     544           1 :   (void) arg;
     545             : 
     546             :   { /* too small line */
     547           1 :     options_sl = get_options_from_transport_options_line("valley", NULL);
     548           1 :     tt_ptr_op(options_sl, OP_EQ, NULL);
     549             :   }
     550             : 
     551             :   { /* no k=v values */
     552           1 :     options_sl = get_options_from_transport_options_line("hit it!", NULL);
     553           1 :     tt_ptr_op(options_sl, OP_EQ, NULL);
     554             :   }
     555             : 
     556             :   { /* correct line, but wrong transport specified */
     557           2 :     options_sl =
     558           1 :       get_options_from_transport_options_line("trebuchet k=v", "rook");
     559           1 :     tt_ptr_op(options_sl, OP_EQ, NULL);
     560             :   }
     561             : 
     562             :   { /* correct -- no transport specified */
     563           1 :     sl_tmp = smartlist_new();
     564           1 :     smartlist_add_asprintf(sl_tmp, "ladi=dadi");
     565           1 :     smartlist_add_asprintf(sl_tmp, "weliketo=party");
     566             : 
     567           2 :     options_sl =
     568           1 :       get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
     569             :                                               NULL);
     570           1 :     tt_assert(options_sl);
     571           1 :     tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
     572             : 
     573           3 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     574           1 :     smartlist_free(sl_tmp);
     575           1 :     sl_tmp = NULL;
     576           3 :     SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
     577           1 :     smartlist_free(options_sl);
     578           1 :     options_sl = NULL;
     579             :   }
     580             : 
     581             :   { /* correct -- correct transport specified */
     582           1 :     sl_tmp = smartlist_new();
     583           1 :     smartlist_add_asprintf(sl_tmp, "ladi=dadi");
     584           1 :     smartlist_add_asprintf(sl_tmp, "weliketo=party");
     585             : 
     586           2 :     options_sl =
     587           1 :       get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
     588             :                                               "rook");
     589           1 :     tt_assert(options_sl);
     590           1 :     tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
     591           3 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     592           1 :     smartlist_free(sl_tmp);
     593           1 :     sl_tmp = NULL;
     594           3 :     SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
     595           1 :     smartlist_free(options_sl);
     596           1 :     options_sl = NULL;
     597             :   }
     598             : 
     599           0 :  done:
     600           1 :   if (options_sl) {
     601           0 :     SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
     602           0 :     smartlist_free(options_sl);
     603             :   }
     604           1 :   if (sl_tmp) {
     605           0 :     SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
     606           0 :     smartlist_free(sl_tmp);
     607             :   }
     608           1 : }
     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           6 : get_total_system_memory_mock(size_t *mem_out)
     618             : {
     619           6 :   if (! mem_out)
     620             :     return -1;
     621             : 
     622           6 :   *mem_out = total_system_memory_output;
     623           6 :   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           2 : pt_kickstart_proxy_mock(const smartlist_t *transport_list,
     642             :                         char **proxy_argv, int is_server)
     643             : {
     644           2 :   (void) transport_list;
     645           2 :   (void) proxy_argv;
     646           2 :   (void) is_server;
     647             :   /* XXXX check that args are as expected. */
     648             : 
     649           2 :   ++pt_kickstart_proxy_mock_call_count;
     650             : 
     651           2 :   free_execve_args(proxy_argv);
     652           2 : }
     653             : 
     654             : static int
     655           1 : transport_add_from_config_mock(const tor_addr_t *addr,
     656             :                                uint16_t port, const char *name,
     657             :                                int socks_ver)
     658             : {
     659           1 :   (void) addr;
     660           1 :   (void) port;
     661           1 :   (void) name;
     662           1 :   (void) socks_ver;
     663             :   /* XXXX check that args are as expected. */
     664             : 
     665           1 :   ++transport_add_from_config_mock_call_count;
     666             : 
     667           1 :   return 0;
     668             : }
     669             : 
     670             : static int
     671           3 : transport_is_needed_mock(const char *transport_name)
     672             : {
     673           3 :   (void) transport_name;
     674             :   /* XXXX check that arg is as expected. */
     675             : 
     676           3 :   ++transport_is_needed_mock_call_count;
     677             : 
     678           3 :   return transport_is_needed_mock_return;
     679             : }
     680             : 
     681             : static void
     682           1 : test_config_parse_tcp_proxy_line(void *arg)
     683             : {
     684           1 :   (void)arg;
     685             : 
     686           1 :   int ret;
     687           1 :   char *msg = NULL;
     688           1 :   or_options_t *options = get_options_mutable();
     689             : 
     690             :   /* Bad TCPProxy line - too short. */
     691           1 :   ret = parse_tcp_proxy_line("haproxy", options, &msg);
     692             :   /* Return error. */
     693           1 :   tt_int_op(ret, OP_EQ, -1);
     694             :   /* Correct error message. */
     695           1 :   tt_str_op(msg, OP_EQ, "TCPProxy has no address/port. Please fix.");
     696             :   /* Free error message. */
     697           1 :   tor_free(msg);
     698             : 
     699             :   /* Bad TCPProxy line - unsupported protocol. */
     700           1 :   ret = parse_tcp_proxy_line("unsupported 95.216.163.36:443", options, &msg);
     701           1 :   tt_int_op(ret, OP_EQ, -1);
     702           1 :   tt_str_op(msg, OP_EQ, "TCPProxy protocol is not supported. Currently the "
     703             :                         "only supported protocol is 'haproxy'. Please fix.");
     704           1 :   tor_free(msg);
     705             : 
     706             :   /* Bad TCPProxy line - unparsable address/port. */
     707           1 :   MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
     708           1 :   ret = parse_tcp_proxy_line("haproxy bogus_address!/300", options, &msg);
     709           1 :   tt_int_op(ret, OP_EQ, -1);
     710           1 :   tt_str_op(msg, OP_EQ, "TCPProxy address/port failed to parse or resolve. "
     711             :                         "Please fix.");
     712           1 :   tor_free(msg);
     713           1 :   UNMOCK(tor_addr_lookup);
     714             : 
     715             :   /* Good TCPProxy line - ipv4. */
     716           1 :   ret = parse_tcp_proxy_line("haproxy 95.216.163.36:443", options, &msg);
     717           1 :   tt_int_op(ret, OP_EQ, 0);
     718           1 :   tt_ptr_op(msg, OP_EQ, NULL);
     719           1 :   tt_int_op(options->TCPProxyProtocol, OP_EQ, TCP_PROXY_PROTOCOL_HAPROXY);
     720             :   /* Correct the address. */
     721           1 :   tt_assert(tor_addr_eq_ipv4h(&options->TCPProxyAddr, 0x5fd8a324));
     722           1 :   tt_int_op(options->TCPProxyPort, OP_EQ, 443);
     723           1 :   tor_free(msg);
     724             : 
     725           1 :  done:
     726           1 :   UNMOCK(tor_addr_lookup);
     727           1 : }
     728             : 
     729             : /**
     730             :  * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
     731             :  * options.
     732             :  */
     733             : 
     734             : static void
     735           1 : test_config_parse_transport_plugin_line(void *arg)
     736             : {
     737           1 :   (void)arg;
     738             : 
     739           1 :   or_options_t *options = get_options_mutable();
     740           1 :   int r, tmp;
     741           1 :   int old_pt_kickstart_proxy_mock_call_count;
     742           1 :   int old_transport_add_from_config_mock_call_count;
     743           1 :   int old_transport_is_needed_mock_call_count;
     744             : 
     745             :   /* Bad transport lines - too short */
     746           1 :   r = pt_parse_transport_line(options, "bad", 1, 0);
     747           1 :   tt_int_op(r, OP_LT, 0);
     748           1 :   r = pt_parse_transport_line(options, "bad", 1, 1);
     749           1 :   tt_int_op(r, OP_LT, 0);
     750           1 :   r = pt_parse_transport_line(options, "bad bad", 1, 0);
     751           1 :   tt_int_op(r, OP_LT, 0);
     752           1 :   r = pt_parse_transport_line(options, "bad bad", 1, 1);
     753           1 :   tt_int_op(r, OP_LT, 0);
     754             : 
     755             :   /* Test transport list parsing */
     756           1 :   r = pt_parse_transport_line(options,
     757             :       "transport_1 exec /usr/bin/fake-transport", 1, 0);
     758           1 :   tt_int_op(r, OP_EQ, 0);
     759           1 :   r = pt_parse_transport_line(options,
     760             :    "transport_1 exec /usr/bin/fake-transport", 1, 1);
     761           1 :   tt_int_op(r, OP_EQ, 0);
     762           1 :   r = pt_parse_transport_line(options,
     763             :       "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
     764           1 :   tt_int_op(r, OP_EQ, 0);
     765           1 :   r = pt_parse_transport_line(options,
     766             :       "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
     767           1 :   tt_int_op(r, OP_EQ, 0);
     768             :   /* Bad transport identifiers */
     769           1 :   r = pt_parse_transport_line(options,
     770             :       "transport_* exec /usr/bin/fake-transport", 1, 0);
     771           1 :   tt_int_op(r, OP_LT, 0);
     772           1 :   r = pt_parse_transport_line(options,
     773             :       "transport_* exec /usr/bin/fake-transport", 1, 1);
     774           1 :   tt_int_op(r, OP_LT, 0);
     775             : 
     776             :   /* Check SOCKS cases for client transport */
     777           1 :   r = pt_parse_transport_line(options,
     778             :       "transport_1 socks4 1.2.3.4:567", 1, 0);
     779           1 :   tt_int_op(r, OP_EQ, 0);
     780           1 :   r = pt_parse_transport_line(options,
     781             :       "transport_1 socks5 1.2.3.4:567", 1, 0);
     782           1 :   tt_int_op(r, OP_EQ, 0);
     783             :   /* Proxy case for server transport */
     784           1 :   r = pt_parse_transport_line(options,
     785             :       "transport_1 proxy 1.2.3.4:567", 1, 1);
     786           1 :   tt_int_op(r, OP_EQ, 0);
     787             :   /* Multiple-transport error exit */
     788           1 :   r = pt_parse_transport_line(options,
     789             :       "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
     790           1 :   tt_int_op(r, OP_LT, 0);
     791           1 :   r = pt_parse_transport_line(options,
     792             :       "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
     793           1 :   tt_int_op(r, OP_LT, 0);
     794             :   /* No port error exit */
     795           1 :   r = pt_parse_transport_line(options,
     796             :       "transport_1 socks5 1.2.3.4", 1, 0);
     797           1 :   tt_int_op(r, OP_LT, 0);
     798           1 :   r = pt_parse_transport_line(options,
     799             :      "transport_1 proxy 1.2.3.4", 1, 1);
     800           1 :   tt_int_op(r, OP_LT, 0);
     801             :   /* Unparsable address error exit */
     802           1 :   r = pt_parse_transport_line(options,
     803             :       "transport_1 socks5 1.2.3:6x7", 1, 0);
     804           1 :   tt_int_op(r, OP_LT, 0);
     805           1 :   r = pt_parse_transport_line(options,
     806             :       "transport_1 proxy 1.2.3:6x7", 1, 1);
     807           1 :   tt_int_op(r, OP_LT, 0);
     808             : 
     809             :   /* "Strange {Client|Server}TransportPlugin field" error exit */
     810           1 :   r = pt_parse_transport_line(options,
     811             :       "transport_1 foo bar", 1, 0);
     812           1 :   tt_int_op(r, OP_LT, 0);
     813           1 :   r = pt_parse_transport_line(options,
     814             :       "transport_1 foo bar", 1, 1);
     815           1 :   tt_int_op(r, OP_LT, 0);
     816             : 
     817             :   /* No sandbox mode error exit */
     818           1 :   tmp = options->Sandbox;
     819           1 :   options->Sandbox = 1;
     820           1 :   r = pt_parse_transport_line(options,
     821             :       "transport_1 exec /usr/bin/fake-transport", 1, 0);
     822           1 :   tt_int_op(r, OP_LT, 0);
     823           1 :   r = pt_parse_transport_line(options,
     824             :       "transport_1 exec /usr/bin/fake-transport", 1, 1);
     825           1 :   tt_int_op(r, OP_LT, 0);
     826           1 :   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           1 :   MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
     833           1 :   old_pt_kickstart_proxy_mock_call_count =
     834             :     pt_kickstart_proxy_mock_call_count;
     835           1 :   r = pt_parse_transport_line(options,
     836             :       "transport_1 exec /usr/bin/fake-transport", 0, 1);
     837           1 :   tt_int_op(r, OP_EQ, 0);
     838           1 :   tt_assert(pt_kickstart_proxy_mock_call_count ==
     839             :       old_pt_kickstart_proxy_mock_call_count + 1);
     840           1 :   UNMOCK(pt_kickstart_proxy);
     841             : 
     842             :   /* This one hits a log line in the !validate_only case only */
     843           1 :   r = pt_parse_transport_line(options,
     844             :       "transport_1 proxy 1.2.3.4:567", 0, 1);
     845           1 :   tt_int_op(r, OP_EQ, 0);
     846             : 
     847             :   /* Check mocked client transport cases */
     848           1 :   MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
     849           1 :   MOCK(transport_add_from_config, transport_add_from_config_mock);
     850           1 :   MOCK(transport_is_needed, transport_is_needed_mock);
     851             : 
     852             :   /* Unnecessary transport case */
     853           1 :   transport_is_needed_mock_return = 0;
     854           1 :   old_pt_kickstart_proxy_mock_call_count =
     855             :     pt_kickstart_proxy_mock_call_count;
     856           1 :   old_transport_add_from_config_mock_call_count =
     857             :     transport_add_from_config_mock_call_count;
     858           1 :   old_transport_is_needed_mock_call_count =
     859             :     transport_is_needed_mock_call_count;
     860           1 :   r = pt_parse_transport_line(options,
     861             :       "transport_1 exec /usr/bin/fake-transport", 0, 0);
     862             :   /* Should have succeeded */
     863           1 :   tt_int_op(r, OP_EQ, 0);
     864             :   /* transport_is_needed() should have been called */
     865           1 :   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           1 :   tt_assert(pt_kickstart_proxy_mock_call_count ==
     872             :       old_pt_kickstart_proxy_mock_call_count);
     873           1 :   tt_assert(transport_add_from_config_mock_call_count ==
     874             :       old_transport_add_from_config_mock_call_count);
     875             : 
     876             :   /* Necessary transport case */
     877           1 :   transport_is_needed_mock_return = 1;
     878           1 :   old_pt_kickstart_proxy_mock_call_count =
     879             :     pt_kickstart_proxy_mock_call_count;
     880           1 :   old_transport_add_from_config_mock_call_count =
     881             :     transport_add_from_config_mock_call_count;
     882           1 :   old_transport_is_needed_mock_call_count =
     883             :     transport_is_needed_mock_call_count;
     884           1 :   r = pt_parse_transport_line(options,
     885             :       "transport_1 exec /usr/bin/fake-transport", 0, 0);
     886             :   /* Should have succeeded */
     887           1 :   tt_int_op(r, OP_EQ, 0);
     888             :   /*
     889             :    * transport_is_needed() and pt_kickstart_proxy() should have been
     890             :    * called.
     891             :    */
     892           1 :   tt_assert(pt_kickstart_proxy_mock_call_count ==
     893             :       old_pt_kickstart_proxy_mock_call_count + 1);
     894           1 :   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           1 :   tt_assert(transport_add_from_config_mock_call_count ==
     898             :       old_transport_add_from_config_mock_call_count);
     899             : 
     900             :   /* proxy case */
     901           1 :   transport_is_needed_mock_return = 1;
     902           1 :   old_pt_kickstart_proxy_mock_call_count =
     903             :     pt_kickstart_proxy_mock_call_count;
     904           1 :   old_transport_add_from_config_mock_call_count =
     905             :     transport_add_from_config_mock_call_count;
     906           1 :   old_transport_is_needed_mock_call_count =
     907             :     transport_is_needed_mock_call_count;
     908           1 :   r = pt_parse_transport_line(options,
     909             :       "transport_1 socks5 1.2.3.4:567", 0, 0);
     910             :   /* Should have succeeded */
     911           1 :   tt_int_op(r, OP_EQ, 0);
     912             :   /*
     913             :    * transport_is_needed() and transport_add_from_config() should have
     914             :    * been called.
     915             :    */
     916           1 :   tt_assert(transport_add_from_config_mock_call_count ==
     917             :       old_transport_add_from_config_mock_call_count + 1);
     918           1 :   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           1 :   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           1 :   UNMOCK(transport_is_needed);
     926           1 :   UNMOCK(transport_add_from_config);
     927           1 :   UNMOCK(pt_kickstart_proxy);
     928             : 
     929           1 :  done:
     930             :   /* Make sure we undo all mocks */
     931           1 :   UNMOCK(pt_kickstart_proxy);
     932           1 :   UNMOCK(transport_add_from_config);
     933           1 :   UNMOCK(transport_is_needed);
     934             : 
     935           1 :   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           1 : test_config_fix_my_family(void *arg)
     942             : {
     943           1 :   char *err = NULL;
     944           1 :   config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
     945           1 :   family->key = tor_strdup("MyFamily");
     946           1 :   family->value = tor_strdup("$1111111111111111111111111111111111111111, "
     947             :                              "1111111111111111111111111111111111111112, "
     948             :                              "$1111111111111111111111111111111111111113");
     949             : 
     950           1 :   config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
     951           1 :   family2->key = tor_strdup("MyFamily");
     952           1 :   family2->value = tor_strdup("1111111111111111111111111111111111111114");
     953             : 
     954           1 :   config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
     955           1 :   family3->key = tor_strdup("MyFamily");
     956           1 :   family3->value = tor_strdup("$1111111111111111111111111111111111111115");
     957             : 
     958           1 :   family->next = family2;
     959           1 :   family2->next = family3;
     960           1 :   family3->next = NULL;
     961             : 
     962           1 :   or_options_t* options = options_new();
     963           1 :   (void) arg;
     964             : 
     965           1 :   options_init(options);
     966           1 :   options->MyFamily_lines = family;
     967             : 
     968           1 :   options_validate(NULL, options, &err) ;
     969             : 
     970           1 :   if (err != NULL) {
     971           0 :     TT_FAIL(("options_validate failed: %s", err));
     972             :   }
     973             : 
     974           1 :   const char *valid[] = { "$1111111111111111111111111111111111111111",
     975             :                           "$1111111111111111111111111111111111111112",
     976             :                           "$1111111111111111111111111111111111111113",
     977             :                           "$1111111111111111111111111111111111111114",
     978             :                           "$1111111111111111111111111111111111111115" };
     979           1 :   int ret_size = 0;
     980           1 :   config_line_t *ret;
     981           6 :   for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
     982           5 :     tt_str_op(ret->value, OP_EQ, valid[ret_size]);
     983           5 :     ret_size++;
     984             :   }
     985           1 :   tt_int_op(ret_size, OP_EQ, 5);
     986             : 
     987           1 :  done:
     988           1 :   tor_free(err);
     989           1 :   or_options_free(options);
     990           1 : }
     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           4 : tor_addr_lookup_01010101(const char *name, uint16_t family, tor_addr_t *addr)
    1004             : {
    1005           4 :   n_hostname_01010101++;
    1006             : 
    1007           4 :   if (family == AF_INET) {
    1008           2 :     if (name && addr) {
    1009           2 :       int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[0]);
    1010           2 :       tt_int_op(ret, OP_EQ, family);
    1011             :     }
    1012           2 :   } else if (family == AF_INET6) {
    1013           2 :     if (name && addr) {
    1014           2 :       int ret = tor_addr_parse(addr, ret_addr_lookup_01010101[1]);
    1015           2 :       tt_int_op(ret, OP_EQ, family);
    1016             :     }
    1017             :   }
    1018           2 :  done:
    1019           4 :   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           2 : tor_addr_lookup_localhost(const char *name, uint16_t family, tor_addr_t *addr)
    1031             : {
    1032           2 :   n_hostname_localhost++;
    1033             : 
    1034           2 :   if (family == AF_INET) {
    1035           1 :     if (name && addr) {
    1036           1 :       tor_addr_from_ipv4h(addr, 0x7f000001);
    1037             :     }
    1038           1 :   } else if (family == AF_INET6) {
    1039           1 :     if (name && addr) {
    1040           1 :       int ret = tor_addr_parse(addr, "::1");
    1041           1 :       tt_int_op(ret, OP_EQ, AF_INET6);
    1042             :     }
    1043             :   }
    1044           1 :  done:
    1045           2 :   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           2 : tor_addr_lookup_failure(const char *name, uint16_t family, tor_addr_t *addr)
    1056             : {
    1057           2 :   (void)name;
    1058           2 :   (void)family;
    1059           2 :   (void)addr;
    1060             : 
    1061           2 :   n_hostname_failure++;
    1062             : 
    1063           2 :   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           8 : tor_addr_lookup_mixed(const char *name, uint16_t family, tor_addr_t *addr)
    1079             : {
    1080           8 :   tt_assert(addr);
    1081           8 :   tt_assert(name);
    1082             : 
    1083           8 :   if (!strcmp(name, "www.torproject.org.v4")) {
    1084           4 :     if (family == AF_INET) {
    1085           2 :       tor_addr_from_ipv4h(addr, 0x01010101);
    1086           2 :       return 0;
    1087             :     }
    1088             :     /* Resolving AF_INET but the asked family is different. Failure. */
    1089             :     return -1;
    1090             :   }
    1091             : 
    1092           4 :   if (!strcmp(name, "www.torproject.org.v6")) {
    1093           4 :     if (family == AF_INET6) {
    1094           2 :       int ret = tor_addr_parse(addr, "0101::0101");
    1095           2 :       tt_int_op(ret, OP_EQ, AF_INET6);
    1096           2 :       return 0;
    1097             :     }
    1098             :     /* Resolving AF_INET6 but the asked family is not. Failure. */
    1099             :     return -1;
    1100             :   }
    1101             : 
    1102           0 :  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           4 : tor_gethostname_replacement(char *name, size_t namelen)
    1115             : {
    1116           4 :   n_gethostname_replacement++;
    1117             : 
    1118           4 :   if (name && namelen) {
    1119           4 :     strlcpy(name,"onionrouter!",namelen);
    1120             :   }
    1121             : 
    1122           4 :   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           2 : tor_gethostname_localhost(char *name, size_t namelen)
    1134             : {
    1135           2 :   n_gethostname_localhost++;
    1136             : 
    1137           2 :   if (name && namelen) {
    1138           2 :     strlcpy(name,"127.0.0.1",namelen);
    1139             :   }
    1140             : 
    1141           2 :   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           2 : tor_gethostname_failure(char *name, size_t namelen)
    1152             : {
    1153           2 :   (void)name;
    1154           2 :   (void)namelen;
    1155           2 :   n_gethostname_failure++;
    1156             : 
    1157           2 :   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           4 : get_interface_address6_08080808(int severity, sa_family_t family,
    1172             :                                 tor_addr_t *addr)
    1173             : {
    1174           4 :   (void)severity;
    1175             : 
    1176           4 :   n_get_interface_address6++;
    1177             : 
    1178           4 :   if (family == AF_INET) {
    1179           2 :     if (addr) {
    1180           2 :       int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[0]);
    1181           2 :       tt_int_op(ret, OP_EQ, AF_INET);
    1182             :     }
    1183           2 :   } else if (family == AF_INET6) {
    1184           2 :     if (addr) {
    1185           2 :       int ret = tor_addr_parse(addr, ret_get_interface_address6_08080808[1]);
    1186           2 :       tt_int_op(ret, OP_EQ, AF_INET6);
    1187             :     }
    1188             :   }
    1189           2 :  done:
    1190           4 :   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           8 : get_interface_address6_failure(int severity, sa_family_t family,
    1231             :                                tor_addr_t *addr)
    1232             : {
    1233           8 :   (void)severity;
    1234           8 :   (void)addr;
    1235           8 :    n_get_interface_address6_failure++;
    1236           8 :    last_address6_family = family;
    1237             : 
    1238           8 :    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           1 : test_config_find_my_address_mixed(void *arg)
    1273             : {
    1274           1 :   or_options_t *options;
    1275           1 :   tor_addr_t resolved_addr, test_addr;
    1276           1 :   resolved_addr_method_t method_used;
    1277           1 :   char *hostname_out = NULL;
    1278           1 :   bool retval;
    1279             : 
    1280           1 :   (void)arg;
    1281             : 
    1282           1 :   options = options_new();
    1283             : 
    1284           1 :   options_init(options);
    1285             : 
    1286             :   /*
    1287             :    * CASE 1: Only IPv6 address. Accepted.
    1288             :    */
    1289           1 :   config_line_append(&options->Address, "Address",
    1290             :                      "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
    1291           1 :   tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
    1292             : 
    1293             :   /* IPv6 address should be found and considered configured. */
    1294           1 :   retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
    1295             :                            &method_used, &hostname_out);
    1296           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1297             : 
    1298           1 :   CLEANUP_FOUND_ADDRESS;
    1299             : 
    1300             :   /*
    1301             :    * Case 2: IPv4 _and_ IPv6 given. Accepted.
    1302             :    */
    1303           1 :   config_line_append(&options->Address, "Address",
    1304             :                      "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
    1305           1 :   config_line_append(&options->Address, "Address", "1.1.1.1");
    1306           1 :   tor_addr_parse(&test_addr, "1.1.1.1");
    1307             : 
    1308             :   /* IPv4 address should be found and considered configured. */
    1309           1 :   retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
    1310             :                            &method_used, &hostname_out);
    1311           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1312             : 
    1313             :   /* IPv6 address should be found and considered configured. */
    1314           1 :   tor_addr_parse(&test_addr, "2a01:4f8:fff0:4f:266:37ff:fe2c:5d19");
    1315           1 :   retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
    1316             :                            &method_used, &hostname_out);
    1317           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1318             : 
    1319           1 :   CLEANUP_FOUND_ADDRESS;
    1320             : 
    1321             :   /*
    1322             :    * Case 3: Two hostnames, IPv4 and IPv6.
    1323             :    */
    1324           1 :   config_line_append(&options->Address, "Address", "www.torproject.org.v4");
    1325           1 :   config_line_append(&options->Address, "Address", "www.torproject.org.v6");
    1326             : 
    1327             :   /* Looks at specific hostname to learn which address family to use. */
    1328           1 :   MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
    1329             : 
    1330             :   /* IPv4 address should be found and considered resolved. */
    1331           1 :   tor_addr_parse(&test_addr, "1.1.1.1");
    1332           1 :   retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
    1333             :                            &method_used, &hostname_out);
    1334           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
    1335             :                          "www.torproject.org.v4");
    1336           1 :   tor_free(hostname_out);
    1337             : 
    1338             :   /* IPv6 address should be found and considered resolved. */
    1339           1 :   tor_addr_parse(&test_addr, "0101::0101");
    1340           1 :   retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
    1341             :                            &method_used, &hostname_out);
    1342           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
    1343             :                          "www.torproject.org.v6");
    1344             : 
    1345           1 :   CLEANUP_FOUND_ADDRESS;
    1346           1 :   UNMOCK(tor_addr_lookup);
    1347             : 
    1348             :   /*
    1349             :    * Case 4: IPv4 address and a hostname resolving to IPV6.
    1350             :    */
    1351           1 :   config_line_append(&options->Address, "Address", "1.1.1.1");
    1352           1 :   config_line_append(&options->Address, "Address", "www.torproject.org.v6");
    1353             : 
    1354             :   /* Looks at specific hostname to learn which address family to use. */
    1355           1 :   MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
    1356             : 
    1357             :   /* IPv4 address should be found and configured. */
    1358           1 :   tor_addr_parse(&test_addr, "1.1.1.1");
    1359           1 :   retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
    1360             :                            &method_used, &hostname_out);
    1361           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1362             : 
    1363             :   /* IPv6 address should be found and considered resolved. */
    1364           1 :   tor_addr_parse(&test_addr, "0101::0101");
    1365           1 :   retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
    1366             :                            &method_used, &hostname_out);
    1367           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
    1368             :                          "www.torproject.org.v6");
    1369             : 
    1370           1 :   CLEANUP_FOUND_ADDRESS;
    1371           1 :   UNMOCK(tor_addr_lookup);
    1372             : 
    1373             :   /*
    1374             :    * Case 5: Hostname resolving to IPv4 and an IPv6 address.
    1375             :    */
    1376           1 :   config_line_append(&options->Address, "Address", "0101::0101");
    1377           1 :   config_line_append(&options->Address, "Address", "www.torproject.org.v4");
    1378             : 
    1379             :   /* Looks at specific hostname to learn which address family to use. */
    1380           1 :   MOCK(tor_addr_lookup, tor_addr_lookup_mixed);
    1381             : 
    1382             :   /* IPv4 address should be found and resolved. */
    1383           1 :   tor_addr_parse(&test_addr, "1.1.1.1");
    1384           1 :   retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
    1385             :                            &method_used, &hostname_out);
    1386           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED,
    1387             :                          "www.torproject.org.v4");
    1388           1 :   tor_free(hostname_out);
    1389             : 
    1390             :   /* IPv6 address should be found and considered resolved. */
    1391           1 :   tor_addr_parse(&test_addr, "0101::0101");
    1392           1 :   retval = find_my_address(options, AF_INET6, LOG_NOTICE, &resolved_addr,
    1393             :                            &method_used, &hostname_out);
    1394           1 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1395           1 :   CLEANUP_FOUND_ADDRESS;
    1396             : 
    1397           1 :   UNMOCK(tor_addr_lookup);
    1398             : 
    1399           1 :  done:
    1400           1 :   config_free_lines(options->Address);
    1401           1 :   or_options_free(options);
    1402           1 :   tor_free(hostname_out);
    1403             : 
    1404           1 :   UNMOCK(tor_addr_lookup);
    1405           1 : }
    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           2 : test_config_find_my_address(void *arg)
    1442             : {
    1443           2 :   or_options_t *options;
    1444           2 :   tor_addr_t resolved_addr, test_addr;
    1445           2 :   resolved_addr_method_t method_used;
    1446           2 :   char *hostname_out = NULL;
    1447           2 :   bool retval;
    1448           2 :   int prev_n_hostname_01010101;
    1449           2 :   int prev_n_hostname_failure;
    1450           2 :   int prev_n_hostname_localhost;
    1451           2 :   int prev_n_gethostname_replacement;
    1452           2 :   int prev_n_gethostname_failure;
    1453           2 :   int prev_n_gethostname_localhost;
    1454           2 :   int prev_n_get_interface_address6;
    1455           2 :   int prev_n_get_interface_address6_failure;
    1456             : 
    1457           2 :   const find_my_address_params_t *p = arg;
    1458             : 
    1459           2 :   options = options_new();
    1460           2 :   options_init(options);
    1461           2 :   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           2 :   if (p->family == AF_INET6) {
    1470           1 :     options->AddressDisableIPv6 = 1;
    1471             :     /* Set a valid IPv6. However, the discovery should still fail. */
    1472           1 :     config_line_append(&options->Address, "Address", p->public_ip);
    1473           1 :     tor_addr_parse(&test_addr, p->public_ip);
    1474             : 
    1475           1 :     retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1476             :                              &method_used, &hostname_out);
    1477           1 :     VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1478           1 :     CLEANUP_FOUND_ADDRESS;
    1479             :   }
    1480             : 
    1481             :   /*
    1482             :    * Case 1:
    1483             :    *    1. Address is a valid address.
    1484             :    *
    1485             :    * Expected to succeed.
    1486             :    */
    1487           2 :   config_line_append(&options->Address, "Address", p->public_ip);
    1488           2 :   tor_addr_parse(&test_addr, p->public_ip);
    1489             : 
    1490           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1491             :                            &method_used, &hostname_out);
    1492             : 
    1493           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1494           2 :   CLEANUP_FOUND_ADDRESS;
    1495             : 
    1496             :   /*
    1497             :    * Case 2: Address is a resolvable address. Expected to succeed.
    1498             :    */
    1499           2 :   MOCK(tor_addr_lookup, tor_addr_lookup_01010101);
    1500             : 
    1501           2 :   config_line_append(&options->Address, "Address", "www.torproject.org");
    1502           2 :   tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]);
    1503             : 
    1504           2 :   prev_n_hostname_01010101 = n_hostname_01010101;
    1505             : 
    1506           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1507             :                            &method_used, &hostname_out);
    1508             : 
    1509           2 :   tt_int_op(n_hostname_01010101, OP_EQ, ++prev_n_hostname_01010101);
    1510           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_RESOLVED, "www.torproject.org");
    1511           2 :   CLEANUP_FOUND_ADDRESS;
    1512             : 
    1513           2 :   UNMOCK(tor_addr_lookup);
    1514             : 
    1515             :   /*
    1516             :    * Case 3: Address is a local addressi (internal). Expected to fail.
    1517             :    */
    1518           2 :   config_line_append(&options->Address, "Address", p->internal_ip);
    1519             : 
    1520           2 :   setup_full_capture_of_logs(LOG_NOTICE);
    1521             : 
    1522           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1523             :                            &method_used, &hostname_out);
    1524             : 
    1525           2 :   expect_log_msg_containing("is a private IP address. Tor relays that "
    1526             :                             "use the default DirAuthorities must have "
    1527           2 :                             "public IP addresses.");
    1528           2 :   teardown_capture_of_logs();
    1529             : 
    1530           2 :   VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1531           2 :   CLEANUP_FOUND_ADDRESS;
    1532             : 
    1533             :   /*
    1534             :    * Case 4: Address is a local address but custom authorities. Expected to
    1535             :    * succeed.
    1536             :    */
    1537           2 :   config_line_append(&options->Address, "Address", p->internal_ip);
    1538           2 :   options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
    1539           2 :   tor_addr_parse(&test_addr, p->internal_ip);
    1540             : 
    1541           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1542             :                            &method_used, &hostname_out);
    1543             : 
    1544           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1545           2 :   CLEANUP_FOUND_ADDRESS;
    1546             : 
    1547             :   /*
    1548             :    * Case 5: Multiple address in Address. Expected to fail.
    1549             :    */
    1550           2 :   config_line_append(&options->Address, "Address", p->public_ip);
    1551           2 :   config_line_append(&options->Address, "Address", p->public_ip);
    1552             : 
    1553           2 :   setup_full_capture_of_logs(LOG_NOTICE);
    1554             : 
    1555           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1556             :                            &method_used, &hostname_out);
    1557             : 
    1558           2 :   expect_log_msg_containing("Found 2 Address statement of address family");
    1559           2 :   teardown_capture_of_logs();
    1560             : 
    1561           2 :   VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1562           2 :   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           2 :   options->Address = NULL;
    1572           2 :   tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]);
    1573             : 
    1574           2 :   MOCK(get_interface_address6, get_interface_address6_08080808);
    1575             : 
    1576           2 :   prev_n_get_interface_address6 = n_get_interface_address6;
    1577             : 
    1578           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1579             :                            &method_used, &hostname_out);
    1580             : 
    1581           2 :   tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6);
    1582           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL);
    1583           2 :   CLEANUP_FOUND_ADDRESS;
    1584             : 
    1585           2 :   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           2 :   options->Address = NULL;
    1596           2 :   tor_addr_parse(&test_addr, ret_addr_lookup_01010101[p->idx]);
    1597             : 
    1598           2 :   MOCK(get_interface_address6, get_interface_address6_failure);
    1599           2 :   MOCK(tor_gethostname, tor_gethostname_replacement);
    1600           2 :   MOCK(tor_addr_lookup, tor_addr_lookup_01010101);
    1601             : 
    1602           2 :   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
    1603           2 :   prev_n_hostname_01010101 = n_hostname_01010101;
    1604           2 :   prev_n_gethostname_replacement = n_gethostname_replacement;
    1605             : 
    1606           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1607             :                            &method_used, &hostname_out);
    1608             : 
    1609           2 :   tt_int_op(n_get_interface_address6_failure, OP_EQ,
    1610             :             ++prev_n_get_interface_address6_failure);
    1611           2 :   tt_int_op(n_hostname_01010101, OP_EQ,
    1612             :             ++prev_n_hostname_01010101);
    1613           2 :   tt_int_op(n_gethostname_replacement, OP_EQ,
    1614             :             ++prev_n_gethostname_replacement);
    1615           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_GETHOSTNAME, "onionrouter!");
    1616           2 :   CLEANUP_FOUND_ADDRESS;
    1617             : 
    1618           2 :   UNMOCK(get_interface_address6);
    1619           2 :   UNMOCK(tor_gethostname);
    1620           2 :   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           2 :   options->Address = NULL;
    1631             : 
    1632           2 :   MOCK(get_interface_address6, get_interface_address6_failure);
    1633           2 :   MOCK(tor_gethostname, tor_gethostname_localhost);
    1634           2 :   MOCK(tor_addr_lookup, tor_addr_lookup_localhost);
    1635             : 
    1636           2 :   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
    1637           2 :   prev_n_hostname_localhost = n_hostname_localhost;
    1638           2 :   prev_n_gethostname_localhost = n_gethostname_localhost;
    1639             : 
    1640           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1641             :                            &method_used, &hostname_out);
    1642             : 
    1643           2 :   tt_int_op(n_get_interface_address6_failure, OP_EQ,
    1644             :             ++prev_n_get_interface_address6_failure);
    1645           2 :   tt_int_op(n_hostname_localhost, OP_EQ,
    1646             :             ++prev_n_hostname_localhost);
    1647           2 :   tt_int_op(n_gethostname_localhost, OP_EQ,
    1648             :             ++prev_n_gethostname_localhost);
    1649           2 :   VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1650           2 :   CLEANUP_FOUND_ADDRESS;
    1651             : 
    1652           2 :   UNMOCK(get_interface_address6);
    1653           2 :   UNMOCK(tor_gethostname);
    1654           2 :   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           2 :   options->Address = NULL;
    1665             : 
    1666           2 :   MOCK(get_interface_address6, get_interface_address6_failure);
    1667           2 :   MOCK(tor_gethostname, tor_gethostname_failure);
    1668             : 
    1669           2 :   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
    1670           2 :   prev_n_gethostname_failure = n_gethostname_failure;
    1671             : 
    1672           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1673             :                            &method_used, &hostname_out);
    1674             : 
    1675           2 :   tt_int_op(n_get_interface_address6_failure, OP_EQ,
    1676             :             ++prev_n_get_interface_address6_failure);
    1677           2 :   tt_int_op(n_gethostname_failure, OP_EQ,
    1678             :             ++prev_n_gethostname_failure);
    1679           2 :   VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1680           2 :   CLEANUP_FOUND_ADDRESS;
    1681             : 
    1682           2 :   UNMOCK(get_interface_address6);
    1683           2 :   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           2 :   options->Address = NULL;
    1694             : 
    1695           2 :   MOCK(get_interface_address6, get_interface_address6_failure);
    1696           2 :   MOCK(tor_gethostname, tor_gethostname_replacement);
    1697           2 :   MOCK(tor_addr_lookup, tor_addr_lookup_failure);
    1698             : 
    1699           2 :   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
    1700           2 :   prev_n_gethostname_replacement = n_gethostname_replacement;
    1701           2 :   prev_n_hostname_failure = n_hostname_failure;
    1702             : 
    1703           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1704             :                            &method_used, &hostname_out);
    1705             : 
    1706           2 :   tt_int_op(n_get_interface_address6_failure, OP_EQ,
    1707             :             ++prev_n_get_interface_address6_failure);
    1708           2 :   tt_int_op(n_gethostname_replacement, OP_EQ,
    1709             :             ++prev_n_gethostname_replacement);
    1710           2 :   tt_int_op(n_hostname_failure, OP_EQ,
    1711             :             ++prev_n_hostname_failure);
    1712           2 :   VALIDATE_FOUND_ADDRESS(false, RESOLVED_ADDR_NONE, NULL);
    1713           2 :   CLEANUP_FOUND_ADDRESS;
    1714             : 
    1715             :   /*
    1716             :    * Case 13:
    1717             :    *    1. Address is NULL.
    1718             :    *    2. ORPort has a valid public address.
    1719             :    */
    1720             :   {
    1721           2 :     char *msg = NULL;
    1722           2 :     int n, w, ret;
    1723           2 :     char *orport_line = NULL;
    1724             : 
    1725           2 :     options->Address = NULL;
    1726           2 :     tor_asprintf(&orport_line, "%s:9001", p->public_ip);
    1727           2 :     config_line_append(&options->ORPort_lines, "ORPort", orport_line);
    1728           2 :     tor_free(orport_line);
    1729             : 
    1730           2 :     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           1 :       config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001");
    1734             :     }
    1735             : 
    1736           2 :     ret = parse_ports(options, 0, &msg, &n, &w);
    1737           2 :     tt_int_op(ret, OP_EQ, 0);
    1738           2 :     tor_addr_parse(&test_addr, p->public_ip);
    1739             :   }
    1740             : 
    1741           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1742             :                            &method_used, &hostname_out);
    1743           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED_ORPORT, NULL);
    1744           2 :   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           2 :     char *msg = NULL;
    1754           2 :     int n, w, ret;
    1755           2 :     char *orport_line = NULL;
    1756             : 
    1757           2 :     options->Address = NULL;
    1758           2 :     tor_asprintf(&orport_line, "%s:9001", p->internal_ip);
    1759           2 :     config_line_append(&options->ORPort_lines, "ORPort", orport_line);
    1760           2 :     tor_free(orport_line);
    1761             : 
    1762           2 :     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           1 :       config_line_append(&options->ORPort_lines, "ORPort", "1.1.1.1:9001");
    1766             :     }
    1767             : 
    1768           2 :     ret = parse_ports(options, 0, &msg, &n, &w);
    1769           2 :     tt_int_op(ret, OP_EQ, 0);
    1770             :   }
    1771           2 :   tor_addr_parse(&test_addr, ret_get_interface_address6_08080808[p->idx]);
    1772             : 
    1773           2 :   MOCK(get_interface_address6, get_interface_address6_08080808);
    1774             : 
    1775           2 :   prev_n_get_interface_address6 = n_get_interface_address6;
    1776             : 
    1777           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1778             :                            &method_used, &hostname_out);
    1779             : 
    1780           2 :   tt_int_op(n_get_interface_address6, OP_EQ, ++prev_n_get_interface_address6);
    1781           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_INTERFACE, NULL);
    1782           2 :   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           2 :   options->PublishServerDescriptor_ = NO_DIRINFO;
    1789           2 :   if (p->family == AF_INET) {
    1790           1 :     options->AssumeReachable = 1;
    1791             :   }
    1792           2 :   config_line_append(&options->Address, "Address", p->internal_ip);
    1793             : 
    1794           2 :   tor_addr_parse(&test_addr, p->internal_ip);
    1795           2 :   retval = find_my_address(options, p->family, LOG_NOTICE, &resolved_addr,
    1796             :                            &method_used, &hostname_out);
    1797           2 :   VALIDATE_FOUND_ADDRESS(true, RESOLVED_ADDR_CONFIGURED, NULL);
    1798           2 :   CLEANUP_FOUND_ADDRESS;
    1799             : 
    1800           2 :   UNMOCK(get_interface_address6);
    1801           2 :   UNMOCK(tor_gethostname);
    1802           2 :   UNMOCK(tor_addr_lookup);
    1803             : 
    1804           2 :  done:
    1805           2 :   or_options_free(options);
    1806             : 
    1807           2 :   UNMOCK(tor_gethostname);
    1808           2 :   UNMOCK(tor_addr_lookup);
    1809           2 :   UNMOCK(get_interface_address6);
    1810           2 : }
    1811             : 
    1812             : static void
    1813           1 : test_config_adding_trusted_dir_server(void *arg)
    1814             : {
    1815           1 :   (void)arg;
    1816             : 
    1817           1 :   const char digest[DIGEST_LEN] = "";
    1818           1 :   dir_server_t *ds = NULL;
    1819           1 :   tor_addr_port_t ipv6;
    1820           1 :   int rv = -1;
    1821             : 
    1822           1 :   clear_dir_servers();
    1823           1 :   routerlist_free_all();
    1824             : 
    1825             :   /* create a trusted ds without an IPv6 address and port */
    1826           1 :   ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
    1827             :                               NULL, V3_DIRINFO, 1.0);
    1828           1 :   tt_assert(ds);
    1829           1 :   dir_server_add(ds);
    1830           1 :   tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
    1831           1 :   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           1 :   rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
    1835           1 :   tt_int_op(rv, OP_EQ, 0);
    1836           1 :   ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
    1837             :                               NULL, V3_DIRINFO, 1.0);
    1838           1 :   tt_assert(ds);
    1839           1 :   dir_server_add(ds);
    1840           1 :   tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
    1841           1 :   tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
    1842             : 
    1843           1 :  done:
    1844           1 :   clear_dir_servers();
    1845           1 :   routerlist_free_all();
    1846           1 : }
    1847             : 
    1848             : static void
    1849           1 : test_config_adding_fallback_dir_server(void *arg)
    1850             : {
    1851           1 :   (void)arg;
    1852             : 
    1853           1 :   const char digest[DIGEST_LEN] = "";
    1854           1 :   dir_server_t *ds = NULL;
    1855           1 :   tor_addr_t ipv4;
    1856           1 :   tor_addr_port_t ipv6;
    1857           1 :   int rv = -1;
    1858             : 
    1859           1 :   clear_dir_servers();
    1860           1 :   routerlist_free_all();
    1861             : 
    1862           1 :   rv = tor_addr_parse(&ipv4, "127.0.0.1");
    1863           1 :   tt_int_op(rv, OP_EQ, AF_INET);
    1864             : 
    1865             :   /* create a trusted ds without an IPv6 address and port */
    1866           1 :   ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
    1867           1 :   tt_assert(ds);
    1868           1 :   dir_server_add(ds);
    1869           1 :   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           1 :   rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
    1873           1 :   tt_int_op(rv, OP_EQ, 0);
    1874           1 :   ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
    1875           1 :   tt_assert(ds);
    1876           1 :   dir_server_add(ds);
    1877           1 :   tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
    1878             : 
    1879           1 :  done:
    1880           1 :   clear_dir_servers();
    1881           1 :   routerlist_free_all();
    1882           1 : }
    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           1 : test_config_parsing_trusted_dir_server(void *arg)
    1900             : {
    1901           1 :   (void)arg;
    1902           1 :   int rv = -1;
    1903             : 
    1904             :   /* parse a trusted dir server without an IPv6 address and port */
    1905           1 :   rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
    1906             :                                 TEST_DIR_AUTH_LINE_END,
    1907             :                                 V3_DIRINFO, 1);
    1908           1 :   tt_int_op(rv, OP_EQ, 0);
    1909             : 
    1910             :   /* parse a trusted dir server with an IPv6 address and port */
    1911           1 :   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           1 :   tt_int_op(rv, OP_EQ, 0);
    1916             : 
    1917             :   /* Since we are only validating, there is no cleanup. */
    1918           1 :  done:
    1919           1 :   ;
    1920           1 : }
    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           1 : test_config_parsing_invalid_dir_address(void *arg)
    1938             : {
    1939           1 :   (void)arg;
    1940           1 :   int rv;
    1941             : 
    1942           1 :   rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
    1943             :                                 TEST_DIR_AUTH_LINE_END_BAD_IP,
    1944             :                                 V3_DIRINFO, 1);
    1945           1 :   tt_int_op(rv, OP_EQ, -1);
    1946             : 
    1947           1 :   rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
    1948             :                                 TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR,
    1949             :                                 V3_DIRINFO, 1);
    1950           1 :   tt_int_op(rv, OP_EQ, -1);
    1951             : 
    1952           1 :   done:
    1953           1 :   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           1 : test_config_parsing_fallback_dir_server(void *arg)
    1971             : {
    1972           1 :   (void)arg;
    1973           1 :   int rv = -1;
    1974             : 
    1975             :   /* parse a trusted dir server without an IPv6 address and port */
    1976           1 :   rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
    1977           1 :   tt_int_op(rv, OP_EQ, 0);
    1978             : 
    1979             :   /* parse a trusted dir server with an IPv6 address and port */
    1980           1 :   rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
    1981             :                                TEST_DIR_FALLBACK_IPV6_FLAG,
    1982             :                                1);
    1983           1 :   tt_int_op(rv, OP_EQ, 0);
    1984             : 
    1985             :   /* Since we are only validating, there is no cleanup. */
    1986           1 :  done:
    1987           1 :   ;
    1988           1 : }
    1989             : 
    1990             : #undef TEST_DIR_FALLBACK_LINE
    1991             : #undef TEST_DIR_FALLBACK_IPV6_FLAG
    1992             : 
    1993             : static void
    1994           1 : test_config_adding_default_trusted_dir_servers(void *arg)
    1995             : {
    1996           1 :   (void)arg;
    1997             : 
    1998           1 :   clear_dir_servers();
    1999           1 :   routerlist_free_all();
    2000             : 
    2001             :   /* Assume we only have one bridge authority */
    2002           1 :   add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
    2003           1 :   tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
    2004           1 :   tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
    2005             : 
    2006             :   /* Assume we have eight V3 authorities */
    2007           1 :   add_default_trusted_dir_authorities(V3_DIRINFO);
    2008           1 :   tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
    2009           1 :   tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
    2010             : 
    2011           1 :  done:
    2012           1 :   clear_dir_servers();
    2013           1 :   routerlist_free_all();
    2014           1 : }
    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           3 : add_default_fallback_dir_servers_known_default(void)
    2027             : {
    2028           3 :   int i;
    2029           3 :   const char *fallback[] = {
    2030             :     "127.0.0.1:60099 orport=9009 "
    2031             :     "id=0923456789012345678901234567890123456789",
    2032             :     NULL
    2033             :   };
    2034           6 :   for (i=0; fallback[i]; i++) {
    2035           3 :     if (parse_dir_fallback_line(fallback[i], 0)<0) {
    2036           0 :       log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
    2037             :               fallback[i]);
    2038             :     }
    2039             :   }
    2040           3 :   n_add_default_fallback_dir_servers_known_default++;
    2041           3 : }
    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          12 : ads_clear_helper(or_options_t *options)
    2048             : {
    2049          12 :   options->DirAuthorities = NULL;
    2050          12 :   options->AlternateBridgeAuthority = NULL;
    2051          12 :   options->AlternateDirAuthority = NULL;
    2052          12 :   options->FallbackDir = NULL;
    2053          12 : }
    2054             : 
    2055             : /* Test all the different combinations of adding dir servers */
    2056             : static void
    2057           1 : test_config_adding_dir_servers(void *arg)
    2058             : {
    2059           1 :   (void)arg;
    2060             : 
    2061             :   /* allocate options */
    2062           1 :   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           1 :   config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
    2072           1 :   test_dir_authority->key = tor_strdup("DirAuthority");
    2073           1 :   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           1 :   config_line_t *test_alt_bridge_authority = tor_malloc_zero(
    2080             :                                                       sizeof(config_line_t));
    2081           1 :   test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
    2082           1 :   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           1 :   config_line_t *test_alt_dir_authority = tor_malloc_zero(
    2088             :                                                       sizeof(config_line_t));
    2089           1 :   test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
    2090           1 :   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           1 :   config_line_t *test_fallback_directory = tor_malloc_zero(
    2098             :                                                       sizeof(config_line_t));
    2099           1 :   test_fallback_directory->key = tor_strdup("FallbackDir");
    2100           1 :   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           1 :   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           1 :   int n_default_alt_bridge_authority = 0;
    2172           1 :   int n_default_alt_dir_authority = 0;
    2173           1 :   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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2183             : 
    2184             :     /* clear options*/
    2185           1 :     ads_clear_helper(options);
    2186           1 :     or_options_free(options);
    2187           1 :     options = options_new();
    2188             : 
    2189             :     /* clear any previous dir servers:
    2190             :      consider_adding_dir_servers() should do this anyway */
    2191           1 :     clear_dir_servers();
    2192             : 
    2193             :     /* assign options: 0000 */
    2194           1 :     options->DirAuthorities = NULL;
    2195           1 :     options->AlternateBridgeAuthority = NULL;
    2196           1 :     options->AlternateDirAuthority = NULL;
    2197           1 :     options->FallbackDir = NULL;
    2198           1 :     options->UseDefaultFallbackDirs = 1;
    2199             : 
    2200             :     /* parse options - ensure we always update by passing NULL old_options */
    2201           1 :     consider_adding_dir_servers(options, NULL);
    2202             : 
    2203             :     /* check outcome */
    2204             : 
    2205             :     /* we must have added the default fallback dirs */
    2206           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    2210             : 
    2211             :     {
    2212             :       /* fallback_dir_servers */
    2213           1 :       const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
    2214             : 
    2215             :       /* Count Bridge Authorities */
    2216          12 :       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           1 :       tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
    2226             : 
    2227             :       /* Count v3 Authorities */
    2228          12 :       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           1 :       tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
    2238             : 
    2239             :       /* Calculate Fallback Directory Count */
    2240           1 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2267             : 
    2268             :     /* clear options*/
    2269           1 :     ads_clear_helper(options);
    2270           1 :     or_options_free(options);
    2271           1 :     options = options_new();
    2272             : 
    2273             :     /* clear any previous dir servers:
    2274             :      consider_adding_dir_servers() should do this anyway */
    2275           1 :     clear_dir_servers();
    2276             : 
    2277             :     /* assign options: 1001 */
    2278           1 :     options->DirAuthorities = test_dir_authority;
    2279           1 :     options->AlternateBridgeAuthority = NULL;
    2280           1 :     options->AlternateDirAuthority = NULL;
    2281           1 :     options->FallbackDir = test_fallback_directory;
    2282           1 :     options->UseDefaultFallbackDirs = 1;
    2283             : 
    2284             :     /* parse options - ensure we always update by passing NULL old_options */
    2285           1 :     consider_adding_dir_servers(options, NULL);
    2286             : 
    2287             :     /* check outcome */
    2288             : 
    2289             :     /* we must not have added the default fallback dirs */
    2290           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    2294             : 
    2295             :     {
    2296             :       /* trusted_dir_servers */
    2297           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    2298             :       /* D0, (No B1), (No A2) */
    2299           1 :       tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
    2300             : 
    2301             :       /* DirAuthority - D0 - dir_port: 60090 */
    2302           1 :       int found_D0 = 0;
    2303           2 :       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           1 :       tt_int_op(found_D0, OP_EQ, 1);
    2312             : 
    2313             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    2314           1 :       int found_B1 = 0;
    2315           2 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    2324             : 
    2325             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2326           1 :       int found_A2 = 0;
    2327           2 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    2336             :     }
    2337             : 
    2338             :     {
    2339             :       /* fallback_dir_servers */
    2340           1 :       const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
    2341             :       /* D0, (No B1), (No A2), Custom Fallback */
    2342           1 :       tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
    2343             : 
    2344             :       /* DirAuthority - D0 - dir_port: 60090 */
    2345           1 :       int found_D0 = 0;
    2346           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 1);
    2355             : 
    2356             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    2357           1 :       int found_B1 = 0;
    2358           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    2367             : 
    2368             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2369           1 :       int found_A2 = 0;
    2370           3 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    2379             : 
    2380             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    2381           1 :       int found_non_default_fallback = 0;
    2382           3 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 1);
    2391             : 
    2392             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    2393           1 :       int found_default_fallback = 0;
    2394           3 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2410             : 
    2411             :     /* clear options*/
    2412           1 :     ads_clear_helper(options);
    2413           1 :     or_options_free(options);
    2414           1 :     options = options_new();
    2415             : 
    2416             :     /* clear any previous dir servers:
    2417             :      consider_adding_dir_servers() should do this anyway */
    2418           1 :     clear_dir_servers();
    2419             : 
    2420             :     /* assign options: 1000 */
    2421           1 :     options->DirAuthorities = test_dir_authority;
    2422           1 :     options->AlternateBridgeAuthority = NULL;
    2423           1 :     options->AlternateDirAuthority = NULL;
    2424           1 :     options->FallbackDir = NULL;
    2425           1 :     options->UseDefaultFallbackDirs = 1;
    2426             : 
    2427             :     /* parse options - ensure we always update by passing NULL old_options */
    2428           1 :     consider_adding_dir_servers(options, NULL);
    2429             : 
    2430             :     /* check outcome */
    2431             : 
    2432             :     /* we must not have added the default fallback dirs */
    2433           1 :     tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
    2434             : 
    2435             :     /* we just have the authorities */
    2436           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
    2437             : 
    2438             :     {
    2439             :       /* trusted_dir_servers */
    2440           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    2441             :       /* D0, (No B1), (No A2) */
    2442           1 :       tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
    2443             : 
    2444             :       /* DirAuthority - D0 - dir_port: 60090 */
    2445           1 :       int found_D0 = 0;
    2446           2 :       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           1 :       tt_int_op(found_D0, OP_EQ, 1);
    2455             : 
    2456             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    2457           1 :       int found_B1 = 0;
    2458           2 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    2467             : 
    2468             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2469           1 :       int found_A2 = 0;
    2470           2 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    2479             :     }
    2480             : 
    2481             :     {
    2482             :       /* fallback_dir_servers */
    2483           1 :       const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
    2484             :       /* D0, (No B1), (No A2), (No Fallback) */
    2485           1 :       tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
    2486             : 
    2487             :       /* DirAuthority - D0 - dir_port: 60090 */
    2488           1 :       int found_D0 = 0;
    2489           2 :       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           1 :       tt_int_op(found_D0, OP_EQ, 1);
    2498             : 
    2499             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    2500           1 :       int found_B1 = 0;
    2501           2 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    2510             : 
    2511             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2512           1 :       int found_A2 = 0;
    2513           2 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    2522             : 
    2523             :       /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
    2524           1 :       int found_non_default_fallback = 0;
    2525           2 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 0);
    2534             : 
    2535             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    2536           1 :       int found_default_fallback = 0;
    2537           2 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2553             : 
    2554             :     /* clear options*/
    2555           1 :     ads_clear_helper(options);
    2556           1 :     or_options_free(options);
    2557           1 :     options = options_new();
    2558             : 
    2559             :     /* clear any previous dir servers:
    2560             :      consider_adding_dir_servers() should do this anyway */
    2561           1 :     clear_dir_servers();
    2562             : 
    2563             :     /* assign options: 0111 */
    2564           1 :     options->DirAuthorities = NULL;
    2565           1 :     options->AlternateBridgeAuthority = test_alt_bridge_authority;
    2566           1 :     options->AlternateDirAuthority = test_alt_dir_authority;
    2567           1 :     options->FallbackDir = test_fallback_directory;
    2568           1 :     options->UseDefaultFallbackDirs = 1;
    2569             : 
    2570             :     /* parse options - ensure we always update by passing NULL old_options */
    2571           1 :     consider_adding_dir_servers(options, NULL);
    2572             : 
    2573             :     /* check outcome */
    2574             : 
    2575             :     /* we must not have added the default fallback dirs */
    2576           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    2580             : 
    2581             :     {
    2582             :       /* trusted_dir_servers */
    2583           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    2584             :       /* (No D0), B1, A2 */
    2585           1 :       tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
    2586             : 
    2587             :       /* (No DirAuthority) - D0 - dir_port: 60090 */
    2588           1 :       int found_D0 = 0;
    2589           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2598             : 
    2599             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2600           1 :       int found_B1 = 0;
    2601           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2610             : 
    2611             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    2612           1 :       int found_A2 = 0;
    2613           3 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    2622             :     }
    2623             : 
    2624             :     {
    2625             :       /* fallback_dir_servers */
    2626           1 :       const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
    2627             :       /* (No D0), B1, A2, Custom Fallback */
    2628           1 :       tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
    2629             : 
    2630             :       /* (No DirAuthority) - D0 - dir_port: 60090 */
    2631           1 :       int found_D0 = 0;
    2632           4 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2641             : 
    2642             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2643           1 :       int found_B1 = 0;
    2644           4 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2653             : 
    2654             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    2655           1 :       int found_A2 = 0;
    2656           4 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    2665             : 
    2666             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    2667           1 :       int found_non_default_fallback = 0;
    2668           4 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 1);
    2677             : 
    2678             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    2679           1 :       int found_default_fallback = 0;
    2680           4 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2697             : 
    2698             :     /* clear options*/
    2699           1 :     ads_clear_helper(options);
    2700           1 :     or_options_free(options);
    2701           1 :     options = options_new();
    2702             : 
    2703             :     /* clear any previous dir servers:
    2704             :      consider_adding_dir_servers() should do this anyway */
    2705           1 :     clear_dir_servers();
    2706             : 
    2707             :     /* assign options: 0110 */
    2708           1 :     options->DirAuthorities = NULL;
    2709           1 :     options->AlternateBridgeAuthority = test_alt_bridge_authority;
    2710           1 :     options->AlternateDirAuthority = test_alt_dir_authority;
    2711           1 :     options->FallbackDir = NULL;
    2712           1 :     options->UseDefaultFallbackDirs = 1;
    2713             : 
    2714             :     /* parse options - ensure we always update by passing NULL old_options */
    2715           1 :     consider_adding_dir_servers(options, NULL);
    2716             : 
    2717             :     /* check outcome */
    2718             : 
    2719             :     /* we must not have added the default fallback dirs */
    2720           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
    2724             : 
    2725             :     {
    2726             :       /* trusted_dir_servers */
    2727           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    2728             :       /* (No D0), B1, A2 */
    2729           1 :       tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
    2730             : 
    2731             :       /* (No DirAuthority) - D0 - dir_port: 60090 */
    2732           1 :       int found_D0 = 0;
    2733           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2742             : 
    2743             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2744           1 :       int found_B1 = 0;
    2745           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2754             : 
    2755             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    2756           1 :       int found_A2 = 0;
    2757           3 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    2766             :     }
    2767             : 
    2768             :     {
    2769             :       /* fallback_dir_servers */
    2770           1 :       const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
    2771             :       /* (No D0), B1, A2, (No Fallback) */
    2772           1 :       tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
    2773             : 
    2774             :       /* (No DirAuthority) - D0 - dir_port: 60090 */
    2775           1 :       int found_D0 = 0;
    2776           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2785             : 
    2786             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2787           1 :       int found_B1 = 0;
    2788           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2797             : 
    2798             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    2799           1 :       int found_A2 = 0;
    2800           3 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    2809             : 
    2810             :       /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
    2811           1 :       int found_non_default_fallback = 0;
    2812           3 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 0);
    2821             : 
    2822             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    2823           1 :       int found_default_fallback = 0;
    2824           3 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    2851             : 
    2852             :     /* clear options*/
    2853           1 :     ads_clear_helper(options);
    2854           1 :     or_options_free(options);
    2855           1 :     options = options_new();
    2856             : 
    2857             :     /* clear any previous dir servers:
    2858             :      consider_adding_dir_servers() should do this anyway */
    2859           1 :     clear_dir_servers();
    2860             : 
    2861             :     /* assign options: 0101 */
    2862           1 :     options->DirAuthorities = NULL;
    2863           1 :     options->AlternateBridgeAuthority = test_alt_bridge_authority;
    2864           1 :     options->AlternateDirAuthority = NULL;
    2865           1 :     options->FallbackDir = test_fallback_directory;
    2866           1 :     options->UseDefaultFallbackDirs = 1;
    2867             : 
    2868             :     /* parse options - ensure we always update by passing NULL old_options */
    2869           1 :     consider_adding_dir_servers(options, NULL);
    2870             : 
    2871             :     /* check outcome */
    2872             : 
    2873             :     /* we must not have added the default fallback dirs */
    2874           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    2878             : 
    2879             :     {
    2880             :       /* trusted_dir_servers */
    2881           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    2882             :       /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
    2883           1 :       tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
    2884             : 
    2885             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    2886           1 :       int found_D0 = 0;
    2887          11 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2896             : 
    2897             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2898           1 :       int found_B1 = 0;
    2899          11 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2908             : 
    2909             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2910           1 :       int found_A2 = 0;
    2911          11 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    2933             :                 2 + n_default_alt_dir_authority);
    2934             : 
    2935             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    2936           1 :       int found_D0 = 0;
    2937          12 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    2946             : 
    2947             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    2948           1 :       int found_B1 = 0;
    2949          12 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    2958             : 
    2959             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    2960           1 :       int found_A2 = 0;
    2961          12 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    2970             : 
    2971             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    2972           1 :       int found_non_default_fallback = 0;
    2973          12 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 1);
    2982             : 
    2983             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    2984           1 :       int found_default_fallback = 0;
    2985          12 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    3007             : 
    3008             :     /* clear options*/
    3009           1 :     ads_clear_helper(options);
    3010           1 :     or_options_free(options);
    3011           1 :     options = options_new();
    3012             : 
    3013             :     /* clear any previous dir servers:
    3014             :      consider_adding_dir_servers() should do this anyway */
    3015           1 :     clear_dir_servers();
    3016             : 
    3017             :     /* assign options: 0100 */
    3018           1 :     options->DirAuthorities = NULL;
    3019           1 :     options->AlternateBridgeAuthority = test_alt_bridge_authority;
    3020           1 :     options->AlternateDirAuthority = NULL;
    3021           1 :     options->FallbackDir = NULL;
    3022           1 :     options->UseDefaultFallbackDirs = 1;
    3023             : 
    3024             :     /* parse options - ensure we always update by passing NULL old_options */
    3025           1 :     consider_adding_dir_servers(options, NULL);
    3026             : 
    3027             :     /* check outcome */
    3028             : 
    3029             :     /* we must have added the default fallback dirs */
    3030           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    3034             : 
    3035             :     {
    3036             :       /* trusted_dir_servers */
    3037           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    3038             :       /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
    3039           1 :       tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
    3040             : 
    3041             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3042           1 :       int found_D0 = 0;
    3043          11 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3052             : 
    3053             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    3054           1 :       int found_B1 = 0;
    3055          11 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    3064             : 
    3065             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3066           1 :       int found_A2 = 0;
    3067          11 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    3089             :                 2 + n_default_alt_dir_authority);
    3090             : 
    3091             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3092           1 :       int found_D0 = 0;
    3093          12 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3102             : 
    3103             :       /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
    3104           1 :       int found_B1 = 0;
    3105          12 :       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           1 :       tt_int_op(found_B1, OP_EQ, 1);
    3114             : 
    3115             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3116           1 :       int found_A2 = 0;
    3117          12 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    3126             : 
    3127             :       /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
    3128           1 :       int found_non_default_fallback = 0;
    3129          12 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 0);
    3138             : 
    3139             :       /* Default FallbackDir - No Nickname - dir_port: 60099 */
    3140           1 :       int found_default_fallback = 0;
    3141          12 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    3172             : 
    3173             :     /* clear options*/
    3174           1 :     ads_clear_helper(options);
    3175           1 :     or_options_free(options);
    3176           1 :     options = options_new();
    3177             : 
    3178             :     /* clear any previous dir servers:
    3179             :      consider_adding_dir_servers() should do this anyway */
    3180           1 :     clear_dir_servers();
    3181             : 
    3182             :     /* assign options: 0011 */
    3183           1 :     options->DirAuthorities = NULL;
    3184           1 :     options->AlternateBridgeAuthority = NULL;
    3185           1 :     options->AlternateDirAuthority = test_alt_dir_authority;
    3186           1 :     options->FallbackDir = test_fallback_directory;
    3187           1 :     options->UseDefaultFallbackDirs = 1;
    3188             : 
    3189             :     /* parse options - ensure we always update by passing NULL old_options */
    3190           1 :     consider_adding_dir_servers(options, NULL);
    3191             : 
    3192             :     /* check outcome */
    3193             : 
    3194             :     /* we must not have added the default fallback dirs */
    3195           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    3199             : 
    3200             :     {
    3201             :       /* trusted_dir_servers */
    3202           1 :       const smartlist_t *dir_servers = router_get_trusted_dir_servers();
    3203             :       /* (No D0), (No B1), Default Bridge Authorities, A2 */
    3204           1 :       tt_assert(smartlist_len(dir_servers) ==
    3205             :                 1 + n_default_alt_bridge_authority);
    3206             : 
    3207             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3208           1 :       int found_D0 = 0;
    3209           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3218             : 
    3219             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3220           1 :       int found_B1 = 0;
    3221           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3230             : 
    3231             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    3232           1 :       int found_A2 = 0;
    3233           3 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    3256             :                 2 + n_default_alt_bridge_authority);
    3257             : 
    3258             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3259           1 :       int found_D0 = 0;
    3260           4 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3269             : 
    3270             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3271           1 :       int found_B1 = 0;
    3272           4 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3281             : 
    3282             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    3283           1 :       int found_A2 = 0;
    3284           4 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    3293             : 
    3294             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    3295           1 :       int found_non_default_fallback = 0;
    3296           4 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 1);
    3305             : 
    3306             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    3307           1 :       int found_default_fallback = 0;
    3308           4 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    3331             : 
    3332             :     /* clear options*/
    3333           1 :     ads_clear_helper(options);
    3334           1 :     or_options_free(options);
    3335           1 :     options = options_new();
    3336             : 
    3337             :     /* clear any previous dir servers:
    3338             :      consider_adding_dir_servers() should do this anyway */
    3339           1 :     clear_dir_servers();
    3340             : 
    3341             :     /* assign options: 0010 */
    3342           1 :     options->DirAuthorities = NULL;
    3343           1 :     options->AlternateBridgeAuthority = NULL;
    3344           1 :     options->AlternateDirAuthority = test_alt_dir_authority;
    3345           1 :     options->FallbackDir = NULL;
    3346           1 :     options->UseDefaultFallbackDirs = 1;
    3347             : 
    3348             :     /* parse options - ensure we always update by passing NULL old_options */
    3349           1 :     consider_adding_dir_servers(options, NULL);
    3350             : 
    3351             :     /* check outcome */
    3352             : 
    3353             :     /* we must not have added the default fallback dirs */
    3354           1 :     tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
    3355             : 
    3356             :     /* we just have the authorities */
    3357           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
    3358             : 
    3359             :     {
    3360             :       /* trusted_dir_servers */
    3361           1 :       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           1 :       tt_assert(smartlist_len(dir_servers) ==
    3365             :                 1 + n_default_alt_bridge_authority);
    3366             : 
    3367             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3368           1 :       int found_D0 = 0;
    3369           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3378             : 
    3379             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3380           1 :       int found_B1 = 0;
    3381           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3390             : 
    3391             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    3392           1 :       int found_A2 = 0;
    3393           3 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    3416             :                 1 + n_default_alt_bridge_authority);
    3417             : 
    3418             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3419           1 :       int found_D0 = 0;
    3420           3 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3429             : 
    3430             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3431           1 :       int found_B1 = 0;
    3432           3 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3441             : 
    3442             :       /* AlternateDirAuthority - A2 - dir_port: 60092 */
    3443           1 :       int found_A2 = 0;
    3444           3 :       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           1 :       tt_int_op(found_A2, OP_EQ, 1);
    3453             : 
    3454             :       /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
    3455           1 :       int found_non_default_fallback = 0;
    3456           3 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 0);
    3465             : 
    3466             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    3467           1 :       int found_default_fallback = 0;
    3468           3 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    3499             : 
    3500             :     /* clear options*/
    3501           1 :     ads_clear_helper(options);
    3502           1 :     or_options_free(options);
    3503           1 :     options = options_new();
    3504             : 
    3505             :     /* clear any previous dir servers:
    3506             :      consider_adding_dir_servers() should do this anyway */
    3507           1 :     clear_dir_servers();
    3508             : 
    3509             :     /* assign options: 0001 */
    3510           1 :     options->DirAuthorities = NULL;
    3511           1 :     options->AlternateBridgeAuthority = NULL;
    3512           1 :     options->AlternateDirAuthority = NULL;
    3513           1 :     options->FallbackDir = test_fallback_directory;
    3514           1 :     options->UseDefaultFallbackDirs = 1;
    3515             : 
    3516             :     /* parse options - ensure we always update by passing NULL old_options */
    3517           1 :     consider_adding_dir_servers(options, NULL);
    3518             : 
    3519             :     /* check outcome */
    3520             : 
    3521             :     /* we must not have added the default fallback dirs */
    3522           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    3526             : 
    3527             :     {
    3528             :       /* trusted_dir_servers */
    3529           1 :       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           1 :       tt_assert(smartlist_len(dir_servers) == n_default_authorities);
    3533             : 
    3534             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3535           1 :       int found_D0 = 0;
    3536          11 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3545             : 
    3546             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3547           1 :       int found_B1 = 0;
    3548          11 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3557             : 
    3558             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3559           1 :       int found_A2 = 0;
    3560          11 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    3583             :                 1 + n_default_authorities);
    3584             : 
    3585             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3586           1 :       int found_D0 = 0;
    3587          12 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3596             : 
    3597             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3598           1 :       int found_B1 = 0;
    3599          12 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3608             : 
    3609             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3610           1 :       int found_A2 = 0;
    3611          12 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    3620             : 
    3621             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    3622           1 :       int found_non_default_fallback = 0;
    3623          12 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 1);
    3632             : 
    3633             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    3634           1 :       int found_default_fallback = 0;
    3635          12 :       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           1 :       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           1 :     n_add_default_fallback_dir_servers_known_default = 0;
    3664             : 
    3665             :     /* clear options*/
    3666           1 :     ads_clear_helper(options);
    3667           1 :     or_options_free(options);
    3668           1 :     options = options_new();
    3669             : 
    3670             :     /* clear any previous dir servers:
    3671             :      consider_adding_dir_servers() should do this anyway */
    3672           1 :     clear_dir_servers();
    3673             : 
    3674             :     /* assign options: 0001 */
    3675           1 :     options->DirAuthorities = NULL;
    3676           1 :     options->AlternateBridgeAuthority = NULL;
    3677           1 :     options->AlternateDirAuthority = NULL;
    3678           1 :     options->FallbackDir = NULL;
    3679           1 :     options->UseDefaultFallbackDirs = 1;
    3680             : 
    3681             :     /* parse options - ensure we always update by passing NULL old_options */
    3682           1 :     consider_adding_dir_servers(options, NULL);
    3683             : 
    3684             :     /* check outcome */
    3685             : 
    3686             :     /* we must have added the default fallback dirs */
    3687           1 :     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           1 :     tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
    3691             : 
    3692             :     {
    3693             :       /* trusted_dir_servers */
    3694           1 :       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           1 :       tt_assert(smartlist_len(dir_servers) == n_default_authorities);
    3698             : 
    3699             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3700           1 :       int found_D0 = 0;
    3701          11 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3710             : 
    3711             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3712           1 :       int found_B1 = 0;
    3713          11 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3722             : 
    3723             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3724           1 :       int found_A2 = 0;
    3725          11 :       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           1 :       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           1 :       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           1 :       tt_assert(smartlist_len(fallback_servers) ==
    3748             :                 n_default_authorities + n_default_fallback_dir);
    3749             : 
    3750             :       /* (No DirAuthorities) - D0 - dir_port: 60090 */
    3751           1 :       int found_D0 = 0;
    3752          12 :       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           1 :       tt_int_op(found_D0, OP_EQ, 0);
    3761             : 
    3762             :       /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
    3763           1 :       int found_B1 = 0;
    3764          12 :       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           1 :       tt_int_op(found_B1, OP_EQ, 0);
    3773             : 
    3774             :       /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
    3775           1 :       int found_A2 = 0;
    3776          12 :       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           1 :       tt_int_op(found_A2, OP_EQ, 0);
    3785             : 
    3786             :       /* Custom FallbackDir - No Nickname - dir_port: 60093 */
    3787           1 :       int found_non_default_fallback = 0;
    3788          12 :       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           1 :       tt_int_op(found_non_default_fallback, OP_EQ, 0);
    3797             : 
    3798             :       /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
    3799           1 :       int found_default_fallback = 0;
    3800          12 :       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           1 :       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           1 :   done:
    3819           1 :   clear_dir_servers();
    3820             : 
    3821           1 :   tor_free(test_dir_authority->key);
    3822           1 :   tor_free(test_dir_authority->value);
    3823           1 :   tor_free(test_dir_authority);
    3824             : 
    3825           1 :   tor_free(test_alt_dir_authority->key);
    3826           1 :   tor_free(test_alt_dir_authority->value);
    3827           1 :   tor_free(test_alt_dir_authority);
    3828             : 
    3829           1 :   tor_free(test_alt_bridge_authority->key);
    3830           1 :   tor_free(test_alt_bridge_authority->value);
    3831           1 :   tor_free(test_alt_bridge_authority);
    3832             : 
    3833           1 :   tor_free(test_fallback_directory->key);
    3834           1 :   tor_free(test_fallback_directory->value);
    3835           1 :   tor_free(test_fallback_directory);
    3836             : 
    3837           1 :   ads_clear_helper(options);
    3838           1 :   or_options_free(options);
    3839             : 
    3840           1 :   UNMOCK(add_default_fallback_dir_servers);
    3841           1 : }
    3842             : 
    3843             : static void
    3844           1 : test_config_default_dir_servers(void *arg)
    3845             : {
    3846           1 :   or_options_t *opts = NULL;
    3847           1 :   (void)arg;
    3848           1 :   int trusted_count = 0;
    3849           1 :   int fallback_count = 0;
    3850             : 
    3851             :   /* new set of options should stop fallback parsing */
    3852           1 :   opts = options_new();
    3853           1 :   opts->UseDefaultFallbackDirs = 0;
    3854             :   /* set old_options to NULL to force dir update */
    3855           1 :   consider_adding_dir_servers(opts, NULL);
    3856           1 :   trusted_count = smartlist_len(router_get_trusted_dir_servers());
    3857           1 :   fallback_count = smartlist_len(router_get_fallback_dir_servers());
    3858           1 :   or_options_free(opts);
    3859           1 :   opts = NULL;
    3860             : 
    3861             :   /* assume a release will never go out with less than 7 authorities */
    3862           1 :   tt_int_op(trusted_count, OP_GE, 7);
    3863             :   /* if we disable the default fallbacks, there must not be any extra */
    3864           1 :   tt_assert(fallback_count == trusted_count);
    3865             : 
    3866           1 :   opts = options_new();
    3867           1 :   opts->UseDefaultFallbackDirs = 1;
    3868           1 :   consider_adding_dir_servers(opts, opts);
    3869           1 :   trusted_count = smartlist_len(router_get_trusted_dir_servers());
    3870           1 :   fallback_count = smartlist_len(router_get_fallback_dir_servers());
    3871           1 :   or_options_free(opts);
    3872           1 :   opts = NULL;
    3873             : 
    3874             :   /* assume a release will never go out with less than 7 authorities */
    3875           1 :   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           1 :   tt_assert(fallback_count >= trusted_count);
    3879             : 
    3880           1 :  done:
    3881           1 :   or_options_free(opts);
    3882           1 : }
    3883             : 
    3884             : static bool mock_relay_find_addr_to_publish_result = true;
    3885             : 
    3886             : static bool
    3887           0 : mock_relay_find_addr_to_publish(const or_options_t *options, int family,
    3888             :                                 int flags, tor_addr_t *addr_out)
    3889             : {
    3890           0 :   (void) options;
    3891           0 :   (void) family;
    3892           0 :   (void) flags;
    3893           0 :   (void) addr_out;
    3894           0 :   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           9 : mock_router_my_exit_policy_is_reject_star(void)
    3901             : {
    3902           9 :   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           6 : mock_advertised_server_mode(void)
    3909             : {
    3910           6 :   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           5 : mock_router_get_my_routerinfo(void)
    3917             : {
    3918           5 :   return mock_router_get_my_routerinfo_result;
    3919             : }
    3920             : 
    3921             : static void
    3922           1 : test_config_directory_fetch(void *arg)
    3923             : {
    3924           1 :   (void)arg;
    3925             : 
    3926             :   /* Test Setup */
    3927           1 :   or_options_t *options = options_new();
    3928           1 :   routerinfo_t routerinfo;
    3929           1 :   memset(&routerinfo, 0, sizeof(routerinfo));
    3930           1 :   mock_relay_find_addr_to_publish_result = false;
    3931           1 :   mock_router_my_exit_policy_is_reject_star_result = 1;
    3932           1 :   mock_advertised_server_mode_result = 0;
    3933           1 :   mock_router_get_my_routerinfo_result = NULL;
    3934           1 :   MOCK(relay_find_addr_to_publish, mock_relay_find_addr_to_publish);
    3935           1 :   MOCK(router_my_exit_policy_is_reject_star,
    3936             :        mock_router_my_exit_policy_is_reject_star);
    3937           1 :   MOCK(advertised_server_mode, mock_advertised_server_mode);
    3938           1 :   MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
    3939           1 :   or_options_free(options);
    3940           1 :   options = options_new();
    3941             : 
    3942             :   /* Clients can use multiple directory mirrors for bootstrap */
    3943           1 :   options->ClientOnly = 1;
    3944           1 :   tt_assert(server_mode(options) == 0);
    3945           1 :   tt_assert(public_server_mode(options) == 0);
    3946           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    3947           1 :   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           1 :   or_options_free(options);
    3952           1 :   options = options_new();
    3953           1 :   options->UseBridges = 1;
    3954           1 :   tt_assert(server_mode(options) == 0);
    3955           1 :   tt_assert(public_server_mode(options) == 0);
    3956           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    3957           1 :   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           1 :   or_options_free(options);
    3963           1 :   options = options_new();
    3964           1 :   options->BridgeRelay = 1;
    3965           1 :   options->ORPort_set = 1;
    3966           1 :   tt_assert(server_mode(options) == 1);
    3967           1 :   tt_assert(public_server_mode(options) == 0);
    3968           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    3969           1 :   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           1 :   or_options_free(options);
    3975           1 :   options = options_new();
    3976           1 :   options->FetchDirInfoEarly = 1;
    3977           1 :   tt_assert(server_mode(options) == 0);
    3978           1 :   tt_assert(public_server_mode(options) == 0);
    3979           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
    3980           1 :   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           1 :   or_options_free(options);
    3987           1 :   options = options_new();
    3988           1 :   options->ORPort_set = 1;
    3989           1 :   options->ExitRelay = 1;
    3990           1 :   mock_relay_find_addr_to_publish_result = true;
    3991           1 :   mock_router_my_exit_policy_is_reject_star_result = 0;
    3992           1 :   mock_advertised_server_mode_result = 1;
    3993           1 :   mock_router_get_my_routerinfo_result = &routerinfo;
    3994             : 
    3995           1 :   routerinfo.supports_tunnelled_dir_requests = 1;
    3996             : 
    3997           1 :   options->RefuseUnknownExits = 1;
    3998           1 :   tt_assert(server_mode(options) == 1);
    3999           1 :   tt_assert(public_server_mode(options) == 1);
    4000           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
    4001           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4002             :             OP_EQ, 0);
    4003             : 
    4004           1 :   options->RefuseUnknownExits = 0;
    4005           1 :   mock_relay_find_addr_to_publish_result = true;
    4006           1 :   tt_assert(server_mode(options) == 1);
    4007           1 :   tt_assert(public_server_mode(options) == 1);
    4008           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    4009           1 :   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           1 :   or_options_free(options);
    4018           1 :   options = options_new();
    4019           1 :   options->DirPort_set = 1;
    4020           1 :   options->ORPort_set = 1;
    4021           1 :   options->DirCache = 1;
    4022           1 :   mock_relay_find_addr_to_publish_result = true;
    4023           1 :   mock_router_my_exit_policy_is_reject_star_result = 1;
    4024             : 
    4025           1 :   mock_advertised_server_mode_result = 1;
    4026           1 :   routerinfo.ipv4_dirport =  1;
    4027           1 :   mock_router_get_my_routerinfo_result = &routerinfo;
    4028           1 :   tt_assert(server_mode(options) == 1);
    4029           1 :   tt_assert(public_server_mode(options) == 1);
    4030           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
    4031           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4032             :             OP_EQ, 0);
    4033             : 
    4034           1 :   mock_advertised_server_mode_result = 0;
    4035           1 :   routerinfo.ipv4_dirport =  1;
    4036           1 :   mock_router_get_my_routerinfo_result = &routerinfo;
    4037           1 :   tt_assert(server_mode(options) == 1);
    4038           1 :   tt_assert(public_server_mode(options) == 1);
    4039           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    4040           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4041             :             OP_EQ, 0);
    4042             : 
    4043           1 :   mock_advertised_server_mode_result = 1;
    4044           1 :   mock_router_get_my_routerinfo_result = NULL;
    4045           1 :   tt_assert(server_mode(options) == 1);
    4046           1 :   tt_assert(public_server_mode(options) == 1);
    4047           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    4048           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4049             :             OP_EQ, 0);
    4050             : 
    4051           1 :   mock_advertised_server_mode_result = 1;
    4052           1 :   routerinfo.ipv4_dirport =  0;
    4053           1 :   routerinfo.supports_tunnelled_dir_requests = 0;
    4054           1 :   mock_router_get_my_routerinfo_result = &routerinfo;
    4055           1 :   tt_assert(server_mode(options) == 1);
    4056           1 :   tt_assert(public_server_mode(options) == 1);
    4057           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 0);
    4058           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4059             :             OP_EQ, 0);
    4060             : 
    4061           1 :   mock_advertised_server_mode_result = 1;
    4062           1 :   routerinfo.ipv4_dirport =  1;
    4063           1 :   routerinfo.supports_tunnelled_dir_requests = 1;
    4064           1 :   mock_router_get_my_routerinfo_result = &routerinfo;
    4065           1 :   tt_assert(server_mode(options) == 1);
    4066           1 :   tt_assert(public_server_mode(options) == 1);
    4067           1 :   tt_int_op(dirclient_fetches_from_authorities(options), OP_EQ, 1);
    4068           1 :   tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
    4069             :             OP_EQ, 0);
    4070             : 
    4071           1 :  done:
    4072           1 :   or_options_free(options);
    4073           1 :   UNMOCK(relay_find_addr_to_publish);
    4074           1 :   UNMOCK(router_get_my_routerinfo);
    4075           1 :   UNMOCK(advertised_server_mode);
    4076           1 :   UNMOCK(router_my_exit_policy_is_reject_star);
    4077           1 : }
    4078             : 
    4079             : static void
    4080           1 : test_config_default_fallback_dirs(void *arg)
    4081             : {
    4082           1 :   const char *fallback[] = {
    4083             : #ifndef COCCI
    4084             : #include "app/config/fallback_dirs.inc"
    4085             : #endif
    4086             :     NULL
    4087             :   };
    4088             : 
    4089           1 :   int n_included_fallback_dirs = 0;
    4090           1 :   int n_added_fallback_dirs = 0;
    4091             : 
    4092           1 :   (void)arg;
    4093           1 :   clear_dir_servers();
    4094             : 
    4095         201 :   while (fallback[n_included_fallback_dirs])
    4096         200 :     n_included_fallback_dirs++;
    4097             : 
    4098           1 :   add_default_fallback_dir_servers();
    4099             : 
    4100           1 :   n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
    4101             : 
    4102           1 :   tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
    4103             : 
    4104           1 :   done:
    4105           1 :   clear_dir_servers();
    4106           1 : }
    4107             : 
    4108             : static void
    4109           1 : test_config_port_cfg_line_extract_addrport(void *arg)
    4110             : {
    4111           1 :   (void)arg;
    4112           1 :   int unixy = 0;
    4113           1 :   const char *rest = NULL;
    4114           1 :   char *a = NULL;
    4115             : 
    4116           1 :   tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
    4117           1 :   tt_int_op(unixy, OP_EQ, 0);
    4118           1 :   tt_str_op(a, OP_EQ, "");
    4119           1 :   tt_str_op(rest, OP_EQ, "");
    4120           1 :   tor_free(a);
    4121             : 
    4122           1 :   tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
    4123             :             OP_EQ, 0);
    4124           1 :   tt_int_op(unixy, OP_EQ, 0);
    4125           1 :   tt_str_op(a, OP_EQ, "hello");
    4126           1 :   tt_str_op(rest, OP_EQ, "");
    4127           1 :   tor_free(a);
    4128             : 
    4129           1 :   tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
    4130             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4131           1 :   tt_int_op(unixy, OP_EQ, 0);
    4132           1 :   tt_str_op(a, OP_EQ, "flipperwalt");
    4133           1 :   tt_str_op(rest, OP_EQ, "gersplut");
    4134           1 :   tor_free(a);
    4135             : 
    4136           1 :   tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
    4137             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4138           1 :   tt_int_op(unixy, OP_EQ, 0);
    4139           1 :   tt_str_op(a, OP_EQ, "flipperwalt");
    4140           1 :   tt_str_op(rest, OP_EQ, "gersplut");
    4141           1 :   tor_free(a);
    4142             : 
    4143           1 :   tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
    4144             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4145           1 :   tt_int_op(unixy, OP_EQ, 0);
    4146           1 :   tt_str_op(a, OP_EQ, "flipperwalt");
    4147           1 :   tt_str_op(rest, OP_EQ, "gersplut");
    4148           1 :   tor_free(a);
    4149             : 
    4150           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
    4151             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4152           1 :   tt_int_op(unixy, OP_EQ, 1);
    4153           1 :   tt_str_op(a, OP_EQ, "flipperwalt");
    4154           1 :   tt_str_op(rest, OP_EQ, "gersplut");
    4155           1 :   tor_free(a);
    4156             : 
    4157           1 :   tt_int_op(port_cfg_line_extract_addrport("lolol",
    4158             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4159           1 :   tt_int_op(unixy, OP_EQ, 0);
    4160           1 :   tt_str_op(a, OP_EQ, "lolol");
    4161           1 :   tt_str_op(rest, OP_EQ, "");
    4162           1 :   tor_free(a);
    4163             : 
    4164           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
    4165             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4166           1 :   tt_int_op(unixy, OP_EQ, 1);
    4167           1 :   tt_str_op(a, OP_EQ, "lolol");
    4168           1 :   tt_str_op(rest, OP_EQ, "");
    4169           1 :   tor_free(a);
    4170             : 
    4171           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
    4172             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4173           1 :   tt_int_op(unixy, OP_EQ, 1);
    4174           1 :   tt_str_op(a, OP_EQ, "lolol");
    4175           1 :   tt_str_op(rest, OP_EQ, "");
    4176           1 :   tor_free(a);
    4177             : 
    4178           1 :   tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
    4179             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4180           1 :   tt_int_op(unixy, OP_EQ, 1);
    4181           1 :   tt_str_op(a, OP_EQ, "lolol");
    4182           1 :   tt_str_op(rest, OP_EQ, "");
    4183           1 :   tor_free(a);
    4184             : 
    4185           1 :   tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
    4186             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4187           1 :   tt_int_op(unixy, OP_EQ, 0);
    4188           1 :   tt_str_op(a, OP_EQ, "foobar:lolol");
    4189           1 :   tt_str_op(rest, OP_EQ, "");
    4190           1 :   tor_free(a);
    4191             : 
    4192           1 :   tt_int_op(port_cfg_line_extract_addrport(":lolol",
    4193             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4194           1 :   tt_int_op(unixy, OP_EQ, 0);
    4195           1 :   tt_str_op(a, OP_EQ, ":lolol");
    4196           1 :   tt_str_op(rest, OP_EQ, "");
    4197           1 :   tor_free(a);
    4198             : 
    4199           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
    4200             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4201           1 :   tt_int_op(unixy, OP_EQ, 1);
    4202           1 :   tt_str_op(a, OP_EQ, "lolol");
    4203           1 :   tt_str_op(rest, OP_EQ, "");
    4204           1 :   tor_free(a);
    4205             : 
    4206           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
    4207             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4208           1 :   tt_int_op(unixy, OP_EQ, 1);
    4209           1 :   tt_str_op(a, OP_EQ, "lolol");
    4210           1 :   tt_str_op(rest, OP_EQ, "");
    4211           1 :   tor_free(a);
    4212             : 
    4213           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
    4214             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4215           1 :   tt_int_op(unixy, OP_EQ, 1);
    4216           1 :   tt_str_op(a, OP_EQ, "lolol");
    4217           1 :   tt_str_op(rest, OP_EQ, "foo ");
    4218           1 :   tor_free(a);
    4219             : 
    4220           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
    4221             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4222           1 :   tt_int_op(unixy, OP_EQ, 1);
    4223           1 :   tt_str_op(a, OP_EQ, "lol ol");
    4224           1 :   tt_str_op(rest, OP_EQ, "foo ");
    4225           1 :   tor_free(a);
    4226             : 
    4227           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
    4228             :                                            &a, &unixy, &rest), OP_EQ, 0);
    4229           1 :   tt_int_op(unixy, OP_EQ, 1);
    4230           1 :   tt_str_op(a, OP_EQ, "lol\" ol");
    4231           1 :   tt_str_op(rest, OP_EQ, "foo ");
    4232           1 :   tor_free(a);
    4233             : 
    4234           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
    4235             :                                            &a, &unixy, &rest), OP_EQ, -1);
    4236           1 :   tor_free(a);
    4237             : 
    4238           1 :   tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
    4239             :                                            &a, &unixy, &rest), OP_EQ, -1);
    4240           1 :   tor_free(a);
    4241             : 
    4242           1 :  done:
    4243           1 :   tor_free(a);
    4244           1 : }
    4245             : 
    4246             : static config_line_t *
    4247          67 : mock_config_line(const char *key, const char *val)
    4248             : {
    4249          67 :   config_line_t *config_line = tor_malloc(sizeof(config_line_t));
    4250          67 :   memset(config_line, 0, sizeof(config_line_t));
    4251          67 :   config_line->key = tor_strdup(key);
    4252          67 :   config_line->value = tor_strdup(val);
    4253          67 :   return config_line;
    4254             : }
    4255             : 
    4256             : static void
    4257           1 : test_config_parse_port_config__ports__no_ports_given(void *data)
    4258             : {
    4259           1 :   (void)data;
    4260           1 :   int ret;
    4261           1 :   smartlist_t *slout = NULL;
    4262           1 :   port_cfg_t *port_cfg = NULL;
    4263             : 
    4264           1 :   slout = smartlist_new();
    4265             : 
    4266             :   // Test no defaultport, no defaultaddress and no out
    4267           1 :   ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
    4268           1 :   tt_int_op(ret, OP_EQ, 0);
    4269             : 
    4270             :   // Test with defaultport, no defaultaddress and no out
    4271           1 :   ret = port_parse_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
    4272           1 :   tt_int_op(ret, OP_EQ, 0);
    4273             : 
    4274             :   // Test no defaultport, with defaultaddress and no out
    4275           1 :   ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
    4276           1 :   tt_int_op(ret, OP_EQ, 0);
    4277             : 
    4278             :   // Test with defaultport, with defaultaddress and no out
    4279           1 :   ret = port_parse_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
    4280           1 :   tt_int_op(ret, OP_EQ, 0);
    4281             : 
    4282             :   // Test no defaultport, no defaultaddress and with out
    4283           1 :   ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 0, 0);
    4284           1 :   tt_int_op(ret, OP_EQ, 0);
    4285           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 0);
    4286             : 
    4287             :   // Test with defaultport, no defaultaddress and with out
    4288           1 :   ret = port_parse_config(slout, NULL, "DNS", 0, NULL, 42, 0);
    4289           1 :   tt_int_op(ret, OP_EQ, 0);
    4290           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 0);
    4291             : 
    4292             :   // Test no defaultport, with defaultaddress and with out
    4293           1 :   ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
    4294           1 :   tt_int_op(ret, OP_EQ, 0);
    4295           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 0);
    4296             : 
    4297             :   // Test with defaultport, with defaultaddress and out, adds a new port cfg
    4298           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4299           1 :   smartlist_clear(slout);
    4300           1 :   ret = port_parse_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
    4301           1 :   tt_int_op(ret, OP_EQ, 0);
    4302           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4303           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4304           1 :   tt_int_op(port_cfg->port, OP_EQ, 42);
    4305           1 :   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           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4310           1 :   smartlist_clear(slout);
    4311           1 :   ret = port_parse_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
    4312             :                           42, CL_PORT_IS_UNIXSOCKET);
    4313           1 :   tt_int_op(ret, OP_EQ, 0);
    4314           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4315           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4316           1 :   tt_int_op(port_cfg->port, OP_EQ, 0);
    4317           1 :   tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
    4318           1 :   tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
    4319             : 
    4320           1 :  done:
    4321           1 :   if (slout)
    4322           2 :     SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4323           1 :   smartlist_free(slout);
    4324           1 : }
    4325             : 
    4326             : static void
    4327           1 : test_config_parse_port_config__ports__ports_given(void *data)
    4328             : {
    4329           1 :   (void)data;
    4330           1 :   int ret;
    4331           1 :   smartlist_t *slout = NULL;
    4332           1 :   port_cfg_t *port_cfg = NULL;
    4333           1 :   config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
    4334           1 :   tor_addr_t addr;
    4335             : 
    4336           1 :   slout = smartlist_new();
    4337             : 
    4338           1 :   mock_hostname_resolver();
    4339             : 
    4340             :   // Test error when encounters an invalid Port specification
    4341           1 :   config_port_invalid = mock_config_line("DNSPort", "");
    4342           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL,
    4343             :                           0, 0);
    4344           1 :   tt_int_op(ret, OP_EQ, -1);
    4345             : 
    4346             :   // Test error when encounters an empty unix domain specification
    4347           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4348           1 :   config_port_invalid = mock_config_line("DNSPort", "unix:");
    4349           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0, NULL,
    4350             :                           0, 0);
    4351           1 :   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           1 :   config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
    4356           1 :   ret = port_parse_config(NULL, config_port_valid, "DNS",
    4357             :                           CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
    4358           1 :   tt_int_op(ret, OP_EQ, -1);
    4359             : 
    4360             :   // Test valid unix domain
    4361           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4362           1 :   smartlist_clear(slout);
    4363           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4369           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4370           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4371           1 :   tt_int_op(port_cfg->port, OP_EQ, 0);
    4372           1 :   tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
    4373           1 :   tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
    4374             :   /* Test entry port defaults as initialised in port_parse_config */
    4375           1 :   tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
    4376           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
    4377           1 :   tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
    4378           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
    4379           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4384           1 :   config_port_invalid = mock_config_line("SOCKSPort",
    4385             :                                          "unix:/tmp/foo/bar NoIPv4Traffic "
    4386             :                                          "NoIPv6Traffic "
    4387             :                                          "NoOnionTraffic");
    4388           1 :   ret = port_parse_config(NULL, config_port_invalid, "SOCKS",
    4389             :                           CONN_TYPE_AP_LISTENER, NULL, 0,
    4390             :                           CL_PORT_TAKES_HOSTNAMES);
    4391           1 :   tt_int_op(ret, OP_EQ, -1);
    4392             : 
    4393             :   // Test failure if we have no DNS and we're a DNSPort
    4394           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4395           1 :   config_port_invalid = mock_config_line("DNSPort",
    4396             :                                          "127.0.0.1:80 NoDNSRequest");
    4397           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS",
    4398             :                           CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
    4399             :                           CL_PORT_TAKES_HOSTNAMES);
    4400           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4405           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4406           1 :   smartlist_clear(slout);
    4407           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
    4408             :                                        "NoIPv6Traffic "
    4409             :                                        "NoIPv4Traffic NoOnionTraffic");
    4410           1 :   ret = port_parse_config(slout, config_port_valid, "DNS",
    4411             :                           CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
    4412             :                           CL_PORT_TAKES_HOSTNAMES);
    4413           1 :   tt_int_op(ret, OP_EQ, 0);
    4414           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4415           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4416           1 :   tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
    4417           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
    4418           1 :   tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
    4419           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4423           1 :   config_port_invalid = mock_config_line("SOCKSPort",
    4424             :                                          "NoIPv6Traffic "
    4425             :                                          "unix:/tmp/foo/bar NoIPv4Traffic");
    4426           1 :   ret = port_parse_config(NULL, config_port_invalid, "SOCKS",
    4427             :                           CONN_TYPE_AP_LISTENER, NULL, 0,
    4428             :                           CL_PORT_TAKES_HOSTNAMES);
    4429           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4434           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4435           1 :   smartlist_clear(slout);
    4436           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
    4437             :                                        "NoIPv6Traffic "
    4438             :                                        "NoDNSRequest NoIPv4Traffic");
    4439           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4446           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4447           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4448           1 :   tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
    4449           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
    4450           1 :   tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
    4451           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4456           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4457           1 :   smartlist_clear(slout);
    4458           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
    4459             :                                        "NoIPv6Traffic "
    4460             :                                        "NoDNSRequest NoIPv4Traffic");
    4461           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4468           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4469           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4470           1 :   tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
    4471           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
    4472           1 :   tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
    4473           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4478           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4479           1 :   smartlist_clear(slout);
    4480           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
    4481             :                                        "NoIPv6Traffic "
    4482             :                                        "NoDNSRequest NoIPv4Traffic");
    4483           1 :   ret = port_parse_config(slout, config_port_valid, "SOCKS",
    4484             :                           CONN_TYPE_AP_LISTENER, NULL, 0,
    4485             :                           CL_PORT_TAKES_HOSTNAMES);
    4486           1 :   tt_int_op(ret, OP_EQ, -1);
    4487             : 
    4488             :   // Test failure with empty quoted unix: address.
    4489           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4490           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4491           1 :   smartlist_clear(slout);
    4492           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
    4493             :                                        "NoIPv6Traffic "
    4494             :                                        "NoDNSRequest NoIPv4Traffic");
    4495           1 :   ret = port_parse_config(slout, config_port_valid, "SOCKS",
    4496             :                           CONN_TYPE_AP_LISTENER, NULL, 0,
    4497             :                           CL_PORT_TAKES_HOSTNAMES);
    4498           1 :   tt_int_op(ret, OP_EQ, -1);
    4499             : 
    4500             :   // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
    4501           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4502           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4503           1 :   smartlist_clear(slout);
    4504           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
    4505             :                                        "OnionTrafficOnly");
    4506           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4513           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4514           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4515           1 :   tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
    4516           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
    4517           1 :   tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
    4518           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4523           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4524           1 :   smartlist_clear(slout);
    4525           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
    4526             :                                        "NoIPv4Traffic IPv6Traffic");
    4527           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4534           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4535           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4536           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
    4537           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4542           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4543           1 :   smartlist_clear(slout);
    4544           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
    4545             :                                        "IPv4Traffic IPv6Traffic");
    4546           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4553           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4554           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4555           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
    4556           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4561           1 :   config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
    4562           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
    4563             :                           "127.0.0.3", 0, 0);
    4564           1 :   tt_int_op(ret, OP_EQ, -1);
    4565             : 
    4566             :   // Test failure if we specify group writable for an IP Port
    4567           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4568           1 :   config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
    4569           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
    4570             :                           "127.0.0.3", 0, 0);
    4571           1 :   tt_int_op(ret, OP_EQ, -1);
    4572             : 
    4573             :   // Test failure if we specify group writable for an IP Port
    4574           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4575           1 :   config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
    4576           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
    4577             :                           "127.0.0.3", 0, 0);
    4578           1 :   tt_int_op(ret, OP_EQ, -1);
    4579             : 
    4580             :   // Test success with only a port (this will fail without a default address)
    4581           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4582           1 :   config_port_valid = mock_config_line("DNSPort", "42");
    4583           1 :   ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
    4584             :                           "127.0.0.3", 0, 0);
    4585           1 :   tt_int_op(ret, OP_EQ, 0);
    4586             : 
    4587             :   // Test success with only a port and isolate destination port
    4588           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4589           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4590           1 :   smartlist_clear(slout);
    4591           1 :   config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
    4592           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4593             :                           "127.0.0.3", 0, 0);
    4594           1 :   tt_int_op(ret, OP_EQ, 0);
    4595           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4596           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4597           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4602           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4603           1 :   smartlist_clear(slout);
    4604           1 :   config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
    4605           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4606             :                           "127.0.0.3", 0, 0);
    4607           1 :   tt_int_op(ret, OP_EQ, 0);
    4608           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4609           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4610           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4615           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4616           1 :   smartlist_clear(slout);
    4617           1 :   config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
    4618           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4619             :                           "127.0.0.3", 0, 0);
    4620           1 :   tt_int_op(ret, OP_EQ, 0);
    4621           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4622           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4623           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4628           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4629           1 :   smartlist_clear(slout);
    4630           1 :   config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
    4631           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4632             :                           "127.0.0.3", 0, 0);
    4633           1 :   tt_int_op(ret, OP_EQ, 0);
    4634           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4635           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4636           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4641           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4642           1 :   smartlist_clear(slout);
    4643           1 :   config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
    4644           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4645             :                           "127.0.0.3", 0, 0);
    4646           1 :   tt_int_op(ret, OP_EQ, 0);
    4647           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4648           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4649           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4654           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4655           1 :   smartlist_clear(slout);
    4656           1 :   config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
    4657           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4658             :                           "127.0.0.3", 0, 0);
    4659           1 :   tt_int_op(ret, OP_EQ, 0);
    4660           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4661           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4662           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4667           1 :   config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
    4668           1 :   ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
    4669             :                           "127.0.0.3", 0, 0);
    4670           1 :   tt_int_op(ret, OP_EQ, 0);
    4671             : 
    4672             :   // Test success with no isolate socks AUTH
    4673           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4674           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4675           1 :   smartlist_clear(slout);
    4676           1 :   config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
    4677           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4678             :                           "127.0.0.3", 0, 0);
    4679           1 :   tt_int_op(ret, OP_EQ, 0);
    4680           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4681           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4682           1 :   tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
    4683             : 
    4684             :   // Test success with prefer ipv6
    4685           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4686           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4687           1 :   smartlist_clear(slout);
    4688           1 :   config_port_valid = mock_config_line("SOCKSPort",
    4689             :                                        "42 IPv6Traffic PreferIPv6");
    4690           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4694           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4695           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4696           1 :   tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
    4697             : 
    4698             :   // Test success with cache ipv4 DNS
    4699           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4700           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4701           1 :   smartlist_clear(slout);
    4702           1 :   config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
    4703           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4704             :                           "127.0.0.42", 0, 0);
    4705           1 :   tt_int_op(ret, OP_EQ, 0);
    4706           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4707           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4708           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
    4709           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
    4710             : 
    4711             :   // Test success with cache ipv6 DNS
    4712           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4713           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4714           1 :   smartlist_clear(slout);
    4715           1 :   config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
    4716           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4717             :                           "127.0.0.42", 0, 0);
    4718           1 :   tt_int_op(ret, OP_EQ, 0);
    4719           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4720           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4721           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
    4722           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
    4723             : 
    4724             :   // Test success with no cache ipv4 DNS
    4725           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4726           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4727           1 :   smartlist_clear(slout);
    4728           1 :   config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
    4729           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4730             :                           "127.0.0.42", 0, 0);
    4731           1 :   tt_int_op(ret, OP_EQ, 0);
    4732           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4733           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4734           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
    4735           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
    4736             : 
    4737             :   // Test success with cache DNS
    4738           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4739           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4740           1 :   smartlist_clear(slout);
    4741           1 :   config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
    4742           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4743             :                           "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
    4744           1 :   tt_int_op(ret, OP_EQ, 0);
    4745           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4746           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4747           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
    4748           1 :   tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
    4749             : 
    4750             :   // Test success with use cached ipv4 DNS
    4751           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4752           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4753           1 :   smartlist_clear(slout);
    4754           1 :   config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
    4755           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4756             :                           "127.0.0.42", 0, 0);
    4757           1 :   tt_int_op(ret, OP_EQ, 0);
    4758           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4759           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4760           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
    4761           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
    4762             : 
    4763             :   // Test success with use cached ipv6 DNS
    4764           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4765           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4766           1 :   smartlist_clear(slout);
    4767           1 :   config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
    4768           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4769             :                           "127.0.0.42", 0, 0);
    4770           1 :   tt_int_op(ret, OP_EQ, 0);
    4771           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4772           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4773           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
    4774           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
    4775             : 
    4776             :   // Test success with use cached DNS
    4777           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4778           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4779           1 :   smartlist_clear(slout);
    4780           1 :   config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
    4781           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4782             :                           "127.0.0.42", 0, 0);
    4783           1 :   tt_int_op(ret, OP_EQ, 0);
    4784           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4785           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4786           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
    4787           1 :   tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
    4788             : 
    4789             :   // Test success with not preferring ipv6 automap
    4790           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4791           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4792           1 :   smartlist_clear(slout);
    4793           1 :   config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
    4794           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4795             :                           "127.0.0.42", 0, 0);
    4796           1 :   tt_int_op(ret, OP_EQ, 0);
    4797           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4798           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4799           1 :   tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
    4800             : 
    4801             :   // Test success with prefer SOCKS no auth
    4802           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4803           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4804           1 :   smartlist_clear(slout);
    4805           1 :   config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
    4806           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4807             :                           "127.0.0.42", 0, 0);
    4808           1 :   tt_int_op(ret, OP_EQ, 0);
    4809           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4810           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4811           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4815           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4816           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4817           1 :   smartlist_clear(slout);
    4818           1 :   config_port_invalid = mock_config_line("DNSPort", "0");
    4819           1 :   config_port_valid = mock_config_line("DNSPort", "42");
    4820           1 :   config_port_invalid->next = config_port_valid;
    4821           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    4822             :                           "127.0.0.42", 0, 0);
    4823           1 :   tt_int_op(ret, OP_EQ, -1);
    4824             : 
    4825             :   // Test success with warn non-local control
    4826           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4827           1 :   smartlist_clear(slout);
    4828           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4832             : 
    4833             :   // Test success with warn non-local listener
    4834           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4835           1 :   smartlist_clear(slout);
    4836           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4840             : 
    4841             :   // Test success with warn non-local other
    4842           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4843           1 :   smartlist_clear(slout);
    4844           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4845             :                           "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
    4846           1 :   tt_int_op(ret, OP_EQ, 0);
    4847             : 
    4848             :   // Test success with warn non-local other without out
    4849           1 :   ret = port_parse_config(NULL, config_port_valid, "DNS", 0,
    4850             :                           "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
    4851           1 :   tt_int_op(ret, OP_EQ, 0);
    4852             : 
    4853             :   // Test success with both ipv4 and ipv6 but without stream options
    4854           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4855           1 :   config_port_valid = NULL;
    4856           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4857           1 :   smartlist_clear(slout);
    4858           1 :   config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
    4859             :                                        "IPv6Traffic");
    4860           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    4865           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4866           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4867           1 :   tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
    4868           1 :   tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
    4869             : 
    4870             :   // Test failure for a SessionGroup argument with invalid value
    4871           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4872           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4873           1 :   smartlist_clear(slout);
    4874           1 :   config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
    4875           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    4876             :                           "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
    4877           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4882           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4883           1 :   smartlist_clear(slout);
    4884           1 :   config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
    4885           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    4886             :                           "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
    4887           1 :   tt_int_op(ret, OP_EQ, -1);
    4888             : 
    4889             :   // Test failure for more than one SessionGroup argument
    4890           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4891           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4892           1 :   smartlist_clear(slout);
    4893           1 :   config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
    4894             :                                          "SessionGroup=321");
    4895           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    4896             :                           "127.0.0.44", 0, 0);
    4897           1 :   tt_int_op(ret, OP_EQ, -1);
    4898             : 
    4899             :   // Test success with a sessiongroup options
    4900           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4901           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4902           1 :   smartlist_clear(slout);
    4903           1 :   config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
    4904           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4905             :                           "127.0.0.44", 0, 0);
    4906           1 :   tt_int_op(ret, OP_EQ, 0);
    4907           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4908           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4909           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4913           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4914           1 :   smartlist_clear(slout);
    4915           1 :   config_port_valid = mock_config_line("DNSPort", "0");
    4916           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4917             :                           "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
    4918           1 :   tt_int_op(ret, OP_EQ, 0);
    4919           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4923           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4924           1 :   smartlist_clear(slout);
    4925           1 :   config_port_valid = mock_config_line("DNSPort", "something");
    4926           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4927             :                           "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
    4928           1 :   tt_int_op(ret, OP_EQ, 0);
    4929           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4930           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4931           1 :   tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
    4932           1 :   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           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4936           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4937           1 :   smartlist_clear(slout);
    4938           1 :   config_port_valid = mock_config_line("DNSPort", "auto");
    4939           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4940             :                           "127.0.0.46", 0, 0);
    4941           1 :   tt_int_op(ret, OP_EQ, 0);
    4942           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4943           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4944           1 :   tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
    4945           1 :   tor_addr_parse(&addr, "127.0.0.46");
    4946           1 :   tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
    4947             : 
    4948             :   // Test success with a port of auto in mixed case
    4949           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4950           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4951           1 :   smartlist_clear(slout);
    4952           1 :   config_port_valid = mock_config_line("DNSPort", "AuTo");
    4953           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4954             :                           "127.0.0.46", 0, 0);
    4955           1 :   tt_int_op(ret, OP_EQ, 0);
    4956           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4957           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4958           1 :   tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
    4959           1 :   tor_addr_parse(&addr, "127.0.0.46");
    4960           1 :   tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
    4961             : 
    4962             :   // Test success with parsing both an address and an auto port
    4963           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4964           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4965           1 :   smartlist_clear(slout);
    4966           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
    4967           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4968             :                           "127.0.0.46", 0, 0);
    4969           1 :   tt_int_op(ret, OP_EQ, 0);
    4970           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4971           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4972           1 :   tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
    4973           1 :   tor_addr_parse(&addr, "127.0.0.122");
    4974           1 :   tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
    4975             : 
    4976             :   // Test failure when asked to parse an invalid address followed by auto
    4977           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    4978           1 :   config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
    4979           1 :   MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
    4980           1 :   ret = port_parse_config(NULL, config_port_invalid, "DNS", 0,
    4981             :                           "127.0.0.46", 0, 0);
    4982           1 :   UNMOCK(tor_addr_lookup);
    4983           1 :   tt_int_op(ret, OP_EQ, -1);
    4984             : 
    4985             :   // Test success with parsing both an address and a real port
    4986           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    4987           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    4988           1 :   smartlist_clear(slout);
    4989           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
    4990           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0,
    4991             :                           "127.0.0.46", 0, 0);
    4992           1 :   tt_int_op(ret, OP_EQ, 0);
    4993           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    4994           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    4995           1 :   tt_int_op(port_cfg->port, OP_EQ, 656);
    4996           1 :   tor_addr_parse(&addr, "127.0.0.123");
    4997           1 :   tt_assert(tor_addr_eq(&port_cfg->addr, &addr));
    4998             : 
    4999             :   // Test failure if we can't parse anything at all
    5000           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5001           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5002           1 :   smartlist_clear(slout);
    5003           1 :   config_port_invalid = mock_config_line("DNSPort", "something wrong");
    5004           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    5005             :                           "127.0.0.46", 0, 0);
    5006           1 :   tt_int_op(ret, OP_EQ, -1);
    5007             : 
    5008             :   // Test failure if we find both an address, a port and an auto
    5009           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5010           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5011           1 :   smartlist_clear(slout);
    5012           1 :   config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
    5013           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0,
    5014             :                           "127.0.0.46", 0, 0);
    5015           1 :   tt_int_op(ret, OP_EQ, -1);
    5016             : 
    5017             :   // Test that default to group writeable default sets group writeable for
    5018             :   // domain socket
    5019           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5020           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5021           1 :   smartlist_clear(slout);
    5022           1 :   config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
    5023           1 :   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           1 :   tt_int_op(ret, OP_EQ, 0);
    5030           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    5031           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    5032           1 :   tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
    5033             : #endif /* defined(_WIN32) */
    5034             : 
    5035           1 :  done:
    5036           1 :   unmock_hostname_resolver();
    5037           1 :   if (slout)
    5038           2 :     SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5039           1 :   smartlist_free(slout);
    5040           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5041           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5042           1 : }
    5043             : 
    5044             : static void
    5045           1 : test_config_parse_port_config__ports__server_options(void *data)
    5046             : {
    5047           1 :   (void)data;
    5048           1 :   int ret;
    5049           1 :   smartlist_t *slout = NULL;
    5050           1 :   port_cfg_t *port_cfg = NULL;
    5051           1 :   config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
    5052             : 
    5053           1 :   slout = smartlist_new();
    5054             : 
    5055             :   // Test success with NoAdvertise option
    5056           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5057           1 :   config_port_valid = mock_config_line("DNSPort",
    5058             :                                        "127.0.0.124:656 NoAdvertise");
    5059           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
    5060             :                           CL_PORT_SERVER_OPTIONS);
    5061           1 :   tt_int_op(ret, OP_EQ, 0);
    5062           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    5063           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    5064           1 :   tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
    5065           1 :   tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
    5066             : 
    5067             :   // Test success with NoListen option
    5068           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5069           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5070           1 :   smartlist_clear(slout);
    5071           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
    5072           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
    5073             :                           CL_PORT_SERVER_OPTIONS);
    5074           1 :   tt_int_op(ret, OP_EQ, 0);
    5075           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    5076           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    5077           1 :   tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
    5078           1 :   tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
    5079             : 
    5080             :   // Test failure with both NoAdvertise and NoListen option
    5081           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5082           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5083           1 :   smartlist_clear(slout);
    5084           1 :   config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
    5085             :                                          "NoAdvertise");
    5086           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
    5087             :                           0, CL_PORT_SERVER_OPTIONS);
    5088           1 :   tt_int_op(ret, OP_EQ, -1);
    5089             : 
    5090             :   // Test success with IPv4Only
    5091           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5092           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5093           1 :   smartlist_clear(slout);
    5094           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
    5095           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
    5096             :                           CL_PORT_SERVER_OPTIONS);
    5097           1 :   tt_int_op(ret, OP_EQ, 0);
    5098           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    5099           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    5100           1 :   tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
    5101           1 :   tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
    5102             : 
    5103             :   // Test success with IPv6Only
    5104           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5105           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5106           1 :   smartlist_clear(slout);
    5107           1 :   config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
    5108           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
    5109             :                           CL_PORT_SERVER_OPTIONS);
    5110           1 :   tt_int_op(ret, OP_EQ, 0);
    5111           1 :   tt_int_op(smartlist_len(slout), OP_EQ, 1);
    5112           1 :   port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
    5113           1 :   tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
    5114           1 :   tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
    5115             : 
    5116             :   // Test failure with both IPv4Only and IPv6Only
    5117           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5118           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5119           1 :   smartlist_clear(slout);
    5120           1 :   config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
    5121             :                                          "IPv4Only");
    5122           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
    5123             :                           0, CL_PORT_SERVER_OPTIONS);
    5124           1 :   tt_int_op(ret, OP_EQ, -1);
    5125             : 
    5126             :   // Test success with invalid parameter
    5127           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5128           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5129           1 :   smartlist_clear(slout);
    5130           1 :   config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
    5131           1 :   ret = port_parse_config(slout, config_port_valid, "DNS", 0, NULL, 0,
    5132             :                           CL_PORT_SERVER_OPTIONS);
    5133           1 :   tt_int_op(ret, OP_EQ, 0);
    5134           1 :   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           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5138           2 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5139           1 :   smartlist_clear(slout);
    5140           1 :   config_port_invalid = mock_config_line("DNSPort",
    5141             :                                          "127.0.0.124:656 IPv6Only");
    5142           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
    5143             :                           0, CL_PORT_SERVER_OPTIONS);
    5144           1 :   tt_int_op(ret, OP_EQ, -1);
    5145             : 
    5146             :   // Test failure when asked to bind only to ipv4 but gets an ipv6 address
    5147           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5148           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5149           1 :   smartlist_clear(slout);
    5150           1 :   config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
    5151           1 :   ret = port_parse_config(slout, config_port_invalid, "DNS", 0, NULL,
    5152             :                           0, CL_PORT_SERVER_OPTIONS);
    5153           1 :   tt_int_op(ret, OP_EQ, -1);
    5154             : 
    5155             :   // Check for failure with empty unix: address.
    5156           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5157           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5158           1 :   smartlist_clear(slout);
    5159           1 :   config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
    5160           1 :   ret = port_parse_config(slout, config_port_invalid, "ORPort", 0, NULL,
    5161             :                           0, CL_PORT_SERVER_OPTIONS);
    5162           1 :   tt_int_op(ret, OP_EQ, -1);
    5163             : 
    5164             :   /* Default address is IPv4 but pass IPv6Only flag. Should be ignored. */
    5165           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5166           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5167           1 :   smartlist_clear(slout);
    5168           1 :   config_port_invalid = mock_config_line("ORPort", "9050 IPv6Only");
    5169           1 :   ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
    5170             :                           "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS);
    5171           1 :   tt_int_op(ret, OP_EQ, 0);
    5172             : 
    5173             :   /* Default address is IPv6 but pass IPv4Only flag. Should be ignored. */
    5174           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5175           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5176           1 :   smartlist_clear(slout);
    5177           1 :   config_port_invalid = mock_config_line("ORPort", "9050 IPv4Only");
    5178           1 :   ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
    5179             :                           "[::]", 0, CL_PORT_SERVER_OPTIONS);
    5180           1 :   tt_int_op(ret, OP_EQ, 0);
    5181             : 
    5182             :   /* Explicit address is IPv6 but pass IPv4Only flag. Should error. */
    5183           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5184           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5185           1 :   smartlist_clear(slout);
    5186           1 :   config_port_invalid = mock_config_line("ORPort",
    5187             :                                          "[4242::4242]:9050 IPv4Only");
    5188           1 :   ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
    5189             :                           "[::]", 0, CL_PORT_SERVER_OPTIONS);
    5190           1 :   tt_int_op(ret, OP_EQ, -1);
    5191             : 
    5192             :   /* Explicit address is IPv4 but pass IPv6Only flag. Should error. */
    5193           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5194           1 :   SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5195           1 :   smartlist_clear(slout);
    5196           1 :   config_port_invalid = mock_config_line("ORPort",
    5197             :                                          "1.2.3.4:9050 IPv6Only");
    5198           1 :   ret = port_parse_config(slout, config_port_invalid, "ORPort", 0,
    5199             :                           "127.0.0.1", 0, CL_PORT_SERVER_OPTIONS);
    5200           1 :   tt_int_op(ret, OP_EQ, -1);
    5201             : 
    5202           1 :  done:
    5203           1 :   if (slout)
    5204           1 :     SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
    5205           1 :   smartlist_free(slout);
    5206           1 :   config_free_lines(config_port_invalid); config_port_invalid = NULL;
    5207           1 :   config_free_lines(config_port_valid); config_port_valid = NULL;
    5208           1 : }
    5209             : 
    5210             : static void
    5211           1 : test_config_get_first_advertised(void *data)
    5212             : {
    5213           1 :   (void)data;
    5214           1 :   int r, w=0, n=0;
    5215           1 :   char *msg=NULL;
    5216           1 :   or_options_t *opts = options_new();
    5217           1 :   int port;
    5218           1 :   const tor_addr_t *addr;
    5219             : 
    5220             :   // no ports are configured? We get NULL.
    5221           1 :   port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
    5222             :                                               AF_INET);
    5223           1 :   tt_int_op(port, OP_EQ, 0);
    5224           1 :   addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
    5225             :                                               AF_INET);
    5226           1 :   tt_ptr_op(addr, OP_EQ, NULL);
    5227             : 
    5228           1 :   port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
    5229             :                                               AF_INET6);
    5230           1 :   tt_int_op(port, OP_EQ, 0);
    5231           1 :   addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
    5232             :                                               AF_INET6);
    5233           1 :   tt_ptr_op(addr, OP_EQ, NULL);
    5234             : 
    5235           1 :   config_line_append(&opts->ORPort_lines, "ORPort", "[1234::5678]:8080");
    5236           1 :   config_line_append(&opts->ORPort_lines, "ORPort",
    5237             :                      "1.2.3.4:9999 noadvertise");
    5238           1 :   config_line_append(&opts->ORPort_lines, "ORPort",
    5239             :                      "5.6.7.8:9911 nolisten");
    5240             : 
    5241           1 :   r = parse_ports(opts, 0, &msg, &n, &w);
    5242           1 :   tt_assert(r == 0);
    5243             : 
    5244             :   // UNSPEC gets us nothing.
    5245           1 :   port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
    5246             :                                               AF_UNSPEC);
    5247           1 :   tt_int_op(port, OP_EQ, 0);
    5248           1 :   addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
    5249             :                                               AF_UNSPEC);
    5250           1 :   tt_ptr_op(addr, OP_EQ, NULL);
    5251             : 
    5252             :   // Try AF_INET.
    5253           1 :   port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
    5254             :                                               AF_INET);
    5255           1 :   tt_int_op(port, OP_EQ, 9911);
    5256           1 :   addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
    5257             :                                               AF_INET);
    5258           1 :   tt_ptr_op(addr, OP_NE, NULL);
    5259           1 :   tt_str_op(fmt_addrport(addr,port), OP_EQ, "5.6.7.8:9911");
    5260             : 
    5261             :   // Try AF_INET6
    5262           1 :   port = portconf_get_first_advertised_port(CONN_TYPE_OR_LISTENER,
    5263             :                                               AF_INET6);
    5264           1 :   tt_int_op(port, OP_EQ, 8080);
    5265           1 :   addr = portconf_get_first_advertised_addr(CONN_TYPE_OR_LISTENER,
    5266             :                                               AF_INET6);
    5267           1 :   tt_ptr_op(addr, OP_NE, NULL);
    5268           1 :   tt_str_op(fmt_addrport(addr,port), OP_EQ, "[1234::5678]:8080");
    5269             : 
    5270           1 :  done:
    5271           1 :   or_options_free(opts);
    5272           1 :   config_free_all();
    5273           1 : }
    5274             : 
    5275             : static void
    5276           1 : test_config_parse_log_severity(void *data)
    5277             : {
    5278           1 :   int ret;
    5279           1 :   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           1 :   int i;
    5287           1 :   log_severity_list_t *severity;
    5288             : 
    5289           1 :   (void) data;
    5290             : 
    5291           1 :   severity = tor_malloc(sizeof(log_severity_list_t));
    5292           6 :   for (i = 0; severity_log_lines[i]; i++) {
    5293           4 :     memset(severity, 0, sizeof(log_severity_list_t));
    5294           4 :     ret = parse_log_severity_config(&severity_log_lines[i], severity);
    5295           4 :     tt_int_op(ret, OP_EQ, 0);
    5296             :   }
    5297             : 
    5298           1 :  done:
    5299           1 :   tor_free(severity);
    5300           1 : }
    5301             : 
    5302             : static void
    5303           1 : test_config_include_limit(void *data)
    5304             : {
    5305           1 :   (void)data;
    5306             : 
    5307           1 :   config_line_t *result = NULL;
    5308           1 :   char *torrc_path = NULL;
    5309           1 :   char *dir = tor_strdup(get_fname("test_include_limit"));
    5310           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5311             : 
    5312             : #ifdef _WIN32
    5313             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5314             : #else
    5315           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5316             : #endif
    5317             : 
    5318           1 :   tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
    5319           1 :   char torrc_contents[1000];
    5320           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
    5321             :                torrc_path);
    5322           1 :   tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
    5323             : 
    5324           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
    5325             :             OP_EQ, -1);
    5326             : 
    5327           1 :  done:
    5328           1 :   config_free_lines(result);
    5329           1 :   tor_free(torrc_path);
    5330           1 :   tor_free(dir);
    5331           1 : }
    5332             : 
    5333             : static void
    5334           1 : test_config_include_does_not_exist(void *data)
    5335             : {
    5336           1 :   (void)data;
    5337             : 
    5338           1 :   config_line_t *result = NULL;
    5339           1 :   char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
    5340           1 :   char *missing_path = NULL;
    5341           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5342             : 
    5343             : #ifdef _WIN32
    5344             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5345             : #else
    5346           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5347             : #endif
    5348             : 
    5349           1 :   tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
    5350           1 :   char torrc_contents[1000];
    5351           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
    5352             :                missing_path);
    5353             : 
    5354           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
    5355             :             OP_EQ, -1);
    5356             : 
    5357           1 :  done:
    5358           1 :   config_free_lines(result);
    5359           1 :   tor_free(dir);
    5360           1 :   tor_free(missing_path);
    5361           1 : }
    5362             : 
    5363             : static void
    5364           1 : test_config_include_error_in_included_file(void *data)
    5365             : {
    5366           1 :   (void)data;
    5367           1 :   config_line_t *result = NULL;
    5368             : 
    5369           1 :   char *dir = tor_strdup(get_fname("test_error_in_included_file"));
    5370           1 :   char *invalid_path = NULL;
    5371           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5372             : 
    5373             : #ifdef _WIN32
    5374             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5375             : #else
    5376           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5377             : #endif
    5378             : 
    5379           1 :   tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
    5380           1 :   tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
    5381             : 
    5382           1 :   char torrc_contents[1000];
    5383           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
    5384             :                invalid_path);
    5385             : 
    5386           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
    5387             :             OP_EQ, -1);
    5388             : 
    5389           1 :  done:
    5390           1 :   config_free_lines(result);
    5391           1 :   tor_free(dir);
    5392           1 :   tor_free(invalid_path);
    5393           1 : }
    5394             : 
    5395             : static void
    5396           1 : test_config_include_empty_file_folder(void *data)
    5397             : {
    5398           1 :   (void)data;
    5399           1 :   config_line_t *result = NULL;
    5400             : 
    5401           1 :   char *folder_path = NULL;
    5402           1 :   char *file_path = NULL;
    5403           1 :   char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
    5404           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5405             : 
    5406             : #ifdef _WIN32
    5407             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5408             : #else
    5409           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5410             : #endif
    5411             : 
    5412           1 :   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           1 :   tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
    5417             : #endif
    5418           1 :   tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
    5419           1 :   tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
    5420             : 
    5421           1 :   char torrc_contents[1000];
    5422           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    5423             :                "%%include %s\n"
    5424             :                "%%include %s\n",
    5425             :                folder_path, file_path);
    5426             : 
    5427           1 :   int include_used;
    5428           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
    5429             :             NULL), OP_EQ, 0);
    5430           1 :   tt_ptr_op(result, OP_EQ, NULL);
    5431           1 :   tt_int_op(include_used, OP_EQ, 1);
    5432             : 
    5433           1 :  done:
    5434           1 :   config_free_lines(result);
    5435           1 :   tor_free(folder_path);
    5436           1 :   tor_free(file_path);
    5437           1 :   tor_free(dir);
    5438           1 : }
    5439             : 
    5440             : #ifndef _WIN32
    5441             : static void
    5442           1 : test_config_include_no_permission(void *data)
    5443             : {
    5444           1 :   (void)data;
    5445           1 :   config_line_t *result = NULL;
    5446             : 
    5447           1 :   char *folder_path = NULL;
    5448           1 :   char *dir = NULL;
    5449           1 :   if (geteuid() == 0)
    5450           0 :     tt_skip();
    5451             : 
    5452           1 :   dir = tor_strdup(get_fname("test_include_forbidden_folder"));
    5453           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5454             : 
    5455           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5456             : 
    5457           1 :   tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
    5458           1 :   tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
    5459             : 
    5460           1 :   char torrc_contents[1000];
    5461           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    5462             :                "%%include %s\n",
    5463             :                folder_path);
    5464             : 
    5465           1 :   int include_used;
    5466           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
    5467             :                                      &include_used, NULL),
    5468             :             OP_EQ, -1);
    5469           1 :   tt_ptr_op(result, OP_EQ, NULL);
    5470             : 
    5471           1 :  done:
    5472           1 :   config_free_lines(result);
    5473           1 :   tor_free(folder_path);
    5474           1 :   if (dir)
    5475           1 :     chmod(dir, 0700);
    5476           1 :   tor_free(dir);
    5477           1 : }
    5478             : #endif /* !defined(_WIN32) */
    5479             : 
    5480             : static void
    5481           1 : test_config_include_recursion_before_after(void *data)
    5482             : {
    5483           1 :   (void)data;
    5484             : 
    5485           1 :   config_line_t *result = NULL;
    5486           1 :   char *torrc_path = NULL;
    5487           1 :   char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
    5488           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5489             : 
    5490             : #ifdef _WIN32
    5491             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5492             : #else
    5493           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5494             : #endif
    5495             : 
    5496           1 :   tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
    5497             : 
    5498           1 :   char file_contents[1000];
    5499           1 :   const int limit = MAX_INCLUDE_RECURSION_LEVEL;
    5500           1 :   int i;
    5501             :   // Loop backwards so file_contents has the contents of the first file by the
    5502             :   // end of the loop
    5503          33 :   for (i = limit; i > 0; i--) {
    5504          31 :     if (i < limit) {
    5505          30 :       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           1 :       tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
    5512             :     }
    5513             : 
    5514          31 :     if (i > 1) {
    5515          30 :       char *file_path = NULL;
    5516          30 :       tor_asprintf(&file_path, "%s%d", torrc_path, i);
    5517          30 :       tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
    5518          30 :       tor_free(file_path);
    5519             :     }
    5520             :   }
    5521             : 
    5522           1 :   int include_used;
    5523           1 :   tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
    5524             :             NULL), OP_EQ, 0);
    5525           1 :   tt_ptr_op(result, OP_NE, NULL);
    5526           1 :   tt_int_op(include_used, OP_EQ, 1);
    5527             : 
    5528           1 :   int len = 0;
    5529           1 :   config_line_t *next;
    5530          62 :   for (next = result; next != NULL; next = next->next) {
    5531          61 :     char expected[10];
    5532          61 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    5533          61 :     tt_str_op(next->key, OP_EQ, "Test");
    5534          61 :     tt_str_op(next->value, OP_EQ, expected);
    5535          61 :     len++;
    5536             :   }
    5537           1 :   tt_int_op(len, OP_EQ, 2 * limit - 1);
    5538             : 
    5539           1 :  done:
    5540           1 :   config_free_lines(result);
    5541           1 :   tor_free(dir);
    5542           1 :   tor_free(torrc_path);
    5543           1 : }
    5544             : 
    5545             : static void
    5546           1 : test_config_include_recursion_after_only(void *data)
    5547             : {
    5548           1 :   (void)data;
    5549             : 
    5550           1 :   config_line_t *result = NULL;
    5551           1 :   char *torrc_path = NULL;
    5552           1 :   char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
    5553           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5554             : 
    5555             : #ifdef _WIN32
    5556             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5557             : #else
    5558           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5559             : #endif
    5560             : 
    5561           1 :   tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
    5562             : 
    5563           1 :   char file_contents[1000];
    5564           1 :   const int limit = MAX_INCLUDE_RECURSION_LEVEL;
    5565           1 :   int i;
    5566             :   // Loop backwards so file_contents has the contents of the first file by the
    5567             :   // end of the loop
    5568          33 :   for (i = limit; i > 0; i--) {
    5569          31 :     int n = (i - limit - 1) * -1;
    5570          31 :     if (i < limit) {
    5571          30 :       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           1 :       tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
    5577             :     }
    5578             : 
    5579          31 :     if (i > 1) {
    5580          30 :       char *file_path = NULL;
    5581          30 :       tor_asprintf(&file_path, "%s%d", torrc_path, i);
    5582          30 :       tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
    5583          30 :       tor_free(file_path);
    5584             :     }
    5585             :   }
    5586             : 
    5587           1 :   int include_used;
    5588           1 :   tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
    5589             :             NULL), OP_EQ, 0);
    5590           1 :   tt_ptr_op(result, OP_NE, NULL);
    5591           1 :   tt_int_op(include_used, OP_EQ, 1);
    5592             : 
    5593           1 :   int len = 0;
    5594           1 :   config_line_t *next;
    5595          32 :   for (next = result; next != NULL; next = next->next) {
    5596          31 :     char expected[10];
    5597          31 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    5598          31 :     tt_str_op(next->key, OP_EQ, "Test");
    5599          31 :     tt_str_op(next->value, OP_EQ, expected);
    5600          31 :     len++;
    5601             :   }
    5602           1 :   tt_int_op(len, OP_EQ, limit);
    5603             : 
    5604           1 :  done:
    5605           1 :   config_free_lines(result);
    5606           1 :   tor_free(dir);
    5607           1 :   tor_free(torrc_path);
    5608           1 : }
    5609             : 
    5610             : static void
    5611           1 : test_config_include_folder_order(void *data)
    5612             : {
    5613           1 :   (void)data;
    5614             : 
    5615           1 :   config_line_t *result = NULL;
    5616           1 :   char *torrcd = NULL;
    5617           1 :   char *path = NULL;
    5618           1 :   char *path2 = NULL;
    5619           1 :   char *dir = tor_strdup(get_fname("test_include_folder_order"));
    5620           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5621             : 
    5622             : #ifdef _WIN32
    5623             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5624             : #else
    5625           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5626             : #endif
    5627             : 
    5628           1 :   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           1 :   tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
    5634             : #endif
    5635             : 
    5636             :   // test that files in subfolders are ignored
    5637           1 :   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           1 :   tt_int_op(mkdir(path, 0700), OP_EQ, 0);
    5643             : #endif
    5644             : 
    5645           1 :   tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
    5646           1 :   tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
    5647           1 :   tor_free(path);
    5648             : 
    5649             :   // test that files starting with . are ignored
    5650           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
    5651           1 :   tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
    5652           1 :   tor_free(path);
    5653             : 
    5654             :   // test file order
    5655           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
    5656           1 :   tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
    5657           1 :   tor_free(path);
    5658             : 
    5659           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
    5660           1 :   tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
    5661           1 :   tor_free(path);
    5662             : 
    5663           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
    5664           1 :   tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
    5665           1 :   tor_free(path);
    5666             : 
    5667           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
    5668           1 :   tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
    5669           1 :   tor_free(path);
    5670             : 
    5671           1 :   char torrc_contents[1000];
    5672           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    5673             :                "%%include %s\n",
    5674             :                torrcd);
    5675             : 
    5676           1 :   int include_used;
    5677           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    5678             :             NULL), OP_EQ, 0);
    5679           1 :   tt_ptr_op(result, OP_NE, NULL);
    5680           1 :   tt_int_op(include_used, OP_EQ, 1);
    5681             : 
    5682           1 :   int len = 0;
    5683           1 :   config_line_t *next;
    5684           5 :   for (next = result; next != NULL; next = next->next) {
    5685           4 :     char expected[10];
    5686           4 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    5687           4 :     tt_str_op(next->key, OP_EQ, "Test");
    5688           4 :     tt_str_op(next->value, OP_EQ, expected);
    5689           4 :     len++;
    5690             :   }
    5691           1 :   tt_int_op(len, OP_EQ, 4);
    5692             : 
    5693           1 :  done:
    5694           1 :   config_free_lines(result);
    5695           1 :   tor_free(torrcd);
    5696           1 :   tor_free(path);
    5697           1 :   tor_free(path2);
    5698           1 :   tor_free(dir);
    5699           1 : }
    5700             : 
    5701             : static void
    5702           1 : test_config_include_blank_file_last(void *data)
    5703             : {
    5704           1 :   (void)data;
    5705             : 
    5706           1 :   config_line_t *result = NULL;
    5707           1 :   char *torrcd = NULL;
    5708           1 :   char *path = NULL;
    5709           1 :   char *dir = tor_strdup(get_fname("test_include_blank_file_last"));
    5710           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5711             : 
    5712             : #ifdef _WIN32
    5713             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5714             : #else
    5715           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5716             : #endif
    5717             : 
    5718           1 :   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           1 :   tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
    5724             : #endif
    5725             : 
    5726           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_1st");
    5727           1 :   tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
    5728           1 :   tor_free(path);
    5729             : 
    5730           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "bb_2nd");
    5731           1 :   tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
    5732           1 :   tor_free(path);
    5733             : 
    5734           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "cc_comment");
    5735           1 :   tt_int_op(write_str_to_file(path, "# comment only\n", 0), OP_EQ, 0);
    5736           1 :   tor_free(path);
    5737             : 
    5738           1 :   char torrc_contents[1000];
    5739           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    5740             :                "%%include %s\n"
    5741             :                "Test 3\n",
    5742             :                torrcd);
    5743             : 
    5744           1 :   int include_used;
    5745           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    5746             :             NULL), OP_EQ, 0);
    5747           1 :   tt_ptr_op(result, OP_NE, NULL);
    5748           1 :   tt_int_op(include_used, OP_EQ, 1);
    5749             : 
    5750           1 :   int len = 0;
    5751           1 :   config_line_t *next;
    5752           4 :   for (next = result; next != NULL; next = next->next) {
    5753           3 :     char expected[10];
    5754           3 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    5755           3 :     tt_str_op(next->key, OP_EQ, "Test");
    5756           3 :     tt_str_op(next->value, OP_EQ, expected);
    5757           3 :     len++;
    5758             :   }
    5759           1 :   tt_int_op(len, OP_EQ, 3);
    5760             : 
    5761           1 :  done:
    5762           1 :   config_free_lines(result);
    5763           1 :   tor_free(torrcd);
    5764           1 :   tor_free(path);
    5765           1 :   tor_free(dir);
    5766           1 : }
    5767             : 
    5768             : static void
    5769           1 : test_config_include_path_syntax(void *data)
    5770             : {
    5771           1 :   (void)data;
    5772             : 
    5773           1 :   config_line_t *result = NULL;
    5774           1 :   char *dir = tor_strdup(get_fname("test_include_path_syntax"));
    5775           1 :   char *esc_dir = NULL, *dir_with_pathsep = NULL,
    5776           1 :     *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
    5777           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5778             : 
    5779             : #ifdef _WIN32
    5780             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5781             : #else
    5782           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5783             : #endif
    5784             : 
    5785           1 :   esc_dir = esc_for_log(dir);
    5786           1 :   tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
    5787           1 :   esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
    5788             : 
    5789           1 :   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           1 :   int include_used;
    5798           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
    5799             :             NULL), OP_EQ, 0);
    5800           1 :   tt_ptr_op(result, OP_EQ, NULL);
    5801           1 :   tt_int_op(include_used, OP_EQ, 1);
    5802             : 
    5803           1 :  done:
    5804           1 :   config_free_lines(result);
    5805           1 :   tor_free(dir);
    5806           1 :   tor_free(torrc_contents);
    5807           1 :   tor_free(esc_dir);
    5808           1 :   tor_free(dir_with_pathsep);
    5809           1 :   tor_free(esc_dir_with_pathsep);
    5810           1 : }
    5811             : 
    5812             : static void
    5813           1 : test_config_include_not_processed(void *data)
    5814             : {
    5815           1 :   (void)data;
    5816             : 
    5817           1 :   char torrc_contents[1000] = "%include does_not_exist\n";
    5818           1 :   config_line_t *result = NULL;
    5819           1 :   tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
    5820           1 :   tt_ptr_op(result, OP_NE, NULL);
    5821             : 
    5822           1 :   int len = 0;
    5823           1 :   config_line_t *next;
    5824           2 :   for (next = result; next != NULL; next = next->next) {
    5825           1 :     tt_str_op(next->key, OP_EQ, "%include");
    5826           1 :     tt_str_op(next->value, OP_EQ, "does_not_exist");
    5827           1 :     len++;
    5828             :   }
    5829           1 :   tt_int_op(len, OP_EQ, 1);
    5830             : 
    5831           1 :  done:
    5832           1 :   config_free_lines(result);
    5833           1 : }
    5834             : 
    5835             : static void
    5836           1 : test_config_include_has_include(void *data)
    5837             : {
    5838           1 :   (void)data;
    5839             : 
    5840           1 :   config_line_t *result = NULL;
    5841           1 :   char *dir = tor_strdup(get_fname("test_include_has_include"));
    5842           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5843             : 
    5844             : #ifdef _WIN32
    5845             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5846             : #else
    5847           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5848             : #endif
    5849             : 
    5850           1 :   char torrc_contents[1000] = "Test 1\n";
    5851           1 :   int include_used;
    5852             : 
    5853           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
    5854             :             NULL), OP_EQ, 0);
    5855           1 :   tt_int_op(include_used, OP_EQ, 0);
    5856           1 :   config_free_lines(result);
    5857             : 
    5858           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
    5859           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
    5860             :             NULL), OP_EQ, 0);
    5861           1 :   tt_int_op(include_used, OP_EQ, 1);
    5862             : 
    5863           1 :  done:
    5864           1 :   config_free_lines(result);
    5865           1 :   tor_free(dir);
    5866           1 : }
    5867             : 
    5868             : static void
    5869           1 : test_config_include_flag_both_without(void *data)
    5870             : {
    5871           1 :   (void)data;
    5872             : 
    5873           1 :   char *errmsg = NULL;
    5874           1 :   char conf_empty[1000];
    5875           1 :   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           1 :   int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
    5880             :                                      NULL, &errmsg);
    5881           1 :   tt_int_op(ret, OP_EQ, 0);
    5882             : 
    5883           1 :   const or_options_t *options = get_options();
    5884           1 :   tt_int_op(options->IncludeUsed, OP_EQ, 0);
    5885             : 
    5886           1 :  done:
    5887           1 :   tor_free(errmsg);
    5888           1 :   config_free_all();
    5889           1 : }
    5890             : 
    5891             : static void
    5892           1 : test_config_include_flag_torrc_only(void *data)
    5893             : {
    5894           1 :   (void)data;
    5895             : 
    5896           1 :   char *errmsg = NULL;
    5897           1 :   char *path = NULL;
    5898           1 :   char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
    5899           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5900             : 
    5901             : #ifdef _WIN32
    5902             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5903             : #else
    5904           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5905             : #endif
    5906             : 
    5907           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
    5908           1 :   tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
    5909             : 
    5910           1 :   char conf_empty[1000];
    5911           1 :   tor_snprintf(conf_empty, sizeof(conf_empty),
    5912             :                "DataDirectory %s\n",
    5913             :                get_fname(NULL));
    5914           1 :   char conf_include[1000];
    5915           1 :   tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
    5916             : 
    5917             :   // test with defaults-torrc without include and torrc with include
    5918           1 :   int ret = options_init_from_string(conf_empty, conf_include,
    5919             :                                      CMD_RUN_UNITTESTS, NULL, &errmsg);
    5920           1 :   tt_int_op(ret, OP_EQ, 0);
    5921             : 
    5922           1 :   const or_options_t *options = get_options();
    5923           1 :   tt_int_op(options->IncludeUsed, OP_EQ, 1);
    5924             : 
    5925           1 :  done:
    5926           1 :   tor_free(errmsg);
    5927           1 :   tor_free(path);
    5928           1 :   tor_free(dir);
    5929           1 :   config_free_all();
    5930           1 : }
    5931             : 
    5932             : static void
    5933           1 : test_config_include_flag_defaults_only(void *data)
    5934             : {
    5935           1 :   (void)data;
    5936             : 
    5937           1 :   char *errmsg = NULL;
    5938           1 :   char *path = NULL;
    5939           1 :   char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
    5940           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5941             : 
    5942             : #ifdef _WIN32
    5943             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5944             : #else
    5945           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5946             : #endif
    5947             : 
    5948           1 :   tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
    5949           1 :   tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
    5950             : 
    5951           1 :   char conf_empty[1000];
    5952           1 :   tor_snprintf(conf_empty, sizeof(conf_empty),
    5953             :                "DataDirectory %s\n",
    5954             :                get_fname(NULL));
    5955           1 :   char conf_include[1000];
    5956           1 :   tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
    5957             : 
    5958             :   // test with defaults-torrc with include and torrc without include
    5959           1 :   int ret = options_init_from_string(conf_include, conf_empty,
    5960             :                                      CMD_RUN_UNITTESTS, NULL, &errmsg);
    5961           1 :   tt_int_op(ret, OP_EQ, 0);
    5962             : 
    5963           1 :   const or_options_t *options = get_options();
    5964           1 :   tt_int_op(options->IncludeUsed, OP_EQ, 0);
    5965             : 
    5966           1 :  done:
    5967           1 :   tor_free(errmsg);
    5968           1 :   tor_free(path);
    5969           1 :   tor_free(dir);
    5970           1 :   config_free_all();
    5971           1 : }
    5972             : 
    5973             : static void
    5974           1 : test_config_include_wildcards(void *data)
    5975             : {
    5976           1 :   (void)data;
    5977             : 
    5978           1 :   char *temp = NULL, *folder = NULL;
    5979           1 :   config_line_t *result = NULL;
    5980           1 :   char *dir = tor_strdup(get_fname("test_include_wildcards"));
    5981           1 :   tt_ptr_op(dir, OP_NE, NULL);
    5982             : 
    5983             : #ifdef _WIN32
    5984             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    5985             : #else
    5986           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    5987             : #endif
    5988             : 
    5989           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "01_one.conf");
    5990           1 :   tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0);
    5991           1 :   tor_free(temp);
    5992             : 
    5993           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "02_two.conf");
    5994           1 :   tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0);
    5995           1 :   tor_free(temp);
    5996             : 
    5997           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "aa_three.conf");
    5998           1 :   tt_int_op(write_str_to_file(temp, "Test 3\n", 0), OP_EQ, 0);
    5999           1 :   tor_free(temp);
    6000             : 
    6001           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", dir, "foo");
    6002           1 :   tt_int_op(write_str_to_file(temp, "Test 6\n", 0), OP_EQ, 0);
    6003           1 :   tor_free(temp);
    6004             : 
    6005           1 :   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           1 :   tt_int_op(mkdir(folder, 0700), OP_EQ, 0);
    6011             : #endif
    6012             : 
    6013           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "04_four.conf");
    6014           1 :   tt_int_op(write_str_to_file(temp, "Test 4\n", 0), OP_EQ, 0);
    6015           1 :   tor_free(temp);
    6016             : 
    6017           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "05_five.conf");
    6018           1 :   tt_int_op(write_str_to_file(temp, "Test 5\n", 0), OP_EQ, 0);
    6019           1 :   tor_free(temp);
    6020             : 
    6021           1 :   char torrc_contents[1000];
    6022           1 :   int include_used;
    6023             : 
    6024             :   // test pattern that matches no file
    6025           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6026             :                "%%include %s"PATH_SEPARATOR"not-exist*\n",
    6027             :                dir);
    6028           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6029             :             NULL), OP_EQ, 0);
    6030           1 :   tt_ptr_op(result, OP_EQ, NULL);
    6031           1 :   tt_int_op(include_used, OP_EQ, 1);
    6032           1 :   config_free_lines(result);
    6033             : 
    6034             : #ifndef _WIN32
    6035             :   // test wildcard escaping
    6036           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6037             :                "%%include %s"PATH_SEPARATOR"\\*\n",
    6038             :                dir);
    6039           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6040             :             NULL), OP_EQ, -1);
    6041           1 :   tt_ptr_op(result, OP_EQ, NULL);
    6042           1 :   tt_int_op(include_used, OP_EQ, 1);
    6043           1 :   config_free_lines(result);
    6044             : #endif /* !defined(_WIN32) */
    6045             : 
    6046             :   // test pattern *.conf
    6047           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6048             :                "%%include %s"PATH_SEPARATOR"*.conf\n",
    6049             :                dir);
    6050           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6051             :             NULL), OP_EQ, 0);
    6052           1 :   tt_ptr_op(result, OP_NE, NULL);
    6053           1 :   tt_int_op(include_used, OP_EQ, 1);
    6054             : 
    6055           1 :   int len = 0;
    6056           1 :   config_line_t *next;
    6057           1 :   char expected[10];
    6058           4 :   for (next = result; next != NULL; next = next->next) {
    6059           3 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    6060           3 :     tt_str_op(next->key, OP_EQ, "Test");
    6061           3 :     tt_str_op(next->value, OP_EQ, expected);
    6062           3 :     len++;
    6063             :   }
    6064           1 :   tt_int_op(len, OP_EQ, 3);
    6065           1 :   config_free_lines(result);
    6066             : 
    6067             :   // test pattern that matches folder and files
    6068           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6069             :                "%%include %s"PATH_SEPARATOR"*\n",
    6070             :                dir);
    6071           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6072             :             NULL), OP_EQ, 0);
    6073           1 :   tt_ptr_op(result, OP_NE, NULL);
    6074           1 :   tt_int_op(include_used, OP_EQ, 1);
    6075             : 
    6076           1 :   len = 0;
    6077           7 :   for (next = result; next != NULL; next = next->next) {
    6078           6 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    6079           6 :     tt_str_op(next->key, OP_EQ, "Test");
    6080           6 :     tt_str_op(next->value, OP_EQ, expected);
    6081           6 :     len++;
    6082             :   }
    6083           1 :   tt_int_op(len, OP_EQ, 6);
    6084           1 :   config_free_lines(result);
    6085             : 
    6086             :   // test pattern ending in PATH_SEPARATOR, test linux path separator
    6087           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6088             :                "%%include %s/f*/\n",
    6089             :                dir);
    6090           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6091             :             NULL), OP_EQ, 0);
    6092           1 :   tt_ptr_op(result, OP_NE, NULL);
    6093           1 :   tt_int_op(include_used, OP_EQ, 1);
    6094             : 
    6095           1 :   len = 0;
    6096           3 :   for (next = result; next != NULL; next = next->next) {
    6097           2 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3);
    6098           2 :     tt_str_op(next->key, OP_EQ, "Test");
    6099           2 :     tt_str_op(next->value, OP_EQ, expected);
    6100           2 :     len++;
    6101             :   }
    6102           1 :   tt_int_op(len, OP_EQ, 2);
    6103           1 :   config_free_lines(result);
    6104             : 
    6105             :   // test pattern with wildcards in folder and file
    6106           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6107             :                "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*.conf\n",
    6108             :                dir);
    6109           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6110             :             NULL), OP_EQ, 0);
    6111           1 :   tt_ptr_op(result, OP_NE, NULL);
    6112           1 :   tt_int_op(include_used, OP_EQ, 1);
    6113             : 
    6114           1 :   len = 0;
    6115           3 :   for (next = result; next != NULL; next = next->next) {
    6116           2 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1 + 3);
    6117           2 :     tt_str_op(next->key, OP_EQ, "Test");
    6118           2 :     tt_str_op(next->value, OP_EQ, expected);
    6119           2 :     len++;
    6120             :   }
    6121           1 :   tt_int_op(len, OP_EQ, 2);
    6122           1 :   config_free_lines(result);
    6123             : 
    6124           1 :  done:
    6125           1 :   config_free_lines(result);
    6126           1 :   tor_free(folder);
    6127           1 :   tor_free(temp);
    6128           1 :   tor_free(dir);
    6129           1 : }
    6130             : 
    6131             : static void
    6132           1 : test_config_include_hidden(void *data)
    6133             : {
    6134           1 :   (void)data;
    6135             : 
    6136           1 :   char *temp = NULL, *folder = NULL;
    6137           1 :   config_line_t *result = NULL;
    6138           1 :   char *dir = tor_strdup(get_fname("test_include_hidden"));
    6139           1 :   tt_ptr_op(dir, OP_NE, NULL);
    6140             : 
    6141             : #ifdef _WIN32
    6142             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    6143             : #else
    6144           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    6145             : #endif
    6146             : 
    6147           1 :   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           1 :   tt_int_op(mkdir(folder, 0700), OP_EQ, 0);
    6153             : #endif
    6154             : 
    6155           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, ".dotfile");
    6156           1 :   tt_int_op(write_str_to_file(temp, "Test 1\n", 0), OP_EQ, 0);
    6157           1 :   tor_free(temp);
    6158             : 
    6159           1 :   tor_asprintf(&temp, "%s"PATH_SEPARATOR"%s", folder, "file");
    6160           1 :   tt_int_op(write_str_to_file(temp, "Test 2\n", 0), OP_EQ, 0);
    6161           1 :   tor_free(temp);
    6162             : 
    6163           1 :   char torrc_contents[1000];
    6164           1 :   int include_used;
    6165           1 :   int len = 0;
    6166           1 :   config_line_t *next;
    6167           1 :   char expected[10];
    6168             : 
    6169             :   // test wildcards do not expand to dot folders (except for windows)
    6170           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6171             :                "%%include %s"PATH_SEPARATOR"*\n",
    6172             :                dir);
    6173           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6174             :             NULL), OP_EQ, 0);
    6175           1 :   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           1 :   tt_ptr_op(result, OP_EQ, NULL);
    6186             : #endif /* defined(_WIN32) */
    6187           1 :   config_free_lines(result);
    6188             : 
    6189             :   // test wildcards match hidden folders when explicitly in the pattern
    6190           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6191             :                "%%include %s"PATH_SEPARATOR".*\n",
    6192             :                dir);
    6193           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6194             :             NULL), OP_EQ, 0);
    6195           1 :   tt_ptr_op(result, OP_NE, NULL);
    6196           1 :   tt_int_op(include_used, OP_EQ, 1);
    6197             : 
    6198           1 :   len = 0;
    6199           2 :   for (next = result; next != NULL; next = next->next) {
    6200           1 :     tor_snprintf(expected, sizeof(expected), "%d", len + 2);
    6201           1 :     tt_str_op(next->key, OP_EQ, "Test");
    6202           1 :     tt_str_op(next->value, OP_EQ, expected);
    6203           1 :     len++;
    6204             :   }
    6205           1 :   tt_int_op(len, OP_EQ, 1);
    6206           1 :   config_free_lines(result);
    6207             : 
    6208             :   // test hidden dir when explicitly included
    6209           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6210             :                "%%include %s"PATH_SEPARATOR".dotdir\n",
    6211             :                dir);
    6212           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6213             :             NULL), OP_EQ, 0);
    6214           1 :   tt_ptr_op(result, OP_NE, NULL);
    6215           1 :   tt_int_op(include_used, OP_EQ, 1);
    6216             : 
    6217           1 :   len = 0;
    6218           2 :   for (next = result; next != NULL; next = next->next) {
    6219           1 :     tor_snprintf(expected, sizeof(expected), "%d", len + 2);
    6220           1 :     tt_str_op(next->key, OP_EQ, "Test");
    6221           1 :     tt_str_op(next->value, OP_EQ, expected);
    6222           1 :     len++;
    6223             :   }
    6224           1 :   tt_int_op(len, OP_EQ, 1);
    6225           1 :   config_free_lines(result);
    6226             : 
    6227             :   // test hidden file when explicitly included
    6228           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6229             :                "%%include %s"PATH_SEPARATOR".dotdir"PATH_SEPARATOR".dotfile\n",
    6230             :                dir);
    6231           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6232             :             NULL), OP_EQ, 0);
    6233           1 :   tt_ptr_op(result, OP_NE, NULL);
    6234           1 :   tt_int_op(include_used, OP_EQ, 1);
    6235             : 
    6236           1 :   len = 0;
    6237           2 :   for (next = result; next != NULL; next = next->next) {
    6238           1 :     tor_snprintf(expected, sizeof(expected), "%d", len + 1);
    6239           1 :     tt_str_op(next->key, OP_EQ, "Test");
    6240           1 :     tt_str_op(next->value, OP_EQ, expected);
    6241           1 :     len++;
    6242             :   }
    6243           1 :   tt_int_op(len, OP_EQ, 1);
    6244           1 :   config_free_lines(result);
    6245             : 
    6246           1 :  done:
    6247           1 :   config_free_lines(result);
    6248           1 :   tor_free(folder);
    6249           1 :   tor_free(temp);
    6250           1 :   tor_free(dir);
    6251           1 : }
    6252             : 
    6253             : static void
    6254           1 : test_config_dup_and_filter(void *arg)
    6255             : {
    6256           1 :   (void)arg;
    6257             :   /* Test normal input. */
    6258           1 :   config_line_t *line = NULL;
    6259           1 :   config_line_append(&line, "abc", "def");
    6260           1 :   config_line_append(&line, "ghi", "jkl");
    6261           1 :   config_line_append(&line, "ABCD", "mno");
    6262             : 
    6263           1 :   config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
    6264           1 :   tt_ptr_op(line_dup, OP_NE, NULL);
    6265           1 :   tt_ptr_op(line_dup->next, OP_NE, NULL);
    6266           1 :   tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
    6267             : 
    6268           1 :   tt_str_op(line_dup->key, OP_EQ, "abc");
    6269           1 :   tt_str_op(line_dup->value, OP_EQ, "def");
    6270           1 :   tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
    6271           1 :   tt_str_op(line_dup->next->value, OP_EQ, "mno");
    6272             : 
    6273             :   /* empty output */
    6274           1 :   config_free_lines(line_dup);
    6275           1 :   line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
    6276           1 :   tt_ptr_op(line_dup, OP_EQ, NULL);
    6277             : 
    6278             :   /* empty input */
    6279           1 :   config_free_lines(line_dup);
    6280           1 :   line_dup = config_lines_dup_and_filter(NULL, "abc");
    6281           1 :   tt_ptr_op(line_dup, OP_EQ, NULL);
    6282             : 
    6283           1 :  done:
    6284           1 :   config_free_lines(line);
    6285           1 :   config_free_lines(line_dup);
    6286           1 : }
    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           1 : test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
    6292             : {
    6293           1 :   or_options_t* options = get_options_mutable();
    6294           1 :   or_options_t* old_options = options;
    6295           1 :   char* message = NULL;
    6296           1 :   int ret;
    6297             : 
    6298           1 :   (void)arg;
    6299             : 
    6300           1 :   options->BridgeRelay = 0;
    6301           1 :   options->BridgeDistribution = (char*)("https");
    6302             : 
    6303           1 :   ret = options_validate(old_options, options, &message);
    6304             : 
    6305           1 :   tt_int_op(ret, OP_EQ, -1);
    6306           1 :   tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
    6307             :             "didn't set BridgeRelay!");
    6308           1 :  done:
    6309           1 :   tor_free(message);
    6310           1 :   options->BridgeDistribution = NULL;
    6311           1 : }
    6312             : 
    6313             : /* If the BridgeDistribution setting was valid, 0 should be returned. */
    6314             : static void
    6315           1 : test_config_check_bridge_distribution_setting_valid(void *arg)
    6316             : {
    6317           1 :   (void)arg;
    6318             : 
    6319             :   // Check all the possible values we support right now.
    6320           1 :   tt_int_op(check_bridge_distribution_setting("none"), OP_EQ, 0);
    6321           1 :   tt_int_op(check_bridge_distribution_setting("any"), OP_EQ, 0);
    6322           1 :   tt_int_op(check_bridge_distribution_setting("https"), OP_EQ, 0);
    6323           1 :   tt_int_op(check_bridge_distribution_setting("email"), OP_EQ, 0);
    6324           1 :   tt_int_op(check_bridge_distribution_setting("moat"), OP_EQ, 0);
    6325             : 
    6326             :   // Check all the possible values we support right now with weird casing.
    6327           1 :   tt_int_op(check_bridge_distribution_setting("NoNe"), OP_EQ, 0);
    6328           1 :   tt_int_op(check_bridge_distribution_setting("anY"), OP_EQ, 0);
    6329           1 :   tt_int_op(check_bridge_distribution_setting("hTTps"), OP_EQ, 0);
    6330           1 :   tt_int_op(check_bridge_distribution_setting("emAIl"), OP_EQ, 0);
    6331           1 :   tt_int_op(check_bridge_distribution_setting("moAt"), OP_EQ, 0);
    6332             : 
    6333             :   // Invalid values.
    6334           1 :   tt_int_op(check_bridge_distribution_setting("x\rx"), OP_EQ, -1);
    6335           1 :   tt_int_op(check_bridge_distribution_setting("x\nx"), OP_EQ, -1);
    6336           1 :   tt_int_op(check_bridge_distribution_setting("\t\t\t"), OP_EQ, -1);
    6337             : 
    6338           1 :  done:
    6339           1 :   return;
    6340             : }
    6341             : 
    6342             : /* If the BridgeDistribution setting was invalid, -1 should be returned. */
    6343             : static void
    6344           1 : test_config_check_bridge_distribution_setting_invalid(void *arg)
    6345             : {
    6346           1 :   int ret = check_bridge_distribution_setting("hyphens-are-allowed");
    6347             : 
    6348           1 :   (void)arg;
    6349             : 
    6350           1 :   tt_int_op(ret, OP_EQ, 0);
    6351             : 
    6352           1 :   ret = check_bridge_distribution_setting("asterisks*are*forbidden");
    6353             : 
    6354           1 :   tt_int_op(ret, OP_EQ, -1);
    6355           1 :  done:
    6356           1 :   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           1 : test_config_check_bridge_distribution_setting_unrecognised(void *arg)
    6363             : {
    6364           1 :   int ret = check_bridge_distribution_setting("unicorn");
    6365             : 
    6366           1 :   (void)arg;
    6367             : 
    6368           1 :   tt_int_op(ret, OP_EQ, 0);
    6369           1 :  done:
    6370           1 :   return;
    6371             : }
    6372             : 
    6373             : static void
    6374           1 : test_config_include_opened_file_list(void *data)
    6375             : {
    6376           1 :   (void)data;
    6377             : 
    6378           1 :   config_line_t *result = NULL;
    6379           1 :   smartlist_t *opened_files = smartlist_new();
    6380           1 :   char *torrcd = NULL;
    6381           1 :   char *subfolder = NULL;
    6382           1 :   char *in_subfolder = NULL;
    6383           1 :   char *empty = NULL;
    6384           1 :   char *file = NULL;
    6385           1 :   char *dot = NULL;
    6386           1 :   char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
    6387           1 :   tt_ptr_op(dir, OP_NE, NULL);
    6388             : 
    6389             : #ifdef _WIN32
    6390             :   tt_int_op(mkdir(dir), OP_EQ, 0);
    6391             : #else
    6392           1 :   tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
    6393             : #endif
    6394             : 
    6395           1 :   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           1 :   tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
    6401             : #endif
    6402             : 
    6403           1 :   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           1 :   tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
    6409             : #endif
    6410             : 
    6411           1 :   tor_asprintf(&in_subfolder, "%s"PATH_SEPARATOR"%s", subfolder,
    6412             :                "01_file_in_subfolder");
    6413           1 :   tt_int_op(write_str_to_file(in_subfolder, "Test 1\n", 0), OP_EQ, 0);
    6414             : 
    6415           1 :   tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty");
    6416           1 :   tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
    6417             : 
    6418           1 :   tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file");
    6419           1 :   tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
    6420             : 
    6421           1 :   tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
    6422           1 :   tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
    6423             : 
    6424           1 :   char torrc_contents[1000];
    6425           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6426             :                "%%include %s\n",
    6427             :                torrcd);
    6428             : 
    6429           1 :   int include_used;
    6430           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6431             :             opened_files), OP_EQ, 0);
    6432           1 :   tt_ptr_op(result, OP_NE, NULL);
    6433           1 :   tt_int_op(include_used, OP_EQ, 1);
    6434             : 
    6435           1 :   tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
    6436           1 :   tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
    6437           1 :   tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
    6438             :   // files inside subfolders are not opended, only the subfolder is opened
    6439           1 :   tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
    6440           1 :   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           5 :   SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
    6446           1 :   smartlist_clear(opened_files);
    6447           1 :   config_free_lines(result);
    6448           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6449             :                "%%include %s"PATH_SEPARATOR"*\n",
    6450             :                torrcd);
    6451           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6452             :             opened_files), OP_EQ, 0);
    6453           1 :   tt_ptr_op(result, OP_NE, NULL);
    6454           1 :   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           1 :   tt_int_op(smartlist_len(opened_files), OP_EQ, 5);
    6460             : #endif
    6461           1 :   tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
    6462           1 :   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           1 :   tt_int_op(smartlist_contains_string(opened_files, in_subfolder), OP_EQ, 1);
    6465           1 :   tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
    6466           1 :   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           6 :   SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
    6474           1 :   smartlist_clear(opened_files);
    6475           1 :   config_free_lines(result);
    6476           1 :   tor_snprintf(torrc_contents, sizeof(torrc_contents),
    6477             :                "%%include %s"PATH_SEPARATOR"*"PATH_SEPARATOR"*\n",
    6478             :                torrcd);
    6479           1 :   tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
    6480             :             opened_files), OP_EQ, 0);
    6481           1 :   tt_ptr_op(result, OP_NE, NULL);
    6482           1 :   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           1 :   tt_int_op(smartlist_len(opened_files), OP_EQ, 5);
    6488             : #endif
    6489           1 :   tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
    6490           1 :   tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
    6491           1 :   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           1 :   tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
    6494           1 :   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           1 :  done:
    6501           6 :   SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
    6502           1 :   smartlist_free(opened_files);
    6503           1 :   config_free_lines(result);
    6504           1 :   tor_free(torrcd);
    6505           1 :   tor_free(subfolder);
    6506           1 :   tor_free(in_subfolder);
    6507           1 :   tor_free(empty);
    6508           1 :   tor_free(file);
    6509           1 :   tor_free(dot);
    6510           1 :   tor_free(dir);
    6511           1 : }
    6512             : 
    6513             : static void
    6514           1 : 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           1 :   (void)data;
    6519           1 :   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           1 :   total_system_memory_return = -1;
    6524             : 
    6525             : #if SIZEOF_VOID_P >= 8
    6526             :   /* We are on a 64-bit system. */
    6527           1 :   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           1 :   total_system_memory_return = 0;
    6535             : 
    6536             :   /* We are running on a system with one gigabyte of RAM. */
    6537           1 :   total_system_memory_output = GIGABYTE(1);
    6538             : 
    6539             :   /* We have 0.75 * RAM available. */
    6540           1 :   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           1 :   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           1 :   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           1 :   total_system_memory_output = GIGABYTE(8);
    6554             : 
    6555             :   /* We will have 0.4 * RAM available. */
    6556           1 :   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           1 :   total_system_memory_output = GIGABYTE(16);
    6561             : 
    6562             :   /* We will have 0.4 * RAM available. */
    6563           1 :   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           1 :   total_system_memory_output = GIGABYTE(32);
    6568             : 
    6569             :   /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
    6570           1 :   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           1 :  done:
    6575           1 :   UNMOCK(get_total_system_memory);
    6576             : 
    6577             : #undef GIGABYTE
    6578             : #undef MEGABYTE
    6579           1 : }
    6580             : 
    6581             : static void
    6582           1 : test_config_extended_fmt(void *arg)
    6583             : {
    6584           1 :   (void)arg;
    6585           1 :   config_line_t *lines = NULL, *lp;
    6586           1 :   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           1 :   int r = config_get_lines(string1, &lines, 0);
    6594           1 :   tt_int_op(r, OP_EQ, 0);
    6595           1 :   lp = lines;
    6596           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6597           1 :   tt_str_op(lp->key, OP_EQ, "thing1");
    6598           1 :   tt_str_op(lp->value, OP_EQ, "is here");
    6599           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
    6600           1 :   lp = lp->next;
    6601           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6602           1 :   tt_str_op(lp->key, OP_EQ, "+thing2");
    6603           1 :   tt_str_op(lp->value, OP_EQ, "is over here");
    6604           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
    6605           1 :   lp = lp->next;
    6606           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6607           1 :   tt_str_op(lp->key, OP_EQ, "/thing3");
    6608           1 :   tt_str_op(lp->value, OP_EQ, "");
    6609           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
    6610           1 :   lp = lp->next;
    6611           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6612           1 :   tt_str_op(lp->key, OP_EQ, "/thing4");
    6613           1 :   tt_str_op(lp->value, OP_EQ, "is back here");
    6614           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
    6615           1 :   lp = lp->next;
    6616           1 :   tt_assert(!lp);
    6617           1 :   config_free_lines(lines);
    6618             : 
    6619             :   /* Try with the "extended" flag enabled. */
    6620           1 :   r = config_get_lines(string1, &lines, 1);
    6621           1 :   tt_int_op(r, OP_EQ, 0);
    6622           1 :   lp = lines;
    6623           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6624           1 :   tt_str_op(lp->key, OP_EQ, "thing1");
    6625           1 :   tt_str_op(lp->value, OP_EQ, "is here");
    6626           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
    6627           1 :   lp = lp->next;
    6628           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6629           1 :   tt_str_op(lp->key, OP_EQ, "thing2");
    6630           1 :   tt_str_op(lp->value, OP_EQ, "is over here");
    6631           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_APPEND);
    6632           1 :   lp = lp->next;
    6633           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6634           1 :   tt_str_op(lp->key, OP_EQ, "thing3");
    6635           1 :   tt_str_op(lp->value, OP_EQ, "");
    6636           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
    6637           1 :   lp = lp->next;
    6638           1 :   tt_ptr_op(lp, OP_NE, NULL);
    6639           1 :   tt_str_op(lp->key, OP_EQ, "thing4");
    6640           1 :   tt_str_op(lp->value, OP_EQ, "");
    6641           1 :   tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
    6642           1 :   lp = lp->next;
    6643           1 :   tt_assert(!lp);
    6644             : 
    6645           1 :  done:
    6646           1 :   config_free_lines(lines);
    6647           1 : }
    6648             : 
    6649             : static void
    6650           1 : test_config_kvline_parse(void *arg)
    6651             : {
    6652           1 :   (void)arg;
    6653             : 
    6654           1 :   config_line_t *lines = NULL;
    6655           1 :   char *enc = NULL;
    6656             : 
    6657           1 :   lines = kvline_parse("A=B CD=EF", 0);
    6658           1 :   tt_assert(lines);
    6659           1 :   tt_str_op(lines->key, OP_EQ, "A");
    6660           1 :   tt_str_op(lines->value, OP_EQ, "B");
    6661           1 :   tt_str_op(lines->next->key, OP_EQ, "CD");
    6662           1 :   tt_str_op(lines->next->value, OP_EQ, "EF");
    6663           1 :   enc = kvline_encode(lines, 0);
    6664           1 :   tt_str_op(enc, OP_EQ, "A=B CD=EF");
    6665           1 :   tor_free(enc);
    6666           1 :   enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
    6667           1 :   tt_str_op(enc, OP_EQ, "A=B CD=EF");
    6668           1 :   tor_free(enc);
    6669           1 :   config_free_lines(lines);
    6670             : 
    6671           1 :   lines = kvline_parse("AB CDE=F", 0);
    6672           1 :   tt_assert(! lines);
    6673             : 
    6674           1 :   lines = kvline_parse("AB CDE=F", KV_OMIT_KEYS);
    6675           1 :   tt_assert(lines);
    6676           1 :   tt_str_op(lines->key, OP_EQ, "");
    6677           1 :   tt_str_op(lines->value, OP_EQ, "AB");
    6678           1 :   tt_str_op(lines->next->key, OP_EQ, "CDE");
    6679           1 :   tt_str_op(lines->next->value, OP_EQ, "F");
    6680           1 :   tt_assert(lines);
    6681           1 :   enc = kvline_encode(lines, 0);
    6682           1 :   tt_assert(!enc);
    6683           1 :   enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
    6684           1 :   tt_str_op(enc, OP_EQ, "AB CDE=F");
    6685           1 :   tor_free(enc);
    6686           1 :   config_free_lines(lines);
    6687             : 
    6688           1 :   lines = kvline_parse("AB=C CDE=\"F G\"", 0);
    6689           1 :   tt_assert(!lines);
    6690             : 
    6691           1 :   lines = kvline_parse("AB=C CDE=\"F G\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
    6692           1 :   tt_assert(lines);
    6693           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6694           1 :   tt_str_op(lines->value, OP_EQ, "C");
    6695           1 :   tt_str_op(lines->next->key, OP_EQ, "CDE");
    6696           1 :   tt_str_op(lines->next->value, OP_EQ, "F G");
    6697           1 :   tt_str_op(lines->next->next->key, OP_EQ, "");
    6698           1 :   tt_str_op(lines->next->next->value, OP_EQ, "GHI");
    6699           1 :   enc = kvline_encode(lines, 0);
    6700           1 :   tt_assert(!enc);
    6701           1 :   enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
    6702           1 :   tt_str_op(enc, OP_EQ, "AB=C CDE=\"F G\" GHI");
    6703           1 :   tor_free(enc);
    6704           1 :   config_free_lines(lines);
    6705             : 
    6706           1 :   lines = kvline_parse("A\"B=C CDE=\"F\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
    6707           1 :   tt_assert(! lines);
    6708             : 
    6709           1 :   lines = kvline_parse("AB=", KV_QUOTED);
    6710           1 :   tt_assert(lines);
    6711           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6712           1 :   tt_str_op(lines->value, OP_EQ, "");
    6713           1 :   config_free_lines(lines);
    6714             : 
    6715           1 :   lines = kvline_parse("AB=", 0);
    6716           1 :   tt_assert(lines);
    6717           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6718           1 :   tt_str_op(lines->value, OP_EQ, "");
    6719           1 :   config_free_lines(lines);
    6720             : 
    6721           1 :   lines = kvline_parse("AB=", KV_OMIT_VALS);
    6722           1 :   tt_assert(lines);
    6723           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6724           1 :   tt_str_op(lines->value, OP_EQ, "");
    6725           1 :   config_free_lines(lines);
    6726             : 
    6727           1 :   lines = kvline_parse(" AB ", KV_OMIT_VALS);
    6728           1 :   tt_assert(lines);
    6729           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6730           1 :   tt_str_op(lines->value, OP_EQ, "");
    6731           1 :   config_free_lines(lines);
    6732             : 
    6733           1 :   lines = kvline_parse("AB", KV_OMIT_VALS);
    6734           1 :   tt_assert(lines);
    6735           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6736           1 :   tt_str_op(lines->value, OP_EQ, "");
    6737           1 :   enc = kvline_encode(lines, KV_OMIT_VALS);
    6738           1 :   tt_str_op(enc, OP_EQ, "AB");
    6739           1 :   tor_free(enc);
    6740           1 :   config_free_lines(lines);
    6741             : 
    6742           1 :   lines = kvline_parse("AB=CD", KV_OMIT_VALS);
    6743           1 :   tt_assert(lines);
    6744           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6745           1 :   tt_str_op(lines->value, OP_EQ, "CD");
    6746           1 :   enc = kvline_encode(lines, KV_OMIT_VALS);
    6747           1 :   tt_str_op(enc, OP_EQ, "AB=CD");
    6748           1 :   tor_free(enc);
    6749           1 :   config_free_lines(lines);
    6750             : 
    6751           1 :   lines = kvline_parse("AB=CD DE FGH=I", KV_OMIT_VALS);
    6752           1 :   tt_assert(lines);
    6753           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6754           1 :   tt_str_op(lines->value, OP_EQ, "CD");
    6755           1 :   tt_str_op(lines->next->key, OP_EQ, "DE");
    6756           1 :   tt_str_op(lines->next->value, OP_EQ, "");
    6757           1 :   tt_str_op(lines->next->next->key, OP_EQ, "FGH");
    6758           1 :   tt_str_op(lines->next->next->value, OP_EQ, "I");
    6759           1 :   enc = kvline_encode(lines, KV_OMIT_VALS);
    6760           1 :   tt_str_op(enc, OP_EQ, "AB=CD DE FGH=I");
    6761           1 :   tor_free(enc);
    6762           1 :   config_free_lines(lines);
    6763             : 
    6764           1 :   lines = kvline_parse("AB=\"CD E\" DE FGH=\"I\"", KV_OMIT_VALS|KV_QUOTED);
    6765           1 :   tt_assert(lines);
    6766           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6767           1 :   tt_str_op(lines->value, OP_EQ, "CD E");
    6768           1 :   tt_str_op(lines->next->key, OP_EQ, "DE");
    6769           1 :   tt_str_op(lines->next->value, OP_EQ, "");
    6770           1 :   tt_str_op(lines->next->next->key, OP_EQ, "FGH");
    6771           1 :   tt_str_op(lines->next->next->value, OP_EQ, "I");
    6772           1 :   enc = kvline_encode(lines, KV_OMIT_VALS|KV_QUOTED);
    6773           1 :   tt_str_op(enc, OP_EQ, "AB=\"CD E\" DE FGH=I");
    6774           1 :   tor_free(enc);
    6775           1 :   config_free_lines(lines);
    6776             : 
    6777           1 :   lines = kvline_parse("AB=CD \"EF=GH\"", KV_OMIT_KEYS|KV_QUOTED);
    6778           1 :   tt_assert(lines);
    6779           1 :   tt_str_op(lines->key, OP_EQ, "AB");
    6780           1 :   tt_str_op(lines->value, OP_EQ, "CD");
    6781           1 :   tt_str_op(lines->next->key, OP_EQ, "");
    6782           1 :   tt_str_op(lines->next->value, OP_EQ, "EF=GH");
    6783           1 :   enc = kvline_encode(lines, KV_OMIT_KEYS);
    6784           1 :   tt_assert(!enc);
    6785           1 :   enc = kvline_encode(lines, KV_OMIT_KEYS|KV_QUOTED);
    6786           1 :   tt_assert(enc);
    6787           1 :   tt_str_op(enc, OP_EQ, "AB=CD \"EF=GH\"");
    6788           1 :   tor_free(enc);
    6789           1 :   config_free_lines(lines);
    6790             : 
    6791           1 :   lines = tor_malloc_zero(sizeof(*lines));
    6792           1 :   lines->key = tor_strdup("A=B");
    6793           1 :   lines->value = tor_strdup("CD");
    6794           1 :   enc = kvline_encode(lines, 0);
    6795           1 :   tt_assert(!enc);
    6796           1 :   config_free_lines(lines);
    6797             : 
    6798           1 :   config_line_append(&lines, "A", "B C");
    6799           1 :   enc = kvline_encode(lines, 0);
    6800           1 :   tt_assert(!enc);
    6801           1 :   enc = kvline_encode(lines, KV_RAW);
    6802           1 :   tt_assert(enc);
    6803           1 :   tt_str_op(enc, OP_EQ, "A=B C");
    6804             : 
    6805           1 :  done:
    6806           1 :   config_free_lines(lines);
    6807           1 :   tor_free(enc);
    6808           1 : }
    6809             : 
    6810             : static void
    6811           1 : test_config_getinfo_config_names(void *arg)
    6812             : {
    6813           1 :   (void)arg;
    6814           1 :   char *answer = NULL;
    6815           1 :   const char *error = NULL;
    6816           1 :   int rv;
    6817             : 
    6818           1 :   rv = getinfo_helper_config(NULL, "config/names", &answer, &error);
    6819           1 :   tt_int_op(rv, OP_EQ, 0);
    6820           1 :   tt_ptr_op(error, OP_EQ, NULL);
    6821             : 
    6822             :   // ContactInfo should be listed.
    6823           1 :   tt_assert(strstr(answer, "\nContactInfo String\n"));
    6824             : 
    6825             :   // V1AuthoritativeDirectory should not be listed, since it is obsolete.
    6826           1 :   tt_assert(! strstr(answer, "V1AuthoritativeDirectory"));
    6827             : 
    6828             :   // ___UsingTestNetworkDefaults should not be listed, since it is invisible.
    6829           1 :   tt_assert(! strstr(answer, "UsingTestNetworkDefaults"));
    6830             : 
    6831           1 :  done:
    6832           1 :   tor_free(answer);
    6833           1 : }
    6834             : 
    6835             : static void
    6836           1 : test_config_duplicate_orports(void *arg)
    6837             : {
    6838           1 :   (void)arg;
    6839             : 
    6840           1 :   config_line_t *config_port = NULL;
    6841           1 :   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           1 :   config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
    6846           1 :   config_line_append(&config_port, "ORPort", "[::1]:9050");
    6847             : 
    6848             :   // Parse IPv4, then IPv6.
    6849           1 :   port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
    6850             :                     0, CL_PORT_SERVER_OPTIONS);
    6851           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 4);
    6861             : 
    6862             :   /* This will remove the [::] and the extra [::1]. */
    6863           1 :   remove_duplicate_orports(ports);
    6864             : 
    6865           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 2);
    6866           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
    6867             :             "ORPort 9050");
    6868           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
    6869             :             "ORPort [::1]:9050");
    6870             : 
    6871             :   /* Reset. Test different ORPort value. */
    6872           3 :   SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p));
    6873           1 :   smartlist_free(ports);
    6874           1 :   config_free_lines(config_port);
    6875           1 :   config_port = NULL;
    6876           1 :   ports = smartlist_new();
    6877             : 
    6878             :   /* Implicit port and then specific IPv6 addresses but more than one. */
    6879           1 :   config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
    6880           1 :   config_line_append(&config_port, "ORPort", "[4242::1]:9051");
    6881           1 :   config_line_append(&config_port, "ORPort", "[4242::2]:9051");
    6882             : 
    6883             :   // Parse IPv4, then IPv6.
    6884           1 :   port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
    6885             :                     0, CL_PORT_SERVER_OPTIONS);
    6886           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 6);
    6898             : 
    6899             :   /* This will remove the [::] and the duplicates. */
    6900           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 4);
    6905           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
    6906             :             "ORPort 9050");
    6907           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
    6908             :             "ORPort [4242::1]:9051");
    6909           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ,
    6910             :             "ORPort [4242::2]:9051");
    6911           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ,
    6912             :             "ORPort 9050");
    6913             : 
    6914             :   /* Reset. Test different ORPort value. */
    6915           5 :   SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p));
    6916           1 :   smartlist_free(ports);
    6917           1 :   config_free_lines(config_port);
    6918           1 :   config_port = NULL;
    6919           1 :   ports = smartlist_new();
    6920             : 
    6921             :   /* Three different ports. */
    6922           1 :   config_line_append(&config_port, "ORPort", "9050"); // two implicit entries.
    6923           1 :   config_line_append(&config_port, "ORPort", "[4242::1]:9051");
    6924           1 :   config_line_append(&config_port, "ORPort", "[4242::2]:9052");
    6925             : 
    6926             :   // Parse IPv4, then IPv6.
    6927           1 :   port_parse_config(ports, config_port, "OR", CONN_TYPE_OR_LISTENER, "0.0.0.0",
    6928             :                     0, CL_PORT_SERVER_OPTIONS);
    6929           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 6);
    6941             : 
    6942             :   /* This will remove the [::] and the duplicates. */
    6943           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 4);
    6948           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 0)), OP_EQ,
    6949             :             "ORPort 9050");
    6950           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 1)), OP_EQ,
    6951             :             "ORPort [4242::1]:9051");
    6952           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 2)), OP_EQ,
    6953             :             "ORPort [4242::2]:9052");
    6954           1 :   tt_str_op(describe_relay_port(smartlist_get(ports, 3)), OP_EQ,
    6955             :             "ORPort 9050");
    6956             : 
    6957           1 :  done:
    6958           5 :   SMARTLIST_FOREACH(ports,port_cfg_t *,pf,port_cfg_free(pf));
    6959           1 :   smartlist_free(ports);
    6960           1 :   config_free_lines(config_port);
    6961           1 : }
    6962             : 
    6963             : static void
    6964           1 : test_config_multifamily_port(void *arg)
    6965             : {
    6966           1 :   (void) arg;
    6967             : 
    6968           1 :   config_line_t *config_port = NULL;
    6969           1 :   smartlist_t *ports = smartlist_new();
    6970             : 
    6971           1 :   config_line_append(&config_port, "SocksPort", "9050");
    6972           1 :   config_line_append(&config_port, "SocksPort", "[::1]:9050");
    6973             : 
    6974             :   // Parse IPv4, then IPv6.
    6975           1 :   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           1 :   tt_int_op(smartlist_len(ports), OP_EQ, 2);
    6983             : 
    6984           1 :  done:
    6985           3 :   SMARTLIST_FOREACH(ports, port_cfg_t *, cfg, port_cfg_free(cfg));
    6986           1 :   smartlist_free(ports);
    6987           1 :   config_free_lines(config_port);
    6988           1 : }
    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             : };

Generated by: LCOV version 1.14