DynamicMessageTest.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections.Generic;
  36. using Google.ProtocolBuffers.TestProtos;
  37. using NUnit.Framework;
  38. namespace Google.ProtocolBuffers {
  39. [TestFixture]
  40. public class DynamicMessageTest {
  41. private ReflectionTester reflectionTester;
  42. private ReflectionTester extensionsReflectionTester;
  43. private ReflectionTester packedReflectionTester;
  44. [SetUp]
  45. public void SetUp() {
  46. reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
  47. extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
  48. packedReflectionTester = ReflectionTester.CreateTestPackedTypesInstance();
  49. }
  50. [Test]
  51. public void DynamicMessageAccessors() {
  52. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  53. reflectionTester.SetAllFieldsViaReflection(builder);
  54. IMessage message = builder.WeakBuild();
  55. reflectionTester.AssertAllFieldsSetViaReflection(message);
  56. }
  57. [Test]
  58. public void DoubleBuildError() {
  59. DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  60. builder.Build();
  61. try {
  62. builder.Build();
  63. Assert.Fail("Should have thrown exception.");
  64. } catch (InvalidOperationException) {
  65. // Success.
  66. }
  67. }
  68. [Test]
  69. public void DynamicMessageSettersRejectNull() {
  70. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  71. reflectionTester.AssertReflectionSettersRejectNull(builder);
  72. }
  73. [Test]
  74. public void DynamicMessageExtensionAccessors() {
  75. // We don't need to extensively test DynamicMessage's handling of
  76. // extensions because, frankly, it doesn't do anything special with them.
  77. // It treats them just like any other fields.
  78. IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
  79. extensionsReflectionTester.SetAllFieldsViaReflection(builder);
  80. IMessage message = builder.WeakBuild();
  81. extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
  82. }
  83. [Test]
  84. public void DynamicMessageExtensionSettersRejectNull() {
  85. IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
  86. extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
  87. }
  88. [Test]
  89. public void DynamicMessageRepeatedSetters() {
  90. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  91. reflectionTester.SetAllFieldsViaReflection(builder);
  92. reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
  93. IMessage message = builder.WeakBuild();
  94. reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
  95. }
  96. [Test]
  97. public void DynamicMessageRepeatedSettersRejectNull() {
  98. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  99. reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
  100. }
  101. [Test]
  102. public void DynamicMessageDefaults() {
  103. reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
  104. reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
  105. }
  106. [Test]
  107. public void DynamicMessageSerializedSize() {
  108. TestAllTypes message = TestUtil.GetAllSet();
  109. IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  110. reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
  111. IMessage dynamicMessage = dynamicBuilder.WeakBuild();
  112. Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
  113. }
  114. [Test]
  115. public void DynamicMessageSerialization() {
  116. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  117. reflectionTester.SetAllFieldsViaReflection(builder);
  118. IMessage message = builder.WeakBuild();
  119. ByteString rawBytes = message.ToByteString();
  120. TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
  121. TestUtil.AssertAllFieldsSet(message2);
  122. // In fact, the serialized forms should be exactly the same, byte-for-byte.
  123. Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
  124. }
  125. [Test]
  126. public void DynamicMessageParsing() {
  127. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  128. TestUtil.SetAllFields(builder);
  129. TestAllTypes message = builder.Build();
  130. ByteString rawBytes = message.ToByteString();
  131. IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
  132. reflectionTester.AssertAllFieldsSetViaReflection(message2);
  133. }
  134. [Test]
  135. public void DynamicMessagePackedSerialization() {
  136. IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);
  137. packedReflectionTester.SetPackedFieldsViaReflection(builder);
  138. IMessage message = builder.WeakBuild();
  139. ByteString rawBytes = message.ToByteString();
  140. TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
  141. TestUtil.AssertPackedFieldsSet(message2);
  142. // In fact, the serialized forms should be exactly the same, byte-for-byte.
  143. Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
  144. }
  145. [Test]
  146. public void testDynamicMessagePackedParsing() {
  147. TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
  148. TestUtil.SetPackedFields(builder);
  149. TestPackedTypes message = builder.Build();
  150. ByteString rawBytes = message.ToByteString();
  151. IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);
  152. packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
  153. }
  154. [Test]
  155. public void DynamicMessageCopy() {
  156. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  157. TestUtil.SetAllFields(builder);
  158. TestAllTypes message = builder.Build();
  159. DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
  160. reflectionTester.AssertAllFieldsSetViaReflection(copy);
  161. }
  162. [Test]
  163. public void ToBuilder() {
  164. DynamicMessage.Builder builder =
  165. DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  166. reflectionTester.SetAllFieldsViaReflection(builder);
  167. int unknownFieldNum = 9;
  168. ulong unknownFieldVal = 90;
  169. builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
  170. .AddField(unknownFieldNum,
  171. UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
  172. .Build());
  173. DynamicMessage message = builder.Build();
  174. DynamicMessage derived = message.ToBuilder().Build();
  175. reflectionTester.AssertAllFieldsSetViaReflection(derived);
  176. IList<ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;
  177. Assert.AreEqual(1, values.Count);
  178. Assert.AreEqual(unknownFieldVal, values[0]);
  179. }
  180. }
  181. }