test_nodelist.c 6.2 KB

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