esp_alox/tests/test_parser.c
2025-07-22 14:22:49 +02:00

86 lines
2.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Unity/src/unity.h"
#include "message_parser.h"
#include <stdint.h>
void setUp(void) {} // optional
void tearDown(void) {} // optional
// Gültige Nachricht mit Payload
void test_valid_message_parses_correctly(void) {
struct MessageRecieve mr = {
.state = WaitingForStartByte,
.messageid = 0,
.index = 0,
.checksum = 0,
};
uint8_t msg[] = {0xAA, 0x01, 0x03, 0x01, 0x02, 0x03, 0x00};
msg[6] = msg[1] ^ msg[2] ^ msg[3] ^ msg[4] ^ msg[5]; // korrekte Checksumme
for (uint8_t i = 0; i < sizeof(msg); ++i)
parse_byte(&mr, msg[i]);
TEST_ASSERT_EQUAL_UINT8(1, mr.messageid);
TEST_ASSERT_EQUAL_UINT8(3, mr.length);
uint8_t expected[] = {1, 2, 3};
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, mr.message, 3);
}
// Ungültige Checksumme Zustand wird zurückgesetzt
void test_invalid_checksum_resets_state(void) {
struct MessageRecieve mr = {
.state = WaitingForStartByte,
.messageid = 0,
.index = 0,
.checksum = 0,
};
uint8_t msg[] = {
0xAA, 0x02, 0x02,
0x10, 0x20, 0x00}; // falsche Checksumme (korr. wäre 0x10 ^ 0x20 ^ 2 ^ 2)
for (uint8_t i = 0; i < sizeof(msg); ++i)
parse_byte(&mr, msg[i]);
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
}
// Kein Startbyte Nachricht ignorieren
void test_no_startbyte_ignored(void) {
struct MessageRecieve mr = {
.state = WaitingForStartByte, .index = 0, .checksum = 0};
uint8_t msg[] = {2, 0x10, 0x20, 0x30}; // kein Startbyte
for (uint8_t i = 0; i < sizeof(msg); ++i)
parse_byte(&mr, msg[i]);
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
TEST_ASSERT_EQUAL_UINT8(0, mr.index);
}
// Länge 0 gültige Nachricht ohne Payload
void test_zero_length_message(void) {
struct MessageRecieve mr = {
.state = WaitingForStartByte, .index = 0, .checksum = 0};
uint8_t msg[] = {0xAA, 3, 0, 0}; // Länge = 0, Checksumme = 3 ^ 0 = 3
msg[3] = msg[1] ^ msg[2];
for (uint8_t i = 0; i < sizeof(msg); ++i)
parse_byte(&mr, msg[i]);
TEST_ASSERT_EQUAL_UINT8(0, mr.index);
TEST_ASSERT_EQUAL_UINT8(3, mr.messageid);
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_valid_message_parses_correctly);
RUN_TEST(test_invalid_checksum_resets_state);
RUN_TEST(test_no_startbyte_ignored);
RUN_TEST(test_zero_length_message);
return UNITY_END();
}