Debian Codegraph @

crt/aws-c-io/tests/socket_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 #include <aws/io/channel_bootstrap.h>
6 #include <aws/io/event_loop.h>
7 #include <aws/io/private/event_loop_impl.h>
8 #include <aws/io/socket.h>
9 #include <aws/io/socket_channel_handler.h>
10 #include <aws/io/statistics.h>
11
12 #include <aws/common/atomics.h>
13 #include <aws/common/clock.h>
14 #include <aws/common/condition_variable.h>
15
16 #include <aws/testing/aws_test_harness.h>
17
18 #include "statistics_handler_test.h"
19 #include <read_write_test_handler.h>
20
21 #include "../include/aws/io/private/socket_impl.h"
22
23 #ifdef _MSC_VER
24 # pragma warning(disable : 4996) /* allow strncpy() */
25 #endif
26
27 #define NANOS_PER_SEC ((uint64_t)AWS_TIMESTAMP_NANOS)
28 #define TIMEOUT (10 * NANOS_PER_SEC)
29
30 struct socket_test_args {
31 struct aws_allocator *allocator;
32 struct aws_mutex *mutex;
33 struct aws_condition_variable *condition_variable;
34 struct aws_channel *channel;
35 struct aws_channel_handler *rw_handler;
36
37 struct aws_channel_slot *rw_slot;
38 int error_code;
39 bool shutdown_invoked;
40 bool error_invoked;
41 bool creation_callback_invoked;
42 bool listener_destroyed;
43 bool listener_connected;
44 };
45
46 /* common structure for test */
47 struct socket_common_tester {
48 struct aws_mutex mutex;
49 struct aws_condition_variable condition_variable;
50 struct aws_event_loop_group *el_group;
51 struct aws_host_resolver *resolver;
52 struct aws_atomic_var current_time_ns;
53 struct aws_atomic_var stats_handler;
54
55 bool setup_called;
56 struct aws_event_loop *requested_callback_event_loop;
57 int setup_error_code;
58 };
59
60 static struct socket_common_tester c_tester;
61
62 static int s_socket_common_tester_init(struct aws_allocator *allocator, struct socket_common_tester *tester) {
63 AWS_ZERO_STRUCT(*tester);
64 aws_io_library_init(allocator);
65
66 struct aws_event_loop_group_options elg_options = {
67 .loop_count = 0,
68 };
69 tester->el_group = aws_event_loop_group_new(allocator, &elg_options);
70
71 struct aws_host_resolver_default_options resolver_options = {
72 .el_group = tester->el_group,
73 .max_entries = 8,
74 };
75 tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
76
77 struct aws_mutex mutex = AWS_MUTEX_INIT;
78 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
79 tester->mutex = mutex;
80 tester->condition_variable = condition_variable;
81 aws_atomic_store_int(&tester->current_time_ns, 0);
82 aws_atomic_store_ptr(&tester->stats_handler, NULL);
83
84 return AWS_OP_SUCCESS;
85 }
86
87 static int s_socket_common_tester_clean_up(struct socket_common_tester *tester) {
88 aws_host_resolver_release(tester->resolver);
89 aws_event_loop_group_release(tester->el_group);
90
91 aws_mutex_clean_up(&tester->mutex);
92
93 aws_io_library_clean_up();
94
95 return AWS_OP_SUCCESS;
96 }
97
98 /* common structure for a local server */
99 struct local_server_tester {
100 struct aws_socket_options socket_options;
101 struct aws_socket_endpoint endpoint;
102 struct aws_server_bootstrap *server_bootstrap;
103 struct aws_socket *listener;
104 };
105
106 static bool s_pinned_channel_setup_predicate(void *user_data) {
107 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
108 return setup_test_args->channel != NULL;
109 }
110
111 static bool s_channel_setup_predicate(void *user_data) {
112 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
113 return setup_test_args->rw_slot != NULL;
114 }
115
116 static bool s_channel_shutdown_predicate(void *user_data) {
117 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
118 bool finished = setup_test_args->shutdown_invoked;
119 return finished;
120 }
121
122 static bool s_listener_destroy_predicate(void *user_data) {
123 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
124 bool finished = setup_test_args->listener_destroyed;
125 return finished;
126 }
127
128 static bool s_listener_connected_predicate(void *user_data) {
129 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
130 bool finished = setup_test_args->listener_connected;
131 return finished;
132 }
133
134 static void s_socket_handler_test_client_setup_callback(
135 struct aws_client_bootstrap *bootstrap,
136 int error_code,
137 struct aws_channel *channel,
138 void *user_data) {
139
140 (void)bootstrap;
141 (void)error_code;
142
143 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
144
145 aws_mutex_lock(setup_test_args->mutex);
146 setup_test_args->channel = channel;
147
148 struct aws_channel_slot *rw_slot = aws_channel_slot_new(channel);
149 aws_channel_slot_insert_end(channel, rw_slot);
150
151 aws_channel_slot_set_handler(rw_slot, setup_test_args->rw_handler);
152 setup_test_args->rw_slot = rw_slot;
153
154 aws_mutex_unlock(setup_test_args->mutex);
155
156 aws_condition_variable_notify_one(setup_test_args->condition_variable);
157 }
158
159 static void s_socket_handler_test_server_setup_callback(
160 struct aws_server_bootstrap *bootstrap,
161 int error_code,
162 struct aws_channel *channel,
163 void *user_data) {
164
165 (void)bootstrap;
166 (void)error_code;
167
168 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
169
170 aws_mutex_lock(setup_test_args->mutex);
171
172 setup_test_args->channel = channel;
173
174 if (setup_test_args->rw_handler != NULL) {
175 struct aws_channel_slot *rw_slot = aws_channel_slot_new(channel);
176 aws_channel_slot_insert_end(channel, rw_slot);
177
178 aws_channel_slot_set_handler(rw_slot, setup_test_args->rw_handler);
179 setup_test_args->rw_slot = rw_slot;
180 }
181
182 aws_mutex_unlock(setup_test_args->mutex);
183
184 aws_condition_variable_notify_one(setup_test_args->condition_variable);
185 }
186
187 static void s_socket_handler_test_client_shutdown_callback(
188 struct aws_client_bootstrap *bootstrap,
189 int error_code,
190 struct aws_channel *channel,
191 void *user_data) {
192
193 (void)bootstrap;
194 (void)channel;
195
196 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
197 aws_mutex_lock(setup_test_args->mutex);
198 setup_test_args->shutdown_invoked = true;
199 setup_test_args->error_code = error_code;
200 aws_mutex_unlock(setup_test_args->mutex);
201
202 aws_condition_variable_notify_one(setup_test_args->condition_variable);
203 }
204
205 static void s_socket_handler_test_server_shutdown_callback(
206 struct aws_server_bootstrap *bootstrap,
207 int error_code,
208 struct aws_channel *channel,
209 void *user_data) {
210
211 (void)bootstrap;
212 (void)error_code;
213 (void)channel;
214
215 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
216 aws_mutex_lock(setup_test_args->mutex);
217 setup_test_args->shutdown_invoked = true;
218 setup_test_args->error_code = error_code;
219 aws_mutex_unlock(setup_test_args->mutex);
220
221 aws_condition_variable_notify_one(setup_test_args->condition_variable);
222 }
223
224 struct socket_test_rw_args {
225 struct aws_mutex *mutex;
226 struct aws_condition_variable *condition_variable;
227 struct aws_byte_buf received_message;
228 size_t amount_read;
229 size_t expected_read;
230 size_t amount_written;
231 bool invocation_happened;
232 bool shutdown_finished;
233 };
234
235 static bool s_socket_test_read_predicate(void *user_data) {
236 struct socket_test_rw_args *rw_args = (struct socket_test_rw_args *)user_data;
237 return rw_args->invocation_happened;
238 }
239
240 static bool s_socket_test_full_read_predicate(void *user_data) {
241 struct socket_test_rw_args *rw_args = (struct socket_test_rw_args *)user_data;
242 return rw_args->invocation_happened && rw_args->amount_read == rw_args->expected_read;
243 }
244
245 static struct aws_byte_buf s_socket_test_handle_read(
246 struct aws_channel_handler *handler,
247 struct aws_channel_slot *slot,
248 struct aws_byte_buf *data_read,
249 void *user_data) {
250
251 (void)handler;
252 (void)slot;
253
254 struct socket_test_rw_args *rw_args = (struct socket_test_rw_args *)user_data;
255
256 aws_mutex_lock(rw_args->mutex);
257 AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_buffer(&rw_args->received_message, *data_read) == true);
258 rw_args->amount_read += data_read->len;
259 rw_args->invocation_happened = true;
260 aws_condition_variable_notify_one(rw_args->condition_variable);
261 aws_mutex_unlock(rw_args->mutex);
262
263 return rw_args->received_message;
264 }
265
266 void s_socket_test_handle_on_write_completed(
267 struct aws_channel *channel,
268 struct aws_io_message *message,
269 int error_code,
270 void *user_data) {
271
272 (void)channel;
273 AWS_FATAL_ASSERT(error_code == 0);
274 struct socket_test_rw_args *rw_args = (struct socket_test_rw_args *)user_data;
275
276 aws_mutex_lock(rw_args->mutex);
277 rw_args->amount_written += message->message_data.len;
278 rw_args->invocation_happened = true;
279 aws_condition_variable_notify_one(rw_args->condition_variable);
280 aws_mutex_unlock(rw_args->mutex);
281 }
282
283 static struct aws_byte_buf s_socket_test_handle_write(
284 struct aws_channel_handler *handler,
285 struct aws_channel_slot *slot,
286 struct aws_byte_buf *data_read,
287 void *user_data) {
288
289 (void)handler;
290 (void)slot;
291 (void)data_read;
292 (void)user_data;
293
294 /*do nothing*/
295 return (struct aws_byte_buf){0};
296 }
297
298 static void s_socket_handler_test_server_listener_destroy_callback(
299 struct aws_server_bootstrap *bootstrap,
300 void *user_data) {
301
302 (void)bootstrap;
303
304 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
305 aws_mutex_lock(setup_test_args->mutex);
306 setup_test_args->listener_destroyed = true;
307 aws_mutex_unlock(setup_test_args->mutex);
308 aws_condition_variable_notify_one(setup_test_args->condition_variable);
309 }
310
311 static void s_socket_handler_test_server_listener_setup_callback(
312 struct aws_server_bootstrap *bootstrap,
313 int error_code,
314 void *user_data) {
315
316 (void)bootstrap;
317
318 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
319 aws_mutex_lock(setup_test_args->mutex);
320 setup_test_args->listener_connected = true;
321 setup_test_args->error_code = error_code;
322 aws_condition_variable_notify_one(setup_test_args->condition_variable);
323 aws_mutex_unlock(setup_test_args->mutex);
324 }
325
326 static int s_rw_args_init(
327 struct socket_test_rw_args *args,
328 struct socket_common_tester *s_c_tester,
329 struct aws_byte_buf received_message,
330 int expected_read) {
331 AWS_ZERO_STRUCT(*args);
332 args->mutex = &s_c_tester->mutex;
333 args->condition_variable = &s_c_tester->condition_variable;
334 args->received_message = received_message;
335 args->expected_read = expected_read;
336 return AWS_OP_SUCCESS;
337 }
338
339 static int s_socket_test_args_init(
340 struct socket_test_args *args,
341 struct socket_common_tester *s_c_tester,
342 struct aws_channel_handler *rw_handler) {
343 AWS_ZERO_STRUCT(*args);
344 args->mutex = &s_c_tester->mutex;
345 args->condition_variable = &s_c_tester->condition_variable;
346 args->rw_handler = rw_handler;
347 return AWS_OP_SUCCESS;
348 }
349
350 static int s_local_server_tester_init(
351 struct aws_allocator *allocator,
352 struct local_server_tester *tester,
353 struct socket_test_args *args,
354 struct socket_common_tester *s_c_tester,
355 enum aws_socket_domain socket_domain,
356 bool enable_back_pressure) {
357
358 AWS_ZERO_STRUCT(*tester);
359 tester->socket_options.connect_timeout_ms = 3000;
360 tester->socket_options.type = AWS_SOCKET_STREAM;
361 tester->socket_options.domain = socket_domain;
362 switch (socket_domain) {
363 case AWS_SOCKET_LOCAL:
364 aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);
365 break;
366 case AWS_SOCKET_IPV4:
367 strncpy(tester->endpoint.address, "127.0.0.1", sizeof(tester->endpoint.address));
368 break;
369 case AWS_SOCKET_IPV6:
370 strncpy(tester->endpoint.address, "::1", sizeof(tester->endpoint.address));
371 break;
372 default:
373 ASSERT_TRUE(false);
374 break;
375 }
376
377 tester->server_bootstrap = aws_server_bootstrap_new(allocator, s_c_tester->el_group);
378 ASSERT_NOT_NULL(tester->server_bootstrap);
379
380 struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
381 .bootstrap = tester->server_bootstrap,
382 .enable_read_back_pressure = enable_back_pressure,
383 .port = tester->endpoint.port,
384 .host_name = tester->endpoint.address,
385 .socket_options = &tester->socket_options,
386 .incoming_callback = s_socket_handler_test_server_setup_callback,
387 .shutdown_callback = s_socket_handler_test_server_shutdown_callback,
388 .destroy_callback = s_socket_handler_test_server_listener_destroy_callback,
389 .setup_callback = s_socket_handler_test_server_listener_setup_callback,
390 .user_data = args,
391 };
392
393 tester->listener = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
394 ASSERT_NOT_NULL(tester->listener);
395 // if server setup properly, waiting for setup callback
396
397 ASSERT_SUCCESS(aws_mutex_lock(args->mutex));
398 /* wait for listener to connected */
399 ASSERT_SUCCESS(
400 aws_condition_variable_wait_pred(args->condition_variable, args->mutex, s_listener_connected_predicate, args));
401 ASSERT_TRUE(args->error_code == AWS_OP_SUCCESS);
402 ASSERT_SUCCESS(aws_mutex_unlock(args->mutex));
403
404 /* find out which port the socket is bound to */
405 ASSERT_SUCCESS(aws_socket_get_bound_address(tester->listener, &tester->endpoint));
406
407 return AWS_OP_SUCCESS;
408 }
409
410 static int s_local_server_tester_clean_up(struct local_server_tester *tester) {
411 aws_server_bootstrap_release(tester->server_bootstrap);
412 return AWS_OP_SUCCESS;
413 }
414
415 static int s_socket_pinned_event_loop_test(struct aws_allocator *allocator, void *ctx) {
416 (void)ctx;
417
418 s_socket_common_tester_init(allocator, &c_tester);
419
420 struct aws_channel_handler *client_rw_handler =
421 rw_handler_new(allocator, s_socket_test_handle_write, s_socket_test_handle_write, true, SIZE_MAX, NULL);
422 ASSERT_NOT_NULL(client_rw_handler);
423
424 struct aws_channel_handler *server_rw_handler =
425 rw_handler_new(allocator, s_socket_test_handle_write, s_socket_test_handle_write, true, SIZE_MAX, NULL);
426 ASSERT_NOT_NULL(server_rw_handler);
427
428 struct socket_test_args server_args;
429 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
430
431 struct socket_test_args client_args;
432 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
433
434 struct local_server_tester local_server_tester;
435 ASSERT_SUCCESS(
436 s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, AWS_SOCKET_LOCAL, true));
437
438 struct aws_client_bootstrap_options client_bootstrap_options = {
439 .event_loop_group = c_tester.el_group,
440 .host_resolver = c_tester.resolver,
441 };
442 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
443 ASSERT_NOT_NULL(client_bootstrap);
444
445 struct aws_event_loop *pinned_event_loop = aws_event_loop_group_get_next_loop(c_tester.el_group);
446
447 struct aws_socket_channel_bootstrap_options client_channel_options;
448 AWS_ZERO_STRUCT(client_channel_options);
449 client_channel_options.bootstrap = client_bootstrap;
450 client_channel_options.host_name = local_server_tester.endpoint.address;
451 client_channel_options.port = local_server_tester.endpoint.port;
452 client_channel_options.socket_options = &local_server_tester.socket_options;
453 client_channel_options.setup_callback = s_socket_handler_test_client_setup_callback;
454 client_channel_options.shutdown_callback = s_socket_handler_test_client_shutdown_callback;
455 client_channel_options.enable_read_back_pressure = false;
456 client_channel_options.requested_event_loop = pinned_event_loop;
457 client_channel_options.user_data = &client_args;
458
459 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
460
461 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
462 /* wait for both ends to setup */
463 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
464 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_pinned_channel_setup_predicate, &server_args));
465 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
466 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_pinned_channel_setup_predicate, &client_args));
467
468 /* Verify the client channel was placed on the requested event loop */
469 ASSERT_PTR_EQUALS(pinned_event_loop, aws_channel_get_event_loop(client_args.channel));
470
471 ASSERT_SUCCESS(aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS));
472 ASSERT_SUCCESS(aws_channel_shutdown(client_args.channel, AWS_OP_SUCCESS));
473
474 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
475 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
476 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
477 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
478 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
479 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
480 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
481
482 aws_mutex_unlock(&c_tester.mutex);
483
484 /* clean up */
485 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
486
487 aws_client_bootstrap_release(client_bootstrap);
488 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
489
490 return AWS_OP_SUCCESS;
491 }
492
493 AWS_TEST_CASE(socket_pinned_event_loop, s_socket_pinned_event_loop_test)
494
495 static void s_dns_failure_test_client_setup_callback(
496 struct aws_client_bootstrap *bootstrap,
497 int error_code,
498 struct aws_channel *channel,
499 void *user_data) {
500
501 (void)bootstrap;
502 (void)channel;
503
504 struct socket_common_tester *socket_tester = (struct socket_common_tester *)user_data;
505
506 aws_mutex_lock(&socket_tester->mutex);
507
508 socket_tester->setup_error_code = error_code;
509 socket_tester->setup_called = true;
510 AWS_FATAL_ASSERT(aws_event_loop_thread_is_callers_thread(socket_tester->requested_callback_event_loop));
511 AWS_FATAL_ASSERT(channel == NULL);
512
513 aws_mutex_unlock(&socket_tester->mutex);
514 aws_condition_variable_notify_one(&socket_tester->condition_variable);
515 }
516
517 static void s_dns_failure_handler_test_client_shutdown_callback(
518 struct aws_client_bootstrap *bootstrap,
519 int error_code,
520 struct aws_channel *channel,
521 void *user_data) {
522
523 (void)error_code;
524 (void)bootstrap;
525 (void)channel;
526 (void)user_data;
527
528 // Should never be called
529 AWS_FATAL_ASSERT(false);
530 }
531
532 static bool s_dns_failure_channel_setup_predicate(void *user_data) {
533 struct socket_common_tester *socket_tester = (struct socket_common_tester *)user_data;
534 return socket_tester->setup_called;
535 }
536
537 static int s_socket_pinned_event_loop_dns_failure_test(struct aws_allocator *allocator, void *ctx) {
538 (void)ctx;
539
540 s_socket_common_tester_init(allocator, &c_tester);
541
542 struct aws_client_bootstrap_options client_bootstrap_options = {
543 .event_loop_group = c_tester.el_group,
544 .host_resolver = c_tester.resolver,
545 };
546 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
547 ASSERT_NOT_NULL(client_bootstrap);
548
549 struct aws_event_loop *pinned_event_loop = aws_event_loop_group_get_next_loop(c_tester.el_group);
550 c_tester.requested_callback_event_loop = pinned_event_loop;
551
552 struct aws_socket_options socket_options = {
553 .domain = AWS_SOCKET_IPV4,
554 .type = AWS_SOCKET_STREAM,
555 .connect_timeout_ms = 10000,
556 };
557
558 struct aws_socket_channel_bootstrap_options client_channel_options;
559 AWS_ZERO_STRUCT(client_channel_options);
560 client_channel_options.bootstrap = client_bootstrap;
561 client_channel_options.host_name = "notavalid.domain-seriously.uffda";
562 client_channel_options.port = 443;
563 client_channel_options.socket_options = &socket_options;
564 client_channel_options.setup_callback = s_dns_failure_test_client_setup_callback;
565 client_channel_options.shutdown_callback = s_dns_failure_handler_test_client_shutdown_callback;
566 client_channel_options.enable_read_back_pressure = false;
567 client_channel_options.requested_event_loop = pinned_event_loop;
568 client_channel_options.user_data = &c_tester;
569
570 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
571
572 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
573 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
574 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_dns_failure_channel_setup_predicate, &c_tester));
575
576 /* Verify the setup callback failure was on the requested event loop */
577 ASSERT_TRUE(c_tester.setup_error_code != 0);
578
579 aws_mutex_unlock(&c_tester.mutex);
580
581 aws_client_bootstrap_release(client_bootstrap);
582 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
583
584 return AWS_OP_SUCCESS;
585 }
586
587 AWS_TEST_CASE(socket_pinned_event_loop_dns_failure, s_socket_pinned_event_loop_dns_failure_test)
588
589 static int s_socket_echo_and_backpressure_test(struct aws_allocator *allocator, void *ctx) {
590 (void)ctx;
591
592 s_socket_common_tester_init(allocator, &c_tester);
593
594 struct aws_byte_buf msg_from_server = aws_byte_buf_from_c_str("I'm a little teapot.");
595 struct aws_byte_buf msg_from_client = aws_byte_buf_from_c_str("I'm a big teapot");
596
597 uint8_t server_received_message[128] = {0};
598 uint8_t client_received_message[128] = {0};
599
600 struct socket_test_rw_args server_rw_args;
601 ASSERT_SUCCESS(s_rw_args_init(
602 &server_rw_args,
603 &c_tester,
604 aws_byte_buf_from_empty_array(server_received_message, sizeof(server_received_message)),
605 (int)msg_from_client.len));
606
607 struct socket_test_rw_args client_rw_args;
608 ASSERT_SUCCESS(s_rw_args_init(
609 &client_rw_args,
610 &c_tester,
611 aws_byte_buf_from_empty_array(client_received_message, sizeof(client_received_message)),
612 (int)msg_from_server.len));
613 /* make the windows small to make sure back pressure is honored. */
614 static size_t s_client_initial_read_window = 9;
615 static size_t s_server_initial_read_window = 8;
616 struct aws_channel_handler *client_rw_handler = rw_handler_new(
617 allocator,
618 s_socket_test_handle_read,
619 s_socket_test_handle_write,
620 true,
621 s_client_initial_read_window,
622 &client_rw_args);
623 ASSERT_NOT_NULL(client_rw_handler);
624
625 struct aws_channel_handler *server_rw_handler = rw_handler_new(
626 allocator,
627 s_socket_test_handle_read,
628 s_socket_test_handle_write,
629 true,
630 s_server_initial_read_window,
631 &server_rw_args);
632 ASSERT_NOT_NULL(server_rw_handler);
633
634 struct socket_test_args server_args;
635 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
636
637 struct socket_test_args client_args;
638 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
639
640 struct local_server_tester local_server_tester;
641 ASSERT_SUCCESS(
642 s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, AWS_SOCKET_LOCAL, true));
643
644 struct aws_client_bootstrap_options client_bootstrap_options = {
645 .event_loop_group = c_tester.el_group,
646 .host_resolver = c_tester.resolver,
647 };
648 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
649 ASSERT_NOT_NULL(client_bootstrap);
650
651 struct aws_socket_channel_bootstrap_options client_channel_options;
652 AWS_ZERO_STRUCT(client_channel_options);
653 client_channel_options.bootstrap = client_bootstrap;
654 client_channel_options.host_name = local_server_tester.endpoint.address;
655 client_channel_options.port = local_server_tester.endpoint.port;
656 client_channel_options.socket_options = &local_server_tester.socket_options;
657 client_channel_options.setup_callback = s_socket_handler_test_client_setup_callback;
658 client_channel_options.shutdown_callback = s_socket_handler_test_client_shutdown_callback;
659 client_channel_options.user_data = &client_args;
660 client_channel_options.enable_read_back_pressure = true;
661
662 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
663
664 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
665
666 /* wait for both ends to setup */
667 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
668 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &server_args));
669 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
670 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &client_args));
671
672 /* send msg from client to server, and wait for some bytes to be received */
673 rw_handler_write(client_args.rw_handler, client_args.rw_slot, &msg_from_client);
674 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
675 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_read_predicate, &server_rw_args));
676
677 /* send msg from server to client, and wait for some bytes to be received */
678 rw_handler_write(server_args.rw_handler, server_args.rw_slot, &msg_from_server);
679 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
680 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_read_predicate, &client_rw_args));
681
682 /* confirm that the initial read window was respected */
683 server_rw_args.invocation_happened = false;
684 client_rw_args.invocation_happened = false;
685
686 ASSERT_INT_EQUALS(s_client_initial_read_window, client_rw_args.amount_read);
687 ASSERT_INT_EQUALS(s_server_initial_read_window, server_rw_args.amount_read);
688
689 /* increment the read window on both sides and confirm they receive the remainder of their message */
690 rw_handler_trigger_increment_read_window(server_args.rw_handler, server_args.rw_slot, 100);
691 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
692 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_full_read_predicate, &server_rw_args));
693
694 rw_handler_trigger_increment_read_window(client_args.rw_handler, client_args.rw_slot, 100);
695 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
696 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_full_read_predicate, &client_rw_args));
697
698 ASSERT_INT_EQUALS(msg_from_server.len, client_rw_args.amount_read);
699 ASSERT_INT_EQUALS(msg_from_client.len, server_rw_args.amount_read);
700
701 ASSERT_BIN_ARRAYS_EQUALS(
702 msg_from_client.buffer,
703 msg_from_client.len,
704 server_rw_args.received_message.buffer,
705 server_rw_args.received_message.len);
706 ASSERT_BIN_ARRAYS_EQUALS(
707 msg_from_server.buffer,
708 msg_from_server.len,
709 client_rw_args.received_message.buffer,
710 client_rw_args.received_message.len);
711
712 /* only shut down one side, this should cause the other side to shutdown as well.*/
713 ASSERT_SUCCESS(aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS));
714 ASSERT_SUCCESS(aws_channel_shutdown(client_args.channel, AWS_OP_SUCCESS));
715
716 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
717 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
718 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
719 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
720 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
721 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
722 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
723
724 aws_mutex_unlock(&c_tester.mutex);
725
726 /* clean up */
727 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
728
729 aws_client_bootstrap_release(client_bootstrap);
730 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
731
732 return AWS_OP_SUCCESS;
733 }
734
735 AWS_TEST_CASE(socket_handler_echo_and_backpressure, s_socket_echo_and_backpressure_test)
736
737 static int s_socket_data_over_multiple_frames_test(struct aws_allocator *allocator, void *ctx) {
738 (void)ctx;
739
740 s_socket_common_tester_init(allocator, &c_tester);
741
742 // Create a large message that will be split over multiple frames
743 const size_t total_bytes_to_send_from_server = g_aws_channel_max_fragment_size * 1024;
744 struct aws_byte_buf msg_from_server;
745 ASSERT_SUCCESS(aws_byte_buf_init(&msg_from_server, allocator, total_bytes_to_send_from_server));
746
747 srand(0);
748 // Fill the buffer with random printable ASCII characters
749 for (size_t i = 0; i < total_bytes_to_send_from_server; ++i) {
750 char random_char = 32 + (rand() % 95); // Printable ASCII characters range from 32 to 126
751 ASSERT_TRUE(aws_byte_buf_write_u8(&msg_from_server, random_char));
752 }
753
754 struct aws_byte_buf client_received_message;
755 ASSERT_SUCCESS(aws_byte_buf_init(&client_received_message, allocator, total_bytes_to_send_from_server));
756
757 struct socket_test_rw_args server_rw_args;
758 ASSERT_SUCCESS(s_rw_args_init(&server_rw_args, &c_tester, aws_byte_buf_from_empty_array(NULL, 0), 0));
759
760 struct socket_test_rw_args client_rw_args;
761 ASSERT_SUCCESS(s_rw_args_init(&client_rw_args, &c_tester, client_received_message, 0));
762
763 struct aws_channel_handler *client_rw_handler =
764 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &client_rw_args);
765 ASSERT_NOT_NULL(client_rw_handler);
766
767 struct aws_channel_handler *server_rw_handler =
768 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &server_rw_args);
769 ASSERT_NOT_NULL(server_rw_handler);
770
771 struct socket_test_args server_args;
772 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
773
774 struct socket_test_args client_args;
775 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
776
777 struct local_server_tester local_server_tester;
778 ASSERT_SUCCESS(
779 s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, AWS_SOCKET_LOCAL, true));
780
781 struct aws_client_bootstrap_options client_bootstrap_options = {
782 .event_loop_group = c_tester.el_group,
783 .host_resolver = c_tester.resolver,
784 };
785 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
786 ASSERT_NOT_NULL(client_bootstrap);
787
788 struct aws_socket_channel_bootstrap_options client_channel_options;
789 AWS_ZERO_STRUCT(client_channel_options);
790 client_channel_options.bootstrap = client_bootstrap;
791 client_channel_options.host_name = local_server_tester.endpoint.address;
792 client_channel_options.port = local_server_tester.endpoint.port;
793 client_channel_options.socket_options = &local_server_tester.socket_options;
794 client_channel_options.setup_callback = s_socket_handler_test_client_setup_callback;
795 client_channel_options.shutdown_callback = s_socket_handler_test_client_shutdown_callback;
796 client_channel_options.user_data = &client_args;
797 client_channel_options.enable_read_back_pressure = true;
798
799 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
800
801 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
802
803 /* wait for both ends to setup */
804 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
805 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &server_args));
806 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
807 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &client_args));
808
809 /* send msg from server to client, and wait for some bytes to be received */
810 rw_handler_write(server_args.rw_handler, server_args.rw_slot, &msg_from_server);
811 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
812 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_read_predicate, &client_rw_args));
813
814 /* confirm that the initial read window was respected */
815 ASSERT_SUCCESS(client_rw_args.amount_read == 1000);
816
817 client_rw_args.invocation_happened = false;
818 client_rw_args.expected_read = total_bytes_to_send_from_server;
819
820 /* increment the read window on the client side and confirm it receives the remainder of the message */
821 rw_handler_trigger_increment_read_window(
822 client_args.rw_handler, client_args.rw_slot, total_bytes_to_send_from_server);
823 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
824 &c_tester.condition_variable, &c_tester.mutex, s_socket_test_full_read_predicate, &client_rw_args));
825
826 ASSERT_INT_EQUALS(total_bytes_to_send_from_server, client_rw_args.amount_read);
827
828 ASSERT_BIN_ARRAYS_EQUALS(
829 msg_from_server.buffer,
830 msg_from_server.len,
831 client_rw_args.received_message.buffer,
832 client_rw_args.received_message.len);
833
834 /* shut down both sides */
835 ASSERT_SUCCESS(aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS));
836 ASSERT_SUCCESS(aws_channel_shutdown(client_args.channel, AWS_OP_SUCCESS));
837
838 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
839 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
840 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
841 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
842 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
843 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
844 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
845
846 aws_mutex_unlock(&c_tester.mutex);
847
848 /* clean up */
849 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
850
851 aws_client_bootstrap_release(client_bootstrap);
852 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
853 aws_byte_buf_clean_up(&msg_from_server);
854 aws_byte_buf_clean_up(&client_received_message);
855
856 return AWS_OP_SUCCESS;
857 }
858
859 AWS_TEST_CASE(socket_data_over_multiple_frames, s_socket_data_over_multiple_frames_test)
860
861 static int s_socket_close_test(struct aws_allocator *allocator, void *ctx) {
862 (void)ctx;
863
864 s_socket_common_tester_init(allocator, &c_tester);
865
866 uint8_t client_received_message[128];
867 uint8_t server_received_message[128];
868
869 struct socket_test_rw_args server_rw_args;
870 ASSERT_SUCCESS(s_rw_args_init(
871 &server_rw_args,
872 &c_tester,
873 aws_byte_buf_from_empty_array(server_received_message, sizeof(server_received_message)),
874 0));
875
876 struct socket_test_rw_args client_rw_args;
877 ASSERT_SUCCESS(s_rw_args_init(
878 &client_rw_args,
879 &c_tester,
880 aws_byte_buf_from_empty_array(client_received_message, sizeof(client_received_message)),
881 0));
882
883 struct aws_channel_handler *client_rw_handler =
884 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &client_rw_args);
885 ASSERT_NOT_NULL(client_rw_handler);
886
887 struct aws_channel_handler *server_rw_handler =
888 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &server_rw_args);
889 ASSERT_NOT_NULL(server_rw_handler);
890
891 struct socket_test_args server_args;
892 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
893
894 struct socket_test_args client_args;
895 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
896
897 struct local_server_tester local_server_tester;
898 ASSERT_SUCCESS(
899 s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, AWS_SOCKET_LOCAL, false));
900
901 struct aws_client_bootstrap_options client_bootstrap_options = {
902 .event_loop_group = c_tester.el_group,
903 .host_resolver = c_tester.resolver,
904 };
905 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
906 ASSERT_NOT_NULL(client_bootstrap);
907
908 struct aws_socket_channel_bootstrap_options client_channel_options;
909 AWS_ZERO_STRUCT(client_channel_options);
910 client_channel_options.bootstrap = client_bootstrap;
911 client_channel_options.host_name = local_server_tester.endpoint.address;
912 client_channel_options.port = local_server_tester.endpoint.port;
913 client_channel_options.socket_options = &local_server_tester.socket_options;
914 client_channel_options.setup_callback = s_socket_handler_test_client_setup_callback;
915 client_channel_options.shutdown_callback = s_socket_handler_test_client_shutdown_callback;
916 client_channel_options.user_data = &client_args;
917
918 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
919
920 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
921
922 /* wait for both ends to setup */
923 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
924 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &server_args));
925 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
926 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &client_args));
927
928 aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS);
929
930 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
931 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
932 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
933 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
934
935 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, server_args.error_code);
936 ASSERT_TRUE(
937 AWS_IO_SOCKET_CLOSED == client_args.error_code || AWS_IO_SOCKET_NOT_CONNECTED == client_args.error_code);
938 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
939 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
940 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
941
942 aws_mutex_unlock(&c_tester.mutex);
943
944 /* clean up */
945 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
946
947 aws_client_bootstrap_release(client_bootstrap);
948 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
949
950 return AWS_OP_SUCCESS;
951 }
952
953 AWS_TEST_CASE(socket_handler_close, s_socket_close_test)
954
955 /* This is a regression test.
956 * Once upon a time, if the socket-handler received READABLE and HANGUP events simultaneously,
957 * it would read one last time from the socket before closing it. But one read may
958 * not be enough to get all remaining data. The correct thing is to do is
959 * repeatedly read until the read() call itself reports EOF or an error.
960 *
961 * Anyway, this test establishes a connection between server and client.
962 * The server sends a big chunk of data, and closes the socket immediately
963 * after the write completes. The client should still be able to read all the data. */
964 static int s_socket_read_to_eof_after_peer_hangup_test_common(
965 struct aws_allocator *allocator,
966 void *ctx,
967 enum aws_socket_domain socket_domain) {
968
969 (void)ctx;
970 s_socket_common_tester_init(allocator, &c_tester);
971
972 const size_t total_bytes_to_send_from_server = g_aws_channel_max_fragment_size;
973
974 struct aws_byte_buf client_received_message;
975 ASSERT_SUCCESS(aws_byte_buf_init(&client_received_message, allocator, total_bytes_to_send_from_server));
976
977 struct aws_byte_buf msg_from_server;
978 ASSERT_SUCCESS(aws_byte_buf_init(&msg_from_server, allocator, total_bytes_to_send_from_server));
979
980 struct socket_test_rw_args server_rw_args;
981 ASSERT_SUCCESS(s_rw_args_init(&server_rw_args, &c_tester, aws_byte_buf_from_empty_array(NULL, 0), 0));
982
983 struct socket_test_rw_args client_rw_args;
984 ASSERT_SUCCESS(s_rw_args_init(&client_rw_args, &c_tester, client_received_message, 0));
985
986 /* NOTE: client starts with window=0, so we can VERY CAREFULLY control when it reads data from the socket */
987 struct aws_channel_handler *client_rw_handler = rw_handler_new(
988 allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 0 /*window*/, &client_rw_args);
989 ASSERT_NOT_NULL(client_rw_handler);
990
991 struct aws_channel_handler *server_rw_handler =
992 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &server_rw_args);
993 ASSERT_NOT_NULL(server_rw_handler);
994
995 struct socket_test_args server_args;
996 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
997
998 struct socket_test_args client_args;
999 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
1000
1001 struct local_server_tester local_server_tester;
1002 if (s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, socket_domain, false)) {
1003 /* Skip test if server can't bind to address (e.g. Codebuild's ubuntu runners don't allow IPv6) */
1004 if (aws_last_error() == AWS_IO_SOCKET_INVALID_ADDRESS) {
1005 return AWS_OP_SKIP;
1006 } else {
1007 ASSERT_TRUE(false, "s_local_server_tester_init() failed");
1008 }
1009 }
1010
1011 struct aws_client_bootstrap_options client_bootstrap_options = {
1012 .event_loop_group = c_tester.el_group,
1013 .host_resolver = c_tester.resolver,
1014 };
1015 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
1016 ASSERT_NOT_NULL(client_bootstrap);
1017
1018 struct aws_socket_channel_bootstrap_options client_channel_options = {
1019 .bootstrap = client_bootstrap,
1020 .host_name = local_server_tester.endpoint.address,
1021 .port = local_server_tester.endpoint.port,
1022 .socket_options = &local_server_tester.socket_options,
1023 .setup_callback = s_socket_handler_test_client_setup_callback,
1024 .shutdown_callback = s_socket_handler_test_client_shutdown_callback,
1025 .user_data = &client_args,
1026 .enable_read_back_pressure = true,
1027 };
1028
1029 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
1030
1031 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1032
1033 /* wait for both ends to setup */
1034 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1035 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &server_args));
1036 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1037 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &client_args));
1038
1039 /* We want the server to send some data and hang up IMMEDIATELY after,
1040 * before the client has fully read the data. This is tricky to do in a test.
1041 *
1042 * First, have the server send data... */
1043 ASSERT_TRUE(aws_byte_buf_write_u8_n(&msg_from_server, 's', total_bytes_to_send_from_server));
1044 rw_handler_write_with_callback(
1045 server_rw_handler,
1046 server_args.rw_slot,
1047 &msg_from_server,
1048 s_socket_test_handle_on_write_completed,
1049 &server_rw_args);
1050
1051 /* ...now have the client open its read window and receive data in tiny chunks,
1052 * stopping once the server has sent all data, but BEFORE the client has read all data.
1053 * This is possible because the client's OS will buffer a certain amount of
1054 * incoming data, before the client application calls read() on it. */
1055 while (server_rw_args.amount_written < total_bytes_to_send_from_server) {
1056 const size_t client_read_chunk_size = 128;
1057 client_rw_args.expected_read += client_read_chunk_size;
1058 rw_handler_trigger_increment_read_window(client_args.rw_handler, client_args.rw_slot, client_read_chunk_size);
1059 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1060 &c_tester.condition_variable,
1061 &c_tester.mutex,
1062 TIMEOUT,
1063 s_socket_test_full_read_predicate,
1064 &client_rw_args));
1065 }
1066
1067 /* Now close the server's socket.*/
1068 ASSERT_SUCCESS(aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS));
1069 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1070 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
1071
1072 /* Now sleep a moment to 100% guarantee the OS propagates the socket-close event to the client-side. */
1073 aws_mutex_unlock(&c_tester.mutex);
1074 aws_thread_current_sleep(NANOS_PER_SEC / 4);
1075 aws_mutex_lock(&c_tester.mutex);
1076
1077 /* Ensure the client hasn't shut down before reading all the data. */
1078 ASSERT_FALSE(client_args.shutdown_invoked, "Client should read all data before shutting down.");
1079
1080 /* Ensure the client hasn't read all data yet */
1081 ASSERT_TRUE(
1082 client_rw_args.amount_read < total_bytes_to_send_from_server,
1083 "If this fails, then we're not truly reproducing the regression test."
1084 " The server needs to finish sending data, and close the socket,"
1085 " BEFORE the client reads all the data.");
1086
1087 /* Have the client open its window more-than-enough to receive the rest of the data.
1088 * If the client socket closes before all the data is received, then we still have the bug. */
1089 rw_handler_trigger_increment_read_window(
1090 client_args.rw_handler, client_args.rw_slot, total_bytes_to_send_from_server * 3 /*more-than-enough*/);
1091 client_rw_args.expected_read = total_bytes_to_send_from_server;
1092 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1093 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_full_read_predicate, &client_rw_args));
1094
1095 /* Wait for client to shutdown, due to the server having closed the socket */
1096 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1097 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
1098
1099 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
1100 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1101 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
1102
1103 ASSERT_INT_EQUALS(AWS_IO_SOCKET_CLOSED, client_args.error_code);
1104
1105 aws_mutex_unlock(&c_tester.mutex);
1106
1107 /* clean up */
1108 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
1109 aws_byte_buf_clean_up(&client_received_message);
1110 aws_byte_buf_clean_up(&msg_from_server);
1111 aws_client_bootstrap_release(client_bootstrap);
1112 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
1113
1114 return AWS_OP_SUCCESS;
1115 }
1116 static int s_socket_read_to_eof_after_peer_hangup_test(struct aws_allocator *allocator, void *ctx) {
1117 return s_socket_read_to_eof_after_peer_hangup_test_common(allocator, ctx, AWS_SOCKET_LOCAL);
1118 }
1119 AWS_TEST_CASE(socket_handler_read_to_eof_after_peer_hangup, s_socket_read_to_eof_after_peer_hangup_test)
1120
1121 static int s_socket_ipv4_read_to_eof_after_peer_hangup_test(struct aws_allocator *allocator, void *ctx) {
1122 return s_socket_read_to_eof_after_peer_hangup_test_common(allocator, ctx, AWS_SOCKET_IPV4);
1123 }
1124 AWS_TEST_CASE(socket_handler_ipv4_read_to_eof_after_peer_hangup, s_socket_ipv4_read_to_eof_after_peer_hangup_test)
1125
1126 static int s_socket_ipv6_read_to_eof_after_peer_hangup_test(struct aws_allocator *allocator, void *ctx) {
1127 return s_socket_read_to_eof_after_peer_hangup_test_common(allocator, ctx, AWS_SOCKET_IPV6);
1128 }
1129 AWS_TEST_CASE(socket_handler_ipv6_read_to_eof_after_peer_hangup, s_socket_ipv6_read_to_eof_after_peer_hangup_test)
1130
1131 static void s_creation_callback_test_channel_creation_callback(
1132 struct aws_client_bootstrap *bootstrap,
1133 int error_code,
1134 struct aws_channel *channel,
1135 void *user_data) {
1136
1137 (void)bootstrap;
1138 (void)error_code;
1139
1140 struct socket_test_args *setup_test_args = (struct socket_test_args *)user_data;
1141
1142 setup_test_args->creation_callback_invoked = true;
1143
1144 struct aws_crt_statistics_handler *stats_handler = aws_statistics_handler_new_test(bootstrap->allocator);
1145 aws_atomic_store_ptr(&c_tester.stats_handler, stats_handler);
1146
1147 aws_channel_set_statistics_handler(channel, stats_handler);
1148 }
1149
1150 static struct aws_event_loop *s_default_new_event_loop(
1151 struct aws_allocator *allocator,
1152 const struct aws_event_loop_options *options,
1153 void *user_data) {
1154
1155 (void)user_data;
1156 return aws_event_loop_new(allocator, options);
1157 }
1158
1159 static int s_statistic_test_clock_fn(uint64_t *timestamp) {
1160 *timestamp = aws_atomic_load_int(&c_tester.current_time_ns);
1161
1162 return AWS_OP_SUCCESS;
1163 }
1164
1165 static int s_socket_common_tester_statistics_init(
1166 struct aws_allocator *allocator,
1167 struct socket_common_tester *tester) {
1168
1169 aws_io_library_init(allocator);
1170
1171 AWS_ZERO_STRUCT(*tester);
1172
1173 struct aws_event_loop_group_options elg_options = {
1174 .loop_count = 1,
1175 .clock_override = s_statistic_test_clock_fn,
1176 };
1177 tester->el_group = aws_event_loop_group_new_internal(allocator, &elg_options, s_default_new_event_loop, NULL);
1178
1179 struct aws_mutex mutex = AWS_MUTEX_INIT;
1180 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1181 tester->mutex = mutex;
1182 tester->condition_variable = condition_variable;
1183 aws_atomic_store_int(&tester->current_time_ns, 0);
1184 aws_atomic_store_ptr(&tester->stats_handler, NULL);
1185
1186 return AWS_OP_SUCCESS;
1187 }
1188
1189 static bool s_stats_processed_predicate(void *user_data) {
1190 struct aws_crt_statistics_handler *stats_handler = user_data;
1191 struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
1192
1193 return stats_impl->total_bytes_read > 0 && stats_impl->total_bytes_written > 0;
1194 }
1195
1196 static int s_open_channel_statistics_test(struct aws_allocator *allocator, void *ctx) {
1197 (void)ctx;
1198
1199 s_socket_common_tester_statistics_init(allocator, &c_tester);
1200
1201 struct aws_byte_buf msg_from_server = aws_byte_buf_from_c_str("Some bytes");
1202 struct aws_byte_buf msg_from_client = aws_byte_buf_from_c_str("Fresh pressed Washington apples");
1203
1204 uint8_t client_received_message[128];
1205 uint8_t server_received_message[128];
1206
1207 struct socket_test_rw_args server_rw_args;
1208 ASSERT_SUCCESS(s_rw_args_init(
1209 &server_rw_args,
1210 &c_tester,
1211 aws_byte_buf_from_empty_array(server_received_message, sizeof(server_received_message)),
1212 0));
1213
1214 struct socket_test_rw_args client_rw_args;
1215 ASSERT_SUCCESS(s_rw_args_init(
1216 &client_rw_args,
1217 &c_tester,
1218 aws_byte_buf_from_empty_array(client_received_message, sizeof(client_received_message)),
1219 0));
1220
1221 struct aws_channel_handler *client_rw_handler =
1222 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &client_rw_args);
1223 ASSERT_NOT_NULL(client_rw_handler);
1224
1225 struct aws_channel_handler *server_rw_handler =
1226 rw_handler_new(allocator, s_socket_test_handle_read, s_socket_test_handle_write, true, 10000, &server_rw_args);
1227 ASSERT_NOT_NULL(server_rw_handler);
1228
1229 struct socket_test_args server_args;
1230 ASSERT_SUCCESS(s_socket_test_args_init(&server_args, &c_tester, server_rw_handler));
1231
1232 struct socket_test_args client_args;
1233 ASSERT_SUCCESS(s_socket_test_args_init(&client_args, &c_tester, client_rw_handler));
1234
1235 struct local_server_tester local_server_tester;
1236 ASSERT_SUCCESS(
1237 s_local_server_tester_init(allocator, &local_server_tester, &server_args, &c_tester, AWS_SOCKET_LOCAL, false));
1238
1239 struct aws_client_bootstrap_options client_bootstrap_options;
1240 AWS_ZERO_STRUCT(client_bootstrap_options);
1241 client_bootstrap_options.event_loop_group = c_tester.el_group;
1242 client_bootstrap_options.host_resolver = c_tester.resolver;
1243
1244 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options);
1245 ASSERT_NOT_NULL(client_bootstrap);
1246
1247 struct aws_socket_channel_bootstrap_options client_channel_options;
1248 AWS_ZERO_STRUCT(client_channel_options);
1249 client_channel_options.bootstrap = client_bootstrap;
1250 client_channel_options.host_name = local_server_tester.endpoint.address;
1251 client_channel_options.port = local_server_tester.endpoint.port;
1252 client_channel_options.socket_options = &local_server_tester.socket_options;
1253 client_channel_options.creation_callback = s_creation_callback_test_channel_creation_callback;
1254 client_channel_options.setup_callback = s_socket_handler_test_client_setup_callback;
1255 client_channel_options.shutdown_callback = s_socket_handler_test_client_shutdown_callback;
1256 client_channel_options.user_data = &client_args;
1257
1258 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&client_channel_options));
1259
1260 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1261
1262 /* wait for both ends to setup */
1263 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1264 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &server_args));
1265 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1266 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_setup_predicate, &client_args));
1267
1268 ASSERT_TRUE(client_args.creation_callback_invoked);
1269
1270 rw_handler_write(client_args.rw_handler, client_args.rw_slot, &msg_from_client);
1271 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1272 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_read_predicate, &server_rw_args));
1273
1274 rw_handler_write(server_args.rw_handler, server_args.rw_slot, &msg_from_server);
1275 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1276 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_socket_test_read_predicate, &client_rw_args));
1277
1278 uint64_t ms_to_ns = aws_timestamp_convert(1, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
1279
1280 aws_atomic_store_int(&c_tester.current_time_ns, (size_t)ms_to_ns);
1281
1282 struct aws_crt_statistics_handler *stats_handler = aws_atomic_load_ptr(&c_tester.stats_handler);
1283 struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
1284
1285 aws_mutex_lock(&stats_impl->lock);
1286 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1287 &stats_impl->signal, &stats_impl->lock, TIMEOUT, s_stats_processed_predicate, stats_handler));
1288
1289 ASSERT_TRUE(stats_impl->total_bytes_read == msg_from_server.len);
1290 ASSERT_TRUE(stats_impl->total_bytes_written == msg_from_client.len);
1291
1292 aws_mutex_unlock(&stats_impl->lock);
1293
1294 aws_channel_shutdown(server_args.channel, AWS_OP_SUCCESS);
1295
1296 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1297 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &server_args));
1298 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1299 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_channel_shutdown_predicate, &client_args));
1300
1301 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
1302 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1303 &c_tester.condition_variable, &c_tester.mutex, TIMEOUT, s_listener_destroy_predicate, &server_args));
1304
1305 aws_mutex_unlock(&c_tester.mutex);
1306
1307 /* clean up */
1308 ASSERT_SUCCESS(s_local_server_tester_clean_up(&local_server_tester));
1309
1310 aws_client_bootstrap_release(client_bootstrap);
1311 ASSERT_SUCCESS(s_socket_common_tester_clean_up(&c_tester));
1312
1313 return AWS_OP_SUCCESS;
1314 }
1315
1316 AWS_TEST_CASE(open_channel_statistics_test, s_open_channel_statistics_test)