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 22, 2024
1 parent aab629b commit 1b30fb6
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions tests/unit/s2n_self_talk_ktls_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static S2N_RESULT s2n_setup_connections(struct s2n_connection *server,
* 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 +92,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 +145,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 +172,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 +217,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 +278,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 +296,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 +330,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 +424,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 +529,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 +553,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 +583,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 +668,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 +707,6 @@ int main(int argc, char **argv)
}
}
}
close(file);

END_TEST();
}

0 comments on commit 1b30fb6

Please sign in to comment.