CustomOptionsTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. using Google.Protobuf.Reflection;
  33. using Google.Protobuf.WellKnownTypes;
  34. using NUnit.Framework;
  35. using System.IO;
  36. using System.Linq;
  37. using UnitTest.Issues.TestProtos;
  38. using static Google.Protobuf.WireFormat;
  39. using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types;
  40. using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types;
  41. using static Google.Protobuf.Test.Reflection.CustomOptionNumber;
  42. #pragma warning disable CS0618
  43. namespace Google.Protobuf.Test.Reflection
  44. {
  45. // Internal enum to allow us to use "using static" for convenience.
  46. // These are the options defined in unittest_custom_options_proto3.proto
  47. internal enum CustomOptionNumber
  48. {
  49. FileOpt1 = 7736974,
  50. MessageOpt1 = 7739036,
  51. FieldOpt1 = 7740936,
  52. OneofOpt1 = 7740111,
  53. EnumOpt1 = 7753576,
  54. EnumValueOpt1 = 1560678,
  55. ServiceOpt1 = 7887650,
  56. MethodOpt1 = 7890860,
  57. // All message options...
  58. BoolOpt = 7706090,
  59. Int32Opt = 7705709,
  60. Int64Opt = 7705542,
  61. UInt32Opt = 7704880,
  62. UInt64Opt = 7702367,
  63. SInt32Opt = 7701568,
  64. SInt64Opt = 7700863,
  65. Fixed32Opt = 7700307,
  66. Fixed64Opt = 7700194,
  67. SFixed32Opt = 7698645,
  68. SFixed64Opt = 7685475,
  69. FloatOpt = 7675390,
  70. DoubleOpt = 7673293,
  71. StringOpt = 7673285,
  72. BytesOpt = 7673238,
  73. EnumOpt = 7673233,
  74. MessageTypeOpt = 7665967,
  75. // Miscellaneous
  76. ComplexOpt4 = 7633546,
  77. ComplexOpt1 = 7646756,
  78. ComplexOpt2 = 7636949,
  79. ComplexOpt3 = 7636463,
  80. // Aggregates
  81. AggregateFileOpt = 15478479,
  82. AggregateMsgOpt = 15480088,
  83. AggregateFieldOpt = 15481374,
  84. AggregateEnumOpt = 15483218,
  85. AggregateEnumValueOpt = 15486921,
  86. AggregateServiceOpt = 15497145,
  87. AggregateMethodOpt = 15512713,
  88. }
  89. /// <summary>
  90. /// The majority of the testing here is done via parsed descriptors. That's simpler to
  91. /// achieve (and more important) than constructing a CodedInputStream manually.
  92. /// </summary>
  93. public class CustomOptionsTest
  94. {
  95. delegate bool OptionFetcher<T>(int field, out T value);
  96. [Test]
  97. public void ScalarOptions()
  98. {
  99. var options = CustomOptionOtherValues.Descriptor.CustomOptions;
  100. AssertOption(-100, options.TryGetInt32, Int32Opt);
  101. AssertOption(12.3456789f, options.TryGetFloat, FloatOpt);
  102. AssertOption(1.234567890123456789d, options.TryGetDouble, DoubleOpt);
  103. AssertOption("Hello, \"World\"", options.TryGetString, StringOpt);
  104. AssertOption(ByteString.CopyFromUtf8("Hello\0World"), options.TryGetBytes, BytesOpt);
  105. AssertOption((int)TestEnumType.TestOptionEnumType2, options.TryGetInt32, EnumOpt);
  106. }
  107. [Test]
  108. public void MessageOptions()
  109. {
  110. var options = VariousComplexOptions.Descriptor.CustomOptions;
  111. AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1);
  112. AssertOption(new ComplexOptionType2
  113. {
  114. Baz = 987,
  115. Bar = new ComplexOptionType1 { Foo = 743 },
  116. Fred = new ComplexOptionType4 { Waldo = 321 },
  117. Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } }
  118. },
  119. options.TryGetMessage, ComplexOpt2);
  120. AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3);
  121. }
  122. [Test]
  123. public void OptionLocations()
  124. {
  125. var fileOptions = UnittestCustomOptionsProto3Reflection.Descriptor.CustomOptions;
  126. AssertOption(9876543210UL, fileOptions.TryGetUInt64, FileOpt1);
  127. var messageOptions = TestMessageWithCustomOptions.Descriptor.CustomOptions;
  128. AssertOption(-56, messageOptions.TryGetInt32, MessageOpt1);
  129. var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"].CustomOptions;
  130. AssertOption(8765432109UL, fieldOptions.TryGetFixed64, FieldOpt1);
  131. var oneofOptions = TestMessageWithCustomOptions.Descriptor.Oneofs[0].CustomOptions;
  132. AssertOption(-99, oneofOptions.TryGetInt32, OneofOpt1);
  133. var enumOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].CustomOptions;
  134. AssertOption(-789, enumOptions.TryGetSFixed32, EnumOpt1);
  135. var enumValueOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).CustomOptions;
  136. AssertOption(123, enumValueOptions.TryGetInt32, EnumValueOpt1);
  137. var service = UnittestCustomOptionsProto3Reflection.Descriptor.Services
  138. .Single(s => s.Name == "TestServiceWithCustomOptions");
  139. var serviceOptions = service.CustomOptions;
  140. AssertOption(-9876543210, serviceOptions.TryGetSInt64, ServiceOpt1);
  141. var methodOptions = service.Methods[0].CustomOptions;
  142. AssertOption((int)UnitTest.Issues.TestProtos.MethodOpt1.Val2, methodOptions.TryGetInt32, CustomOptionNumber.MethodOpt1);
  143. }
  144. [Test]
  145. public void MinValues()
  146. {
  147. var options = CustomOptionMinIntegerValues.Descriptor.CustomOptions;
  148. AssertOption(false, options.TryGetBool, BoolOpt);
  149. AssertOption(int.MinValue, options.TryGetInt32, Int32Opt);
  150. AssertOption(long.MinValue, options.TryGetInt64, Int64Opt);
  151. AssertOption(uint.MinValue, options.TryGetUInt32, UInt32Opt);
  152. AssertOption(ulong.MinValue, options.TryGetUInt64, UInt64Opt);
  153. AssertOption(int.MinValue, options.TryGetSInt32, SInt32Opt);
  154. AssertOption(long.MinValue, options.TryGetSInt64, SInt64Opt);
  155. AssertOption(uint.MinValue, options.TryGetUInt32, Fixed32Opt);
  156. AssertOption(ulong.MinValue, options.TryGetUInt64, Fixed64Opt);
  157. AssertOption(int.MinValue, options.TryGetInt32, SFixed32Opt);
  158. AssertOption(long.MinValue, options.TryGetInt64, SFixed64Opt);
  159. }
  160. [Test]
  161. public void MaxValues()
  162. {
  163. var options = CustomOptionMaxIntegerValues.Descriptor.CustomOptions;
  164. AssertOption(true, options.TryGetBool, BoolOpt);
  165. AssertOption(int.MaxValue, options.TryGetInt32, Int32Opt);
  166. AssertOption(long.MaxValue, options.TryGetInt64, Int64Opt);
  167. AssertOption(uint.MaxValue, options.TryGetUInt32, UInt32Opt);
  168. AssertOption(ulong.MaxValue, options.TryGetUInt64, UInt64Opt);
  169. AssertOption(int.MaxValue, options.TryGetSInt32, SInt32Opt);
  170. AssertOption(long.MaxValue, options.TryGetSInt64, SInt64Opt);
  171. AssertOption(uint.MaxValue, options.TryGetFixed32, Fixed32Opt);
  172. AssertOption(ulong.MaxValue, options.TryGetFixed64, Fixed64Opt);
  173. AssertOption(int.MaxValue, options.TryGetSFixed32, SFixed32Opt);
  174. AssertOption(long.MaxValue, options.TryGetSFixed64, SFixed64Opt);
  175. }
  176. [Test]
  177. public void AggregateOptions()
  178. {
  179. // Just two examples
  180. var messageOptions = AggregateMessage.Descriptor.CustomOptions;
  181. AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageOptions.TryGetMessage, AggregateMsgOpt);
  182. var fieldOptions = AggregateMessage.Descriptor.Fields["fieldname"].CustomOptions;
  183. AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, AggregateFieldOpt);
  184. }
  185. private void AssertOption<T>(T expected, OptionFetcher<T> fetcher, CustomOptionNumber field)
  186. {
  187. T actual;
  188. Assert.IsTrue(fetcher((int)field, out actual));
  189. Assert.AreEqual(expected, actual);
  190. }
  191. }
  192. }