Debian Codegraph @

crt/aws-c-io/tests/socket_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/testing/aws_test_harness.h>
7
8 #include <aws/common/clock.h>
9 #include <aws/common/condition_variable.h>
10 #include <aws/common/string.h>
11 #include <aws/common/task_scheduler.h>
12
13 #include <aws/io/event_loop.h>
14 #include <aws/io/host_resolver.h>
15 #include <aws/io/private/event_loop_impl.h>
16 #include <aws/io/socket.h>
17
18 #ifdef _MSC_VER
19 # pragma warning(disable : 4996) /* strncpy */
20 #endif
21
22 #if USE_VSOCK
23 # include <linux/vm_sockets.h>
24 #endif
25
26 struct local_listener_args {
27 struct aws_socket *incoming;
28 struct aws_mutex *mutex;
29 struct aws_condition_variable *condition_variable;
30 bool incoming_invoked;
31 bool error_invoked;
32 bool shutdown_complete;
33 };
34
35 static void s_local_listener_shutdown_complete(void *user_data) {
36 struct local_listener_args *listener_args = (struct local_listener_args *)user_data;
37
38 aws_mutex_lock(listener_args->mutex);
39 listener_args->shutdown_complete = true;
40 aws_mutex_unlock(listener_args->mutex);
41 aws_condition_variable_notify_one(listener_args->condition_variable);
42 }
43
44 static bool s_local_listener_shutdown_completed_predicate(void *arg) {
45 struct local_listener_args *listener_args = arg;
46
47 return listener_args->shutdown_complete;
48 }
49
50 static bool s_incoming_predicate(void *arg) {
51 struct local_listener_args *listener_args = (struct local_listener_args *)arg;
52
53 return listener_args->incoming_invoked || listener_args->error_invoked;
54 }
55
56 static void s_local_listener_incoming(
57 struct aws_socket *socket,
58 int error_code,
59 struct aws_socket *new_socket,
60 void *user_data) {
61 (void)socket;
62 struct local_listener_args *listener_args = (struct local_listener_args *)user_data;
63 aws_mutex_lock(listener_args->mutex);
64
65 if (!error_code) {
66 listener_args->incoming = new_socket;
67 listener_args->incoming_invoked = true;
68 } else {
69 listener_args->error_invoked = true;
70 }
71 aws_mutex_unlock(listener_args->mutex);
72 aws_condition_variable_notify_one(listener_args->condition_variable);
73 }
74
75 struct local_outgoing_args {
76 bool connect_invoked;
77 bool error_invoked;
78 int last_error;
79 bool shutdown_complete;
80 struct aws_mutex *mutex;
81 struct aws_condition_variable *condition_variable;
82 };
83
84 static bool s_connection_completed_predicate(void *arg) {
85 struct local_outgoing_args *outgoing_args = (struct local_outgoing_args *)arg;
86
87 return outgoing_args->connect_invoked || outgoing_args->error_invoked;
88 }
89
90 static void s_local_outgoing_connection(struct aws_socket *socket, int error_code, void *user_data) {
91 (void)socket;
92 struct local_outgoing_args *outgoing_args = (struct local_outgoing_args *)user_data;
93
94 aws_mutex_lock(outgoing_args->mutex);
95
96 if (!error_code) {
97 outgoing_args->connect_invoked = true;
98
99 } else {
100 outgoing_args->last_error = error_code;
101 outgoing_args->error_invoked = true;
102 }
103
104 aws_mutex_unlock(outgoing_args->mutex);
105 aws_condition_variable_notify_one(outgoing_args->condition_variable);
106 }
107
108 static void s_local_outgoing_connection_shutdown_complete(void *user_data) {
109 struct local_outgoing_args *outgoing_args = (struct local_outgoing_args *)user_data;
110
111 aws_mutex_lock(outgoing_args->mutex);
112 outgoing_args->shutdown_complete = true;
113 aws_mutex_unlock(outgoing_args->mutex);
114 aws_condition_variable_notify_one(outgoing_args->condition_variable);
115 }
116
117 static bool s_outgoing_shutdown_completed_predicate(void *arg) {
118 struct local_outgoing_args *io_args = arg;
119
120 return io_args->shutdown_complete;
121 }
122
123 struct socket_io_args {
124 struct aws_socket *socket;
125 struct aws_byte_cursor *to_write;
126 struct aws_byte_buf *to_read;
127 struct aws_byte_buf *read_data;
128 size_t amount_written;
129 size_t amount_read;
130 int error_code;
131 bool close_completed;
132 bool shutdown_complete;
133 struct aws_mutex *mutex;
134 struct aws_condition_variable condition_variable;
135 };
136
137 static bool s_shutdown_completed_predicate(void *arg) {
138 struct socket_io_args *io_args = arg;
139
140 return io_args->shutdown_complete;
141 }
142
143 static void s_on_written(struct aws_socket *socket, int error_code, size_t amount_written, void *user_data) {
144 (void)socket;
145 struct socket_io_args *write_args = user_data;
146 aws_mutex_lock(write_args->mutex);
147 write_args->error_code = error_code;
148 write_args->amount_written = amount_written;
149 aws_condition_variable_notify_one(&write_args->condition_variable);
150 aws_mutex_unlock(write_args->mutex);
151 }
152
153 static bool s_write_completed_predicate(void *arg) {
154 struct socket_io_args *io_args = arg;
155
156 return io_args->amount_written;
157 }
158
159 static void s_write_task(struct aws_task *task, void *args, enum aws_task_status status) {
160 (void)task;
161 (void)status;
162
163 struct socket_io_args *io_args = args;
164 aws_socket_write(io_args->socket, io_args->to_write, s_on_written, io_args);
165 }
166
167 static void s_read_task(struct aws_task *task, void *args, enum aws_task_status status) {
168 (void)task;
169 (void)status;
170
171 struct socket_io_args *io_args = args;
172
173 aws_mutex_lock(io_args->mutex);
174
175 size_t read = 0;
176
177 while (read < io_args->to_read->len) {
178 size_t data_len = 0;
179
180 if (aws_socket_read(io_args->socket, io_args->read_data, &data_len)) {
181 if (AWS_IO_READ_WOULD_BLOCK == aws_last_error()) {
182 /* we can't just loop here, since the socket may rely on the event-loop for actually getting
183 * the data, so schedule a task to force a context switch and give the socket a chance to catch up. */
184 aws_mutex_unlock(io_args->mutex);
185 aws_event_loop_schedule_task_now(io_args->socket->event_loop, task);
186 return;
187 }
188 break;
189 }
190
191 read += data_len;
192 }
193 io_args->amount_read = read;
194
195 aws_mutex_unlock(io_args->mutex);
196 aws_condition_variable_notify_one(&io_args->condition_variable);
197 }
198
199 static bool s_read_task_predicate(void *arg) {
200 struct socket_io_args *io_args = arg;
201
202 return io_args->amount_read;
203 }
204
205 static void s_on_readable(struct aws_socket *socket, int error_code, void *user_data) {
206 (void)socket;
207 (void)user_data;
208 (void)error_code;
209 }
210
211 static bool s_close_completed_predicate(void *arg) {
212 struct socket_io_args *io_args = (struct socket_io_args *)arg;
213
214 return io_args->close_completed;
215 }
216
217 static void s_socket_close_task(struct aws_task *task, void *args, enum aws_task_status status) {
218 (void)task;
219 (void)status;
220 struct socket_io_args *io_args = args;
221 aws_mutex_lock(io_args->mutex);
222 aws_socket_close(io_args->socket);
223 io_args->close_completed = true;
224 aws_mutex_unlock(io_args->mutex);
225 aws_condition_variable_notify_one(&io_args->condition_variable);
226 }
227
228 static void s_socket_shutdown_complete_fn(void *user_data) {
229 struct socket_io_args *close_args = user_data;
230 aws_mutex_lock(close_args->mutex);
231 close_args->shutdown_complete = true;
232 aws_mutex_unlock(close_args->mutex);
233 aws_condition_variable_notify_one(&close_args->condition_variable);
234 }
235 struct error_test_args {
236 int error_code;
237 struct aws_mutex mutex;
238 struct aws_condition_variable condition_variable;
239 bool shutdown_invoked;
240 };
241
242 static bool s_socket_error_shutdown_predicate(void *args) {
243 struct error_test_args *test_args = (struct error_test_args *)args;
244
245 return test_args->shutdown_invoked;
246 }
247
248 static void s_socket_error_shutdown_complete(void *user_data) {
249 struct error_test_args *test_args = (struct error_test_args *)user_data;
250
251 aws_mutex_lock(&test_args->mutex);
252 test_args->shutdown_invoked = true;
253 aws_mutex_unlock(&test_args->mutex);
254 aws_condition_variable_notify_one(&test_args->condition_variable);
255 }
256
257 /* we have tests that need to check the error handling path, but it's damn near
258 impossible to predictably make sockets fail, the best idea we have is to
259 do something the OS won't allow for the access permissions (like attempt to listen
260 on a port < 1024), but alas, what if you're running the build as root? This disables
261 those tests if the user runs the build as a root user. */
262 static bool s_test_running_as_root(struct aws_allocator *alloc) {
263 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 80};
264 struct aws_socket socket;
265
266 struct aws_socket_options options = {
267 .type = AWS_SOCKET_STREAM,
268 .domain = AWS_SOCKET_IPV4,
269 .keep_alive_interval_sec = 0,
270 .keep_alive_timeout_sec = 0,
271 .connect_timeout_ms = 0,
272 .keepalive = 0,
273 };
274
275 int err = aws_socket_init(&socket, alloc, &options);
276 AWS_FATAL_ASSERT(!err);
277
278 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
279
280 err = aws_socket_bind(&socket, &socket_bind_options);
281 err |= aws_socket_listen(&socket, 1024);
282
283 struct error_test_args args = {
284 .error_code = 0,
285 .mutex = AWS_MUTEX_INIT,
286 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
287 .shutdown_invoked = false,
288 };
289
290 aws_socket_set_cleanup_complete_callback(&socket, s_socket_error_shutdown_complete, &args);
291
292 bool is_root = !err;
293 aws_socket_clean_up(&socket);
294 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
295 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
296 &args.condition_variable, &args.mutex, s_socket_error_shutdown_predicate, &args));
297 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
298
299 return is_root;
300 }
301
302 static int s_test_socket_ex(
303 struct aws_allocator *allocator,
304 struct aws_socket_options *options,
305 struct aws_socket_endpoint *local,
306 struct aws_socket_endpoint *endpoint) {
307
308 aws_io_library_init(allocator);
309
310 struct aws_event_loop_group_options elg_options = {
311 .loop_count = 1,
312 };
313 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
314 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
315
316 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
317
318 struct aws_mutex mutex = AWS_MUTEX_INIT;
319 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
320
321 struct local_listener_args listener_args = {
322 .mutex = &mutex,
323 .condition_variable = &condition_variable,
324 .incoming = NULL,
325 .incoming_invoked = false,
326 .error_invoked = false,
327 };
328
329 /* now test the read and write across the connection. */
330 const char read_data[] = "I'm a little teapot";
331 char write_data[sizeof(read_data)] = {0};
332
333 struct aws_byte_buf read_buffer = aws_byte_buf_from_array((const uint8_t *)read_data, sizeof(read_data));
334 struct aws_byte_buf write_buffer = aws_byte_buf_from_array((const uint8_t *)write_data, sizeof(write_data));
335 write_buffer.len = 0;
336
337 struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(&read_buffer);
338
339 struct socket_io_args io_args = {
340 .socket = NULL,
341 .to_write = &read_cursor,
342 .to_read = &read_buffer,
343 .read_data = &write_buffer,
344 .mutex = &mutex,
345 .amount_read = 0,
346 .amount_written = 0,
347 .error_code = 0,
348 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
349 .close_completed = false,
350 .shutdown_complete = false,
351 };
352
353 struct aws_socket listener;
354 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, options));
355
356 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = endpoint};
357
358 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
359
360 struct aws_socket_endpoint bound_endpoint;
361 ASSERT_SUCCESS(aws_socket_get_bound_address(&listener, &bound_endpoint));
362 ASSERT_INT_EQUALS(endpoint->port, bound_endpoint.port);
363 ASSERT_STR_EQUALS(endpoint->address, bound_endpoint.address);
364
365 // The Apple Network Framework always require a "start listener/start connection"
366 // for setup a server socket
367 if (options->type == AWS_SOCKET_STREAM ||
368 aws_socket_get_default_impl_type() == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) {
369 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
370 struct aws_socket_listener_options listener_options = {
371 .on_accept_result = s_local_listener_incoming, .on_accept_result_user_data = &listener_args};
372 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
373 }
374
375 struct local_outgoing_args outgoing_args = {
376 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
377
378 struct aws_socket outgoing;
379
380 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, options));
381 if (local && (strcmp(local->address, endpoint->address) != 0 || local->port != endpoint->port)) {
382 struct aws_socket_bind_options socket_bind_options_local = {.local_endpoint = local};
383 ASSERT_SUCCESS(aws_socket_bind(&outgoing, &socket_bind_options_local));
384 }
385
386 struct aws_socket_connect_options connect_options = {
387 .remote_endpoint = endpoint,
388 .event_loop = event_loop,
389 .on_connection_result = s_local_outgoing_connection,
390 .user_data = &outgoing_args};
391
392 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
393
394 if (listener.options.type == AWS_SOCKET_STREAM ||
395 aws_socket_get_default_impl_type() == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) {
396 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
397 ASSERT_SUCCESS(
398 aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
399 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
400 }
401 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
402 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
403 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
404 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
405
406 struct aws_socket *server_sock = &listener;
407
408 if (options->type == AWS_SOCKET_STREAM ||
409 aws_socket_get_default_impl_type() == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) {
410 ASSERT_TRUE(listener_args.incoming_invoked);
411 ASSERT_FALSE(listener_args.error_invoked);
412 server_sock = listener_args.incoming;
413 ASSERT_TRUE(outgoing_args.connect_invoked);
414 ASSERT_FALSE(outgoing_args.error_invoked);
415 ASSERT_INT_EQUALS(options->domain, listener_args.incoming->options.domain);
416 ASSERT_INT_EQUALS(options->type, listener_args.incoming->options.type);
417 }
418
419 ASSERT_SUCCESS(aws_socket_assign_to_event_loop(server_sock, event_loop));
420 ASSERT_SUCCESS(aws_socket_subscribe_to_readable_events(server_sock, s_on_readable, NULL));
421 ASSERT_SUCCESS(aws_socket_subscribe_to_readable_events(&outgoing, s_on_readable, NULL));
422
423 io_args.socket = &outgoing;
424
425 struct aws_task write_task = {
426 .fn = s_write_task,
427 .arg = &io_args,
428 };
429
430 aws_event_loop_schedule_task_now(event_loop, &write_task);
431 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
432 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_write_completed_predicate, &io_args);
433 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
434 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
435
436 io_args.socket = server_sock;
437 struct aws_task read_task = {
438 .fn = s_read_task,
439 .arg = &io_args,
440 };
441
442 aws_event_loop_schedule_task_now(event_loop, &read_task);
443 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
444 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_read_task_predicate, &io_args);
445 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
446 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
447 ASSERT_BIN_ARRAYS_EQUALS(read_buffer.buffer, read_buffer.len, write_buffer.buffer, write_buffer.len);
448
449 if (options->type != AWS_SOCKET_DGRAM) {
450 memset((void *)write_data, 0, sizeof(write_data));
451 write_buffer.len = 0;
452
453 io_args.error_code = 0;
454 io_args.amount_read = 0;
455 io_args.amount_written = 0;
456 io_args.socket = server_sock;
457 aws_event_loop_schedule_task_now(event_loop, &write_task);
458 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
459 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_write_completed_predicate, &io_args);
460 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
461 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
462
463 io_args.socket = &outgoing;
464 aws_event_loop_schedule_task_now(event_loop, &read_task);
465 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
466 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_read_task_predicate, &io_args);
467 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
468 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
469 ASSERT_BIN_ARRAYS_EQUALS(read_buffer.buffer, read_buffer.len, write_buffer.buffer, write_buffer.len);
470 }
471
472 struct aws_task close_task = {
473 .fn = s_socket_close_task,
474 .arg = &io_args,
475 };
476
477 if (listener_args.incoming) {
478 io_args.socket = listener_args.incoming;
479 io_args.close_completed = false;
480 io_args.shutdown_complete = false;
481 aws_socket_set_cleanup_complete_callback(listener_args.incoming, s_socket_shutdown_complete_fn, &io_args);
482 aws_event_loop_schedule_task_now(event_loop, &close_task);
483 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
484 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
485 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
486
487 aws_socket_clean_up(listener_args.incoming);
488 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
489 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
490 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
491 aws_mem_release(allocator, listener_args.incoming);
492 }
493
494 io_args.socket = &outgoing;
495 io_args.close_completed = false;
496 io_args.shutdown_complete = false;
497 aws_socket_set_cleanup_complete_callback(&outgoing, s_socket_shutdown_complete_fn, &io_args);
498 aws_event_loop_schedule_task_now(event_loop, &close_task);
499 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
500 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
501 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
502
503 aws_socket_clean_up(&outgoing);
504 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
505 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
506 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
507
508 io_args.socket = &listener;
509 io_args.close_completed = false;
510 io_args.shutdown_complete = false;
511 aws_socket_set_cleanup_complete_callback(&listener, s_socket_shutdown_complete_fn, &io_args);
512 aws_event_loop_schedule_task_now(event_loop, &close_task);
513 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
514 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
515 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
516
517 aws_socket_clean_up(&listener);
518 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
519 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
520 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
521
522 aws_event_loop_group_release(el_group);
523 aws_io_library_clean_up();
524
525 return 0;
526 }
527
528 static int s_test_socket_udp_apple_network_framework(
529 struct aws_allocator *allocator,
530 struct aws_socket_options *options,
531 struct aws_socket_endpoint *endpoint) {
532
533 aws_io_library_init(allocator);
534
535 struct aws_event_loop_group_options elg_options = {
536 .loop_count = 1,
537 };
538 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
539 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
540
541 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
542
543 struct aws_mutex mutex = AWS_MUTEX_INIT;
544 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
545
546 struct local_listener_args listener_args = {
547 .mutex = &mutex,
548 .condition_variable = &condition_variable,
549 .incoming = NULL,
550 .incoming_invoked = false,
551 .error_invoked = false,
552 };
553
554 struct aws_socket listener;
555 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, options));
556
557 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = endpoint};
558 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
559
560 struct aws_socket_endpoint bound_endpoint;
561 ASSERT_SUCCESS(aws_socket_get_bound_address(&listener, &bound_endpoint));
562 ASSERT_INT_EQUALS(endpoint->port, bound_endpoint.port);
563 ASSERT_STR_EQUALS(endpoint->address, bound_endpoint.address);
564
565 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
566 struct aws_socket_listener_options listener_options = {
567 .on_accept_result = s_local_listener_incoming, .on_accept_result_user_data = &listener_args};
568 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
569
570 struct local_outgoing_args outgoing_args = {
571 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
572
573 struct aws_socket outgoing;
574 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, options));
575
576 struct aws_socket_connect_options connect_options = {
577 .remote_endpoint = endpoint,
578 .event_loop = event_loop,
579 .on_connection_result = s_local_outgoing_connection,
580 .user_data = &outgoing_args};
581
582 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
583
584 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
585 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
586 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
587 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
588
589 ASSERT_SUCCESS(aws_socket_subscribe_to_readable_events(&outgoing, s_on_readable, NULL));
590
591 /* now test the read and write across the connection. */
592 const char read_data[] = "I'm a little teapot";
593 char write_data[sizeof(read_data)] = {0};
594
595 struct aws_byte_buf read_buffer = aws_byte_buf_from_array((const uint8_t *)read_data, sizeof(read_data));
596 struct aws_byte_buf write_buffer = aws_byte_buf_from_array((const uint8_t *)write_data, sizeof(write_data));
597 write_buffer.len = 0;
598
599 struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(&read_buffer);
600
601 struct socket_io_args io_args = {
602 .socket = &outgoing,
603 .to_write = &read_cursor,
604 .to_read = &read_buffer,
605 .read_data = &write_buffer,
606 .mutex = &mutex,
607 .amount_read = 0,
608 .amount_written = 0,
609 .error_code = 0,
610 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
611 .close_completed = false,
612 };
613
614 struct aws_task write_task = {
615 .fn = s_write_task,
616 .arg = &io_args,
617 };
618
619 aws_event_loop_schedule_task_now(event_loop, &write_task);
620 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
621 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_write_completed_predicate, &io_args);
622 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
623 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
624
625 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
626 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
627 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
628
629 ASSERT_TRUE(listener_args.incoming_invoked);
630 ASSERT_FALSE(listener_args.error_invoked);
631 struct aws_socket *server_sock = listener_args.incoming;
632 ASSERT_TRUE(outgoing_args.connect_invoked);
633 ASSERT_FALSE(outgoing_args.error_invoked);
634 ASSERT_INT_EQUALS(options->domain, listener_args.incoming->options.domain);
635 ASSERT_INT_EQUALS(options->type, listener_args.incoming->options.type);
636 ASSERT_SUCCESS(aws_socket_assign_to_event_loop(server_sock, event_loop));
637
638 aws_socket_subscribe_to_readable_events(server_sock, s_on_readable, NULL);
639
640 io_args.socket = server_sock;
641 struct aws_task read_task = {
642 .fn = s_read_task,
643 .arg = &io_args,
644 };
645
646 aws_event_loop_schedule_task_now(event_loop, &read_task);
647 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
648 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_read_task_predicate, &io_args);
649 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
650 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
651 ASSERT_BIN_ARRAYS_EQUALS(read_buffer.buffer, read_buffer.len, write_buffer.buffer, write_buffer.len);
652
653 struct aws_task close_task = {
654 .fn = s_socket_close_task,
655 .arg = &io_args,
656 };
657
658 if (listener_args.incoming) {
659 io_args.socket = listener_args.incoming;
660 io_args.close_completed = false;
661 io_args.shutdown_complete = false;
662 aws_socket_set_cleanup_complete_callback(listener_args.incoming, s_socket_shutdown_complete_fn, &io_args);
663 aws_event_loop_schedule_task_now(event_loop, &close_task);
664 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
665 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
666 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
667
668 aws_socket_clean_up(listener_args.incoming);
669 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
670 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
671 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
672
673 aws_mem_release(allocator, listener_args.incoming);
674 }
675
676 io_args.socket = &outgoing;
677 io_args.close_completed = false;
678 io_args.shutdown_complete = false;
679 aws_socket_set_cleanup_complete_callback(&outgoing, s_socket_shutdown_complete_fn, &io_args);
680 aws_event_loop_schedule_task_now(event_loop, &close_task);
681 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
682 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
683 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
684
685 aws_socket_clean_up(&outgoing);
686 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
687 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
688 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
689 io_args.socket = &listener;
690 io_args.close_completed = false;
691 io_args.shutdown_complete = false;
692 aws_socket_set_cleanup_complete_callback(&listener, s_socket_shutdown_complete_fn, &io_args);
693 aws_event_loop_schedule_task_now(event_loop, &close_task);
694 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
695 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
696 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
697
698 aws_socket_clean_up(&listener);
699 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
700 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
701 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
702 aws_event_loop_group_release(el_group);
703 aws_io_library_clean_up();
704
705 return 0;
706 }
707
708 static int s_test_socket_creation(struct aws_allocator *alloc, enum aws_socket_impl_type type, int expected_result) {
709 struct aws_socket socket;
710
711 struct aws_socket_options options = {
712 .type = AWS_SOCKET_STREAM,
713 .domain = AWS_SOCKET_IPV4,
714 .keep_alive_interval_sec = 0,
715 .keep_alive_timeout_sec = 0,
716 .connect_timeout_ms = 0,
717 .keepalive = 0,
718 .impl_type = type,
719 };
720
721 int err = aws_socket_init(&socket, alloc, &options);
722 if (err == AWS_OP_SUCCESS) {
723 aws_socket_clean_up(&socket);
724 ASSERT_INT_EQUALS(err, expected_result);
725 } else { // socket init failed, validate the last error
726 ASSERT_INT_EQUALS(aws_last_error(), expected_result);
727 }
728 return AWS_OP_SUCCESS;
729 }
730
731 static int s_socket_test_posix_expected_result = AWS_ERROR_PLATFORM_NOT_SUPPORTED;
732 static int s_socket_test_winsock_expected_result = AWS_ERROR_PLATFORM_NOT_SUPPORTED;
733
734 static int s_test_socket_posix_creation(struct aws_allocator *allocator, void *ctx) {
735 (void)ctx;
736
737 #if defined(AWS_ENABLE_KQUEUE) || defined(AWS_ENABLE_EPOLL)
738 s_socket_test_posix_expected_result = AWS_OP_SUCCESS;
739 #endif
740 return s_test_socket_creation(allocator, AWS_SOCKET_IMPL_POSIX, s_socket_test_posix_expected_result);
741 }
742
743 AWS_TEST_CASE(socket_posix_creation, s_test_socket_posix_creation)
744
745 static int s_test_socket_winsock_creation(struct aws_allocator *allocator, void *ctx) {
746 (void)ctx;
747
748 #ifdef AWS_ENABLE_IO_COMPLETION_PORTS
749 s_socket_test_winsock_expected_result = AWS_OP_SUCCESS;
750 #endif
751 return s_test_socket_creation(allocator, AWS_SOCKET_IMPL_WINSOCK, s_socket_test_winsock_expected_result);
752 }
753
754 AWS_TEST_CASE(socket_winsock_creation, s_test_socket_winsock_creation)
755
756 static int s_test_socket(
757 struct aws_allocator *allocator,
758 struct aws_socket_options *options,
759 struct aws_socket_endpoint *endpoint) {
760
761 if (aws_socket_get_default_impl_type() == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK &&
762 options->type == AWS_SOCKET_DGRAM)
763 return s_test_socket_udp_apple_network_framework(allocator, options, endpoint);
764 else
765 return s_test_socket_ex(allocator, options, NULL, endpoint);
766 }
767
768 static int s_test_local_socket_communication(struct aws_allocator *allocator, void *ctx) {
769 (void)ctx;
770
771 struct aws_socket_options options;
772 AWS_ZERO_STRUCT(options);
773 options.connect_timeout_ms = 3000;
774 options.type = AWS_SOCKET_STREAM;
775 options.domain = AWS_SOCKET_LOCAL;
776
777 uint64_t timestamp = 0;
778 ASSERT_SUCCESS(aws_sys_clock_get_ticks(&timestamp));
779 struct aws_socket_endpoint endpoint;
780 AWS_ZERO_STRUCT(endpoint);
781 aws_socket_endpoint_init_local_address_for_test(&endpoint);
782
783 return s_test_socket(allocator, &options, &endpoint);
784 }
785
786 AWS_TEST_CASE(local_socket_communication, s_test_local_socket_communication)
787
788 static int s_test_tcp_socket_communication(struct aws_allocator *allocator, void *ctx) {
789 (void)ctx;
790
791 struct aws_socket_options options;
792 AWS_ZERO_STRUCT(options);
793 options.connect_timeout_ms = 3000;
794 options.keepalive = true;
795 options.keep_alive_interval_sec = 1000;
796 options.keep_alive_timeout_sec = 60000;
797 options.type = AWS_SOCKET_STREAM;
798 options.domain = AWS_SOCKET_IPV4;
799
800 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8127};
801
802 return s_test_socket(allocator, &options, &endpoint);
803 }
804
805 AWS_TEST_CASE(tcp_socket_communication, s_test_tcp_socket_communication)
806
807 static int s_test_socket_with_bind_to_interface(struct aws_allocator *allocator, void *ctx) {
808 (void)ctx;
809 struct aws_socket_options options;
810 AWS_ZERO_STRUCT(options);
811 options.connect_timeout_ms = 3000;
812 options.keepalive = true;
813 options.keep_alive_interval_sec = 1000;
814 options.keep_alive_timeout_sec = 60000;
815 options.type = AWS_SOCKET_STREAM;
816 options.domain = AWS_SOCKET_IPV4;
817 #if defined(AWS_OS_APPLE)
818 strncpy(options.network_interface_name, "lo0", AWS_NETWORK_INTERFACE_NAME_MAX);
819 #else
820 strncpy(options.network_interface_name, "lo", AWS_NETWORK_INTERFACE_NAME_MAX);
821 #endif
822 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8128};
823 if (s_test_socket(allocator, &options, &endpoint)) {
824 #if !defined(AWS_OS_LINUX)
825 // On Apple, nw_socket currently not support network_interface_name
826 if (aws_last_error() == AWS_ERROR_PLATFORM_NOT_SUPPORTED) {
827 return AWS_OP_SKIP;
828 }
829 #endif
830 ASSERT_TRUE(false, "s_test_socket() failed");
831 }
832 options.type = AWS_SOCKET_DGRAM;
833 options.domain = AWS_SOCKET_IPV4;
834 ASSERT_SUCCESS(s_test_socket(allocator, &options, &endpoint));
835
836 struct aws_socket_endpoint endpoint_ipv6 = {.address = "::1", .port = 8129};
837 options.type = AWS_SOCKET_STREAM;
838 options.domain = AWS_SOCKET_IPV6;
839 if (s_test_socket(allocator, &options, &endpoint_ipv6)) {
840 /* Skip test if server can't bind to address (e.g. Codebuild's ubuntu runners don't allow IPv6) */
841 if (aws_last_error() == AWS_IO_SOCKET_INVALID_ADDRESS) {
842 return AWS_OP_SKIP;
843 }
844 ASSERT_TRUE(false, "s_test_socket() failed");
845 }
846
847 return AWS_OP_SUCCESS;
848 }
849 AWS_TEST_CASE(test_socket_with_bind_to_interface, s_test_socket_with_bind_to_interface)
850
851 static int s_test_socket_with_bind_to_invalid_interface(struct aws_allocator *allocator, void *ctx) {
852 (void)ctx;
853 struct aws_socket_options options;
854 AWS_ZERO_STRUCT(options);
855 options.connect_timeout_ms = 3000;
856 options.keepalive = true;
857 options.keep_alive_interval_sec = 1000;
858 options.keep_alive_timeout_sec = 60000;
859 options.type = AWS_SOCKET_STREAM;
860 options.domain = AWS_SOCKET_IPV4;
861 strncpy(options.network_interface_name, "invalid", AWS_NETWORK_INTERFACE_NAME_MAX);
862 struct aws_socket outgoing;
863 #if (defined(AWS_OS_APPLE) && !defined(AWS_USE_APPLE_NETWORK_FRAMEWORK)) || defined(AWS_OS_LINUX)
864 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_OPTIONS, aws_socket_init(&outgoing, allocator, &options));
865 #else
866 ASSERT_ERROR(AWS_ERROR_PLATFORM_NOT_SUPPORTED, aws_socket_init(&outgoing, allocator, &options));
867 #endif
868 return AWS_OP_SUCCESS;
869 }
870 AWS_TEST_CASE(test_socket_with_bind_to_invalid_interface, s_test_socket_with_bind_to_invalid_interface)
871
872 static int s_test_is_network_interface_name_valid(struct aws_allocator *allocator, void *ctx) {
873 (void)ctx;
874 (void)allocator;
875
876 ASSERT_FALSE(aws_is_network_interface_name_valid("invalid_name"));
877 #if defined(AWS_OS_LINUX)
878 ASSERT_TRUE(aws_is_network_interface_name_valid("lo"));
879 #elif !defined(AWS_OS_WINDOWS)
880 ASSERT_TRUE(aws_is_network_interface_name_valid("lo0"));
881 #endif
882 return AWS_OP_SUCCESS;
883 }
884 AWS_TEST_CASE(test_is_network_interface_name_valid, s_test_is_network_interface_name_valid)
885
886 #if defined(USE_VSOCK)
887 static int s_test_vsock_loopback_socket_communication(struct aws_allocator *allocator, void *ctx) {
888 /* Without vsock loopback it's difficult to test vsock functionality.
889 * Also note that having this defined does not guarantee that it's available
890 * for use and there's no path to figure out dynamically if it can be used. */
891 # if defined(VMADDR_CID_LOCAL)
892 (void)ctx;
893
894 struct aws_socket_options options;
895 AWS_ZERO_STRUCT(options);
896 options.connect_timeout_ms = 3000;
897 options.type = AWS_SOCKET_STREAM;
898 options.domain = AWS_SOCKET_VSOCK;
899
900 struct aws_socket_endpoint endpoint = {.address = "1" /* VMADDR_CID_LOCAL */, .port = 8127};
901
902 return s_test_socket(allocator, &options, &endpoint);
903 # else
904 return 0;
905 # endif
906 }
907
908 AWS_TEST_CASE(vsock_loopback_socket_communication, s_test_vsock_loopback_socket_communication)
909 #endif
910
911 static int s_test_udp_socket_communication(struct aws_allocator *allocator, void *ctx) {
912 (void)ctx;
913
914 struct aws_socket_options options;
915 AWS_ZERO_STRUCT(options);
916 options.connect_timeout_ms = 3000;
917 options.type = AWS_SOCKET_DGRAM;
918 options.domain = AWS_SOCKET_IPV4;
919
920 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8126};
921
922 return s_test_socket(allocator, &options, &endpoint);
923 }
924
925 AWS_TEST_CASE(udp_socket_communication, s_test_udp_socket_communication)
926
927 static int s_test_udp_bind_connect_communication(struct aws_allocator *allocator, void *ctx) {
928 (void)ctx;
929
930 struct aws_socket_options options;
931 AWS_ZERO_STRUCT(options);
932 options.connect_timeout_ms = 3000;
933 options.type = AWS_SOCKET_DGRAM;
934 options.domain = AWS_SOCKET_IPV4;
935
936 struct aws_socket_endpoint local = {.address = "127.0.0.1", .port = 4242};
937 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8126};
938
939 return s_test_socket_ex(allocator, &options, &local, &endpoint);
940 }
941 AWS_TEST_CASE(udp_bind_connect_communication, s_test_udp_bind_connect_communication)
942
943 struct test_host_callback_data {
944 struct aws_host_address a_address;
945 struct aws_mutex *mutex;
946 bool has_a_address;
947 struct aws_condition_variable condition_variable;
948 bool invoked;
949 };
950
951 static bool s_test_host_resolved_predicate(void *arg) {
952 struct test_host_callback_data *callback_data = arg;
953
954 return callback_data->invoked;
955 }
956
957 static void s_test_host_resolver_shutdown_callback(void *user_data) {
958 struct test_host_callback_data *callback_data = user_data;
959
960 aws_mutex_lock(callback_data->mutex);
961 callback_data->invoked = true;
962 aws_mutex_unlock(callback_data->mutex);
963
964 aws_condition_variable_notify_one(&callback_data->condition_variable);
965 }
966
967 static void s_test_host_resolved_test_callback(
968 struct aws_host_resolver *resolver,
969 const struct aws_string *host_name,
970 int err_code,
971 const struct aws_array_list *host_addresses,
972 void *user_data) {
973
974 (void)resolver;
975 (void)host_name;
976 (void)err_code;
977
978 struct test_host_callback_data *callback_data = user_data;
979
980 aws_mutex_lock(callback_data->mutex);
981 struct aws_host_address *host_address = NULL;
982
983 if (aws_array_list_length(host_addresses) == 1) {
984 aws_array_list_get_at_ptr(host_addresses, (void **)&host_address, 0);
985
986 aws_host_address_copy(host_address, &callback_data->a_address);
987 callback_data->has_a_address = true;
988 }
989
990 callback_data->invoked = true;
991 aws_mutex_unlock(callback_data->mutex);
992 aws_condition_variable_notify_one(&callback_data->condition_variable);
993 }
994
995 static int s_test_connect_timeout(struct aws_allocator *allocator, void *ctx) {
996 (void)ctx;
997
998 aws_io_library_init(allocator);
999
1000 struct aws_event_loop_group_options elg_options = {
1001 .loop_count = 1,
1002 };
1003 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1004 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1005 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1006
1007 struct aws_mutex mutex = AWS_MUTEX_INIT;
1008 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1009
1010 struct aws_socket_options options;
1011 AWS_ZERO_STRUCT(options);
1012 options.connect_timeout_ms = 1000;
1013 options.type = AWS_SOCKET_STREAM;
1014 options.domain = AWS_SOCKET_IPV4;
1015
1016 struct aws_host_resolver_default_options resolver_options = {
1017 .el_group = el_group,
1018 .max_entries = 2,
1019 };
1020 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1021
1022 struct aws_host_resolution_config resolution_config = {
1023 .impl = aws_default_dns_resolve, .impl_data = NULL, .max_ttl = 1};
1024
1025 struct test_host_callback_data host_callback_data = {
1026 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1027 .invoked = false,
1028 .has_a_address = false,
1029 .mutex = &mutex,
1030 };
1031
1032 /* This ec2 instance sits in a VPC that makes sure port 81 is black-holed (no TCP SYN should be received). */
1033 struct aws_string *host_name = aws_string_new_from_c_str(allocator, "ec2-54-158-231-48.compute-1.amazonaws.com");
1034 ASSERT_SUCCESS(aws_host_resolver_resolve_host(
1035 resolver, host_name, s_test_host_resolved_test_callback, &resolution_config, &host_callback_data));
1036
1037 aws_mutex_lock(&mutex);
1038 aws_condition_variable_wait_pred(
1039 &host_callback_data.condition_variable, &mutex, s_test_host_resolved_predicate, &host_callback_data);
1040 aws_mutex_unlock(&mutex);
1041
1042 aws_host_resolver_release(resolver);
1043
1044 ASSERT_TRUE(host_callback_data.has_a_address);
1045
1046 struct aws_socket_endpoint endpoint = {.port = 81};
1047 snprintf(endpoint.address, sizeof(endpoint.address), "%s", aws_string_bytes(host_callback_data.a_address.address));
1048
1049 aws_string_destroy((void *)host_name);
1050 aws_host_address_clean_up(&host_callback_data.a_address);
1051
1052 struct local_outgoing_args outgoing_args = {
1053 .mutex = &mutex,
1054 .condition_variable = &condition_variable,
1055 .connect_invoked = false,
1056 .error_invoked = false,
1057 .shutdown_complete = false,
1058 };
1059
1060 struct aws_socket outgoing;
1061 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1062 aws_socket_set_cleanup_complete_callback(&outgoing, s_local_outgoing_connection_shutdown_complete, &outgoing_args);
1063
1064 struct aws_socket_connect_options connect_options = {
1065 .remote_endpoint = &endpoint,
1066 .event_loop = event_loop,
1067 .on_connection_result = s_local_outgoing_connection,
1068 .user_data = &outgoing_args};
1069
1070 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
1071 aws_mutex_lock(&mutex);
1072 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1073 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
1074 aws_mutex_unlock(&mutex);
1075 ASSERT_INT_EQUALS(AWS_IO_SOCKET_TIMEOUT, outgoing_args.last_error);
1076
1077 aws_socket_set_cleanup_complete_callback(&outgoing, s_local_outgoing_connection_shutdown_complete, &outgoing_args);
1078 aws_socket_clean_up(&outgoing);
1079 aws_mutex_lock(&mutex);
1080 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1081 &condition_variable, &mutex, s_outgoing_shutdown_completed_predicate, &outgoing_args));
1082 aws_mutex_unlock(&mutex);
1083 aws_event_loop_group_release(el_group);
1084
1085 aws_io_library_clean_up();
1086
1087 return 0;
1088 }
1089
1090 AWS_TEST_CASE(connect_timeout, s_test_connect_timeout)
1091
1092 static int s_test_connect_timeout_cancellation(struct aws_allocator *allocator, void *ctx) {
1093 (void)ctx;
1094
1095 aws_io_library_init(allocator);
1096
1097 struct aws_event_loop_group_options elg_options = {
1098 .loop_count = 1,
1099 };
1100 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1101 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1102 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1103
1104 struct aws_mutex mutex = AWS_MUTEX_INIT;
1105 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1106
1107 struct aws_socket_options options;
1108 AWS_ZERO_STRUCT(options);
1109 options.connect_timeout_ms = 10000;
1110 options.type = AWS_SOCKET_STREAM;
1111 options.domain = AWS_SOCKET_IPV4;
1112
1113 struct test_host_callback_data host_callback_data = {
1114 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1115 .invoked = false,
1116 .has_a_address = false,
1117 .mutex = &mutex,
1118 };
1119
1120 struct aws_shutdown_callback_options shutdown_options = {
1121 .shutdown_callback_fn = s_test_host_resolver_shutdown_callback,
1122 .shutdown_callback_user_data = &host_callback_data,
1123 };
1124 shutdown_options.shutdown_callback_fn = s_test_host_resolver_shutdown_callback;
1125
1126 struct aws_host_resolver_default_options resolver_options = {
1127 .el_group = el_group,
1128 .max_entries = 2,
1129 .shutdown_options = &shutdown_options,
1130 };
1131 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1132
1133 struct aws_host_resolution_config resolution_config = {
1134 .impl = aws_default_dns_resolve, .impl_data = NULL, .max_ttl = 1};
1135
1136 /* This ec2 instance sits in a VPC that makes sure port 81 is black-holed (no TCP SYN should be received). */
1137 struct aws_string *host_name = aws_string_new_from_c_str(allocator, "ec2-54-158-231-48.compute-1.amazonaws.com");
1138 ASSERT_SUCCESS(aws_host_resolver_resolve_host(
1139 resolver, host_name, s_test_host_resolved_test_callback, &resolution_config, &host_callback_data));
1140
1141 aws_mutex_lock(&mutex);
1142 aws_condition_variable_wait_pred(
1143 &host_callback_data.condition_variable, &mutex, s_test_host_resolved_predicate, &host_callback_data);
1144 host_callback_data.invoked = false;
1145 aws_mutex_unlock(&mutex);
1146
1147 aws_host_resolver_release(resolver);
1148 /* wait for shutdown callback */
1149 aws_mutex_lock(&mutex);
1150 aws_condition_variable_wait_pred(
1151 &host_callback_data.condition_variable, &mutex, s_test_host_resolved_predicate, &host_callback_data);
1152 aws_mutex_unlock(&mutex);
1153
1154 ASSERT_TRUE(host_callback_data.has_a_address);
1155
1156 struct aws_socket_endpoint endpoint = {.port = 81};
1157 snprintf(endpoint.address, sizeof(endpoint.address), "%s", aws_string_bytes(host_callback_data.a_address.address));
1158
1159 aws_string_destroy((void *)host_name);
1160 aws_host_address_clean_up(&host_callback_data.a_address);
1161
1162 struct local_outgoing_args outgoing_args = {
1163 .mutex = &mutex,
1164 .condition_variable = &condition_variable,
1165 .connect_invoked = false,
1166 .error_invoked = false,
1167 .shutdown_complete = false,
1168 };
1169
1170 struct aws_socket outgoing;
1171 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1172
1173 struct aws_socket_connect_options connect_options = {
1174 .remote_endpoint = &endpoint,
1175 .event_loop = event_loop,
1176 .on_connection_result = s_local_outgoing_connection,
1177 .user_data = &outgoing_args};
1178
1179 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
1180
1181 aws_socket_set_cleanup_complete_callback(&outgoing, s_local_outgoing_connection_shutdown_complete, &outgoing_args);
1182
1183 aws_event_loop_group_release(el_group);
1184 aws_io_library_clean_up();
1185
1186 ASSERT_INT_EQUALS(AWS_IO_EVENT_LOOP_SHUTDOWN, outgoing_args.last_error);
1187
1188 aws_socket_clean_up(&outgoing);
1189 ASSERT_SUCCESS(aws_mutex_lock(outgoing_args.mutex));
1190 aws_condition_variable_wait_pred(
1191 outgoing_args.condition_variable, outgoing_args.mutex, s_outgoing_shutdown_completed_predicate, &outgoing_args);
1192 ASSERT_SUCCESS(aws_mutex_unlock(outgoing_args.mutex));
1193
1194 aws_io_library_clean_up();
1195
1196 return 0;
1197 }
1198
1199 AWS_TEST_CASE(connect_timeout_cancelation, s_test_connect_timeout_cancellation)
1200
1201 static void s_null_sock_connection(struct aws_socket *socket, int error_code, void *user_data) {
1202 (void)socket;
1203 struct error_test_args *error_args = (struct error_test_args *)user_data;
1204
1205 aws_mutex_lock(&error_args->mutex);
1206 if (error_code) {
1207 error_args->error_code = error_code;
1208 }
1209 aws_socket_close(socket);
1210 aws_condition_variable_notify_one(&error_args->condition_variable);
1211 aws_mutex_unlock(&error_args->mutex);
1212 }
1213
1214 static bool s_outgoing_local_error_predicate(void *args) {
1215 struct error_test_args *test_args = (struct error_test_args *)args;
1216
1217 return test_args->error_code != 0;
1218 }
1219
1220 static int s_test_outgoing_local_sock_errors(struct aws_allocator *allocator, void *ctx) {
1221 (void)ctx;
1222 aws_io_library_init(allocator);
1223
1224 struct aws_event_loop_group_options elg_options = {
1225 .loop_count = 1,
1226 };
1227 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1228 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1229
1230 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1231
1232 struct aws_socket_options options;
1233 AWS_ZERO_STRUCT(options);
1234 options.connect_timeout_ms = 1000;
1235 options.type = AWS_SOCKET_STREAM;
1236 options.domain = AWS_SOCKET_LOCAL;
1237
1238 struct aws_socket_endpoint endpoint = {.address = ""};
1239
1240 struct error_test_args args = {
1241 .error_code = 0,
1242 .mutex = AWS_MUTEX_INIT,
1243 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1244 .shutdown_invoked = false,
1245 };
1246
1247 struct aws_socket outgoing;
1248 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1249
1250 aws_socket_set_cleanup_complete_callback(&outgoing, s_socket_error_shutdown_complete, &args);
1251
1252 struct aws_socket_connect_options connect_options = {
1253 .remote_endpoint = &endpoint,
1254 .event_loop = event_loop,
1255 .on_connection_result = s_null_sock_connection,
1256 .user_data = &args};
1257
1258 int socket_connect_result = aws_socket_connect(&outgoing, &connect_options);
1259 // As Apple network framework has an async API design, we would not get the error back on connect
1260 if (aws_socket_get_default_impl_type() != AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) {
1261 ASSERT_FAILS(socket_connect_result);
1262 ASSERT_TRUE(
1263 aws_last_error() == AWS_IO_SOCKET_CONNECTION_REFUSED || aws_last_error() == AWS_ERROR_FILE_INVALID_PATH);
1264 } else {
1265 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
1266 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1267 &args.condition_variable, &args.mutex, s_outgoing_local_error_predicate, &args));
1268 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
1269 ASSERT_TRUE(
1270 args.error_code == AWS_IO_SOCKET_CONNECTION_REFUSED || args.error_code == AWS_ERROR_FILE_INVALID_PATH);
1271 }
1272
1273 aws_socket_clean_up(&outgoing);
1274 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
1275 aws_condition_variable_wait_pred(&args.condition_variable, &args.mutex, s_socket_error_shutdown_predicate, &args);
1276 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
1277 aws_event_loop_group_release(el_group);
1278 aws_io_library_clean_up();
1279
1280 return 0;
1281 }
1282
1283 AWS_TEST_CASE(outgoing_local_sock_errors, s_test_outgoing_local_sock_errors)
1284
1285 static bool s_outgoing_tcp_error_predicate(void *args) {
1286 struct error_test_args *test_args = (struct error_test_args *)args;
1287
1288 return test_args->error_code != 0;
1289 }
1290
1291 static int s_test_outgoing_tcp_sock_error(struct aws_allocator *allocator, void *ctx) {
1292 (void)ctx;
1293 aws_io_library_init(allocator);
1294
1295 struct aws_event_loop_group_options elg_options = {
1296 .loop_count = 1,
1297 };
1298 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1299 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1300
1301 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1302
1303 struct aws_socket_options options;
1304 AWS_ZERO_STRUCT(options);
1305 options.connect_timeout_ms = 50000;
1306 options.type = AWS_SOCKET_STREAM;
1307 options.domain = AWS_SOCKET_IPV4;
1308
1309 struct aws_socket_endpoint endpoint = {
1310 .address = "127.0.0.1",
1311 .port = 1567,
1312 };
1313
1314 struct error_test_args args = {
1315 .error_code = 0,
1316 .mutex = AWS_MUTEX_INIT,
1317 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1318 .shutdown_invoked = false,
1319 };
1320
1321 struct aws_socket outgoing;
1322 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1323 aws_socket_set_cleanup_complete_callback(&outgoing, s_socket_error_shutdown_complete, &args);
1324
1325 struct aws_socket_connect_options connect_options = {
1326 .remote_endpoint = &endpoint,
1327 .event_loop = event_loop,
1328 .on_connection_result = s_null_sock_connection,
1329 .user_data = &args};
1330
1331 int result = aws_socket_connect(&outgoing, &connect_options);
1332 #ifdef __FreeBSD__
1333 /**
1334 * FreeBSD doesn't seem to respect the O_NONBLOCK or SOCK_NONBLOCK flag. It fails immediately when trying to
1335 * connect to a socket which is not listening. This is flaky and works sometimes, but we don't know why. Since this
1336 * test does not aim to test for that, skip it in that case.
1337 */
1338 if (result != AWS_ERROR_SUCCESS) {
1339 ASSERT_INT_EQUALS(AWS_IO_SOCKET_CONNECTION_REFUSED, aws_last_error());
1340 result = AWS_OP_SKIP;
1341 goto cleanup;
1342 }
1343 #endif
1344 ASSERT_SUCCESS(result);
1345 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
1346 ASSERT_SUCCESS(
1347 aws_condition_variable_wait_pred(&args.condition_variable, &args.mutex, s_outgoing_tcp_error_predicate, &args));
1348 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
1349 ASSERT_INT_EQUALS(AWS_IO_SOCKET_CONNECTION_REFUSED, args.error_code);
1350 result = AWS_OP_SUCCESS;
1351
1352 goto cleanup; /* to avoid unused label warning on systems other than FreeBSD */
1353 cleanup:
1354 aws_socket_clean_up(&outgoing);
1355 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
1356 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1357 &args.condition_variable, &args.mutex, s_socket_error_shutdown_predicate, &args));
1358 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
1359 aws_event_loop_group_release(el_group);
1360 aws_io_library_clean_up();
1361
1362 return result;
1363 }
1364 AWS_TEST_CASE(outgoing_tcp_sock_error, s_test_outgoing_tcp_sock_error)
1365
1366 static int s_test_incoming_tcp_sock_errors(struct aws_allocator *allocator, void *ctx) {
1367 (void)ctx;
1368 if (!s_test_running_as_root(allocator)) {
1369 aws_io_library_init(allocator);
1370
1371 struct aws_event_loop_group_options elg_options = {
1372 .loop_count = 1,
1373 };
1374 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1375 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1376
1377 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1378
1379 struct aws_socket_options options;
1380 AWS_ZERO_STRUCT(options);
1381 options.connect_timeout_ms = 1000;
1382 options.type = AWS_SOCKET_STREAM;
1383 options.domain = AWS_SOCKET_IPV4;
1384
1385 struct aws_socket_endpoint endpoint = {
1386 .address = "127.0.0.1",
1387 .port = 80,
1388 };
1389
1390 struct error_test_args args = {
1391 .error_code = 0,
1392 .mutex = AWS_MUTEX_INIT,
1393 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1394 .shutdown_invoked = false,
1395 };
1396
1397 struct aws_socket incoming;
1398 ASSERT_SUCCESS(aws_socket_init(&incoming, allocator, &options));
1399
1400 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1401 ASSERT_ERROR(AWS_ERROR_NO_PERMISSION, aws_socket_bind(&incoming, &socket_bind_options));
1402
1403 aws_socket_set_cleanup_complete_callback(&incoming, s_socket_error_shutdown_complete, &args);
1404
1405 aws_socket_clean_up(&incoming);
1406 ASSERT_SUCCESS(aws_mutex_lock(&args.mutex));
1407 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1408 &args.condition_variable, &args.mutex, s_socket_error_shutdown_predicate, &args));
1409 ASSERT_SUCCESS(aws_mutex_unlock(&args.mutex));
1410
1411 aws_event_loop_group_release(el_group);
1412 aws_io_library_clean_up();
1413 }
1414 return 0;
1415 }
1416
1417 AWS_TEST_CASE(incoming_tcp_sock_errors, s_test_incoming_tcp_sock_errors)
1418
1419 static int s_test_incoming_duplicate_tcp_bind_errors(struct aws_allocator *allocator, void *ctx) {
1420 (void)ctx;
1421
1422 aws_io_library_init(allocator);
1423 struct aws_event_loop_group_options elg_options = {
1424 .loop_count = 1,
1425 };
1426 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1427 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1428
1429 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1430
1431 struct aws_socket_options options;
1432 AWS_ZERO_STRUCT(options);
1433 options.connect_timeout_ms = 1000;
1434 options.type = AWS_SOCKET_STREAM;
1435 options.domain = AWS_SOCKET_IPV4;
1436
1437 struct aws_socket_endpoint endpoint = {
1438 .address = "127.0.0.1",
1439 .port = 30123,
1440 };
1441
1442 struct aws_socket incoming;
1443 ASSERT_SUCCESS(aws_socket_init(&incoming, allocator, &options));
1444
1445 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1446 ASSERT_SUCCESS(aws_socket_bind(&incoming, &socket_bind_options));
1447 ASSERT_SUCCESS(aws_socket_listen(&incoming, 1024));
1448 struct aws_socket duplicate_bind;
1449 ASSERT_SUCCESS(aws_socket_init(&duplicate_bind, allocator, &options));
1450 ASSERT_ERROR(AWS_IO_SOCKET_ADDRESS_IN_USE, aws_socket_bind(&duplicate_bind, &socket_bind_options));
1451
1452 aws_socket_close(&duplicate_bind);
1453 aws_socket_clean_up(&duplicate_bind);
1454 aws_socket_close(&incoming);
1455 aws_socket_clean_up(&incoming);
1456 aws_event_loop_group_release(el_group);
1457 aws_io_library_clean_up();
1458 return 0;
1459 }
1460
1461 AWS_TEST_CASE(incoming_duplicate_tcp_bind_errors, s_test_incoming_duplicate_tcp_bind_errors)
1462
1463 struct nw_socket_bind_args {
1464 struct aws_socket *incoming;
1465 struct aws_socket *listener;
1466 struct aws_mutex *mutex;
1467 struct aws_condition_variable *condition_variable;
1468 bool start_listening;
1469 bool incoming_invoked;
1470 bool error_invoked;
1471 bool shutdown_complete;
1472 };
1473
1474 static void s_bind_args_shutdown_complete(void *user_data) {
1475 struct nw_socket_bind_args *bind_args = (struct nw_socket_bind_args *)user_data;
1476
1477 aws_mutex_lock(bind_args->mutex);
1478 bind_args->shutdown_complete = true;
1479 aws_mutex_unlock(bind_args->mutex);
1480 aws_condition_variable_notify_one(bind_args->condition_variable);
1481 }
1482
1483 static bool s_bind_args_shutdown_completed_predicate(void *arg) {
1484 struct nw_socket_bind_args *bind_args = arg;
1485
1486 return bind_args->shutdown_complete;
1487 }
1488
1489 static bool s_bind_args_start_listening_predicate(void *arg) {
1490 struct nw_socket_bind_args *bind_args = arg;
1491
1492 return bind_args->start_listening;
1493 }
1494
1495 static void s_local_listener_incoming_destroy_listener_bind(
1496 struct aws_socket *socket,
1497 int error_code,
1498 struct aws_socket *new_socket,
1499 void *user_data) {
1500 (void)socket;
1501 struct nw_socket_bind_args *listener_args = (struct nw_socket_bind_args *)user_data;
1502 aws_mutex_lock(listener_args->mutex);
1503
1504 if (!error_code) {
1505 listener_args->incoming = new_socket;
1506 listener_args->incoming_invoked = true;
1507 } else {
1508 listener_args->error_invoked = true;
1509 }
1510 if (new_socket)
1511 aws_socket_clean_up(new_socket);
1512 aws_condition_variable_notify_one(listener_args->condition_variable);
1513 aws_mutex_unlock(listener_args->mutex);
1514 }
1515
1516 static void s_local_listener_start_accept(struct aws_socket *socket, int error_code, void *user_data) {
1517 (void)socket;
1518 struct nw_socket_bind_args *listener_args = (struct nw_socket_bind_args *)user_data;
1519 aws_mutex_lock(listener_args->mutex);
1520
1521 if (!error_code) {
1522 listener_args->start_listening = true;
1523 } else {
1524 listener_args->error_invoked = true;
1525 }
1526 aws_condition_variable_notify_one(listener_args->condition_variable);
1527 aws_mutex_unlock(listener_args->mutex);
1528 }
1529
1530 /* Ensure that binding to port 0 results in OS assigning a port */
1531 static int s_test_bind_on_zero_port(
1532 struct aws_allocator *allocator,
1533 enum aws_socket_type sock_type,
1534 enum aws_socket_domain sock_domain,
1535 const char *address) {
1536
1537 aws_io_library_init(allocator);
1538
1539 struct aws_event_loop_group_options elg_options = {
1540 .loop_count = 1,
1541 };
1542 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1543 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1544
1545 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1546
1547 struct aws_socket_options options;
1548 AWS_ZERO_STRUCT(options);
1549 options.connect_timeout_ms = 1000;
1550 options.type = sock_type;
1551 options.domain = sock_domain;
1552
1553 struct aws_socket_endpoint endpoint = {
1554 .port = 0 /* important: must be 0 for this test */,
1555 };
1556 strncpy(endpoint.address, address, sizeof(endpoint.address));
1557
1558 struct aws_socket incoming;
1559 ASSERT_SUCCESS(aws_socket_init(&incoming, allocator, &options));
1560
1561 /* ensure address query fails if socket isn't bound yet */
1562 struct aws_socket_endpoint local_address1;
1563 ASSERT_FAILS(aws_socket_get_bound_address(&incoming, &local_address1));
1564
1565 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1566 ASSERT_SUCCESS(aws_socket_bind(&incoming, &socket_bind_options));
1567
1568 ASSERT_SUCCESS(aws_socket_get_bound_address(&incoming, &local_address1));
1569
1570 struct aws_mutex mutex = AWS_MUTEX_INIT;
1571 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1572 struct nw_socket_bind_args listener_args = {
1573 .incoming = NULL,
1574 .listener = &incoming,
1575 .incoming_invoked = false,
1576 .error_invoked = false,
1577 .mutex = &mutex,
1578 .condition_variable = &condition_variable,
1579 };
1580
1581 if (aws_socket_get_default_impl_type() == AWS_SOCKET_IMPL_APPLE_NETWORK_FRAMEWORK) {
1582
1583 ASSERT_SUCCESS(aws_socket_listen(&incoming, 1024));
1584 struct aws_socket_listener_options listener_options = {
1585 .on_accept_result = s_local_listener_incoming_destroy_listener_bind,
1586 .on_accept_result_user_data = &listener_args,
1587 .on_accept_start = s_local_listener_start_accept,
1588 .on_accept_start_user_data = &listener_args};
1589
1590 ASSERT_SUCCESS(aws_socket_start_accept(&incoming, event_loop, listener_options));
1591
1592 // Apple Dispatch Queue requires a listener to be ready before it can get the assigned port. We wait until the
1593 // port is back.
1594 ASSERT_SUCCESS(aws_mutex_lock(listener_args.mutex));
1595 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1596 listener_args.condition_variable,
1597 listener_args.mutex,
1598 s_bind_args_start_listening_predicate,
1599 &listener_args));
1600 ASSERT_SUCCESS(aws_mutex_unlock(listener_args.mutex));
1601
1602 ASSERT_SUCCESS(aws_socket_get_bound_address(&incoming, &local_address1));
1603
1604 } else {
1605 if (sock_type != AWS_SOCKET_DGRAM) {
1606 ASSERT_SUCCESS(aws_socket_listen(&incoming, 1024));
1607 }
1608 }
1609 ASSERT_TRUE(local_address1.port > 0);
1610 ASSERT_STR_EQUALS(address, local_address1.address);
1611
1612 /* ensure that querying again gets the same results */
1613 struct aws_socket_endpoint local_address2;
1614 ASSERT_SUCCESS(aws_socket_get_bound_address(&incoming, &local_address2));
1615 ASSERT_INT_EQUALS(local_address1.port, local_address2.port);
1616 ASSERT_STR_EQUALS(local_address1.address, local_address2.address);
1617
1618 aws_socket_set_cleanup_complete_callback(&incoming, s_bind_args_shutdown_complete, &listener_args);
1619 aws_socket_close(&incoming);
1620 aws_socket_clean_up(&incoming);
1621
1622 ASSERT_SUCCESS(aws_mutex_lock(listener_args.mutex));
1623 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1624 listener_args.condition_variable,
1625 listener_args.mutex,
1626 s_bind_args_shutdown_completed_predicate,
1627 &listener_args));
1628 ASSERT_SUCCESS(aws_mutex_unlock(listener_args.mutex));
1629 aws_event_loop_group_release(el_group);
1630 aws_io_library_clean_up();
1631 return 0;
1632 }
1633
1634 static int s_bind_on_zero_port_tcp_ipv4(struct aws_allocator *allocator, void *ctx) {
1635 (void)ctx;
1636 return s_test_bind_on_zero_port(allocator, AWS_SOCKET_STREAM, AWS_SOCKET_IPV4, "127.0.0.1");
1637 }
1638 AWS_TEST_CASE(bind_on_zero_port_tcp_ipv4, s_bind_on_zero_port_tcp_ipv4)
1639
1640 static int s_bind_on_zero_port_udp_ipv4(struct aws_allocator *allocator, void *ctx) {
1641 (void)ctx;
1642 return s_test_bind_on_zero_port(allocator, AWS_SOCKET_DGRAM, AWS_SOCKET_IPV4, "127.0.0.1");
1643 }
1644 AWS_TEST_CASE(bind_on_zero_port_udp_ipv4, s_bind_on_zero_port_udp_ipv4)
1645
1646 static int s_test_incoming_udp_sock_errors(struct aws_allocator *allocator, void *ctx) {
1647 (void)ctx;
1648 if (!s_test_running_as_root(allocator)) {
1649
1650 aws_io_library_init(allocator);
1651
1652 struct aws_event_loop_group_options elg_options = {
1653 .loop_count = 1,
1654 };
1655 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1656 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1657
1658 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1659
1660 struct aws_socket_options options;
1661 AWS_ZERO_STRUCT(options);
1662 options.connect_timeout_ms = 1000;
1663 options.type = AWS_SOCKET_DGRAM;
1664 options.domain = AWS_SOCKET_IPV4;
1665
1666 /* hit a endpoint that will not send me a SYN packet. */
1667 struct aws_socket_endpoint endpoint = {
1668 .address = "127.0",
1669 .port = 80,
1670 };
1671
1672 struct aws_socket incoming;
1673 ASSERT_SUCCESS(aws_socket_init(&incoming, allocator, &options));
1674 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1675 ASSERT_FAILS(aws_socket_bind(&incoming, &socket_bind_options));
1676 int error = aws_last_error();
1677 ASSERT_TRUE(AWS_IO_SOCKET_INVALID_ADDRESS == error || AWS_ERROR_NO_PERMISSION == error);
1678
1679 aws_socket_clean_up(&incoming);
1680 aws_event_loop_group_release(el_group);
1681 aws_io_library_clean_up();
1682 }
1683 return 0;
1684 }
1685
1686 AWS_TEST_CASE(incoming_udp_sock_errors, s_test_incoming_udp_sock_errors)
1687
1688 static void s_on_null_readable_notification(struct aws_socket *socket, int error_code, void *user_data) {
1689 (void)socket;
1690 (void)error_code;
1691 (void)user_data;
1692 }
1693
1694 static int s_test_wrong_thread_read_write_fails(struct aws_allocator *allocator, void *ctx) {
1695 (void)ctx;
1696 aws_io_library_init(allocator);
1697
1698 struct aws_event_loop_group_options elg_options = {
1699 .loop_count = 1,
1700 };
1701 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1702 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1703
1704 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1705
1706 struct aws_socket_options options;
1707 AWS_ZERO_STRUCT(options);
1708 options.connect_timeout_ms = 1000;
1709 options.type = AWS_SOCKET_DGRAM;
1710 options.domain = AWS_SOCKET_IPV4;
1711
1712 struct aws_socket_endpoint endpoint = {
1713 .address = "127.0.0.1",
1714 .port = 50000,
1715 };
1716
1717 struct aws_socket socket;
1718 ASSERT_SUCCESS(aws_socket_init(&socket, allocator, &options));
1719
1720 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1721 aws_socket_bind(&socket, &socket_bind_options);
1722 aws_socket_assign_to_event_loop(&socket, event_loop);
1723 aws_socket_subscribe_to_readable_events(&socket, s_on_null_readable_notification, NULL);
1724 size_t amount_read = 0;
1725 ASSERT_ERROR(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY, aws_socket_read(&socket, NULL, &amount_read));
1726 ASSERT_ERROR(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY, aws_socket_write(&socket, NULL, NULL, NULL));
1727
1728 struct aws_mutex mutex = AWS_MUTEX_INIT;
1729
1730 struct socket_io_args io_args;
1731 io_args.socket = &socket;
1732 io_args.close_completed = false;
1733 io_args.condition_variable = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT;
1734 io_args.mutex = &mutex;
1735
1736 struct aws_task close_task = {
1737 .fn = s_socket_close_task,
1738 .arg = &io_args,
1739 };
1740
1741 aws_event_loop_schedule_task_now(event_loop, &close_task);
1742 aws_mutex_lock(&mutex);
1743 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
1744 aws_mutex_unlock(&mutex);
1745
1746 aws_socket_clean_up(&socket);
1747 aws_event_loop_group_release(el_group);
1748 aws_io_library_clean_up();
1749
1750 return 0;
1751 }
1752
1753 AWS_TEST_CASE(wrong_thread_read_write_fails, s_test_wrong_thread_read_write_fails)
1754
1755 static void s_test_destroy_socket_task(struct aws_task *task, void *arg, enum aws_task_status status) {
1756 (void)task;
1757 (void)status;
1758
1759 struct aws_socket *socket = arg;
1760 aws_socket_clean_up(socket);
1761 }
1762
1763 static int s_cleanup_before_connect_or_timeout_doesnt_explode(struct aws_allocator *allocator, void *ctx) {
1764 (void)ctx;
1765
1766 aws_io_library_init(allocator);
1767
1768 struct aws_event_loop_group_options elg_options = {
1769 .loop_count = 1,
1770 };
1771 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1772 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1773
1774 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1775
1776 struct aws_mutex mutex = AWS_MUTEX_INIT;
1777 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1778
1779 struct aws_socket_options options;
1780 AWS_ZERO_STRUCT(options);
1781 options.connect_timeout_ms = 1000;
1782 options.type = AWS_SOCKET_STREAM;
1783 options.domain = AWS_SOCKET_IPV4;
1784
1785 struct aws_host_resolver_default_options resolver_options = {
1786 .el_group = el_group,
1787 .max_entries = 2,
1788 };
1789 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1790
1791 struct aws_host_resolution_config resolution_config = {
1792 .impl = aws_default_dns_resolve, .impl_data = NULL, .max_ttl = 1};
1793
1794 struct test_host_callback_data host_callback_data = {
1795 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1796 .invoked = false,
1797 .has_a_address = false,
1798 .mutex = &mutex,
1799 };
1800
1801 /* This ec2 instance sits in a VPC that makes sure port 81 is black-holed (no TCP SYN should be received). */
1802 struct aws_string *host_name = aws_string_new_from_c_str(allocator, "ec2-54-158-231-48.compute-1.amazonaws.com");
1803 ASSERT_SUCCESS(aws_host_resolver_resolve_host(
1804 resolver, host_name, s_test_host_resolved_test_callback, &resolution_config, &host_callback_data));
1805
1806 aws_mutex_lock(&mutex);
1807 aws_condition_variable_wait_pred(
1808 &host_callback_data.condition_variable, &mutex, s_test_host_resolved_predicate, &host_callback_data);
1809 aws_mutex_unlock(&mutex);
1810
1811 aws_host_resolver_release(resolver);
1812
1813 ASSERT_TRUE(host_callback_data.has_a_address);
1814
1815 struct aws_socket_endpoint endpoint = {.port = 81};
1816 snprintf(endpoint.address, sizeof(endpoint.address), "%s", aws_string_bytes(host_callback_data.a_address.address));
1817
1818 aws_string_destroy((void *)host_name);
1819 aws_host_address_clean_up(&host_callback_data.a_address);
1820
1821 struct local_outgoing_args outgoing_args = {
1822 .mutex = &mutex,
1823 .condition_variable = &condition_variable,
1824 .connect_invoked = false,
1825 .error_invoked = false,
1826 .shutdown_complete = false,
1827 };
1828
1829 struct error_test_args shutdown_args = {
1830 .mutex = AWS_MUTEX_INIT,
1831 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1832 .shutdown_invoked = false,
1833 };
1834
1835 struct aws_socket outgoing;
1836
1837 struct aws_task destroy_task = {
1838 .fn = s_test_destroy_socket_task,
1839 .arg = &outgoing,
1840 };
1841
1842 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1843
1844 struct aws_socket_connect_options connect_options = {
1845 .remote_endpoint = &endpoint,
1846 .event_loop = event_loop,
1847 .on_connection_result = s_local_outgoing_connection,
1848 .user_data = &outgoing_args};
1849
1850 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
1851 aws_socket_set_cleanup_complete_callback(&outgoing, s_socket_error_shutdown_complete, &shutdown_args);
1852
1853 aws_event_loop_schedule_task_now(event_loop, &destroy_task);
1854 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
1855 ASSERT_ERROR(
1856 AWS_ERROR_COND_VARIABLE_TIMED_OUT,
1857 aws_condition_variable_wait_for(
1858 &condition_variable,
1859 &mutex,
1860 aws_timestamp_convert(options.connect_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL)));
1861 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
1862 ASSERT_FALSE(outgoing_args.connect_invoked);
1863 ASSERT_FALSE(outgoing_args.error_invoked);
1864
1865 aws_event_loop_group_release(el_group);
1866
1867 aws_io_library_clean_up();
1868
1869 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
1870 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1871 &condition_variable, &mutex, s_socket_error_shutdown_predicate, &shutdown_args));
1872 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
1873
1874 return 0;
1875 }
1876
1877 AWS_TEST_CASE(cleanup_before_connect_or_timeout_doesnt_explode, s_cleanup_before_connect_or_timeout_doesnt_explode)
1878
1879 static void s_local_listener_incoming_destroy_listener(
1880 struct aws_socket *socket,
1881 int error_code,
1882 struct aws_socket *new_socket,
1883 void *user_data) {
1884 (void)socket;
1885 struct local_listener_args *listener_args = (struct local_listener_args *)user_data;
1886 aws_mutex_lock(listener_args->mutex);
1887
1888 if (!error_code) {
1889 listener_args->incoming = new_socket;
1890 listener_args->incoming_invoked = true;
1891 } else {
1892 listener_args->error_invoked = true;
1893 }
1894
1895 if (socket) {
1896 aws_socket_clean_up(socket);
1897 }
1898 aws_condition_variable_notify_one(listener_args->condition_variable);
1899 aws_mutex_unlock(listener_args->mutex);
1900 }
1901
1902 static int s_cleanup_in_accept_doesnt_explode(struct aws_allocator *allocator, void *ctx) {
1903 (void)ctx;
1904
1905 aws_io_library_init(allocator);
1906
1907 struct aws_event_loop_group_options elg_options = {
1908 .loop_count = 1,
1909 };
1910 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
1911 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
1912
1913 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
1914
1915 struct aws_mutex mutex = AWS_MUTEX_INIT;
1916 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
1917
1918 struct local_listener_args listener_args = {
1919 .mutex = &mutex,
1920 .condition_variable = &condition_variable,
1921 .incoming = NULL,
1922 .incoming_invoked = false,
1923 .error_invoked = false,
1924 .shutdown_complete = false,
1925 };
1926
1927 struct aws_socket_options options;
1928 AWS_ZERO_STRUCT(options);
1929 options.connect_timeout_ms = 3000;
1930 options.keepalive = true;
1931 options.keep_alive_interval_sec = 1000;
1932 options.keep_alive_timeout_sec = 60000;
1933 options.type = AWS_SOCKET_STREAM;
1934 options.domain = AWS_SOCKET_IPV4;
1935
1936 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8129};
1937
1938 struct aws_socket listener;
1939 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
1940
1941 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
1942 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
1943
1944 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
1945 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
1946 aws_socket_set_cleanup_complete_callback(&listener, s_local_listener_shutdown_complete, &listener_args);
1947 #endif
1948 struct aws_socket_listener_options listener_options = {
1949 .on_accept_result = s_local_listener_incoming_destroy_listener, .on_accept_result_user_data = &listener_args};
1950
1951 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
1952
1953 struct local_outgoing_args outgoing_args = {
1954 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
1955
1956 struct aws_socket outgoing;
1957 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
1958
1959 struct aws_socket_connect_options connect_options = {
1960 .remote_endpoint = &endpoint,
1961 .event_loop = event_loop,
1962 .on_connection_result = s_local_outgoing_connection,
1963 .user_data = &outgoing_args};
1964
1965 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
1966
1967 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
1968 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
1969 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1970 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
1971 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
1972
1973 ASSERT_TRUE(listener_args.incoming_invoked);
1974 ASSERT_FALSE(listener_args.error_invoked);
1975 ASSERT_TRUE(outgoing_args.connect_invoked);
1976 ASSERT_FALSE(outgoing_args.error_invoked);
1977 ASSERT_INT_EQUALS(options.domain, listener_args.incoming->options.domain);
1978 ASSERT_INT_EQUALS(options.type, listener_args.incoming->options.type);
1979
1980 struct socket_io_args io_args = {
1981 .socket = &outgoing,
1982 .to_write = NULL,
1983 .to_read = NULL,
1984 .read_data = NULL,
1985 .mutex = &mutex,
1986 .amount_read = 0,
1987 .amount_written = 0,
1988 .error_code = 0,
1989 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
1990 .close_completed = false,
1991 .shutdown_complete = false,
1992 };
1993
1994 struct aws_task close_task = {
1995 .fn = s_socket_close_task,
1996 .arg = &io_args,
1997 };
1998
1999 if (listener_args.incoming) {
2000 io_args.socket = listener_args.incoming;
2001 io_args.close_completed = false;
2002
2003 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2004 aws_socket_set_cleanup_complete_callback(io_args.socket, s_socket_shutdown_complete_fn, &io_args);
2005 io_args.shutdown_complete = false;
2006 #endif
2007 aws_socket_assign_to_event_loop(io_args.socket, event_loop);
2008 aws_event_loop_schedule_task_now(event_loop, &close_task);
2009 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2010 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
2011 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2012
2013 aws_socket_clean_up(io_args.socket);
2014 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2015 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2016 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
2017 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2018 #endif
2019 aws_mem_release(allocator, listener_args.incoming);
2020 }
2021
2022 io_args.socket = &outgoing;
2023 aws_socket_set_cleanup_complete_callback(io_args.socket, s_socket_shutdown_complete_fn, &io_args);
2024 io_args.close_completed = false;
2025 io_args.shutdown_complete = false;
2026 aws_event_loop_schedule_task_now(event_loop, &close_task);
2027 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2028 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
2029 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2030 aws_socket_clean_up(&outgoing);
2031 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2032 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
2033 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2034
2035 aws_event_loop_group_release(el_group);
2036 aws_io_library_clean_up();
2037
2038 return 0;
2039 }
2040 AWS_TEST_CASE(cleanup_in_accept_doesnt_explode, s_cleanup_in_accept_doesnt_explode)
2041
2042 static void s_on_written_destroy(struct aws_socket *socket, int error_code, size_t amount_written, void *user_data) {
2043 (void)socket;
2044 struct socket_io_args *write_args = user_data;
2045 aws_mutex_lock(write_args->mutex);
2046 write_args->error_code = error_code;
2047 write_args->amount_written = amount_written;
2048 if (socket) {
2049 aws_socket_clean_up(socket);
2050 }
2051 aws_condition_variable_notify_one(&write_args->condition_variable);
2052 aws_mutex_unlock(write_args->mutex);
2053 }
2054
2055 static bool s_write_completed_predicate_destroy(void *arg) {
2056 struct socket_io_args *io_args = arg;
2057
2058 return io_args->amount_written || io_args->error_code;
2059 }
2060
2061 static void s_write_task_destroy(struct aws_task *task, void *args, enum aws_task_status status) {
2062 (void)task;
2063 (void)status;
2064
2065 struct socket_io_args *io_args = args;
2066 aws_socket_write(io_args->socket, io_args->to_write, s_on_written_destroy, io_args);
2067 }
2068
2069 static int s_cleanup_in_write_cb_doesnt_explode(struct aws_allocator *allocator, void *ctx) {
2070 (void)ctx;
2071
2072 aws_io_library_init(allocator);
2073
2074 struct aws_event_loop_group_options elg_options = {
2075 .loop_count = 1,
2076 };
2077 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
2078 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
2079
2080 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
2081
2082 struct aws_mutex mutex = AWS_MUTEX_INIT;
2083 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
2084
2085 struct local_listener_args listener_args = {
2086 .mutex = &mutex,
2087 .condition_variable = &condition_variable,
2088 .incoming = NULL,
2089 .incoming_invoked = false,
2090 .error_invoked = false,
2091 .shutdown_complete = false,
2092 };
2093
2094 struct aws_socket_options options;
2095 AWS_ZERO_STRUCT(options);
2096 options.connect_timeout_ms = 3000;
2097 options.keepalive = true;
2098 options.keep_alive_interval_sec = 1000;
2099 options.keep_alive_timeout_sec = 60000;
2100 options.type = AWS_SOCKET_STREAM;
2101 options.domain = AWS_SOCKET_IPV4;
2102
2103 struct aws_socket_endpoint endpoint = {.address = "127.0.0.1", .port = 8130};
2104
2105 struct aws_socket listener;
2106 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
2107
2108 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
2109 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
2110 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
2111 struct aws_socket_listener_options listener_options = {
2112 .on_accept_result = s_local_listener_incoming, .on_accept_result_user_data = &listener_args};
2113 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
2114
2115 struct local_outgoing_args outgoing_args = {
2116 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
2117
2118 struct aws_socket outgoing;
2119 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
2120
2121 struct aws_socket_connect_options connect_options = {
2122 .remote_endpoint = &endpoint,
2123 .event_loop = event_loop,
2124 .on_connection_result = s_local_outgoing_connection,
2125 .user_data = &outgoing_args};
2126
2127 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
2128
2129 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2130 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
2131 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2132 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
2133 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2134
2135 ASSERT_TRUE(listener_args.incoming_invoked);
2136 ASSERT_FALSE(listener_args.error_invoked);
2137 struct aws_socket *server_sock = listener_args.incoming;
2138 ASSERT_TRUE(outgoing_args.connect_invoked);
2139 ASSERT_FALSE(outgoing_args.error_invoked);
2140 ASSERT_INT_EQUALS(options.domain, listener_args.incoming->options.domain);
2141 ASSERT_INT_EQUALS(options.type, listener_args.incoming->options.type);
2142
2143 ASSERT_SUCCESS(aws_socket_assign_to_event_loop(server_sock, event_loop));
2144 aws_socket_subscribe_to_readable_events(server_sock, s_on_readable, NULL);
2145 aws_socket_subscribe_to_readable_events(&outgoing, s_on_readable, NULL);
2146
2147 /* now test the read and write across the connection. */
2148 const char read_data[] = "I'm a little teapot";
2149 char write_data[sizeof(read_data)] = {0};
2150
2151 struct aws_byte_buf read_buffer = aws_byte_buf_from_array((const uint8_t *)read_data, sizeof(read_data));
2152 struct aws_byte_buf write_buffer = aws_byte_buf_from_array((const uint8_t *)write_data, sizeof(write_data));
2153 write_buffer.len = 0;
2154
2155 struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(&read_buffer);
2156
2157 struct socket_io_args io_args = {
2158 .socket = &outgoing,
2159 .to_write = &read_cursor,
2160 .to_read = &read_buffer,
2161 .read_data = &write_buffer,
2162 .mutex = &mutex,
2163 .amount_read = 0,
2164 .amount_written = 0,
2165 .error_code = 0,
2166 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
2167 .close_completed = false,
2168 .shutdown_complete = false,
2169 };
2170
2171 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2172 aws_socket_set_cleanup_complete_callback(io_args.socket, s_socket_shutdown_complete_fn, &io_args);
2173 #endif
2174
2175 struct aws_task write_task = {
2176 .fn = s_write_task_destroy,
2177 .arg = &io_args,
2178 };
2179
2180 aws_event_loop_schedule_task_now(event_loop, &write_task);
2181 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2182 aws_condition_variable_wait_pred(
2183 &io_args.condition_variable, &mutex, s_write_completed_predicate_destroy, &io_args);
2184 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2185 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
2186
2187 memset((void *)write_data, 0, sizeof(write_data));
2188 write_buffer.len = 0;
2189
2190 io_args.error_code = 0;
2191 io_args.amount_written = 0;
2192 io_args.socket = server_sock;
2193 io_args.shutdown_complete = false;
2194 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2195 aws_socket_set_cleanup_complete_callback(io_args.socket, s_socket_shutdown_complete_fn, &io_args);
2196 #endif
2197 aws_event_loop_schedule_task_now(event_loop, &write_task);
2198 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2199 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_write_completed_predicate, &io_args);
2200 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2201 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_shutdown_completed_predicate, &io_args);
2202 #endif
2203 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2204 ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);
2205 aws_mem_release(allocator, server_sock);
2206
2207 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2208 aws_socket_set_cleanup_complete_callback(&listener, s_local_listener_shutdown_complete, &listener_args);
2209 #endif
2210 aws_socket_clean_up(&listener);
2211 #ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
2212 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2213 aws_condition_variable_wait_pred(
2214 listener_args.condition_variable, &mutex, s_local_listener_shutdown_completed_predicate, &listener_args);
2215 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2216 #endif
2217
2218 aws_event_loop_group_release(el_group);
2219 aws_io_library_clean_up();
2220
2221 return 0;
2222 }
2223 AWS_TEST_CASE(cleanup_in_write_cb_doesnt_explode, s_cleanup_in_write_cb_doesnt_explode)
2224
2225 /* stuff for the sock_write_cb_is_async test */
2226 enum async_role {
2227 ASYNC_ROLE_A_CALLBACK_WRITES_D,
2228 ASYNC_ROLE_B_CALLBACK_CLEANS_UP_SOCKET,
2229 ASYNC_ROLE_C_IS_LAST_FROM_INITIAL_BATCH_OF_WRITES,
2230 ASYNC_ROLE_D_GOT_WRITTEN_VIA_CALLBACK,
2231 ASYNC_ROLE_COUNT
2232 };
2233
2234 static struct async_test_args {
2235 struct aws_allocator *allocator;
2236 struct aws_event_loop *event_loop;
2237 struct aws_socket *write_socket;
2238 struct aws_socket *read_socket;
2239 bool currently_writing;
2240 enum async_role next_expected_callback;
2241 int read_error;
2242
2243 struct aws_mutex *mutex;
2244 struct aws_condition_variable *condition_variable;
2245 bool write_tasks_complete;
2246 bool read_tasks_complete;
2247 } g_async_tester;
2248
2249 static bool s_async_tasks_complete_pred(void *arg) {
2250 (void)arg;
2251 return g_async_tester.write_tasks_complete && g_async_tester.read_tasks_complete;
2252 }
2253
2254 /* read until socket gets hung up on */
2255 static void s_async_read_task(struct aws_task *task, void *args, enum aws_task_status status) {
2256 (void)args;
2257 (void)status;
2258 uint8_t buf_storage[100];
2259 AWS_ZERO_ARRAY(buf_storage);
2260
2261 struct aws_byte_buf buf = aws_byte_buf_from_array(buf_storage, sizeof(buf_storage));
2262 while (true) {
2263 size_t amount_read = 0;
2264 buf.len = 0;
2265 if (aws_socket_read(g_async_tester.read_socket, &buf, &amount_read)) {
2266 /* reschedule task to try reading more later */
2267 /*
2268 * For Apple Network Framework (dispatch queue), the read error would not directly returned from
2269 * aws_socket_read, but from the callback, therefore, we validate the g_async_tester.read_error
2270 * returned from the callback
2271 */
2272 if (!g_async_tester.read_error && AWS_IO_READ_WOULD_BLOCK == aws_last_error()) {
2273 aws_event_loop_schedule_task_now(g_async_tester.event_loop, task);
2274 break;
2275 }
2276
2277 /* other end must have hung up. clean up and signal completion */
2278 aws_socket_clean_up(g_async_tester.read_socket);
2279 aws_mem_release(g_async_tester.allocator, g_async_tester.read_socket);
2280
2281 aws_mutex_lock(g_async_tester.mutex);
2282 g_async_tester.read_tasks_complete = true;
2283 aws_mutex_unlock(g_async_tester.mutex);
2284 aws_condition_variable_notify_all(g_async_tester.condition_variable);
2285 break;
2286 }
2287 }
2288 }
2289
2290 static void s_async_write_completion(struct aws_socket *socket, int error_code, size_t bytes_written, void *user_data) {
2291 enum async_role role = *(enum async_role *)user_data;
2292 aws_mem_release(g_async_tester.allocator, user_data);
2293
2294 /* ensure callback is not firing synchronously from within aws_socket_write() */
2295 AWS_FATAL_ASSERT(!g_async_tester.currently_writing);
2296
2297 /* ensure callbacks arrive in order */
2298 AWS_FATAL_ASSERT(g_async_tester.next_expected_callback == role);
2299 g_async_tester.next_expected_callback++;
2300
2301 switch (role) {
2302 case ASYNC_ROLE_A_CALLBACK_WRITES_D: {
2303 AWS_FATAL_ASSERT(0 == error_code);
2304 AWS_FATAL_ASSERT(1 == bytes_written);
2305 g_async_tester.currently_writing = true;
2306 struct aws_byte_cursor data = aws_byte_cursor_from_c_str("D");
2307 enum async_role *d_role = aws_mem_acquire(g_async_tester.allocator, sizeof(enum async_role));
2308 *d_role = ASYNC_ROLE_D_GOT_WRITTEN_VIA_CALLBACK;
2309 AWS_FATAL_ASSERT(0 == aws_socket_write(socket, &data, s_async_write_completion, d_role));
2310 g_async_tester.currently_writing = false;
2311 break;
2312 }
2313 case ASYNC_ROLE_B_CALLBACK_CLEANS_UP_SOCKET:
2314 AWS_FATAL_ASSERT(0 == error_code);
2315 AWS_FATAL_ASSERT(1 == bytes_written);
2316 aws_socket_clean_up(socket);
2317 break;
2318 case ASYNC_ROLE_C_IS_LAST_FROM_INITIAL_BATCH_OF_WRITES:
2319 /* C might succeed or fail (since socket killed after B completes), either is valid */
2320 break;
2321 case ASYNC_ROLE_D_GOT_WRITTEN_VIA_CALLBACK:
2322 /* write tasks complete! */
2323 aws_mutex_lock(g_async_tester.mutex);
2324 g_async_tester.write_tasks_complete = true;
2325 aws_mutex_unlock(g_async_tester.mutex);
2326 aws_condition_variable_notify_all(g_async_tester.condition_variable);
2327 break;
2328 default:
2329 AWS_FATAL_ASSERT(0);
2330 }
2331 }
2332
2333 static void s_async_write_task(struct aws_task *task, void *args, enum aws_task_status status) {
2334 (void)task;
2335 (void)args;
2336 (void)status;
2337
2338 g_async_tester.currently_writing = true;
2339
2340 struct aws_byte_cursor data = aws_byte_cursor_from_c_str("A");
2341 enum async_role *role = aws_mem_acquire(g_async_tester.allocator, sizeof(enum async_role));
2342 *role = ASYNC_ROLE_A_CALLBACK_WRITES_D;
2343 AWS_FATAL_ASSERT(0 == aws_socket_write(g_async_tester.write_socket, &data, s_async_write_completion, role));
2344
2345 data = aws_byte_cursor_from_c_str("B");
2346 role = aws_mem_acquire(g_async_tester.allocator, sizeof(enum async_role));
2347 *role = ASYNC_ROLE_B_CALLBACK_CLEANS_UP_SOCKET;
2348 AWS_FATAL_ASSERT(0 == aws_socket_write(g_async_tester.write_socket, &data, s_async_write_completion, role));
2349
2350 data = aws_byte_cursor_from_c_str("C");
2351 role = aws_mem_acquire(g_async_tester.allocator, sizeof(enum async_role));
2352 *role = ASYNC_ROLE_C_IS_LAST_FROM_INITIAL_BATCH_OF_WRITES;
2353 AWS_FATAL_ASSERT(0 == aws_socket_write(g_async_tester.write_socket, &data, s_async_write_completion, role));
2354
2355 g_async_tester.currently_writing = false;
2356 }
2357
2358 static void s_on_readable_return(struct aws_socket *socket, int error_code, void *user_data) {
2359 (void)socket;
2360 (void)error_code;
2361 struct async_test_args *async_tester = user_data;
2362 if (error_code) {
2363 async_tester->read_error = error_code;
2364 }
2365 }
2366
2367 /**
2368 * aws_socket_write()'s completion callback MUST fire asynchronously.
2369 * Otherwise, we can get multiple write() calls in the same callstack, which
2370 * leads to esoteric bugs (https://github.com/aws/aws-iot-device-sdk-cpp-v2/issues/194).
2371 */
2372 static int s_sock_write_cb_is_async(struct aws_allocator *allocator, void *ctx) {
2373 (void)ctx;
2374
2375 /* set up server (read) and client (write) sockets */
2376 aws_io_library_init(allocator);
2377
2378 struct aws_event_loop_group_options elg_options = {
2379 .loop_count = 1,
2380 };
2381 struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options);
2382 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(el_group);
2383
2384 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
2385
2386 struct aws_mutex mutex = AWS_MUTEX_INIT;
2387 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
2388
2389 struct local_listener_args listener_args = {
2390 .mutex = &mutex,
2391 .condition_variable = &condition_variable,
2392 .incoming = NULL,
2393 .incoming_invoked = false,
2394 .error_invoked = false,
2395 .shutdown_complete = false,
2396 };
2397
2398 struct aws_socket_options options;
2399 AWS_ZERO_STRUCT(options);
2400 options.connect_timeout_ms = 3000;
2401 options.keepalive = true;
2402 options.keep_alive_interval_sec = 1000;
2403 options.keep_alive_timeout_sec = 60000;
2404 options.type = AWS_SOCKET_STREAM;
2405 options.domain = AWS_SOCKET_LOCAL;
2406 struct aws_socket_endpoint endpoint;
2407 AWS_ZERO_STRUCT(endpoint);
2408 aws_socket_endpoint_init_local_address_for_test(&endpoint);
2409
2410 struct aws_socket listener;
2411 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
2412
2413 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
2414 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
2415 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
2416 struct aws_socket_listener_options listener_options = {
2417 .on_accept_result = s_local_listener_incoming, .on_accept_result_user_data = &listener_args};
2418 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
2419
2420 struct local_outgoing_args outgoing_args = {
2421 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
2422
2423 struct aws_socket outgoing;
2424 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
2425
2426 struct aws_socket_connect_options connect_options = {
2427 .remote_endpoint = &endpoint,
2428 .event_loop = event_loop,
2429 .on_connection_result = s_local_outgoing_connection,
2430 .user_data = &outgoing_args};
2431
2432 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
2433
2434 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2435 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
2436 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2437 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
2438 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2439
2440 ASSERT_TRUE(listener_args.incoming_invoked);
2441 ASSERT_FALSE(listener_args.error_invoked);
2442 struct aws_socket *server_sock = listener_args.incoming;
2443 ASSERT_TRUE(outgoing_args.connect_invoked);
2444 ASSERT_FALSE(outgoing_args.error_invoked);
2445 ASSERT_INT_EQUALS(options.domain, listener_args.incoming->options.domain);
2446 ASSERT_INT_EQUALS(options.type, listener_args.incoming->options.type);
2447
2448 ASSERT_SUCCESS(aws_socket_assign_to_event_loop(server_sock, event_loop));
2449 aws_socket_subscribe_to_readable_events(server_sock, s_on_readable_return, &g_async_tester);
2450 aws_socket_subscribe_to_readable_events(&outgoing, s_on_readable, NULL);
2451
2452 /* set up g_async_tester */
2453 g_async_tester.allocator = allocator;
2454 g_async_tester.event_loop = event_loop;
2455 g_async_tester.write_socket = &outgoing;
2456 g_async_tester.read_socket = server_sock;
2457 g_async_tester.mutex = &mutex;
2458 g_async_tester.condition_variable = &condition_variable;
2459
2460 /* kick off writer and reader tasks */
2461 struct aws_task writer_task;
2462 aws_task_init(&writer_task, s_async_write_task, NULL, "async_test_write_task");
2463 aws_event_loop_schedule_task_now(event_loop, &writer_task);
2464
2465 struct aws_task reader_task;
2466 aws_task_init(&reader_task, s_async_read_task, NULL, "async_test_read_task");
2467 aws_event_loop_schedule_task_now(event_loop, &reader_task);
2468
2469 /* wait for tasks to complete */
2470 aws_mutex_lock(&mutex);
2471 aws_condition_variable_wait_pred(&condition_variable, &mutex, s_async_tasks_complete_pred, NULL);
2472 aws_mutex_unlock(&mutex);
2473
2474 aws_socket_set_cleanup_complete_callback(&listener, s_local_listener_shutdown_complete, &listener_args);
2475 /* cleanup */
2476 aws_socket_clean_up(&listener);
2477 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
2478 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2479 &condition_variable, &mutex, s_local_listener_shutdown_completed_predicate, &listener_args));
2480 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
2481
2482 aws_event_loop_group_release(el_group);
2483 aws_io_library_clean_up();
2484
2485 return 0;
2486 }
2487 AWS_TEST_CASE(sock_write_cb_is_async, s_sock_write_cb_is_async)
2488
2489 #ifdef _WIN32
2490 static int s_local_socket_pipe_connected_race(struct aws_allocator *allocator, void *ctx) {
2491 (void)ctx;
2492
2493 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
2494
2495 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
2496 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
2497
2498 struct aws_mutex mutex = AWS_MUTEX_INIT;
2499 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
2500
2501 struct local_listener_args listener_args = {
2502 .mutex = &mutex,
2503 .condition_variable = &condition_variable,
2504 .incoming = NULL,
2505 .incoming_invoked = false,
2506 .error_invoked = false,
2507 };
2508
2509 struct aws_socket_options options;
2510 AWS_ZERO_STRUCT(options);
2511 options.connect_timeout_ms = 3000;
2512 options.type = AWS_SOCKET_STREAM;
2513 options.domain = AWS_SOCKET_LOCAL;
2514
2515 struct aws_socket_endpoint endpoint;
2516 AWS_ZERO_STRUCT(endpoint);
2517 aws_socket_endpoint_init_local_address_for_test(&endpoint);
2518
2519 struct aws_socket listener;
2520 ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
2521
2522 struct aws_socket_bind_options socket_bind_options = {.local_endpoint = &endpoint};
2523 ASSERT_SUCCESS(aws_socket_bind(&listener, &socket_bind_options));
2524
2525 ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
2526
2527 /* do the connect after the named pipe has been created (in the bind call), but before the connect named pipe call
2528 has been made in start accept. This will ensure IOCP does what we think it does. */
2529 struct local_outgoing_args outgoing_args = {
2530 .mutex = &mutex, .condition_variable = &condition_variable, .connect_invoked = false, .error_invoked = false};
2531
2532 struct aws_socket outgoing;
2533 ASSERT_SUCCESS(aws_socket_init(&outgoing, allocator, &options));
2534
2535 struct aws_socket_connect_options connect_options = {
2536 .remote_endpoint = &endpoint,
2537 .event_loop = event_loop,
2538 .on_connection_result = s_local_outgoing_connection,
2539 .user_data = &outgoing_args};
2540
2541 ASSERT_SUCCESS(aws_socket_connect(&outgoing, &connect_options));
2542
2543 struct aws_socket_listener_options listener_options = {
2544 .on_accept_result = s_local_listener_incoming, .on_accept_result_user_data = &listener_args};
2545 ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, listener_options));
2546 aws_mutex_lock(&mutex);
2547 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
2548 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2549 &condition_variable, &mutex, s_connection_completed_predicate, &outgoing_args));
2550 aws_mutex_unlock(&mutex);
2551
2552 struct aws_socket *server_sock = &listener;
2553
2554 ASSERT_TRUE(listener_args.incoming_invoked);
2555 ASSERT_FALSE(listener_args.error_invoked);
2556 server_sock = listener_args.incoming;
2557 ASSERT_TRUE(outgoing_args.connect_invoked);
2558 ASSERT_FALSE(outgoing_args.error_invoked);
2559 ASSERT_INT_EQUALS(options.domain, listener_args.incoming->options.domain);
2560 ASSERT_INT_EQUALS(options.type, listener_args.incoming->options.type);
2561
2562 struct socket_io_args io_args = {
2563 .socket = &outgoing,
2564 .to_write = NULL,
2565 .to_read = NULL,
2566 .read_data = NULL,
2567 .mutex = &mutex,
2568 .amount_read = 0,
2569 .amount_written = 0,
2570 .error_code = 0,
2571 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
2572 .close_completed = false,
2573 };
2574
2575 struct aws_task close_task = {
2576 .fn = s_socket_close_task,
2577 .arg = &io_args,
2578 };
2579
2580 if (listener_args.incoming) {
2581 io_args.socket = listener_args.incoming;
2582 io_args.close_completed = false;
2583 aws_event_loop_schedule_task_now(event_loop, &close_task);
2584 aws_mutex_lock(&mutex);
2585 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
2586 aws_mutex_unlock(&mutex);
2587
2588 aws_socket_clean_up(listener_args.incoming);
2589 aws_mem_release(allocator, listener_args.incoming);
2590 }
2591
2592 io_args.socket = &outgoing;
2593 io_args.close_completed = false;
2594 aws_event_loop_schedule_task_now(event_loop, &close_task);
2595 aws_mutex_lock(&mutex);
2596 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
2597 aws_mutex_unlock(&mutex);
2598
2599 aws_socket_clean_up(&outgoing);
2600
2601 io_args.socket = &listener;
2602 io_args.close_completed = false;
2603 aws_event_loop_schedule_task_now(event_loop, &close_task);
2604 aws_mutex_lock(&mutex);
2605 aws_condition_variable_wait_pred(&io_args.condition_variable, &mutex, s_close_completed_predicate, &io_args);
2606 aws_mutex_unlock(&mutex);
2607
2608 aws_socket_clean_up(&listener);
2609
2610 aws_event_loop_destroy(event_loop);
2611
2612 return 0;
2613 }
2614 AWS_TEST_CASE(local_socket_pipe_connected_race, s_local_socket_pipe_connected_race)
2615
2616 #endif
2617
2618 static int s_test_socket_validate_port(struct aws_allocator *allocator, void *ctx) {
2619 (void)allocator;
2620 (void)ctx;
2621
2622 /* IPv4 - 16bit port, only bind can use 0 */
2623 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(80, AWS_SOCKET_IPV4));
2624 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(80, AWS_SOCKET_IPV4));
2625
2626 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect(0, AWS_SOCKET_IPV4));
2627 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(0, AWS_SOCKET_IPV4));
2628
2629 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect(0xFFFFFFFF, AWS_SOCKET_IPV4));
2630 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_bind(0xFFFFFFFF, AWS_SOCKET_IPV4));
2631
2632 /* IPv6 - 16bit port, only bind can use 0 */
2633 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(80, AWS_SOCKET_IPV6));
2634 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(80, AWS_SOCKET_IPV6));
2635
2636 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect(0, AWS_SOCKET_IPV6));
2637 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(0, AWS_SOCKET_IPV6));
2638
2639 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect(0xFFFFFFFF, AWS_SOCKET_IPV6));
2640 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_bind(0xFFFFFFFF, AWS_SOCKET_IPV6));
2641
2642 /* VSOCK - 32bit port, only bind can use VMADDR_PORT_ANY (-1U) */
2643 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(80, AWS_SOCKET_VSOCK));
2644 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(80, AWS_SOCKET_VSOCK));
2645
2646 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(0, AWS_SOCKET_VSOCK));
2647 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(0, AWS_SOCKET_VSOCK));
2648
2649 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(0x7FFFFFFF, AWS_SOCKET_VSOCK));
2650 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(0x7FFFFFFF, AWS_SOCKET_VSOCK));
2651
2652 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect((uint32_t)-1, AWS_SOCKET_VSOCK));
2653 ASSERT_SUCCESS(aws_socket_validate_port_for_bind((uint32_t)-1, AWS_SOCKET_VSOCK));
2654
2655 /* LOCAL - ignores port */
2656 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(0, AWS_SOCKET_LOCAL));
2657 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(0, AWS_SOCKET_LOCAL));
2658 ASSERT_SUCCESS(aws_socket_validate_port_for_connect(80, AWS_SOCKET_LOCAL));
2659 ASSERT_SUCCESS(aws_socket_validate_port_for_bind(80, AWS_SOCKET_LOCAL));
2660 ASSERT_SUCCESS(aws_socket_validate_port_for_connect((uint32_t)-1, AWS_SOCKET_LOCAL));
2661 ASSERT_SUCCESS(aws_socket_validate_port_for_bind((uint32_t)-1, AWS_SOCKET_LOCAL));
2662
2663 /* invalid domain should fail */
2664 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_connect(80, (enum aws_socket_domain)(-1)));
2665 ASSERT_ERROR(AWS_IO_SOCKET_INVALID_ADDRESS, aws_socket_validate_port_for_bind(80, (enum aws_socket_domain)(-1)));
2666
2667 return 0;
2668 }
2669 AWS_TEST_CASE(socket_validate_port, s_test_socket_validate_port)
2670
2671 static int s_test_parse_ipv4_valid_addresses(struct aws_allocator *allocator, void *ctx) {
2672 (void)ctx;
2673
2674 struct {
2675 const char *input;
2676 uint32_t expected_network_order;
2677 } test_cases[] = {
2678 {"127.0.0.1", aws_hton32(0x7F000001)}, /* localhost */
2679 {"0.0.0.0", aws_hton32(0x00000000)}, /* any address */
2680 {"255.255.255.255", aws_hton32(0xFFFFFFFF)}, /* broadcast */
2681 {"192.168.1.1", aws_hton32(0xC0A80101)}, /* common private IP */
2682 {"10.0.0.1", aws_hton32(0x0A000001)}, /* private IP */
2683 {"172.16.0.1", aws_hton32(0xAC100001)}, /* private IP */
2684 {"8.8.8.8", aws_hton32(0x08080808)}, /* Google DNS */
2685 {"1.2.3.4", aws_hton32(0x01020304)}, /* simple test case */
2686 };
2687
2688 for (size_t i = 0; i < AWS_ARRAY_SIZE(test_cases); i++) {
2689 uint32_t result;
2690 struct aws_string *addr_str = aws_string_new_from_c_str(allocator, test_cases[i].input);
2691 ASSERT_SUCCESS(aws_parse_ipv4_address(addr_str, &result));
2692 ASSERT_INT_EQUALS(test_cases[i].expected_network_order, result, "Failed for %s", test_cases[i].input);
2693 aws_string_destroy(addr_str);
2694 }
2695
2696 return AWS_OP_SUCCESS;
2697 }
2698
2699 AWS_TEST_CASE(parse_ipv4_valid_addresses, s_test_parse_ipv4_valid_addresses)
2700
2701 static int s_test_parse_ipv4_invalid_addresses(struct aws_allocator *allocator, void *ctx) {
2702 (void)ctx;
2703
2704 const char *invalid_addresses[] = {
2705 "", /* empty string */
2706 "256.1.1.1", /* octet > 255 */
2707 "1.1.1", /* too few octets */
2708 "1.1.1.1.1", /* too many octets */
2709 "1.1.1.a", /* non-numeric */
2710 "1..1.1", /* empty octet */
2711 "192.168.1.-1", /* negative number */
2712 "not.an.ip.address", /* clearly not an IP */
2713 "2001:db8::1", /* IPv6 address */
2714 };
2715
2716 for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_addresses); i++) {
2717 uint32_t result;
2718 struct aws_string *addr_str = aws_string_new_from_c_str(allocator, invalid_addresses[i]);
2719 ASSERT_FAILS(aws_parse_ipv4_address(addr_str, &result), "Failed for %s", invalid_addresses[i]);
2720 ASSERT_INT_EQUALS(AWS_IO_SOCKET_INVALID_ADDRESS, aws_last_error(), "Wrong error for %s", invalid_addresses[i]);
2721 aws_string_destroy(addr_str);
2722 }
2723
2724 return AWS_OP_SUCCESS;
2725 }
2726
2727 AWS_TEST_CASE(parse_ipv4_invalid_addresses, s_test_parse_ipv4_invalid_addresses)
2728
2729 static int s_test_parse_ipv6_valid_addresses(struct aws_allocator *allocator, void *ctx) {
2730 (void)ctx;
2731
2732 struct {
2733 const char *input;
2734 uint8_t expected[16];
2735 } test_cases[] = {
2736 // {"::1", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, /* loopback */
2737 {"::", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* any address */
2738 {"2001:db8:85a3::8a2e:370:7334",
2739 {0x20,
2740 0x01,
2741 0x0d,
2742 0xb8,
2743 0x85,
2744 0xa3,
2745 0x00,
2746 0x00,
2747 0x00,
2748 0x00,
2749 0x8a,
2750 0x2e,
2751 0x03,
2752 0x70,
2753 0x73,
2754 0x34}}, /* compressed
2755 */
2756 {"::ffff:192.168.1.1", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 1, 1}}, /* IPv4-mapped */
2757 {"fe80::1", {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, /* link-local */
2758 };
2759
2760 struct aws_byte_buf result;
2761 aws_byte_buf_init(&result, allocator, 16);
2762 for (size_t i = 0; i < AWS_ARRAY_SIZE(test_cases); i++) {
2763 struct aws_string *addr_str = aws_string_new_from_c_str(allocator, test_cases[i].input);
2764 ASSERT_SUCCESS(aws_parse_ipv6_address(addr_str, &result), "Failed for %s", test_cases[i].input);
2765 struct aws_byte_cursor expected = aws_byte_cursor_from_array(test_cases[i].expected, 16);
2766 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected, &result));
2767 aws_string_destroy(addr_str);
2768 aws_byte_buf_reset(&result, false);
2769 }
2770 aws_byte_buf_clean_up(&result);
2771 return AWS_OP_SUCCESS;
2772 }
2773
2774 AWS_TEST_CASE(parse_ipv6_valid_addresses, s_test_parse_ipv6_valid_addresses)
2775
2776 static int s_test_parse_ipv6_invalid_addresses(struct aws_allocator *allocator, void *ctx) {
2777 (void)ctx;
2778
2779 const char *invalid_addresses[] = {
2780 "", /* empty string */
2781 ":::", /* too many colons */
2782 "2001:db8:85a3::8a2e::7334", /* multiple :: */
2783 "2001:db8:85a3:0000:0000:8a2e:0370:7334:extra", /* too many groups */
2784 "2001:db8:85a3:0000:0000:8a2e:0370:733g", /* invalid hex digit */
2785 "192.168.1.1", /* IPv4 address */
2786 "not:an:ipv6:address", /* clearly not IPv6 */
2787 "gggg::1", /* invalid hex characters */
2788 };
2789
2790 struct aws_byte_buf result;
2791 aws_byte_buf_init(&result, allocator, 16);
2792 for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_addresses); i++) {
2793 struct aws_string *addr_str = aws_string_new_from_c_str(allocator, invalid_addresses[i]);
2794 ASSERT_FAILS(aws_parse_ipv6_address(addr_str, &result), "Failed for %s", invalid_addresses[i]);
2795 ASSERT_INT_EQUALS(AWS_IO_SOCKET_INVALID_ADDRESS, aws_last_error(), "Wrong error for %s", invalid_addresses[i]);
2796 aws_string_destroy(addr_str);
2797 aws_byte_buf_reset(&result, false);
2798 }
2799 aws_byte_buf_clean_up(&result);
2800 return AWS_OP_SUCCESS;
2801 }
2802
2803 AWS_TEST_CASE(parse_ipv6_invalid_addresses, s_test_parse_ipv6_invalid_addresses)