Files
smart-home-control-system/tests/test_runner.c
T
sambu 4b4b24ca4a fix(storage)+test: 修复设备表校验和可被绕过,登记 5 套新单元测试
- 缺陷(由测试 agent 用例 TC-FILE-005 发现):read_device 在读取后立刻把 name 末字节置 0,
  导致文件里该字节被篡改时校验和仍能对上,完整性校验形同虚设。
  修复:先按原始字节做校验和比对,通过后再统一做字段清洗(补 NUL 结尾)。
  实测:篡改 name 末字节的设备表现在被正确判为 SHS_ERR_PARSE。
- 测试期望修正:配置名称上限 32 字节(约 10 个汉字),超长应返回 SHS_ERR_RANGE
  而不是静默截断;补充正常长度用例。
- 登记 unit.branch / unit.loop / unit.bitops / unit.errors / unit.file 五套到 runner 与 CTest。
- ctest 8/8 通过。
2026-09-14 20:28:32 +08:00

110 lines
3.2 KiB
C

/**
* @file test_runner.c
* @brief 测试入口:按套件名选择运行,并与 CTest 集成
*
* 用法:
* ./shs_tests # 运行全部套件
* ./shs_tests --suite unit.string
* ./shs_tests --list # 列出全部套件
* ./shs_tests --no-color
*/
#include "framework/test_framework.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 各测试套件的声明(新增套件时在此与 g_suites 表中登记) */
void suite_unit_string(void);
void suite_unit_num(void);
void suite_unit_branch(void);
void suite_unit_loop(void);
void suite_unit_bitops(void);
void suite_unit_errors(void);
void suite_unit_file(void);
typedef struct {
const char *name; /* 套件名,与 CTest 中注册的名字一致 */
tf_suite_fn fn;
const char *desc; /* 教材知识点说明 */
} test_suite_t;
static const test_suite_t g_suites[] = {
{ "unit.string", suite_unit_string, "项目五 字符数组与字符串处理" },
{ "unit.num", suite_unit_num, "项目二 数据类型、运算符与类型转换" },
{ "unit.branch", suite_unit_branch, "项目三 分支结构与条件判断" },
{ "unit.loop", suite_unit_loop, "项目四 循环结构与嵌套" },
{ "unit.bitops", suite_unit_bitops, "项目九 位运算与位标志" },
{ "unit.errors", suite_unit_errors, "项目十 错误处理与错误码" },
{ "unit.file", suite_unit_file, "项目九 文件与配置存储" }
};
#define SUITE_COUNT ((int)(sizeof(g_suites) / sizeof(g_suites[0])))
static bool is_tty_stdout(void)
{
#ifdef _WIN32
return false;
#else
return true;
#endif
}
static void run_suite(const test_suite_t *suite)
{
printf("\n=== 套件 %s (%s) ===\n", suite->name, suite->desc);
tf_begin(suite->name);
suite->fn();
(void)tf_end();
}
int main(int argc, char **argv)
{
const char *only = NULL;
bool list_only = false;
bool use_color = is_tty_stdout();
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--suite") == 0 && i + 1 < argc) {
only = argv[++i];
} else if (strcmp(argv[i], "--list") == 0) {
list_only = true;
} else if (strcmp(argv[i], "--no-color") == 0) {
use_color = false;
} else {
fprintf(stderr, "未知参数: %s\n", argv[i]);
return 2;
}
}
tf_set_color(use_color);
if (list_only) {
for (int i = 0; i < SUITE_COUNT; i++) {
printf("%s\t%s\n", g_suites[i].name, g_suites[i].desc);
}
return 0;
}
int matched = 0;
for (int i = 0; i < SUITE_COUNT; i++) {
if (only != NULL && strcmp(only, g_suites[i].name) != 0) {
continue;
}
run_suite(&g_suites[i]);
matched++;
}
if (only != NULL && matched == 0) {
fprintf(stderr, "未找到套件: %s\n", only);
return 2;
}
printf("\n---------------- 测试汇总 ----------------\n");
printf("套件数 : %d\n", tf_suites_run);
printf("断言数 : %d\n", tf_total_checks);
printf("失败数 : %d\n", tf_total_failures);
printf("结果 : %s\n", (tf_total_failures == 0) ? "全部通过" : "存在失败");
printf("------------------------------------------\n");
return (tf_total_failures == 0) ? 0 : 1;
}