Debian Codegraph @

crt/aws-c-io/tests/alpn_handler_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/channel.h>
7 #include <aws/io/event_loop.h>
8 #include <aws/io/private/event_loop_impl.h>
9 #include <aws/io/tls_channel_handler.h>
10 #include <aws/testing/aws_test_harness.h>
11
12 #include <aws/common/clock.h>
13 #include <aws/common/condition_variable.h>
14
15 struct alpn_channel_setup_test_args {
16 struct aws_condition_variable condition_variable;
17 struct aws_mutex mutex;
18 int error_code;
19 bool shutdown_finished;
20 bool setup_completed;
21 };
22
23 static void s_alpn_channel_setup_test_on_setup_completed(struct aws_channel *channel, int error_code, void *ctx) {
24 (void)channel;
25
26 struct alpn_channel_setup_test_args *setup_test_args = (struct alpn_channel_setup_test_args *)ctx;
27 aws_mutex_lock(&setup_test_args->mutex);
28 setup_test_args->setup_completed = true;
29 setup_test_args->error_code |= error_code;
30 aws_mutex_unlock(&setup_test_args->mutex);
31 aws_condition_variable_notify_one(&setup_test_args->condition_variable);
32 }
33
34 static bool s_alpn_test_setup_completed_predicate(void *arg) {
35 struct alpn_channel_setup_test_args *setup_test_args = (struct alpn_channel_setup_test_args *)arg;
36 return setup_test_args->setup_completed;
37 }
38
39 struct alpn_test_on_negotiation_args {
40 struct aws_allocator *allocator;
41 struct aws_channel_slot *new_slot;
42 struct aws_channel_handler *new_handler;
43 struct aws_byte_buf protocol;
44 };
45
46 static int s_alpn_test_shutdown(
47 struct aws_channel_handler *handler,
48 struct aws_channel_slot *slot,
49 enum aws_channel_direction dir,
50 int error_code,
51 bool abort_immediately) {
52 (void)handler;
53
54 return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately);
55 }
56
57 static size_t s_alpn_test_message_overhead(struct aws_channel_handler *handler) {
58 (void)handler;
59
60 return 0;
61 }
62
63 static size_t s_alpn_test_initial_window_size(struct aws_channel_handler *handler) {
64 (void)handler;
65
66 return SIZE_MAX;
67 }
68
69 static void s_alpn_test_destroy(struct aws_channel_handler *handler) {
70 aws_mem_release(handler->alloc, (void *)handler);
71 }
72
73 struct aws_channel_handler_vtable s_alpn_test_vtable = {
74 .destroy = s_alpn_test_destroy,
75 .shutdown = s_alpn_test_shutdown,
76 .initial_window_size = s_alpn_test_initial_window_size,
77 .message_overhead = s_alpn_test_message_overhead,
78 };
79
80 static struct aws_channel_handler *s_alpn_tls_successful_negotiation(
81 struct aws_channel_slot *new_slot,
82 struct aws_byte_buf *protocol,
83 void *ctx) {
84 struct alpn_test_on_negotiation_args *negotiation_args = (struct alpn_test_on_negotiation_args *)ctx;
85
86 struct aws_channel_handler *handler =
87 aws_mem_calloc(negotiation_args->allocator, 1, sizeof(struct aws_channel_handler));
88
89 negotiation_args->new_handler = handler;
90 negotiation_args->protocol = *protocol;
91 negotiation_args->new_slot = new_slot;
92
93 handler->vtable = &s_alpn_test_vtable;
94 handler->alloc = negotiation_args->allocator;
95
96 return handler;
97 }
98
99 static bool s_alpn_test_shutdown_predicate(void *arg) {
100 struct alpn_channel_setup_test_args *test_args = (struct alpn_channel_setup_test_args *)arg;
101 return test_args->shutdown_finished;
102 }
103
104 static void s_on_server_channel_on_shutdown(struct aws_channel *channel, int error_code, void *user_data) {
105
106 (void)channel;
107 (void)error_code;
108
109 struct alpn_channel_setup_test_args *test_args = (struct alpn_channel_setup_test_args *)user_data;
110
111 aws_mutex_lock(&test_args->mutex);
112 test_args->shutdown_finished = true;
113 aws_mutex_unlock(&test_args->mutex);
114
115 aws_condition_variable_notify_one(&test_args->condition_variable);
116 }
117
118 static int s_test_alpn_successfully_negotiates(struct aws_allocator *allocator, void *ctx) {
119
120 (void)ctx;
121
122 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
123
124 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
125 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
126 struct aws_channel *channel;
127
128 struct alpn_channel_setup_test_args test_args = {
129 .error_code = 0,
130 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
131 .mutex = AWS_MUTEX_INIT,
132 .setup_completed = false,
133 .shutdown_finished = false,
134 };
135
136 struct aws_channel_options args = {
137 .on_setup_completed = s_alpn_channel_setup_test_on_setup_completed,
138 .setup_user_data = &test_args,
139 .on_shutdown_completed = s_on_server_channel_on_shutdown,
140 .shutdown_user_data = &test_args,
141 .event_loop = event_loop,
142 };
143
144 channel = aws_channel_new(allocator, &args);
145 ASSERT_NOT_NULL(channel);
146 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
147 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
148 &test_args.condition_variable, &test_args.mutex, s_alpn_test_setup_completed_predicate, &test_args));
149 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
150
151 struct aws_channel_slot *slot = aws_channel_slot_new(channel);
152 ASSERT_NOT_NULL(slot);
153
154 struct alpn_test_on_negotiation_args on_negotiation_args = {
155 .new_slot = NULL, .protocol = {0}, .new_handler = NULL, .allocator = allocator};
156
157 struct aws_channel_handler *handler =
158 aws_tls_alpn_handler_new(allocator, s_alpn_tls_successful_negotiation, &on_negotiation_args);
159 ASSERT_NOT_NULL(handler);
160 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, handler));
161
162 struct aws_tls_negotiated_protocol_message protocol_message = {.protocol = aws_byte_buf_from_c_str("h2")};
163
164 struct aws_io_message message = {
165 .allocator = NULL,
166 .user_data = NULL,
167 .message_tag = AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE,
168 .message_data = aws_byte_buf_from_array(
169 (const uint8_t *)&protocol_message, sizeof(struct aws_tls_negotiated_protocol_message)),
170 .copy_mark = 0,
171 .on_completion = NULL,
172 .message_type = AWS_IO_MESSAGE_APPLICATION_DATA,
173 };
174
175 ASSERT_SUCCESS(aws_channel_handler_process_read_message(handler, slot, &message));
176 ASSERT_BIN_ARRAYS_EQUALS(
177 protocol_message.protocol.buffer,
178 protocol_message.protocol.len,
179 on_negotiation_args.protocol.buffer,
180 on_negotiation_args.protocol.len);
181
182 aws_channel_shutdown(channel, AWS_OP_SUCCESS);
183 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
184 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
185 &test_args.condition_variable, &test_args.mutex, s_alpn_test_shutdown_predicate, &test_args));
186 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
187
188 aws_channel_destroy(channel);
189 aws_event_loop_destroy(event_loop);
190
191 return AWS_OP_SUCCESS;
192 }
193
194 AWS_TEST_CASE(alpn_successfully_negotiates, s_test_alpn_successfully_negotiates)
195
196 static int s_test_alpn_no_protocol_message(struct aws_allocator *allocator, void *ctx) {
197
198 (void)ctx;
199
200 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
201
202 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
203 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
204 struct aws_channel *channel;
205
206 struct alpn_channel_setup_test_args test_args = {
207 .error_code = 0,
208 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
209 .mutex = AWS_MUTEX_INIT,
210 .shutdown_finished = false,
211 };
212
213 struct aws_channel_options args = {
214 .on_setup_completed = s_alpn_channel_setup_test_on_setup_completed,
215 .setup_user_data = &test_args,
216 .on_shutdown_completed = s_on_server_channel_on_shutdown,
217 .shutdown_user_data = &test_args,
218 .event_loop = event_loop,
219 };
220
221 channel = aws_channel_new(allocator, &args);
222 ASSERT_NOT_NULL(channel);
223 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
224 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
225 &test_args.condition_variable, &test_args.mutex, s_alpn_test_setup_completed_predicate, &test_args));
226 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
227
228 struct aws_channel_slot *slot = aws_channel_slot_new(channel);
229 ASSERT_NOT_NULL(slot);
230
231 struct alpn_test_on_negotiation_args on_negotiation_args = {
232 .new_slot = NULL, .protocol = {0}, .new_handler = NULL, .allocator = allocator};
233
234 struct aws_channel_handler *handler =
235 aws_tls_alpn_handler_new(allocator, s_alpn_tls_successful_negotiation, &on_negotiation_args);
236 ASSERT_NOT_NULL(handler);
237 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, handler));
238
239 /*this is just for the test since it's the only slot in the channel */
240 handler->vtable->shutdown = s_alpn_test_shutdown;
241
242 struct aws_io_message message = {
243 .allocator = NULL,
244 .user_data = NULL,
245 .message_tag = 0,
246 .copy_mark = 0,
247 .on_completion = NULL,
248 .message_type = AWS_IO_MESSAGE_APPLICATION_DATA,
249 };
250
251 ASSERT_ERROR(AWS_IO_MISSING_ALPN_MESSAGE, aws_channel_handler_process_read_message(handler, slot, &message));
252
253 aws_channel_shutdown(channel, AWS_OP_SUCCESS);
254 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
255 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
256 &test_args.condition_variable, &test_args.mutex, s_alpn_test_shutdown_predicate, &test_args));
257 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
258 aws_channel_destroy(channel);
259 aws_event_loop_destroy(event_loop);
260
261 return AWS_OP_SUCCESS;
262 }
263
264 AWS_TEST_CASE(alpn_no_protocol_message, s_test_alpn_no_protocol_message)
265
266 static struct aws_channel_handler *s_alpn_tls_failed_negotiation(
267 struct aws_channel_slot *new_slot,
268 struct aws_byte_buf *protocol,
269 void *ctx) {
270
271 (void)new_slot;
272 (void)protocol;
273 (void)ctx;
274
275 return NULL;
276 }
277
278 static int s_test_alpn_error_creating_handler(struct aws_allocator *allocator, void *ctx) {
279
280 (void)ctx;
281
282 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
283
284 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
285 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
286 struct aws_channel *channel;
287
288 struct alpn_channel_setup_test_args test_args = {
289 .error_code = 0,
290 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
291 .mutex = AWS_MUTEX_INIT,
292 .shutdown_finished = false,
293 };
294
295 struct aws_channel_options args = {
296 .on_setup_completed = s_alpn_channel_setup_test_on_setup_completed,
297 .setup_user_data = &test_args,
298 .on_shutdown_completed = s_on_server_channel_on_shutdown,
299 .shutdown_user_data = &test_args,
300 .event_loop = event_loop,
301 };
302
303 channel = aws_channel_new(allocator, &args);
304 ASSERT_NOT_NULL(channel);
305 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
306 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
307 &test_args.condition_variable, &test_args.mutex, s_alpn_test_setup_completed_predicate, &test_args));
308 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
309
310 struct aws_channel_slot *slot = aws_channel_slot_new(channel);
311 ASSERT_NOT_NULL(slot);
312
313 struct aws_tls_negotiated_protocol_message protocol_message = {.protocol = aws_byte_buf_from_c_str("h2")};
314
315 struct aws_io_message message = {
316 .allocator = NULL,
317 .user_data = NULL,
318 .message_tag = AWS_TLS_NEGOTIATED_PROTOCOL_MESSAGE,
319 .message_data = aws_byte_buf_from_array(
320 (const uint8_t *)&protocol_message, sizeof(struct aws_tls_negotiated_protocol_message)),
321 .copy_mark = 0,
322 .on_completion = NULL,
323 .message_type = AWS_IO_MESSAGE_APPLICATION_DATA,
324 };
325
326 struct alpn_test_on_negotiation_args on_negotiation_args = {
327 .new_slot = NULL, .protocol = {0}, .new_handler = NULL, .allocator = allocator};
328
329 struct aws_channel_handler *handler =
330 aws_tls_alpn_handler_new(allocator, s_alpn_tls_failed_negotiation, &on_negotiation_args);
331 ASSERT_NOT_NULL(handler);
332 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, handler));
333
334 /*this is just for the test since it's the only slot in the channel */
335 handler->vtable->shutdown = s_alpn_test_shutdown;
336
337 ASSERT_ERROR(
338 AWS_IO_UNHANDLED_ALPN_PROTOCOL_MESSAGE, aws_channel_handler_process_read_message(handler, slot, &message));
339
340 aws_channel_shutdown(channel, AWS_OP_SUCCESS);
341 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
342 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
343 &test_args.condition_variable, &test_args.mutex, s_alpn_test_shutdown_predicate, &test_args));
344 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
345 aws_channel_destroy(channel);
346 aws_event_loop_destroy(event_loop);
347
348 return AWS_OP_SUCCESS;
349 }
350
351 AWS_TEST_CASE(alpn_error_creating_handler, s_test_alpn_error_creating_handler)