using Google.Protobuf; using Google.Protobuf.Reflection; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.Protos; using ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1; namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.DataConnections; /// /// X0 contract reservation. Each reserved number must stay unused until its phase adds it, and then carry exactly /// the planned field. These run against the generated descriptors, so a proto edit that takes a number for /// something else fails here. /// [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "X0")] public sealed class GrpcDataProtoReservationTests { public static TheoryData Reservations => new() { { nameof(CapabilitiesResponse), DataConnectionProtoContract.CapabilitiesHistoryField, "history", "HistoryCapabilities", "" }, { nameof(CapabilitiesResponse), DataConnectionProtoContract.CapabilitiesConditionsField, "conditions", "ConditionCapabilities", "" }, { nameof(Metadata), DataConnectionProtoContract.MetadataHistoryField, "history", "HistoryInfo", "" }, { nameof(SubscriptionItem), DataConnectionProtoContract.SubscriptionItemConditionScopeField, "condition_scope", "ConditionScope", "" }, { nameof(SubscriptionFrame), DataConnectionProtoContract.SubscriptionFrameConditionStatesArm, "condition_states", "ConditionStates", "frame" }, }; [Theory] [MemberData(nameof(Reservations))] public void Reserved_number_is_unused_or_carries_its_planned_field(string message, int number, string expected, string type, string oneof) { var descriptor = DataConnectionReflection.Descriptor.MessageTypes.Single(m => m.Name == message); var field = descriptor.FindFieldByNumber(number); if (field is not null) { field.Name.ShouldBe(expected, $"{message} field {number} is reserved for '{expected}'"); field.FieldType.ShouldBe(FieldType.Message); field.IsRepeated.ShouldBeFalse(); field.MessageType.Name.ShouldBe(type); if (oneof.Length == 0) field.RealContainingOneof.ShouldBeNull(); else field.RealContainingOneof.ShouldNotBeNull().Name.ShouldBe(oneof); } // The planned name is not squatted on another number either. var named = descriptor.FindFieldByName(expected); if (named is not null) named.FieldNumber.ShouldBe(number, $"{message}.{expected} must use its reserved number"); DataConnectionProtoContract.Reserved(descriptor, number, expected, type, oneof.Length == 0 ? null : oneof).ShouldBeTrue(); DataConnectionProtoContract.Reserved(descriptor, number, expected + "_other", type, oneof.Length == 0 ? null : oneof).ShouldBe(field is null); } [Fact] public void A_reserved_number_taken_with_the_wrong_type_or_shape_fails_the_check() { // Synthetic descriptors: the same name on the reserved number but the wrong type must not pass. var file = new FileDescriptorProto { Name = "x0_reservation_probe.proto", Package = "probe", Syntax = "proto3" }; file.MessageType.Add(new DescriptorProto { Name = "HistoryCapabilities" }); var wrongType = new DescriptorProto { Name = "WrongType" }; wrongType.Field.Add(new FieldDescriptorProto { Name = "history", Number = 16, Type = FieldDescriptorProto.Types.Type.String, Label = FieldDescriptorProto.Types.Label.Optional, JsonName = "history" }); var repeated = new DescriptorProto { Name = "Repeated" }; repeated.Field.Add(new FieldDescriptorProto { Name = "history", Number = 16, Type = FieldDescriptorProto.Types.Type.Message, TypeName = ".probe.HistoryCapabilities", Label = FieldDescriptorProto.Types.Label.Repeated, JsonName = "history" }); var right = new DescriptorProto { Name = "Right" }; right.Field.Add(new FieldDescriptorProto { Name = "history", Number = 16, Type = FieldDescriptorProto.Types.Type.Message, TypeName = ".probe.HistoryCapabilities", Label = FieldDescriptorProto.Types.Label.Optional, JsonName = "history" }); file.MessageType.Add(wrongType); file.MessageType.Add(repeated); file.MessageType.Add(right); var built = FileDescriptor.BuildFromByteStrings([file.ToByteString()]).Single(); DataConnectionProtoContract.Reserved(built.FindTypeByName("WrongType"), 16, "history", "HistoryCapabilities").ShouldBeFalse(); DataConnectionProtoContract.Reserved(built.FindTypeByName("Repeated"), 16, "history", "HistoryCapabilities").ShouldBeFalse(); DataConnectionProtoContract.Reserved(built.FindTypeByName("Right"), 16, "history", "HistoryCapabilities").ShouldBeTrue(); DataConnectionProtoContract.Reserved(built.FindTypeByName("Right"), 16, "history", "HistoryCapabilities", "frame").ShouldBeFalse(); } [Fact] public void Reserved_numbers_are_pinned() { DataConnectionProtoContract.CapabilitiesHistoryField.ShouldBe(16); DataConnectionProtoContract.CapabilitiesConditionsField.ShouldBe(17); DataConnectionProtoContract.MetadataHistoryField.ShouldBe(14); DataConnectionProtoContract.SubscriptionItemConditionScopeField.ShouldBe(4); DataConnectionProtoContract.SubscriptionFrameConditionStatesArm.ShouldBe(9); // The arm is a member of the frame oneof once it exists (a null oneof fails). var arm = SubscriptionFrame.Descriptor.FindFieldByNumber(DataConnectionProtoContract.SubscriptionFrameConditionStatesArm); if (arm is not null) arm.RealContainingOneof.ShouldNotBeNull().Name.ShouldBe("frame"); } // The X0 tripwire ("no field yet") was REPLACED by H2 for 16/14 and by A2a for 17/4/9: those numbers are pinned with // their real types in GrpcDataHistoryCodecTests and GrpcDataConditionCodecTests. The Reservations theory above still // checks name/type/oneof for every reserved number. [Fact] public void Header_names_are_pinned_and_contract_validation_includes_the_reservations() { DataConnectionProtoContract.FeaturesHeader.ShouldBe("x-otopcua-data-features"); DataConnectionProtoContract.CompactCodecHeader.ShouldBe("x-data-compact-codec"); DataConnectionProtoContract.MaxFeaturesHeaderLength.ShouldBe(256); Should.NotThrow(DataConnectionProtoContract.Validate); } }