diff --git a/docs/测试说明/单元测试/主机单测覆盖率口径.md b/docs/测试说明/单元测试/主机单测覆盖率口径.md index 1f18da90..3c29f153 100644 --- a/docs/测试说明/单元测试/主机单测覆盖率口径.md +++ b/docs/测试说明/单元测试/主机单测覆盖率口径.md @@ -99,6 +99,24 @@ 分母一律取"**插桩报告里出现的行**",**不做任何静态行数猜测**;`--md` 报告里"有效代码行" 那一列只用于未触及面的体量提示,不参与任何百分比。 +### 4.1 纯 Python 套件:显式 `COVERAGE_SKIP`,不参与 C 覆盖率 + +覆盖率统计面是 MSVC 插桩后的 C/C++ 生产源码;`test/host/**` 里少数**纯 Python** +套件(例如独立协议 oracle / 解码器测试)不编译 C 文件,天然没有模块级 `BUILD` +(`TestBuild`)。这类运行器必须在模块顶层声明一个显式标记: + +```python +COVERAGE_SKIP = "纯 Python oracle/解码器测试,无 C 编译单元" +``` + +`scripts/check_test_coverage.py` 在读取 `BUILD` 前先读该常量:有标记即把该套件记入 +基线 `no_coverage_suites`、控制台以 `SKIP` / `[NOTE]` 打印,**不计入 `modules` +与 `modules_total/modules_ok`**,也不会因缺 `BUILD` 判 FAIL。没有标记又没有 `BUILD` +的运行器仍然 FAIL,保持"缺编译配置就是真缺陷"的严格性。 + +**不得用启发式**(如"目录里没有 `.c` 就跳过")替代这个显式标记:那会把真正丢了 +`BUILD` 的 C 套件静默洗成绿灯,正是覆盖率门禁要拦的假绿形态。 + ## 5. 门禁姿态:只拦回退,不拦进步 台账 = [`scripts/test_coverage_baseline.json`](../../../scripts/test_coverage_baseline.json) diff --git a/scripts/check_test_coverage.py b/scripts/check_test_coverage.py index 4607fc88..fce706ab 100644 --- a/scripts/check_test_coverage.py +++ b/scripts/check_test_coverage.py @@ -7,6 +7,7 @@ r""" 1. 发现 test/host/**/run_unit_test.py(与 scripts/ci_local.py 的主机侧纯逻辑单测同一套发现规则), 直接 import 各模块运行器、复用其中声明的 TestBuild(**单一事实源**:include 目录、 桩顺序、测试单元路径都不在这里重复一遍); + 0. 模块运行器顶部显式声明 COVERAGE_SKIP 的纯 Python 套件不参与 C 覆盖率(见 load_test_build); 2. 用**与门禁完全相同的编译开关**(unit_test_harness._BASE_FLAGS)另建一份带 /Zi 调试 信息的产物,交给 Microsoft.CodeCoverage.Console.exe 动态插桩运行; 3. 解析 Cobertura XML,只保留**自研生产源码**(App/**、Drivers/BSP/** 的 .c/.h)的行与 @@ -141,6 +142,10 @@ def load_test_build(runner_path, harness): 各运行器都是声明式的:顶层只 import + 构造 TestBuild,真跑在 if __name__ == "__main__" 里,所以按独立模块名 import 是安全的。 + + 纯 Python 套件没有 C 编译单元,天然没有 BUILD;由运行器顶部显式声明 + COVERAGE_SKIP = "<原因>" 退出本 C 覆盖率门禁。必须是显式标记,不能靠 + "目录里有没有 .c" 猜——那会把真正丢了 BUILD 的 C 套件静默洗成绿灯。 """ mod_name = "dtu_cov_" + re.sub(r"\W+", "_", Path(runner_path).parent.name) spec = importlib.util.spec_from_file_location(mod_name, runner_path) @@ -153,6 +158,9 @@ def load_test_build(runner_path, harness): return None, "{} 在 import 阶段就调用了 sys.exit()".format(runner_path) except Exception as exc: return None, "import {} 失败: {}".format(runner_path, exc) + skip = getattr(mod, "COVERAGE_SKIP", None) + if skip is not None: + return None, ("__SKIP__" + str(skip)) build = getattr(mod, "BUILD", None) if build is None: return None, "{} 未声明模块级 BUILD,覆盖率脚本无法复用其编译配置".format(runner_path) @@ -435,7 +443,7 @@ BASELINE_FILE_FIELDS = ("lines_valid", "lines_covered", "funcs_valid", "funcs_co "line_rate", "func_rate") -def write_baseline(path, modules, files, totals, tested_sources, untested): +def write_baseline(path, modules, files, totals, tested_sources, untested, no_coverage_suites): # 基线只留可比字段:missing_lines 每次跑都会漂,进了台账就是无意义的提交抖动 payload = { "schema": BASELINE_SCHEMA, @@ -444,6 +452,7 @@ def write_baseline(path, modules, files, totals, tested_sources, untested): "门禁判据:files[].lines_covered 与 funcs_covered 只增不减,下降即拦提交。", "totals": totals, "modules": modules, + "no_coverage_suites": no_coverage_suites, "files": {rel: {k: v for k, v in rec.items() if k in BASELINE_FILE_FIELDS} for rel, rec in files.items()}, "tested_sources": tested_sources, @@ -454,7 +463,7 @@ def write_baseline(path, modules, files, totals, tested_sources, untested): f.write("\n") -def diff_baseline(baseline, modules, files, totals, full_run): +def diff_baseline(baseline, modules, files, totals, full_run, no_coverage_suites): """返回 (回退项, 提示项);回退项非空即门禁失败。""" regress, notes = [], [] if baseline is None: @@ -496,6 +505,8 @@ def diff_baseline(baseline, modules, files, totals, full_run): for name, b in sorted(base_mods.items()): cur = modules.get(name) if cur is None: + if name in no_coverage_suites: + continue if full_run: regress.append("模块 {}: 基线里有,本次未跑到(运行器缺失 / import 失败?)".format(name)) elif not cur.get("ok"): @@ -511,6 +522,8 @@ def diff_baseline(baseline, modules, files, totals, full_run): notes.append("新纳入统计: {}({}/{} 行,{})——确认无误后 --update-baseline 登记".format( rel, files[rel]["lines_covered"], files[rel]["lines_valid"], pct(files[rel]["line_rate"]))) + for name, reason in sorted(no_coverage_suites.items()): + notes.append("套件 {} 不参与 C 覆盖率:{}".format(name, reason)) if not full_run: notes.append("本次是 --module 子集运行:并集必然变小,跳过全部覆盖率回退判定" "(文件消失 / 行减少 / 函数减少 / 总计下降);只保留" @@ -522,10 +535,13 @@ def diff_baseline(baseline, modules, files, totals, full_run): # 输出 # -------------------------------------------------------------------------- -def print_report(modules, files, totals, untested, generated, notes, regress, verbose): +def print_report(modules, files, totals, untested, generated, notes, regress, verbose, + no_coverage_suites): print("=== 主机单测覆盖率(口径:docs/测试说明/单元测试/主机单测覆盖率口径.md)===") print("") print("--- 各套单测(插桩运行)---") + for name, reason in sorted(no_coverage_suites.items()): + print(" {:<12} SKIP {}".format(name, reason)) for name in sorted(modules): m = modules[name] if not m["ok"]: @@ -571,7 +587,7 @@ def print_report(modules, files, totals, untested, generated, notes, regress, ve totals["files_tested"], totals["files_untested"])) -def write_markdown(path, modules, files, totals, untested, notes, regress): +def write_markdown(path, modules, files, totals, untested, notes, regress, no_coverage_suites): """一次性 Markdown 报告(建议放临时目录,不入库)。 ⚠ 行拼装一律走 code() 显式拼接:`.format` 的优先级高于 `+`,写成 @@ -604,6 +620,8 @@ def write_markdown(path, modules, files, totals, untested, notes, regress): "| 套件 | 结果 | 触及生产文件 | 插桩行 |", "|---|---|---|---|", ] + for name, reason in sorted(no_coverage_suites.items()): + out.append("| " + code(name) + " | SKIP(" + reason + ") | 0 | 0 |") for name in sorted(modules): m = modules[name] out.append("| " + code(name) + " | " @@ -704,12 +722,16 @@ def run(args): total_files = {} modules = {} + no_coverage_suites = {} for name, runner in sorted(runners.items()): + build, err = load_test_build(runner, harness) + if err and err.startswith("__SKIP__"): + no_coverage_suites[name] = err[len("__SKIP__"):] + continue workdir = os.path.join(work_root, name) shutil.rmtree(workdir, ignore_errors=True) os.makedirs(workdir, exist_ok=True) - build, err = load_test_build(runner, harness) if err: modules[name] = {"ok": False, "reason": err, "files": {}, "lines_total": 0} print("[FAIL] {}".format(err), file=sys.stderr) @@ -796,6 +818,7 @@ def run(args): if args.json: payload = {"totals": totals, "modules": modules, "files": files, + "no_coverage_suites": no_coverage_suites, "untested_sources": untested, "generated_excluded": generated} Path(args.json).write_text( json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n", @@ -805,15 +828,19 @@ def run(args): if args.build_only: regress, notes = [], ["--build-only:只编译覆盖率形态(带 PDB),不采集数据"] else: - regress, notes = diff_baseline(baseline, modules, files, totals, full_run) + regress, notes = diff_baseline(baseline, modules, files, totals, full_run, + no_coverage_suites) - print_report(modules, files, totals, untested, generated, notes, regress, args.verbose) + print_report(modules, files, totals, untested, generated, notes, regress, args.verbose, + no_coverage_suites) if args.md: - write_markdown(args.md, modules, files, totals, untested, notes, regress) + write_markdown(args.md, modules, files, totals, untested, notes, regress, + no_coverage_suites) print("[OK] Markdown 报告: {}".format(args.md)) if args.update_baseline: - write_baseline(args.baseline, modules, files, totals, tested, untested) + write_baseline(args.baseline, modules, files, totals, tested, untested, + no_coverage_suites) print("[OK] 基线已写入 {}".format(os.path.relpath(args.baseline, REPO_ROOT))) return 0 diff --git a/scripts/test_coverage_baseline.json b/scripts/test_coverage_baseline.json index c711066c..24d96431 100644 --- a/scripts/test_coverage_baseline.json +++ b/scripts/test_coverage_baseline.json @@ -144,13 +144,29 @@ "lines_covered": 33, "lines_valid": 33 }, + "App/iec60870/104_port/iec104_comm.c": { + "func_rate": 1.0, + "funcs_covered": 7, + "funcs_valid": 7, + "line_rate": 1.0, + "lines_covered": 50, + "lines_valid": 50 + }, + "App/iec60870/common/IEC60870_Time.c": { + "func_rate": 0.45, + "funcs_covered": 9, + "funcs_valid": 20, + "line_rate": 0.3669, + "lines_covered": 62, + "lines_valid": 169 + }, "App/log/console_log.c": { "func_rate": 1.0, "funcs_covered": 21, "funcs_valid": 21, "line_rate": 0.9961, - "lines_covered": 256, - "lines_valid": 257 + "lines_covered": 255, + "lines_valid": 256 }, "App/log/soe_rec.c": { "func_rate": 1.0, @@ -176,38 +192,6 @@ "lines_covered": 410, "lines_valid": 413 }, - "App/iec60870/104_port/iec104_asdu.c": { - "func_rate": 1.0, - "funcs_covered": 10, - "funcs_valid": 10, - "line_rate": 0.932, - "lines_covered": 96, - "lines_valid": 103 - }, - "App/iec60870/104_port/iec104_comm.c": { - "func_rate": 1.0, - "funcs_covered": 7, - "funcs_valid": 7, - "line_rate": 0.98, - "lines_covered": 49, - "lines_valid": 50 - }, - "App/iec60870/104_port/iec104_link.c": { - "func_rate": 1.0, - "funcs_covered": 23, - "funcs_valid": 23, - "line_rate": 0.9062, - "lines_covered": 367, - "lines_valid": 405 - }, - "App/iec60870/104_port/iec104_pts.c": { - "func_rate": 0.9524, - "funcs_covered": 20, - "funcs_valid": 21, - "line_rate": 0.8902, - "lines_covered": 227, - "lines_valid": 255 - }, "App/sync/time_sntp.c": { "func_rate": 1.0, "funcs_covered": 18, @@ -225,12 +209,12 @@ "lines_valid": 20 }, "App/task/iec104_serv.c": { - "func_rate": 0.75, + "func_rate": 1.0, "funcs_covered": 9, - "funcs_valid": 12, - "line_rate": 0.7572, - "lines_covered": 131, - "lines_valid": 173 + "funcs_valid": 9, + "line_rate": 1.0, + "lines_covered": 102, + "lines_valid": 102 }, "App/util/boot_stats.c": { "func_rate": 1.0, @@ -242,11 +226,11 @@ }, "App/util/ioboard_di.c": { "func_rate": 1.0, - "funcs_covered": 8, - "funcs_valid": 8, - "line_rate": 0.9358, - "lines_covered": 102, - "lines_valid": 109 + "funcs_covered": 10, + "funcs_valid": 10, + "line_rate": 0.9426, + "lines_covered": 115, + "lines_valid": 122 }, "App/util/sha256.c": { "func_rate": 1.0, @@ -369,12 +353,12 @@ "lines_valid": 117 }, "Drivers/BSP/NET/net_socket.c": { - "func_rate": 1.0, + "func_rate": 0.9516, "funcs_covered": 59, - "funcs_valid": 59, - "line_rate": 0.9969, + "funcs_valid": 62, + "line_rate": 0.9612, "lines_covered": 966, - "lines_valid": 969 + "lines_valid": 1005 }, "Drivers/BSP/NET/net_udp.c": { "func_rate": 1.0, @@ -496,9 +480,9 @@ }, "console_log": { "files": { - "App/log/console_log.c": 257 + "App/log/console_log.c": 256 }, - "lines_total": 257, + "lines_total": 256, "ok": true }, "freq": { @@ -525,38 +509,27 @@ "lines_total": 36, "ok": true }, - "iec104": { + "iec104_comm": { "files": { - "App/algo/adc_cal.c": 193, - "App/algo/freq.c": 130, - "App/algo/meas.c": 64, - "App/algo/phasor.c": 55, - "App/algo/prot_elm.c": 122, - "App/algo/prot_set.c": 179, - "App/algo/run_par.c": 148, - "App/algo/sample_ring.c": 41, - "App/cfg/cfg_file.c": 754, - "App/iec60870/104_port/iec104_asdu.c": 103, - "App/iec60870/104_port/iec104_comm.c": 50, - "App/iec60870/104_port/iec104_link.c": 405, - "App/iec60870/104_port/iec104_pts.c": 255 + "App/iec60870/104_port/iec104_comm.c": 50 }, - "lines_total": 2499, + "lines_total": 50, "ok": true }, "iec104_serv": { "files": { - "App/task/iec104_serv.c": 173 + "App/iec60870/104_port/iec104_comm.c": 50, + "App/task/iec104_serv.c": 102 }, - "lines_total": 173, + "lines_total": 152, "ok": true }, "ioboard": { "files": { - "App/util/ioboard_di.c": 109, + "App/util/ioboard_di.c": 122, "Drivers/BSP/IOBOARD/ioboard_bits.c": 8 }, - "lines_total": 117, + "lines_total": 130, "ok": true }, "lftpd": { @@ -636,10 +609,10 @@ }, "net_socket": { "files": { - "Drivers/BSP/NET/net_socket.c": 969, + "Drivers/BSP/NET/net_socket.c": 1005, "Drivers/BSP/NET/net_udp.c": 239 }, - "lines_total": 1208, + "lines_total": 1244, "ok": true }, "phasor": { @@ -736,10 +709,10 @@ }, "soe": { "files": { - "App/log/soe_rec.c": 87, - "App/iec60870/104_port/iec104_asdu.c": 103 + "App/iec60870/common/IEC60870_Time.c": 169, + "App/log/soe_rec.c": 87 }, - "lines_total": 190, + "lines_total": 256, "ok": true }, "soe_store": { @@ -838,6 +811,9 @@ "ok": true } }, + "no_coverage_suites": { + "iec104_dtu": "纯 Python oracle/解码器测试,无 C 编译单元" + }, "note": "主机单测覆盖率基线。口径与工具链限制见 docs/测试说明/单元测试/主机单测覆盖率口径.md。门禁判据:files[].lines_covered 与 funcs_covered 只增不减,下降即拦提交。", "schema": 1, "tested_sources": [ @@ -859,14 +835,12 @@ "App/cfg/prot_set_store.c", "App/fs/vol_idle_sync.c", "App/fs/vol_lock.c", + "App/iec60870/104_port/iec104_comm.c", + "App/iec60870/common/IEC60870_Time.c", "App/log/console_log.c", "App/log/soe_rec.c", "App/log/soe_store.c", "App/log/sys_log_mcu.c", - "App/iec60870/104_port/iec104_asdu.c", - "App/iec60870/104_port/iec104_comm.c", - "App/iec60870/104_port/iec104_link.c", - "App/iec60870/104_port/iec104_pts.c", "App/sync/time_sntp.c", "App/sync/time_sync.c", "App/task/iec104_serv.c", @@ -899,39 +873,123 @@ "Drivers/BSP/UI/ui_w_soe.c" ], "totals": { - "files_generated_excluded": 5, - "files_tested": 56, - "files_untested": 181, - "func_rate": 0.9339, - "funcs_covered": 664, - "funcs_valid": 711, - "line_rate": 0.9027, - "lines_covered": 8848, - "lines_valid": 9802, + "files_generated_excluded": 6, + "files_tested": 54, + "files_untested": 270, + "func_rate": 0.9161, + "funcs_covered": 622, + "funcs_valid": 679, + "line_rate": 0.8932, + "lines_covered": 8204, + "lines_valid": 9185, "modules_ok": 49, "modules_total": 49 }, "untested_sources": [ + { + "code_lines": 4058, + "path": "App/iec60870/common/IEC60870_InformationObjects.c" + }, + { + "code_lines": 2176, + "path": "App/iec60870/104/IEC104_TcpServer.c" + }, + { + "code_lines": 1680, + "path": "App/iec60870/104/IEC104_AsduQueue.c" + }, + { + "code_lines": 1480, + "path": "App/iec60870/104/business/IEC104_Interrogation.c" + }, + { + "code_lines": 1374, + "path": "App/iec60870/104/business/IEC104_Command.c" + }, + { + "code_lines": 1125, + "path": "App/iec60870/104/IEC104_SendManager.c" + }, + { + "code_lines": 1086, + "path": "App/iec60870/104/business/IEC104_RemoteParameter.c" + }, + { + "code_lines": 1027, + "path": "App/iec60870/104/business/IEC104_PeriodicScan.c" + }, + { + "code_lines": 945, + "path": "App/iec60870/104/IEC104_Connection.c" + }, + { + "code_lines": 892, + "path": "App/iec60870/104/business/IEC104_SystemCommand.c" + }, + { + "code_lines": 802, + "path": "App/iec60870/104/IEC104_Slave.c" + }, { "code_lines": 801, "path": "Drivers/BSP/CH395F/ch395f.c" }, + { + "code_lines": 757, + "path": "App/iec60870/104/business/IEC104_Parameter.c" + }, + { + "code_lines": 749, + "path": "App/iec60870/common/IEC60870_InformationObjects.h" + }, { "code_lines": 712, "path": "Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c" }, + { + "code_lines": 653, + "path": "App/iec60870/104/business/IEC104_Spontaneous.c" + }, + { + "code_lines": 640, + "path": "App/iec60870/104_port/iec104_core.c" + }, { "code_lines": 597, "path": "App/task/ui_task.c" }, { - "code_lines": 539, + "code_lines": 585, + "path": "App/iec60870/examples/iec104_dtu_profile.h" + }, + { + "code_lines": 579, + "path": "App/iec60870/104/IEC104_Link.c" + }, + { + "code_lines": 541, "path": "App/task/default_task.c" }, { - "code_lines": 423, + "code_lines": 517, + "path": "App/iec60870/104/business/IEC104_ClockSynchronization.c" + }, + { + "code_lines": 489, + "path": "App/iec60870/104_port/iec104_source.c" + }, + { + "code_lines": 465, "path": "App/app/ui_data_feed.c" }, + { + "code_lines": 462, + "path": "App/iec60870/104/business/IEC104_Read.c" + }, + { + "code_lines": 441, + "path": "App/iec60870/104_port/iec104_net_mcu.c" + }, { "code_lines": 375, "path": "App/task/adc_task.c" @@ -944,21 +1002,29 @@ "code_lines": 353, "path": "Drivers/BSP/UI/ui_w_setview.c" }, + { + "code_lines": 343, + "path": "App/iec60870/104/IEC104_SendBuffer.c" + }, { "code_lines": 334, "path": "Drivers/BSP/UI/ui_content_page.c" }, + { + "code_lines": 281, + "path": "App/iec60870/common/IEC60870_Asdu.c" + }, { "code_lines": 265, "path": "Drivers/BSP/UI/ui_w_editor.c" }, { "code_lines": 264, - "path": "Drivers/BSP/SD2506/sd2506.c" + "path": "Drivers/BSP/IOBOARD/ioboard.c" }, { - "code_lines": 262, - "path": "Drivers/BSP/IOBOARD/ioboard.c" + "code_lines": 264, + "path": "Drivers/BSP/SD2506/sd2506.c" }, { "code_lines": 261, @@ -973,7 +1039,11 @@ "path": "Drivers/BSP/UI/ui_page_wave_play.c" }, { - "code_lines": 232, + "code_lines": 247, + "path": "App/iec60870/104/IEC104_Net_Windows.c" + }, + { + "code_lines": 238, "path": "Drivers/BSP/UI/ui_data.h" }, { @@ -984,6 +1054,14 @@ "code_lines": 215, "path": "Drivers/BSP/UI/ui_w_setlist.c" }, + { + "code_lines": 210, + "path": "App/iec60870/104/IEC104_TcpTypes.h" + }, + { + "code_lines": 208, + "path": "App/iec60870/common/IEC60870_FaultEvent.c" + }, { "code_lines": 206, "path": "Drivers/BSP/CH395F/ch395f.h" @@ -992,10 +1070,46 @@ "code_lines": 205, "path": "Drivers/BSP/UI/ui_page_net_cfg.c" }, + { + "code_lines": 204, + "path": "App/iec60870/104/IEC104_SendManager.h" + }, { "code_lines": 200, "path": "Drivers/BSP/UI/ui_w_ytview.c" }, + { + "code_lines": 190, + "path": "App/iec60870/104/IEC104_Slave.h" + }, + { + "code_lines": 186, + "path": "App/iec60870/104/business/IEC104_FileService.h" + }, + { + "code_lines": 182, + "path": "App/iec60870/104/IEC104_AsduQueue.h" + }, + { + "code_lines": 180, + "path": "App/iec60870/104/business/IEC104_Command.h" + }, + { + "code_lines": 179, + "path": "App/iec60870/104/business/IEC104_RemoteParameter.h" + }, + { + "code_lines": 175, + "path": "App/iec60870/104/IEC104_Connection.h" + }, + { + "code_lines": 169, + "path": "App/iec60870/104/IEC104_Codec.c" + }, + { + "code_lines": 169, + "path": "App/iec60870/104/IEC104_Receiver.c" + }, { "code_lines": 157, "path": "Drivers/BSP/TPAFE5160/tpafe5160.c" @@ -1008,14 +1122,26 @@ "code_lines": 152, "path": "App/algo/wave_store.h" }, + { + "code_lines": 151, + "path": "App/iec60870/common/IEC60870_EnergyFreeze.h" + }, { "code_lines": 150, "path": "Drivers/BSP/UI/ui_page_password.c" }, + { + "code_lines": 148, + "path": "App/iec60870/104/IEC104_Link_Common.h" + }, { "code_lines": 148, "path": "Drivers/BSP/UI/ui_page_device_info.c" }, + { + "code_lines": 147, + "path": "App/iec60870/common/IEC60870_Codec.c" + }, { "code_lines": 143, "path": "App/app/app_main.c" @@ -1024,10 +1150,18 @@ "code_lines": 142, "path": "App/task/protect_task.c" }, + { + "code_lines": 136, + "path": "App/iec60870/104/business/IEC104_Interrogation.h" + }, { "code_lines": 134, "path": "Drivers/BSP/UI/ui_menu.c" }, + { + "code_lines": 129, + "path": "App/task/net_task.c" + }, { "code_lines": 129, "path": "Drivers/BSP/UI/ui_page_clear.c" @@ -1037,8 +1171,16 @@ "path": "Drivers/BSP/UI/ui_page_comm.c" }, { - "code_lines": 123, - "path": "App/task/net_task.c" + "code_lines": 126, + "path": "App/iec60870/104/business/IEC104_Startup.c" + }, + { + "code_lines": 124, + "path": "App/iec60870/104/business/IEC104_Spontaneous.h" + }, + { + "code_lines": 120, + "path": "Drivers/BSP/UI/ui_page_yx.c" }, { "code_lines": 119, @@ -1048,6 +1190,22 @@ "code_lines": 114, "path": "Drivers/BSP/UI/ui_page_ac.c" }, + { + "code_lines": 111, + "path": "App/iec60870/104/business/IEC104_Parameter.h" + }, + { + "code_lines": 110, + "path": "App/iec60870/104/IEC104_SendBuffer.h" + }, + { + "code_lines": 109, + "path": "App/iec60870/104/business/IEC104_PeriodicScan.h" + }, + { + "code_lines": 108, + "path": "App/iec60870/common/IEC60870_History.h" + }, { "code_lines": 104, "path": "Drivers/BSP/NET/net_types.h" @@ -1057,20 +1215,36 @@ "path": "Drivers/BSP/UI/ui_port.c" }, { - "code_lines": 98, - "path": "Drivers/BSP/SD2506/sd2506.h" + "code_lines": 102, + "path": "App/iec60870/104/IEC104_TcpServer.h" }, { - "code_lines": 94, + "code_lines": 99, "path": "Drivers/BSP/NET/net_socket.h" }, + { + "code_lines": 98, + "path": "App/iec60870/104/business/IEC104_SoftwareUpgrade.h" + }, + { + "code_lines": 98, + "path": "Drivers/BSP/SD2506/sd2506.h" + }, { "code_lines": 93, "path": "Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.h" }, { - "code_lines": 87, - "path": "Drivers/BSP/UI/ui_page_yx.c" + "code_lines": 89, + "path": "App/iec60870/104/IEC104_Link_Slave_StartStop.c" + }, + { + "code_lines": 88, + "path": "App/iec60870/common/IEC60870_FirmwareUpdate.h" + }, + { + "code_lines": 85, + "path": "App/iec60870/104/business/IEC104_SystemCommand.h" }, { "code_lines": 84, @@ -1084,26 +1258,46 @@ "code_lines": 77, "path": "App/algo/prot_set.h" }, + { + "code_lines": 77, + "path": "App/iec60870/104/business/IEC104_ClockSynchronization.h" + }, { "code_lines": 77, "path": "Drivers/BSP/dbg_log.h" }, + { + "code_lines": 75, + "path": "App/iec60870/104_port/iec104_dtu_table.c" + }, + { + "code_lines": 74, + "path": "App/iec60870/common/IEC60870_FaultRecorder.h" + }, + { + "code_lines": 73, + "path": "App/iec60870/common/IEC60870_Asdu.h" + }, { "code_lines": 70, "path": "Drivers/BSP/UI/ui_page_dc.c" }, { - "code_lines": 62, - "path": "App/iec60870/104_port/iec104_asdu.h" + "code_lines": 66, + "path": "App/iec60870/104/business/IEC104_Read.h" + }, + { + "code_lines": 66, + "path": "App/iec60870/common/IEC60870_Time.h" + }, + { + "code_lines": 59, + "path": "App/iec60870/104/IEC104_Types.h" }, { "code_lines": 55, "path": "App/algo/adc_health.h" }, - { - "code_lines": 55, - "path": "App/iec60870/104_port/iec104_link.h" - }, { "code_lines": 55, "path": "Drivers/BSP/ScreenLink/sb_link.h" @@ -1112,18 +1306,38 @@ "code_lines": 54, "path": "Drivers/BSP/TPAFE5160/tpafe5160.h" }, + { + "code_lines": 53, + "path": "App/iec60870/common/IEC60870_FaultEvent.h" + }, { "code_lines": 52, "path": "Drivers/BSP/UI/ui_w_setlist.h" }, + { + "code_lines": 51, + "path": "App/iec60870/104/IEC104_Net.h" + }, + { + "code_lines": 51, + "path": "App/iec60870/104/IEC104_Receiver.h" + }, { "code_lines": 51, "path": "App/util/task_watchdog.h" }, + { + "code_lines": 50, + "path": "App/iec60870/common/IEC60870_UserLog.h" + }, { "code_lines": 50, "path": "App/log/sys_log.h" }, + { + "code_lines": 49, + "path": "App/iec60870/104_port/iec104_source.h" + }, { "code_lines": 47, "path": "Drivers/BSP/UI/ui_w_soe.h" @@ -1152,6 +1366,10 @@ "code_lines": 42, "path": "App/cfg/cfg_file.h" }, + { + "code_lines": 41, + "path": "App/iec60870/104/IEC104_Codec.h" + }, { "code_lines": 41, "path": "Drivers/BSP/NET/lftpd/private/lftpd_status.h" @@ -1176,6 +1394,10 @@ "code_lines": 41, "path": "Drivers/BSP/UI/ui_page_view_word.c" }, + { + "code_lines": 40, + "path": "App/util/ioboard_di.h" + }, { "code_lines": 40, "path": "Drivers/BSP/UI/ui_page_deadband.c" @@ -1186,12 +1408,20 @@ }, { "code_lines": 38, - "path": "App/util/ioboard_di.h" + "path": "App/iec60870/common/IEC60870_InformationObjects_Internal.h" }, { "code_lines": 38, "path": "Drivers/BSP/UI/ui_page_wave_manual.c" }, + { + "code_lines": 37, + "path": "App/iec60870/104/business/IEC104_GeneralInterrogation.c" + }, + { + "code_lines": 36, + "path": "App/iec60870/common/IEC60870_Comtrade.h" + }, { "code_lines": 36, "path": "Drivers/BSP/UI/ui_w_records.h" @@ -1200,10 +1430,26 @@ "code_lines": 35, "path": "App/algo/prot_elm.h" }, + { + "code_lines": 35, + "path": "App/iec60870/104/business/IEC104_CounterInterrogation.c" + }, + { + "code_lines": 35, + "path": "App/iec60870/104_port/iec104_core.h" + }, + { + "code_lines": 34, + "path": "App/iec60870/104/IEC104_TcpServer_Config.h" + }, { "code_lines": 34, "path": "Drivers/BSP/NETCFG/net_cfg.h" }, + { + "code_lines": 33, + "path": "Drivers/BSP/IOBOARD/ioboard.h" + }, { "code_lines": 33, "path": "Drivers/BSP/NET/net_config.h" @@ -1220,6 +1466,10 @@ "code_lines": 33, "path": "Drivers/BSP/UI/ui_page_action.h" }, + { + "code_lines": 32, + "path": "App/iec60870/104/IEC104_Log.h" + }, { "code_lines": 32, "path": "App/util/boot_stats.h" @@ -1230,11 +1480,11 @@ }, { "code_lines": 31, - "path": "App/iec60870/104_port/iec104_pts.h" + "path": "App/iec60870/104/IEC104_Link_Master_StartStop.h" }, { "code_lines": 31, - "path": "Drivers/BSP/IOBOARD/ioboard.h" + "path": "App/iec60870/104_port/iec104_net_mcu.h" }, { "code_lines": 31, @@ -1248,6 +1498,14 @@ "code_lines": 31, "path": "Drivers/BSP/UI/ui_w_editor.h" }, + { + "code_lines": 30, + "path": "App/iec60870/104/IEC104.h" + }, + { + "code_lines": 30, + "path": "App/iec60870/104_port/iec104_dtu_table.h" + }, { "code_lines": 29, "path": "App/algo/meas.h" @@ -1266,7 +1524,11 @@ }, { "code_lines": 27, - "path": "App/task/task_handles.h" + "path": "App/iec60870/common/IEC60870_Codec.h" + }, + { + "code_lines": 27, + "path": "App/iec60870/common/IEC60870_EnergyFreezeFile.h" }, { "code_lines": 27, @@ -1288,10 +1550,22 @@ "code_lines": 26, "path": "App/algo/sb_led_map.h" }, + { + "code_lines": 26, + "path": "App/iec60870/104/business/IEC104_Startup.h" + }, { "code_lines": 26, "path": "Drivers/BSP/UI/ui_port.h" }, + { + "code_lines": 25, + "path": "App/iec60870/common/IEC60870_Slave.h" + }, + { + "code_lines": 25, + "path": "App/task/task_handles.h" + }, { "code_lines": 25, "path": "Drivers/BSP/NETCFG/sntp_cfg.h" @@ -1312,10 +1586,22 @@ "code_lines": 23, "path": "App/iec60870/104_port/iec104_comm.h" }, + { + "code_lines": 23, + "path": "App/iec60870/common/IEC60870_Clock.h" + }, + { + "code_lines": 23, + "path": "App/iec60870/common/IEC60870_HistoryFile.h" + }, { "code_lines": 22, "path": "App/cfg/cfg_file_store.h" }, + { + "code_lines": 22, + "path": "App/iec60870/104/IEC104_Link_Slave_StartStop.h" + }, { "code_lines": 22, "path": "App/log/console_log.h" @@ -1360,6 +1646,10 @@ "code_lines": 22, "path": "Drivers/BSP/UI/ui_page_yk.c" }, + { + "code_lines": 20, + "path": "App/iec60870/104/business/IEC104_Interrogation_Internal.h" + }, { "code_lines": 19, "path": "App/app/sys_events.h" @@ -1368,6 +1658,10 @@ "code_lines": 19, "path": "App/fs/vol_lock.h" }, + { + "code_lines": 19, + "path": "App/iec60870/common/IEC60870_Slave.c" + }, { "code_lines": 18, "path": "Drivers/BSP/NET/lftpd/lftpd.h" @@ -1380,14 +1674,30 @@ "code_lines": 17, "path": "App/algo/sample_ring.h" }, + { + "code_lines": 16, + "path": "App/iec60870/104/business/IEC104_CounterInterrogation.h" + }, { "code_lines": 16, "path": "Drivers/BSP/UI/ui_menu_view.h" }, + { + "code_lines": 16, + "path": "Drivers/BSP/UI/ui_net_sink_backend_mcu.c" + }, { "code_lines": 15, "path": "App/algo/freq.h" }, + { + "code_lines": 15, + "path": "App/iec60870/104/IEC104_Link_Internal.h" + }, + { + "code_lines": 15, + "path": "App/iec60870/common/IEC60870_UserLogFile.h" + }, { "code_lines": 15, "path": "App/log/soe_store.h" @@ -1400,10 +1710,6 @@ "code_lines": 15, "path": "Drivers/BSP/NET/net_udp.h" }, - { - "code_lines": 15, - "path": "Drivers/BSP/UI/ui_net_sink_backend_mcu.c" - }, { "code_lines": 15, "path": "Drivers/BSP/UI/ui_standby.h" @@ -1416,6 +1722,10 @@ "code_lines": 14, "path": "Drivers/BSP/UI/ui_style.c" }, + { + "code_lines": 13, + "path": "App/iec60870/common/IEC60870_Types.h" + }, { "code_lines": 13, "path": "App/task/adc_task.h" @@ -1424,6 +1734,10 @@ "code_lines": 13, "path": "Drivers/BSP/NET/net_events.h" }, + { + "code_lines": 13, + "path": "Drivers/BSP/UI/ui_net_sink_backend.h" + }, { "code_lines": 13, "path": "Drivers/BSP/UI/ui_pwd_def.h" @@ -1436,6 +1750,10 @@ "code_lines": 12, "path": "App/app/app_main.h" }, + { + "code_lines": 12, + "path": "App/iec60870/104/business/IEC104_BusinessConfig_Internal.h" + }, { "code_lines": 12, "path": "App/sync/time_sntp.h" @@ -1452,10 +1770,6 @@ "code_lines": 12, "path": "Drivers/BSP/UI/ui_menu.h" }, - { - "code_lines": 12, - "path": "Drivers/BSP/UI/ui_net_sink_backend.h" - }, { "code_lines": 11, "path": "App/task/net_task.h" @@ -1556,6 +1870,10 @@ "code_lines": 10, "path": "App/fs/vol_idle_sync.h" }, + { + "code_lines": 10, + "path": "App/iec60870/104/IEC104_Config_Internal.h" + }, { "code_lines": 10, "path": "App/sync/time_sync.h" @@ -1564,6 +1882,10 @@ "code_lines": 10, "path": "App/task/default_task.h" }, + { + "code_lines": 9, + "path": "App/iec60870/common/IEC60870_StaticAssert.h" + }, { "code_lines": 6, "path": "Drivers/BSP/UI/ui_page_password.h" @@ -1608,10 +1930,18 @@ "code_lines": 5, "path": "Drivers/BSP/UI/ui_page_oper.h" }, + { + "code_lines": 5, + "path": "Drivers/BSP/UI/ui_page_runlog.h" + }, { "code_lines": 5, "path": "Drivers/BSP/UI/ui_page_runreport.h" }, + { + "code_lines": 5, + "path": "Drivers/BSP/UI/ui_page_selftest.h" + }, { "code_lines": 5, "path": "Drivers/BSP/UI/ui_page_start.h" @@ -1621,16 +1951,16 @@ "path": "Drivers/BSP/UI/ui_page_yk.h" }, { - "code_lines": 4, - "path": "Drivers/BSP/UI/ui_page_runlog.h" - }, - { - "code_lines": 4, - "path": "Drivers/BSP/UI/ui_page_selftest.h" + "code_lines": 3, + "path": "App/app/app_cfg.h" }, { "code_lines": 3, - "path": "App/app/app_cfg.h" + "path": "App/iec60870/104/IEC104_Link.h" + }, + { + "code_lines": 3, + "path": "App/iec60870/104/business/IEC104_GeneralInterrogation.h" }, { "code_lines": 3, diff --git a/test/host/iec104_comm/iec104_comm_test.c b/test/host/iec104_comm/iec104_comm_test.c new file mode 100644 index 00000000..3d0eb127 --- /dev/null +++ b/test/host/iec104_comm/iec104_comm_test.c @@ -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 +#include +#include + +#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; +} diff --git a/test/host/iec104_comm/run_unit_test.py b/test/host/iec104_comm/run_unit_test.py new file mode 100644 index 00000000..5e6fe15c --- /dev/null +++ b/test/host/iec104_comm/run_unit_test.py @@ -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)) diff --git a/test/host/iec104_dtu/run_unit_test.py b/test/host/iec104_dtu/run_unit_test.py index 978748ae..996de33a 100644 --- a/test/host/iec104_dtu/run_unit_test.py +++ b/test/host/iec104_dtu/run_unit_test.py @@ -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 diff --git a/test/host/iec104_serv/iec104_serv_test.c b/test/host/iec104_serv/iec104_serv_test.c new file mode 100644 index 00000000..2563f377 --- /dev/null +++ b/test/host/iec104_serv/iec104_serv_test.c @@ -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 +#include +#include +#include + +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; +} diff --git a/test/host/iec104_serv/run_unit_test.py b/test/host/iec104_serv/run_unit_test.py new file mode 100644 index 00000000..5caccda1 --- /dev/null +++ b/test/host/iec104_serv/run_unit_test.py @@ -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)) diff --git a/test/host/iec104_serv/stub/cmsis_os2.h b/test/host/iec104_serv/stub/cmsis_os2.h new file mode 100644 index 00000000..3d772461 --- /dev/null +++ b/test/host/iec104_serv/stub/cmsis_os2.h @@ -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 + +#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 */ diff --git a/test/host/iec104_serv/stub/dbg_log.h b/test/host/iec104_serv/stub/dbg_log.h new file mode 100644 index 00000000..6ecd2060 --- /dev/null +++ b/test/host/iec104_serv/stub/dbg_log.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 */ diff --git a/test/host/iec104_serv/stub/iec104_comm.h b/test/host/iec104_serv/stub/iec104_comm.h new file mode 100644 index 00000000..44bfd3d5 --- /dev/null +++ b/test/host/iec104_serv/stub/iec104_comm.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 + +#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 */ diff --git a/test/host/iec104_serv/stub/net_socket.h b/test/host/iec104_serv/stub/net_socket.h new file mode 100644 index 00000000..31754d67 --- /dev/null +++ b/test/host/iec104_serv/stub/net_socket.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 + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t net_get_generation(void); + +#ifdef __cplusplus +} +#endif + +#endif /* NET_SOCKET_H */ diff --git a/test/host/iec104_serv/stub/sys_events.h b/test/host/iec104_serv/stub/sys_events.h new file mode 100644 index 00000000..1dab5c2c --- /dev/null +++ b/test/host/iec104_serv/stub/sys_events.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 */