test_nodelist.c 6.4 KB

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