test_voting_flags.c 4.7 KB

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