Skip to content

Commit

Permalink
fix: address PR comments
Browse files Browse the repository at this point in the history
* do open and close `file` whenever that is needed
  • Loading branch information
Boquan Fang committed Oct 23, 2024
1 parent aab629b commit 33f9a89
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions tests/unit/s2n_self_talk_ktls_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ static S2N_RESULT s2n_setup_connections(struct s2n_connection *server,
* For a real self-talk test we need real kernel support for kTLS, and only
* AF_INET sockets support kTLS.
*
* Pass in file as an argument, so that we can close it once the child process
* exits.
*/
static S2N_RESULT s2n_new_inet_socket_pair(struct s2n_test_io_pair *io_pair, int file)
static S2N_RESULT s2n_new_inet_socket_pair(struct s2n_test_io_pair *io_pair)
{
RESULT_ENSURE_REF(io_pair);

Expand All @@ -92,13 +90,12 @@ static S2N_RESULT s2n_new_inet_socket_pair(struct s2n_test_io_pair *io_pair, int
RESULT_ENSURE_EQ(connect(io_pair->client, (struct sockaddr *) &saddr, addrlen), 0);
EXPECT_SUCCESS(s2n_io_pair_close(io_pair));
ZERO_TO_DISABLE_DEFER_CLEANUP(io_pair);
close(listener);
close(file);
RESULT_ENSURE_EQ(close(listener), 0);
exit(0);
}
io_pair->server = accept(listener, NULL, NULL);
RESULT_ENSURE_GT(io_pair->server, 0);
close(listener);
RESULT_ENSURE_EQ(close(listener), 0);
return S2N_RESULT_OK;
}

Expand Down Expand Up @@ -146,6 +143,7 @@ int main(int argc, char **argv)
EXPECT_TRUE(file > 0);
int file_read = pread(file, file_test_data, sizeof(file_test_data), 0);
EXPECT_EQUAL(file_read, sizeof(file_test_data));
EXPECT_SUCCESS(close(file));

DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
Expand All @@ -172,7 +170,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_config(server, config));

DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
if (s2n_result_is_error(s2n_new_inet_socket_pair(&io_pair, file))) {
if (s2n_result_is_error(s2n_new_inet_socket_pair(&io_pair))) {
/* We should be able to setup AF_INET sockets everywhere, but if
* we can't, don't block the build unless the build explicitly expects
* to be able to test ktls.
Expand Down Expand Up @@ -217,7 +215,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_config(server, config));

DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));

struct s2n_connection *conns[] = {
Expand Down Expand Up @@ -278,6 +276,7 @@ int main(int argc, char **argv)
};

/* Test: s2n_sendfile */
file = open(argv[0], O_RDONLY);
for (size_t offset_i = 0; offset_i < s2n_array_len(test_offsets); offset_i++) {
const size_t offset = test_offsets[offset_i];
const size_t expected_written = sizeof(test_data) - offset;
Expand All @@ -295,6 +294,7 @@ int main(int argc, char **argv)

EXPECT_BYTEARRAY_EQUAL(file_test_data + offset, buffer, read);
}
EXPECT_SUCCESS(close(file));

/* Test: s2n_shutdown */
{
Expand Down Expand Up @@ -328,7 +328,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));

DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));

struct s2n_connection *conns[] = {
Expand Down Expand Up @@ -422,7 +422,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "default_tls13"));

DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
EXPECT_EQUAL(client->actual_protocol_version, S2N_TLS13);
EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
Expand Down Expand Up @@ -527,7 +527,7 @@ int main(int argc, char **argv)
/* Test: Receive an alert while calling s2n_recv */
{
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));

Expand All @@ -551,7 +551,7 @@ int main(int argc, char **argv)
/* Test: Receive an alert while calling s2n_shutdown */
{
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));

Expand Down Expand Up @@ -581,7 +581,7 @@ int main(int argc, char **argv)
/* Test: Receive "end of data" while calling s2n_recv */
{
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));

Expand Down Expand Up @@ -666,7 +666,7 @@ int main(int argc, char **argv)
server->security_policy_override = &policy;

DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair, file));
EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
EXPECT_OK(s2n_setup_connections(server, client, &io_pair));

struct s2n_connection *conns[] = {
Expand Down Expand Up @@ -705,7 +705,6 @@ int main(int argc, char **argv)
}
}
}
close(file);

END_TEST();
}

0 comments on commit 33f9a89

Please sign in to comment.