Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-mariappan-ramasamy committed Jan 2, 2025
1 parent aaf1f31 commit c654796
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/he/wolf.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ int he_wolf_tls_read(WOLFSSL *ssl, char *buf, int sz, void *ctx);
// Todo document this
int he_wolf_tls_write(WOLFSSL *ssl, char *buf, int sz, void *ctx);

extern thread_local uint8_t write_buffer[HE_MAX_WIRE_MTU];

#endif // WOLF_H
3 changes: 2 additions & 1 deletion test/he/test_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ void tearDown(void) {
}

void test_he_internal_stream_setup_state_overwrite(void) {
he_conn_set_ssl_error_Expect(conn, 0);

conn->incoming_data_left_to_read = 42;
int res = he_internal_setup_stream_state(conn, empty_data, sizeof(empty_data));

TEST_ASSERT_EQUAL(HE_ERR_SSL_ERROR, res);
TEST_ASSERT_EQUAL(0, conn->wolf_error);
}
7 changes: 5 additions & 2 deletions test/he/test_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ void test_outside_pktrcv_good_packet_in_connecting_actual_error(void) {
he_internal_generate_event_Expect(conn, HE_EVENT_FIRST_MESSAGE_RECEIVED);
wolfSSL_negotiate_ExpectAndReturn(conn->wolf_ssl, FATAL_ERROR);
wolfSSL_get_error_ExpectAndReturn(conn->wolf_ssl, FATAL_ERROR, SSL_FATAL_ERROR);
he_conn_set_ssl_error_Expect(conn, SSL_FATAL_ERROR);
conn->state = HE_STATE_CONNECTING;
he_return_code_t res1 =
he_internal_flow_outside_packet_received(conn, packet, test_buffer_length);
Expand Down Expand Up @@ -536,6 +537,7 @@ void test_handle_process_packet_other_error(void) {
wolfSSL_read_ExpectAndReturn(conn->wolf_ssl, conn->read_packet.packet,
sizeof(conn->read_packet.packet), SSL_FATAL_ERROR);
wolfSSL_get_error_ExpectAndReturn(conn->wolf_ssl, SSL_FATAL_ERROR, SSL_FATAL_ERROR);
he_conn_set_ssl_error_Expect(conn, SSL_FATAL_ERROR);

he_return_code_t res = he_internal_flow_outside_packet_received(conn, packet, packet_max_length);
TEST_ASSERT_EQUAL(HE_SUCCESS, res);
Expand Down Expand Up @@ -693,11 +695,11 @@ void test_outside_data_received_disconnected(void) {

void test_he_internal_flow_process_message_too_small(void) {
conn->read_packet.packet_size = 0;
he_conn_set_ssl_error_Expect(conn, 0);

he_return_code_t res = he_internal_flow_process_message(conn);
TEST_ASSERT_EQUAL(HE_ERR_SSL_ERROR, res);
TEST_ASSERT_FALSE(conn->read_packet.has_packet);
TEST_ASSERT_EQUAL(0, conn->wolf_error);
}

void test_he_internal_flow_process_message_null_conn(void) {
Expand Down Expand Up @@ -757,6 +759,7 @@ void test_he_internal_flow_fetch_message_error_conn_closed(void) {
void test_he_internal_flow_fetch_message_error_non_fatal(void) {
wolfSSL_read_ExpectAndReturn(conn->wolf_ssl, conn->read_packet.packet, sizeof(conn->read_packet.packet), -1);
wolfSSL_get_error_ExpectAndReturn(conn->wolf_ssl, -1, SSL_ERROR_SSL);
he_conn_set_ssl_error_Expect(conn, SSL_ERROR_SSL);
conn->connection_type = HE_CONNECTION_TYPE_DATAGRAM;
int res = he_internal_flow_fetch_message(conn);
TEST_ASSERT_EQUAL(HE_ERR_SSL_ERROR_NONFATAL, res);
Expand All @@ -767,12 +770,12 @@ void test_he_internal_flow_fetch_message_error_non_fatal(void) {
void test_he_internal_flow_fetch_message_error_fatal(void) {
wolfSSL_read_ExpectAndReturn(conn->wolf_ssl, conn->read_packet.packet, sizeof(conn->read_packet.packet), -1);
wolfSSL_get_error_ExpectAndReturn(conn->wolf_ssl, -1, SSL_ERROR_SSL);
he_conn_set_ssl_error_Expect(conn, SSL_ERROR_SSL);
conn->connection_type = HE_CONNECTION_TYPE_STREAM;
int res = he_internal_flow_fetch_message(conn);
TEST_ASSERT_EQUAL(HE_ERR_SSL_ERROR, res);
TEST_ASSERT_FALSE(conn->read_packet.has_packet);
TEST_ASSERT_EQUAL(0, conn->read_packet.packet_size);
TEST_ASSERT_EQUAL(SSL_ERROR_SSL, conn->wolf_error);
}

// In general we try to avoid complex macros in libhelium, but these tests are so repetitive the
Expand Down
116 changes: 59 additions & 57 deletions test/he/test_wolf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

// Internal Mocks
#include "mock_plugin_chain.h"
#include "mock_flow.h"
#include "mock_conn.h"

uint8_t *packet = NULL;
uint8_t *buffer = NULL;
Expand Down Expand Up @@ -114,20 +116,20 @@ he_return_code_t outside_write_return_failure_on_third_call(he_conn_t *conn1, ui
}
}

void assert_standard_header(uint8_t write_buffer[]) {
TEST_ASSERT_EQUAL_CHAR('H', write_buffer[0]);
TEST_ASSERT_EQUAL_CHAR('e', write_buffer[1]);
void assert_standard_header(uint8_t buffer[]) {
TEST_ASSERT_EQUAL_CHAR('H', buffer[0]);
TEST_ASSERT_EQUAL_CHAR('e', buffer[1]);
}

void assert_standard_version(uint8_t write_buffer[]) {
TEST_ASSERT_EQUAL(HE_WIRE_MAXIMUM_PROTOCOL_MAJOR_VERSION, write_buffer[2]);
TEST_ASSERT_EQUAL(HE_WIRE_MAXIMUM_PROTOCOL_MINOR_VERSION, write_buffer[3]);
void assert_standard_version(uint8_t buffer[]) {
TEST_ASSERT_EQUAL(HE_WIRE_MAXIMUM_PROTOCOL_MAJOR_VERSION, buffer[2]);
TEST_ASSERT_EQUAL(HE_WIRE_MAXIMUM_PROTOCOL_MINOR_VERSION, buffer[3]);
}

void assert_standard_reserved_section(uint8_t write_buffer[]) {
TEST_ASSERT_EQUAL(0x00, conn->write_buffer[5]);
TEST_ASSERT_EQUAL(0x00, conn->write_buffer[6]);
TEST_ASSERT_EQUAL(0x00, conn->write_buffer[7]);
void assert_standard_reserved_section(uint8_t buffer[]) {
TEST_ASSERT_EQUAL(0x00, buffer[5]);
TEST_ASSERT_EQUAL(0x00, buffer[6]);
TEST_ASSERT_EQUAL(0x00, buffer[7]);
}

thread_local char* cur_packet;
Expand Down Expand Up @@ -213,15 +215,15 @@ void test_write_create_packet(void) {
TEST_ASSERT_EQUAL(test_packet_size, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &write_buffer[8], sizeof(conn->session_id));

// Test packet is unchanged
TEST_ASSERT_EQUAL_MEMORY(packet, conn->write_buffer + sizeof(he_wire_hdr_t), test_packet_size);
TEST_ASSERT_EQUAL_MEMORY(packet, write_buffer + sizeof(he_wire_hdr_t), test_packet_size);

// Test that the callback is set correctly
TEST_ASSERT_EQUAL(outside_write_test, conn->outside_write_cb);
Expand All @@ -234,7 +236,7 @@ void test_write_packet_too_big(void) {
TEST_ASSERT_EQUAL(WOLFSSL_CBIO_ERR_GENERAL, res1);

// Ensure the write buffer wasn't touched
TEST_ASSERT_EQUAL(0, conn->write_buffer[0]);
TEST_ASSERT_EQUAL(0, write_buffer[0]);
}

void test_write_context_gets_passed(void) {
Expand All @@ -247,32 +249,32 @@ void test_write_context_gets_passed(void) {
void test_internal_pkt_header_writer(void) {
conn->session_id = 0x1234567891234567;

int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)conn->write_buffer);
int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)write_buffer);
TEST_ASSERT_EQUAL(HE_SUCCESS, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &write_buffer[8], sizeof(conn->session_id));
}

void test_internal_pkt_header_writer_aggressive_mode(void) {
conn->session_id = 0x1234567891234567;
conn->use_aggressive_mode = true;

int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)conn->write_buffer);
int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)write_buffer);
TEST_ASSERT_EQUAL(HE_SUCCESS, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x01, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x01, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &write_buffer[8], sizeof(conn->session_id));
}

void test_internal_pkt_header_writer_disabled_roaming_sessions(void) {
Expand All @@ -281,19 +283,19 @@ void test_internal_pkt_header_writer_disabled_roaming_sessions(void) {
// Setting this manually so that these tests don't have a dependency on conn
conn->disable_roaming_connections = true;

int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)conn->write_buffer);
int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)write_buffer);
TEST_ASSERT_EQUAL(HE_SUCCESS, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&temp_session, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&temp_session, &write_buffer[8], sizeof(conn->session_id));
}
void test_internal_pkt_header_various_nulls(void) {
int res1 = he_internal_write_packet_header(NULL, (he_wire_hdr_t *)conn->write_buffer);
int res1 = he_internal_write_packet_header(NULL, (he_wire_hdr_t *)write_buffer);
int res2 = he_internal_write_packet_header(conn, NULL);
int res3 = he_internal_write_packet_header(NULL, NULL);

Expand All @@ -305,16 +307,16 @@ void test_internal_pkt_header_various_nulls(void) {
void test_internal_pkt_header_writer_pending_session(void) {
conn->session_id = 0x1234567891234567;
conn->pending_session_id = 0x9876543219876543;
int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)conn->write_buffer);
int res1 = he_internal_write_packet_header(conn, (he_wire_hdr_t *)write_buffer);
TEST_ASSERT_EQUAL(HE_SUCCESS, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->pending_session_id, &conn->write_buffer[8],
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->pending_session_id, &write_buffer[8],
sizeof(conn->session_id));
}

Expand All @@ -329,15 +331,15 @@ void test_write_dont_explode_if_not_write_cb_set(void) {
TEST_ASSERT_EQUAL(test_packet_size, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_version(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_version(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &write_buffer[8], sizeof(conn->session_id));

// Test packet is unchanged
TEST_ASSERT_EQUAL_MEMORY(packet, conn->write_buffer + sizeof(he_wire_hdr_t), test_packet_size);
TEST_ASSERT_EQUAL_MEMORY(packet, write_buffer + sizeof(he_wire_hdr_t), test_packet_size);

// Test that the callback is set correctly
TEST_ASSERT_NULL(conn->outside_write_cb);
Expand All @@ -354,17 +356,17 @@ void test_write_accepts_conn_version(void) {
TEST_ASSERT_EQUAL(test_packet_size, res1);

// Test we have a valid helium header
assert_standard_header(conn->write_buffer);
assert_standard_reserved_section(conn->write_buffer);
assert_standard_header(write_buffer);
assert_standard_reserved_section(write_buffer);

TEST_ASSERT_EQUAL(0xFF, conn->write_buffer[2]);
TEST_ASSERT_EQUAL(0x99, conn->write_buffer[3]);
TEST_ASSERT_EQUAL(0xFF, write_buffer[2]);
TEST_ASSERT_EQUAL(0x99, write_buffer[3]);

TEST_ASSERT_EQUAL(0x00, conn->write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &conn->write_buffer[8], sizeof(conn->session_id));
TEST_ASSERT_EQUAL(0x00, write_buffer[4]);
TEST_ASSERT_EQUAL_MEMORY(&conn->session_id, &write_buffer[8], sizeof(conn->session_id));

// Test packet is unchanged
TEST_ASSERT_EQUAL_MEMORY(packet, conn->write_buffer + sizeof(he_wire_hdr_t), test_packet_size);
TEST_ASSERT_EQUAL_MEMORY(packet, write_buffer + sizeof(he_wire_hdr_t), test_packet_size);
}

void test_aggressive_mode_is_off_write_callback_called_once_when_online(void) {
Expand Down Expand Up @@ -517,7 +519,7 @@ void test_tls_write_simple(void) {

// Make sure it sent all the data
TEST_ASSERT_EQUAL(test_packet_size, res1);
TEST_ASSERT_EQUAL_MEMORY(packet, &conn->write_buffer[0], test_packet_size);
TEST_ASSERT_EQUAL_MEMORY(packet, &write_buffer[0], test_packet_size);
}

void test_tls_write_attemps_lots_of_data_but_only_write_our_buffer_size(void) {
Expand All @@ -529,7 +531,7 @@ void test_tls_write_attemps_lots_of_data_but_only_write_our_buffer_size(void) {

// Make sure it sent only HE_MAX_WIRE_MTU worth of data and told wolf just that many
TEST_ASSERT_EQUAL(HE_MAX_WIRE_MTU, res1);
TEST_ASSERT_EQUAL_MEMORY(packet, &conn->write_buffer[0], HE_MAX_WIRE_MTU);
TEST_ASSERT_EQUAL_MEMORY(packet, &write_buffer[0], HE_MAX_WIRE_MTU);
}

void test_plugin_drop_results_in_no_write_tls(void) {
Expand Down

0 comments on commit c654796

Please sign in to comment.