tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test_voting_flags.c (4782B)


      1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 #include "orconfig.h"
      5 
      6 #define VOTEFLAGS_PRIVATE
      7 
      8 #include "core/or/or.h"
      9 
     10 #include "feature/dirauth/voteflags.h"
     11 #include "feature/dirauth/dirauth_options_st.h"
     12 #include "feature/nodelist/node_st.h"
     13 #include "feature/nodelist/routerstatus_st.h"
     14 #include "feature/nodelist/routerinfo_st.h"
     15 
     16 #include "app/config/config.h"
     17 
     18 #include "test/test.h"
     19 #include "test/opts_test_helpers.h"
     20 
     21 typedef struct {
     22  time_t now;
     23  routerinfo_t ri;
     24  node_t node;
     25 
     26  routerstatus_t expected;
     27 } flag_vote_test_cfg_t;
     28 
     29 static void
     30 setup_cfg(flag_vote_test_cfg_t *c)
     31 {
     32  memset(c, 0, sizeof(*c));
     33 
     34  c->now = approx_time();
     35 
     36  c->ri.nickname = (char *) "testing100";
     37  strlcpy(c->expected.nickname, "testing100", sizeof(c->expected.nickname));
     38 
     39  memset(c->ri.cache_info.identity_digest, 0xff, DIGEST_LEN);
     40  memset(c->ri.cache_info.signed_descriptor_digest, 0xee, DIGEST_LEN);
     41 
     42  c->ri.cache_info.published_on = c->now - 100;
     43 
     44  tor_addr_from_ipv4h(&c->ri.ipv4_addr, 0x7f010105);
     45  tor_addr_from_ipv4h(&c->expected.ipv4_addr, 0x7f010105);
     46  c->ri.ipv4_orport = 9090;
     47  c->expected.ipv4_orport = 9090;
     48 
     49  tor_addr_make_null(&c->ri.ipv6_addr, AF_INET6);
     50  tor_addr_make_null(&c->expected.ipv6_addr, AF_INET6);
     51 
     52  // By default we have no loaded information about stability or speed,
     53  // so we'll default to voting "yeah sure." on these two.
     54  c->expected.is_fast = 1;
     55  c->expected.is_stable = 1;
     56 }
     57 
     58 static bool
     59 check_result(flag_vote_test_cfg_t *c)
     60 {
     61  bool result = false;
     62  routerstatus_t rs;
     63  memset(&rs, 0, sizeof(rs));
     64  dirauth_set_routerstatus_from_routerinfo(&rs, &c->node, &c->ri, c->now,
     65                                           0, 0);
     66 
     67  tt_str_op(rs.nickname, OP_EQ, c->expected.nickname);
     68 
     69  // identity_digest and descriptor_digest are not set here.
     70 
     71  tt_assert(tor_addr_eq(&rs.ipv4_addr, &c->expected.ipv4_addr));
     72  tt_uint_op(rs.ipv4_orport, OP_EQ, c->expected.ipv4_orport);
     73  tt_uint_op(rs.ipv4_dirport, OP_EQ, c->expected.ipv4_dirport);
     74 
     75  tt_assert(tor_addr_eq(&rs.ipv6_addr, &c->expected.ipv6_addr));
     76  tt_uint_op(rs.ipv6_orport, OP_EQ, c->expected.ipv6_orport);
     77 
     78 #define FLAG(flagname) \
     79  tt_uint_op(rs.flagname, OP_EQ, c->expected.flagname)
     80 
     81  FLAG(is_authority);
     82  FLAG(is_exit);
     83  FLAG(is_stable);
     84  FLAG(is_fast);
     85  FLAG(is_flagged_running);
     86  FLAG(is_named);
     87  FLAG(is_unnamed);
     88  FLAG(is_valid);
     89  FLAG(is_possible_guard);
     90  FLAG(is_bad_exit);
     91  FLAG(is_hs_dir);
     92  FLAG(is_v2_dir);
     93  FLAG(is_staledesc);
     94  FLAG(has_bandwidth);
     95  FLAG(has_exitsummary);
     96  FLAG(bw_is_unmeasured);
     97 
     98  result = true;
     99 
    100 done:
    101  return result;
    102 }
    103 
    104 static void
    105 test_voting_flags_minimal(void *arg)
    106 {
    107  flag_vote_test_cfg_t *cfg = arg;
    108  (void) check_result(cfg);
    109 }
    110 
    111 static void
    112 test_voting_flags_ipv6(void *arg)
    113 {
    114  flag_vote_test_cfg_t *cfg = arg;
    115 
    116  tt_assert(tor_addr_parse(&cfg->ri.ipv6_addr, "f00::b42") == AF_INET6);
    117  cfg->ri.ipv6_orport = 9091;
    118  // no change in expected results, since we aren't set up with ipv6
    119  // connectivity.
    120  if (!check_result(cfg))
    121    goto done;
    122 
    123  get_dirauth_options(get_options_mutable())->AuthDirHasIPv6Connectivity = 1;
    124  // no change in expected results, since last_reachable6 won't be set.
    125  if (!check_result(cfg))
    126    goto done;
    127 
    128  cfg->node.last_reachable6 = cfg->now - 10;
    129  // now that lastreachable6 is set, we expect to see the result.
    130  tt_assert(tor_addr_parse(&cfg->expected.ipv6_addr, "f00::b42") == AF_INET6);
    131  cfg->expected.ipv6_orport = 9091;
    132  if (!check_result(cfg))
    133    goto done;
    134 done:
    135  ;
    136 }
    137 
    138 static void
    139 test_voting_flags_staledesc(void *arg)
    140 {
    141  flag_vote_test_cfg_t *cfg = arg;
    142  time_t now = cfg->now;
    143 
    144  cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL + 10;
    145  // no change in expectations for is_staledesc
    146  if (!check_result(cfg))
    147    goto done;
    148 
    149  cfg->ri.cache_info.published_on = now - DESC_IS_STALE_INTERVAL - 10;
    150  cfg->expected.is_staledesc = 1;
    151  if (!check_result(cfg))
    152    goto done;
    153 
    154 done:
    155  ;
    156 }
    157 
    158 static void *
    159 setup_voting_flags_test(const struct testcase_t *testcase)
    160 {
    161  (void)testcase;
    162  flag_vote_test_cfg_t *cfg = tor_malloc_zero(sizeof(*cfg));
    163  setup_cfg(cfg);
    164  return cfg;
    165 }
    166 
    167 static int
    168 teardown_voting_flags_test(const struct testcase_t *testcase, void *arg)
    169 {
    170  (void)testcase;
    171  flag_vote_test_cfg_t *cfg = arg;
    172  tor_free(cfg);
    173  return 1;
    174 }
    175 
    176 static const struct testcase_setup_t voting_flags_setup = {
    177  .setup_fn = setup_voting_flags_test,
    178  .cleanup_fn = teardown_voting_flags_test,
    179 };
    180 
    181 #define T(name,flags)                                   \
    182  { #name, test_voting_flags_##name, (flags), &voting_flags_setup, NULL }
    183 
    184 struct testcase_t voting_flags_tests[] = {
    185  T(minimal, 0),
    186  T(ipv6, TT_FORK),
    187  // TODO: Add more of these tests.
    188  T(staledesc, TT_FORK),
    189  END_OF_TESTCASES
    190 };