Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbCip.IntegrationTests/HardwareGate/IdleConnectionEvidenceTests.cs
T
Joseph Doherty 1f983d610f test(abcip): hw5 hardware gate — #566 idle close drops TCP; native STRING write on Z-35162 (#592)
A. #566 on a real CompactLogix (Z-35162, read-only): a class-3 connection idled 60 s past its
16 s timeout is closed at the TCP level, not with CIP 0x0107/0x0203; the next request fails on
its write (Broken pipe -> BadConnection) in 9 ms and the one after reopens. The transport already
handles this, so no production behaviour changed; the shape is pinned offline by
CipConnectionTests.A_target_that_resets_an_idle_session_... (FakeEipServer.ResetAllConnections),
proven red with a reconnect-skipping mutation. The diagnostics-only exchange tap now also sees
Forward Open replies (granted RPIs).

B. Native STRING write through the driver on Program:ES03_TemperatureControl._StrTmp: write,
read back, restored byte-exact (user-approved). Array write NOT run: the census's unreferenced
DINT[32] _SEQ_EP arrays are aliased sequencer step registers (raw L5X grep), so none qualifies.
#567 still impossible (a 128 B array / 88 B STRING never fragments).

New env-gated harness: AbCip.IntegrationTests/HardwareGate (IdleConnectionEvidenceTests,
StringWriteGateTests). Docs: gate results (hw5 section + raw output), AbCip.md, CLAUDE.md.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
2026-10-09 06:16:16 -04:00

137 lines
7.5 KiB
C#

using System.Buffers.Binary;
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport;
using ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Cip;
using ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Logix;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip.IntegrationTests.Differential;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.IntegrationTests.HardwareGate;
/// <summary>
/// Gitea #566 hardware evidence (hw5): what a real Logix does with a class-3 connection that has been left idle
/// past its connection timeout. READ-ONLY: three Read Tag requests of one tag on one private pool, nothing else.
/// <list type="bullet">
/// <item>Opt-in: <c>AB_DIFF_ENDPOINT</c> (the target, see <see cref="DifferentialTarget"/>) AND
/// <c>AB_EVID_IDLE=1</c>.</item>
/// <item><c>AB_EVID_IDLE_TAG</c> — the tag read, default <c>test2</c> (a DINT).</item>
/// <item><c>AB_EVID_IDLE_SECONDS</c> — idle time, default 60, in [1, 300]. It must exceed the expiry the
/// controller grants (logged from the Forward Open reply), or the read just succeeds.</item>
/// </list>
/// The keep-alive is pushed out to one hour on this private pool so nothing touches the connection while it is
/// idle (in production it fires every half expiry, which is why this case only arises after a controller
/// restart / re-download or a network gap). The open-attempt budget is 2: the first open and one reconnect.
/// </summary>
[Trait("Category", "LiveIntegration")]
public sealed class IdleConnectionEvidenceTests
{
private const string DefaultTag = "test2";
[Fact]
public async Task An_idle_connection_past_its_timeout_reports_what_the_controller_does_and_recovers()
{
var target = DifferentialTarget.FromEnvironment();
if (target is null) Assert.Skip(DifferentialTarget.SkipReason);
if (Environment.GetEnvironmentVariable("AB_EVID_IDLE") != "1") Assert.Skip("AB_EVID_IDLE=1 not set.");
var tag = Environment.GetEnvironmentVariable("AB_EVID_IDLE_TAG") is { Length: > 0 } t ? t.Trim() : DefaultTag;
var idleSeconds = 60;
if (Environment.GetEnvironmentVariable("AB_EVID_IDLE_SECONDS") is { Length: > 0 } rawIdle
&& (!int.TryParse(rawIdle, NumberStyles.None, CultureInfo.InvariantCulture, out idleSeconds) || idleSeconds is < 1 or > 300))
throw new InvalidOperationException($"AB_EVID_IDLE_SECONDS='{rawIdle}' must be an integer in [1, 300].");
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(idleSeconds + 120));
var ct = cts.Token;
var pool = new DeviceSessionPool();
var diagnostics = pool.Diagnostics;
var p = target.Params(tag);
var key = NativeAbCipDevice.Key(p);
var options = NativeAbCipDevice.Options(p) with { KeepAliveInterval = TimeSpan.FromHours(1) };
var clock = Stopwatch.StartNew();
var log = new List<string>();
void Note(string line)
{
var stamped = $"[{clock.Elapsed.TotalSeconds,7:F2} s] {line}";
log.Add(stamped);
TestContext.Current.TestOutputHelper?.WriteLine(stamped);
}
diagnostics.ExchangeObserver = (req, rep) => Note(DescribeExchange(req.Span, rep.Span));
using var budget = diagnostics.LimitOpenAttempts(key, 2);
var request = LogixServices.ReadTag(CipPath.EncodeSymbolic(tag), 1);
Note($"Target {target}; tag '{tag}'; requested RPI {options.RpiMicros} us both ways, timeout multiplier " +
$"{options.TimeoutMultiplier} (x{4 << options.TimeoutMultiplier}) => expiry {options.ConnectionExpiry.TotalSeconds:F1} s; " +
$"keep-alive pushed to {options.KeepAliveInterval}; idle {idleSeconds} s");
var lease = pool.Acquire(key, options);
try
{
var first = await ReadAsync("read 1 (opens)", lease.Channel, request, Note, ct);
first.ShouldBeTrue("the first read must succeed, or there is no connection to idle");
Note($"Forward Opens {diagnostics.ForwardOpens}; now idle for {idleSeconds} s with no traffic");
await Task.Delay(TimeSpan.FromSeconds(idleSeconds), ct);
var opensBefore = diagnostics.ForwardOpens;
var second = await ReadAsync("read 2 (after idle)", lease.Channel, request, Note, ct);
Note($"After read 2: Forward Opens {diagnostics.ForwardOpens} (delta {diagnostics.ForwardOpens - opensBefore}), " +
$"closed-by-target {diagnostics.ConnectionsClosedByTarget}, success={second}");
await Task.Delay(TimeSpan.FromSeconds(2), ct); // past the 1 s Forward Open interval
var third = await ReadAsync("read 3 (recovery)", lease.Channel, request, Note, ct);
Note($"After read 3: Forward Opens {diagnostics.ForwardOpens}, closed-by-target " +
$"{diagnostics.ConnectionsClosedByTarget}, open attempts used {budget.Attempts}/{budget.Budget}, success={third}");
third.ShouldBeTrue("the transport must recover on the request after the failed one");
}
finally
{
await lease.DisposeAsync();
diagnostics.ExchangeObserver = null;
Note($"Lease released: Forward Closes sent/acked {diagnostics.ForwardClosesSent}/{diagnostics.ForwardClosesAcked}");
}
}
private static async Task<bool> ReadAsync(string label, ICipChannel channel, byte[] request, Action<string> note,
CancellationToken ct)
{
var sw = Stopwatch.StartNew();
try
{
var reply = CipReply.Parse(await channel.SendAsync(request, ct));
note($"{label}: reply in {sw.ElapsedMilliseconds} ms, general 0x{reply.GeneralStatus:X2}" +
$"{(reply.ExtendedStatus.Length > 0 ? $" extended {string.Join(",", reply.ExtendedStatus.Select(e => $"0x{e:X4}"))}" : "")}");
return reply.GeneralStatus == 0;
}
catch (AbTransportException ex)
{
note($"{label}: THREW after {sw.ElapsedMilliseconds} ms: AbTransportException status={ex.Status} " +
$"(OPC UA 0x{AbOpcUaStatusMap.MapStatus(ex.Status):X8}) message='{ex.Message}' inner={Inner(ex)}");
return false;
}
}
private static string Inner(Exception ex)
{
var parts = new List<string>();
for (var e = ex.InnerException; e is not null; e = e.InnerException)
parts.Add(e is SocketException se ? $"SocketException({se.SocketErrorCode})" : $"{e.GetType().Name}('{e.Message}')");
return parts.Count == 0 ? "none" : string.Join(" <- ", parts);
}
private static string DescribeExchange(ReadOnlySpan<byte> request, ReadOnlySpan<byte> reply)
{
var service = request.Length > 0 ? request[0] : (byte)0;
var hex = Convert.ToHexString(reply[..Math.Min(reply.Length, 48)]);
if (service is 0x54 or 0x5B && reply.Length >= 4 && reply[2] == 0)
{
// Forward Open reply data: O->T id, T->O id, serial, vendor, originator serial, O->T API, T->O API.
var d = reply[(4 + 2 * reply[3])..];
if (d.Length >= 24)
return $"Forward Open 0x{service:X2} granted: O->T id 0x{BinaryPrimitives.ReadUInt32LittleEndian(d):X8}, " +
$"O->T API {BinaryPrimitives.ReadUInt32LittleEndian(d[16..])} us, T->O API {BinaryPrimitives.ReadUInt32LittleEndian(d[20..])} us; raw {hex}";
}
return $"exchange: request service 0x{service:X2} ({request.Length} B) -> reply {reply.Length} B: {hex}";
}
}