test_nodelist.c 6.3 KB

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