1 /* Copyright (c) 2017-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file test_hs_control.c
6 * \brief Unit tests for hidden service control port event and command.
9 #define CONTROL_PRIVATE
11 #include "core/or/or.h"
12 #include "test/test.h"
13 #include "feature/control/control.h"
14 #include "app/config/config.h"
15 #include "feature/hs/hs_common.h"
16 #include "feature/hs/hs_control.h"
17 #include "feature/nodelist/nodelist.h"
19 #include "feature/nodelist/node_st.h"
20 #include "feature/nodelist/routerstatus_st.h"
21 #include "lib/crypt_ops/crypto_format.h"
23 #include "test/test_helpers.h"
25 /* mock ID digest and longname for node that's in nodelist */
26 #define HSDIR_EXIST_ID \
27 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
28 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
29 #define STR_HSDIR_EXIST_LONGNAME \
30 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
31 #define STR_HSDIR_NONE_EXIST_LONGNAME \
32 "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
34 /* Helper global variable for hidden service descriptor event test.
35 * It's used as a pointer to dynamically created message buffer in
36 * send_control_event_string_replacement function, which mocks
37 * send_control_event_string function.
39 * Always free it after use! */
40 static char *received_msg
= NULL
;
42 /** Mock function for send_control_event_string
45 queue_control_event_string_replacement(uint16_t event
, char *msg
)
48 tor_free(received_msg
);
52 /** Mock function for node_describe_longname_by_id, it returns either
53 * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
56 node_describe_longname_by_id_replacement(const char *id_digest
)
58 if (!strcmp(id_digest
, HSDIR_EXIST_ID
)) {
59 return STR_HSDIR_EXIST_LONGNAME
;
61 return STR_HSDIR_NONE_EXIST_LONGNAME
;
65 /* HSDir fetch index is a series of 'D' */
66 #define HSDIR_INDEX_FETCH_HEX \
67 "4343434343434343434343434343434343434343434343434343434343434343"
68 #define HSDIR_INDEX_STORE_HEX \
69 "4444444444444444444444444444444444444444444444444444444444444444"
72 mock_node_get_by_id(const char *digest
)
75 memcpy(node
.identity
, digest
, DIGEST_LEN
);
76 memset(node
.hsdir_index
.fetch
, 'C', DIGEST256_LEN
);
77 memset(node
.hsdir_index
.store_first
, 'D', DIGEST256_LEN
);
82 test_hs_desc_event(void *arg
)
85 char *expected_msg
= NULL
;
86 char onion_address
[HS_SERVICE_ADDR_LEN_BASE32
+ 1];
87 ed25519_keypair_t identity_kp
;
88 ed25519_public_key_t blinded_pk
;
89 char base64_blinded_pk
[ED25519_BASE64_LEN
+ 1];
90 routerstatus_t hsdir_rs
;
91 hs_ident_dir_conn_t ident
;
94 MOCK(queue_control_event_string
,
95 queue_control_event_string_replacement
);
96 MOCK(node_describe_longname_by_id
,
97 node_describe_longname_by_id_replacement
);
98 MOCK(node_get_by_id
, mock_node_get_by_id
);
100 /* Setup what we need for this test. */
101 ed25519_keypair_generate(&identity_kp
, 0);
102 hs_build_address(&identity_kp
.pubkey
, HS_VERSION_THREE
, onion_address
);
103 ret
= hs_address_is_valid(onion_address
);
104 tt_int_op(ret
, OP_EQ
, 1);
105 memset(&blinded_pk
, 'B', sizeof(blinded_pk
));
106 memset(&hsdir_rs
, 0, sizeof(hsdir_rs
));
107 memcpy(hsdir_rs
.identity_digest
, HSDIR_EXIST_ID
, DIGEST_LEN
);
108 ret
= ed25519_public_to_base64(base64_blinded_pk
, &blinded_pk
);
109 tt_int_op(ret
, OP_EQ
, 0);
110 memcpy(&ident
.identity_pk
, &identity_kp
.pubkey
,
111 sizeof(ed25519_public_key_t
));
112 memcpy(&ident
.blinded_pk
, &blinded_pk
, sizeof(blinded_pk
));
114 /* HS_DESC REQUESTED ... */
115 hs_control_desc_event_requested(&identity_kp
.pubkey
, base64_blinded_pk
,
117 tor_asprintf(&expected_msg
, "650 HS_DESC REQUESTED %s NO_AUTH "
118 STR_HSDIR_EXIST_LONGNAME
" %s HSDIR_INDEX="
119 HSDIR_INDEX_FETCH_HEX
"\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
);
126 /* HS_DESC CREATED... */
127 hs_control_desc_event_created(onion_address
, &blinded_pk
);
128 tor_asprintf(&expected_msg
, "650 HS_DESC CREATED %s UNKNOWN "
130 onion_address
, base64_blinded_pk
);
131 tt_assert(received_msg
);
132 tt_str_op(received_msg
, OP_EQ
, expected_msg
);
133 tor_free(received_msg
);
134 tor_free(expected_msg
);
136 /* HS_DESC UPLOAD... */
137 uint8_t hsdir_index_store
[DIGEST256_LEN
];
138 memset(hsdir_index_store
, 'D', sizeof(hsdir_index_store
));
139 hs_control_desc_event_upload(onion_address
, HSDIR_EXIST_ID
,
140 &blinded_pk
, hsdir_index_store
);
141 tor_asprintf(&expected_msg
, "650 HS_DESC UPLOAD %s UNKNOWN "
142 STR_HSDIR_EXIST_LONGNAME
" %s "
143 "HSDIR_INDEX=" HSDIR_INDEX_STORE_HEX
"\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
);
150 /* HS_DESC FAILED... */
151 hs_control_desc_event_failed(&ident
, HSDIR_EXIST_ID
, "BAD_DESC");
152 tor_asprintf(&expected_msg
, "650 HS_DESC FAILED %s NO_AUTH "
153 STR_HSDIR_EXIST_LONGNAME
" %s "
154 "REASON=BAD_DESC\r\n",
155 onion_address
, base64_blinded_pk
);
156 tt_assert(received_msg
);
157 tt_str_op(received_msg
, OP_EQ
, expected_msg
);
158 tor_free(received_msg
);
159 tor_free(expected_msg
);
161 /* HS_DESC RECEIVED... */
162 hs_control_desc_event_received(&ident
, HSDIR_EXIST_ID
);
163 tor_asprintf(&expected_msg
, "650 HS_DESC RECEIVED %s NO_AUTH "
164 STR_HSDIR_EXIST_LONGNAME
" %s\r\n",
165 onion_address
, base64_blinded_pk
);
166 tt_assert(received_msg
);
167 tt_str_op(received_msg
, OP_EQ
, expected_msg
);
168 tor_free(received_msg
);
169 tor_free(expected_msg
);
171 /* HS_DESC UPLOADED... */
172 hs_control_desc_event_uploaded(&ident
, HSDIR_EXIST_ID
);
173 tor_asprintf(&expected_msg
, "650 HS_DESC UPLOADED %s UNKNOWN "
174 STR_HSDIR_EXIST_LONGNAME
"\r\n",
176 tt_assert(received_msg
);
177 tt_str_op(received_msg
, OP_EQ
, expected_msg
);
178 tor_free(received_msg
);
179 tor_free(expected_msg
);
182 UNMOCK(queue_control_event_string
);
183 UNMOCK(node_describe_longname_by_id
);
184 UNMOCK(node_get_by_id
);
185 tor_free(received_msg
);
186 tor_free(expected_msg
);
189 struct testcase_t hs_control_tests
[] = {
190 { "hs_desc_event", test_hs_desc_event
, TT_FORK
,