test_hs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (c) 2007-2015, 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. #define CIRCUITBUILD_PRIVATE
  9. #include "or.h"
  10. #include "test.h"
  11. #include "control.h"
  12. #include "config.h"
  13. #include "routerset.h"
  14. #include "circuitbuild.h"
  15. #include "test_helpers.h"
  16. /* mock ID digest and longname for node that's in nodelist */
  17. #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  18. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  19. #define STR_HSDIR_EXIST_LONGNAME \
  20. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  21. /* mock ID digest and longname for node that's not in nodelist */
  22. #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
  23. "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
  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. send_control_event_string_replacement(uint16_t event, event_format_t which,
  37. const char *msg)
  38. {
  39. (void) event;
  40. (void) which;
  41. tor_free(received_msg);
  42. received_msg = tor_strdup(msg);
  43. }
  44. /** Mock function for node_describe_longname_by_id, it returns either
  45. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  46. */
  47. static const char *
  48. node_describe_longname_by_id_replacement(const char *id_digest)
  49. {
  50. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  51. return STR_HSDIR_EXIST_LONGNAME;
  52. } else {
  53. return STR_HSDIR_NONE_EXIST_LONGNAME;
  54. }
  55. }
  56. /** Make sure each hidden service descriptor async event generation
  57. *
  58. * function generates the message in expected format.
  59. */
  60. static void
  61. test_hs_desc_event(void *arg)
  62. {
  63. #define STR_HS_ADDR "ajhb7kljbiru65qo"
  64. #define STR_HS_ID "b3oeducbhjmbqmgw2i3jtz4fekkrinwj"
  65. rend_data_t rend_query;
  66. const char *expected_msg;
  67. (void) arg;
  68. MOCK(send_control_event_string,
  69. send_control_event_string_replacement);
  70. MOCK(node_describe_longname_by_id,
  71. node_describe_longname_by_id_replacement);
  72. /* setup rend_query struct */
  73. strncpy(rend_query.onion_address, STR_HS_ADDR,
  74. REND_SERVICE_ID_LEN_BASE32+1);
  75. rend_query.auth_type = 0;
  76. /* test request event */
  77. control_event_hs_descriptor_requested(&rend_query, HSDIR_EXIST_ID,
  78. STR_HS_ID);
  79. expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
  80. STR_HSDIR_EXIST_LONGNAME" "STR_HS_ID"\r\n";
  81. tt_assert(received_msg);
  82. tt_str_op(received_msg,OP_EQ, expected_msg);
  83. tor_free(received_msg);
  84. /* test received event */
  85. rend_query.auth_type = 1;
  86. control_event_hs_descriptor_received(&rend_query, HSDIR_EXIST_ID);
  87. expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
  88. STR_HSDIR_EXIST_LONGNAME"\r\n";
  89. tt_assert(received_msg);
  90. tt_str_op(received_msg,OP_EQ, expected_msg);
  91. tor_free(received_msg);
  92. /* test failed event */
  93. rend_query.auth_type = 2;
  94. control_event_hs_descriptor_failed(&rend_query, HSDIR_NONE_EXIST_ID,
  95. "QUERY_REJECTED");
  96. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  97. STR_HSDIR_NONE_EXIST_LONGNAME" REASON=QUERY_REJECTED\r\n";
  98. tt_assert(received_msg);
  99. tt_str_op(received_msg,OP_EQ, expected_msg);
  100. tor_free(received_msg);
  101. /* test invalid auth type */
  102. rend_query.auth_type = 999;
  103. control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID,
  104. "QUERY_REJECTED");
  105. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  106. STR_HSDIR_EXIST_LONGNAME" REASON=QUERY_REJECTED\r\n";
  107. tt_assert(received_msg);
  108. tt_str_op(received_msg,OP_EQ, expected_msg);
  109. tor_free(received_msg);
  110. done:
  111. UNMOCK(send_control_event_string);
  112. UNMOCK(node_describe_longname_by_id);
  113. tor_free(received_msg);
  114. }
  115. /* Make sure we always pick the right RP, given a well formatted
  116. * Tor2webRendezvousPoints value. */
  117. static void
  118. test_pick_tor2web_rendezvous_node(void *arg)
  119. {
  120. or_options_t *options = get_options_mutable();
  121. const node_t *chosen_rp = NULL;
  122. router_crn_flags_t flags = CRN_NEED_DESC;
  123. int retval, i;
  124. const char *tor2web_rendezvous_str = "test003r";
  125. (void) arg;
  126. /* Setup fake routerlist. */
  127. helper_setup_fake_routerlist();
  128. /* Parse Tor2webRendezvousPoints as a routerset. */
  129. options->Tor2webRendezvousPoints = routerset_new();
  130. retval = routerset_parse(options->Tor2webRendezvousPoints,
  131. tor2web_rendezvous_str,
  132. "test_tor2web_rp");
  133. tt_int_op(retval, >=, 0);
  134. /* Pick rendezvous point. Make sure the correct one is
  135. picked. Repeat many times to make sure it works properly. */
  136. for (i = 0; i < 50 ; i++) {
  137. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  138. tt_assert(chosen_rp);
  139. tt_str_op(chosen_rp->ri->nickname, ==, tor2web_rendezvous_str);
  140. }
  141. done:
  142. routerset_free(options->Tor2webRendezvousPoints);
  143. }
  144. /* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
  145. * correspond to an actual node. */
  146. static void
  147. test_pick_bad_tor2web_rendezvous_node(void *arg)
  148. {
  149. or_options_t *options = get_options_mutable();
  150. const node_t *chosen_rp = NULL;
  151. router_crn_flags_t flags = CRN_NEED_DESC;
  152. int retval, i;
  153. const char *tor2web_rendezvous_str = "dummy";
  154. (void) arg;
  155. /* Setup fake routerlist. */
  156. helper_setup_fake_routerlist();
  157. /* Parse Tor2webRendezvousPoints as a routerset. */
  158. options->Tor2webRendezvousPoints = routerset_new();
  159. retval = routerset_parse(options->Tor2webRendezvousPoints,
  160. tor2web_rendezvous_str,
  161. "test_tor2web_rp");
  162. tt_int_op(retval, >=, 0);
  163. /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
  164. dummy value, we shouldn't find any eligible RPs. */
  165. for (i = 0; i < 50 ; i++) {
  166. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  167. tt_assert(!chosen_rp);
  168. }
  169. done:
  170. routerset_free(options->Tor2webRendezvousPoints);
  171. }
  172. struct testcase_t hs_tests[] = {
  173. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  174. NULL, NULL },
  175. { "pick_tor2web_rendezvous_node", test_pick_tor2web_rendezvous_node, TT_FORK,
  176. NULL, NULL },
  177. { "pick_bad_tor2web_rendezvous_node",
  178. test_pick_bad_tor2web_rendezvous_node, TT_FORK,
  179. NULL, NULL },
  180. END_OF_TESTCASES
  181. };