TOR: update to v0.2.5.12
[tomato.git] / release / src / router / tor / src / test / test_hs.c
blob99ef7dd570759187c3bc3b646522e1d9e251c9d2
1 /* Copyright (c) 2007-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file test_hs.c
6 * \brief Unit tests for hidden service.
7 **/
9 #define CONTROL_PRIVATE
10 #include "or.h"
11 #include "test.h"
12 #include "control.h"
14 /* mock ID digest and longname for node that's in nodelist */
15 #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
16 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
17 #define STR_HSDIR_EXIST_LONGNAME \
18 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
19 /* mock ID digest and longname for node that's not in nodelist */
20 #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
21 "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
22 #define STR_HSDIR_NONE_EXIST_LONGNAME \
23 "$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.
30 * Always free it after use! */
31 static char *received_msg = NULL;
33 /** Mock function for send_control_event_string
35 static void
36 send_control_event_string_replacement(uint16_t event, event_format_t which,
37 const char *msg)
39 (void) event;
40 (void) which;
41 tor_free(received_msg);
42 received_msg = tor_strdup(msg);
45 /** Mock function for node_describe_longname_by_id, it returns either
46 * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
48 static const char *
49 node_describe_longname_by_id_replacement(const char *id_digest)
51 if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
52 return STR_HSDIR_EXIST_LONGNAME;
53 } else {
54 return STR_HSDIR_NONE_EXIST_LONGNAME;
58 /** Make sure each hidden service descriptor async event generation
60 * function generates the message in expected format.
62 static void
63 test_hs_desc_event(void *arg)
65 #define STR_HS_ADDR "ajhb7kljbiru65qo"
66 #define STR_HS_ID "b3oeducbhjmbqmgw2i3jtz4fekkrinwj"
68 rend_data_t rend_query;
69 const char *expected_msg;
71 (void) arg;
72 MOCK(send_control_event_string,
73 send_control_event_string_replacement);
74 MOCK(node_describe_longname_by_id,
75 node_describe_longname_by_id_replacement);
77 /* setup rend_query struct */
78 strncpy(rend_query.onion_address, STR_HS_ADDR,
79 REND_SERVICE_ID_LEN_BASE32+1);
80 rend_query.auth_type = 0;
82 /* test request event */
83 control_event_hs_descriptor_requested(&rend_query, HSDIR_EXIST_ID,
84 STR_HS_ID);
85 expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
86 STR_HSDIR_EXIST_LONGNAME" "STR_HS_ID"\r\n";
87 test_assert(received_msg);
88 test_streq(received_msg, expected_msg);
89 tor_free(received_msg);
91 /* test received event */
92 rend_query.auth_type = 1;
93 control_event_hs_descriptor_received(&rend_query, HSDIR_EXIST_ID);
94 expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
95 STR_HSDIR_EXIST_LONGNAME"\r\n";
96 test_assert(received_msg);
97 test_streq(received_msg, expected_msg);
98 tor_free(received_msg);
100 /* test failed event */
101 rend_query.auth_type = 2;
102 control_event_hs_descriptor_failed(&rend_query, HSDIR_NONE_EXIST_ID);
103 expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
104 STR_HSDIR_NONE_EXIST_LONGNAME"\r\n";
105 test_assert(received_msg);
106 test_streq(received_msg, expected_msg);
107 tor_free(received_msg);
109 /* test invalid auth type */
110 rend_query.auth_type = 999;
111 control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID);
112 expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
113 STR_HSDIR_EXIST_LONGNAME"\r\n";
114 test_assert(received_msg);
115 test_streq(received_msg, expected_msg);
116 tor_free(received_msg);
118 done:
119 UNMOCK(send_control_event_string);
120 UNMOCK(node_describe_longname_by_id);
121 tor_free(received_msg);
124 struct testcase_t hs_tests[] = {
125 { "hs_desc_event", test_hs_desc_event, TT_FORK,
126 NULL, NULL },
127 END_OF_TESTCASES