Debian Codegraph @

crt/aws-c-io/tests/byo_crypto_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 #ifdef _MSC_VER
7 /* allow this file to be empty */
8 # pragma warning(disable : 4206)
9 #endif /* MSVC_VER */
10
11 /* these tests only get built and run with the BYO_CRYPTO compiler define. */
12 #ifdef BYO_CRYPTO
13
14 # include <aws/io/channel_bootstrap.h>
15 # include <aws/io/event_loop.h>
16 # include <aws/io/socket.h>
17 # include <aws/io/socket_channel_handler.h>
18 # include <aws/io/tls_channel_handler.h>
19
20 # include <aws/common/atomics.h>
21 # include <aws/common/condition_variable.h>
22
23 # include <aws/testing/aws_test_harness.h>
24
25 # include "statistics_handler_test.h"
26 # include <read_write_test_handler.h>
27
28 struct byo_crypto_test_args {
29 struct aws_allocator *allocator;
30 struct aws_mutex *mutex;
31 struct aws_condition_variable *condition_variable;
32 struct aws_channel *channel;
33 struct aws_channel_handler *rw_handler;
34 struct aws_channel_slot *rw_slot;
35 struct aws_tls_ctx tls_ctx;
36 struct aws_tls_connection_options tls_options;
37 aws_tls_on_negotiation_result_fn *negotiation_result_fn;
38 void *cb_data;
39 int error_code;
40 bool shutdown_invoked;
41 bool listener_destroyed;
42 bool setup_completed;
43 };
44
45 /* common structure for test */
46 struct byo_crypto_common_tester {
47 struct aws_mutex mutex;
48 struct aws_condition_variable condition_variable;
49 struct aws_event_loop_group *el_group;
50 };
51
52 static struct byo_crypto_common_tester c_tester;
53
54 static int s_byo_crypto_common_tester_init(struct aws_allocator *allocator, struct byo_crypto_common_tester *tester) {
55 AWS_ZERO_STRUCT(*tester);
56 aws_io_library_init(allocator);
57
58 struct aws_event_loop_group_options elg_options = {
59 .loop_count = 0,
60 };
61 tester->el_group = aws_event_loop_group_new(allocator, &elg_options);
62 struct aws_mutex mutex = AWS_MUTEX_INIT;
63 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
64 tester->mutex = mutex;
65 tester->condition_variable = condition_variable;
66 return AWS_OP_SUCCESS;
67 }
68
69 static int s_byo_crypto_common_tester_clean_up(struct byo_crypto_common_tester *tester) {
70 aws_event_loop_group_release(tester->el_group);
71 aws_mutex_clean_up(&tester->mutex);
72
73 aws_io_library_clean_up();
74
75 return AWS_OP_SUCCESS;
76 }
77
78 /* common structure for a local server */
79 struct local_server_tester {
80 struct aws_socket_options socket_options;
81 struct aws_socket_endpoint endpoint;
82 struct aws_server_bootstrap *server_bootstrap;
83 struct aws_socket *listener;
84 };
85
86 static bool s_channel_setup_predicate(void *user_data) {
87 struct byo_crypto_test_args *setup_test_args = user_data;
88 return setup_test_args->setup_completed;
89 }
90
91 static bool s_channel_shutdown_predicate(void *user_data) {
92 struct byo_crypto_test_args *setup_test_args = user_data;
93 return setup_test_args->shutdown_invoked;
94 }
95
96 static bool s_listener_destroy_predicate(void *user_data) {
97 struct byo_crypto_test_args *setup_test_args = user_data;
98 return setup_test_args->listener_destroyed;
99 }
100
101 static void s_byo_crypto_test_client_setup_callback(
102 struct aws_client_bootstrap *bootstrap,
103 int error_code,
104 struct aws_channel *channel,
105 void *user_data) {
106
107 (void)bootstrap;
108 (void)error_code;
109
110 struct byo_crypto_test_args *setup_test_args = user_data;
111
112 aws_mutex_lock(setup_test_args->mutex);
113 setup_test_args->channel = channel;
114 setup_test_args->setup_completed = true;
115 aws_mutex_unlock(setup_test_args->mutex);
116 aws_condition_variable_notify_one(setup_test_args->condition_variable);
117 }
118
119 static void s_byo_crypto_test_server_setup_callback(
120 struct aws_server_bootstrap *bootstrap,
121 int error_code,
122 struct aws_channel *channel,
123 void *user_data) {
124
125 (void)bootstrap;
126 (void)error_code;
127
128 struct byo_crypto_test_args *setup_test_args = user_data;
129
130 aws_mutex_lock(setup_test_args->mutex);
131 setup_test_args->channel = channel;
132 setup_test_args->setup_completed = true;
133 aws_mutex_unlock(setup_test_args->mutex);
134 aws_condition_variable_notify_one(setup_test_args->condition_variable);
135 }
136
137 static void s_byo_crypto_test_client_shutdown_callback(
138 struct aws_client_bootstrap *bootstrap,
139 int error_code,
140 struct aws_channel *channel,
141 void *user_data) {
142
143 (void)bootstrap;
144 (void)channel;
145
146 struct byo_crypto_test_args *setup_test_args = user_data;
147 aws_mutex_lock(setup_test_args->mutex);
148 setup_test_args->shutdown_invoked = true;
149 setup_test_args->error_code = error_code;
150 aws_mutex_unlock(setup_test_args->mutex);
151
152 aws_condition_variable_notify_one(setup_test_args->condition_variable);
153 }
154
155 static void s_byo_crypto_test_server_shutdown_callback(
156 struct aws_server_bootstrap *bootstrap,
157 int error_code,
158 struct aws_channel *channel,
159 void *user_data) {
160
161 (void)bootstrap;
162 (void)error_code;
163 (void)channel;
164
165 struct byo_crypto_test_args *setup_test_args = user_data;
166 aws_mutex_lock(setup_test_args->mutex);
167 setup_test_args->shutdown_invoked = true;
168 setup_test_args->error_code = error_code;
169 aws_mutex_unlock(setup_test_args->mutex);
170
171 aws_condition_variable_notify_one(setup_test_args->condition_variable);
172 }
173
174 struct byo_crypto_test_rw_args {
175 struct aws_mutex *mutex;
176 struct aws_condition_variable *condition_variable;
177 struct aws_byte_buf received_message;
178 struct byo_crypto_test_args *test_args;
179 bool invocation_happened;
180 bool shutdown_finished;
181 };
182
183 static bool s_byo_crypto_test_predicate(void *user_data) {
184 struct byo_crypto_test_rw_args *rw_args = user_data;
185 return rw_args->invocation_happened;
186 }
187
188 static struct aws_byte_buf s_byo_crypto_test_handle_read(
189 struct aws_channel_handler *handler,
190 struct aws_channel_slot *slot,
191 struct aws_byte_buf *data_read,
192 void *user_data) {
193
194 (void)handler;
195 (void)slot;
196
197 struct byo_crypto_test_rw_args *rw_args = user_data;
198
199 aws_mutex_lock(rw_args->mutex);
200 memcpy(rw_args->received_message.buffer + rw_args->received_message.len, data_read->buffer, data_read->len);
201 rw_args->received_message.len += data_read->len;
202 rw_args->invocation_happened = true;
203 aws_condition_variable_notify_one(rw_args->condition_variable);
204 aws_mutex_unlock(rw_args->mutex);
205 if (rw_args->test_args->negotiation_result_fn) {
206 rw_args->test_args->negotiation_result_fn(handler, slot, AWS_ERROR_SUCCESS, rw_args->test_args->cb_data);
207 rw_args->test_args->negotiation_result_fn = NULL;
208 }
209
210 return rw_args->received_message;
211 }
212
213 static struct aws_byte_buf s_byo_crypto_test_handle_write(
214 struct aws_channel_handler *handler,
215 struct aws_channel_slot *slot,
216 struct aws_byte_buf *data_read,
217 void *user_data) {
218
219 (void)handler;
220 (void)slot;
221 (void)data_read;
222 (void)user_data;
223
224 /*do nothing*/
225 return (struct aws_byte_buf){0};
226 }
227
228 static void s_byo_crypto_test_server_listener_destroy_callback(
229 struct aws_server_bootstrap *bootstrap,
230 void *user_data) {
231
232 (void)bootstrap;
233
234 struct byo_crypto_test_args *setup_test_args = user_data;
235 aws_mutex_lock(setup_test_args->mutex);
236 setup_test_args->listener_destroyed = true;
237 aws_mutex_unlock(setup_test_args->mutex);
238 aws_condition_variable_notify_one(setup_test_args->condition_variable);
239 }
240
241 static int s_rw_args_init(
242 struct byo_crypto_test_rw_args *args,
243 struct byo_crypto_common_tester *s_c_tester,
244 struct aws_byte_buf received_message,
245 int expected_read) {
246 AWS_ZERO_STRUCT(*args);
247 args->mutex = &s_c_tester->mutex;
248 args->condition_variable = &s_c_tester->condition_variable;
249 args->received_message = received_message;
250 return AWS_OP_SUCCESS;
251 }
252
253 static int s_byo_crypto_test_args_init(
254 struct byo_crypto_test_args *args,
255 struct byo_crypto_common_tester *s_c_tester,
256 struct aws_channel_handler *rw_handler) {
257 AWS_ZERO_STRUCT(*args);
258 args->mutex = &s_c_tester->mutex;
259 args->condition_variable = &s_c_tester->condition_variable;
260 args->rw_handler = rw_handler;
261 return AWS_OP_SUCCESS;
262 }
263
264 static int s_local_server_tester_init(
265 struct aws_allocator *allocator,
266 struct local_server_tester *tester,
267 struct byo_crypto_test_args *args,
268 struct byo_crypto_common_tester *s_c_tester,
269 bool enable_back_pressure) {
270 AWS_ZERO_STRUCT(*tester);
271 tester->socket_options.connect_timeout_ms = 3000;
272 tester->socket_options.type = AWS_SOCKET_STREAM;
273 tester->socket_options.domain = AWS_SOCKET_LOCAL;
274
275 aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);
276
277 tester->server_bootstrap = aws_server_bootstrap_new(allocator, s_c_tester->el_group);
278 ASSERT_NOT_NULL(tester->server_bootstrap);
279
280 aws_atomic_init_int((volatile struct aws_atomic_var *)&args->tls_ctx.ref_count, 1u);
281
282 args->tls_options.ctx = &args->tls_ctx;
283
284 struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
285 .bootstrap = tester->server_bootstrap,
286 .enable_read_back_pressure = enable_back_pressure,
287 .port = tester->endpoint.port,
288 .host_name = tester->endpoint.address,
289 .socket_options = &tester->socket_options,
290 .incoming_callback = s_byo_crypto_test_server_setup_callback,
291 .shutdown_callback = s_byo_crypto_test_server_shutdown_callback,
292 .destroy_callback = s_byo_crypto_test_server_listener_destroy_callback,
293 .tls_options = &args->tls_options,
294 .user_data = args,
295 };
296 tester->listener = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
297 ASSERT_NOT_NULL(tester->listener);
298
299 return AWS_OP_SUCCESS;
300 }
301
302 static int s_local_server_tester_clean_up(struct local_server_tester *tester) {
303 aws_server_bootstrap_release(tester->server_bootstrap);
304 return AWS_OP_SUCCESS;
305 }
306
307 static const char *s_write_tag = "I'm a big teapot";
308
309 static int s_start_negotiation_fn(struct aws_channel_handler *handler, void *user_data) {
310 struct aws_byte_buf write_tag = aws_byte_buf_from_c_str(s_write_tag);
311 rw_handler_write(handler, handler->slot, &write_tag);
312
313 struct byo_crypto_test_args *test_args = user_data;
314 if (test_args->negotiation_result_fn) {
315 test_args->negotiation_result_fn(handler, handler->slot, AWS_ERROR_SUCCESS, test_args->cb_data);
316 test_args->negotiation_result_fn = NULL;
317 }
318
319 return AWS_OP_SUCCESS;
320 }
321
322 struct aws_channel_handler *s_tls_handler_new(
323 struct aws_allocator *allocator,
324 struct aws_tls_connection_options *options,
325 struct aws_channel_slot *slot,
326 void *user_data) {
327 (void)allocator;
328 (void)options;
329 (void)slot;
330
331 struct byo_crypto_test_args *test_args = user_data;
332 test_args->negotiation_result_fn = options->on_negotiation_result;
333 test_args->cb_data = options->user_data;
334 return test_args->rw_handler;
335 }
336
337 static int s_byo_tls_handler_test(struct aws_allocator *allocator, void *ctx) {
338 (void)ctx;
339
340 s_byo_crypto_common_tester_init(allocator, &c_tester);
341
342 struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("I'm a little teapot.");
343 struct aws_byte_buf write_tag = aws_byte_buf_from_c_str(s_write_tag);
344
345 uint8_t incoming_received_message[128] = {0};
346 uint8_t outgoing_received_message[128] = {0};
347
348 struct byo_crypto_test_rw_args incoming_rw_args;
349 ASSERT_SUCCESS(s_rw_args_init(
350 &incoming_rw_args,
351 &c_tester,
352 aws_byte_buf_from_empty_array(incoming_received_message, sizeof(incoming_received_message)),
353 (int)write_tag.len));
354
355 struct byo_crypto_test_rw_args outgoing_rw_args;
356 ASSERT_SUCCESS(s_rw_args_init(
357 &outgoing_rw_args,
358 &c_tester,
359 aws_byte_buf_from_empty_array(outgoing_received_message, sizeof(outgoing_received_message)),
360 (int)read_tag.len));
361
362 /* doesn't matter what these are, I'm turning back pressure off anyways. */
363 static size_t s_outgoing_initial_read_window = 128;
364 static size_t s_incoming_initial_read_window = 128;
365 struct aws_channel_handler *outgoing_rw_handler = rw_handler_new(
366 allocator,
367 s_byo_crypto_test_handle_read,
368 s_byo_crypto_test_handle_write,
369 true,
370 s_outgoing_initial_read_window,
371 &outgoing_rw_args);
372 ASSERT_NOT_NULL(outgoing_rw_handler);
373
374 struct aws_channel_handler *incoming_rw_handler = rw_handler_new(
375 allocator,
376 s_byo_crypto_test_handle_read,
377 s_byo_crypto_test_handle_write,
378 true,
379 s_incoming_initial_read_window,
380 &incoming_rw_args);
381 ASSERT_NOT_NULL(outgoing_rw_handler);
382
383 struct byo_crypto_test_args incoming_args;
384 ASSERT_SUCCESS(s_byo_crypto_test_args_init(&incoming_args, &c_tester, incoming_rw_handler));
385 incoming_rw_args.test_args = &incoming_args;
386
387 struct byo_crypto_test_args outgoing_args;
388 ASSERT_SUCCESS(s_byo_crypto_test_args_init(&outgoing_args, &c_tester, outgoing_rw_handler));
389 outgoing_rw_args.test_args = &outgoing_args;
390
391 struct aws_tls_byo_crypto_setup_options client_setup_options = {
392 .new_handler_fn = s_tls_handler_new,
393 .start_negotiation_fn = s_start_negotiation_fn,
394 .user_data = &outgoing_args,
395 };
396
397 aws_tls_byo_crypto_set_client_setup_options(&client_setup_options);
398
399 struct aws_tls_byo_crypto_setup_options server_setup_options = {
400 .new_handler_fn = s_tls_handler_new,
401 .user_data = &incoming_args,
402 };
403
404 aws_tls_byo_crypto_set_server_setup_options(&server_setup_options);
405
406 struct local_server_tester local_server_tester;
407 ASSERT_SUCCESS(s_local_server_tester_init(allocator, &local_server_tester, &incoming_args, &c_tester, true));
408
409 aws_atomic_init_int((volatile struct aws_atomic_var *)&outgoing_args.tls_ctx.ref_count, 1u);
410 outgoing_args.tls_options.ctx = &outgoing_args.tls_ctx;
411
412 struct aws_client_bootstrap_options bootstrap_options = {
413 .event_loop_group = c_tester.el_group,
414 .host_resolver = NULL,
415 };
416
417 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
418 ASSERT_NOT_NULL(client_bootstrap);
419
420 struct aws_socket_channel_bootstrap_options channel_options;
421 AWS_ZERO_STRUCT(channel_options);
422 channel_options.bootstrap = client_bootstrap;
423 channel_options.host_name = local_server_tester.endpoint.address;
424 channel_options.port = 0;
425 channel_options.socket_options = &local_server_tester.socket_options;
426 channel_options.setup_callback = s_byo_crypto_test_client_setup_callback;
427 channel_options.shutdown_callback = s_byo_crypto_test_client_shutdown_callback;
428 channel_options.user_data = &outgoing_args;
429 channel_options.tls_options = &outgoing_args.tls_options;
430 channel_options.enable_read_back_pressure = false;
431
432 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
433 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
434
435 /* wait for both ends to setup */
436 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
437 &c_tester.condition_variable, &c_tester.mutex, s_channel_setup_predicate, &incoming_args));
438 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
439 &c_tester.condition_variable, &c_tester.mutex, s_channel_setup_predicate, &outgoing_args));
440
441 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
442 &c_tester.condition_variable, &c_tester.mutex, s_byo_crypto_test_predicate, &incoming_rw_args));
443
444 rw_handler_write(incoming_args.rw_handler, incoming_args.rw_handler->slot, &read_tag);
445 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
446 &c_tester.condition_variable, &c_tester.mutex, s_byo_crypto_test_predicate, &outgoing_rw_args));
447 incoming_rw_args.invocation_happened = false;
448 outgoing_rw_args.invocation_happened = false;
449
450 ASSERT_BIN_ARRAYS_EQUALS(
451 write_tag.buffer,
452 write_tag.len,
453 incoming_rw_args.received_message.buffer,
454 incoming_rw_args.received_message.len);
455 ASSERT_BIN_ARRAYS_EQUALS(
456 read_tag.buffer, read_tag.len, outgoing_rw_args.received_message.buffer, outgoing_rw_args.received_message.len);
457
458 /* only shut down one side, this should cause the other side to shutdown as well.*/
459 ASSERT_SUCCESS(aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS));
460 ASSERT_SUCCESS(aws_channel_shutdown(outgoing_args.channel, AWS_OP_SUCCESS));
461
462 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
463 &c_tester.condition_variable, &c_tester.mutex, s_channel_shutdown_predicate, &incoming_args));
464 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
465 &c_tester.condition_variable, &c_tester.mutex, s_channel_shutdown_predicate, &outgoing_args));
466 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
467 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
468 &c_tester.condition_variable, &c_tester.mutex, s_listener_destroy_predicate, &incoming_args));
469
470 aws_mutex_unlock(&c_tester.mutex);
471
472 /* clean up */
473 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
474
475 aws_client_bootstrap_release(client_bootstrap);
476 ASSERT_SUCCESS(s_byo_crypto_common_tester_clean_up(&c_tester));
477 return AWS_OP_SUCCESS;
478 }
479
480 AWS_TEST_CASE(byo_tls_handler_test, s_byo_tls_handler_test)
481
482 #endif /* BYO_CRYPTO */