using Google.Protobuf; using Google.Protobuf.Reflection; 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; [Trait("Feature", "GrpcData"), Trait("Phase", "P11"), Trait("Category", "Unit")] public sealed class GrpcDataMetadataLeaseCodecTests { private static readonly ServingIdentity Serving = new("node", "incarnation", "Primary", 11, 12, 13); private static readonly MetadataLease Lease = new(11, 12, TimeSpan.FromTicks(1234567)); private static DataMetadata Metadata(string identifier = "driver/device/tag", DataRealm realm = DataRealm.Raw) => new(new(new("cluster", "urn:fixture:" + realm, "string", identifier), realm, null, 0, realm == DataRealm.Uns ? "area/line/equipment" : identifier), "tag", "Variable", DataValueKind.Int32, -1, Array.Empty(), false, DataOperations.Browse | DataOperations.Metadata | DataOperations.Read, 11, "source-id", "unit", false); private static DataItemResult Good(string correlation = "request-slot-0") => new(correlation, DataItemStatus.Good, Metadata(), null, Lease); private static DataCapabilities Capabilities() => new(1, 99, Serving, DataReadiness.Ready, DataOperations.Metadata, new HashSet { DataValueKind.Int32 }, new(10, 10, 65536, 10, 1, 65536, TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)), true, 1, "cluster", SubscriptionAgeReadiness: true, ResponseServingIdentity: true, SubscriptionFreshnessChallenges: true); [Fact] public void Exact_metadata_result_can_negotiate_a_per_item_authority_lease() { W.CapabilitiesResponse.Descriptor.FindFieldByName("metadata_item_leases").ShouldNotBeNull().FieldNumber.ShouldBe(15); W.MetadataResult.Descriptor.FindFieldByName("lease").ShouldNotBeNull().FieldNumber.ShouldBe(5); typeof(DataCapabilities).GetProperty("MetadataItemLeases").ShouldNotBeNull(); typeof(DataItemResult).GetProperty("Lease").ShouldNotBeNull(); } [Fact] public void Serialized_response_preserves_each_items_lease_view_revision_correlation_and_serving_envelope() { var first = Good(); var second = new DataItemResult("request-slot-1", DataItemStatus.Good, Metadata("EQ-container", DataRealm.Uns), null, Lease with { Duration = TimeSpan.FromTicks(9876543) }); var wire = DataProtoMapper.EncodeMetadataResponse([first, second, new("request-slot-2", DataItemStatus.Denied, null, "denied"), Good("legacy-peer") with { Lease = null }]); wire.Serving = DataProtoMapper.EncodeServing(Serving); var received = W.MetadataResponse.Parser.ParseFrom(wire.ToByteArray()); var decoded = DataProtoMapper.DecodeMetadataResponse(received); received.Items[0].Lease.CatalogEpoch = 999; received.Items[1].Lease.DurationTicks = 1; DataProtoMapper.DecodeServing(received.Serving).ShouldBe(Serving); decoded.Select(item => item.CorrelationId).ShouldBe(new[] { "request-slot-0", "request-slot-1", "request-slot-2", "legacy-peer" }); decoded[0].Lease.ShouldBe(Lease); decoded[1].Lease.ShouldBe(second.Lease); decoded[0].Value.ShouldNotBeNull().View.ShouldBe(first.Value!.View); decoded[1].Value.ShouldNotBeNull().View.ShouldBe(second.Value!.View); decoded[0].Value!.CatalogRevision.ShouldBe(11); decoded[1].Value!.CatalogRevision.ShouldBe(11); decoded[1].Value!.View.Address.Identifier.ShouldBe("EQ-container"); decoded[1].Value!.View.Subject.ShouldBe("area/line/equipment"); decoded[2].Status.ShouldBe(DataItemStatus.Denied); decoded[2].Value.ShouldBeNull(); decoded[2].Lease.ShouldBeNull(); decoded[3].Status.ShouldBe(DataItemStatus.Good); decoded[3].Lease.ShouldBeNull(); } [Fact] public void Missing_per_item_lease_stays_unknown_and_is_not_borrowed_from_another_result() { var wire = DataProtoMapper.EncodeMetadataResponse([Good(), Good("other") with { Lease = null }]); wire.Items[0].Lease.ShouldNotBeNull(); wire.Items[1].Lease.ShouldBeNull(); var decoded = DataProtoMapper.DecodeMetadataResponse(W.MetadataResponse.Parser.ParseFrom(wire.ToByteArray())); decoded[0].Lease.ShouldBe(Lease); decoded[1].Lease.ShouldBeNull(); typeof(DataItemResult).GetProperty("Lease")!.GetValue( new DataItemResult("old-constructor", DataItemStatus.Good, Metadata(), null)).ShouldBeNull(); } [Fact] public void Older_peer_defaults_to_no_metadata_lease_capability_despite_newer_minor_or_other_extensions() { var capabilities = Capabilities(); var oldPeer = DataProtoMapper.EncodeCapabilities(capabilities); DataProtoMapper.DecodeCapabilities(W.CapabilitiesResponse.Parser.ParseFrom(oldPeer.ToByteArray())) .MetadataItemLeases.ShouldBeFalse(); var advertised = DataProtoMapper.EncodeCapabilities(capabilities with { MetadataItemLeases = true }); var decoded = DataProtoMapper.DecodeCapabilities(W.CapabilitiesResponse.Parser.ParseFrom(advertised.ToByteArray())); decoded.MetadataItemLeases.ShouldBeTrue(); decoded.ProtocolMinor.ShouldBe(99); decoded.Serving.ShouldBe(Serving); } [Fact] public void Additive_lease_fields_have_explicit_types_and_capability_does_not_imply_readiness() { var capability = W.CapabilitiesResponse.Descriptor.FindFieldByName("metadata_item_leases"); capability.FieldNumber.ShouldBe(15); capability.FieldType.ShouldBe(FieldType.Bool); var lease = W.MetadataResult.Descriptor.FindFieldByName("lease"); lease.FieldNumber.ShouldBe(5); lease.FieldType.ShouldBe(FieldType.Message); lease.MessageType.FullName.ShouldBe(W.Lease.Descriptor.FullName); var wire = DataProtoMapper.EncodeCapabilities(Capabilities() with { MetadataItemLeases = true, Readiness = DataReadiness.Unavailable }); DataProtoMapper.DecodeCapabilities(wire).Readiness.ShouldBe(DataReadiness.Unavailable); wire.Readiness = (int)DataReadiness.Unspecified; Should.Throw(() => DataProtoMapper.DecodeCapabilities(wire)); } [Theory] [InlineData(0L, 12L, 1L)] [InlineData(-1L, 12L, 1L)] [InlineData(11L, 0L, 1L)] [InlineData(11L, -1L, 1L)] [InlineData(11L, 12L, 0L)] [InlineData(11L, 12L, -1L)] public void Invalid_authority_epochs_and_duration_are_rejected_on_encode_and_decode(long catalog, long authority, long ticks) { var invalid = new MetadataLease(catalog, authority, TimeSpan.FromTicks(ticks)); Should.Throw(() => DataProtoMapper.EncodeMetadataResult(Good() with { Lease = invalid })); var wire = DataProtoMapper.EncodeMetadataResult(Good()); wire.Lease = new W.Lease { CatalogEpoch = catalog, AuthorityEpoch = authority, DurationTicks = ticks }; Should.Throw(() => DataProtoMapper.DecodeMetadataResult(wire)); } [Theory] [InlineData(DataItemStatus.Good, false)] [InlineData(DataItemStatus.Denied, false)] [InlineData(DataItemStatus.Denied, true)] [InlineData(DataItemStatus.Unavailable, true)] [InlineData(DataItemStatus.NotFound, false)] public void Only_a_successful_metadata_value_can_carry_a_lease(DataItemStatus status, bool hasValue) { var invalid = Good() with { Status = status, Value = hasValue ? Metadata() : null }; Should.Throw(() => DataProtoMapper.EncodeMetadataResult(invalid)); var wire = DataProtoMapper.EncodeMetadataResult(Good()); wire.Status = (int)status; if (!hasValue) wire.Value = null; Should.Throw(() => DataProtoMapper.DecodeMetadataResult(wire)); } [Theory] [InlineData("revision")] [InlineData("view-address")] [InlineData("view-mapping")] public void A_lease_cannot_make_malformed_metadata_decodable(string malformed) { var wire = DataProtoMapper.EncodeMetadataResult(Good()); switch (malformed) { case "revision": wire.Value.CatalogRevision = 0; break; case "view-address": wire.Value.View.Address.NamespaceUri = ""; break; case "view-mapping": wire.Value.View.MappingId = "mapping-without-revision"; break; } Should.Throw(() => DataProtoMapper.DecodeMetadataResult(wire)); } [Fact] public void Leased_metadata_still_obeys_receiver_item_byte_and_owned_decode_budgets() { var wire = DataProtoMapper.EncodeMetadataResponse([Good(), Good("second")]); Should.Throw(() => DataProtoMapper.DecodeMetadataResponse(wire, new DataDecodeLimits(1, 65536, 100))); Should.Throw(() => DataProtoMapper.DecodeMetadataResponse(wire, new DataDecodeLimits(2, 1, 100))); Should.Throw(() => DataProtoMapper.DecodeMetadataResponse(wire, new DataDecodeLimits(2, wire.CalculateSize() + 1, 100))); var large = DataProtoMapper.EncodeMetadataResult(Good() with { Value = Metadata() with { BrowseName = new string('x', 4096) } }); Should.Throw(() => DataProtoMapper.DecodeMetadataResult(large, new DataDecodeLimits(1, 1024, 100))); } }