Skip to content

Commit

Permalink
Added serialize ability
Browse files Browse the repository at this point in the history
  • Loading branch information
maddeleine committed Mar 27, 2024
1 parent ba825b8 commit 7fbd369
Show file tree
Hide file tree
Showing 12 changed files with 671 additions and 49 deletions.
9 changes: 0 additions & 9 deletions tests/testlib/s2n_connection_test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,3 @@ S2N_RESULT s2n_set_all_mutually_supported_groups(struct s2n_connection *conn)

return S2N_RESULT_OK;
}

S2N_RESULT s2n_skip_handshake(struct s2n_connection *conn)
{
conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
while (!s2n_handshake_is_complete(conn)) {
conn->handshake.message_number++;
}
return S2N_RESULT_OK;
}
21 changes: 21 additions & 0 deletions tests/testlib/s2n_test_server_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,24 @@ int s2n_shutdown_test_server_and_client(struct s2n_connection *server_conn, stru
int rc = (server_rc == 0 && client_rc == 0) ? 0 : -1;
return rc;
}

S2N_RESULT s2n_send_and_recv_test(struct s2n_connection *send_conn, struct s2n_connection *recv_conn)
{
RESULT_ENSURE_REF(send_conn);
RESULT_ENSURE_REF(recv_conn);

s2n_blocked_status blocked = S2N_NOT_BLOCKED;

const uint8_t send_data[] = "hello world";
ssize_t send_size = s2n_send(send_conn, send_data, sizeof(send_data), &blocked);
RESULT_GUARD_POSIX(send_size);
RESULT_ENSURE_EQ(send_size, sizeof(send_data));

uint8_t recv_data[sizeof(send_data)] = { 0 };
ssize_t recv_size = s2n_recv(recv_conn, recv_data, send_size, &blocked);
RESULT_GUARD_POSIX(recv_size);
RESULT_ENSURE_EQ(recv_size, send_size);
RESULT_ENSURE_EQ(memcmp(recv_data, send_data, send_size), 0);

return S2N_RESULT_OK;
}
2 changes: 1 addition & 1 deletion tests/testlib/s2n_testlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ int s2n_connection_allow_response_extension(struct s2n_connection *conn, uint16_
int s2n_connection_allow_all_response_extensions(struct s2n_connection *conn);
int s2n_connection_set_all_protocol_versions(struct s2n_connection *conn, uint8_t version);
S2N_RESULT s2n_set_all_mutually_supported_groups(struct s2n_connection *conn);
S2N_RESULT s2n_skip_handshake(struct s2n_connection *conn);
S2N_RESULT s2n_connection_set_test_message_type(struct s2n_connection *conn, message_type_t expected_message_type);

S2N_RESULT s2n_connection_set_secrets(struct s2n_connection *conn);
Expand Down Expand Up @@ -233,6 +232,7 @@ S2N_RESULT s2n_negotiate_test_server_and_client_until_message(struct s2n_connect
int s2n_shutdown_test_server_and_client(struct s2n_connection *server_conn, struct s2n_connection *client_conn);
S2N_RESULT s2n_negotiate_test_server_and_client_with_early_data(struct s2n_connection *server_conn,
struct s2n_connection *client_conn, struct s2n_blob *early_data_to_send, struct s2n_blob *early_data_received);
S2N_RESULT s2n_send_and_recv_test(struct s2n_connection *send_conn, struct s2n_connection *recv_conn);

/* Testing only with easily constructed contiguous data buffers could hide errors.
* We should use iovecs where every buffer is allocated separately.
Expand Down
51 changes: 15 additions & 36 deletions tests/unit/s2n_client_hello_request_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,6 @@ static const uint8_t hello_request_msg[] = {
/* empty message body */
};

static S2N_RESULT s2n_test_send_and_recv(struct s2n_connection *send_conn, struct s2n_connection *recv_conn)
{
RESULT_ENSURE_REF(send_conn);
RESULT_ENSURE_REF(recv_conn);

s2n_blocked_status blocked = S2N_NOT_BLOCKED;

const uint8_t send_data[] = "hello world";
ssize_t send_size = s2n_send(send_conn, send_data, sizeof(send_data), &blocked);
RESULT_GUARD_POSIX(send_size);
RESULT_ENSURE_EQ(send_size, sizeof(send_data));

uint8_t recv_data[sizeof(send_data)] = { 0 };
ssize_t recv_size = s2n_recv(recv_conn, recv_data, send_size, &blocked);
RESULT_GUARD_POSIX(recv_size);
RESULT_ENSURE_EQ(recv_size, send_size);
EXPECT_BYTEARRAY_EQUAL(recv_data, send_data, send_size);

return S2N_RESULT_OK;
}

static S2N_RESULT s2n_send_client_hello_request(struct s2n_connection *server_conn)
{
RESULT_ENSURE_REF(server_conn);
Expand Down Expand Up @@ -213,16 +192,16 @@ int main(int argc, char **argv)
EXPECT_TRUE(client_conn->secure_renegotiation);

/* Send some data */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_OK(s2n_test_send_and_recv(client_conn, server_conn));
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));

/* Send the hello request message. */
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* Send some more data */
for (size_t i = 0; i < 10; i++) {
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_OK(s2n_test_send_and_recv(client_conn, server_conn));
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
EXPECT_TRUE(s2n_connection_check_io_status(client_conn, S2N_IO_FULL_DUPLEX));
}
};
Expand Down Expand Up @@ -261,8 +240,8 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* no_renegotation alert NOT sent and received */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_OK(s2n_test_send_and_recv(client_conn, server_conn));
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));

/* Callback was not set */
EXPECT_NULL(client_conn->config->renegotiate_request_cb);
Expand Down Expand Up @@ -309,8 +288,8 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* no_renegotation alert sent and received */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_ERROR_WITH_ERRNO(s2n_test_send_and_recv(client_conn, server_conn), S2N_ERR_ALERT);
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(client_conn, server_conn), S2N_ERR_ALERT);
EXPECT_EQUAL(s2n_connection_get_alert(server_conn), S2N_TLS_ALERT_NO_RENEGOTIATION);

/* Callback triggered */
Expand Down Expand Up @@ -353,8 +332,8 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* no_renegotation alert NOT sent and received */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_OK(s2n_test_send_and_recv(client_conn, server_conn));
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));

/* Callback triggered */
EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
Expand Down Expand Up @@ -391,8 +370,8 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* no_renegotation alert NOT sent and received */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_OK(s2n_test_send_and_recv(client_conn, server_conn));
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));

/* Callback triggered */
EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
Expand Down Expand Up @@ -445,8 +424,8 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_send_client_hello_request(server_conn));

/* no_renegotation alert sent and received */
EXPECT_OK(s2n_test_send_and_recv(server_conn, client_conn));
EXPECT_ERROR_WITH_ERRNO(s2n_test_send_and_recv(client_conn, server_conn), S2N_ERR_ALERT);
EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(client_conn, server_conn), S2N_ERR_ALERT);
EXPECT_EQUAL(s2n_connection_get_alert(server_conn), S2N_TLS_ALERT_NO_RENEGOTIATION);

/* Callback was not triggered */
Expand Down Expand Up @@ -491,7 +470,7 @@ int main(int argc, char **argv)
* Applications won't be able to set s2n_errno to a meaningful value,
* so we need to set it to S2N_ERR_CANCELED for them.
*/
EXPECT_ERROR_WITH_ERRNO(s2n_test_send_and_recv(server_conn, client_conn), S2N_ERR_CANCELLED);
EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(server_conn, client_conn), S2N_ERR_CANCELLED);
};

/* Test: SSLv3 sends a fatal handshake_failure alert instead of no_renegotiate
Expand Down
Loading

0 comments on commit 7fbd369

Please sign in to comment.