Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
toidiu committed Aug 17, 2023
1 parent 68ead2c commit 2dbf438
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
37 changes: 16 additions & 21 deletions tests/unit/s2n_ktls_io_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ int main(int argc, char **argv)
s2n_ktls_io_stuffer_pair_free);
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));

struct iovec msg_iov_valid = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
struct iovec msg_iov_valid = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };

EXPECT_ERROR_WITH_ERRNO(
s2n_ktls_sendmsg(server, test_record_type, NULL, 1, &blocked, &bytes_written),
Expand All @@ -142,9 +142,9 @@ int main(int argc, char **argv)
s2n_ktls_io_stuffer_pair_free);
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));

struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
EXPECT_OK(s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written));
EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
Expand All @@ -168,17 +168,16 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));

uint8_t count = 5;
struct iovec msg_iov[sizeof(struct iovec) * 5] = { 0 };
size_t total_sent = 0;
struct iovec msg_iov[sizeof(struct iovec) * 5] = { 0 };
for (size_t i = 0; i < count; i++) {
msg_iov[i].iov_base = test_data + total_sent;
msg_iov[i].iov_len = S2N_TEST_TO_SEND;

total_sent += S2N_TEST_TO_SEND;
}

s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
EXPECT_OK(s2n_ktls_sendmsg(
server, test_record_type, msg_iov, count, &blocked, &bytes_written));
EXPECT_EQUAL(bytes_written, total_sent);
Expand All @@ -205,10 +204,10 @@ int main(int argc, char **argv)
/* disable growable to simulate blocked/network buffer full */
io_pair.client_in.data_buffer.growable = false;

size_t bytes_written = 0;
size_t blocked_invoked_count = 5;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
for (size_t i = 0; i < blocked_invoked_count; i++) {
EXPECT_ERROR_WITH_ERRNO(
s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written),
Expand All @@ -235,21 +234,19 @@ int main(int argc, char **argv)
{
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
/* setup sendmsg callback */
struct s2n_test_ktls_io_fail io_ctx = {
.invoked_count = 0
};
struct s2n_test_ktls_io_fail io_ctx = { 0 };
EXPECT_OK(s2n_ktls_set_sendmsg_cb(server, s2n_test_ktls_sendmsg_fail, &io_ctx));

struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };

io_ctx.errno_code = EWOULDBLOCK;
EXPECT_ERROR_WITH_ERRNO(
s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written),
S2N_ERR_IO_BLOCKED);

/* cppcheck-suppress redundantAssignment */
io_ctx.errno_code = EAGAIN;
EXPECT_ERROR_WITH_ERRNO(
s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written),
Expand All @@ -262,16 +259,14 @@ int main(int argc, char **argv)
{
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
/* setup sendmsg callback */
struct s2n_test_ktls_io_fail io_ctx = {
.errno_code = EINVAL,
.invoked_count = 0,
};
EXPECT_OK(s2n_ktls_set_sendmsg_cb(server, s2n_test_ktls_sendmsg_fail, &io_ctx));

struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
EXPECT_ERROR_WITH_ERRNO(
s2n_ktls_sendmsg(server, test_record_type, &msg_iov, 1, &blocked, &bytes_written),
S2N_ERR_IO);
Expand All @@ -291,9 +286,9 @@ int main(int argc, char **argv)
s2n_ktls_io_stuffer_pair_free);
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));

struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
s2n_blocked_status blocked = S2N_NOT_BLOCKED;
struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };

size_t iovlen_zero = 0;
EXPECT_OK(s2n_ktls_sendmsg(
Expand Down
13 changes: 7 additions & 6 deletions tls/s2n_ktls_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ S2N_RESULT s2n_ktls_sendmsg(struct s2n_connection *conn, uint8_t record_type,

RESULT_ENSURE(msg_iov->iov_base, S2N_ERR_INVALID_ARGUMENT);

struct msghdr msg = { 0 };
/* msghdr requires a non-const iovec. This is safe because s2n-tls does
* not modify msg_iov after this point.
*/
msg.msg_iov = (struct iovec *) (uintptr_t) msg_iov;
msg.msg_iovlen = msg_iovlen;
struct msghdr msg = {
/* msghdr requires a non-const iovec. This is safe because s2n-tls does
* not modify msg_iov after this point.
*/
.msg_iov = (struct iovec *) (uintptr_t) msg_iov,
.msg_iovlen = msg_iovlen,
};

char control_data[S2N_KTLS_CONTROL_BUFFER_SIZE] = { 0 };
RESULT_GUARD(s2n_ktls_set_control_data(&msg, control_data, sizeof(control_data),
Expand Down

0 comments on commit 2dbf438

Please sign in to comment.