test_hs.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright (c) 2007-2013, 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 "testhelper.h"
  13. #include "config.h"
  14. #include "routerset.h"
  15. #include "circuitbuild.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. test_assert(received_msg);
  82. test_streq(received_msg, 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. test_assert(received_msg);
  90. test_streq(received_msg, 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. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  96. STR_HSDIR_NONE_EXIST_LONGNAME"\r\n";
  97. test_assert(received_msg);
  98. test_streq(received_msg, expected_msg);
  99. tor_free(received_msg);
  100. /* test invalid auth type */
  101. rend_query.auth_type = 999;
  102. control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID);
  103. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  104. STR_HSDIR_EXIST_LONGNAME"\r\n";
  105. test_assert(received_msg);
  106. test_streq(received_msg, expected_msg);
  107. tor_free(received_msg);
  108. done:
  109. UNMOCK(send_control_event_string);
  110. UNMOCK(node_describe_longname_by_id);
  111. tor_free(received_msg);
  112. }
  113. /* Make sure we always pick the right RP, given a well formatted
  114. * Tor2webRendezvousPoints value. */
  115. static void
  116. test_pick_tor2web_rendezvous_node(void *arg)
  117. {
  118. or_options_t *options = get_options_mutable();
  119. const node_t *chosen_rp = NULL;
  120. router_crn_flags_t flags = CRN_NEED_DESC;
  121. int retval, i;
  122. const char *tor2web_rendezvous_str = "test003r";
  123. (void) arg;
  124. /* Setup fake routerlist. */
  125. helper_setup_fake_routerlist();
  126. /* Parse Tor2webRendezvousPoints as a routerset. */
  127. options->Tor2webRendezvousPoints = routerset_new();
  128. retval = routerset_parse(options->Tor2webRendezvousPoints,
  129. tor2web_rendezvous_str,
  130. "test_tor2web_rp");
  131. tt_int_op(retval, >=, 0);
  132. /* Pick rendezvous point. Make sure the correct one is
  133. picked. Repeat many times to make sure it works properly. */
  134. for (i = 0; i < 50 ; i++) {
  135. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  136. tt_assert(chosen_rp);
  137. tt_str_op(chosen_rp->ri->nickname, ==, tor2web_rendezvous_str);
  138. }
  139. done:
  140. routerset_free(options->Tor2webRendezvousPoints);
  141. }
  142. /* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
  143. * correspond to an actual node. */
  144. static void
  145. test_pick_bad_tor2web_rendezvous_node(void *arg)
  146. {
  147. or_options_t *options = get_options_mutable();
  148. const node_t *chosen_rp = NULL;
  149. router_crn_flags_t flags = CRN_NEED_DESC;
  150. int retval, i;
  151. const char *tor2web_rendezvous_str = "dummy";
  152. (void) arg;
  153. /* Setup fake routerlist. */
  154. helper_setup_fake_routerlist();
  155. /* Parse Tor2webRendezvousPoints as a routerset. */
  156. options->Tor2webRendezvousPoints = routerset_new();
  157. retval = routerset_parse(options->Tor2webRendezvousPoints,
  158. tor2web_rendezvous_str,
  159. "test_tor2web_rp");
  160. tt_int_op(retval, >=, 0);
  161. /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
  162. dummy value, we shouldn't find any eligible RPs. */
  163. for (i = 0; i < 50 ; i++) {
  164. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  165. tt_assert(!chosen_rp);
  166. }
  167. done:
  168. routerset_free(options->Tor2webRendezvousPoints);
  169. }
  170. struct testcase_t hs_tests[] = {
  171. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  172. NULL, NULL },
  173. { "pick_tor2web_rendezvous_node", test_pick_tor2web_rendezvous_node, TT_FORK,
  174. NULL, NULL },
  175. { "pick_bad_tor2web_rendezvous_node",
  176. test_pick_bad_tor2web_rendezvous_node, TT_FORK,
  177. NULL, NULL },
  178. END_OF_TESTCASES
  179. };