Files
DTU-HMI/tests/test_p0_remote_display.c

78 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.
/* P0 测试remoteDisplay 协议核心
* 目标:
* - 校验 CRC 纯函数行为calc_crc
* - 校验 RemoDispBus 帧解析边界parse_frame
*/
#include <stdint.h>
#include "test_common.h"
/* 直接包含 .c 以访问 static 函数calc_crc / parse_frame */
#include "../src/remoteDisplay.c"
/* 用例1CRC 计算
* - 空数据长度应返回 0
* - 常规 payload 应返回逐字节异或结果
*/
static int test_calc_crc(void)
{
const uint8_t payload[] = {0x12, 0x34, 0x56};
ASSERT_EQ_INT(0x00, calc_crc(NULL, 0));
ASSERT_EQ_INT((0x12 ^ 0x34 ^ 0x56), calc_crc(payload, 3));
return 0;
}
/* 用例2合法帧解析
* 验证:
* - parse_frame 返回成功
* - cmd/data/data_len/consume 与期望一致
*/
static int test_parse_frame_ok(void)
{
uint8_t cmd = 0;
const uint8_t* data = NULL;
unsigned int data_len = 0;
unsigned int consume = 0;
const uint8_t payload[] = {0x10, 0x20};
const uint8_t frame[] = {0xAA, 0x03, 0x00, 0x02, 0x10, 0x20, (uint8_t)(0x10 ^ 0x20)};
ASSERT_EQ_INT(1, parse_frame(frame, sizeof(frame), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0x03, cmd);
ASSERT_EQ_INT(2, (int)data_len);
ASSERT_EQ_INT((int)sizeof(frame), (int)consume);
ASSERT_EQ_INT(payload[0], data[0]);
ASSERT_EQ_INT(payload[1], data[1]);
return 0;
}
/* 用例3非法输入集合
* - TAG 非客户端 TAG
* - 帧长度不足(截断)
* - CRC 错误
* 期望均解析失败(返回 0
*/
static int test_parse_frame_invalid_cases(void)
{
uint8_t cmd = 0;
const uint8_t* data = NULL;
unsigned int data_len = 0;
unsigned int consume = 0;
const uint8_t bad_tag[] = {0xBB, 0x03, 0x00, 0x00, 0x00};
const uint8_t short_buf[] = {0xAA, 0x03, 0x00, 0x01};
const uint8_t bad_crc[] = {0xAA, 0x02, 0x00, 0x01, 0x55, 0x00};
ASSERT_EQ_INT(0, parse_frame(bad_tag, sizeof(bad_tag), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0, parse_frame(short_buf, sizeof(short_buf), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0, parse_frame(bad_crc, sizeof(bad_crc), &cmd, &data, &data_len, &consume));
return 0;
}
/* 测试入口:按顺序执行各子用例,任一失败即返回非 0 */
int main(void)
{
if (test_calc_crc() != 0) return 1;
if (test_parse_frame_ok() != 0) return 1;
if (test_parse_frame_invalid_cases() != 0) return 1;
return 0;
}