7892eb13a8
CIP codec (#564): PatchU16 bounds guard; CipReply content equality; CipPath port/length/ASCII/odd/oversize validation with ArgumentException for malformed tag names and routes; CipPath.MessageRouter/ConnectionManager are now ReadOnlySpan<byte>; refused Forward Open surfaces ForwardOpenRefusedException with structured general/extended status; test CaptureFile uses BinaryPrimitives. EncapsulationClient (#565): frame write bounded by the exchange timeout, which faults (never cancels) the connection; finite positive timeouts enforced at every entry point; gate wait bounded by the timeout; deterministic reorder of the 300 ms test; new dispose-during-drain, gate-wait and RegisterSession zero-context tests. Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
106 lines
3.9 KiB
C#
106 lines
3.9 KiB
C#
using System.Buffers.Binary;
|
|
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests.Captures;
|
|
|
|
public sealed class CaptureFileTests
|
|
{
|
|
private static readonly string[] Required =
|
|
[
|
|
"logix-register-session.hex", "logix-forward-open.hex",
|
|
"logix-read-dint.hex", "logix-read-dint-array16.hex", "logix-read-string.hex", "logix-read-bool.hex",
|
|
"logix-write-dint.hex", "logix-write-string.hex", "logix-read-unknown-tag.hex",
|
|
"logix-tags-listing.hex", "logix-forward-close.hex", "micro800-read-dint.hex",
|
|
"slc500-read-n7.hex", "slc500-write-n7.hex", "slc500-read-f8.hex",
|
|
"micrologix-read-n7.hex", "plc5-read-n7.hex",
|
|
];
|
|
|
|
public static TheoryData<string> AllCaptures()
|
|
{
|
|
var data = new TheoryData<string>();
|
|
foreach (var f in Directory.GetFiles(CaptureFile.Dir, "*.hex").Order()) data.Add(Path.GetFileName(f));
|
|
return data;
|
|
}
|
|
|
|
[Fact]
|
|
public void Every_required_capture_is_present() =>
|
|
Required.Where(n => !File.Exists(Path.Combine(CaptureFile.Dir, n))).ShouldBeEmpty();
|
|
|
|
[Theory]
|
|
[MemberData(nameof(AllCaptures))]
|
|
public void Every_frame_length_field_matches_its_data_length(string name)
|
|
{
|
|
var frames = CaptureFile.Load(name);
|
|
|
|
frames.ShouldNotBeEmpty();
|
|
foreach (var f in frames)
|
|
{
|
|
f.Bytes.Length.ShouldBeGreaterThanOrEqualTo(24, name);
|
|
BinaryPrimitives.ReadUInt16LittleEndian(f.Bytes.AsSpan(2)).ShouldBe((ushort)(f.Bytes.Length - 24), name);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(AllCaptures))]
|
|
public void Frames_alternate_request_then_reply(string name)
|
|
{
|
|
var frames = CaptureFile.Load(name);
|
|
|
|
frames.Count.ShouldBe(frames.Count / 2 * 2, name);
|
|
for (var i = 0; i < frames.Count; i++) frames[i].FromClient.ShouldBe(i % 2 == 0, $"{name} frame {i}");
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(AllCaptures))]
|
|
public void Every_CPF_item_walk_ends_exactly_at_the_frame_end(string name)
|
|
{
|
|
foreach (var f in CaptureFile.Load(name).Where(f => BinaryPrimitives.ReadUInt16LittleEndian(f.Bytes.AsSpan(0)) is 0x6F or 0x70))
|
|
{
|
|
var pos = 24 + 4 + 2;
|
|
var itemCount = BinaryPrimitives.ReadUInt16LittleEndian(f.Bytes.AsSpan(pos));
|
|
pos += 2;
|
|
for (var i = 0; i < itemCount; i++) pos += 4 + BinaryPrimitives.ReadUInt16LittleEndian(f.Bytes.AsSpan(pos + 2));
|
|
|
|
pos.ShouldBe(f.Bytes.Length, name);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(AllCaptures))]
|
|
public void Every_reply_service_is_its_request_service_with_the_reply_bit(string name)
|
|
{
|
|
var frames = CaptureFile.Load(name);
|
|
for (var i = 0; i + 1 < frames.Count; i += 2)
|
|
{
|
|
var request = CaptureFile.CipPayload(frames[i]);
|
|
var reply = CaptureFile.CipPayload(frames[i + 1]);
|
|
if (request.Length == 0) continue; // RegisterSession: no CIP
|
|
|
|
reply[0].ShouldBe((byte)(request[0] | 0x80), $"{name} pair {i / 2}");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void CipPayload_strips_the_connected_sequence_count()
|
|
{
|
|
// Connected Read Tag Fragmented of TestDINT: service 0x52, path size 5 words, symbol 0x91 len 8.
|
|
var request = CaptureFile.Load("logix-read-dint.hex")[0];
|
|
|
|
CaptureFile.CipPayload(request)[..4].ShouldBe(new byte[] { 0x52, 0x05, 0x91, 0x08 });
|
|
}
|
|
|
|
[Fact]
|
|
public void CipPayload_returns_the_unconnected_item_for_SendRRData()
|
|
{
|
|
// Large Forward Open request goes out in an unconnected data item (0xB2): service 0x5B.
|
|
var request = CaptureFile.Load("logix-forward-open.hex")[0];
|
|
|
|
CaptureFile.CipPayload(request)[0].ShouldBe((byte)0x5B);
|
|
}
|
|
|
|
[Fact]
|
|
public void CipPayload_is_empty_for_RegisterSession() =>
|
|
CaptureFile.CipPayload(CaptureFile.Load("logix-register-session.hex")[0]).ShouldBeEmpty();
|
|
}
|