Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 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 | |
| 33 | using Google.Protobuf.Reflection; |
| 34 | using Google.Protobuf.WellKnownTypes; |
| 35 | using NUnit.Framework; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 36 | using System; |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 37 | using System.IO; |
| 38 | using System.Linq; |
| 39 | using UnitTest.Issues.TestProtos; |
| 40 | using static Google.Protobuf.WireFormat; |
| 41 | using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 42 | using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions; |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 43 | using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types; |
Jon Skeet | 7282f29 | 2019-10-30 11:59:09 +0000 | [diff] [blame] | 44 | using Google.Protobuf.TestProtos; |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 45 | |
Sydney Acksman | 05712c6 | 2019-03-15 21:28:59 -0500 | [diff] [blame] | 46 | #pragma warning disable CS0618 |
| 47 | |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 48 | namespace Google.Protobuf.Test.Reflection |
| 49 | { |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 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 | |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 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 | |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 74 | [Test] |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 75 | public void ScalarOptions() |
| 76 | { |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 77 | var d = CustomOptionOtherValues.Descriptor; |
Sydney Acksman | 6b90ac1 | 2019-05-28 15:37:04 -0500 | [diff] [blame] | 78 | var options = d.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 79 | 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 Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | [Test] |
| 88 | public void MessageOptions() |
| 89 | { |
Sydney Acksman | 6b90ac1 | 2019-05-28 15:37:04 -0500 | [diff] [blame] | 90 | var d = VariousComplexOptions.Descriptor; |
| 91 | var options = d.CustomOptions; |
| 92 | AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1, d.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 93 | AssertOption(new ComplexOptionType2 |
Sydney Acksman | 9e89b6e | 2019-05-03 15:54:41 -0500 | [diff] [blame] | 94 | { |
| 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 Acksman | 6b90ac1 | 2019-05-28 15:37:04 -0500 | [diff] [blame] | 100 | options.TryGetMessage, ComplexOpt2, d.GetOption); |
| 101 | AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3, d.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | [Test] |
| 105 | public void OptionLocations() |
| 106 | { |
| 107 | var fileOptions = UnittestCustomOptionsProto3Reflection.Descriptor.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 108 | AssertOption(9876543210UL, fileOptions.TryGetUInt64, FileOpt1, UnittestCustomOptionsProto3Reflection.Descriptor.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 109 | |
| 110 | var messageOptions = TestMessageWithCustomOptions.Descriptor.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 111 | AssertOption(-56, messageOptions.TryGetInt32, MessageOpt1, TestMessageWithCustomOptions.Descriptor.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 112 | |
Sydney Acksman | 9e89b6e | 2019-05-03 15:54:41 -0500 | [diff] [blame] | 113 | var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"].CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 114 | AssertOption(8765432109UL, fieldOptions.TryGetFixed64, FieldOpt1, TestMessageWithCustomOptions.Descriptor.Fields["field1"].GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 115 | |
Jie Luo | ccb76ff | 2017-01-27 11:45:31 -0800 | [diff] [blame] | 116 | var oneofOptions = TestMessageWithCustomOptions.Descriptor.Oneofs[0].CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 117 | AssertOption(-99, oneofOptions.TryGetInt32, OneofOpt1, TestMessageWithCustomOptions.Descriptor.Oneofs[0].GetOption); |
Jie Luo | ccb76ff | 2017-01-27 11:45:31 -0800 | [diff] [blame] | 118 | |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 119 | var enumOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 120 | AssertOption(-789, enumOptions.TryGetSFixed32, EnumOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 121 | |
| 122 | var enumValueOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 123 | AssertOption(123, enumValueOptions.TryGetInt32, EnumValueOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 124 | |
| 125 | var service = UnittestCustomOptionsProto3Reflection.Descriptor.Services |
| 126 | .Single(s => s.Name == "TestServiceWithCustomOptions"); |
| 127 | var serviceOptions = service.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 128 | AssertOption(-9876543210, serviceOptions.TryGetSInt64, ServiceOpt1, service.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 129 | |
| 130 | var methodOptions = service.Methods[0].CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 131 | AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher<UnitTest.Issues.TestProtos.MethodOpt1>(methodOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, service.Methods[0].GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | [Test] |
| 135 | public void MinValues() |
| 136 | { |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 137 | var d = CustomOptionMinIntegerValues.Descriptor; |
Sydney Acksman | 6b90ac1 | 2019-05-28 15:37:04 -0500 | [diff] [blame] | 138 | var options = d.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 139 | 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 Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | [Test] |
| 153 | public void MaxValues() |
| 154 | { |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 155 | var d = CustomOptionMaxIntegerValues.Descriptor; |
Sydney Acksman | 6b90ac1 | 2019-05-28 15:37:04 -0500 | [diff] [blame] | 156 | var options = d.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 157 | 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 Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | [Test] |
| 171 | public void AggregateOptions() |
| 172 | { |
| 173 | // Just two examples |
| 174 | var messageOptions = AggregateMessage.Descriptor.CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 175 | AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageOptions.TryGetMessage, Msgopt, AggregateMessage.Descriptor.GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 176 | |
| 177 | var fieldOptions = AggregateMessage.Descriptor.Fields["fieldname"].CustomOptions; |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 178 | AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, Fieldopt, AggregateMessage.Descriptor.Fields["fieldname"].GetOption); |
Jon Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Jon Skeet | 7282f29 | 2019-10-30 11:59:09 +0000 | [diff] [blame] | 181 | [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 Acksman | 2b0b838 | 2019-11-24 14:22:50 -0600 | [diff] [blame] | 196 | [Test] |
| 197 | public void MultipleImportOfSameFileWithExtension() |
| 198 | { |
Sydney Acksman | 145033c | 2019-11-26 18:45:22 -0600 | [diff] [blame^] | 199 | var descriptor = ExtensionsIssue6936CReflection.Descriptor; |
Sydney Acksman | cd11d54 | 2019-11-24 14:56:35 -0600 | [diff] [blame] | 200 | var foo = Foo.Descriptor; |
| 201 | var bar = Bar.Descriptor; |
Sydney Acksman | 145033c | 2019-11-26 18:45:22 -0600 | [diff] [blame^] | 202 | AssertOption("foo", foo.CustomOptions.TryGetString, ExtensionsIssue6936AExtensions.Opt, foo.GetOption); |
| 203 | AssertOption("bar", bar.CustomOptions.TryGetString, ExtensionsIssue6936AExtensions.Opt, bar.GetOption); |
Sydney Acksman | 2b0b838 | 2019-11-24 14:22:50 -0600 | [diff] [blame] | 204 | } |
| 205 | |
Sydney Acksman | 011427c | 2019-05-03 21:34:48 -0500 | [diff] [blame] | 206 | 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 Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 207 | { |
Sydney Acksman | 6e7c43f | 2019-03-20 23:39:38 -0500 | [diff] [blame] | 208 | 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 Skeet | 047575f | 2017-01-16 11:23:32 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |