test_hs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (c) 2007-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs.c
  5. * \brief Unit tests for hidden service.
  6. **/
  7. #define CONTROL_PRIVATE
  8. #include "or.h"
  9. #include "test.h"
  10. #include "control.h"
  11. /* mock ID digest and longname for node that's in nodelist */
  12. #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  13. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14. #define STR_HSDIR_EXIST_LONGNAME \
  15. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  16. /* mock ID digest and longname for node that's not in nodelist */
  17. #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
  18. "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
  19. #define STR_HSDIR_NONE_EXIST_LONGNAME \
  20. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  21. /* Helper global variable for hidden service descriptor event test.
  22. * It's used as a pointer to dynamically created message buffer in
  23. * send_control_event_string_replacement function, which mocks
  24. * send_control_event_string function.
  25. *
  26. * Always free it after use! */
  27. static char *received_msg = NULL;
  28. /** Mock function for send_control_event_string
  29. */
  30. static void
  31. send_control_event_string_replacement(uint16_t event, event_format_t which,
  32. const char *msg)
  33. {
  34. (void) event;
  35. (void) which;
  36. tor_free(received_msg);
  37. received_msg = tor_strdup(msg);
  38. }
  39. /** Mock function for node_describe_longname_by_id, it returns either
  40. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  41. */
  42. static const char *
  43. node_describe_longname_by_id_replacement(const char *id_digest)
  44. {
  45. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  46. return STR_HSDIR_EXIST_LONGNAME;
  47. } else {
  48. return STR_HSDIR_NONE_EXIST_LONGNAME;
  49. }
  50. }
  51. /** Make sure each hidden service descriptor async event generation
  52. *
  53. * function generates the message in expected format.
  54. */
  55. static void
  56. test_hs_desc_event(void *arg)
  57. {
  58. #define STR_HS_ADDR "ajhb7kljbiru65qo"
  59. #define STR_HS_ID "b3oeducbhjmbqmgw2i3jtz4fekkrinwj"
  60. rend_data_t rend_query;
  61. const char *expected_msg;
  62. (void) arg;
  63. MOCK(send_control_event_string,
  64. send_control_event_string_replacement);
  65. MOCK(node_describe_longname_by_id,
  66. node_describe_longname_by_id_replacement);
  67. /* setup rend_query struct */
  68. strncpy(rend_query.onion_address, STR_HS_ADDR,
  69. REND_SERVICE_ID_LEN_BASE32+1);
  70. rend_query.auth_type = 0;
  71. /* test request event */
  72. control_event_hs_descriptor_requested(&rend_query, HSDIR_EXIST_ID,
  73. STR_HS_ID);
  74. expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
  75. STR_HSDIR_EXIST_LONGNAME" "STR_HS_ID"\r\n";
  76. tt_assert(received_msg);
  77. tt_str_op(received_msg,==, expected_msg);
  78. tor_free(received_msg);
  79. /* test received event */
  80. rend_query.auth_type = 1;
  81. control_event_hs_descriptor_received(&rend_query, HSDIR_EXIST_ID);
  82. expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
  83. STR_HSDIR_EXIST_LONGNAME"\r\n";
  84. tt_assert(received_msg);
  85. tt_str_op(received_msg,==, expected_msg);
  86. tor_free(received_msg);
  87. /* test failed event */
  88. rend_query.auth_type = 2;
  89. control_event_hs_descriptor_failed(&rend_query, HSDIR_NONE_EXIST_ID);
  90. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  91. STR_HSDIR_NONE_EXIST_LONGNAME"\r\n";
  92. tt_assert(received_msg);
  93. tt_str_op(received_msg,==, expected_msg);
  94. tor_free(received_msg);
  95. /* test invalid auth type */
  96. rend_query.auth_type = 999;
  97. control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID);
  98. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  99. STR_HSDIR_EXIST_LONGNAME"\r\n";
  100. tt_assert(received_msg);
  101. tt_str_op(received_msg,==, expected_msg);
  102. tor_free(received_msg);
  103. done:
  104. UNMOCK(send_control_event_string);
  105. UNMOCK(node_describe_longname_by_id);
  106. tor_free(received_msg);
  107. }
  108. struct testcase_t hs_tests[] = {
  109. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  110. NULL, NULL },
  111. END_OF_TESTCASES
  112. };