test_hs_control.c 6.5 KB

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