From 84d15331c2e73d53cf46d6f47b71afd70243e97f Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 22 Feb 2025 21:01:37 +0100 Subject: [PATCH] test_connection: test handling of (lost) data packets --- impl/test/unittest/test_connection.c | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/impl/test/unittest/test_connection.c b/impl/test/unittest/test_connection.c index 060d8d8..23c9343 100644 --- a/impl/test/unittest/test_connection.c +++ b/impl/test/unittest/test_connection.c @@ -233,5 +233,57 @@ int main(void) ASSERT_STATE(expected_state, conn); + LOG(LVL_INFO, ">>> Checking data packet reception and lost packet handling."); + LOG(LVL_INFO, "Generating a burst of 5 packets where the 4th is lost."); + + for(size_t i = 0; i < 5; i++) { + // build connection parameters packet + header.dst_addr = dut_address; + header.src_addr = peer_address; + header.msg_type = L2_MSG_TYPE_DATA; + header.rx_seq_nr = 0; + header.tx_seq_nr = i+1; // 0 were Connection Parameters, 1 is the first data packet + header.tx_request = (i == 4); + + static_payload[0] = L2_PAYLOAD_TYPE_IPV4; + static_payload[1] = i; + static_payload[i+2] = i; + + payload_len = i+3; + + packet_len = layer2_encode_packet(&header, static_payload, payload_len, packet_buf, sizeof(packet_buf)); + + if(i == 3) { + // packet 4 is corrupted + packet_buf[3] = 0x42; + } + + result_t result = connection_handle_packet(&conn, packet_buf, packet_len, &data_packet, &tx_req_rcvd); + + const static result_t EXPECTED_RESULTS[5] = { + OK, OK, OK, ERR_INTEGRITY, ERR_SEQUENCE + }; + + ASSERT_RESULT(EXPECTED_RESULTS[i], result); + + if(result == OK) { + LOG(LVL_DEBUG, "Checking decode result for packet %zu (%zu bytes)", i, payload_len-1); + // check that payload length and type are decoded correctly + ASSERT_EQUAL_UINT(payload_len-1, data_packet.payload_len); + ASSERT_EQUAL_UINT(L2_PAYLOAD_TYPE_IPV4, data_packet.payload_type); + + // verify the complete packet data + for(size_t k = 0; k < payload_len-1; k++) { + ASSERT_EQUAL_UINT(static_payload[k+1], data_packet.payload[k]); + } + } + } + + // after the connection was accepted, the connection must switch to established state. + + expected_state.next_expected_seq = 4; + + ASSERT_STATE(expected_state, conn); + LOG(LVL_INFO, ">>> All assertions successful."); }