1 /* Copyright (c) 2014-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /* Unit tests for handling different kinds of relay cell */
9 #include "connection.h"
10 #include "connection_edge.h"
14 static int srm_ncalls
;
15 static entry_connection_t
*srm_conn
;
17 static size_t srm_alen
;
18 static int srm_answer_is_set
;
19 static uint8_t srm_answer
[512];
21 static time_t srm_expires
;
23 /* Mock replacement for connection_ap_hannshake_socks_resolved() */
25 socks_resolved_mock(entry_connection_t
*conn
,
28 const uint8_t *answer
,
34 srm_atype
= answer_type
;
35 srm_alen
= answer_len
;
37 memset(srm_answer
, 0, sizeof(srm_answer
));
38 memcpy(srm_answer
, answer
, answer_len
< 512 ? answer_len
: 512);
39 srm_answer_is_set
= 1;
41 srm_answer_is_set
= 0;
44 srm_expires
= expires
;
47 static int mum_ncalls
;
48 static entry_connection_t
*mum_conn
;
49 static int mum_endreason
;
51 /* Mock replacement for connection_mark_unattached_ap_() */
53 mark_unattached_mock(entry_connection_t
*conn
, int endreason
,
54 int line
, const char *file
)
58 mum_endreason
= endreason
;
63 /* Tests for connection_edge_process_resolved_cell().
65 The point of ..process_resolved_cell() is to handle an incoming cell
66 on an entry connection, and call connection_mark_unattached_ap() and/or
67 connection_ap_handshake_socks_resolved().
70 test_relaycell_resolved(void *arg
)
72 entry_connection_t
*entryconn
;
73 edge_connection_t
*edgeconn
;
77 or_options_t
*options
= get_options_mutable();
79 #define SET_CELL(s) do { \
80 memset(&cell, 0, sizeof(cell)); \
81 memset(&rh, 0, sizeof(rh)); \
82 memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
83 rh.length = sizeof((s))-1; \
84 rh.command = RELAY_COMMAND_RESOLVED; \
86 #define MOCK_RESET() do { \
87 srm_ncalls = mum_ncalls = 0; \
89 #define ASSERT_MARK_CALLED(reason) do { \
90 tt_int_op(mum_ncalls, OP_EQ, 1); \
91 tt_ptr_op(mum_conn, OP_EQ, entryconn); \
92 tt_int_op(mum_endreason, OP_EQ, (reason)); \
94 #define ASSERT_RESOLVED_CALLED(atype, answer, ttl, expires) do { \
95 tt_int_op(srm_ncalls, OP_EQ, 1); \
96 tt_ptr_op(srm_conn, OP_EQ, entryconn); \
97 tt_int_op(srm_atype, OP_EQ, (atype)); \
99 tt_int_op(srm_alen, OP_EQ, sizeof(answer)-1); \
100 tt_int_op(srm_alen, OP_LT, 512); \
101 tt_int_op(srm_answer_is_set, OP_EQ, 1); \
102 tt_mem_op(srm_answer, OP_EQ, answer, sizeof(answer)-1); \
104 tt_int_op(srm_answer_is_set, OP_EQ, 0); \
106 tt_int_op(srm_ttl, OP_EQ, ttl); \
107 tt_i64_op(srm_expires, OP_EQ, expires); \
112 MOCK(connection_mark_unattached_ap_
, mark_unattached_mock
);
113 MOCK(connection_ap_handshake_socks_resolved
, socks_resolved_mock
);
115 options
->ClientDNSRejectInternalAddresses
= 0;
117 SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
118 "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
119 /* IPv4: 18.0.0.1, ttl 512 */
120 "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"
121 /* IPv6: 2003::3, ttl 1024 */
123 "\x20\x02\x00\x00\x00\x00\x00\x00"
124 "\x00\x00\x00\x00\x00\x00\x00\x03"
127 entryconn
= entry_connection_new(CONN_TYPE_AP
, AF_INET
);
128 edgeconn
= ENTRY_TO_EDGE_CONN(entryconn
);
130 /* Try with connection in non-RESOLVE_WAIT state: cell gets ignored */
132 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
133 tt_int_op(r
, OP_EQ
, 0);
134 tt_int_op(srm_ncalls
, OP_EQ
, 0);
135 tt_int_op(mum_ncalls
, OP_EQ
, 0);
137 /* Now put it in the right state. */
138 ENTRY_TO_CONN(entryconn
)->state
= AP_CONN_STATE_RESOLVE_WAIT
;
139 entryconn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE
;
140 entryconn
->entry_cfg
.ipv4_traffic
= 1;
141 entryconn
->entry_cfg
.ipv6_traffic
= 1;
142 entryconn
->entry_cfg
.prefer_ipv6
= 0;
144 /* We prefer ipv4, so we should get the first ipv4 answer */
146 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
147 tt_int_op(r
, OP_EQ
, 0);
148 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
149 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
150 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4
, "\x7f\x00\x01\x02", 256, -1);
152 /* But we may be discarding private answers. */
154 options
->ClientDNSRejectInternalAddresses
= 1;
155 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
156 tt_int_op(r
, OP_EQ
, 0);
157 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
158 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
159 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4
, "\x12\x00\x00\x01", 512, -1);
161 /* now prefer ipv6, and get the first ipv6 answer */
162 entryconn
->entry_cfg
.prefer_ipv6
= 1;
164 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
165 tt_int_op(r
, OP_EQ
, 0);
166 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
167 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
168 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV6
,
169 "\x20\x02\x00\x00\x00\x00\x00\x00"
170 "\x00\x00\x00\x00\x00\x00\x00\x03",
173 /* With a cell that only has IPv4, we report IPv4 even if we prefer IPv6 */
175 SET_CELL("\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
176 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
177 tt_int_op(r
, OP_EQ
, 0);
178 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
179 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
180 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4
, "\x12\x00\x00\x01", 512, -1);
182 /* But if we don't allow IPv4, we report nothing if the cell contains only
185 entryconn
->entry_cfg
.ipv4_traffic
= 0;
186 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
187 tt_int_op(r
, OP_EQ
, 0);
188 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
189 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
190 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR
, NULL
, -1, -1);
192 /* If we wanted hostnames, we report nothing, since we only had IPs. */
194 entryconn
->entry_cfg
.ipv4_traffic
= 1;
195 entryconn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE_PTR
;
196 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
197 tt_int_op(r
, OP_EQ
, 0);
198 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
199 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
200 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR
, NULL
, -1, -1);
202 /* A hostname cell is fine though. */
204 SET_CELL("\x00\x0fwww.example.com\x00\x01\x00\x00");
205 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
206 tt_int_op(r
, OP_EQ
, 0);
207 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
208 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
209 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_HOSTNAME
, "www.example.com", 65536, -1);
211 /* error on malformed cell */
213 entryconn
->socks_request
->command
= SOCKS_COMMAND_RESOLVE
;
214 SET_CELL("\x04\x04\x01\x02\x03\x04"); /* no ttl */
215 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
216 tt_int_op(r
, OP_EQ
, 0);
217 ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL
);
218 tt_int_op(srm_ncalls
, OP_EQ
, 0);
220 /* error on all addresses private */
222 SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
223 "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
224 /* IPv4: 192.168.1.1, ttl 256 */
225 "\x04\x04\xc0\xa8\x01\x01\x00\x00\x01\x00");
226 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
227 tt_int_op(r
, OP_EQ
, 0);
228 ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL
);
229 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT
, NULL
, 0, TIME_MAX
);
231 /* Legit error code */
233 SET_CELL("\xf0\x15" "quiet and meaningless" "\x00\x00\x0f\xff");
234 r
= connection_edge_process_resolved_cell(edgeconn
, &cell
, &rh
);
235 tt_int_op(r
, OP_EQ
, 0);
236 ASSERT_MARK_CALLED(END_STREAM_REASON_DONE
|
237 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
238 ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT
, NULL
, -1, -1);
241 UNMOCK(connection_mark_unattached_ap_
);
242 UNMOCK(connection_ap_handshake_socks_resolved
);
245 struct testcase_t relaycell_tests
[] = {
246 { "resolved", test_relaycell_resolved
, TT_FORK
, NULL
, NULL
},