test_nodelist.c 6.3 KB

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