opcua-kit 0.2.0

Embeddable OPC UA client, server and UADP library for Rust applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""Exercise the default Rust library and opt-in CLI from the extracted Cargo package."""

import argparse
import json
import os
import subprocess
import tempfile
from pathlib import Path


def cargo(*args):
    return subprocess.run(
        ["cargo", *args], text=True, check=True, stdout=subprocess.PIPE
    ).stdout


def verify_library_consumer(root, source, rust_version, name, *, profile=None):
    consumer = root / name
    (consumer / "src").mkdir(parents=True)
    manifest = consumer / "Cargo.toml"
    features = (
        f', default-features = false, features = ["{profile}"]' if profile else ""
    )
    time_dep = 'time = "0.3"' if profile == "cems" else ""
    content = f"""[package]
name = "consumer-{name}"
version = "0.0.0"
edition = "2024"
rust-version = "{rust_version}"

[dependencies]
opcua-kit = {{ path = {json.dumps(str(source), ensure_ascii=False)}{features} }}
{time_dep}
"""
    manifest.write_text(content)
    source_file = {
        None: "default.rs",
        "client": "session.rs",
        "commissioning": "commissioning.rs",
        "cems": "meter.rs",
    }[profile]
    (consumer / "src/main.rs").write_bytes(
        (source / "tests/consumer" / source_file).read_bytes()
    )
    (consumer / "Cargo.lock").write_bytes((source / "Cargo.lock").read_bytes())
    args = ["--manifest-path", str(manifest)]
    graph = json.loads(cargo("metadata", "--format-version=1", *args))
    names = {p["id"]: p["name"] for p in graph["packages"]}
    for node in graph["resolve"]["nodes"]:
        package = names[node["id"]]
        assert package not in {"chrono", "chrono-tz", "clap", "signal-hook"}, (
            name,
            package,
        )
        if package == "opcua-kit":
            expected = (
                {"commissioning", profile}
                if profile
                else {"discovery", "default", "pubsub"}
            )
            if profile == "client":
                expected = {"client", "discovery"}
            assert set(node["features"]) == expected, node
        if package == "time":
            assert "local-offset" not in node["features"], node
    if profile is None:
        assert set(names.values()) == {"opcua-kit", f"consumer-{name}"}, names
    if profile == "commissioning":
        assert "time" not in names.values()
    fixture = "meter" if profile == "cems" else "commissioning"
    run_args = (
        [str(source / "fixtures" / fixture)]
        if profile in ("commissioning", "cems")
        else []
    )
    cargo("run", "--locked", *args, "--", *run_args)
    print(f"Verified {name} Rust consumer.")


parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
    "--allow-dirty", action="store_true", help="package edits in this checkout"
)
parser.add_argument(
    "--cemsd", type=Path, help="also run the installed trace through this EMS consumer"
)
options = parser.parse_args()
cemsd = options.cemsd.resolve(strict=True) if options.cemsd else None
package_args = ["--locked"]
if options.allow_dirty:
    package_args.append("--allow-dirty")
cargo("package", *package_args)
files = cargo("package", *package_args, "--list").splitlines()
assert "fixtures/units/contract.json" in files
assert {"CHANGELOG.md", "CONTRIBUTING.md", "LICENSE-OPC-Foundation"} <= set(files)
assert not any(f.startswith(("vendor/", "contrib/open62541")) for f in files)
assert "site/content/client.md" in files
assert "site/content/services.md" in files
assert {
    "examples/uadp.rs",
    "examples/local_server.rs",
    "src/pubsub.rs",
    "src/server.rs",
} <= set(files)
assert {
    "src/secure_channel.rs",
    "src/client/connection.rs",
    "src/ua/value.rs",
    "src/ua/generated.rs",
    "examples/discover.rs",
} <= set(files)
assert all(f.endswith(".rs") for f in files if f.startswith("src/"))
assert "site/content/authoring.md" in files
assert "site/content/meter.md" in files
assert "fixtures/meter/readings.ndjson" in files
assert not any(f.startswith(("concepts/", "specs/", ".specify/")) for f in files)
meta = json.loads(cargo("metadata", "--format-version=1", "--no-deps"))
crate = next(p for p in meta["packages"] if p["name"] == "opcua-kit")
build_directory = meta.get("build_directory", meta["target_directory"])
source = Path(build_directory, "package", "opcua-kit-" + crate["version"])

with tempfile.TemporaryDirectory() as scratch:
    root = Path(scratch)
    checkout = Path(crate["manifest_path"]).parent
    verify_library_consumer(root, source, crate["rust_version"], "archive")
    for feature in ("client", "commissioning", "cems"):
        verify_library_consumer(
            root,
            source,
            crate["rust_version"],
            f"archive-{feature}",
            profile=feature,
        )
    # Verify optional roles against the normalized archive and its resolved registry dependencies.
    for feature, example in (("server", "local_server"), ("pubsub", "uadp")):
        cargo(
            "build",
            "--locked",
            "--manifest-path",
            str(source / "Cargo.toml"),
            "--no-default-features",
            "--features",
            feature,
            "--example",
            example,
        )
    cargo(
        "check",
        "--locked",
        "--manifest-path",
        str(source / "Cargo.toml"),
        "--features",
        "control",
    )
    # The archive must resolve independently for every role without build-time C tooling.
    archive = json.loads(
        cargo(
            "metadata",
            "--locked",
            "--format-version=1",
            "--all-features",
            "--manifest-path",
            str(source / "Cargo.toml"),
        )
    )
    assert all(
        p["source"] and p["source"].startswith("registry+")
        for p in archive["packages"]
        if p["name"] != "opcua-kit"
    )
    forbidden = {
        "async-opcua-types",
        "chrono",
        "open62541",
        "open62541-sys",
        "openssl",
        "openssl-sys",
        "aws-lc-sys",
        "ring",
        "mbedtls",
        "mbedtls-sys-auto",
        "cmake",
        "bindgen",
        "signal-hook",
    }
    assert not forbidden.intersection(p["name"] for p in archive["packages"])
    verify_library_consumer(
        root, checkout, crate["rust_version"], "checkout", profile="client"
    )
    cargo(
        "install",
        "--locked",
        "--features",
        "cli,cems",
        "--path",
        str(source),
        "--root",
        str(root / "kit"),
    )
    binary = root / "kit/bin/opcua-kit"

    def invoke(code, *args, stdin=None):
        r = subprocess.run(
            [binary, *args],
            cwd=root,
            env={**os.environ, "PATH": ""},
            input=stdin,
            capture_output=True,
            check=False,
            timeout=10,
        )
        assert r.returncode == code, (r.returncode, r.stderr)
        return r.stdout

    # 009-safe-export/SR-001 009-safe-export/SR-002
    def save_trace(filename, args, source, stdin=None):
        expected = invoke(0, *args, source, stdin=stdin)
        assert invoke(0, *args, source, "--output", filename, stdin=stdin) == b""
        path = root / filename
        assert path.read_bytes() == expected
        return path

    # 006-meter-pilot/SR-002 006-meter-pilot/SR-003
    invoke(0, "meter", "example", "--export", "shift")
    trace = (
        "meter",
        "trace",
        "--contract",
        "shift/contract.json",
        "--window",
        "shift/window.json",
        "--readings",
    )
    readings = (root / "shift/readings.ndjson").read_bytes()
    saved_good = save_trace("trace.json", trace, "shift/readings.ndjson")
    good_trace = saved_good.read_bytes()
    assert good_trace == invoke(0, *trace, "-", stdin=readings)
    assert invoke(1, *trace, "shift/readings.ndjson", "--output", "-") == b""
    assert not (root / "-").exists()
    assert invoke(0, *trace, "shift/readings.ndjson", "--output", "./-") == b""
    assert (root / "-").read_bytes() == good_trace
    (root / "-").unlink()
    records = [json.loads(line) for line in readings.splitlines()]
    assert [p["wh"] for p in json.loads(good_trace)["register"]] == [
        r["value"] for r in records
    ]
    # Substitute an unusable read at the boundary shared by two quarters.
    shared = records[-2]
    shared["status"] = 0x80340000
    del shared["value"]
    batch = "\n".join(map(json.dumps, records)).encode()
    saved_gap = save_trace("gap.json", trace, "-", stdin=batch)
    gap_trace = saved_gap.read_bytes()
    assert invoke(1, *trace, "-", "--output", "trace.json", stdin=batch) == b""
    assert saved_good.read_bytes() == good_trace
    assert invoke(1, *trace, "-", "--output", "invalid.json", stdin=b"invalid") == b""
    assert not (root / "invalid.json").exists()
    assert len(json.loads(gap_trace)["register"]) == len(records) - 1
    # Interior readings remain available to the consumer's decrease detector.
    reset_records = [json.loads(line) for line in readings.splitlines()]
    reset_records[1]["value"] = reset_records[0]["value"] - 1
    reset = "\n".join(map(json.dumps, reset_records)).encode()
    saved_reset = save_trace("reset.json", trace, "-", stdin=reset)
    # 007-float-meter/SR-002 008-double-meter/SR-002. Both floating-point
    # fixtures add a fractional interior sample that cannot supply whole Wh.
    cases = [
        (saved_good, 0, ["within_tolerance"] * 2),
        (saved_gap, 1, ["missing_boundary"] * 2),
        (saved_reset, 1, ["register_decreased", "within_tolerance"]),
    ]
    for dtype, offset, coarse_word in [
        ("float", 0, 0x4B800000),
        ("double", 4_000_000_001, "4340000000000000"),
    ]:
        invoke(0, "meter", "example", "--register-type", dtype, "--export", dtype)
        args = (
            "meter",
            "trace",
            "--contract",
            f"{dtype}/contract.json",
            "--window",
            f"{dtype}/window.json",
            "--readings",
        )
        sample = save_trace(f"{dtype}-trace.json", args, f"{dtype}/readings.ndjson")
        expected = json.loads(good_trace)
        for point in expected["register"]:
            point["wh"] += offset
        assert json.loads(sample.read_bytes()) == expected
        rows = [
            json.loads(line)
            for line in (root / dtype / "readings.ndjson").read_bytes().splitlines()
        ]
        shared_at = json.loads((root / dtype / "window.json").read_bytes())["closed"][
            1
        ]["at"]
        shared_row = next(row for row in rows if row["sourceAt"] == shared_at)
        shared_row["valueBits"] = coarse_word
        batch = "\n".join(map(json.dumps, rows)).encode()
        gap = save_trace(f"{dtype}-gap.json", args, "-", stdin=batch)
        expected_gap = json.loads(gap_trace)
        for point in expected_gap["register"]:
            point["wh"] += offset
        assert json.loads(gap.read_bytes()) == expected_gap
        cases.extend(
            [(sample, 0, ["within_tolerance"] * 2), (gap, 1, ["missing_boundary"] * 2)]
        )

    if cemsd and os.name == "posix":
        # The shell creates the consumer report only if the right side of &&
        # runs. Keep all paths in positional arguments, outside shell source.
        chain = subprocess.run(
            [
                "sh",
                "-c",
                (
                    '"$1" meter trace --contract shift/contract.json --window shift/window.json '
                    '--readings - --output rejected.json && "$2" reconcile rejected.json > downstream.json'
                ),
                "handoff",
                str(binary),
                str(cemsd),
            ],
            cwd=root,
            input=b"invalid",
            capture_output=True,
            check=False,
            timeout=10,
        )
        assert chain.returncode == 1 and chain.stdout == b""
        assert not (root / "rejected.json").exists()
        assert not (root / "downstream.json").exists()

    if cemsd:
        for trace_file, expected, states in cases:
            actual = subprocess.run(
                [cemsd, "reconcile", trace_file],
                cwd=root,
                capture_output=True,
                check=False,
                timeout=10,
            )
            assert actual.returncode == expected, actual.stderr
            report = json.loads(actual.stdout)
            assert [i["status"] for i in report["intervals"]] == states
            if expected == 0:
                assert [i["difference_wh"] for i in report["intervals"]] == [0, 1]
        print(
            "Real cemsd confirmed integer, Float and Double quarters, missing quality and precision boundaries, and an interior counter decrease."
        )

    invoke(0, "demo", "--scenario", "units", "--export", "visit")
    # 022-native-commissioning/SR-002
    # Generate short-lived application identities for the installed CLI's offline check.
    # Only the fixture generator needs openssl. All CLI invocations run with an empty PATH.
    device_uri = json.loads((root / "visit/contract.json").read_bytes())[
        "applicationUri"
    ]
    for role, uri in (("server", device_uri), ("client", "urn:test:packaged-client")):
        config = root / f"{role}.cnf"
        config.write_text(
            "[req]\nprompt = no\ndistinguished_name = subject\nx509_extensions = profile\n"
            f"[subject]\nCN = {role}\n[profile]\nsubjectAltName = IP:127.0.0.1,URI:{uri}\n"
            "basicConstraints = critical,CA:FALSE\n"
            "keyUsage = critical,digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment\n"
            f"extendedKeyUsage = {role}Auth\n"
        )
        subprocess.run(
            [
                "openssl",
                "req",
                "-new",
                "-x509",
                "-newkey",
                "rsa:2048",
                "-nodes",
                "-days",
                "1",
                "-config",
                str(config),
                "-keyout",
                f"{role}.key",
                "-out",
                f"{role}.crt",
            ],
            cwd=root,
            capture_output=True,
            check=True,
            timeout=10,
        )
    prepare = (
        "capture",
        "--preflight",
        "--contract",
        "visit/contract.json",
        "--endpoint",
        "opc.tcp://127.0.0.1:4840",
        "--server-cert",
        "server.crt",
        "--client-cert",
        "client.crt",
        "--client-key",
        "client.key",
    )
    ready = invoke(0, *prepare)
    assert b"offline" in ready.lower() and b"endpoint" in ready.lower()
    assert invoke(1, *prepare, "--output", "unused.json") == b""
    assert not (root / "unused.json").exists()
    # 004-contract-authoring/SR-001 004-contract-authoring/SR-003
    draft_args = ("contract", "draft", "--snapshot")
    validate_args = ("contract", "validate", "--contract")
    reference = (root / "visit/before.json").read_bytes()
    draft = invoke(0, *draft_args, "visit/before.json")
    assert draft == invoke(0, *draft_args, "-", stdin=reference)
    assert invoke(1, *draft_args, "visit/before.json", "--output", "-") == b""
    assert not (root / "-").exists()
    assert invoke(0, *draft_args, "visit/before.json", "--output", "./-") == b""
    assert (root / "-").read_bytes() == draft
    assert draft.endswith(b"\n")
    assert json.loads(draft) == json.loads((root / "visit/contract.json").read_bytes())
    assert invoke(0, *draft_args, "visit/before.json", "--output", "draft.json") == b""
    assert (root / "draft.json").read_bytes() == draft
    summary = invoke(0, *validate_args, "draft.json")
    assert b"1 signal" in summary and summary == invoke(
        0, *validate_args, "-", stdin=draft
    )
    author_check = ("check", "--contract", "draft.json", "--snapshot")
    invoke(0, *author_check, "visit/before.json")
    assert b"SupplyTemp.EngineeringUnits" in invoke(
        2, *author_check, "visit/after.json"
    )
    edited = json.loads(draft)
    edited["signals"][0]["valueRank"] = -1
    edited_bytes = json.dumps(edited).encode()
    (root / "draft.json").write_bytes(edited_bytes)
    assert invoke(1, *draft_args, "-", "--output", "draft.json", stdin=reference) == b""
    assert (root / "draft.json").read_bytes() == edited_bytes
    invoke(0, *validate_args, "draft.json")
    assert b"UNVERIFIED" in invoke(3, *author_check, "visit/before.json")
    incomplete = json.loads(reference)
    incomplete["signals"][0]["dataType"] = {"status": 0x80340000}
    assert (
        invoke(
            1,
            *draft_args,
            "-",
            "--output",
            "unusable.json",
            stdin=json.dumps(incomplete).encode(),
        )
        == b""
    )
    assert not (root / "unusable.json").exists()
    assert invoke(1, *draft_args, "-", stdin=json.dumps(incomplete).encode()) == b""
    assert invoke(1, *validate_args, "-", stdin=b"{}") == b""
    check = ("check", "--contract", "visit/contract.json")
    after = (root / "visit/after.json").read_bytes()
    assert b"First inspection" in invoke(0, *check, "--snapshot", "visit/before.json")
    empty = json.loads(after)
    empty["signals"] = []
    (root / "visit/empty.json").write_text(json.dumps(empty))
    for code, name, decision in (
        (0, "before", "matched"),
        (2, "after", "mismatch"),
        (3, "empty", "unverified"),
    ):
        path = f"visit/{name}.json"
        raw = (root / path).read_bytes()
        current = invoke(code, *check, "--snapshot", path, "--json")
        assert invoke(code, *check, "--snapshot", "-", "--json", stdin=raw) == current
        report = json.loads(current)
        assert report["schemaVersion"] == 2 and report["baselineProvided"] is False
        assert report["decision"] == decision and report["complete"] == (code != 3)
        observed_rows = json.loads(raw)["signals"]
        row = observed_rows[0] if observed_rows else {}
        fields = {"DataType": "dataType", "EngineeringUnits": "engineeringUnits"}
        assert [f["attribute"] for f in report["findings"]] == list(fields)
        for finding in report["findings"]:
            assert finding["name"] == "SupplyTemp"
            assert finding["beforeOutcome"] is None and finding["before"] is None
            assert finding["after"] == row.get(fields[finding["attribute"]])
    mismatch = invoke(2, *check, "--snapshot", "visit/after.json")
    assert b"SupplyTemp.EngineeringUnits" in mismatch
    assert "expected °C (CEL), observed °F (FAH)" in mismatch.decode()
    repaired = invoke(
        0, *check, "--before", "visit/after.json", "--after", "visit/before.json"
    )
    assert b"RESOLVED" in repaired
    assert invoke(1, *check, "--snapshot", "-", "--json", stdin=after + b"{}") == b""
    print(
        "Packaged CLI verified outside checkout for local preflight, exits 0/1/2/3, file/pipe parity, absent history and repair."
    )