b95b99ba8e
Every value verified against Opc.Ua.StatusCodes in the pinned SDK (1.5.378.106) by reflection, not by copying siblings. All are client-visible: OPC UA clients branch on status. The three named in #497: - Historian.Gateway SampleMapper "BadNoData" 0x800E0000 -> 0x809B0000 (0x800E0000 is BadServerHalted) - Galaxy "BadTimeout" 0x800B0000 -> 0x800A0000 (0x800B0000 is BadServiceUnsupported) - TwinCAT BadTypeMismatch 0x80730000 -> 0x80740000 (0x80730000 is BadWriteNotSupported) BadTypeMismatch was wrong in three MORE drivers the issue did not name — FOCAS, AbLegacy and AbCip carried the same 0x80730000. Every call site is a FormatException/InvalidCastException conversion failure, so the name was right and the value was wrong in all four. Adding the reflection guard then surfaced nine further defects offline tests had never checked: - Galaxy GoodLocalOverride 0x00D80000 -> 0x00960000 (not a UA code at all) - Galaxy UncertainLastUsableValue 0x40A40000 -> 0x40900000 - Galaxy UncertainSensorNotAccurate 0x408D0000 -> 0x40930000 - Galaxy UncertainEngineeringUnitsExceeded 0x408E0000 -> 0x40940000 - Galaxy UncertainSubNormal 0x408F0000 -> 0x40950000 - TwinCAT BadOutOfService 0x80BE0000 -> 0x808D0000 (was BadProtocolVersionUnsupported) - TwinCAT BadInvalidState 0x80350000 -> 0x80AF0000 (was BadAttributeIdInvalid) - AbCip/AbLegacy GoodMoreData 0x00A70000 -> 0x00A60000 (was GoodCommunicationEvent) - Historian.Gateway GatewayQualityMapper Good_LocalOverride 0x00D80000 -> 0x00960000 Galaxy's whole Uncertain block read as though the OPC DA quality byte could be shifted into the UA substatus position; it cannot — the substatuses are an unrelated enumeration. GatewayQualityMapper already held the correct table, which is what the Galaxy values are now reconciled against. Guard: StatusCodeParityTests (Core.Abstractions.Tests, which already project- references every driver) reflects over every `const uint` in the deployed ZB.MOM.WW.OtOpcUa.Driver.*.dll whose name reads as a status code, and asserts it equals Opc.Ua.StatusCodes.<name>. Discovery is by convention, so a new driver assembly referenced by that project is covered with no edit here. It went red on all nine defects above before the fixes and now checks 109 constants green. Because reflection can only see a NAMED constant, two inline literals were hoisted so the guard can reach them: Galaxy's BadTimeout/Bad into StatusCodeMap, and GatewayQualityMapper's 15-entry table into a new HistorianStatusCodes. An inline `0x800B0000u, // BadTimeout` at a call site is exactly how the Galaxy defect survived. The drivers stay SDK-free by design; only the test project references Opc.Ua.Core, as the oracle. Also corrected: tests that pinned the wrong values (Galaxy StatusCodeMapTests, SampleMapperTests, GatewayQualityMapperTests — all written from the same bad source), a mislabelled comment in DriverInstanceActorTests, and stale constants in two design docs, one of them the unexecuted MTConnect driver design that would have propagated BadNoData's wrong value into a new driver. The CLI's SnapshotFormatter value->name table was checked against the SDK and is correct; no change needed.
157 lines
7.9 KiB
C#
157 lines
7.9 KiB
C#
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
|
// MxStatusCategory needed for Driver.Galaxy-003 regression tests.
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
|
|
|
|
/// <summary>
|
|
/// Exhaustive table-driven tests for <see cref="StatusCodeMap"/>. Pinning the byte→uint
|
|
/// mapping here protects against accidental drift — every Galaxy deployment that
|
|
/// reaches the parity matrix in PR 5.2 depends on these specific OPC UA StatusCode
|
|
/// values matching the legacy <c>HistorianQualityMapper</c> output.
|
|
/// </summary>
|
|
public sealed class StatusCodeMapTests
|
|
{
|
|
/// <summary>Verifies that known quality bytes map to the expected OPC UA status codes.</summary>
|
|
/// <param name="input">The Galaxy quality byte to map.</param>
|
|
/// <param name="expected">The expected OPC UA status code.</param>
|
|
[Theory]
|
|
[InlineData((byte)192, 0x00000000u)] // Good
|
|
[InlineData((byte)216, 0x00960000u)] // Good_LocalOverride
|
|
[InlineData((byte)64, 0x40000000u)] // Uncertain
|
|
[InlineData((byte)68, 0x40900000u)] // Uncertain_LastUsableValue
|
|
[InlineData((byte)80, 0x40930000u)] // Uncertain_SensorNotAccurate
|
|
[InlineData((byte)84, 0x40940000u)] // Uncertain_EngineeringUnitsExceeded
|
|
[InlineData((byte)88, 0x40950000u)] // Uncertain_SubNormal
|
|
[InlineData((byte)0, 0x80000000u)] // Bad
|
|
[InlineData((byte)4, 0x80890000u)] // Bad_ConfigurationError
|
|
[InlineData((byte)8, 0x808A0000u)] // Bad_NotConnected
|
|
[InlineData((byte)12, 0x808B0000u)] // Bad_DeviceFailure
|
|
[InlineData((byte)16, 0x808C0000u)] // Bad_SensorFailure
|
|
[InlineData((byte)20, 0x80050000u)] // Bad_CommunicationError
|
|
[InlineData((byte)24, 0x808D0000u)] // Bad_OutOfService
|
|
[InlineData((byte)32, 0x80320000u)] // Bad_WaitingForInitialData
|
|
public void FromQualityByte_KnownValues_MapToOpcUaStatusCode(byte input, uint expected)
|
|
{
|
|
StatusCodeMap.FromQualityByte(input).ShouldBe(expected);
|
|
}
|
|
|
|
/// <summary>Verifies that unknown quality bytes fall back to category bucket status codes.</summary>
|
|
/// <param name="input">The unknown Galaxy quality byte to categorize.</param>
|
|
[Theory]
|
|
[InlineData((byte)200)] // Unknown Good — falls back to category bucket
|
|
[InlineData((byte)100)] // Unknown Uncertain
|
|
[InlineData((byte)40)] // Unknown Bad
|
|
public void FromQualityByte_UnknownValues_FallBackToCategoryBucket(byte input)
|
|
{
|
|
var mapped = StatusCodeMap.FromQualityByte(input);
|
|
if (input >= 192) mapped.ShouldBe(StatusCodeMap.Good);
|
|
else if (input >= 64) mapped.ShouldBe(StatusCodeMap.Uncertain);
|
|
else mapped.ShouldBe(StatusCodeMap.Bad);
|
|
}
|
|
|
|
// ===== Driver.Galaxy-003 regression: FromMxStatus uses IsSuccess() (category + success) =====
|
|
// The proto doc: "clients should branch on category, not on a specific success value."
|
|
// IsSuccess() requires BOTH success != 0 AND category == Ok — checking success alone
|
|
// would invert the mapping when the worker sets success=1 for an error code (the numeric
|
|
// value is NOT a boolean).
|
|
|
|
/// <summary>Verifies that null status maps to Good status code.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_NullStatus_IsGood()
|
|
{
|
|
StatusCodeMap.FromMxStatus(null).ShouldBe(StatusCodeMap.Good);
|
|
}
|
|
|
|
/// <summary>Verifies that non-zero success with OK category maps to Good.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_SuccessNonZeroAndCategoryOk_IsGood()
|
|
{
|
|
// Both conditions required — this is the canonical OK path.
|
|
var s = new MxStatusProxy { Success = 1, Category = MxStatusCategory.Ok };
|
|
StatusCodeMap.FromMxStatus(s).ShouldBe(StatusCodeMap.Good);
|
|
}
|
|
|
|
/// <summary>Verifies that non-zero success with non-OK category does not map to Good.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_SuccessNonZeroButCategoryNotOk_IsNotGood()
|
|
{
|
|
// Bug fixed by Driver.Galaxy-003: success != 0 alone used to return Good even when
|
|
// category indicates an error. The proto says success is NOT a boolean — category wins.
|
|
var s = new MxStatusProxy { Success = 1, Category = MxStatusCategory.CommunicationError };
|
|
StatusCodeMap.FromMxStatus(s).ShouldNotBe(StatusCodeMap.Good);
|
|
}
|
|
|
|
/// <summary>Verifies that known detail codes map to their specific status codes.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_SuccessZeroAndCategoryNotOk_DetailKnown_MapsToSpecificCode()
|
|
{
|
|
var s = new MxStatusProxy { Success = 0, Category = MxStatusCategory.OperationalError, Detail = 8 /* Bad_NotConnected */ };
|
|
StatusCodeMap.FromMxStatus(s).ShouldBe(StatusCodeMap.BadNotConnected);
|
|
}
|
|
|
|
/// <summary>Verifies that non-zero DetectedBy maps to communication error when detail is zero.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_SuccessZero_DetailZero_DetectedByNonZero_IsCommunicationError()
|
|
{
|
|
var s = new MxStatusProxy { Success = 0, Detail = 0, RawDetectedBy = 3 };
|
|
StatusCodeMap.FromMxStatus(s).ShouldBe(StatusCodeMap.BadCommunicationError);
|
|
}
|
|
|
|
/// <summary>Verifies that all-zero status maps to Bad status code.</summary>
|
|
[Fact]
|
|
public void FromMxStatus_SuccessZero_AllZero_IsBad()
|
|
{
|
|
var s = new MxStatusProxy();
|
|
StatusCodeMap.FromMxStatus(s).ShouldBe(StatusCodeMap.Bad);
|
|
}
|
|
|
|
/// <summary>Verifies that top two bits of status codes follow OPC UA convention.</summary>
|
|
[Fact]
|
|
public void TopByteCategoryBits_StayWithinOpcUaConvention()
|
|
{
|
|
// Sanity check that every Bad code we mint actually has the Bad top byte (0x80…),
|
|
// every Uncertain has 0x40…, every Good has 0x00…. Pins the OPC UA Part 4 invariant.
|
|
StatusCodeMap.Good.ShouldBeLessThan(0x40000000u);
|
|
StatusCodeMap.GoodLocalOverride.ShouldBeLessThan(0x40000000u);
|
|
|
|
((StatusCodeMap.Uncertain >> 30) & 0x3u).ShouldBe(1u);
|
|
((StatusCodeMap.UncertainLastUsableValue >> 30) & 0x3u).ShouldBe(1u);
|
|
|
|
((StatusCodeMap.Bad >> 30) & 0x3u).ShouldBe(2u);
|
|
((StatusCodeMap.BadNotConnected >> 30) & 0x3u).ShouldBe(2u);
|
|
((StatusCodeMap.BadOutOfService >> 30) & 0x3u).ShouldBe(2u);
|
|
}
|
|
|
|
// ===== Driver.Galaxy-004 regression: ToQualityCategoryByte lives next to its inverse =====
|
|
|
|
/// <summary>Verifies that status codes extract to their OPC DA category byte equivalent.</summary>
|
|
/// <param name="statusCode">The OPC UA status code to convert.</param>
|
|
/// <param name="expected">The expected OPC DA category byte.</param>
|
|
[Theory]
|
|
[InlineData(0x00000000u, (byte)192)] // Good
|
|
[InlineData(0x00960000u, (byte)192)] // GoodLocalOverride — still Good category
|
|
[InlineData(0x40000000u, (byte)64)] // Uncertain
|
|
[InlineData(0x40950000u, (byte)64)] // UncertainSubNormal — still Uncertain category
|
|
[InlineData(0x80000000u, (byte)0)] // Bad
|
|
[InlineData(0x808A0000u, (byte)0)] // BadNotConnected — still Bad category
|
|
[InlineData(0x80020000u, (byte)0)] // BadInternalError — still Bad category
|
|
public void ToQualityCategoryByte_ExtractsTopTwoBitsAsOpcDaByte(uint statusCode, byte expected)
|
|
{
|
|
StatusCodeMap.ToQualityCategoryByte(statusCode).ShouldBe(expected);
|
|
}
|
|
|
|
/// <summary>Verifies that ToQualityCategoryByte is the right inverse of FromQualityByte for category bytes.</summary>
|
|
[Fact]
|
|
public void ToQualityCategoryByte_IsRightInverseOfFromQualityByte_ForCategoryBytes()
|
|
{
|
|
// The category bytes the probe watcher uses are 0, 64, 192. Round-trip: a value
|
|
// that came FROM those bytes should map back to the same byte.
|
|
StatusCodeMap.ToQualityCategoryByte(StatusCodeMap.FromQualityByte(0)).ShouldBe((byte)0);
|
|
StatusCodeMap.ToQualityCategoryByte(StatusCodeMap.FromQualityByte(64)).ShouldBe((byte)64);
|
|
StatusCodeMap.ToQualityCategoryByte(StatusCodeMap.FromQualityByte(192)).ShouldBe((byte)192);
|
|
}
|
|
}
|