Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests/OpcUaDataTypeNameMapTests.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

93 lines
3.4 KiB
C#

using Opc.Ua;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
/// <summary>
/// Type-info: pure NodeId → friendly built-in DataType name mapping.
/// Known OPC UA built-in DataType NodeIds (ns=0;i=…) resolve to their CLR-ish
/// friendly names; unknown/custom DataType NodeIds fall back to the NodeId string.
/// No live server is required — this is a pure lookup helper.
/// </summary>
public class OpcUaDataTypeNameMapTests
{
/// <summary>Verifies that resolve, known built in types, return friendly names.</summary>
/// <param name="typeName">Test case input.</param>
[Theory]
[InlineData("Boolean")]
[InlineData("SByte")]
[InlineData("Byte")]
[InlineData("Int16")]
[InlineData("UInt16")]
[InlineData("Int32")]
[InlineData("UInt32")]
[InlineData("Int64")]
[InlineData("UInt64")]
[InlineData("Float")]
[InlineData("Double")]
[InlineData("String")]
[InlineData("DateTime")]
[InlineData("Guid")]
[InlineData("ByteString")]
public void Resolve_KnownBuiltInTypes_ReturnFriendlyNames(string typeName)
{
// Look up the well-known DataType NodeId by its standard name on
// DataTypeIds (e.g. DataTypeIds.Double) so the test mirrors exactly
// the constants the helper maps against.
// SDK 2 (ScadaBridge#125) exposes the ids as static properties, not fields.
var property = typeof(DataTypeIds).GetProperty(typeName)!;
var nodeId = (NodeId)property.GetValue(null)!;
Assert.Equal(typeName, OpcUaBuiltInTypeNames.Resolve(nodeId));
}
/// <summary>Verifies that resolve, double, returns double.</summary>
[Fact]
public void Resolve_Double_ReturnsDouble()
{
Assert.Equal("Double", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Double));
}
/// <summary>Verifies that resolve, int32, returns int32.</summary>
[Fact]
public void Resolve_Int32_ReturnsInt32()
{
Assert.Equal("Int32", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Int32));
}
/// <summary>Verifies that resolve, boolean, returns boolean.</summary>
[Fact]
public void Resolve_Boolean_ReturnsBoolean()
{
Assert.Equal("Boolean", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Boolean));
}
/// <summary>Verifies that resolve, string, returns string.</summary>
[Fact]
public void Resolve_String_ReturnsString()
{
Assert.Equal("String", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.String));
}
/// <summary>Verifies that resolve, unknown custom node id, returns node id string.</summary>
[Fact]
public void Resolve_UnknownCustomNodeId_ReturnsNodeIdString()
{
// A vendor-defined DataType (string identifier, non-zero namespace) is
// not a built-in, so the helper falls back to the NodeId string — the
// only thing the UI can meaningfully display for an opaque type.
var custom = new NodeId("CustomType", 5);
Assert.Equal(custom.ToString(), OpcUaBuiltInTypeNames.Resolve(custom));
}
/// <summary>Verifies that resolve, null, returns empty string.</summary>
[Fact]
public void Resolve_Null_ReturnsEmptyString()
{
// Defensive: a null DataType attribute value must not throw during a
// best-effort browse type-read.
Assert.Equal(string.Empty, OpcUaBuiltInTypeNames.Resolve(null));
}
}