test_nodelist.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright (c) 2007-2013, 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. /** Tese 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. test_assert(!node_get_by_id(ID));
  23. node_get_verbose_nickname_by_id(ID, vname);
  24. test_streq(vname, "$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. test_streq(vname, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
  49. done:
  50. return;
  51. }
  52. #define NODE(name, flags) \
  53. { #name, test_nodelist_##name, (flags), NULL, NULL }
  54. struct testcase_t nodelist_tests[] = {
  55. NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
  56. NODE(node_get_verbose_nickname_not_named, TT_FORK),
  57. END_OF_TESTCASES
  58. };