test_hs_control.c 6.2 KB

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