1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2017 Google Inc.  All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#endregion
32
33using Google.Protobuf.Reflection;
34using Google.Protobuf.WellKnownTypes;
35using NUnit.Framework;
36using System;
37using System.IO;
38using System.Linq;
39using UnitTest.Issues.TestProtos;
40using static Google.Protobuf.WireFormat;
41using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types;
42using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions;
43using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types;
44using Google.Protobuf.TestProtos;
45
46#pragma warning disable CS0618
47
48namespace Google.Protobuf.Test.Reflection
49{
50    /// <summary>
51    /// The majority of the testing here is done via parsed descriptors. That's simpler to
52    /// achieve (and more important) than constructing a CodedInputStream manually.
53    /// </summary>
54    public class CustomOptionsTest
55    {
56        delegate bool OptionFetcher<T>(int field, out T value);
57
58        OptionFetcher<E> EnumFetcher<E>(CustomOptions options)
59        {
60            return (int i, out E v) => {
61                if (options.TryGetInt32(i, out int value))
62                {
63                    v = (E)(object)value;
64                    return true;
65                }
66                else
67                {
68                    v = default(E);
69                    return false;
70                }
71            };
72        }
73
74        [Test]
75        public void BuiltinOptionsCanBeRetrieved()
76        {
77            // non-custom options (that are not extensions but regular fields) can only be accessed via descriptor.Options
78            var fileOptions = UnittestProto3Reflection.Descriptor.GetOptions();
79            Assert.AreEqual("Google.Protobuf.TestProtos", fileOptions.CsharpNamespace);
80        }
81
82        [Test]
83        public void OptionPresenceCanBeDetected()
84        {
85            // case 1: the descriptor has no options at all so the options message is not present
86            Assert.IsNull(TestAllTypes.Descriptor.GetOptions());
87
88            // case 2: the descriptor has some options, but not the one we're looking for
89            // HasExtension will be false and GetExtension returns extension's default value
90            Assert.IsFalse(UnittestProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1));
91            Assert.AreEqual(0, UnittestProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1));
92
93            // case 3: option is present
94            Assert.IsTrue(UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1));
95            Assert.AreEqual(9876543210UL, UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1));
96        }
97
98        [Test]
99        public void ScalarOptions()
100        {
101            var d = CustomOptionOtherValues.Descriptor;
102            var customOptions = d.CustomOptions;
103            AssertOption(-100, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
104            AssertOption(12.3456789f, customOptions.TryGetFloat, FloatOpt, d.GetOption, d.GetOptions().GetExtension);
105            AssertOption(1.234567890123456789d, customOptions.TryGetDouble, DoubleOpt, d.GetOption, d.GetOptions().GetExtension);
106            AssertOption("Hello, \"World\"", customOptions.TryGetString, StringOpt, d.GetOption, d.GetOptions().GetExtension);
107            AssertOption(ByteString.CopyFromUtf8("Hello\0World"), customOptions.TryGetBytes, BytesOpt, d.GetOption, d.GetOptions().GetExtension);
108            AssertOption(TestEnumType.TestOptionEnumType2, EnumFetcher<TestEnumType>(customOptions), EnumOpt, d.GetOption, d.GetOptions().GetExtension);
109        }
110
111        [Test]
112        public void MessageOptions()
113        {
114            var d = VariousComplexOptions.Descriptor;
115            var customOptions = d.CustomOptions;
116            AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, customOptions.TryGetMessage, ComplexOpt1, d.GetOption, d.GetOptions().GetExtension);
117            AssertOption(new ComplexOptionType2
118            {
119                Baz = 987,
120                Bar = new ComplexOptionType1 { Foo = 743 },
121                Fred = new ComplexOptionType4 { Waldo = 321 },
122                Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } }
123            },
124                customOptions.TryGetMessage, ComplexOpt2, d.GetOption, d.GetOptions().GetExtension);
125            AssertOption(new ComplexOptionType3 { Qux = 9 }, customOptions.TryGetMessage, ComplexOpt3, d.GetOption, d.GetOptions().GetExtension);
126        }
127
128        [Test]
129        public void OptionLocations()
130        {
131            var fileDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor;
132            AssertOption(9876543210UL, fileDescriptor.CustomOptions.TryGetUInt64, FileOpt1, fileDescriptor.GetOption, fileDescriptor.GetOptions().GetExtension);
133
134            var messageDescriptor = TestMessageWithCustomOptions.Descriptor;
135            AssertOption(-56, messageDescriptor.CustomOptions.TryGetInt32, MessageOpt1, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension);
136
137            var fieldDescriptor = TestMessageWithCustomOptions.Descriptor.Fields["field1"];
138            AssertOption(8765432109UL, fieldDescriptor.CustomOptions.TryGetFixed64, FieldOpt1, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension);
139
140            var oneofDescriptor = TestMessageWithCustomOptions.Descriptor.Oneofs[0];
141            AssertOption(-99, oneofDescriptor.CustomOptions.TryGetInt32, OneofOpt1, oneofDescriptor.GetOption, oneofDescriptor.GetOptions().GetExtension);
142
143            var enumDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0];
144            AssertOption(-789, enumDescriptor.CustomOptions.TryGetSFixed32, EnumOpt1, enumDescriptor.GetOption, enumDescriptor.GetOptions().GetExtension);
145
146            var enumValueDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2);
147            AssertOption(123, enumValueDescriptor.CustomOptions.TryGetInt32, EnumValueOpt1, enumValueDescriptor.GetOption, enumValueDescriptor.GetOptions().GetExtension);
148
149            var serviceDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor.Services
150                .Single(s => s.Name == "TestServiceWithCustomOptions");
151            AssertOption(-9876543210, serviceDescriptor.CustomOptions.TryGetSInt64, ServiceOpt1, serviceDescriptor.GetOption, serviceDescriptor.GetOptions().GetExtension);
152
153            var methodDescriptor = serviceDescriptor.Methods[0];
154            AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher<UnitTest.Issues.TestProtos.MethodOpt1>(methodDescriptor.CustomOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, methodDescriptor.GetOption, methodDescriptor.GetOptions().GetExtension);
155        }
156
157        [Test]
158        public void MinValues()
159        {
160            var d = CustomOptionMinIntegerValues.Descriptor;
161            var customOptions = d.CustomOptions;
162            AssertOption(false, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension);
163            AssertOption(int.MinValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
164            AssertOption(long.MinValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension);
165            AssertOption(uint.MinValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension);
166            AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension);
167            AssertOption(int.MinValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension);
168            AssertOption(long.MinValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension);
169            AssertOption(uint.MinValue, customOptions.TryGetUInt32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension);
170            AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension);
171            AssertOption(int.MinValue, customOptions.TryGetInt32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension);
172            AssertOption(long.MinValue, customOptions.TryGetInt64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension);
173        }
174
175        [Test]
176        public void MaxValues()
177        {
178            var d = CustomOptionMaxIntegerValues.Descriptor;
179            var customOptions = d.CustomOptions;
180            AssertOption(true, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension);
181            AssertOption(int.MaxValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension);
182            AssertOption(long.MaxValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension);
183            AssertOption(uint.MaxValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension);
184            AssertOption(ulong.MaxValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension);
185            AssertOption(int.MaxValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension);
186            AssertOption(long.MaxValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension);
187            AssertOption(uint.MaxValue, customOptions.TryGetFixed32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension);
188            AssertOption(ulong.MaxValue, customOptions.TryGetFixed64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension);
189            AssertOption(int.MaxValue, customOptions.TryGetSFixed32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension);
190            AssertOption(long.MaxValue, customOptions.TryGetSFixed64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension);
191        }
192
193        [Test]
194        public void AggregateOptions()
195        {
196            // Just two examples
197            var messageDescriptor = AggregateMessage.Descriptor;
198            AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageDescriptor.CustomOptions.TryGetMessage, Msgopt, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension);
199
200            var fieldDescriptor = messageDescriptor.Fields["fieldname"];
201            AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldDescriptor.CustomOptions.TryGetMessage, Fieldopt, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension);
202        }
203
204        [Test]
205        public void NoOptions()
206        {
207            var fileDescriptor = UnittestProto3Reflection.Descriptor;
208            var messageDescriptor = TestAllTypes.Descriptor;
209            Assert.NotNull(fileDescriptor.CustomOptions);
210            Assert.NotNull(messageDescriptor.CustomOptions);
211            Assert.NotNull(messageDescriptor.Fields[1].CustomOptions);
212            Assert.NotNull(fileDescriptor.Services[0].CustomOptions);
213            Assert.NotNull(fileDescriptor.Services[0].Methods[0].CustomOptions);
214            Assert.NotNull(fileDescriptor.EnumTypes[0].CustomOptions);
215            Assert.NotNull(fileDescriptor.EnumTypes[0].Values[0].CustomOptions);
216            Assert.NotNull(TestAllTypes.Descriptor.Oneofs[0].CustomOptions);
217        }
218
219        [Test]
220        public void MultipleImportOfSameFileWithExtension()
221        {
222            var descriptor = UnittestIssue6936CReflection.Descriptor;
223            var foo = Foo.Descriptor;
224            var bar = Bar.Descriptor;
225            AssertOption("foo", foo.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, foo.GetOption, foo.GetOptions().GetExtension);
226            AssertOption("bar", bar.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, bar.GetOption, bar.GetOptions().GetExtension);
227        }
228
229        [Test]
230        public void SelfReferentialOptions()
231        {
232            // Custom field option used in definition of the custom option's message.
233            var fooField = UnitTest.Issues.TestProtos.SelfreferentialOptions.FooOptions.Descriptor.FindFieldByName("foo");
234            var fooFieldFooExtensionValue = fooField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooOptions);
235            Assert.AreEqual(1234, fooFieldFooExtensionValue.Foo);
236
237            // Custom field option used on the definition of that field option.
238            var fileDescriptor = UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsReflection.Descriptor;
239            var barOptionsField = fileDescriptor.Extensions.UnorderedExtensions.Single(field => field.Name == "bar_options");
240            var barExtensionValue = barOptionsField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.BarOptions);
241            Assert.AreEqual(1234, barExtensionValue);
242
243            // Custom field option used in definition of the extension message.
244            var intOptField = UnitTest.Issues.TestProtos.SelfreferentialOptions.FooOptions.Descriptor.FindFieldByName("int_opt");
245            var intOptFieldFooExtensionValue = intOptField.GetOptions().GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooOptions);
246            Assert.AreEqual(1, intOptFieldFooExtensionValue.IntOpt);
247            Assert.AreEqual(2, intOptFieldFooExtensionValue.GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooIntOpt));
248            Assert.AreEqual(3, intOptFieldFooExtensionValue.GetExtension(UnitTest.Issues.TestProtos.SelfreferentialOptions.UnittestSelfreferentialOptionsExtensions.FooFooOpt).IntOpt);
249        }
250
251        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>
252        {
253            Assert.IsTrue(customOptionFetcher(extension.FieldNumber, out T customOptionsValue));
254            Assert.AreEqual(expected, customOptionsValue);
255
256            T getOptionValue = getOptionFetcher(extension);
257            Assert.AreEqual(expected, getOptionValue);
258
259            T extensionValue = extensionFetcher(extension);
260            Assert.AreEqual(expected, extensionValue);
261        }
262    }
263}
264