blob: bf5f5ad9c303ce0dcafbbec2b0bdda629fed4f88 [file] [log] [blame]
Jon Skeet047575f2017-01-16 11:23:32 +00001#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;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -050036using System;
Jon Skeet047575f2017-01-16 11:23:32 +000037using System.IO;
38using System.Linq;
39using UnitTest.Issues.TestProtos;
40using static Google.Protobuf.WireFormat;
41using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -050042using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions;
Jon Skeet047575f2017-01-16 11:23:32 +000043using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types;
Jon Skeet7282f292019-10-30 11:59:09 +000044using Google.Protobuf.TestProtos;
Jon Skeet047575f2017-01-16 11:23:32 +000045
Sydney Acksman05712c62019-03-15 21:28:59 -050046#pragma warning disable CS0618
47
Jon Skeet047575f2017-01-16 11:23:32 +000048namespace Google.Protobuf.Test.Reflection
49{
Jon Skeet047575f2017-01-16 11:23:32 +000050 /// <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
Sydney Acksman6e7c43f2019-03-20 23:39:38 -050058 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
Jon Skeet047575f2017-01-16 11:23:32 +000074 [Test]
Jon Skeet047575f2017-01-16 11:23:32 +000075 public void ScalarOptions()
76 {
Sydney Acksman6e7c43f2019-03-20 23:39:38 -050077 var d = CustomOptionOtherValues.Descriptor;
Sydney Acksman6b90ac12019-05-28 15:37:04 -050078 var options = d.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -050079 AssertOption(-100, options.TryGetInt32, Int32Opt, d.GetOption);
80 AssertOption(12.3456789f, options.TryGetFloat, FloatOpt, d.GetOption);
81 AssertOption(1.234567890123456789d, options.TryGetDouble, DoubleOpt, d.GetOption);
82 AssertOption("Hello, \"World\"", options.TryGetString, StringOpt, d.GetOption);
83 AssertOption(ByteString.CopyFromUtf8("Hello\0World"), options.TryGetBytes, BytesOpt, d.GetOption);
84 AssertOption(TestEnumType.TestOptionEnumType2, EnumFetcher<TestEnumType>(options), EnumOpt, d.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +000085 }
86
87 [Test]
88 public void MessageOptions()
89 {
Sydney Acksman6b90ac12019-05-28 15:37:04 -050090 var d = VariousComplexOptions.Descriptor;
91 var options = d.CustomOptions;
92 AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1, d.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +000093 AssertOption(new ComplexOptionType2
Sydney Acksman9e89b6e2019-05-03 15:54:41 -050094 {
95 Baz = 987,
96 Bar = new ComplexOptionType1 { Foo = 743 },
97 Fred = new ComplexOptionType4 { Waldo = 321 },
98 Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } }
99 },
Sydney Acksman6b90ac12019-05-28 15:37:04 -0500100 options.TryGetMessage, ComplexOpt2, d.GetOption);
101 AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3, d.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000102 }
103
104 [Test]
105 public void OptionLocations()
106 {
107 var fileOptions = UnittestCustomOptionsProto3Reflection.Descriptor.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500108 AssertOption(9876543210UL, fileOptions.TryGetUInt64, FileOpt1, UnittestCustomOptionsProto3Reflection.Descriptor.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000109
110 var messageOptions = TestMessageWithCustomOptions.Descriptor.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500111 AssertOption(-56, messageOptions.TryGetInt32, MessageOpt1, TestMessageWithCustomOptions.Descriptor.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000112
Sydney Acksman9e89b6e2019-05-03 15:54:41 -0500113 var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"].CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500114 AssertOption(8765432109UL, fieldOptions.TryGetFixed64, FieldOpt1, TestMessageWithCustomOptions.Descriptor.Fields["field1"].GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000115
Jie Luoccb76ff2017-01-27 11:45:31 -0800116 var oneofOptions = TestMessageWithCustomOptions.Descriptor.Oneofs[0].CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500117 AssertOption(-99, oneofOptions.TryGetInt32, OneofOpt1, TestMessageWithCustomOptions.Descriptor.Oneofs[0].GetOption);
Jie Luoccb76ff2017-01-27 11:45:31 -0800118
Jon Skeet047575f2017-01-16 11:23:32 +0000119 var enumOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500120 AssertOption(-789, enumOptions.TryGetSFixed32, EnumOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000121
122 var enumValueOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500123 AssertOption(123, enumValueOptions.TryGetInt32, EnumValueOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000124
125 var service = UnittestCustomOptionsProto3Reflection.Descriptor.Services
126 .Single(s => s.Name == "TestServiceWithCustomOptions");
127 var serviceOptions = service.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500128 AssertOption(-9876543210, serviceOptions.TryGetSInt64, ServiceOpt1, service.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000129
130 var methodOptions = service.Methods[0].CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500131 AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher<UnitTest.Issues.TestProtos.MethodOpt1>(methodOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, service.Methods[0].GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000132 }
133
134 [Test]
135 public void MinValues()
136 {
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500137 var d = CustomOptionMinIntegerValues.Descriptor;
Sydney Acksman6b90ac12019-05-28 15:37:04 -0500138 var options = d.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500139 AssertOption(false, options.TryGetBool, BoolOpt, d.GetOption);
140 AssertOption(int.MinValue, options.TryGetInt32, Int32Opt, d.GetOption);
141 AssertOption(long.MinValue, options.TryGetInt64, Int64Opt, d.GetOption);
142 AssertOption(uint.MinValue, options.TryGetUInt32, Uint32Opt, d.GetOption);
143 AssertOption(ulong.MinValue, options.TryGetUInt64, Uint64Opt, d.GetOption);
144 AssertOption(int.MinValue, options.TryGetSInt32, Sint32Opt, d.GetOption);
145 AssertOption(long.MinValue, options.TryGetSInt64, Sint64Opt, d.GetOption);
146 AssertOption(uint.MinValue, options.TryGetUInt32, Fixed32Opt, d.GetOption);
147 AssertOption(ulong.MinValue, options.TryGetUInt64, Fixed64Opt, d.GetOption);
148 AssertOption(int.MinValue, options.TryGetInt32, Sfixed32Opt, d.GetOption);
149 AssertOption(long.MinValue, options.TryGetInt64, Sfixed64Opt, d.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000150 }
151
152 [Test]
153 public void MaxValues()
154 {
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500155 var d = CustomOptionMaxIntegerValues.Descriptor;
Sydney Acksman6b90ac12019-05-28 15:37:04 -0500156 var options = d.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500157 AssertOption(true, options.TryGetBool, BoolOpt, d.GetOption);
158 AssertOption(int.MaxValue, options.TryGetInt32, Int32Opt, d.GetOption);
159 AssertOption(long.MaxValue, options.TryGetInt64, Int64Opt, d.GetOption);
160 AssertOption(uint.MaxValue, options.TryGetUInt32, Uint32Opt, d.GetOption);
161 AssertOption(ulong.MaxValue, options.TryGetUInt64, Uint64Opt, d.GetOption);
162 AssertOption(int.MaxValue, options.TryGetSInt32, Sint32Opt, d.GetOption);
163 AssertOption(long.MaxValue, options.TryGetSInt64, Sint64Opt, d.GetOption);
164 AssertOption(uint.MaxValue, options.TryGetFixed32, Fixed32Opt, d.GetOption);
165 AssertOption(ulong.MaxValue, options.TryGetFixed64, Fixed64Opt, d.GetOption);
166 AssertOption(int.MaxValue, options.TryGetSFixed32, Sfixed32Opt, d.GetOption);
167 AssertOption(long.MaxValue, options.TryGetSFixed64, Sfixed64Opt, d.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000168 }
169
170 [Test]
171 public void AggregateOptions()
172 {
173 // Just two examples
174 var messageOptions = AggregateMessage.Descriptor.CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500175 AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageOptions.TryGetMessage, Msgopt, AggregateMessage.Descriptor.GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000176
177 var fieldOptions = AggregateMessage.Descriptor.Fields["fieldname"].CustomOptions;
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500178 AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, Fieldopt, AggregateMessage.Descriptor.Fields["fieldname"].GetOption);
Jon Skeet047575f2017-01-16 11:23:32 +0000179 }
180
Jon Skeet7282f292019-10-30 11:59:09 +0000181 [Test]
182 public void NoOptions()
183 {
184 var fileDescriptor = UnittestProto3Reflection.Descriptor;
185 var messageDescriptor = TestAllTypes.Descriptor;
186 Assert.NotNull(fileDescriptor.CustomOptions);
187 Assert.NotNull(messageDescriptor.CustomOptions);
188 Assert.NotNull(messageDescriptor.Fields[1].CustomOptions);
189 Assert.NotNull(fileDescriptor.Services[0].CustomOptions);
190 Assert.NotNull(fileDescriptor.Services[0].Methods[0].CustomOptions);
191 Assert.NotNull(fileDescriptor.EnumTypes[0].CustomOptions);
192 Assert.NotNull(fileDescriptor.EnumTypes[0].Values[0].CustomOptions);
193 Assert.NotNull(TestAllTypes.Descriptor.Oneofs[0].CustomOptions);
194 }
195
Sydney Acksman2b0b8382019-11-24 14:22:50 -0600196 [Test]
197 public void MultipleImportOfSameFileWithExtension()
198 {
Sydney Acksman145033c2019-11-26 18:45:22 -0600199 var descriptor = ExtensionsIssue6936CReflection.Descriptor;
Sydney Acksmancd11d542019-11-24 14:56:35 -0600200 var foo = Foo.Descriptor;
201 var bar = Bar.Descriptor;
Sydney Acksman145033c2019-11-26 18:45:22 -0600202 AssertOption("foo", foo.CustomOptions.TryGetString, ExtensionsIssue6936AExtensions.Opt, foo.GetOption);
203 AssertOption("bar", bar.CustomOptions.TryGetString, ExtensionsIssue6936AExtensions.Opt, bar.GetOption);
Sydney Acksman2b0b8382019-11-24 14:22:50 -0600204 }
205
Sydney Acksman011427c2019-05-03 21:34:48 -0500206 private void AssertOption<T, D>(T expected, OptionFetcher<T> fetcher, Extension<D, T> extension, Func<Extension<D, T>, T> descriptorOptionFetcher) where D : IExtendableMessage<D>
Jon Skeet047575f2017-01-16 11:23:32 +0000207 {
Sydney Acksman6e7c43f2019-03-20 23:39:38 -0500208 T customOptionsValue;
209 T extensionValue = descriptorOptionFetcher(extension);
210 Assert.IsTrue(fetcher(extension.FieldNumber, out customOptionsValue));
211 Assert.AreEqual(expected, customOptionsValue);
212 Assert.AreEqual(expected, extensionValue);
Jon Skeet047575f2017-01-16 11:23:32 +0000213 }
214 }
215}