dabd8eb955
MAJOR-1: the SDK skips TypeInfo.IsInstanceOfDataType on any node with an OnWriteValue handler (every writable equipment node), and the node manager's fail-fast treated numeric<->numeric as compatible, so a UInt32[] to a Galaxy Int32 array matched MxValueEncoder's int[] arm by CLR covariance and was written bit-reinterpreted with a Good result. EvaluateEquipmentWriteStructure now matches an ARRAY write's element type exactly (Byte[]/ByteString the only equivalence; Variant[]/object[] refused) -> BadTypeMismatch, nothing dispatched. MxValueEncoder's int[]/long[] arms are exact-type guarded too, and the Galaxy parity test's false "the SDK refuses" premise is corrected (the UInt32/UInt64 exclusion is now the encoder's own answer). Scalar writes keep numeric widening by decision: consumers write CLR-typed values (Double to a Float node) and every driver's scalar encoder range-checks. MINOR: Modbus row honours maxRegistersPerWrite (smallest of DriverConfig / the tag's DeviceConfig, default 123) and FC15's 1968-coil ceiling, and requires region + numeric address; Modbus/S7/Galaxy require isArray + integer arrayLength >= 1 (Galaxy: an unbounded node would let a short write resize the attribute); an array value to a scalar node is refused inline. Docs: Uns.md array-write rules + upgrade note (ReadWrite array tags become client-writable on first boot), Modbus.md, Galaxy.md. Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
161 lines
6.5 KiB
C#
161 lines
6.5 KiB
C#
using Google.Protobuf.WellKnownTypes;
|
|
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="MxValueEncoder"/>. Pinning each scalar + array case here
|
|
/// guards against accidental drift in the IWritable wire format.
|
|
/// </summary>
|
|
public sealed class MxValueEncoderTests
|
|
{
|
|
/// <summary>Verifies that encoding a null value sets the IsNull flag.</summary>
|
|
[Fact]
|
|
public void Encode_Null_SetsIsNullFlag()
|
|
{
|
|
var v = MxValueEncoder.Encode(null);
|
|
v.IsNull.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>Verifies that encoding a boolean value encodes correctly.</summary>
|
|
[Fact]
|
|
public void Encode_Bool() => MxValueEncoder.Encode(true).BoolValue.ShouldBe(true);
|
|
|
|
/// <summary>Verifies that narrow signed and unsigned types fit into Int32 encoding.</summary>
|
|
/// <param name="input">The input value to encode.</param>
|
|
/// <param name="expected">The expected encoded integer value.</param>
|
|
[Theory]
|
|
[InlineData((sbyte)-5, -5)]
|
|
[InlineData((short)-1000, -1000)]
|
|
[InlineData((byte)42, 42)]
|
|
[InlineData((ushort)42_000, 42_000)]
|
|
public void Encode_NarrowSignedAndUnsigned_FitsInInt32(object input, int expected)
|
|
{
|
|
var v = MxValueEncoder.Encode(input);
|
|
v.KindCase.ShouldBe(MxValue.KindOneofCase.Int32Value);
|
|
v.Int32Value.ShouldBe(expected);
|
|
}
|
|
|
|
/// <summary>Verifies that Int32 values round-trip through encoding.</summary>
|
|
[Fact]
|
|
public void Encode_Int32_RoundTrip() => MxValueEncoder.Encode(int.MinValue).Int32Value.ShouldBe(int.MinValue);
|
|
|
|
/// <summary>Verifies that Int64 values round-trip through encoding.</summary>
|
|
[Fact]
|
|
public void Encode_Int64_RoundTrip()
|
|
{
|
|
var v = MxValueEncoder.Encode(long.MaxValue);
|
|
v.KindCase.ShouldBe(MxValue.KindOneofCase.Int64Value);
|
|
v.Int64Value.ShouldBe(long.MaxValue);
|
|
}
|
|
|
|
/// <summary>Verifies that UInt32 values fit in Int32 encoding.</summary>
|
|
[Fact]
|
|
public void Encode_UInt32_FitsInInt32() => MxValueEncoder.Encode((uint)int.MaxValue).Int32Value.ShouldBe(int.MaxValue);
|
|
|
|
/// <summary>Verifies that float values encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_Float() => MxValueEncoder.Encode(3.14f).FloatValue.ShouldBe(3.14f);
|
|
|
|
/// <summary>Verifies that double values encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_Double() => MxValueEncoder.Encode(2.71828).DoubleValue.ShouldBe(2.71828);
|
|
|
|
/// <summary>Verifies that string values encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_String() => MxValueEncoder.Encode("hello").StringValue.ShouldBe("hello");
|
|
|
|
/// <summary>Verifies that UTC DateTime values encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_DateTimeUtc()
|
|
{
|
|
var when = new DateTime(2026, 4, 29, 12, 0, 0, DateTimeKind.Utc);
|
|
var v = MxValueEncoder.Encode(when);
|
|
v.TimestampValue.ShouldNotBeNull();
|
|
v.TimestampValue.ToDateTime().ShouldBe(when);
|
|
}
|
|
|
|
/// <summary>Verifies that local DateTime values are converted to UTC during encoding.</summary>
|
|
[Fact]
|
|
public void Encode_DateTimeLocal_ConvertsToUtc()
|
|
{
|
|
var local = new DateTime(2026, 4, 29, 12, 0, 0, DateTimeKind.Local);
|
|
var v = MxValueEncoder.Encode(local);
|
|
v.TimestampValue.ToDateTime().ShouldBe(local.ToUniversalTime());
|
|
}
|
|
|
|
/// <summary>Verifies that boolean arrays encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_BoolArray()
|
|
{
|
|
var v = MxValueEncoder.Encode(new[] { true, false, true });
|
|
v.ArrayValue.BoolValues.Values.ToArray().ShouldBe(new[] { true, false, true });
|
|
v.ArrayValue.Dimensions[0].ShouldBe(3u);
|
|
}
|
|
|
|
/// <summary>Verifies that double arrays encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_DoubleArray()
|
|
{
|
|
var v = MxValueEncoder.Encode(new[] { 1.0, 2.0, 3.5 });
|
|
v.ArrayValue.DoubleValues.Values.ToArray().ShouldBe(new[] { 1.0, 2.0, 3.5 });
|
|
}
|
|
|
|
/// <summary>Verifies that string arrays encode correctly.</summary>
|
|
[Fact]
|
|
public void Encode_StringArray()
|
|
{
|
|
var v = MxValueEncoder.Encode(new[] { "a", "b" });
|
|
v.ArrayValue.StringValues.Values.ToArray().ShouldBe(new[] { "a", "b" });
|
|
}
|
|
|
|
/// <summary>Verifies that all DateTime values in arrays are converted to UTC during encoding.</summary>
|
|
[Fact]
|
|
public void Encode_DateTimeArray_ConvertsAllToUtc()
|
|
{
|
|
var inputs = new[] { new DateTime(2026, 4, 29, 0, 0, 0, DateTimeKind.Utc) };
|
|
var v = MxValueEncoder.Encode(inputs);
|
|
v.ArrayValue.TimestampValues.Values[0].ToDateTime().ShouldBe(inputs[0]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gitea #610 review — CLR array covariance makes <c>uint[] is int[]</c> and <c>ulong[] is long[]</c> true,
|
|
/// so the int[]/long[] arms used to encode an unsigned array by reinterpreting its bits (4,000,000,000 →
|
|
/// -294,967,296). An unsigned array is refused instead, like an out-of-range unsigned scalar.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Encode_UnsignedArrays_AreRefused_NotReinterpreted()
|
|
{
|
|
Should.Throw<ArgumentException>(() => MxValueEncoder.Encode(new uint[] { 4_000_000_000 }));
|
|
Should.Throw<ArgumentException>(() => MxValueEncoder.Encode(new ulong[] { ulong.MaxValue }));
|
|
// Positive control: the signed arrays still encode.
|
|
MxValueEncoder.Encode(new[] { -1 }).ArrayValue.Int32Values.Values.ShouldBe(new[] { -1 });
|
|
MxValueEncoder.Encode(new[] { -1L }).ArrayValue.Int64Values.Values.ShouldBe(new[] { -1L });
|
|
}
|
|
|
|
/// <summary>Verifies that encoding an unsupported type throws an exception.</summary>
|
|
[Fact]
|
|
public void Encode_UnsupportedType_Throws()
|
|
{
|
|
Should.Throw<ArgumentException>(() => MxValueEncoder.Encode(new { Foo = 1 }));
|
|
}
|
|
|
|
/// <summary>Verifies that all scalar types round-trip correctly through encode and decode.</summary>
|
|
[Fact]
|
|
public void RoundTrip_AllScalarTypes_DecodeMatchesOriginal()
|
|
{
|
|
// The encoder + decoder must be inverses for every scalar a Galaxy driver might
|
|
// hand to a write. This pin-test catches accidental drift in either direction.
|
|
object[] inputs = [true, 42, 12345L, 3.14f, 2.71828, "x"];
|
|
foreach (var input in inputs)
|
|
{
|
|
var encoded = MxValueEncoder.Encode(input);
|
|
var decoded = MxValueDecoder.Decode(encoded);
|
|
decoded.ShouldBe(input);
|
|
}
|
|
}
|
|
}
|