S7 integration fixture — python-snap7 server closes the wire-level coverage gap (#216) + per-driver fixture coverage docs for every driver in the fleet. Closes #216. Two shipments in one PR because the docs landed as I surveyed each driver's fixture + the S7 work is the first wire-level-gap closer pulled from that survey.

S7 integration — AbCip/Modbus already have real-simulator integration suites; S7 had zero wire-level coverage despite being a Tier-A driver (all unit tests mocked IS7Client). Picked python-snap7's `snap7.server.Server` over raw Snap7 C library because `pip install` beats per-OS binary-pin maintenance, the package ships a Python __main__ shim that mirrors our existing pymodbus serve.ps1 + *.json pattern structurally, and the python-snap7 project is actively maintained. New project `tests/ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests/` with four moving parts: (a) `Snap7ServerFixture` — collection-scoped TCP probe on `localhost:1102` that sets `SkipReason` when the simulator's not running, matching the `ModbusSimulatorFixture` shape one directory over (same S7_SIM_ENDPOINT env var override convention for pointing at a real S7 CPU on port 102); (b) `PythonSnap7/` — `serve.ps1` wrapper + `server.py` shim + `s7_1500.json` seed profile + `README.md` documenting install / run / known limitations; (c) `S7_1500/S7_1500Profile.cs` — driver-side `S7DriverOptions` whose tag addresses map 1:1 to the JSON profile's seed offsets (DB1.DBW0 u16, DB1.DBW10 i16, DB1.DBD20 i32, DB1.DBD30 f32, DB1.DBX50.3 bool, DB1.DBW100 scratch); (d) `S7_1500SmokeTests` — three tests proving typed reads + write-then-read round-trip work through real S7netplus + real ISO-on-TCP + real snap7 server. Picked port 1102 default instead of S7-standard 102 because 102 is privileged on Linux + triggers Windows Firewall prompt; S7netplus 0.20 has a 5-arg `Plc(CpuType, host, port, rack, slot)` ctor that lets the driver honour `S7DriverOptions.Port`, but the existing driver code called the 4-arg overload + silently hardcoded 102. One-line driver fix (S7Driver.cs:87) threads `_options.Port` through — the S7 unit suite (58/58) still passes unchanged because every unit test uses a fake IS7Client that never sees the real ctor. Server seed-type matrix in `server.py` covers u8 / i8 / u16 / i16 / u32 / i32 / f32 / bool-with-bit / ascii (S7 STRING with max_len header). register_area takes the SrvArea enum value, not the string name — a 15-minute debug after the first test run caught that; documented inline.

Per-driver test-fixture coverage docs — eight new files in `docs/drivers/` laying out what each driver's harness actually benchmarks vs. what's trusted from field deployments. Pattern mirrors the AbServer-Test-Fixture.md doc that shipped earlier in this arc: TL;DR → What the fixture is → What it actually covers → What it does NOT cover → When-to-trust table → Follow-up candidates → Key files. Ugly truth the survey made visible: Galaxy + Modbus + (now) S7 + AB CIP have real wire-level coverage; AB Legacy / TwinCAT / FOCAS / OpcUaClient are still contract-only because their libraries ship no fake + no open-source simulator exists (AB Legacy PCCC), no public simulator exists (FOCAS), the vendor SDK has no in-process fake (TwinCAT/ADS.NET), or the test wiring just hasn't happened yet (OpcUaClient could trivially loopback against this repo's own server — flagged as #215). Each doc names the specific follow-up route: Snap7 server for S7 (done), TwinCAT 3 developer-runtime auto-restart for TwinCAT, Tier-C out-of-process Host for FOCAS, lab rigs for AB Legacy + hardware-gated bits of the others. `docs/drivers/README.md` gains a coverage-map section linking all eight. Tracking tasks #215-#222 filed for each PR-able follow-up.

Build clean (driver + integration project + docs); S7.Tests 58/58 (unchanged); S7.IntegrationTests 3/3 (new, verified end-to-end against a live python-snap7 server: `driver_reads_seeded_u16_through_real_S7comm`, `driver_reads_seeded_typed_batch`, `driver_write_then_read_round_trip_on_scratch_word`). Next fixture follow-up is #215 (OpcUaClient loopback against own server) — highest ROI of the remaining set, zero external deps.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-04-20 11:29:15 -04:00
parent 4fe96fca9b
commit 1d3544f18e
19 changed files with 1605 additions and 1 deletions

View File

@@ -0,0 +1,110 @@
# python-snap7 server profiles
JSON-driven seed profiles for `snap7.server.Server` from
[python-snap7](https://github.com/gijzelaerr/python-snap7) (MIT). Shape
mirrors the pymodbus profiles under
`tests/ZB.MOM.WW.OtOpcUa.Driver.Modbus.IntegrationTests/Pymodbus/` — a
PowerShell launcher + per-family JSON + a Python shim that the launcher
exec's.
| File | What it seeds | Test category |
|---|---|---|
| [`s7_1500.json`](s7_1500.json) | DB1 (1024 bytes) with smoke values at known offsets (i16 @ DBW10, i32 @ DBD20, f32 @ DBD30, bool @ DBX50.3, scratch word @ DBW100, STRING "Hello" @ 200) + MB (256 bytes) with probe marker at MW0. | `Trait=Integration, Device=S7_1500` |
Default port **1102** (non-privileged; sidesteps Windows Firewall prompt +
Linux's root-required bind on port 102). The fixture
(`Snap7ServerFixture`) defaults to `localhost:1102`. Override via
`S7_SIM_ENDPOINT` to point at a real S7 CPU on port 102. The S7 driver
threads `_options.Port` through to S7netplus's 5-arg `Plc` ctor, so the
non-standard port works end-to-end.
## Install
```powershell
pip install "python-snap7>=2.0"
```
`python-snap7` wraps the upstream `snap7` C library; the install pulls
platform-specific binaries automatically. Requires Python ≥ 3.10.
Windows Firewall will prompt on first bind; allow Private network.
## Run
Foreground (Ctrl+C to stop):
```powershell
.\serve.ps1 -Profile s7_1500
```
Non-default port:
```powershell
.\serve.ps1 -Profile s7_1500 -Port 102
```
Or invoke the Python shim directly:
```powershell
python .\server.py .\s7_1500.json --port 1102
```
## Run the integration tests
In a separate shell with the simulator running:
```powershell
cd C:\Users\dohertj2\Desktop\lmxopcua
dotnet test tests\ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests
```
Tests auto-skip with a clear `SkipReason` when `localhost:1102` isn't
reachable within 2 seconds.
## What's encoded in `s7_1500.json`
| Address | Type | Seed | Purpose |
|---|---|---|---|
| `DB1.DBW0` | u16 | `4242` | read-back probe |
| `DB1.DBW10` | i16 | `-12345` | smoke i16 read |
| `DB1.DBD20` | i32 | `1234567890` | smoke i32 read |
| `DB1.DBD30` | f32 | `3.14159` | smoke f32 read (big-endian) |
| `DB1.DBX50.3` | bool | `true` | smoke bool read at bit 3 |
| `DB1.DBW100` | u16 | `0` | scratch for write-then-read |
| `DB1.STRING[200]` | S7 STRING | `"Hello"` (max 32, cur 5) | smoke string read |
| `MW0` | u16 | `1` | `S7ProbeOptions.ProbeAddress` default |
Seed types supported by `server.py`: `u8`, `i8`, `u16`, `i16`, `u32`,
`i32`, `f32`, `bool` (with `"bit": 0..7`), `ascii` (S7 STRING type with
configurable `max_len`).
## Known limitations (python-snap7 upstream)
The `snap7.server.Server` docstring admits:
> "Legacy S7 server implementation. Emulates a Siemens S7 PLC for testing
> and development purposes. [...] pure Python emulator implementation that
> simulates PLC behaviour for protocol compliance testing rather than
> full industrial-grade functionality."
What that means in practice — things this fixture does NOT cover:
- **S7-1500 Optimized-DB symbolic access** — the real S7-1500 with TIA Portal
optimization enabled uses symbolic addressing that's wire-incompatible with
absolute DB addressing. Our driver targets non-optimized DBs; so does
snap7's server. Rig test required to verify against an Optimized CPU.
- **PG / OP / S7-Basic session types** — S7netplus uses OP session; the
simulator accepts whatever session type is requested, unlike real CPUs
that allocate session slots differently.
- **SCL variant-specific behaviour** — e.g. S7-1200 missing certain PDU
types, S7-300's older handshake, S7-400 multi-CPU racks with non-zero
slot. Simulator collapses all into one generic CPU emulation.
- **PUT/GET-disabled-by-default** — real S7-1200/1500 CPUs refuse reads
when PUT/GET is off in TIA Portal hardware config; the driver maps that
to `BadDeviceFailure`. Simulator has no such toggle + always accepts.
## References
- [python-snap7 GitHub](https://github.com/gijzelaerr/python-snap7) — source + install
- [snap7.server API](https://python-snap7.readthedocs.io/en/latest/API/server.html) — `Server` class reference
- [`docs/drivers/S7-Test-Fixture.md`](../../../docs/drivers/S7-Test-Fixture.md) — coverage map + gap inventory
- [`docs/v2/s7.md`](../../../docs/v2/s7.md) — driver-side addressing + family notes

View File

@@ -0,0 +1,35 @@
{
"_description": "S7-1500 profile — single DB1 (1024 bytes) + MB (256 bytes) with well-known seeds at named offsets for the smoke + byte-order + string tests. Big-endian Siemens wire order throughout.",
"areas": [
{
"area": "DB",
"index": 1,
"size": 1024,
"seeds": [
{ "_desc": "DB1.DBW0 — read-back probe, S7Driver default ProbeAddress target is MW0; this shadows it",
"offset": 0, "type": "u16", "value": 4242 },
{ "_desc": "DB1.DBW10 — i16 smoke value for SmokeI16 read path",
"offset": 10, "type": "i16", "value": -12345 },
{ "_desc": "DB1.DBD20 — i32 smoke value for SmokeI32 read path",
"offset": 20, "type": "i32", "value": 1234567890 },
{ "_desc": "DB1.DBD30 — f32 smoke value for SmokeF32 read path (IEEE-754 big-endian)",
"offset": 30, "type": "f32", "value": 3.14159 },
{ "_desc": "DB1.DBX50.3 — bool bit at byte-50 bit-3 for SmokeBool read path",
"offset": 50, "type": "bool", "value": true, "bit": 3 },
{ "_desc": "DB1.DBW100 — scratch for write-then-read round-trip tests; seeded 0",
"offset": 100, "type": "u16", "value": 0 },
{ "_desc": "DB1.STRING[200] — S7 string 'Hello' (max 32, cur 5)",
"offset": 200, "type": "ascii", "value": "Hello", "max_len": 32 }
]
},
{
"area": "MK",
"index": 0,
"size": 256,
"seeds": [
{ "_desc": "MW0 — probe target for S7ProbeOptions.ProbeAddress default",
"offset": 0, "type": "u16", "value": 1 }
]
}
]
}

View File

@@ -0,0 +1,56 @@
<#
.SYNOPSIS
Launches the python-snap7 S7 server with one of the integration-test
profiles. Foreground process — Ctrl+C to stop. Mirrors the pymodbus
`serve.ps1` wrapper in tests\...\Modbus.IntegrationTests\Pymodbus\.
.PARAMETER Profile
Which profile JSON to load: currently only 's7_1500' ships. Additional
families (S7-1200, S7-300) can drop in as new JSON files alongside.
.PARAMETER Port
TCP port to bind. Default 1102 (non-privileged; matches
Snap7ServerFixture default endpoint). Pass 102 to match S7 standard —
requires root on Linux + triggers Windows Firewall prompt.
.EXAMPLE
.\serve.ps1 -Profile s7_1500
.EXAMPLE
.\serve.ps1 -Profile s7_1500 -Port 102
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)] [ValidateSet('s7_1500')] [string]$Profile,
[int]$Port = 1102
)
$ErrorActionPreference = 'Stop'
$here = $PSScriptRoot
# python-snap7 installs the `snap7` Python package; we call via `python -m`
# or via the server.py shim in this folder. Shim path is simpler to diagnose.
$python = Get-Command python -ErrorAction SilentlyContinue
if (-not $python) { $python = Get-Command py -ErrorAction SilentlyContinue }
if (-not $python) {
Write-Error "python not found on PATH. Install Python 3.10+ and 'pip install python-snap7'."
exit 1
}
# Verify python-snap7 is installed so failures surface here, not in a
# confusing ImportError from server.py.
& $python.Source -c "import snap7.server" 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Error "python-snap7 not importable. Install with: pip install 'python-snap7>=2.0'"
exit 1
}
$jsonFile = Join-Path $here "$Profile.json"
if (-not (Test-Path $jsonFile)) {
Write-Error "Profile config not found: $jsonFile"
exit 1
}
Write-Host "Starting python-snap7 server: profile=$Profile TCP=localhost:$Port"
Write-Host "Ctrl+C to stop."
& $python.Source (Join-Path $here "server.py") $jsonFile --port $Port

View File

@@ -0,0 +1,150 @@
"""python-snap7 S7 server for integration tests.
Reads a JSON profile from argv[1], allocates bytearrays for each declared area
(DB / MB / EB / AB), poke-seeds values at declared offsets, then starts the
snap7 Server on the configured port + blocks until Ctrl+C. Shape intentionally
mirrors the pymodbus `serve.ps1 + *.json` pattern one directory over so
someone familiar with the Modbus fixture can read this without re-learning.
The snap7.server.Server class is the MIT-licensed S7 PLC emulator wrapped by
python-snap7 (https://github.com/gijzelaerr/python-snap7). Its own docstring
admits "protocol compliance testing rather than full industrial-grade
functionality" — good enough for ISO-on-TCP wire-level round-trip but NOT
for S7-1500 Optimized-DB symbolic access, SCL variant-specific behaviour, or
PG/OP/S7-Basic session differentiation.
"""
from __future__ import annotations
import argparse
import ctypes
import json
import signal
import sys
import time
from pathlib import Path
# python-snap7 installs as `snap7` package; Server class lives under `snap7.server`.
import snap7
from snap7.type import SrvArea
# Map JSON area names → SrvArea enum values. PE = inputs (I/E), PA = outputs
# (Q/A), MK = memory (M), DB = data blocks, TM = timers, CT = counters.
AREA_MAP: dict[str, int] = {
"PE": SrvArea.PE,
"PA": SrvArea.PA,
"MK": SrvArea.MK,
"DB": SrvArea.DB,
"TM": SrvArea.TM,
"CT": SrvArea.CT,
}
def seed_buffer(buf: bytearray, seeds: list[dict]) -> None:
"""Poke seed values into the area buffer at declared byte offsets.
Each seed is {"offset": int, "type": str, "value": int|float|bool|str}
where type ∈ {u8, i8, u16, i16, u32, i32, f32, bool, ascii}. Endianness is
big-endian (Siemens wire format).
"""
for seed in seeds:
off = int(seed["offset"])
t = seed["type"]
v = seed["value"]
if t == "u8":
buf[off] = int(v) & 0xFF
elif t == "i8":
buf[off] = int(v) & 0xFF
elif t == "u16":
buf[off:off + 2] = int(v).to_bytes(2, "big", signed=False)
elif t == "i16":
buf[off:off + 2] = int(v).to_bytes(2, "big", signed=True)
elif t == "u32":
buf[off:off + 4] = int(v).to_bytes(4, "big", signed=False)
elif t == "i32":
buf[off:off + 4] = int(v).to_bytes(4, "big", signed=True)
elif t == "f32":
import struct
buf[off:off + 4] = struct.pack(">f", float(v))
elif t == "bool":
bit = int(seed.get("bit", 0))
if bool(v):
buf[off] |= (1 << bit)
else:
buf[off] &= ~(1 << bit) & 0xFF
elif t == "ascii":
# Siemens STRING type: byte 0 = max length, byte 1 = current length,
# bytes 2+ = payload. Seeds supply the payload text; we fill max/cur.
payload = str(v).encode("ascii")
max_len = int(seed.get("max_len", 254))
buf[off] = max_len
buf[off + 1] = len(payload)
buf[off + 2:off + 2 + len(payload)] = payload
else:
raise ValueError(f"Unknown seed type '{t}'")
def main() -> int:
parser = argparse.ArgumentParser(description="python-snap7 S7 server for integration tests")
parser.add_argument("profile", help="Path to profile JSON")
parser.add_argument("--port", type=int, default=1102, help="TCP port (default 1102 non-privileged)")
args = parser.parse_args()
profile_path = Path(args.profile)
if not profile_path.is_file():
print(f"profile not found: {profile_path}", file=sys.stderr)
return 1
with profile_path.open() as f:
profile = json.load(f)
server = snap7.server.Server()
# Keep bytearray refs alive for the server's lifetime — snap7 doesn't copy
# the buffer, it takes a pointer. Letting GC collect would corrupt reads.
buffers: list[bytearray] = []
for area_decl in profile.get("areas", []):
area_name = area_decl["area"]
if area_name not in AREA_MAP:
print(f"unknown area '{area_name}' (expected one of {list(AREA_MAP)})", file=sys.stderr)
return 1
index = int(area_decl.get("index", 0)) # DB number for DB area, 0 for MK/PE/PA
size = int(area_decl["size"])
buf = bytearray(size)
seed_buffer(buf, area_decl.get("seeds", []))
buffers.append(buf)
# register_area takes (area, index, c-array); we wrap the bytearray
# into a ctypes char array so the native lib can take &buf[0].
arr_type = ctypes.c_char * size
arr = arr_type.from_buffer(buf)
server.register_area(AREA_MAP[area_name], index, arr)
print(f" seeded {area_name}{index} size={size} seeds={len(area_decl.get('seeds', []))}")
port = int(args.port)
print(f"Starting python-snap7 server on TCP {port} (Ctrl+C to stop)")
server.start(tcp_port=port)
stop = {"sig": False}
def _handle(*_a):
stop["sig"] = True
signal.signal(signal.SIGINT, _handle)
try:
signal.signal(signal.SIGTERM, _handle)
except Exception:
pass # SIGTERM not on all platforms
try:
while not stop["sig"]:
time.sleep(0.25)
finally:
print("stopping python-snap7 server")
try:
server.stop()
except Exception:
pass
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -0,0 +1,53 @@
using S7NetCpuType = global::S7.Net.CpuType;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests.S7_1500;
/// <summary>
/// Driver-side configuration matching what <c>PythonSnap7/s7_1500.json</c> seeds
/// into the simulator's DB1 + MB areas. Tag names here become the full references
/// the smoke tests read/write against; addresses map 1:1 to the JSON profile's
/// seed offsets so a seed drift in the JSON surfaces as a driver-side read
/// mismatch, not a mystery test failure.
/// </summary>
public static class S7_1500Profile
{
public const string ProbeTag = "ProbeProbeWord";
public const int ProbeSeedValue = 4242;
public const string SmokeI16Tag = "SmokeI16";
public const short SmokeI16SeedValue = -12345;
public const string SmokeI32Tag = "SmokeI32";
public const int SmokeI32SeedValue = 1234567890;
public const string SmokeF32Tag = "SmokeF32";
public const float SmokeF32SeedValue = 3.14159f;
public const string SmokeBoolTag = "SmokeBool";
public const string WriteScratchTag = "WriteScratch";
public static S7DriverOptions BuildOptions(string host, int port) => new()
{
Host = host,
Port = port,
CpuType = S7NetCpuType.S71500,
Rack = 0,
Slot = 0,
Timeout = TimeSpan.FromSeconds(5),
// Disable the probe loop — the integration tests run their own reads +
// a background probe would race with them for the S7netplus mailbox
// gate, injecting flakiness that has nothing to do with the code
// under test.
Probe = new S7ProbeOptions { Enabled = false },
Tags =
[
new S7TagDefinition(ProbeTag, "DB1.DBW0", S7DataType.UInt16),
new S7TagDefinition(SmokeI16Tag, "DB1.DBW10", S7DataType.Int16),
new S7TagDefinition(SmokeI32Tag, "DB1.DBD20", S7DataType.Int32),
new S7TagDefinition(SmokeF32Tag, "DB1.DBD30", S7DataType.Float32),
new S7TagDefinition(SmokeBoolTag, "DB1.DBX50.3", S7DataType.Bool),
new S7TagDefinition(WriteScratchTag, "DB1.DBW100", S7DataType.UInt16),
],
};
}

View File

@@ -0,0 +1,83 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests.S7_1500;
/// <summary>
/// End-to-end smoke against the python-snap7 S7-1500 profile. Drives the real
/// <see cref="S7Driver"/> + real S7netplus ISO-on-TCP stack + real CIP-free
/// S7comm exchange against <c>localhost:1102</c>. Success proves initialisation,
/// typed reads (u16 / i16 / i32 / f32 / bool-with-bit), and a write-then-read
/// round-trip all work against a real S7 server — the baseline everything
/// S7-specific (byte-order, optimized-DB differences, probe behaviour) layers on.
/// </summary>
[Collection(Snap7ServerCollection.Name)]
[Trait("Category", "Integration")]
[Trait("Device", "S7_1500")]
public sealed class S7_1500SmokeTests(Snap7ServerFixture sim)
{
[Fact]
public async Task Driver_reads_seeded_u16_through_real_S7comm()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-u16");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var snapshots = await drv.ReadAsync(
[S7_1500Profile.ProbeTag], TestContext.Current.CancellationToken);
snapshots.Count.ShouldBe(1);
snapshots[0].StatusCode.ShouldBe(0u, "seeded u16 read must succeed end-to-end");
Convert.ToInt32(snapshots[0].Value).ShouldBe(S7_1500Profile.ProbeSeedValue);
}
[Fact]
public async Task Driver_reads_seeded_typed_batch()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-batch");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var snapshots = await drv.ReadAsync(
[S7_1500Profile.SmokeI16Tag, S7_1500Profile.SmokeI32Tag,
S7_1500Profile.SmokeF32Tag, S7_1500Profile.SmokeBoolTag],
TestContext.Current.CancellationToken);
snapshots.Count.ShouldBe(4);
foreach (var s in snapshots) s.StatusCode.ShouldBe(0u);
Convert.ToInt32(snapshots[0].Value).ShouldBe((int)S7_1500Profile.SmokeI16SeedValue);
Convert.ToInt32(snapshots[1].Value).ShouldBe(S7_1500Profile.SmokeI32SeedValue);
Convert.ToSingle(snapshots[2].Value).ShouldBe(S7_1500Profile.SmokeF32SeedValue, tolerance: 0.0001f);
Convert.ToBoolean(snapshots[3].Value).ShouldBeTrue();
}
[Fact]
public async Task Driver_write_then_read_round_trip_on_scratch_word()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-write");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
const ushort probe = 0xBEEF;
var writeResults = await drv.WriteAsync(
[new WriteRequest(S7_1500Profile.WriteScratchTag, probe)],
TestContext.Current.CancellationToken);
writeResults.Count.ShouldBe(1);
writeResults[0].StatusCode.ShouldBe(0u,
"write must succeed against snap7's DB1.DBW100 scratch register");
var readResults = await drv.ReadAsync(
[S7_1500Profile.WriteScratchTag], TestContext.Current.CancellationToken);
readResults.Count.ShouldBe(1);
readResults[0].StatusCode.ShouldBe(0u);
Convert.ToInt32(readResults[0].Value).ShouldBe(probe);
}
}

View File

@@ -0,0 +1,83 @@
using System.Net.Sockets;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests;
/// <summary>
/// Reachability probe for a python-snap7 simulator (see
/// <c>PythonSnap7/serve.ps1</c>) or a real S7 PLC. Parses <c>S7_SIM_ENDPOINT</c>
/// (default <c>localhost:1102</c>) + TCP-connects once at fixture construction.
/// Tests check <see cref="SkipReason"/> + call <c>Assert.Skip</c> when unreachable, so
/// `dotnet test` stays green on a fresh box without the simulator installed —
/// mirrors the <c>ModbusSimulatorFixture</c> pattern.
/// </summary>
/// <remarks>
/// <para>
/// Default port is <b>1102</b>, not the S7-standard 102. 102 is a privileged port
/// on Linux (needs root) + triggers the Windows Firewall prompt on first bind;
/// 1102 sidesteps both. S7netplus 0.20 supports the 5-arg <c>Plc</c> ctor that
/// takes an explicit port (verified + wired through <c>S7DriverOptions.Port</c>),
/// so the driver can reach the simulator on its non-standard port without
/// hacks.
/// </para>
/// <para>
/// The probe is a one-shot liveness check; tests open their own S7netplus
/// sessions against the same endpoint. Don't share a socket — S7 CPUs serialise
/// concurrent connections against the same mailbox anyway, but sharing would
/// couple test ordering to socket reuse in ways this harness shouldn't care
/// about.
/// </para>
/// <para>
/// Fixture is a collection fixture so the probe runs once per test session, not
/// per test.
/// </para>
/// </remarks>
public sealed class Snap7ServerFixture : IAsyncDisposable
{
// Default 1102 (non-privileged) matches PythonSnap7/server.py. Override with
// S7_SIM_ENDPOINT to point at a real PLC on its native 102.
private const string DefaultEndpoint = "localhost:1102";
private const string EndpointEnvVar = "S7_SIM_ENDPOINT";
public string Host { get; }
public int Port { get; }
public string? SkipReason { get; }
public Snap7ServerFixture()
{
var raw = Environment.GetEnvironmentVariable(EndpointEnvVar) ?? DefaultEndpoint;
var parts = raw.Split(':', 2);
Host = parts[0];
Port = parts.Length == 2 && int.TryParse(parts[1], out var p) ? p : 102;
try
{
// Force IPv4 — python-snap7 binds 0.0.0.0 (IPv4) and .NET's default
// dual-stack "localhost" resolves IPv6 ::1 first then times out before
// falling back. Same story the Modbus fixture hits.
using var client = new TcpClient(AddressFamily.InterNetwork);
var task = client.ConnectAsync(
System.Net.Dns.GetHostAddresses(Host)
.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork)
?? System.Net.IPAddress.Loopback,
Port);
if (!task.Wait(TimeSpan.FromSeconds(2)) || !client.Connected)
{
SkipReason = $"python-snap7 simulator at {Host}:{Port} did not accept a TCP connection within 2s. " +
$"Start it (PythonSnap7\\serve.ps1 -Profile s7_1500) or override {EndpointEnvVar}.";
}
}
catch (Exception ex)
{
SkipReason = $"python-snap7 simulator at {Host}:{Port} unreachable: {ex.GetType().Name}: {ex.Message}. " +
$"Start it (PythonSnap7\\serve.ps1 -Profile s7_1500) or override {EndpointEnvVar}.";
}
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
[Xunit.CollectionDefinition(Name)]
public sealed class Snap7ServerCollection : Xunit.ICollectionFixture<Snap7ServerFixture>
{
public const string Name = "Snap7Server";
}

View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit.v3" Version="1.1.0"/>
<PackageReference Include="Shouldly" Version="4.3.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ZB.MOM.WW.OtOpcUa.Driver.S7\ZB.MOM.WW.OtOpcUa.Driver.S7.csproj"/>
</ItemGroup>
<ItemGroup>
<None Update="PythonSnap7\**\*" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
<ItemGroup>
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-37gx-xxp4-5rgx"/>
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-w3x6-4m5h-cxqf"/>
</ItemGroup>
</Project>