Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataVersionSkewCodecTests.cs
T
Joseph Doherty e66f072121 fix(grpc-data): an unknown value/element kind or per-item metadata status from a newer origin fails one item, not the whole browse/metadata response (#956)
DataProtoMapper.DecodeMetadata decodes an undefined ValueKind / ArrayElementKind number to an unknown-kind
marker (DataMetadata.HasUnknownKind, ValidateDecoded) instead of throwing; Validate/Encode still refuse it.
The resolver's #941 IsMaterialisable skips the marker whatever its node kind (a Folder included); a remote
root carrying it is refused. An undefined per-item metadata status decodes to Unavailable (no value, no lease).
Unspecified (0), bad array shape, unknown realm and unknown branch status still fail closed.
2026-12-01 20:20:15 -05:00

137 lines
6.9 KiB
C#

using Google.Protobuf;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.DataConnections;
/// <summary>
/// #956: an enum number a newer origin added must fail only the item that carries it, never the whole browse page or
/// metadata batch at decode — the consumer's resolver (#941) skips the item. Malformed values (Unspecified = 0, a shape
/// that does not agree) still fail closed.
/// </summary>
[Trait("Feature", "GrpcData"), Trait("Category", "Unit")]
public sealed class GrpcDataVersionSkewCodecTests
{
private const int NewerKind = 99; // a ValueKind number this build does not define
private static readonly DataView View = new(new DataAddress("c", "urn:test", "string", "x"), DataRealm.Raw, null, 0, "x");
private static DataMetadata Scalar(string name = "x") =>
new(View with { Address = View.Address with { Identifier = name }, Subject = name }, name, "Variable", DataValueKind.Int32, -1,
[], false, DataOperations.Read, 1, null, null, false);
private static DataMetadata Array1() =>
new(View, "a", "Variable", DataValueKind.Array, 1, [0], false, DataOperations.Read, 1, null, null, false, DataValueKind.Int32);
/// <summary>Wire bytes from a newer origin: the field carries a number this build's proto enum does not define.</summary>
private static W.Metadata Reparse(W.Metadata wire) => W.Metadata.Parser.ParseFrom(wire.ToByteArray());
[Theory]
[InlineData("Variable")]
[InlineData("Folder")]
[InlineData("Method")]
public void An_unknown_value_kind_decodes_to_an_unknown_kind_marker_not_a_failure(string nodeKind)
{
var wire = DataProtoMapper.EncodeMetadata(Scalar() with { NodeKind = nodeKind });
wire.ValueKind = (W.ValueKind)NewerKind;
var decoded = DataProtoMapper.DecodeMetadata(Reparse(wire));
((int)decoded.ValueKind).ShouldBe(NewerKind);
decoded.HasUnknownKind.ShouldBeTrue();
decoded.BrowseName.ShouldBe("x");
// The marker is never valid as a contract value: it cannot be validated, re-encoded or granted.
Should.Throw<ArgumentException>(() => decoded.Validate());
Should.Throw<ArgumentException>(() => DataProtoMapper.EncodeMetadata(decoded));
}
[Fact]
public void An_unknown_array_element_kind_decodes_to_the_marker_with_its_shape_still_checked()
{
var wire = DataProtoMapper.EncodeMetadata(Array1());
wire.ArrayElementKind = (W.ValueKind)NewerKind;
var decoded = DataProtoMapper.DecodeMetadata(Reparse(wire));
decoded.HasUnknownKind.ShouldBeTrue();
((int)decoded.ArrayElementKind!.Value).ShouldBe(NewerKind);
// A known Array whose shape does not agree is still malformed, whatever its element kind.
var badShape = wire.Clone();
badShape.Dimensions.Add(0);
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadata(badShape));
// A known scalar may not carry an element kind at all — an unknown one included.
var scalar = DataProtoMapper.EncodeMetadata(Scalar());
scalar.ArrayElementKind = (W.ValueKind)NewerKind;
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadata(scalar));
}
[Fact]
public void Unspecified_is_still_malformed_never_version_skew()
{
var wire = DataProtoMapper.EncodeMetadata(Scalar());
wire.ValueKind = W.ValueKind.Unspecified;
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadata(wire));
var unknownWithUnspecifiedElement = DataProtoMapper.EncodeMetadata(Scalar());
unknownWithUnspecifiedElement.ValueKind = (W.ValueKind)NewerKind;
unknownWithUnspecifiedElement.ArrayElementKind = W.ValueKind.Unspecified;
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadata(unknownWithUnspecifiedElement));
var unknownWithBadRevision = DataProtoMapper.EncodeMetadata(Scalar());
unknownWithBadRevision.ValueKind = (W.ValueKind)NewerKind;
unknownWithBadRevision.CatalogRevision = 0;
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadata(unknownWithBadRevision));
}
[Fact]
public void One_unknown_kind_item_does_not_fail_the_browse_page()
{
var response = DataProtoMapper.EncodeBrowseResponse(new DataBrowseBranch(View, DataItemStatus.Good,
[Scalar("a"), Scalar("b"), Scalar("c")], true, null, null));
response.Items[1].ValueKind = (W.ValueKind)NewerKind;
var branch = DataProtoMapper.DecodeBrowseResponse(W.BrowseResponse.Parser.ParseFrom(response.ToByteArray()),
new DataDecodeLimits(10, 100000, 100));
branch.Items.Select(x => x.BrowseName).ShouldBe(["a", "b", "c"]);
branch.Items.Select(x => x.HasUnknownKind).ShouldBe([false, true, false]);
}
[Fact]
public void One_unknown_kind_item_does_not_fail_the_metadata_batch()
{
var response = DataProtoMapper.EncodeMetadataResponse([
new DataItemResult<DataMetadata>("0", DataItemStatus.Good, Scalar("a"), null),
new DataItemResult<DataMetadata>("1", DataItemStatus.Good, Scalar("b"), null)]);
response.Items[0].Value.ValueKind = (W.ValueKind)NewerKind;
var items = DataProtoMapper.DecodeMetadataResponse(W.MetadataResponse.Parser.ParseFrom(response.ToByteArray()),
new DataDecodeLimits(10, 100000, 100));
items[0].Value!.HasUnknownKind.ShouldBeTrue();
items[1].Value!.HasUnknownKind.ShouldBeFalse();
items[1].Value!.ValueKind.ShouldBe(DataValueKind.Int32);
}
[Fact]
public void An_unknown_per_item_metadata_status_fails_that_item_transiently_not_the_batch()
{
var response = DataProtoMapper.EncodeMetadataResponse([
new DataItemResult<DataMetadata>("0", DataItemStatus.Good, Scalar("a"), null,
new MetadataLease(1, 1, TimeSpan.FromSeconds(1))),
new DataItemResult<DataMetadata>("1", DataItemStatus.Good, Scalar("b"), null)]);
response.Items[0].Status = 99; // a status a newer origin added; it even carries a value and a lease
var items = DataProtoMapper.DecodeMetadataResponse(W.MetadataResponse.Parser.ParseFrom(response.ToByteArray()),
new DataDecodeLimits(10, 100000, 100));
items[0].CorrelationId.ShouldBe("0");
items[0].Status.ShouldBe(DataItemStatus.Unavailable);
items[0].Value.ShouldBeNull("an unknown status grants nothing");
items[0].Lease.ShouldBeNull("an unknown status grants nothing");
items[1].Status.ShouldBe(DataItemStatus.Good);
// Unspecified (0) is malformed and still fails the response.
response.Items[0].Status = 0;
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeMetadataResponse(response, new DataDecodeLimits(10, 100000, 100)));
}
}