Skip to content

Commit

Permalink
only init send io
Browse files Browse the repository at this point in the history
  • Loading branch information
toidiu committed Aug 18, 2023
1 parent 9b511e2 commit 1f3fa42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
28 changes: 24 additions & 4 deletions tests/testlib/s2n_ktls_test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_server_io_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)
{
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/testlib/s2n_ktls_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_server_io_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);
Expand Down
59 changes: 25 additions & 34 deletions tests/unit/s2n_ktls_io_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_server_io_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;
Expand All @@ -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_server_io_send(server, &client_in));

struct iovec msg_iov[S2N_TEST_MSG_IOVLEN] = { 0 };
size_t total_sent = 0;
Expand All @@ -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_server_io_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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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_server_io_send(server, &client_in));

s2n_blocked_status blocked = S2N_NOT_BLOCKED;
size_t bytes_written = 0;
Expand All @@ -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);
};
};

Expand Down

0 comments on commit 1f3fa42

Please sign in to comment.