• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2017 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using Google.Protobuf.Reflection;
11 using NUnit.Framework;
12 using System;
13 using System.Linq;
14 using UnitTest.Issues.TestProtos;
15 using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types;
16 using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions;
17 using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types;
18 using Google.Protobuf.TestProtos;
19 
20 #pragma warning disable CS0618
21 
22 namespace Google.Protobuf.Test.Reflection
23 {
24     /// <summary>
25     /// The majority of the testing here is done via parsed descriptors. That's simpler to
26     /// achieve (and more important) than constructing a CodedInputStream manually.
27     /// </summary>
28     public class CustomOptionsTest
29     {
OptionFetcher(int field, out T value)30         delegate bool OptionFetcher<T>(int field, out T value);
31 
EnumFetcher(CustomOptions options)32         OptionFetcher<E> EnumFetcher<E>(CustomOptions options)
33         {
34             return (int i, out E v) => {
35                 if (options.TryGetInt32(i, out int value))
36                 {
37                     v = (E)(object)value;
38                     return true;
39                 }
40                 else
41                 {
42                     v = default;
43                     return false;
44                 }
45             };
46         }
47 
48         [Test]
BuiltinOptionsCanBeRetrieved()49         public void BuiltinOptionsCanBeRetrieved()
50         {
51             // non-custom options (that are not extensions but regular fields) can only be accessed via descriptor.Options
52             var fileOptions = UnittestProto3Reflection.Descriptor.GetOptions();
53             Assert.AreEqual("Google.Protobuf.TestProtos", fileOptions.CsharpNamespace);
54         }
55 
56         [Test]
OptionPresenceCanBeDetected()57         public void OptionPresenceCanBeDetected()
58         {
59             // case 1: the descriptor has no options at all so the options message is not present
60             Assert.IsNull(TestAllTypes.Descriptor.GetOptions());
61 
62             // case 2: the descriptor has some options, but not the one we're looking for
63             // HasExtension will be false and GetExtension returns extension's default value
64             Assert.IsFalse(UnittestProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1));
65             Assert.AreEqual(0, UnittestProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1));
66 
67             // case 3: option is present
68             Assert.IsTrue(UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1));
69             Assert.AreEqual(9876543210UL, UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1));
70         }
71 
72         [Test]
ScalarOptions()73         public void ScalarOptions()
74         {
75             var d = CustomOptionOtherValues.Descriptor;
76             var customOptions = d.CustomOptions;
77             AssertOption(-100, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
78             AssertOption(12.3456789f, customOptions.TryGetFloat, FloatOpt, d.GetOption, d.GetOptions().GetExtension);
79             AssertOption(1.234567890123456789d, customOptions.TryGetDouble, DoubleOpt, d.GetOption, d.GetOptions().GetExtension);
80             AssertOption("Hello, \"World\"", customOptions.TryGetString, StringOpt, d.GetOption, d.GetOptions().GetExtension);
81             AssertOption(ByteString.CopyFromUtf8("Hello\0World"), customOptions.TryGetBytes, BytesOpt, d.GetOption, d.GetOptions().GetExtension);
82             AssertOption(TestEnumType.TestOptionEnumType2, EnumFetcher<TestEnumType>(customOptions), EnumOpt, d.GetOption, d.GetOptions().GetExtension);
83         }
84 
85         [Test]
MessageOptions()86         public void MessageOptions()
87         {
88             var d = VariousComplexOptions.Descriptor;
89             var customOptions = d.CustomOptions;
90             AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, customOptions.TryGetMessage, ComplexOpt1, d.GetOption, d.GetOptions().GetExtension);
91             AssertOption(new ComplexOptionType2
92             {
93                 Baz = 987,
94                 Bar = new ComplexOptionType1 { Foo = 743 },
95                 Fred = new ComplexOptionType4 { Waldo = 321 },
96                 Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } }
97             },
98                 customOptions.TryGetMessage, ComplexOpt2, d.GetOption, d.GetOptions().GetExtension);
99             AssertOption(new ComplexOptionType3 { Qux = 9 }, customOptions.TryGetMessage, ComplexOpt3, d.GetOption, d.GetOptions().GetExtension);
100         }
101 
102         [Test]
OptionLocations()103         public void OptionLocations()
104         {
105             var fileDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor;
106             AssertOption(9876543210UL, fileDescriptor.CustomOptions.TryGetUInt64, FileOpt1, fileDescriptor.GetOption, fileDescriptor.GetOptions().GetExtension);
107 
108             var messageDescriptor = TestMessageWithCustomOptions.Descriptor;
109             AssertOption(-56, messageDescriptor.CustomOptions.TryGetInt32, MessageOpt1, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension);
110 
111             var fieldDescriptor = TestMessageWithCustomOptions.Descriptor.Fields["field1"];
112             AssertOption(8765432109UL, fieldDescriptor.CustomOptions.TryGetFixed64, FieldOpt1, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension);
113 
114             var oneofDescriptor = TestMessageWithCustomOptions.Descriptor.Oneofs[0];
115             AssertOption(-99, oneofDescriptor.CustomOptions.TryGetInt32, OneofOpt1, oneofDescriptor.GetOption, oneofDescriptor.GetOptions().GetExtension);
116 
117             var enumDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0];
118             AssertOption(-789, enumDescriptor.CustomOptions.TryGetSFixed32, EnumOpt1, enumDescriptor.GetOption, enumDescriptor.GetOptions().GetExtension);
119 
120             var enumValueDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2);
121             AssertOption(123, enumValueDescriptor.CustomOptions.TryGetInt32, EnumValueOpt1, enumValueDescriptor.GetOption, enumValueDescriptor.GetOptions().GetExtension);
122 
123             var serviceDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor.Services
124                 .Single(s => s.Name == "TestServiceWithCustomOptions");
125             AssertOption(-9876543210, serviceDescriptor.CustomOptions.TryGetSInt64, ServiceOpt1, serviceDescriptor.GetOption, serviceDescriptor.GetOptions().GetExtension);
126 
127             var methodDescriptor = serviceDescriptor.Methods[0];
128             AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher<UnitTest.Issues.TestProtos.MethodOpt1>(methodDescriptor.CustomOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, methodDescriptor.GetOption, methodDescriptor.GetOptions().GetExtension);
129         }
130 
131         [Test]
MinValues()132         public void MinValues()
133         {
134             var d = CustomOptionMinIntegerValues.Descriptor;
135             var customOptions = d.CustomOptions;
136             AssertOption(false, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension);
137             AssertOption(int.MinValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
138             AssertOption(long.MinValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension);
139             AssertOption(uint.MinValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension);
140             AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension);
141             AssertOption(int.MinValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension);
142             AssertOption(long.MinValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension);
143             AssertOption(uint.MinValue, customOptions.TryGetUInt32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension);
144             AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension);
145             AssertOption(int.MinValue, customOptions.TryGetInt32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension);
146             AssertOption(long.MinValue, customOptions.TryGetInt64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension);
147         }
148 
149         [Test]
MaxValues()150         public void MaxValues()
151         {
152             var d = CustomOptionMaxIntegerValues.Descriptor;
153             var customOptions = d.CustomOptions;
154             AssertOption(true, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension);
155             AssertOption(int.MaxValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
156             AssertOption(long.MaxValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension);
157             AssertOption(uint.MaxValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension);
158             AssertOption(ulong.MaxValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension);
159             AssertOption(int.MaxValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension);
160             AssertOption(long.MaxValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension);
161             AssertOption(uint.MaxValue, customOptions.TryGetFixed32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension);
162             AssertOption(ulong.MaxValue, customOptions.TryGetFixed64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension);
163             AssertOption(int.MaxValue, customOptions.TryGetSFixed32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension);
164             AssertOption(long.MaxValue, customOptions.TryGetSFixed64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension);
165         }
166 
167         [Test]
AggregateOptions()168         public void AggregateOptions()
169         {
170             // Just two examples
171             var messageDescriptor = AggregateMessage.Descriptor;
172             AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageDescriptor.CustomOptions.TryGetMessage, Msgopt, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension);
173 
174             var fieldDescriptor = messageDescriptor.Fields["fieldname"];
175             AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldDescriptor.CustomOptions.TryGetMessage, Fieldopt, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension);
176         }
177 
178         [Test]
NoOptions()179         public void NoOptions()
180         {
181             var fileDescriptor = UnittestProto3Reflection.Descriptor;
182             var messageDescriptor = TestAllTypes.Descriptor;
183             Assert.NotNull(fileDescriptor.CustomOptions);
184             Assert.NotNull(messageDescriptor.CustomOptions);
185             Assert.NotNull(messageDescriptor.Fields[1].CustomOptions);
186             Assert.NotNull(fileDescriptor.Services[0].CustomOptions);
187             Assert.NotNull(fileDescriptor.Services[0].Methods[0].CustomOptions);
188             Assert.NotNull(fileDescriptor.EnumTypes[0].CustomOptions);
189             Assert.NotNull(fileDescriptor.EnumTypes[0].Values[0].CustomOptions);
190             Assert.NotNull(TestAllTypes.Descriptor.Oneofs[0].CustomOptions);
191         }
192 
193         [Test]
MultipleImportOfSameFileWithExtension()194         public void MultipleImportOfSameFileWithExtension()
195         {
196             var descriptor = UnittestIssue6936CReflection.Descriptor;
197             var foo = Foo.Descriptor;
198             var bar = Bar.Descriptor;
199             AssertOption("foo", foo.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, foo.GetOption, foo.GetOptions().GetExtension);
200             AssertOption("bar", bar.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, bar.GetOption, bar.GetOptions().GetExtension);
201         }
202 
203         [Test]
SelfReferentialOptions()204         public void SelfReferentialOptions()
205         {
206             // Custom field option used in definition of the custom option's message.
207             var fooField = UnitTest.Issues.TestProtos.SelfreferentialOptions.FooOptions.Descriptor.FindFieldByName("foo");
208             var fooFieldFooExtensionValue = fooField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooOptions);
209             Assert.AreEqual(1234, fooFieldFooExtensionValue.Foo);
210 
211             // Custom field option used on the definition of that field option.
212             var fileDescriptor = UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsReflection.Descriptor;
213             var barOptionsField = fileDescriptor.Extensions.UnorderedExtensions.Single(field => field.Name == "bar_options");
214             var barExtensionValue = barOptionsField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.BarOptions);
215             Assert.AreEqual(1234, barExtensionValue);
216 
217             // Custom field option used in definition of the extension message.
218             var intOptField = UnitTest.Issues.TestProtos.SelfreferentialOptions.FooOptions.Descriptor.FindFieldByName("int_opt");
219             var intOptFieldFooExtensionValue = intOptField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooOptions);
220             Assert.AreEqual(1, intOptFieldFooExtensionValue.IntOpt);
221             Assert.AreEqual(2, intOptFieldFooExtensionValue.GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooIntOpt));
222             Assert.AreEqual(3, intOptFieldFooExtensionValue.GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooFooOpt).IntOpt);
223         }
224 
225         private void AssertOption<T, D>(T expected, OptionFetcher<T> customOptionFetcher, Extension<D, T> extension, Func<Extension<D, T>, T> getOptionFetcher, Func<Extension<D, T>, T> extensionFetcher) where D : IExtendableMessage<D>
226         {
227             Assert.IsTrue(customOptionFetcher(extension.FieldNumber, out T customOptionsValue));
228             Assert.AreEqual(expected, customOptionsValue);
229 
230             T getOptionValue = getOptionFetcher(extension);
231             Assert.AreEqual(expected, getOptionValue);
232 
233             T extensionValue = extensionFetcher(extension);
234             Assert.AreEqual(expected, extensionValue);
235         }
236     }
237 }
238