test_nodelist.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "nodelist.h"
  9. #include "test.h"
  10. /** Test the case when node_get_by_id() returns NULL,
  11. * node_get_verbose_nickname_by_id should return the base 16 encoding
  12. * of the id.
  13. */
  14. static void
  15. test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
  16. {
  17. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  18. const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  19. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
  20. (void) arg;
  21. /* make sure node_get_by_id returns NULL */
  22. tt_assert(!node_get_by_id(ID));
  23. node_get_verbose_nickname_by_id(ID, vname);
  24. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  25. done:
  26. return;
  27. }
  28. /** For routers without named flag, get_verbose_nickname should return
  29. * "Fingerprint~Nickname"
  30. */
  31. static void
  32. test_nodelist_node_get_verbose_nickname_not_named(void *arg)
  33. {
  34. node_t mock_node;
  35. routerstatus_t mock_rs;
  36. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  37. (void) arg;
  38. memset(&mock_node, 0, sizeof(node_t));
  39. memset(&mock_rs, 0, sizeof(routerstatus_t));
  40. /* verbose nickname should use ~ instead of = for unnamed routers */
  41. strlcpy(mock_rs.nickname, "TestOR", sizeof(mock_rs.nickname));
  42. mock_node.rs = &mock_rs;
  43. memcpy(mock_node.identity,
  44. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  45. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  46. DIGEST_LEN);
  47. node_get_verbose_nickname(&mock_node, vname);
  48. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
  49. done:
  50. return;
  51. }
  52. /** A node should be considered a directory server if it has an open dirport
  53. * of it accepts tunnelled directory requests.
  54. */
  55. static void
  56. test_nodelist_node_is_dir(void *arg)
  57. {
  58. (void)arg;
  59. routerstatus_t rs;
  60. routerinfo_t ri;
  61. node_t node;
  62. memset(&node, 0, sizeof(node_t));
  63. memset(&rs, 0, sizeof(routerstatus_t));
  64. memset(&ri, 0, sizeof(routerinfo_t));
  65. tt_assert(!node_is_dir(&node));
  66. node.rs = &rs;
  67. tt_assert(!node_is_dir(&node));
  68. rs.is_v2_dir = 1;
  69. tt_assert(node_is_dir(&node));
  70. rs.is_v2_dir = 0;
  71. rs.dir_port = 1;
  72. tt_assert(! node_is_dir(&node));
  73. node.rs = NULL;
  74. tt_assert(!node_is_dir(&node));
  75. node.ri = &ri;
  76. ri.supports_tunnelled_dir_requests = 1;
  77. tt_assert(node_is_dir(&node));
  78. ri.supports_tunnelled_dir_requests = 0;
  79. ri.dir_port = 1;
  80. tt_assert(! node_is_dir(&node));
  81. done:
  82. return;
  83. }
  84. #define NODE(name, flags) \
  85. { #name, test_nodelist_##name, (flags), NULL, NULL }
  86. struct testcase_t nodelist_tests[] = {
  87. NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
  88. NODE(node_get_verbose_nickname_not_named, TT_FORK),
  89. NODE(node_is_dir, TT_FORK),
  90. END_OF_TESTCASES
  91. };