Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UUID for socket endpoints #570

Merged
merged 10 commits into from
May 19, 2023
6 changes: 6 additions & 0 deletions include/aws/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ AWS_IO_API int aws_socket_get_error(struct aws_socket *socket);
*/
AWS_IO_API bool aws_socket_is_open(struct aws_socket *socket);

/**
* Assigns a random address (UUID) for use with AWS_SOCKET_LOCAL (Unix Domain Sockets).
* For use in internal tests only.
*/
AWS_IO_API void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

Expand Down
11 changes: 11 additions & 0 deletions source/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <aws/common/condition_variable.h>
#include <aws/common/mutex.h>
#include <aws/common/string.h>
#include <aws/common/uuid.h>

#include <aws/io/event_loop.h>
#include <aws/io/logging.h>
Expand Down Expand Up @@ -1907,3 +1908,13 @@ int aws_socket_get_error(struct aws_socket *socket) {
bool aws_socket_is_open(struct aws_socket *socket) {
return socket->io_handle.data.fd >= 0;
}

void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint) {
struct aws_uuid uuid;
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS);
char uuid_str[AWS_UUID_STR_LEN] = {0};
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_str, sizeof(uuid_str));
uuid_buf.len = 0;
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf));
}
11 changes: 11 additions & 0 deletions source/windows/iocp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ below, clang-format doesn't work (at least on my version) with the c-style comme
#include <aws/common/condition_variable.h>
#include <aws/common/mutex.h>
#include <aws/common/task_scheduler.h>
#include <aws/common/uuid.h>

#include <aws/io/event_loop.h>
#include <aws/io/logging.h>
Expand Down Expand Up @@ -3232,3 +3233,13 @@ int aws_socket_get_error(struct aws_socket *socket) {
bool aws_socket_is_open(struct aws_socket *socket) {
return socket->io_handle.data.handle != INVALID_HANDLE_VALUE;
}

void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint) {
struct aws_uuid uuid;
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS);
char uuid_str[AWS_UUID_STR_LEN] = {0};
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_str, sizeof(uuid_str));
uuid_buf.len = 0;
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
snprintf(endpoint->address, sizeof(endpoint->address), "\\\\.\\pipe\\testsock" PRInSTR, AWS_BYTE_BUF_PRI(uuid_buf));
}
16 changes: 3 additions & 13 deletions tests/byo_crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@
# include <aws/common/atomics.h>
# include <aws/common/clock.h>
# include <aws/common/condition_variable.h>
# include <aws/common/uuid.h>
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved

# include <aws/testing/aws_test_harness.h>

# include "statistics_handler_test.h"
# include <read_write_test_handler.h>

# ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu"
# else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu.sock"
# endif

struct byo_crypto_test_args {
struct aws_allocator *allocator;
struct aws_mutex *mutex;
Expand Down Expand Up @@ -84,7 +79,6 @@ struct local_server_tester {
struct aws_socket_endpoint endpoint;
struct aws_server_bootstrap *server_bootstrap;
struct aws_socket *listener;
uint64_t timestamp;
};

static bool s_channel_setup_predicate(void *user_data) {
Expand Down Expand Up @@ -276,12 +270,8 @@ static int s_local_server_tester_init(
tester->socket_options.type = AWS_SOCKET_STREAM;
tester->socket_options.domain = AWS_SOCKET_LOCAL;

ASSERT_SUCCESS(aws_sys_clock_get_ticks(&tester->timestamp));
snprintf(
tester->endpoint.address,
sizeof(tester->endpoint.address),
LOCAL_SOCK_TEST_PATTERN,
(long long unsigned)tester->timestamp);
aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);

tester->server_bootstrap = aws_server_bootstrap_new(allocator, s_c_tester->el_group);
ASSERT_NOT_NULL(tester->server_bootstrap);

Expand Down
16 changes: 3 additions & 13 deletions tests/socket_handler_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
#include <aws/common/atomics.h>
#include <aws/common/clock.h>
#include <aws/common/condition_variable.h>
#include <aws/common/uuid.h>

#include <aws/testing/aws_test_harness.h>

#include "statistics_handler_test.h"
#include <read_write_test_handler.h>

#ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu"
#else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu.sock"
#endif

struct socket_test_args {
struct aws_allocator *allocator;
struct aws_mutex *mutex;
Expand Down Expand Up @@ -80,7 +75,6 @@ struct local_server_tester {
struct aws_socket_endpoint endpoint;
struct aws_server_bootstrap *server_bootstrap;
struct aws_socket *listener;
uint64_t timestamp;
};

static bool s_pinned_channel_setup_predicate(void *user_data) {
Expand Down Expand Up @@ -300,12 +294,8 @@ static int s_local_server_tester_init(
tester->socket_options.type = AWS_SOCKET_STREAM;
tester->socket_options.domain = AWS_SOCKET_LOCAL;

ASSERT_SUCCESS(aws_sys_clock_get_ticks(&tester->timestamp));
snprintf(
tester->endpoint.address,
sizeof(tester->endpoint.address),
LOCAL_SOCK_TEST_PATTERN,
(long long unsigned)tester->timestamp);
aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);

tester->server_bootstrap = aws_server_bootstrap_new(allocator, s_c_tester->el_group);
ASSERT_NOT_NULL(tester->server_bootstrap);

Expand Down
22 changes: 4 additions & 18 deletions tests/socket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
#include <aws/common/condition_variable.h>
#include <aws/common/string.h>
#include <aws/common/task_scheduler.h>
#include <aws/common/uuid.h>
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved

#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/io/socket.h>

#ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu"
#else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu.sock"
#endif

#ifdef _MSC_VER
# pragma warning(disable : 4996) /* strncpy */
#endif
Expand Down Expand Up @@ -410,13 +405,9 @@ static int s_test_local_socket_communication(struct aws_allocator *allocator, vo
options.connect_timeout_ms = 3000;
options.type = AWS_SOCKET_STREAM;
options.domain = AWS_SOCKET_LOCAL;

uint64_t timestamp = 0;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(&timestamp));
struct aws_socket_endpoint endpoint;
AWS_ZERO_STRUCT(endpoint);

snprintf(endpoint.address, sizeof(endpoint.address), LOCAL_SOCK_TEST_PATTERN, (long long unsigned)timestamp);
aws_socket_endpoint_init_local_address_for_test(&endpoint);

return s_test_socket(allocator, &options, &endpoint);
}
Expand Down Expand Up @@ -1578,12 +1569,9 @@ static int s_sock_write_cb_is_async(struct aws_allocator *allocator, void *ctx)
options.keep_alive_timeout_sec = 60000;
options.type = AWS_SOCKET_STREAM;
options.domain = AWS_SOCKET_LOCAL;

uint64_t timestamp = 0;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(&timestamp));
struct aws_socket_endpoint endpoint;
AWS_ZERO_STRUCT(endpoint);
snprintf(endpoint.address, sizeof(endpoint.address), LOCAL_SOCK_TEST_PATTERN, (long long unsigned)timestamp);
aws_socket_endpoint_init_local_address_for_test(&endpoint);

struct aws_socket listener;
ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
Expand Down Expand Up @@ -1672,11 +1660,9 @@ static int s_local_socket_pipe_connected_race(struct aws_allocator *allocator, v
options.type = AWS_SOCKET_STREAM;
options.domain = AWS_SOCKET_LOCAL;

uint64_t timestamp = 0;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(&timestamp));
struct aws_socket_endpoint endpoint;
AWS_ZERO_STRUCT(endpoint);
snprintf(endpoint.address, sizeof(endpoint.address), LOCAL_SOCK_TEST_PATTERN, (long long unsigned)timestamp);
aws_socket_endpoint_init_local_address_for_test(&endpoint);

struct aws_socket listener;
ASSERT_SUCCESS(aws_socket_init(&listener, allocator, &options));
Expand Down
19 changes: 5 additions & 14 deletions tests/tls_handler_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# include <aws/common/clock.h>
# include <aws/common/condition_variable.h>
# include <aws/common/thread.h>
# include <aws/common/uuid.h>
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved

# include <aws/testing/aws_test_harness.h>

Expand All @@ -25,12 +26,6 @@

# include <aws/io/private/pki_utils.h>

# ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu_%d"
# else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu_%d.sock"
# endif

struct tls_test_args {
struct aws_allocator *allocator;
struct aws_mutex *mutex;
Expand Down Expand Up @@ -139,7 +134,6 @@ struct tls_local_server_tester {
struct aws_socket_endpoint endpoint;
struct aws_server_bootstrap *server_bootstrap;
struct aws_socket *listener;
uint64_t timestamp;
};

static int s_tls_test_arg_init(
Expand Down Expand Up @@ -391,19 +385,16 @@ static int s_tls_local_server_tester_init(
int server_index,
const char *cert_path,
const char *pkey_path) {
(void)server_index;
TwistedTwigleg marked this conversation as resolved.
Show resolved Hide resolved
AWS_ZERO_STRUCT(*tester);
ASSERT_SUCCESS(s_tls_server_opt_tester_init(allocator, &tester->server_tls_opt_tester, cert_path, pkey_path));
aws_tls_connection_options_set_callbacks(&tester->server_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, args);
tester->socket_options.connect_timeout_ms = 3000;
tester->socket_options.type = AWS_SOCKET_STREAM;
tester->socket_options.domain = AWS_SOCKET_LOCAL;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(&tester->timestamp));
snprintf(
tester->endpoint.address,
sizeof(tester->endpoint.address),
LOCAL_SOCK_TEST_PATTERN,
(long long unsigned)tester->timestamp,
server_index);

aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);

tester->server_bootstrap = aws_server_bootstrap_new(allocator, tls_c_tester->el_group);
ASSERT_NOT_NULL(tester->server_bootstrap);

Expand Down