test_address_set.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "core/or/or.h"
  4. #include "lib/crypt_ops/crypto_rand.h"
  5. #include "core/or/address_set.h"
  6. #include "feature/nodelist/microdesc.h"
  7. #include "feature/nodelist/networkstatus.h"
  8. #include "feature/nodelist/nodelist.h"
  9. #include "feature/nodelist/routerlist.h"
  10. #include "feature/nodelist/torcert.h"
  11. #include "feature/nodelist/microdesc_st.h"
  12. #include "feature/nodelist/networkstatus_st.h"
  13. #include "feature/nodelist/routerinfo_st.h"
  14. #include "feature/nodelist/routerstatus_st.h"
  15. #include "test/test.h"
  16. static networkstatus_t *dummy_ns = NULL;
  17. static networkstatus_t *
  18. mock_networkstatus_get_latest_consensus(void)
  19. {
  20. return dummy_ns;
  21. }
  22. static networkstatus_t *
  23. mock_networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  24. {
  25. tor_assert(f == FLAV_MICRODESC);
  26. return dummy_ns;
  27. }
  28. /* Number of address a single node_t can have. Default to the production
  29. * value. This is to control the size of the bloom filter. */
  30. static int addr_per_node = 2;
  31. static int
  32. mock_get_estimated_address_per_node(void)
  33. {
  34. return addr_per_node;
  35. }
  36. static void
  37. test_contains(void *arg)
  38. {
  39. int ret;
  40. address_set_t *set = NULL;
  41. (void) arg;
  42. /* Setup an IPv4 and IPv6 addresses. */
  43. tor_addr_t addr_v6;
  44. tor_addr_parse(&addr_v6, "1:2:3:4::");
  45. tor_addr_t addr_v4;
  46. tor_addr_parse(&addr_v4, "42.42.42.42");
  47. uint32_t ipv4h = tor_addr_to_ipv4h(&addr_v4);
  48. /* Make it very big so the chance of failing the contain test will be
  49. * extremely rare. */
  50. set = address_set_new(1024);
  51. tt_assert(set);
  52. /* Add and lookup IPv6. */
  53. address_set_add(set, &addr_v6);
  54. ret = address_set_probably_contains(set, &addr_v6);
  55. tt_int_op(ret, OP_EQ, 1);
  56. /* Add and lookup IPv4. */
  57. address_set_add_ipv4h(set, ipv4h);
  58. ret = address_set_probably_contains(set, &addr_v4);
  59. tt_int_op(ret, OP_EQ, 1);
  60. /* Try a lookup of rubbish. */
  61. tor_addr_t dummy_addr;
  62. memset(&dummy_addr, 'A', sizeof(dummy_addr));
  63. dummy_addr.family = AF_INET;
  64. ret = address_set_probably_contains(set, &dummy_addr);
  65. tt_int_op(ret, OP_EQ, 0);
  66. dummy_addr.family = AF_INET6;
  67. ret = address_set_probably_contains(set, &dummy_addr);
  68. tt_int_op(ret, OP_EQ, 0);
  69. done:
  70. address_set_free(set);
  71. }
  72. static void
  73. test_nodelist(void *arg)
  74. {
  75. int ret;
  76. routerstatus_t *rs = NULL; microdesc_t *md = NULL; routerinfo_t *ri = NULL;
  77. (void) arg;
  78. MOCK(networkstatus_get_latest_consensus,
  79. mock_networkstatus_get_latest_consensus);
  80. MOCK(networkstatus_get_latest_consensus_by_flavor,
  81. mock_networkstatus_get_latest_consensus_by_flavor);
  82. MOCK(get_estimated_address_per_node,
  83. mock_get_estimated_address_per_node);
  84. dummy_ns = tor_malloc_zero(sizeof(*dummy_ns));
  85. dummy_ns->flavor = FLAV_MICRODESC;
  86. dummy_ns->routerstatus_list = smartlist_new();
  87. tor_addr_t addr_v4, addr_v6, dummy_addr;
  88. tor_addr_parse(&addr_v4, "42.42.42.42");
  89. uint32_t ipv4h = tor_addr_to_ipv4h(&addr_v4);
  90. tor_addr_parse(&addr_v6, "1:2:3:4::");
  91. memset(&dummy_addr, 'A', sizeof(dummy_addr));
  92. /* This will make the nodelist bloom filter very large
  93. * (the_nodelist->node_addrs) so we will fail the contain test rarely. */
  94. addr_per_node = 1024;
  95. /* No node no nothing. The lookups should be empty. */
  96. nodelist_set_consensus(dummy_ns);
  97. /* The address set should be empty. */
  98. ret = nodelist_probably_contains_address(&addr_v4);
  99. tt_int_op(ret, OP_EQ, 0);
  100. ret = nodelist_probably_contains_address(&addr_v6);
  101. tt_int_op(ret, OP_EQ, 0);
  102. dummy_addr.family = AF_INET;
  103. ret = nodelist_probably_contains_address(&dummy_addr);
  104. tt_int_op(ret, OP_EQ, 0);
  105. dummy_addr.family = AF_INET6;
  106. ret = nodelist_probably_contains_address(&dummy_addr);
  107. tt_int_op(ret, OP_EQ, 0);
  108. md = tor_malloc_zero(sizeof(*md));
  109. ri = tor_malloc_zero(sizeof(*ri));
  110. rs = tor_malloc_zero(sizeof(*rs));
  111. crypto_rand(rs->identity_digest, sizeof(rs->identity_digest));
  112. crypto_rand(md->digest, sizeof(md->digest));
  113. memcpy(rs->descriptor_digest, md->digest, DIGEST256_LEN);
  114. /* Setup the rs, ri and md addresses. */
  115. rs->addr = ipv4h;
  116. tor_addr_parse(&rs->ipv6_addr, "1:2:3:4::");
  117. ri->addr = ipv4h;
  118. tor_addr_parse(&ri->ipv6_addr, "1:2:3:4::");
  119. tor_addr_parse(&md->ipv6_addr, "1:2:3:4::");
  120. /* Add the rs to the consensus becoming a node_t. */
  121. smartlist_add(dummy_ns->routerstatus_list, rs);
  122. nodelist_set_consensus(dummy_ns);
  123. /* At this point, the address set should be initialized in the nodelist and
  124. * we should be able to lookup. */
  125. ret = nodelist_probably_contains_address(&addr_v4);
  126. tt_int_op(ret, OP_EQ, 1);
  127. ret = nodelist_probably_contains_address(&addr_v6);
  128. tt_int_op(ret, OP_EQ, 1);
  129. /* Lookup unknown address. */
  130. dummy_addr.family = AF_INET;
  131. ret = nodelist_probably_contains_address(&dummy_addr);
  132. tt_int_op(ret, OP_EQ, 0);
  133. dummy_addr.family = AF_INET6;
  134. ret = nodelist_probably_contains_address(&dummy_addr);
  135. tt_int_op(ret, OP_EQ, 0);
  136. done:
  137. routerstatus_free(rs); routerinfo_free(ri); microdesc_free(md);
  138. smartlist_clear(dummy_ns->routerstatus_list);
  139. networkstatus_vote_free(dummy_ns);
  140. UNMOCK(networkstatus_get_latest_consensus);
  141. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  142. UNMOCK(get_estimated_address_per_node);
  143. }
  144. struct testcase_t address_set_tests[] = {
  145. { "contains", test_contains, TT_FORK,
  146. NULL, NULL },
  147. { "nodelist", test_nodelist, TT_FORK,
  148. NULL, NULL },
  149. END_OF_TESTCASES
  150. };