test_hs_control.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_control.c
  5. * \brief Unit tests for hidden service control port event and command.
  6. **/
  7. #define CONTROL_PRIVATE
  8. #include "or.h"
  9. #include "test.h"
  10. #include "control.h"
  11. #include "config.h"
  12. #include "hs_common.h"
  13. #include "hs_control.h"
  14. #include "nodelist.h"
  15. #include "node_st.h"
  16. #include "routerstatus_st.h"
  17. #include "test_helpers.h"
  18. /* mock ID digest and longname for node that's in nodelist */
  19. #define HSDIR_EXIST_ID \
  20. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  21. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  22. #define STR_HSDIR_EXIST_LONGNAME \
  23. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  24. #define STR_HSDIR_NONE_EXIST_LONGNAME \
  25. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  26. /* Helper global variable for hidden service descriptor event test.
  27. * It's used as a pointer to dynamically created message buffer in
  28. * send_control_event_string_replacement function, which mocks
  29. * send_control_event_string function.
  30. *
  31. * Always free it after use! */
  32. static char *received_msg = NULL;
  33. /** Mock function for send_control_event_string
  34. */
  35. static void
  36. queue_control_event_string_replacement(uint16_t event, char *msg)
  37. {
  38. (void) event;
  39. tor_free(received_msg);
  40. received_msg = msg;
  41. }
  42. /** Mock function for node_describe_longname_by_id, it returns either
  43. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  44. */
  45. static const char *
  46. node_describe_longname_by_id_replacement(const char *id_digest)
  47. {
  48. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  49. return STR_HSDIR_EXIST_LONGNAME;
  50. } else {
  51. return STR_HSDIR_NONE_EXIST_LONGNAME;
  52. }
  53. }
  54. /* HSDir fetch index is a series of 'D' */
  55. #define HSDIR_INDEX_FETCH_HEX \
  56. "4343434343434343434343434343434343434343434343434343434343434343"
  57. #define HSDIR_INDEX_STORE_HEX \
  58. "4444444444444444444444444444444444444444444444444444444444444444"
  59. static const node_t *
  60. mock_node_get_by_id(const char *digest)
  61. {
  62. static node_t node;
  63. memcpy(node.identity, digest, DIGEST_LEN);
  64. memset(node.hsdir_index.fetch, 'C', DIGEST256_LEN);
  65. memset(node.hsdir_index.store_first, 'D', DIGEST256_LEN);
  66. return &node;
  67. }
  68. static void
  69. test_hs_desc_event(void *arg)
  70. {
  71. int ret;
  72. char *expected_msg = NULL;
  73. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  74. ed25519_keypair_t identity_kp;
  75. ed25519_public_key_t blinded_pk;
  76. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  77. routerstatus_t hsdir_rs;
  78. hs_ident_dir_conn_t ident;
  79. (void) arg;
  80. MOCK(queue_control_event_string,
  81. queue_control_event_string_replacement);
  82. MOCK(node_describe_longname_by_id,
  83. node_describe_longname_by_id_replacement);
  84. MOCK(node_get_by_id, mock_node_get_by_id);
  85. /* Setup what we need for this test. */
  86. ed25519_keypair_generate(&identity_kp, 0);
  87. hs_build_address(&identity_kp.pubkey, HS_VERSION_THREE, onion_address);
  88. ret = hs_address_is_valid(onion_address);
  89. tt_int_op(ret, OP_EQ, 1);
  90. memset(&blinded_pk, 'B', sizeof(blinded_pk));
  91. memset(&hsdir_rs, 0, sizeof(hsdir_rs));
  92. memcpy(hsdir_rs.identity_digest, HSDIR_EXIST_ID, DIGEST_LEN);
  93. ret = ed25519_public_to_base64(base64_blinded_pk, &blinded_pk);
  94. tt_int_op(ret, OP_EQ, 0);
  95. memcpy(&ident.identity_pk, &identity_kp.pubkey,
  96. sizeof(ed25519_public_key_t));
  97. memcpy(&ident.blinded_pk, &blinded_pk, sizeof(blinded_pk));
  98. /* HS_DESC REQUESTED ... */
  99. hs_control_desc_event_requested(&identity_kp.pubkey, base64_blinded_pk,
  100. &hsdir_rs);
  101. tor_asprintf(&expected_msg, "650 HS_DESC REQUESTED %s NO_AUTH "
  102. STR_HSDIR_EXIST_LONGNAME " %s HSDIR_INDEX="
  103. HSDIR_INDEX_FETCH_HEX "\r\n",
  104. onion_address, base64_blinded_pk);
  105. tt_assert(received_msg);
  106. tt_str_op(received_msg, OP_EQ, expected_msg);
  107. tor_free(received_msg);
  108. tor_free(expected_msg);
  109. /* HS_DESC CREATED... */
  110. hs_control_desc_event_created(onion_address, &blinded_pk);
  111. tor_asprintf(&expected_msg, "650 HS_DESC CREATED %s UNKNOWN "
  112. "UNKNOWN %s\r\n",
  113. onion_address, base64_blinded_pk);
  114. tt_assert(received_msg);
  115. tt_str_op(received_msg, OP_EQ, expected_msg);
  116. tor_free(received_msg);
  117. tor_free(expected_msg);
  118. /* HS_DESC UPLOAD... */
  119. uint8_t hsdir_index_store[DIGEST256_LEN];
  120. memset(hsdir_index_store, 'D', sizeof(hsdir_index_store));
  121. hs_control_desc_event_upload(onion_address, HSDIR_EXIST_ID,
  122. &blinded_pk, hsdir_index_store);
  123. tor_asprintf(&expected_msg, "650 HS_DESC UPLOAD %s UNKNOWN "
  124. STR_HSDIR_EXIST_LONGNAME " %s "
  125. "HSDIR_INDEX=" HSDIR_INDEX_STORE_HEX "\r\n",
  126. onion_address, base64_blinded_pk);
  127. tt_assert(received_msg);
  128. tt_str_op(received_msg, OP_EQ, expected_msg);
  129. tor_free(received_msg);
  130. tor_free(expected_msg);
  131. /* HS_DESC FAILED... */
  132. hs_control_desc_event_failed(&ident, HSDIR_EXIST_ID, "BAD_DESC");
  133. tor_asprintf(&expected_msg, "650 HS_DESC FAILED %s NO_AUTH "
  134. STR_HSDIR_EXIST_LONGNAME " %s "
  135. "REASON=BAD_DESC\r\n",
  136. onion_address, base64_blinded_pk);
  137. tt_assert(received_msg);
  138. tt_str_op(received_msg, OP_EQ, expected_msg);
  139. tor_free(received_msg);
  140. tor_free(expected_msg);
  141. /* HS_DESC RECEIVED... */
  142. hs_control_desc_event_received(&ident, HSDIR_EXIST_ID);
  143. tor_asprintf(&expected_msg, "650 HS_DESC RECEIVED %s NO_AUTH "
  144. STR_HSDIR_EXIST_LONGNAME " %s\r\n",
  145. onion_address, base64_blinded_pk);
  146. tt_assert(received_msg);
  147. tt_str_op(received_msg, OP_EQ, expected_msg);
  148. tor_free(received_msg);
  149. tor_free(expected_msg);
  150. /* HS_DESC UPLOADED... */
  151. hs_control_desc_event_uploaded(&ident, HSDIR_EXIST_ID);
  152. tor_asprintf(&expected_msg, "650 HS_DESC UPLOADED %s UNKNOWN "
  153. STR_HSDIR_EXIST_LONGNAME "\r\n",
  154. onion_address);
  155. tt_assert(received_msg);
  156. tt_str_op(received_msg, OP_EQ, expected_msg);
  157. tor_free(received_msg);
  158. tor_free(expected_msg);
  159. done:
  160. UNMOCK(queue_control_event_string);
  161. UNMOCK(node_describe_longname_by_id);
  162. UNMOCK(node_get_by_id);
  163. tor_free(received_msg);
  164. tor_free(expected_msg);
  165. }
  166. struct testcase_t hs_control_tests[] = {
  167. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  168. NULL, NULL },
  169. END_OF_TESTCASES
  170. };