diff --git a/tests/testlib/s2n_ktls_test_utils.c b/tests/testlib/s2n_ktls_test_utils.c index 569f4c4a94b..20c0897c4e3 100644 --- a/tests/testlib/s2n_ktls_test_utils.c +++ b/tests/testlib/s2n_ktls_test_utils.c @@ -152,6 +152,19 @@ ssize_t s2n_test_ktls_recvmsg_io_stuffer(void *io_context, struct msghdr *msg) return bytes_read; } +S2N_RESULT s2n_test_init_ktls_io_stuffer_send(struct s2n_connection *conn, + struct s2n_test_ktls_io_stuffer *io) +{ + RESULT_ENSURE_REF(conn); + RESULT_ENSURE_REF(io); + + RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&io->data_buffer, 0)); + RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&io->ancillary_buffer, 0)); + RESULT_GUARD(s2n_ktls_set_sendmsg_cb(conn, s2n_test_ktls_sendmsg_io_stuffer, io)); + + return S2N_RESULT_OK; +} + S2N_RESULT s2n_test_init_ktls_io_stuffer(struct s2n_connection *server, struct s2n_connection *client, struct s2n_test_ktls_io_stuffer_pair *io_pair) { @@ -172,13 +185,20 @@ S2N_RESULT s2n_test_init_ktls_io_stuffer(struct s2n_connection *server, return S2N_RESULT_OK; } +S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_free(struct s2n_test_ktls_io_stuffer *io) +{ + RESULT_ENSURE_REF(io); + RESULT_GUARD_POSIX(s2n_stuffer_free(&io->data_buffer)); + RESULT_GUARD_POSIX(s2n_stuffer_free(&io->ancillary_buffer)); + return S2N_RESULT_OK; +} + S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_pair_free(struct s2n_test_ktls_io_stuffer_pair *pair) { RESULT_ENSURE_REF(pair); - RESULT_GUARD_POSIX(s2n_stuffer_free(&pair->client_in.data_buffer)); - RESULT_GUARD_POSIX(s2n_stuffer_free(&pair->client_in.ancillary_buffer)); - RESULT_GUARD_POSIX(s2n_stuffer_free(&pair->server_in.data_buffer)); - RESULT_GUARD_POSIX(s2n_stuffer_free(&pair->server_in.ancillary_buffer)); + + RESULT_GUARD(s2n_ktls_io_stuffer_free(&pair->client_in)); + RESULT_GUARD(s2n_ktls_io_stuffer_free(&pair->server_in)); return S2N_RESULT_OK; } diff --git a/tests/testlib/s2n_ktls_test_utils.h b/tests/testlib/s2n_ktls_test_utils.h index 9adacc46ebc..aed6b546bd8 100644 --- a/tests/testlib/s2n_ktls_test_utils.h +++ b/tests/testlib/s2n_ktls_test_utils.h @@ -62,8 +62,11 @@ struct s2n_test_ktls_io_stuffer_pair { ssize_t s2n_test_ktls_sendmsg_io_stuffer(void *io_context, const struct msghdr *msg); ssize_t s2n_test_ktls_recvmsg_io_stuffer(void *io_context, struct msghdr *msg); +S2N_RESULT s2n_test_init_ktls_io_stuffer_send(struct s2n_connection *conn, + struct s2n_test_ktls_io_stuffer *io); S2N_RESULT s2n_test_init_ktls_io_stuffer(struct s2n_connection *server, struct s2n_connection *client, struct s2n_test_ktls_io_stuffer_pair *io_pair); +S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_free(struct s2n_test_ktls_io_stuffer *io); S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_pair_free(struct s2n_test_ktls_io_stuffer_pair *pair); S2N_RESULT s2n_test_validate_data(struct s2n_test_ktls_io_stuffer *ktls_io, uint8_t *expected_data, uint16_t expected_len); diff --git a/tests/unit/s2n_ktls_io_test.c b/tests/unit/s2n_ktls_io_test.c index c0874d2487f..936bbf29bd9 100644 --- a/tests/unit/s2n_ktls_io_test.c +++ b/tests/unit/s2n_ktls_io_test.c @@ -136,11 +136,9 @@ int main(int argc, char **argv) { DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), - s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, - s2n_ktls_io_stuffer_pair_free); - EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); + DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 }, + s2n_ktls_io_stuffer_free); + EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in)); struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND }; s2n_blocked_status blocked = S2N_NOT_BLOCKED; @@ -150,22 +148,19 @@ int main(int argc, char **argv) EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); /* confirm sent data */ - EXPECT_OK(s2n_test_validate_ancillary(&io_pair.client_in, test_record_type, S2N_TEST_TO_SEND)); - EXPECT_OK(s2n_test_validate_data(&io_pair.client_in, test_data, S2N_TEST_TO_SEND)); + EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, S2N_TEST_TO_SEND)); + EXPECT_OK(s2n_test_validate_data(&client_in, test_data, S2N_TEST_TO_SEND)); - EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 1); - EXPECT_EQUAL(io_pair.server_in.sendmsg_invoked_count, 0); + EXPECT_EQUAL(client_in.sendmsg_invoked_count, 1); }; /* Happy case: msg_iovlen > 1 */ { DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), - s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, - s2n_ktls_io_stuffer_pair_free); - EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); + DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 }, + s2n_ktls_io_stuffer_free); + EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in)); struct iovec msg_iov[S2N_TEST_MSG_IOVLEN] = { 0 }; size_t total_sent = 0; @@ -182,26 +177,24 @@ int main(int argc, char **argv) EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); /* confirm sent data */ - EXPECT_OK(s2n_test_validate_ancillary(&io_pair.client_in, test_record_type, total_sent)); - EXPECT_OK(s2n_test_validate_data(&io_pair.client_in, test_data, total_sent)); + EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, total_sent)); + EXPECT_OK(s2n_test_validate_data(&client_in, test_data, total_sent)); /* validate only 1 record was sent */ - EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.client_in.ancillary_buffer), + EXPECT_EQUAL(s2n_stuffer_data_available(&client_in.ancillary_buffer), S2N_TEST_KTLS_MOCK_HEADER_SIZE); - EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 1); + EXPECT_EQUAL(client_in.sendmsg_invoked_count, 1); }; /* Simulate a blocked network and handle a S2N_ERR_IO_BLOCKED error */ { DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), - s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, - s2n_ktls_io_stuffer_pair_free); - EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); + DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 }, + s2n_ktls_io_stuffer_free); + EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in)); /* disable growable to simulate blocked/network buffer full */ - io_pair.client_in.data_buffer.growable = false; + client_in.data_buffer.growable = false; struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND }; s2n_blocked_status blocked = S2N_NOT_BLOCKED; @@ -216,15 +209,15 @@ int main(int argc, char **argv) /* enable growable to unblock write */ /* cppcheck-suppress redundantAssignment */ - io_pair.client_in.data_buffer.growable = true; + client_in.data_buffer.growable = true; EXPECT_OK(s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written)); EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND); /* confirm sent data */ - EXPECT_OK(s2n_test_validate_ancillary(&io_pair.client_in, test_record_type, S2N_TEST_TO_SEND)); - EXPECT_OK(s2n_test_validate_data(&io_pair.client_in, test_data, S2N_TEST_TO_SEND)); + EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, S2N_TEST_TO_SEND)); + EXPECT_OK(s2n_test_validate_data(&client_in, test_data, S2N_TEST_TO_SEND)); - EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, blocked_invoked_count + 1); + EXPECT_EQUAL(client_in.sendmsg_invoked_count, blocked_invoked_count + 1); }; /* Both EWOULDBLOCK and EAGAIN should return a S2N_ERR_IO_BLOCKED error */ @@ -277,11 +270,9 @@ int main(int argc, char **argv) { DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), - s2n_connection_ptr_free); - DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, - s2n_ktls_io_stuffer_pair_free); - EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); + DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 }, + s2n_ktls_io_stuffer_free); + EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in)); s2n_blocked_status blocked = S2N_NOT_BLOCKED; size_t bytes_written = 0; @@ -297,7 +288,7 @@ int main(int argc, char **argv) EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); EXPECT_EQUAL(bytes_written, 0); - EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 2); + EXPECT_EQUAL(client_in.sendmsg_invoked_count, 2); }; };