"""Real-loopback acceptance against the same lvgl_sim binary as UI tests.""" import argparse import datetime import hashlib import json import math from pathlib import Path import re import socket import struct import subprocess import tempfile import threading import time import traceback from oracle import ROOT, reference from wire import Client, asdu, cp56, header, objects, remote_objects, tlv, u24 class Acceptance: def __init__(self, args): self.args = args self.oracle = reference() self.trace, self.decoded, self.results = [], [], [] self.client = None def connect(self): if self.client: self.client.close() time.sleep(0.10) # Give the single-owner service one close/poll cycle. self.client = Client(self.args.host, self.args.port, self.trace) self.client.start(split=True) return self.client def run(self, name, function): started = time.monotonic() try: self.connect() function() result = {"name": name, "passed": True} except Exception as exc: result = {"name": name, "passed": False, "error": str(exc), "traceback": traceback.format_exc()} result["seconds"] = time.monotonic() - started self.results.append(result) print(("PASS " if result["passed"] else "FAIL ") + name + (": " + result["error"] if not result["passed"] else ""), flush=True) if self.client: self.client.close() self.client = None def read(self, ioa): self.client.request(asdu(102, u24(ioa), cot=5)) data, _ = self.client.receive(lambda d: header(d)["cot"] == 5 or header(d)["pn"]) assert not header(data)["pn"], data.hex() assert header(data)["ca"] == 1 and header(data)["oa"] == 17, data.hex() rows = objects(data) assert len(rows) == 1 and rows[0]["ioa"] == ioa self.decoded.extend(rows) return rows[0] def remote_read(self, ioas=(), area=0): self.client.request(asdu(202, struct.pack("= 100: group, index = divmod(row["ioa"] - 0x8601, 0x20) expected_value = (200 if index < 4 else 655) if group == 0 else (-10000 if group == 1 else 10000 if group == 2 else 0) assert row["value"] == expected_value and row["qpm"] == (1, 3, 4, 2)[group], row def test_counter_exact_set_zero_and_invalid(self): self.client.request(asdu(101, u24(0) + bytes((5,)))) self.client.response(101, 7) rows = [] while True: data, _ = self.client.receive(lambda d: header(d)["type"] in (15, 37, 206, 207, 101)) h = header(data) assert h["ca"] == 1 and h["oa"] == 17 and not h["pn"] if h["type"] == 101: assert h["cot"] == 10 break assert h["cot"] == 37 rows.extend(objects(data)) self.decoded.extend(rows) assert len(rows) == len(self.oracle["counters"]) assert {r["ioa"] for r in rows} == set(self.oracle["counters"]) assert all(r["value"] == 0 and r["quality"] & 128 for r in rows) def test_read_all_runtime_addresses(self): for ioa, typ in sorted(self.oracle["read"].items()): row = self.read(ioa) assert row["type"] == typ, row assert row["value"] == 0, row assert row["quality"] == (32 if typ in (5, 7) else 128), row def test_remote_complete_typed_snapshot(self): actual = self.remote_read() expected = self.oracle["remote"] assert set(actual) == set(expected), {"missing": sorted(expected.keys() - actual.keys()), "extra": sorted(actual.keys() - expected.keys())} for ioa, (tag, text) in expected.items(): row = actual[ioa] assert row["tag"] == tag, row value = text if tag == 4 else float(text) if tag == 38 else int(text) assert math.isclose(row["value"], value, rel_tol=1e-6, abs_tol=1e-7) if tag == 38 else row["value"] == value, row def test_area_switch_and_readback(self): for area in (1, 4, 0): self.client.request(asdu(200, u24(0) + struct.pack("= 100). Cost note (measured 2026-11-18, must be respected when editing this test): one full GI here is ~126 I-frames / ~3.9 s wall clock, because the 120 standard parameter rows cannot be coalesced - they span types 110/111/112 and each ASDU carries exactly one object. Call this only as often as the assertion genuinely needs; every extra call costs ~4 s in the manual gate. """ self.client.request(asdu(100, u24(0) + b"\x14")) self.client.response(100, 7) rows = [] while True: data, _ = self.client.receive(lambda d: header(d)["cot"] in (20, 10)) if header(data)["type"] == 100: assert header(data)["cot"] == 10 break rows.extend(objects(data)) self.decoded.extend(rows) return {row["ioa"]: row for row in rows if row["type"] >= 100} def param_load(self, type_id, ioa, value, kpa, test=False, cot=6): payload = struct.pack("=4 is type 110, else 111 # group 1 (0x8621..): kind LOW_LIMIT(3), type 111 # group 2 (0x8641..): kind HIGH_LIMIT(4), type 111 # group 3 (0x8661..): kind SMOOTHING(2), type 112 before = self.standard_params() assert before[0x8601]["type"] == 111 and before[0x8601]["value"] == 200 assert before[0x8605]["type"] == 110 and before[0x8605]["value"] == 655 assert before[0x8661]["type"] == 112 and before[0x8661]["value"] == 0.0 # --- accepted LOADs, one per type ------------------------------------- data, h = self.param_load(110, 0x8605, 700, 1) assert h["cot"] == 7 and not h["pn"], ("110 LOAD must be accepted", data.hex()) assert objects(data)[0]["value"] == 700, "positive CON must carry the stored value" data, h = self.param_load(111, 0x8621, -9999, 3) assert h["cot"] == 7 and not h["pn"], ("111 LOAD must be accepted", data.hex()) data, h = self.param_load(112, 0x8661, 12.5, 2) assert h["cot"] == 7 and not h["pn"], ("112 LOAD must be accepted", data.hex()) # --- rejection: unknown IOA / unknown CA / unknown COT ---------------- # These use the standard error causes and echo the request bytes verbatim, # because no point row matched so no running value could be frozen. data, h = self.param_load(111, 0x9999, -5, 1) assert h["cot"] == 47 and h["pn"], ("unknown IOA -> COT 47", data.hex()) data, h = self.param_load(111, 0x8601, -5, 1, cot=3) assert h["cot"] == 45 and h["pn"], ("unknown COT -> COT 45", data.hex()) # --- rejection: KPA (kind) must match the row ------------------------- data, h = self.param_load(111, 0x8601, -5, 3) assert h["cot"] == 7 and h["pn"], ("kind mismatch must be a negative CON", data.hex()) assert objects(data)[0]["value"] == -5, \ "address matched but kind did not, so no running value was frozen: reply echoes the request" # --- rejection: type id must match the row ---------------------------- data, h = self.param_load(110, 0x8601, -5, 1) assert h["cot"] == 7 and h["pn"], ("type mismatch must be a negative CON", data.hex()) assert objects(data)[0]["value"] == -5, "type mismatch likewise echoes the request" # --- rejection: range/content per group ------------------------------- # group 0 requires a non-negative value, so -1 is refused on a 110 row. # NOTE the contrast with the two cases above: the row DID match here, so # the module froze the running value and the negative CON carries 700 # (the current value), not the refused request value. Getting this # backwards is the easiest way to write a test that passes for the wrong # reason, so both shapes are asserted explicitly. data, h = self.param_load(110, 0x8605, -1, 1) assert h["cot"] == 7 and h["pn"], ("negative on unsigned-style row must be refused", data.hex()) assert objects(data)[0]["value"] == 700, \ "row matched so the running value is frozen: negative CON carries the CURRENT value, not the request" data, h = self.param_load(111, 0x8621, -10001, 3) # below LOW_LIMIT's own floor assert h["cot"] == 7 and h["pn"], "below low limit must be refused" data, h = self.param_load(111, 0x8641, 10001, 4) # above HIGH_LIMIT's own ceiling assert h["cot"] == 7 and h["pn"], "above high limit must be refused" data, h = self.param_load(112, 0x8661, 61.0, 2) # smoothing is 0..60 s assert h["cot"] == 7 and h["pn"], "float above 60 must be refused" data, h = self.param_load(112, 0x8661, float("nan"), 2) assert h["cot"] == 7 and h["pn"], "NaN must be refused before it can poison the store" # --- TEST bit: accepted, but must not mutate -------------------------- data, h = self.param_load(111, 0x8621, -100, 3, test=True) assert h["cot"] == 7 and not h["pn"], ("TEST LOAD must be accepted", data.hex()) # --- ONE readback verifies everything above at once ------------------- # Single GI, deliberately: each call costs ~4 s (see standard_params). # It must show the three accepted values, prove the six refusals and the # TEST request moved nothing, and prove an untouched row stayed put. after = self.standard_params() assert after[0x8605]["value"] == 700, "110 LOAD did not reach the value store" assert after[0x8621]["value"] == -9999, "111 LOAD did not reach the value store, or a refusal/TEST leaked" assert after[0x8661]["value"] == 12.5, "112 LOAD did not reach the value store, or a refusal leaked" assert after[0x8601]["value"] == 200, "untouched row must not move" assert after[0x8641]["value"] == 10000, "high-limit row was mutated by a refusal" assert after[0x8622]["value"] == -10000, "TEST LOAD mutated a sibling row in its group" # --- restore so the run is repeatable --------------------------------- for type_id, ioa, value, kpa in ((110, 0x8605, 655, 1), (111, 0x8621, -10000, 3), (112, 0x8661, 0.0, 2)): data, h = self.param_load(type_id, ioa, value, kpa) assert h["cot"] == 7 and not h["pn"], ("restore failed", data.hex()) # A second GI is the only way to prove the restore actually landed; it is # worth the ~4 s because a silent restore failure would poison every later # GI-based test in this suite. assert self.standard_params() == before, "restore did not return the exact initial snapshot" def test_standard_parameter_activation_113(self): # QPA selects the activation flavour. Only QPA=3 (cyclic transmission) is # enabled by default; QPA=1/2 are gated behind config extensions that this # build does not turn on, so they must be refused rather than silently # accepted. This distinction is the point of the test. data, h = self.param_activate(0, 3) assert h["cot"] == 7 and not h["pn"], ("QPA=3 activate must be accepted", data.hex()) data, h = self.param_activate(0, 3, cot=8) assert h["cot"] == 9 and not h["pn"], ("QPA=3 deactivate must be confirmed with COT 9", data.hex()) # A non-zero IOA with QPA=3 reaches the application, which refuses it # because periodic transmission is addressed by IOA 0 only. data, h = self.param_activate(0x1234, 3) assert h["cot"] == 7 and h["pn"], ("QPA=3 with IOA!=0 must be refused", data.hex()) for qpa in (1, 2): data, h = self.param_activate(0, qpa) assert h["cot"] == 7 and h["pn"], \ ("QPA=%d must be refused: its config extension is off" % qpa, data.hex()) # TEST bit is accepted and must not alter the schedule. data, h = self.param_activate(0, 3, test=True) assert h["cot"] == 7 and not h["pn"], ("TEST activate must be accepted", data.hex()) # Error causes. data, h = self.param_activate(0, 3, cot=3) assert h["cot"] == 45 and h["pn"], ("unknown COT -> COT 45", data.hex()) self.client.request(asdu(113, u24(0) + bytes((3,)), ca=9)) data, _ = self.client.receive(lambda d: header(d)["type"] == 113) h = header(data) assert h["cot"] == 46 and h["pn"], ("unknown CA -> COT 46", data.hex()) # Leave the periodic scan off so it cannot perturb anything after us. data, h = self.param_activate(0, 3, cot=8) assert h["cot"] == 9 and not h["pn"], ("final deactivate failed", data.hex()) READY_PATTERN = re.compile(r"^\[iec104\] READY address=(?P
\S+) port=(?P\d+) ca=(?P\d+) mode=(?P\S+)\s*$") class ReadyWatcher: """转发模拟器 stdout 到日志文件,并从中取回 READY 行里的实际监听端口。 为什么不靠轮询端口探活(2026-11-18 改): 1) 探活的每一次 connect 都会被固件当成一次真实接入,白白吃掉一次 accept, 让 STOP 摘要里的 connections 多算一次,也让单连接模型的会话序列多一段; 2) 端口固定时探活"成功"只说明有人在这个号上监听,并行构建或端口被别的进程 占住时会把真正的失败伪装成通过; 3) READY 行里的 port 是固件 bind 成功后回报的 bound_port,--iec104-port 0 时 这正是唯一能拿到实际端口的途径(simulator/README.md 明确要求读它)。 日志必须保留:它带着 START/READY/STOP 摘要,是人工复核的证据面。 """ def __init__(self, log): self.log = log self.ready = threading.Event() self.port = None self.ca = None self.address = None self.mode = None self.error = None self._thread = threading.Thread(target=self._pump, name="sim-stdout", daemon=True) def start(self, stream): self._stream = stream self._thread.start() def _pump(self): try: for line in self._stream: self.log.write(line) self.log.flush() match = READY_PATTERN.match(line) if match and not self.ready.is_set(): self.port = int(match.group("port")) self.ca = int(match.group("ca")) self.address = match.group("address") self.mode = match.group("mode") self.ready.set() except Exception as exc: # 读管道失败也要让主线程拿到结论 self.error = exc self.ready.set() def join(self, timeout=5.0): self._thread.join(timeout) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--exe", type=Path) parser.add_argument("--host", default="127.0.0.1") parser.add_argument("--port", type=int, default=24040) # 这里是"等一个不可能发生的 vendor 超时"的窗口,不是真实超时值。 # 所有命令都被否定确认(COT 7 + P/N=1),库里根本不会进入选择/执行事务, # 因此 select(30s)/decision(15s)/execute(60s) 三个计时器都不会被启动。 # 保留 2s 只是给"迟到的 ACT_TERM"留一个可观测窗口;若将来命令被受理, # 必须调回 30 或更大,否则这条用例会在超时真正到期前就宣告通过(假绿)。 parser.add_argument("--sbo-timeout", type=float, default=2) parser.add_argument("--output", type=Path, default=ROOT / "ci-artifacts" / "iec104-tests") parser.add_argument("--only", default="") args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=True) suite = Acceptance(args) process, log, watcher = None, None, None binary = {} with tempfile.TemporaryDirectory(prefix="iec104-dtu-") as cwd: try: if args.exe: exe = args.exe.resolve() binary = {"path": str(exe), "sha256": hashlib.sha256(exe.read_bytes()).hexdigest()} log = (args.output / "simulator.log").open("w", encoding="utf-8") process = subprocess.Popen( [str(exe), "--iec104", "--iec104-headless", "--iec104-port", str(args.port), "--iec104-duration", "180"], cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", bufsize=1) watcher = ReadyWatcher(log) watcher.start(process.stdout) if not watcher.ready.wait(15.0): raise TimeoutError("simulator did not print READY within 15s") if watcher.error is not None: raise RuntimeError("simulator stdout plumbing failed: " + str(watcher.error)) if watcher.port is None: raise RuntimeError("simulator exited before listen: " + str(process.poll())) # 用固件回报的 bound_port 连接,而不是我们请求的那个号:--iec104-port 0 时 # 请求号无意义,且这样连等于断言"READY 里说的端口真的在听"。 args.port = watcher.port binary["ready"] = {"address": watcher.address, "port": watcher.port, "ca": watcher.ca, "mode": watcher.mode} names = ["test_link_start_stop_testfr_and_prestart", "test_gi_exact_set_and_zero_observations", "test_counter_exact_set_zero_and_invalid", "test_read_all_runtime_addresses", "test_remote_complete_typed_snapshot", "test_area_switch_and_readback", "test_typed_preset_commit_cancel_readback", "test_unknown_and_bad_typed_requests", "test_standard_parameter_load_110_111_112", "test_standard_parameter_activation_113", "test_maximum_apdu_and_coalesced_frames", "test_sbo_order_and_refusal_semantics", "test_clock_is_device_only_and_no_command_completes"] for name in names: if not args.only or args.only in name: suite.run(name, getattr(suite, name)) finally: if process and process.poll() is None: process.terminate() process.wait(timeout=10) # 先收干 stdout 再关日志:进程被 terminate 后管道里可能还有最后几行 # (含 STOP 摘要),丢了就等于丢掉唯一的人工复核证据。 if watcher: watcher.join(5.0) if process and process.stdout: process.stdout.close() if log: log.close() report = {"created": datetime.datetime.now(datetime.timezone.utc).isoformat(), "binary": binary, "reference": suite.oracle, "results": suite.results, "limitations": ["Service-side short-write/WOULD_BLOCK requires separate transport unit evidence", "No-physical-DO requires source/build audit in addition to TCP feedback", "TCP process is terminated by harness after tests"]} (args.output / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8") (args.output / "wire.json").write_text(json.dumps(suite.trace, indent=2), encoding="utf-8") (args.output / "objects.json").write_text(json.dumps(suite.decoded, indent=2), encoding="utf-8") return 0 if suite.results and all(row["passed"] for row in suite.results) else 1 if __name__ == "__main__": raise SystemExit(main())