Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests/Adapters/OpcUaValueBoundaryTests.cs
T
Joseph Doherty a2e9d71824 feat(dcl): move the OPC UA client to OPC Foundation SDK 2.0.1 (#125)
OPCFoundation.NetStandard.Opc.Ua.Client and the test-only .Server move
1.5.378.182 -> 2.0.1, the version OtOpcUa master runs (lmxopcua#782; #799
and #801 closed). Transitive Core/Core.Types/Types/Configuration/
Security.Certificates resolve to 2.0.1.

API breaks fixed in the DCL adapter:
- Built-in types are immutable structs (Variant, DataValue, NodeId,
  LocalizedText, StatusCode, ArrayOf<T>). New OpcUaValueBoundary restores
  the SDK 1.5 CLR shapes for every read, subscription and alarm-event
  value (byte[], UTC DateTime with no-timestamp = MinValue, Guid,
  detached arrays) and builds write variants with SDK 1.5's
  new Variant(object) semantics (byte[] = ByteString scalar).
- StatusCodes members are no longer constants: switches/patterns over
  them (verify-outcome mapping, connection-level classification,
  BadResourceUnavailable) match by value.
- CertificateIdentifier no longer caches a certificate and the
  CertificateValidator/CertificateValidation event is gone: the
  configuration's CertificateManager owns trust lists and the loaded
  application certificate and is disposed with the session; auto-accept
  and the verify probe's capture-and-reject use AcceptError.
- new UserIdentity(X509Certificate2) is gone: X509 user identities use
  X509IdentityTokenHandler with an on-demand PFX provider (still fails
  fast on a missing file/key).
- DefaultSessionFactory takes the telemetry context; collections are
  ArrayOf<T>; EventFilter select clauses and ContentFilter operands are
  built immutably.

UserName identity (lmxopcua#880): SDK 2's default handler sends a
password over 64 bytes - every 65-character OtOpcUa API key - as an
RsaEncryptedSecret that SDK 1.5 servers reject with BadIdentityTokenInvalid
and SDK 2 servers accept only within a 5-minute clock window (#873). The
DCL now always uses LegacyRsaUserNameIdentityTokenHandler (ported from
OtOpcUa be28aa44), i.e. exactly what it sent on SDK 1.5, so it logs in to
both server generations during a mixed rollout.

Tests: the in-process secured test server is ported to SDK 2 and can
admit an API key; new tests cover the handler (mutation-checked: 5 of 7
fail with the SDK default handler), the value boundary, and a 65-char key
end to end over None/Sign/SignAndEncrypt with a wrong key reaching the
key check (BadIdentityTokenRejected).

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
2026-11-12 22:20:53 -05:00

96 lines
3.8 KiB
C#

using Opc.Ua;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;
/// <summary>
/// ScadaBridge#125: SDK 2's legacy boxing hands back SDK wrapper types where SDK 1.5 handed back
/// CLR ones. The DCL boundary must restore the SDK 1.5 payload shapes the codec, scripts and the
/// alarm mapper were written against, and must not alias SDK buffers.
/// </summary>
public class OpcUaValueBoundaryTests
{
[Fact]
public void Scalars_KeepTheirClrTypes()
{
Assert.Equal(42, OpcUaValueBoundary.ToClr(Variant.From(42)));
Assert.Equal(42.5, OpcUaValueBoundary.ToClr(Variant.From(42.5)));
Assert.Equal(true, OpcUaValueBoundary.ToClr(Variant.From(true)));
Assert.Equal("abc", OpcUaValueBoundary.ToClr(Variant.From("abc")));
Assert.Equal((ushort)500, OpcUaValueBoundary.ToClr(Variant.From((ushort)500)));
Assert.Null(OpcUaValueBoundary.ToClr(Variant.Null));
}
[Fact]
public void ByteString_BecomesAByteArray()
{
var value = OpcUaValueBoundary.ToClr(Variant.From(ByteString.From(new byte[] { 1, 2, 3 })));
Assert.Equal(new byte[] { 1, 2, 3 }, Assert.IsType<byte[]>(value));
}
[Fact]
public void DateTime_BecomesAUtcDateTime_AndNoTimestampIsMinValue()
{
var when = new DateTime(2026, 11, 12, 10, 30, 0, DateTimeKind.Utc);
var value = Assert.IsType<DateTime>(OpcUaValueBoundary.ToClr(Variant.From(when)));
Assert.Equal(when, value);
Assert.Equal(DateTimeKind.Utc, value.Kind);
Assert.Equal(DateTime.MinValue, OpcUaValueBoundary.ToClrTimestamp(DateTimeUtc.MinValue));
Assert.Equal(when, OpcUaValueBoundary.ToClrTimestamp(when));
}
[Fact]
public void Guid_BecomesASystemGuid()
{
var id = Guid.NewGuid();
Assert.Equal(id, Assert.IsType<Guid>(OpcUaValueBoundary.ToClr(Variant.From(new Uuid(id)))));
}
[Fact]
public void Int32Array_BecomesADetachedClrArray()
{
var source = new[] { 1, 2, 3 };
var value = Assert.IsType<int[]>(OpcUaValueBoundary.ToClr(Variant.From((ArrayOf<int>)source)));
Assert.Equal(source, value);
Assert.NotSame(source, value);
}
[Fact]
public void ToVariant_ByteArray_IsAByteStringScalar()
{
// SDK 1.5's new Variant(byte[]) was a ByteString scalar; a Byte[] array would change the wire type.
var variant = OpcUaValueBoundary.ToVariant(new byte[] { 9, 8 });
Assert.Equal(BuiltInType.ByteString, variant.TypeInfo.BuiltInType);
Assert.Equal(ValueRanks.Scalar, variant.TypeInfo.ValueRank);
}
[Fact]
public void ToVariant_Int32Array_IsAOneDimensionalInt32Array_AndCopiesTheBuffer()
{
var source = new[] { 4, 5, 6 };
var variant = OpcUaValueBoundary.ToVariant(source);
source[0] = 99;
Assert.Equal(BuiltInType.Int32, variant.TypeInfo.BuiltInType);
Assert.Equal(ValueRanks.OneDimension, variant.TypeInfo.ValueRank);
Assert.Equal(new[] { 4, 5, 6 }, Assert.IsType<int[]>(OpcUaValueBoundary.ToClr(variant)));
}
[Fact]
public void ToVariant_Scalars_MapToTheirBuiltInType()
{
Assert.Equal(BuiltInType.Double, OpcUaValueBoundary.ToVariant(1.5).TypeInfo.BuiltInType);
Assert.Equal(BuiltInType.Boolean, OpcUaValueBoundary.ToVariant(true).TypeInfo.BuiltInType);
Assert.Equal(BuiltInType.String, OpcUaValueBoundary.ToVariant("x").TypeInfo.BuiltInType);
Assert.True(OpcUaValueBoundary.ToVariant(null).IsNull);
}
[Fact]
public void ToVariant_UnsupportedType_Throws()
{
Assert.Throws<ArgumentException>(() => OpcUaValueBoundary.ToVariant(new object()));
Assert.Throws<ArgumentException>(() => OpcUaValueBoundary.ToVariant(new int[2, 2]));
}
}