d50ee809e3
Native LSV2 TCP transport (default port 19000; no Java/Python/vendor SDK), ported from the ignitionoee v0.20.3 reference driver: - Lsv2ReadOnlyPolicy: payload-aware outbound allowlist (A_LG INSPECT/DNC/DATA, R_VR, R_PR, R_RI 22-28/30-34/51, bounded R_MB, R_DP), applied in Lsv2Codec.Frame before any byte is written; no public raw-command API. - Lsv2Client: serialized transactions, one monotonic deadline per transaction, EOF/oversize/ unexpected/trickle close the client; Close never waits on the gate. - Lsv2Data / HeidenhainBindingCodec: reference decoders, R_PR byte-capacity + element-index rules, next-area cap, DATA Boolean 00/01/FF, strict non-truncating coercion. - HeidenhainSession: per-batch wire cache, refusal suppression, provisional DNC denial retry, alarm cursor (57 = end, overflow = BadOutOfRange), capability strings. - HeidenhainDeviceRuntime: one loop per control, generation-guarded publication, all-node BadNoCommunication on comms loss, bounded exponential reconnect. - HeidenhainDriver: cached ReadAsync, push subscriptions deduped on (value,status), static catalog discovery (no connect), host status, diagnostics, skipped-tag report. No IWritable. - Probe (INSPECT login + version) and factory registered in DriverFactoryBootstrap (probe on admin nodes too); DriverTypeNames.Heidenhain. Tests: 159 unit tests against an in-process LSV2 simulator incl. the Java fixtures' captured iTNC 530 replies as golden vectors; env-gated LiveIntegration category for real hardware.
147 lines
6.0 KiB
C#
147 lines
6.0 KiB
C#
using System.Diagnostics;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Lsv2;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests.Simulator;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests;
|
|
|
|
/// <summary>Framing/transport regression ported from the reference <c>Lsv2TransportTest</c> (literal frames, independent of the encoder).</summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class Lsv2TransportTests
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
private static async Task<Lsv2Client> OpenAsync(Lsv2Simulator sim, int timeoutMs = 1000)
|
|
{
|
|
var c = new Lsv2Client("127.0.0.1", sim.Port, TimeSpan.FromMilliseconds(timeoutMs));
|
|
await c.OpenAsync(Ct);
|
|
return c;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Literal_runtime_request_and_byte_fragmented_response()
|
|
{
|
|
byte[]? seen = null;
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
var req = await c.ReadRequestAsync(ct);
|
|
seen = SimConnection.Frame(req!.Command, req.Payload);
|
|
await c.WriteRawAsync(Convert.FromHexString("00000002535f52490003"), byteByByte: true, ct);
|
|
});
|
|
await using var client = await OpenAsync(sim);
|
|
(await client.ReadRuntimeAsync(23, Ct)).ShouldBe(new byte[] { 0, 3 });
|
|
seen.ShouldBe(Convert.FromHexString("00000002525f52490017"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Controller_refusal_preserves_code_and_stream_sync()
|
|
{
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
await c.ReadRequestAsync(ct);
|
|
await c.ReplyAsync("T_ER", [1, 23], ct);
|
|
await c.ReadRequestAsync(ct);
|
|
await c.ReplyAsync("S_VR", [65, 0], ct);
|
|
});
|
|
await using var client = await OpenAsync(sim);
|
|
var refusal = await Should.ThrowAsync<Lsv2RefusalException>(() => client.ReadRuntimeAsync(23, Ct));
|
|
refusal.Code.ShouldBe(23);
|
|
client.IsClosed.ShouldBeFalse();
|
|
(await client.ReadVersionAsync(Ct)).ShouldBe(new byte[] { 65, 0 });
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("00010000535f5249")] // oversized length: rejected before allocation
|
|
[InlineData("00000000535f5652")] // unexpected response type (S_VR for an R_RI)
|
|
[InlineData("00000002535f524900")] // partial body then EOF
|
|
[InlineData("00000003545f455201")] // malformed refusal (3-byte T_ER)
|
|
[InlineData("00000001545f4f4b00")] // T_OK with a body
|
|
public async Task Bad_frames_close_the_client_and_later_requests_fail(string hex)
|
|
{
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
await c.ReadRequestAsync(ct);
|
|
await c.WriteRawAsync(Convert.FromHexString(hex), byteByByte: false, ct);
|
|
if (hex.EndsWith("00", StringComparison.Ordinal) && hex.Length == 18) c.Close();
|
|
await Task.Delay(2000, ct);
|
|
});
|
|
await using var client = await OpenAsync(sim);
|
|
var req = hex.Contains("545f4f4b", StringComparison.Ordinal)
|
|
? client.LoginAsync("INSPECT", "", Ct)
|
|
: client.ReadRuntimeAsync(23, Ct);
|
|
await Should.ThrowAsync<Lsv2TransportException>(() => req);
|
|
client.IsClosed.ShouldBeTrue();
|
|
await Should.ThrowAsync<Lsv2TransportException>(() => client.ReadVersionAsync(Ct));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Close_interrupts_a_blocked_read_without_waiting_for_the_gate()
|
|
{
|
|
var requested = new TaskCompletionSource();
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
await c.ReadRequestAsync(ct);
|
|
requested.TrySetResult();
|
|
await c.ReadByteAsync(ct); // never answers
|
|
});
|
|
await using var client = await OpenAsync(sim, timeoutMs: 10_000);
|
|
var pending = client.ReadRuntimeAsync(23, Ct);
|
|
await requested.Task.WaitAsync(TimeSpan.FromSeconds(2), Ct);
|
|
var sw = Stopwatch.StartNew();
|
|
client.Close();
|
|
await Should.ThrowAsync<Lsv2TransportException>(() => pending.WaitAsync(TimeSpan.FromSeconds(2), Ct));
|
|
sw.ElapsedMilliseconds.ShouldBeLessThan(2000);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Trickle_cannot_reset_the_transaction_deadline()
|
|
{
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
await c.ReadRequestAsync(ct);
|
|
for (var i = 0; i < 20; i++)
|
|
{
|
|
await c.WriteRawAsync([0], byteByByte: false, ct);
|
|
await Task.Delay(40, ct);
|
|
}
|
|
});
|
|
await using var client = await OpenAsync(sim, timeoutMs: 120);
|
|
var sw = Stopwatch.StartNew();
|
|
await Should.ThrowAsync<Lsv2TransportException>(() => client.ReadRuntimeAsync(23, Ct));
|
|
sw.ElapsedMilliseconds.ShouldBeLessThan(1000);
|
|
client.IsClosed.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Transactions_are_serialized_one_frame_at_a_time()
|
|
{
|
|
var concurrent = 0;
|
|
var maxConcurrent = 0;
|
|
await using var sim = Lsv2Simulator.WithScript(async (c, ct) =>
|
|
{
|
|
while (await c.ReadRequestAsync(ct) is not null)
|
|
{
|
|
var now = Interlocked.Increment(ref concurrent);
|
|
maxConcurrent = Math.Max(maxConcurrent, now);
|
|
await Task.Delay(10, ct);
|
|
Interlocked.Decrement(ref concurrent);
|
|
await c.ReplyAsync("S_RI", [0, 1], ct);
|
|
}
|
|
});
|
|
await using var client = await OpenAsync(sim);
|
|
await Task.WhenAll(Enumerable.Range(0, 8).Select(_ => client.ReadRuntimeAsync(23, Ct)));
|
|
maxConcurrent.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Connect_to_a_closed_port_is_a_transport_failure()
|
|
{
|
|
int port;
|
|
await using (var sim = Lsv2Simulator.WithScript((_, _) => Task.CompletedTask)) port = sim.Port;
|
|
var client = new Lsv2Client("127.0.0.1", port, TimeSpan.FromMilliseconds(500));
|
|
await Should.ThrowAsync<Lsv2TransportException>(() => client.OpenAsync(Ct));
|
|
client.IsClosed.ShouldBeTrue();
|
|
}
|
|
}
|