/** * @file test_protocol.c * @brief 通信协议单元测试 */ #include #include #include #include /* 协议常量定义 */ #define FRAME_HEAD 0xAA #define FRAME_TAIL 0x55 #define PROTOCOL_MAX_DATA_LEN 1024 #define BOOT_UART_BUFFER_SIZE 2048 /* 命令定义 */ typedef enum { CMD_HANDSHAKE = 0x01, CMD_ERASE_FLASH = 0x02, CMD_WRITE_FLASH = 0x03, CMD_VERIFY_FLASH = 0x04, CMD_READ_FLASH = 0x05, CMD_JUMP_APP = 0x10, CMD_CHECK_APP = 0x11, CMD_RESET_TARGET = 0x12, CMD_ACK = 0x80, CMD_NACK = 0x81, } protocol_cmd_t; /* CRC16 计算 */ static uint16_t calc_crc16(const uint8_t *data, uint32_t len) { uint16_t crc = 0xFFFF; for (uint32_t i = 0; i < len; i++) { crc ^= data[i]; for (uint32_t j = 0; j < 8; j++) { if (crc & 1) { crc = (crc >> 1) ^ 0xA001; } else { crc >>= 1; } } } return crc; } /* 模拟发送帧 */ static int send_frame(protocol_cmd_t cmd, const uint8_t *data, uint16_t len, uint8_t *tx_buffer) { uint32_t tx_index = 0; tx_buffer[tx_index++] = FRAME_HEAD; tx_buffer[tx_index++] = cmd; tx_buffer[tx_index++] = (len >> 8) & 0xFF; tx_buffer[tx_index++] = len & 0xFF; if (data && len > 0) { memcpy(&tx_buffer[tx_index], data, len); tx_index += len; } uint16_t crc = calc_crc16(&tx_buffer[1], tx_index - 1); tx_buffer[tx_index++] = (crc >> 8) & 0xFF; tx_buffer[tx_index++] = crc & 0xFF; tx_buffer[tx_index++] = FRAME_TAIL; return tx_index; } /* 解析帧 */ static int parse_frame(uint8_t *buf, uint32_t len) { /* 验证帧头 */ if (buf[0] != FRAME_HEAD) return -1; /* 解析长度 */ uint16_t data_len = (buf[2] << 8) | buf[3]; /* 验证帧尾 */ uint32_t frame_end = 4 + data_len + 2; if (len < frame_end + 1) return -3; /* 数据不完整 */ if (buf[frame_end] != FRAME_TAIL) return -1; /* 验证 CRC */ uint16_t recv_crc = (buf[frame_end - 2] << 8) | buf[frame_end - 1]; uint16_t calc_crc = calc_crc16(&buf[1], frame_end - 3); if (recv_crc != calc_crc) return -2; return 0; } /* 测试宏 */ #define TEST_ASSERT(condition, message) \ do { \ if (!(condition)) { \ printf("[FAIL] %s\n", message); \ failures++; \ } else { \ printf("[PASS] %s\n", message); \ passes++; \ } \ } while(0) static int passes = 0; static int failures = 0; void test_send_handshake_frame(void) { uint8_t tx_buffer[64]; uint8_t version_info[8] = {0x01, 0x00, 0x01, 0x00, 'B', 'O', 'O', 'T'}; int len = send_frame(CMD_HANDSHAKE, version_info, sizeof(version_info), tx_buffer); TEST_ASSERT(len > 0, "Send handshake frame returns length"); TEST_ASSERT(tx_buffer[0] == FRAME_HEAD, "Frame head correct"); TEST_ASSERT(tx_buffer[1] == CMD_HANDSHAKE, "Command correct"); TEST_ASSERT(tx_buffer[len - 1] == FRAME_TAIL, "Frame tail correct"); } void test_send_frame_crc_verification(void) { uint8_t tx_buffer[64]; uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; int len = send_frame(CMD_WRITE_FLASH, data, sizeof(data), tx_buffer); /* 验证 CRC */ uint16_t recv_crc = (tx_buffer[len - 3] << 8) | tx_buffer[len - 2]; uint16_t calc_crc = calc_crc16(&tx_buffer[1], len - 5); TEST_ASSERT(recv_crc == calc_crc, "CRC verification"); } void test_parse_valid_frame(void) { uint8_t rx_buffer[64]; uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; int len = send_frame(CMD_READ_FLASH, data, sizeof(data), rx_buffer); int result = parse_frame(rx_buffer, len); TEST_ASSERT(result == 0, "Parse valid frame"); } void test_parse_invalid_header(void) { uint8_t rx_buffer[64]; uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; send_frame(CMD_READ_FLASH, data, sizeof(data), rx_buffer); rx_buffer[0] = 0xBB; /* 错误的帧头 */ int result = parse_frame(rx_buffer, 16); TEST_ASSERT(result == -1, "Parse invalid header"); } void test_parse_invalid_tail(void) { uint8_t rx_buffer[64]; uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; send_frame(CMD_READ_FLASH, data, sizeof(data), rx_buffer); /* 找到帧尾位置并修改 */ for (int i = 0; i < 64; i++) { if (rx_buffer[i] == 0x55) { rx_buffer[i] = 0x66; break; } } int result = parse_frame(rx_buffer, 16); TEST_ASSERT(result == -1, "Parse invalid tail"); } void test_parse_invalid_crc(void) { uint8_t rx_buffer[64]; uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; send_frame(CMD_READ_FLASH, data, sizeof(data), rx_buffer); /* 篡改 CRC */ rx_buffer[6] ^= 0xFF; int result = parse_frame(rx_buffer, 16); TEST_ASSERT(result == -2, "Parse invalid CRC"); } void test_parse_incomplete_frame(void) { uint8_t rx_buffer[64]; uint8_t data[10] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; send_frame(CMD_WRITE_FLASH, data, sizeof(data), rx_buffer); /* 模拟接收不完整 */ int result = parse_frame(rx_buffer, 5); TEST_ASSERT(result == -3, "Parse incomplete frame"); } void test_frame_length_boundaries(void) { uint8_t tx_buffer[PROTOCOL_MAX_DATA_LEN + 16]; /* 测试最小帧(无数据) */ int min_len = send_frame(CMD_HANDSHAKE, NULL, 0, tx_buffer); TEST_ASSERT(min_len == 8, "Minimum frame length is 8"); /* 测试最大帧 */ uint8_t max_data[PROTOCOL_MAX_DATA_LEN]; memset(max_data, 0xAA, sizeof(max_data)); int max_len = send_frame(CMD_WRITE_FLASH, max_data, sizeof(max_data), tx_buffer); TEST_ASSERT(max_len == PROTOCOL_MAX_DATA_LEN + 8, "Maximum frame length"); } void test_empty_data_frame(void) { uint8_t tx_buffer[64]; int len = send_frame(CMD_CHECK_APP, NULL, 0, tx_buffer); TEST_ASSERT(len == 8, "Empty data frame length"); TEST_ASSERT(tx_buffer[2] == 0x00 && tx_buffer[3] == 0x00, "Length field is zero"); int result = parse_frame(tx_buffer, len); TEST_ASSERT(result == 0, "Parse empty data frame"); } void test_special_characters_in_data(void) { uint8_t tx_buffer[64]; /* 数据中包含帧头帧尾值 */ uint8_t data[8] = {0xAA, 0x11, 0x55, 0x22, 0xAA, 0x33, 0x55, 0x44}; int len = send_frame(CMD_WRITE_FLASH, data, sizeof(data), tx_buffer); int result = parse_frame(tx_buffer, len); TEST_ASSERT(result == 0, "Frame with special characters in data"); } int main(void) { printf("===========================================\n"); printf(" Protocol Module Unit Tests\n"); printf("===========================================\n\n"); printf("Running protocol tests...\n\n"); test_send_handshake_frame(); test_send_frame_crc_verification(); test_parse_valid_frame(); test_parse_invalid_header(); test_parse_invalid_tail(); test_parse_invalid_crc(); test_parse_incomplete_frame(); test_frame_length_boundaries(); test_empty_data_frame(); test_special_characters_in_data(); printf("\n===========================================\n"); printf("Test Summary:\n"); printf(" Passed: %d\n", passes); printf(" Failed: %d\n", failures); printf(" Total: %d\n", passes + failures); printf("===========================================\n"); return (failures == 0) ? 0 : 1; }