using libplctag; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy; namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests; [Trait("Category", "Unit")] public sealed class AbLegacyHostAndStatusTests { [Theory] [InlineData("ab://10.0.0.5/1,0", "10.0.0.5", 44818, "1,0")] [InlineData("ab://10.0.0.5/", "10.0.0.5", 44818, "")] [InlineData("ab://10.0.0.5:2222/1,0", "10.0.0.5", 2222, "1,0")] [InlineData("ab://plc-slc.factory/1,2", "plc-slc.factory", 44818, "1,2")] public void HostAddress_parses_valid(string input, string gateway, int port, string path) { var parsed = AbLegacyHostAddress.TryParse(input); parsed.ShouldNotBeNull(); parsed.Gateway.ShouldBe(gateway); parsed.Port.ShouldBe(port); parsed.CipPath.ShouldBe(path); } [Theory] [InlineData(null)] [InlineData("http://10.0.0.5/1,0")] [InlineData("ab://10.0.0.5")] [InlineData("ab:///1,0")] [InlineData("ab://10.0.0.5:0/1,0")] public void HostAddress_rejects_invalid(string? input) { AbLegacyHostAddress.TryParse(input).ShouldBeNull(); } [Fact] public void HostAddress_ToString_canonicalises() { new AbLegacyHostAddress("10.0.0.5", 44818, "1,0").ToString().ShouldBe("ab://10.0.0.5/1,0"); new AbLegacyHostAddress("10.0.0.5", 2222, "1,0").ToString().ShouldBe("ab://10.0.0.5:2222/1,0"); } [Theory] [InlineData((byte)0x00, AbLegacyStatusMapper.Good)] [InlineData((byte)0x10, AbLegacyStatusMapper.BadNotSupported)] [InlineData((byte)0x20, AbLegacyStatusMapper.BadNodeIdUnknown)] [InlineData((byte)0x30, AbLegacyStatusMapper.BadNotWritable)] [InlineData((byte)0x40, AbLegacyStatusMapper.BadDeviceFailure)] [InlineData((byte)0x50, AbLegacyStatusMapper.BadDeviceFailure)] [InlineData((byte)0xF0, AbLegacyStatusMapper.BadInternalError)] [InlineData((byte)0xFF, AbLegacyStatusMapper.BadCommunicationError)] public void PcccStatus_maps_known_codes(byte sts, uint expected) { AbLegacyStatusMapper.MapPcccStatus(sts).ShouldBe(expected); } // Driver.AbLegacy-010 — tests use the libplctag.NET Status enum members (what // (int)Tag.GetStatus() actually returns) rather than the unverified magic integers // that predated this fix (-5/-7/-14/-16/-17 matched neither native PLCTAG_ERR_* // constants nor the .NET wrapper enum ordinals reliably). [Theory] [InlineData(Status.Ok, AbLegacyStatusMapper.Good)] [InlineData(Status.Pending, AbLegacyStatusMapper.GoodMoreData)] [InlineData(Status.ErrorTimeout, AbLegacyStatusMapper.BadTimeout)] [InlineData(Status.ErrorNotFound, AbLegacyStatusMapper.BadNodeIdUnknown)] [InlineData(Status.ErrorNoMatch, AbLegacyStatusMapper.BadNodeIdUnknown)] [InlineData(Status.ErrorNotAllowed, AbLegacyStatusMapper.BadNotWritable)] [InlineData(Status.ErrorOutOfBounds, AbLegacyStatusMapper.BadOutOfRange)] [InlineData(Status.ErrorTooLarge, AbLegacyStatusMapper.BadOutOfRange)] [InlineData(Status.ErrorBadConnection, AbLegacyStatusMapper.BadCommunicationError)] [InlineData(Status.ErrorBadGateway, AbLegacyStatusMapper.BadCommunicationError)] [InlineData(Status.ErrorUnsupported, AbLegacyStatusMapper.BadNotSupported)] [InlineData(Status.ErrorNoMem, AbLegacyStatusMapper.BadCommunicationError)] // unmapped → generic comms public void LibplctagStatus_maps_real_enum_members(Status status, uint expected) { AbLegacyStatusMapper.MapLibplctagStatus(status).ShouldBe(expected); // The int overload must agree — it is the seam IAbLegacyTagRuntime.GetStatus() drives. AbLegacyStatusMapper.MapLibplctagStatus((int)status).ShouldBe(expected); } [Fact] public void MapLibplctagStatus_distinguishes_timeout_from_generic_comms_error() { // Regression for Driver.AbLegacy-010: timeout must not fall through to // BadCommunicationError the way the old magic-integer switch did. AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorTimeout) .ShouldBe(AbLegacyStatusMapper.BadTimeout); AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorNotFound) .ShouldBe(AbLegacyStatusMapper.BadNodeIdUnknown); } }