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
10 changes: 10 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,12 @@ 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_empty_array(uuid_str, sizeof(uuid_str));
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));
}
10 changes: 10 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,12 @@ 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_empty_array(uuid_str, sizeof(uuid_str));
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: 2 additions & 14 deletions tests/byo_crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@
# include <aws/io/tls_channel_handler.h>

# include <aws/common/atomics.h>
# include <aws/common/clock.h>
# include <aws/common/condition_variable.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 byo_crypto_test_args {
struct aws_allocator *allocator;
struct aws_mutex *mutex;
Expand Down Expand Up @@ -84,7 +77,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 +268,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
15 changes: 2 additions & 13 deletions tests/socket_handler_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
#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 +74,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 +293,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
21 changes: 3 additions & 18 deletions tests/socket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
#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 +404,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 +1568,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 +1659,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
28 changes: 8 additions & 20 deletions tests/tls_handler_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,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 +133,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 @@ -388,7 +381,6 @@ static int s_tls_local_server_tester_init(
struct tls_test_args *args,
struct tls_common_tester *tls_c_tester,
bool enable_back_pressure,
int server_index,
const char *cert_path,
const char *pkey_path) {
AWS_ZERO_STRUCT(*tester);
Expand All @@ -397,13 +389,9 @@ static int s_tls_local_server_tester_init(
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 Expand Up @@ -524,7 +512,7 @@ static int s_tls_channel_echo_and_backpressure_test_fn(struct aws_allocator *all

struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, true, 1, "server.crt", "server.key"));
allocator, &local_server_tester, &incoming_args, &c_tester, true, "server.crt", "server.key"));
/* make the windows small to make sure back pressure is honored. */
struct aws_channel_handler *outgoing_rw_handler = rw_handler_new(
allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, write_tag.len / 2, &outgoing_rw_args);
Expand Down Expand Up @@ -1431,7 +1419,7 @@ static int s_tls_server_multiple_connections_fn(struct aws_allocator *allocator,

struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));

struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
Expand Down Expand Up @@ -1580,7 +1568,7 @@ static int s_tls_server_hangup_during_negotiation_fn(struct aws_allocator *alloc

struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));

ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));

Expand Down Expand Up @@ -1725,7 +1713,7 @@ static int s_tls_channel_statistics_test(struct aws_allocator *allocator, void *

struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));

struct aws_channel_handler *outgoing_rw_handler =
rw_handler_new(allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, 10000, &outgoing_rw_args);
Expand Down Expand Up @@ -1846,7 +1834,7 @@ static int s_tls_certificate_chain_test(struct aws_allocator *allocator, void *c

struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server_chain.crt", "server.key"));
allocator, &local_server_tester, &incoming_args, &c_tester, false, "server_chain.crt", "server.key"));

struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
Expand Down