test_nodelist.c 7.1 KB

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