test_hs_control.c 6.5 KB

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