test(iec104): 补回 104 适配层与服务层的主机单测,覆盖率门禁转绿
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。
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* IEC104 [comm] segment host unit test.
|
||||
* Builds the real App/iec60870/104_port/iec104_comm.c into this translation unit
|
||||
* and fakes only the config engine boundary (cfg_file_register/cfg_file_save)
|
||||
* against the real App/cfg/cfg_file.h interface.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg_file.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)
|
||||
|
||||
/* Config engine fake, controlled by each test case. */
|
||||
static int g_save_result;
|
||||
static int g_register_calls;
|
||||
static int g_save_calls;
|
||||
static cfg_section_reg_t g_last_reg;
|
||||
|
||||
int cfg_file_save(void)
|
||||
{
|
||||
g_save_calls++;
|
||||
return g_save_result;
|
||||
}
|
||||
|
||||
int cfg_file_register(const cfg_section_reg_t *p_reg)
|
||||
{
|
||||
g_register_calls++;
|
||||
if (p_reg != NULL) {
|
||||
g_last_reg = *p_reg;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "iec104_comm.c"
|
||||
|
||||
static void reset_fakes(int save_result)
|
||||
{
|
||||
g_save_result = save_result;
|
||||
g_register_calls = 0;
|
||||
g_save_calls = 0;
|
||||
memset(&g_last_reg, 0, sizeof(g_last_reg));
|
||||
}
|
||||
|
||||
static void tc_init_defaults(void)
|
||||
{
|
||||
uint16_t ca, port;
|
||||
|
||||
reset_fakes(0);
|
||||
CHECK(iec104_comm_ca_set(1234) == 0, "set ca before init");
|
||||
CHECK(iec104_comm_port_set(4000) == 0, "set port before init");
|
||||
CHECK(iec104_comm_init() == 0, "init returns register result");
|
||||
CHECK(g_register_calls == 1, "init registers exactly once");
|
||||
CHECK(strcmp(g_last_reg.name, IEC104_COMM_SEC) == 0, "registered section name");
|
||||
CHECK(g_last_reg.p_data == &s_comm, "registered RAM copy pointer");
|
||||
CHECK(g_last_reg.p_defaults == &s_comm_rom, "registered defaults pointer");
|
||||
CHECK(g_last_reg.data_size == sizeof(iec104_comm_t), "registered data size");
|
||||
CHECK(g_last_reg.p_keys == s_comm_keys, "registered key table");
|
||||
CHECK(g_last_reg.key_count == 2u, "registered key count");
|
||||
CHECK(g_last_reg.p_cb == NULL, "no config callback");
|
||||
CHECK(s_comm_keys[0].type == CFG_VAL_UINT && s_comm_keys[0].len == 2u &&
|
||||
s_comm_keys[0].offset == offsetof(iec104_comm_t, ca), "ca key descriptor");
|
||||
CHECK(s_comm_keys[1].type == CFG_VAL_UINT && s_comm_keys[1].len == 2u &&
|
||||
s_comm_keys[1].offset == offsetof(iec104_comm_t, port), "port key descriptor");
|
||||
ca = iec104_comm_ca();
|
||||
port = iec104_comm_port();
|
||||
CHECK(ca == IEC104_COMM_DEF_CA, "init restores ca default");
|
||||
CHECK(port == IEC104_COMM_DEF_PORT, "init restores port default");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "init clears dirty flag");
|
||||
}
|
||||
|
||||
static void tc_getter_band(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
|
||||
s_comm.ca = 0u;
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_DEF_CA, "ca below band falls back");
|
||||
s_comm.ca = IEC104_COMM_CA_MAX;
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MAX, "ca upper bound accepted");
|
||||
s_comm.ca = 42u;
|
||||
CHECK(iec104_comm_ca() == 42u, "ca in-band returned");
|
||||
|
||||
s_comm.port = 0u;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "port below band falls back");
|
||||
s_comm.port = IEC104_COMM_PORT_MIN - 1u;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "port 1023 falls back");
|
||||
s_comm.port = IEC104_COMM_PORT_MAX;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MAX, "port upper bound accepted");
|
||||
s_comm.port = 2404u;
|
||||
CHECK(iec104_comm_port() == 2404u, "port in-band returned");
|
||||
}
|
||||
|
||||
static void tc_setters(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
|
||||
CHECK(iec104_comm_ca_set(IEC104_COMM_CA_MIN) == 0, "ca min accepted");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MIN, "ca min stored");
|
||||
CHECK(iec104_comm_ca_set(IEC104_COMM_CA_MAX) == 0, "ca max accepted");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MAX, "ca max stored");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "legal ca set makes dirty");
|
||||
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_ca_set(0u) == -1, "ca zero rejected");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_DEF_CA, "rejected ca leaves copy unchanged");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "rejected ca does not make dirty");
|
||||
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MIN) == 0, "port min accepted");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MIN, "port min stored");
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MAX) == 0, "port max accepted");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MAX, "port max stored");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "legal port set makes dirty");
|
||||
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MIN - 1u) == -1, "port 1023 rejected");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "rejected port leaves copy unchanged");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "rejected port does not make dirty");
|
||||
}
|
||||
|
||||
static void tc_store_save(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_store_save() == 0, "clean store is a no-op success");
|
||||
CHECK(g_save_calls == 0, "clean store does not call engine");
|
||||
|
||||
CHECK(iec104_comm_ca_set(7u) == 0, "dirty setup");
|
||||
g_save_result = -1;
|
||||
CHECK(iec104_comm_store_save() == -1, "engine failure propagated");
|
||||
CHECK(g_save_calls == 1, "engine save attempted once on failure");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "failed save keeps dirty for retry");
|
||||
|
||||
g_save_result = 0;
|
||||
CHECK(iec104_comm_store_save() == 0, "retry save succeeds");
|
||||
CHECK(g_save_calls == 2, "retry calls engine again");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "successful save clears dirty");
|
||||
CHECK(iec104_comm_store_save() == 0, "next clean store again no-op");
|
||||
CHECK(g_save_calls == 2, "no extra engine call after clear");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("=== IEC104 [comm] Host Unit Tests ===\n");
|
||||
tc_init_defaults();
|
||||
tc_getter_band();
|
||||
tc_setters();
|
||||
tc_store_save();
|
||||
printf("=== IEC104 Comm Tests: %d/%d PASSED (failed=%d) ===\n",
|
||||
s_passed, s_total, s_failed);
|
||||
return (s_failed == 0) ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Host unit-test runner for the IEC104 [comm] configuration segment."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "test", "host", "common"))
|
||||
|
||||
from unit_test_harness import TestBuild, run_host_unit_test # noqa: E402
|
||||
|
||||
BUILD = TestBuild(
|
||||
name="IEC104 Comm Tests",
|
||||
src=os.path.join(HERE, "iec104_comm_test.c"),
|
||||
include_dirs=[
|
||||
os.path.join(ROOT, "App", "cfg"),
|
||||
os.path.join(ROOT, "App", "iec60870", "104_port"),
|
||||
HERE,
|
||||
],
|
||||
summary_marker="=== IEC104 Comm Tests:",
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run_host_unit_test(BUILD))
|
||||
@@ -1,6 +1,15 @@
|
||||
"""Test the independent acceptance oracle/decoder; not product acceptance."""
|
||||
import struct
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
if str(HERE) not in sys.path:
|
||||
sys.path.insert(0, str(HERE))
|
||||
|
||||
# Pure Python oracle/decoder checks: no C translation unit, so C line coverage does not apply.
|
||||
COVERAGE_SKIP = "纯 Python oracle/解码器测试,无 C 编译单元"
|
||||
|
||||
from oracle import reference
|
||||
from wire import asdu, header, objects, remote_objects, tlv
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Host unit-test runner for the IEC104 netTask service adapter."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "test", "host", "common"))
|
||||
|
||||
from unit_test_harness import TestBuild, run_host_unit_test # noqa: E402
|
||||
|
||||
BUILD = TestBuild(
|
||||
name="IEC104 Serv Tests",
|
||||
src=os.path.join(HERE, "iec104_serv_test.c"),
|
||||
include_dirs=[
|
||||
os.path.join(HERE, "stub"),
|
||||
os.path.join(ROOT, "App", "task"),
|
||||
os.path.join(ROOT, "App", "iec60870", "104_port"),
|
||||
os.path.join(ROOT, "App", "iec60870", "common"),
|
||||
os.path.join(ROOT, "App", "iec60870", "104"),
|
||||
os.path.join(ROOT, "App", "iec60870", "104", "business"),
|
||||
os.path.join(ROOT, "App", "iec60870", "platform"),
|
||||
os.path.join(ROOT, "App", "iec60870", "examples"),
|
||||
os.path.join(ROOT, "App", "algo"),
|
||||
os.path.join(ROOT, "App", "sync"),
|
||||
os.path.join(ROOT, "App", "util"),
|
||||
os.path.join(ROOT, "App", "app"),
|
||||
os.path.join(ROOT, "App", "cfg"),
|
||||
HERE,
|
||||
],
|
||||
summary_marker="=== IEC104 Serv Tests:",
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run_host_unit_test(BUILD))
|
||||
@@ -0,0 +1,27 @@
|
||||
/* Host-only stub: CMSIS-RTOS v2 surface used by iec104_serv.c. */
|
||||
#ifndef CMSIS_OS2_H
|
||||
#define CMSIS_OS2_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void *osEventFlagsId_t;
|
||||
|
||||
#define osFlagsWaitAny 0x00000000U
|
||||
#define osFlagsNoClear 0x00000002U
|
||||
#define osFlagsError 0x80000000U
|
||||
#define osOK 0
|
||||
|
||||
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,
|
||||
uint32_t options, uint32_t timeout);
|
||||
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags);
|
||||
uint32_t osKernelGetTickCount(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMSIS_OS2_H */
|
||||
@@ -0,0 +1,13 @@
|
||||
/* Host-only stub: logging macros used by iec104_serv.c (counting only). */
|
||||
#ifndef DBG_LOG_H
|
||||
#define DBG_LOG_H
|
||||
|
||||
extern int g_i104_info_count;
|
||||
extern int g_i104_error_count;
|
||||
|
||||
/* Consume all arguments so production call sites with computed locals do not
|
||||
* trigger C4189 under /W4. The comma expression has no side effects. */
|
||||
#define DBG_INFO(...) do { g_i104_info_count++; (void)(__VA_ARGS__); } while (0)
|
||||
#define DBG_ERROR(...) do { g_i104_error_count++; (void)(__VA_ARGS__); } while (0)
|
||||
|
||||
#endif /* DBG_LOG_H */
|
||||
@@ -0,0 +1,13 @@
|
||||
/* Host-only stub: IEC104 [comm] getters used by the service adapter. */
|
||||
#ifndef IEC104_COMM_H
|
||||
#define IEC104_COMM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define IEC104_COMM_DEF_CA 1u
|
||||
#define IEC104_COMM_DEF_PORT 2404u
|
||||
|
||||
uint16_t iec104_comm_ca(void);
|
||||
uint16_t iec104_comm_port(void);
|
||||
|
||||
#endif /* IEC104_COMM_H */
|
||||
@@ -0,0 +1,17 @@
|
||||
/* Host-only stub: net generation accessor used by iec104_serv.c. */
|
||||
#ifndef NET_SOCKET_H
|
||||
#define NET_SOCKET_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32_t net_get_generation(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NET_SOCKET_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Host-only stub: system event flags and values used by iec104_serv.c. */
|
||||
#ifndef SYS_EVENTS_H
|
||||
#define SYS_EVENTS_H
|
||||
|
||||
#include "cmsis_os2.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern osEventFlagsId_t sys_evtHandle;
|
||||
|
||||
#define SYS_EVT_NET_READY (1u << 0)
|
||||
#define SYS_EVT_PROT_SET_SAVE (1u << 7)
|
||||
#define SYS_EVT_RUN_PAR_SAVE (1u << 8)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SYS_EVENTS_H */
|
||||
Reference in New Issue
Block a user