d8d8fbe081
90cc9a7d 把旧自研协议层(iec104_asdu/iec104_link/iec104_pts)整体退役
时,连带删掉了 test/host/iec104 与 test/host/iec104_serv 两套单测,却
没给留下来的代码补替代测试,也没同步覆盖率基线。结果 test-coverage
门禁(manual 阶段、远端 CI 强制)一直是红的:
[REGRESS] 总计: 已覆盖行 8848 -> 8052
[REGRESS] App/task/iec104_serv.c 基线里在测,本次未出现在任何单测编译
单元里
[REGRESS] 模块 iec104 / iec104_serv: 基线里有,本次未跑到
两个模块里约 180 行现役代码一行主机单测都没有:104 的 [comm] 段存取
(iec104_comm.c)与现役服务层(iec104_serv.c)。本笔把它们补回来。
新增:
test/host/iec104_comm/ 50 用例,iec104_comm.c 50/50 行
test/host/iec104_serv/ 50 用例,iec104_serv.c 102/102 行
两个套件都 #include 真实生产源(不是测桩),依赖全部走受控 stub。
同时修两个门禁自身的问题:
一、纯 Python 套件被误判为"缺 BUILD"
iec104_dtu/run_unit_test.py 是独立 oracle/解码器测试,只有一个 unittest
模块、不编任何 C 文件,天然没有 TestBuild。覆盖率脚本却把它当 FAIL,
于是基线里被写进一条 ok:false —— 等于把已知失败烤进台账,下次谁跑都
还是红。改成显式标记 COVERAGE_SKIP:有标记即记入 no_coverage_suites、
不计入 modules/modules_total,控制台打 SKIP。没有标记又没有 BUILD 的
运行器仍然 FAIL,严格性不放宽。
刻意不用"目录里没有 .c 就跳过"这类启发式:那会把真丢了 BUILD 的 C 套件
静默洗成绿灯,正是这道门禁要拦的东西。
二、该运行器缺 sys.path 引导
它 from oracle import ... 但没把自己目录加进 sys.path。直接跑能过
(sys.path[0] 是脚本目录),被 importlib 加载就 No module named
'oracle' —— 覆盖率脚本走的正是后者。补上后两种加载方式都过。
基线在全绿状态重建:modules_total = modules_ok = 49,没有任何 ok:false
条目,iec104_dtu 移入 no_coverage_suites;连跑两次全量都 RC=0 且汇总行
逐字一致(8204/9185 行、622/679 函数,suites 49/49)。
测试有牙(源变异实测,每次改完按字节还原并校验 sha 一致):
iec104_comm.c CA 下限 < 改 <= / PORT 上限 > 改 >= / ca_set 不置脏
-> 三条全部 KILLED
iec104_serv.c carry 饱和值减一 / 回绕判定 < 改 <= / carry += 改 =
-> 三条全部 KILLED
最后一条边界判据 `carry > SAT - now` 改 `>=` 经穷举 20 万组输入证明是
等价变异(两个分支返回值相同),没有假装能区分它,只在测试注释里写明。
顺带把 s_monotonic 的用例从 6 条扩到 10 条:补 now == last 不算回绕、
饱和后 carry 恰好等于 SAT、以及 carry + now == SAT-1 不被提前饱和。
只断言返回值不看内部状态时,前一版测试对两个真变异是无感的。
验证:pre-commit 9 Passed / 0 Failed;manual 阶段 test-coverage 与
iec104-tcp(13/13 真实 TCP 回环)均 Passed;新旧文件字节体检 BOM=False
CR=0 U+FFFD=0。
390 lines
14 KiB
C
390 lines
14 KiB
C
/*
|
|
* IEC104 service adapter host unit test.
|
|
* Compiles the real App/task/iec104_serv.c into this TU. All collaborators are
|
|
* controlled stubs: core lifecycle, source binding, measurement snapshot,
|
|
* calibration flags, net generation, event flags and task watchdog.
|
|
*/
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static int s_total = 0, s_passed = 0, s_failed = 0;
|
|
#define CHECK(cond, name) do { \
|
|
s_total++; \
|
|
if (cond) { s_passed++; printf(" [PASS] %s\n", name); } \
|
|
else { s_failed++; printf(" [FAIL] %s (line %d)\n", name, __LINE__); } \
|
|
} while (0)
|
|
|
|
#include "cmsis_os2.h"
|
|
#include "sys_events.h"
|
|
#include "net_socket.h"
|
|
#include "cfg_file.h"
|
|
|
|
static int g_cfg_register_calls, g_cfg_save_result, g_cfg_save_calls;
|
|
int cfg_file_register(const cfg_section_reg_t *reg) { (void)reg; g_cfg_register_calls++; return 0; }
|
|
int cfg_file_save(void) { g_cfg_save_calls++; return g_cfg_save_result; }
|
|
|
|
int g_i104_info_count = 0;
|
|
int g_i104_error_count = 0;
|
|
|
|
/* ---------- CMSIS/RTOS fake ---------- */
|
|
osEventFlagsId_t sys_evtHandle = (osEventFlagsId_t)1;
|
|
static uint32_t g_tick;
|
|
static uint32_t g_ready_wait_result;
|
|
static uint32_t g_ready_wait_options;
|
|
static uint32_t g_events;
|
|
|
|
uint32_t osKernelGetTickCount(void) { return g_tick; }
|
|
uint32_t osEventFlagsWait(osEventFlagsId_t id, uint32_t flags,
|
|
uint32_t options, uint32_t timeout)
|
|
{
|
|
(void)id; (void)flags; (void)timeout;
|
|
g_ready_wait_options = options;
|
|
return g_ready_wait_result;
|
|
}
|
|
uint32_t osEventFlagsSet(osEventFlagsId_t id, uint32_t flags)
|
|
{
|
|
(void)id;
|
|
g_events |= flags;
|
|
return 0u;
|
|
}
|
|
|
|
/* ---------- production headers, then production source ---------- */
|
|
#include "iec104_comm.c"
|
|
#include "iec104_core.h"
|
|
#include "iec104_source.h"
|
|
#include "iec104_net_mcu.h"
|
|
#include "meas.h"
|
|
#include "adc_cal.h"
|
|
#include "ioboard_di.h"
|
|
#include "prot_set.h"
|
|
#include "run_par.h"
|
|
#include "task_watchdog.h"
|
|
#include "dbg_log.h"
|
|
|
|
/* ---------- collaborator stubs ---------- */
|
|
static uint32_t g_net_generation;
|
|
uint32_t net_get_generation(void) { return g_net_generation; }
|
|
|
|
static iec104_source_bindings_t g_bound;
|
|
static int g_core_start_result;
|
|
static int g_core_poll_result;
|
|
static iec104_core_status_t g_core_status;
|
|
static iec104_core_config_t g_last_core_cfg;
|
|
static int g_core_start_calls, g_core_poll_calls, g_core_stop_calls, g_bind_calls;
|
|
|
|
int iec104_core_start(const iec104_core_config_t *cfg)
|
|
{
|
|
g_core_start_calls++;
|
|
if (cfg != NULL) { g_last_core_cfg = *cfg; }
|
|
return g_core_start_result;
|
|
}
|
|
int iec104_core_poll(void) { g_core_poll_calls++; return g_core_poll_result; }
|
|
int iec104_core_stop(void) { g_core_stop_calls++; return 0; }
|
|
void iec104_core_get_status(iec104_core_status_t *status)
|
|
{
|
|
if (status != NULL) { *status = g_core_status; }
|
|
}
|
|
void iec104_source_bind(const iec104_source_bindings_t *bindings)
|
|
{
|
|
g_bind_calls++;
|
|
if (bindings != NULL) { g_bound = *bindings; }
|
|
}
|
|
|
|
static uint32_t g_rx_bytes;
|
|
void iec104_net_mcu_stats(uint32_t *rx_bytes)
|
|
{
|
|
if (rx_bytes != NULL) { *rx_bytes = g_rx_bytes; }
|
|
}
|
|
|
|
static meas_snap_t g_snap;
|
|
static int g_meas_result;
|
|
int meas_get(meas_snap_t *out)
|
|
{
|
|
if (out != NULL) { *out = g_snap; }
|
|
return g_meas_result;
|
|
}
|
|
static uint32_t g_cal_flags[ADC_SEM_COUNT];
|
|
uint32_t adc_cal_flags(adc_semantic_t sem)
|
|
{
|
|
if ((int)sem < 0 || (int)sem >= (int)ADC_SEM_COUNT) { return 0u; }
|
|
return g_cal_flags[sem];
|
|
}
|
|
|
|
static uint8_t g_prot_dirty, g_run_dirty;
|
|
uint8_t prot_set_dirty_peek(void) { return g_prot_dirty; }
|
|
uint8_t run_par_dirty_peek(void) { return g_run_dirty; }
|
|
|
|
static int g_wd_beats;
|
|
static task_wd_id_t g_last_wd;
|
|
void task_wd_beat(task_wd_id_t id) { g_wd_beats++; g_last_wd = id; }
|
|
|
|
#include "iec104_serv.c"
|
|
|
|
static void reset_all(void)
|
|
{
|
|
g_tick = 0u;
|
|
g_ready_wait_result = SYS_EVT_NET_READY;
|
|
g_ready_wait_options = 0u;
|
|
g_events = 0u;
|
|
g_net_generation = 0u;
|
|
g_core_start_result = 0;
|
|
g_core_poll_result = 0;
|
|
memset(&g_core_status, 0, sizeof(g_core_status));
|
|
memset(&g_last_core_cfg, 0, sizeof(g_last_core_cfg));
|
|
g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0;
|
|
g_rx_bytes = 0u;
|
|
memset(&g_snap, 0, sizeof(g_snap));
|
|
g_meas_result = 0;
|
|
memset(g_cal_flags, 0, sizeof(g_cal_flags));
|
|
g_prot_dirty = g_run_dirty = 0u;
|
|
g_wd_beats = 0;
|
|
g_i104_info_count = g_i104_error_count = 0;
|
|
g_cfg_register_calls = g_cfg_save_calls = 0;
|
|
g_cfg_save_result = 0;
|
|
(void)iec104_comm_init();
|
|
/* reset service static owner state via a fresh start cycle */
|
|
s_started = 0u; s_gen = 0u; s_retry_tick = 0u; s_dirty_next = 0u;
|
|
s_last_active = 0u; s_link_run = 0u;
|
|
memset(&s_stats, 0, sizeof(s_stats));
|
|
s_last_conn = s_carry_conn = 0u;
|
|
s_last_apdu = s_carry_apdu = 0u;
|
|
s_last_txfail = s_carry_txfail = 0u;
|
|
s_last_rx = s_carry_rx = 0u;
|
|
g_tick = I104_START_RETRY_MS + 1u;
|
|
iec104_serv_net_tick();
|
|
g_tick = 0u;
|
|
g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0;
|
|
g_wd_beats = 0;
|
|
g_events = 0u;
|
|
g_i104_info_count = g_i104_error_count = 0;
|
|
}
|
|
|
|
static void tc_monotonic(void)
|
|
{
|
|
uint32_t last = 100u, carry = 7u;
|
|
|
|
/* 正序:不跨回绕,carry 与 last 一起向后走。 */
|
|
CHECK(s_monotonic(150u, &last, &carry) == 157u, "monotonic normal increment");
|
|
CHECK(last == 150u && carry == 7u, "monotonic normal state");
|
|
|
|
/* 一次真回绕:now < last,把上一段的 last 折进 carry。 */
|
|
CHECK(s_monotonic(50u, &last, &carry) == 207u, "monotonic reset carries previous");
|
|
CHECK(last == 50u && carry == 157u, "monotonic reset state");
|
|
|
|
/* now == last 不算回绕:时间没有倒退,只是原地等了一拍。
|
|
* 这一档单独写断言,是因为 `now < last` 误写成 `now <= last` 时
|
|
* 返回值会多出一整个 last(本例 5 -> 10),而只看"单调增"是看不出来的。 */
|
|
last = 5u; carry = 0u;
|
|
CHECK(s_monotonic(5u, &last, &carry) == 5u, "monotonic same tick does not wrap");
|
|
CHECK(last == 5u && carry == 0u, "monotonic same tick leaves carry alone");
|
|
|
|
/* 折进 last 会溢出时,carry 直接钉到 SAT —— 断言 carry 本身而不只是
|
|
* 返回值:把 `*carry = I104_STAT_SAT` 改成 SAT-1,返回值仍是 SAT
|
|
* (下一句的饱和分支照样命中),只有 carry 的内部状态能抓出来。 */
|
|
last = 20u; carry = I104_STAT_SAT - 5u;
|
|
CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT, "monotonic carry saturation");
|
|
CHECK(carry == I104_STAT_SAT, "monotonic carry pinned exactly at SAT");
|
|
|
|
/* 已经饱和之后再进来,仍然饱和且不再叠加。 */
|
|
last = 10u; carry = I104_STAT_SAT;
|
|
CHECK(s_monotonic(15u, &last, &carry) == I104_STAT_SAT, "monotonic already saturated");
|
|
CHECK(carry == I104_STAT_SAT && last == 15u, "monotonic stays saturated");
|
|
|
|
/* 32 位 tick 自然回绕(now 从 0xFFFFFFF0 走到 5)必须被判成回绕并饱和,
|
|
* 不能算成"倒退了 4G 多"。 */
|
|
last = 0xFFFFFFF0u; carry = 0u;
|
|
CHECK(s_monotonic(5u, &last, &carry) == I104_STAT_SAT,
|
|
"monotonic u32 wrap saturates");
|
|
|
|
/* 边界对照组:carry + now 恰好等于 SAT-1 时**不许**提前饱和。
|
|
* 饱和判据是 `carry > SAT - now`,等号留给正常的加法。
|
|
* 说明:carry + now 恰等于 SAT 的那一档与饱和分支返回值相同,
|
|
* 属等价变异,这里不假装能区分,只用 SAT-1 这一档卡住"提前饱和"。 */
|
|
last = 10u; carry = I104_STAT_SAT - 11u;
|
|
CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT - 1u,
|
|
"monotonic sum SAT-1 is not saturated early");
|
|
}
|
|
|
|
static void tc_measure(void)
|
|
{
|
|
float value = -1.0f;
|
|
uint8_t quality = 0xFFu;
|
|
|
|
reset_all();
|
|
memset(&g_snap, 0, sizeof(g_snap));
|
|
g_snap.yc[ADC_SEM_UB] = 12.5f;
|
|
g_snap.qds[ADC_SEM_UB] = 0x20u; /* NT only */
|
|
g_cal_flags[ADC_SEM_UB] = ADC_CAL_F_VALID;
|
|
CHECK(g_bound.measurement != NULL && g_bound.signal != NULL,
|
|
"source bindings installed after core start");
|
|
CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true,
|
|
"mapped UB source available");
|
|
CHECK(value == 12.5f && quality == 0x20u,
|
|
"UB value and quality copied from snapshot");
|
|
|
|
g_cal_flags[ADC_SEM_UB] = 0u;
|
|
CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true,
|
|
"uncalibrated UB still reports snapshot");
|
|
CHECK(quality == 0xA0u, "uncalibrated UB gets IV bit");
|
|
|
|
g_snap.freq_valid = 1u;
|
|
g_snap.freq_hz = 50.01f;
|
|
CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == true,
|
|
"frequency source available");
|
|
CHECK(value == 50.01f && quality == 0u, "frequency value and clean quality");
|
|
|
|
g_snap.freq_valid = 0u;
|
|
CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == false,
|
|
"invalid frequency is unavailable");
|
|
CHECK(g_bound.measurement(NULL, 0x9999u, &value, &quality) == false,
|
|
"unmapped source is unavailable");
|
|
CHECK(g_bound.measurement(NULL, 0x9605u, NULL, &quality) == false,
|
|
"NULL value output rejected");
|
|
CHECK(g_bound.measurement(NULL, 0x9605u, &value, NULL) == false,
|
|
"NULL quality output rejected");
|
|
g_meas_result = -2;
|
|
CHECK(g_bound.measurement(NULL, 0x9605u, &value, &quality) == false,
|
|
"no published snapshot is unavailable");
|
|
g_meas_result = 0;
|
|
|
|
CHECK(g_bound.signal(NULL, 0x9141u, &quality, &quality) == false,
|
|
"signal source always unavailable");
|
|
}
|
|
|
|
static void tc_stats_and_link(void)
|
|
{
|
|
iec104_serv_stats_t stats;
|
|
uint8_t link;
|
|
|
|
reset_all();
|
|
memset(&stats, 0x55, sizeof(stats));
|
|
iec104_serv_get_stats(&stats);
|
|
CHECK(stats.conn_up == 0u && stats.apdu_tx == 0u &&
|
|
stats.tx_fail == 0u && stats.rx_bytes == 0u,
|
|
"get_stats fills four fields");
|
|
iec104_serv_get_stats(NULL);
|
|
CHECK(true, "get_stats NULL is harmless");
|
|
link = iec104_serv_link_run();
|
|
CHECK(link == 0u, "link_run starts at zero");
|
|
|
|
g_tick = I104_START_RETRY_MS;
|
|
g_core_status.connections = 3u;
|
|
g_core_status.written_i_frames = 4u;
|
|
g_core_status.frame_failures = 5u;
|
|
g_core_status.active = 1u;
|
|
g_rx_bytes = 6u;
|
|
iec104_serv_net_tick();
|
|
iec104_serv_get_stats(&stats);
|
|
CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u &&
|
|
stats.tx_fail == 5u && stats.rx_bytes == 6u,
|
|
"stats mirror one core poll");
|
|
CHECK(iec104_serv_link_run() == 1u, "link_run mirrors active");
|
|
CHECK(s_last_active == 1u, "active edge recorded");
|
|
|
|
g_core_status.connections = 0u;
|
|
g_core_status.written_i_frames = 0u;
|
|
g_core_status.frame_failures = 0u;
|
|
g_core_status.active = 0u;
|
|
g_rx_bytes = 0u;
|
|
g_tick += 1u;
|
|
iec104_serv_net_tick();
|
|
iec104_serv_get_stats(&stats);
|
|
CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u &&
|
|
stats.tx_fail == 5u && stats.rx_bytes == 6u,
|
|
"stats stay monotonic when sources reset");
|
|
CHECK(iec104_serv_link_run() == 0u, "link_run follows active low");
|
|
}
|
|
|
|
static void tc_tick_control(void)
|
|
{
|
|
iec104_serv_stats_t stats;
|
|
int wd_before;
|
|
|
|
reset_all();
|
|
g_ready_wait_result = osFlagsError;
|
|
g_tick = 1000u;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_wd_beats == 1 && g_last_wd == TASK_WD_IEC104,
|
|
"watchdog beat precedes NET_READY return");
|
|
CHECK((g_ready_wait_options & osFlagsNoClear) != 0u,
|
|
"NET_READY is queried without clearing");
|
|
CHECK(g_core_start_calls == 0 && g_core_poll_calls == 0,
|
|
"network not ready: no core calls");
|
|
|
|
reset_all();
|
|
g_ready_wait_result = SYS_EVT_NET_READY;
|
|
g_tick = I104_START_RETRY_MS;
|
|
g_core_start_result = -1;
|
|
s_started = 0u; s_retry_tick = 0u;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_core_start_calls == 1 && g_bind_calls == 0,
|
|
"failed start does not bind sources");
|
|
g_core_start_result = 0;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_core_start_calls == 1, "start retry is throttled");
|
|
g_tick += I104_START_RETRY_MS;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_core_start_calls == 2 && g_bind_calls == 1,
|
|
"retry after throttle binds sources once");
|
|
CHECK(strcmp(g_last_core_cfg.bind_address, "0.0.0.0") == 0,
|
|
"core bind address is wildcard");
|
|
CHECK(g_last_core_cfg.port == iec104_comm_port() &&
|
|
g_last_core_cfg.common_address == iec104_comm_ca(),
|
|
"core config uses live comm getters");
|
|
CHECK(g_core_poll_calls > 0, "successful start allows polling in later ticks");
|
|
|
|
reset_all();
|
|
g_tick = I104_START_RETRY_MS + 1u;
|
|
g_net_generation = 9u;
|
|
iec104_serv_net_tick(); /* generation change: stop + restart_core (throttled) */
|
|
g_tick += I104_START_RETRY_MS;
|
|
iec104_serv_net_tick(); /* retry window elapsed: core starts again + rebind */
|
|
CHECK(g_core_stop_calls >= 1 && g_core_start_calls >= 1,
|
|
"generation change restarts core without depending on stop status");
|
|
CHECK(g_bind_calls >= 1, "generation restart rebinds sources");
|
|
CHECK(g_i104_info_count >= 1, "generation change leaves an info trace");
|
|
|
|
reset_all();
|
|
g_tick = I104_START_RETRY_MS + 1u;
|
|
iec104_serv_net_tick();
|
|
g_tick += 1u;
|
|
g_core_poll_result = -1;
|
|
wd_before = g_wd_beats;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_core_poll_calls == 2 && g_i104_error_count >= 1,
|
|
"poll failure logs and restarts");
|
|
iec104_serv_get_stats(&stats);
|
|
CHECK(stats.conn_up == 0u, "poll failure does not publish stats");
|
|
|
|
reset_all();
|
|
g_tick = I104_START_RETRY_MS;
|
|
iec104_serv_net_tick();
|
|
g_prot_dirty = 1u;
|
|
g_run_dirty = 1u;
|
|
g_tick = s_dirty_next;
|
|
iec104_serv_net_tick();
|
|
CHECK((g_events & SYS_EVT_PROT_SET_SAVE) != 0u,
|
|
"protection dirty posts save event on deadline");
|
|
CHECK((g_events & SYS_EVT_RUN_PAR_SAVE) != 0u,
|
|
"run-param dirty posts save event on deadline");
|
|
g_events = 0u;
|
|
g_tick += 1u;
|
|
iec104_serv_net_tick();
|
|
CHECK(g_events == 0u, "no duplicate dirty event before next deadline");
|
|
(void)wd_before;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
printf("=== IEC104 Service Host Unit Tests ===\n");
|
|
tc_monotonic();
|
|
tc_measure();
|
|
tc_stats_and_link();
|
|
tc_tick_control();
|
|
printf("=== IEC104 Serv Tests: %d/%d PASSED (failed=%d) ===\n",
|
|
s_passed, s_total, s_failed);
|
|
return (s_failed == 0) ? 0 : 1;
|
|
}
|