test_nodelist.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_nodelist.c
  5. * \brief Unit tests for nodelist related functions.
  6. **/
  7. #include "core/or/or.h"
  8. #include "lib/crypt_ops/crypto_rand.h"
  9. #include "feature/nodelist/networkstatus.h"
  10. #include "feature/nodelist/nodelist.h"
  11. #include "feature/nodelist/torcert.h"
  12. #include "feature/nodelist/microdesc_st.h"
  13. #include "feature/nodelist/networkstatus_st.h"
  14. #include "feature/nodelist/node_st.h"
  15. #include "feature/nodelist/routerinfo_st.h"
  16. #include "feature/nodelist/routerstatus_st.h"
  17. #include "test/test.h"
  18. #include "test/log_test_helpers.h"
  19. /** Test the case when node_get_by_id() returns NULL,
  20. * node_get_verbose_nickname_by_id should return the base 16 encoding
  21. * of the id.
  22. */
  23. static void
  24. test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
  25. {
  26. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  27. const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  28. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
  29. (void) arg;
  30. /* make sure node_get_by_id returns NULL */
  31. tt_assert(!node_get_by_id(ID));
  32. node_get_verbose_nickname_by_id(ID, vname);
  33. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  34. done:
  35. return;
  36. }
  37. /** For routers without named flag, get_verbose_nickname should return
  38. * "Fingerprint~Nickname"
  39. */
  40. static void
  41. test_nodelist_node_get_verbose_nickname_not_named(void *arg)
  42. {
  43. node_t mock_node;
  44. routerstatus_t mock_rs;
  45. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  46. (void) arg;
  47. memset(&mock_node, 0, sizeof(node_t));
  48. memset(&mock_rs, 0, sizeof(routerstatus_t));
  49. /* verbose nickname should use ~ instead of = for unnamed routers */
  50. strlcpy(mock_rs.nickname, "TestOR", sizeof(mock_rs.nickname));
  51. mock_node.rs = &mock_rs;
  52. memcpy(mock_node.identity,
  53. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  54. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  55. DIGEST_LEN);
  56. node_get_verbose_nickname(&mock_node, vname);
  57. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
  58. done:
  59. return;
  60. }
  61. /** A node should be considered a directory server if it has an open dirport
  62. * of it accepts tunnelled directory requests.
  63. */
  64. static void
  65. test_nodelist_node_is_dir(void *arg)
  66. {
  67. (void)arg;
  68. routerstatus_t rs;
  69. routerinfo_t ri;
  70. node_t node;
  71. memset(&node, 0, sizeof(node_t));
  72. memset(&rs, 0, sizeof(routerstatus_t));
  73. memset(&ri, 0, sizeof(routerinfo_t));
  74. tt_assert(!node_is_dir(&node));
  75. node.rs = &rs;
  76. tt_assert(!node_is_dir(&node));
  77. rs.is_v2_dir = 1;
  78. tt_assert(node_is_dir(&node));
  79. rs.is_v2_dir = 0;
  80. rs.dir_port = 1;
  81. tt_assert(! node_is_dir(&node));
  82. node.rs = NULL;
  83. tt_assert(!node_is_dir(&node));
  84. node.ri = &ri;
  85. ri.supports_tunnelled_dir_requests = 1;
  86. tt_assert(node_is_dir(&node));
  87. ri.supports_tunnelled_dir_requests = 0;
  88. ri.dir_port = 1;
  89. tt_assert(! node_is_dir(&node));
  90. done:
  91. return;
  92. }
  93. static networkstatus_t *dummy_ns = NULL;
  94. static networkstatus_t *
  95. mock_networkstatus_get_latest_consensus(void)
  96. {
  97. return dummy_ns;
  98. }
  99. static networkstatus_t *
  100. mock_networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  101. {
  102. tor_assert(f == FLAV_MICRODESC);
  103. return dummy_ns;
  104. }
  105. static void
  106. test_nodelist_ed_id(void *arg)
  107. {
  108. #define N_NODES 5
  109. routerstatus_t *rs[N_NODES];
  110. microdesc_t *md[N_NODES];
  111. routerinfo_t *ri[N_NODES];
  112. networkstatus_t *ns;
  113. int i;
  114. (void)arg;
  115. ns = tor_malloc_zero(sizeof(networkstatus_t));
  116. ns->flavor = FLAV_MICRODESC;
  117. ns->routerstatus_list = smartlist_new();
  118. dummy_ns = ns;
  119. MOCK(networkstatus_get_latest_consensus,
  120. mock_networkstatus_get_latest_consensus);
  121. MOCK(networkstatus_get_latest_consensus_by_flavor,
  122. mock_networkstatus_get_latest_consensus_by_flavor);
  123. /* Make a bunch of dummy objects that we can play around with. Only set the
  124. necessary fields */
  125. for (i = 0; i < N_NODES; ++i) {
  126. rs[i] = tor_malloc_zero(sizeof(*rs[i]));
  127. md[i] = tor_malloc_zero(sizeof(*md[i]));
  128. ri[i] = tor_malloc_zero(sizeof(*ri[i]));
  129. crypto_rand(md[i]->digest, sizeof(md[i]->digest));
  130. md[i]->ed25519_identity_pkey = tor_malloc(sizeof(ed25519_public_key_t));
  131. crypto_rand((char*)md[i]->ed25519_identity_pkey,
  132. sizeof(ed25519_public_key_t));
  133. crypto_rand(rs[i]->identity_digest, sizeof(rs[i]->identity_digest));
  134. memcpy(ri[i]->cache_info.identity_digest, rs[i]->identity_digest,
  135. DIGEST_LEN);
  136. memcpy(rs[i]->descriptor_digest, md[i]->digest, DIGEST256_LEN);
  137. ri[i]->cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  138. memcpy(&ri[i]->cache_info.signing_key_cert->signing_key,
  139. md[i]->ed25519_identity_pkey, sizeof(ed25519_public_key_t));
  140. if (i < 3)
  141. smartlist_add(ns->routerstatus_list, rs[i]);
  142. }
  143. tt_int_op(0, OP_EQ, smartlist_len(nodelist_get_list()));
  144. nodelist_set_consensus(ns);
  145. tt_int_op(3, OP_EQ, smartlist_len(nodelist_get_list()));
  146. /* No Ed25519 info yet, so nothing has an ED id. */
  147. tt_ptr_op(NULL, OP_EQ, node_get_by_ed25519_id(md[0]->ed25519_identity_pkey));
  148. /* Register the first one by md, then look it up. */
  149. node_t *n = nodelist_add_microdesc(md[0]);
  150. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[0]->ed25519_identity_pkey));
  151. /* Register the second by ri, then look it up. */
  152. routerinfo_t *ri_old = NULL;
  153. n = nodelist_set_routerinfo(ri[1], &ri_old);
  154. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[1]->ed25519_identity_pkey));
  155. tt_ptr_op(ri_old, OP_EQ, NULL);
  156. /* Register it by md too. */
  157. node_t *n2 = nodelist_add_microdesc(md[1]);
  158. tt_ptr_op(n2, OP_EQ, n);
  159. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[1]->ed25519_identity_pkey));
  160. /* Register the 4th by ri only -- we never put it into the networkstatus,
  161. * so it has to be independent */
  162. node_t *n3 = nodelist_set_routerinfo(ri[3], &ri_old);
  163. tt_ptr_op(n3, OP_EQ, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  164. tt_ptr_op(ri_old, OP_EQ, NULL);
  165. tt_int_op(4, OP_EQ, smartlist_len(nodelist_get_list()));
  166. /* Register the 5th by ri only, and rewrite its ed25519 pubkey to be
  167. * the same as the 4th, to test the duplicate ed25519 key logging in
  168. * nodelist.c */
  169. memcpy(md[4]->ed25519_identity_pkey, md[3]->ed25519_identity_pkey,
  170. sizeof(ed25519_public_key_t));
  171. memcpy(&ri[4]->cache_info.signing_key_cert->signing_key,
  172. md[3]->ed25519_identity_pkey, sizeof(ed25519_public_key_t));
  173. setup_capture_of_logs(LOG_NOTICE);
  174. node_t *n4 = nodelist_set_routerinfo(ri[4], &ri_old);
  175. tt_ptr_op(ri_old, OP_EQ, NULL);
  176. tt_int_op(5, OP_EQ, smartlist_len(nodelist_get_list()));
  177. tt_ptr_op(n4, OP_NE, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  178. tt_ptr_op(n3, OP_EQ, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  179. expect_log_msg_containing("Reused ed25519_id");
  180. done:
  181. teardown_capture_of_logs();
  182. for (i = 0; i < N_NODES; ++i) {
  183. tor_free(rs[i]);
  184. tor_free(md[i]->ed25519_identity_pkey);
  185. tor_free(md[i]);
  186. tor_free(ri[i]->cache_info.signing_key_cert);
  187. tor_free(ri[i]);
  188. }
  189. smartlist_clear(ns->routerstatus_list);
  190. networkstatus_vote_free(ns);
  191. UNMOCK(networkstatus_get_latest_consensus);
  192. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  193. #undef N_NODES
  194. }
  195. #define NODE(name, flags) \
  196. { #name, test_nodelist_##name, (flags), NULL, NULL }
  197. struct testcase_t nodelist_tests[] = {
  198. NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
  199. NODE(node_get_verbose_nickname_not_named, TT_FORK),
  200. NODE(node_is_dir, TT_FORK),
  201. NODE(ed_id, TT_FORK),
  202. END_OF_TESTCASES
  203. };