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。
67 lines
2.9 KiB
Python
67 lines
2.9 KiB
Python
"""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
|
|
|
|
|
|
class WireOracleTests(unittest.TestCase):
|
|
def test_reference_has_independently_derived_sets(self):
|
|
ref = reference()
|
|
self.assertEqual(len(ref["gi"]), 225)
|
|
self.assertEqual(len(ref["read"]), 121)
|
|
self.assertEqual(len(ref["counters"]), 8)
|
|
self.assertEqual(len(ref["remote"]), 345)
|
|
self.assertEqual(len(ref["diagnostic_aliases_pending_scope"]), 150)
|
|
self.assertNotIn(0x9601, ref["remote"])
|
|
self.assertEqual(ref["remote"][0x8858], (38, "25"))
|
|
|
|
def test_pn_zero_positive_and_one_negative(self):
|
|
self.assertFalse(header(bytes.fromhex("64010711010000000014"))["pn"])
|
|
self.assertTrue(header(bytes.fromhex("64014711010000000014"))["pn"])
|
|
self.assertEqual(header(bytes.fromhex("64014711010000000014"))["cot"], 7)
|
|
|
|
def test_float_objects_nonsequential_and_sequential(self):
|
|
rows = objects(bytes.fromhex("0d02141101000140000000c03f00024000000020c000"))
|
|
self.assertEqual([(r["ioa"], r["value"]) for r in rows], [(0x4001, 1.5), (0x4002, -2.5)])
|
|
rows = objects(bytes.fromhex("0d82141101000140000000c03f00000020c000"))
|
|
self.assertEqual([(r["ioa"], r["value"]) for r in rows], [(0x4001, 1.5), (0x4002, -2.5)])
|
|
|
|
def test_truncation_and_trailing_bytes_rejected(self):
|
|
valid = bytes.fromhex("01011411010001000000")
|
|
for bad in (valid[:-1], valid + b"\x00"):
|
|
with self.assertRaises(AssertionError):
|
|
objects(bad)
|
|
|
|
def test_remote_type_tags_decode_independent_vector(self):
|
|
vector = bytes.fromhex("ca030711010000000020880001010123880026040000e0402d88002d020100")
|
|
rows = remote_objects(vector)
|
|
self.assertEqual([(r["ioa"], r["tag"], r["value"]) for r in rows], [(0x8820, 1, 1), (0x8823, 38, 7.0), (0x882D, 45, 1)])
|
|
self.assertEqual(tlv(0x8823, 38, 7.0), bytes.fromhex("23880026040000e040"))
|
|
|
|
def test_remote_malformed_tlv_length_rejected(self):
|
|
with self.assertRaises(AssertionError):
|
|
remote_objects(bytes.fromhex("ca01071101000000002388002603000000"))
|
|
|
|
def test_maximum_apdu_is_255_not_257(self):
|
|
payload = bytes.fromhex("000080ffffff0000") + bytes(235)
|
|
data = asdu(203, payload)
|
|
self.assertEqual(len(data), 249)
|
|
frame = bytes((0x68, len(data) + 4)) + struct.pack("<HH", 0, 0) + data
|
|
self.assertEqual(len(frame), 255)
|
|
self.assertEqual(frame[1], 253)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|