WireFormatTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.IO;
  35. using System.Reflection;
  36. using Google.ProtocolBuffers.Descriptors;
  37. using Google.ProtocolBuffers.TestProtos;
  38. using NUnit.Framework;
  39. namespace Google.ProtocolBuffers
  40. {
  41. public class WireFormatTest
  42. {
  43. /// <summary>
  44. /// Keeps the attributes on FieldType and the switch statement in WireFormat in sync.
  45. /// </summary>
  46. [Test]
  47. public void FieldTypeToWireTypeMapping()
  48. {
  49. foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public))
  50. {
  51. FieldType fieldType = (FieldType) field.GetValue(null);
  52. FieldMappingAttribute mapping =
  53. (FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
  54. Assert.AreEqual(mapping.WireType, WireFormat.GetWireType(fieldType));
  55. }
  56. }
  57. [Test]
  58. public void Serialization()
  59. {
  60. TestAllTypes message = TestUtil.GetAllSet();
  61. ByteString rawBytes = message.ToByteString();
  62. Assert.AreEqual(rawBytes.Length, message.SerializedSize);
  63. TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
  64. TestUtil.AssertAllFieldsSet(message2);
  65. }
  66. [Test]
  67. public void SerializationPacked()
  68. {
  69. TestPackedTypes message = TestUtil.GetPackedSet();
  70. ByteString rawBytes = message.ToByteString();
  71. Assert.AreEqual(rawBytes.Length, message.SerializedSize);
  72. TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
  73. TestUtil.AssertPackedFieldsSet(message2);
  74. }
  75. [Test]
  76. public void SerializeExtensions()
  77. {
  78. // TestAllTypes and TestAllExtensions should have compatible wire formats,
  79. // so if we serialize a TestAllExtensions then parse it as TestAllTypes
  80. // it should work.
  81. TestAllExtensions message = TestUtil.GetAllExtensionsSet();
  82. ByteString rawBytes = message.ToByteString();
  83. Assert.AreEqual(rawBytes.Length, message.SerializedSize);
  84. TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
  85. TestUtil.AssertAllFieldsSet(message2);
  86. }
  87. [Test]
  88. public void SerializePackedExtensions()
  89. {
  90. // TestPackedTypes and TestPackedExtensions should have compatible wire
  91. // formats; check that they serialize to the same string.
  92. TestPackedExtensions message = TestUtil.GetPackedExtensionsSet();
  93. ByteString rawBytes = message.ToByteString();
  94. TestPackedTypes message2 = TestUtil.GetPackedSet();
  95. ByteString rawBytes2 = message2.ToByteString();
  96. Assert.AreEqual(rawBytes, rawBytes2);
  97. }
  98. [Test]
  99. public void SerializeDelimited()
  100. {
  101. MemoryStream stream = new MemoryStream();
  102. TestUtil.GetAllSet().WriteDelimitedTo(stream);
  103. stream.WriteByte(12);
  104. TestUtil.GetPackedSet().WriteDelimitedTo(stream);
  105. stream.WriteByte(34);
  106. stream.Position = 0;
  107. TestUtil.AssertAllFieldsSet(TestAllTypes.ParseDelimitedFrom(stream));
  108. Assert.AreEqual(12, stream.ReadByte());
  109. TestUtil.AssertPackedFieldsSet(TestPackedTypes.ParseDelimitedFrom(stream));
  110. Assert.AreEqual(34, stream.ReadByte());
  111. Assert.AreEqual(-1, stream.ReadByte());
  112. }
  113. [Test]
  114. public void ParseExtensions()
  115. {
  116. // TestAllTypes and TestAllExtensions should have compatible wire formats,
  117. // so if we serealize a TestAllTypes then parse it as TestAllExtensions
  118. // it should work.
  119. TestAllTypes message = TestUtil.GetAllSet();
  120. ByteString rawBytes = message.ToByteString();
  121. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  122. TestUtil.RegisterAllExtensions(registry);
  123. registry = registry.AsReadOnly();
  124. TestAllExtensions message2 = TestAllExtensions.ParseFrom(rawBytes, registry);
  125. TestUtil.AssertAllExtensionsSet(message2);
  126. }
  127. [Test]
  128. public void ParsePackedExtensions()
  129. {
  130. // Ensure that packed extensions can be properly parsed.
  131. TestPackedExtensions message = TestUtil.GetPackedExtensionsSet();
  132. ByteString rawBytes = message.ToByteString();
  133. ExtensionRegistry registry = TestUtil.CreateExtensionRegistry();
  134. TestPackedExtensions message2 = TestPackedExtensions.ParseFrom(rawBytes, registry);
  135. TestUtil.AssertPackedExtensionsSet(message2);
  136. }
  137. [Test]
  138. public void ExtensionsSerializedSize()
  139. {
  140. Assert.AreEqual(TestUtil.GetAllSet().SerializedSize, TestUtil.GetAllExtensionsSet().SerializedSize);
  141. }
  142. private static void AssertFieldsInOrder(ByteString data)
  143. {
  144. CodedInputStream input = data.CreateCodedInput();
  145. uint previousTag = 0;
  146. uint tag;
  147. string name;
  148. while (input.ReadTag(out tag, out name))
  149. {
  150. Assert.IsTrue(tag > previousTag);
  151. previousTag = tag;
  152. input.SkipField();
  153. }
  154. }
  155. [Test]
  156. public void InterleavedFieldsAndExtensions()
  157. {
  158. // Tests that fields are written in order even when extension ranges
  159. // are interleaved with field numbers.
  160. ByteString data =
  161. TestFieldOrderings.CreateBuilder()
  162. .SetMyInt(1)
  163. .SetMyString("foo")
  164. .SetMyFloat(1.0F)
  165. .SetExtension(Unittest.MyExtensionInt, 23)
  166. .SetExtension(Unittest.MyExtensionString, "bar")
  167. .Build().ToByteString();
  168. AssertFieldsInOrder(data);
  169. MessageDescriptor descriptor = TestFieldOrderings.Descriptor;
  170. ByteString dynamic_data =
  171. DynamicMessage.CreateBuilder(TestFieldOrderings.Descriptor)
  172. .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_int"), 1L)
  173. .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_string"), "foo")
  174. .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_float"), 1.0F)
  175. .SetField(Unittest.MyExtensionInt.Descriptor, 23)
  176. .SetField(Unittest.MyExtensionString.Descriptor, "bar")
  177. .WeakBuild().ToByteString();
  178. AssertFieldsInOrder(dynamic_data);
  179. }
  180. private const int UnknownTypeId = 1550055;
  181. private static readonly int TypeId1 = TestMessageSetExtension1.Descriptor.Extensions[0].FieldNumber;
  182. private static readonly int TypeId2 = TestMessageSetExtension2.Descriptor.Extensions[0].FieldNumber;
  183. [Test]
  184. public void SerializeMessageSet()
  185. {
  186. // Set up a TestMessageSet with two known messages and an unknown one.
  187. TestMessageSet messageSet =
  188. TestMessageSet.CreateBuilder()
  189. .SetExtension(
  190. TestMessageSetExtension1.MessageSetExtension,
  191. TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
  192. .SetExtension(
  193. TestMessageSetExtension2.MessageSetExtension,
  194. TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
  195. .SetUnknownFields(
  196. UnknownFieldSet.CreateBuilder()
  197. .AddField(UnknownTypeId,
  198. UnknownField.CreateBuilder()
  199. .AddLengthDelimited(ByteString.CopyFromUtf8("bar"))
  200. .Build())
  201. .Build())
  202. .Build();
  203. ByteString data = messageSet.ToByteString();
  204. // Parse back using RawMessageSet and check the contents.
  205. RawMessageSet raw = RawMessageSet.ParseFrom(data);
  206. Assert.AreEqual(0, raw.UnknownFields.FieldDictionary.Count);
  207. Assert.AreEqual(3, raw.ItemCount);
  208. Assert.AreEqual(TypeId1, raw.ItemList[0].TypeId);
  209. Assert.AreEqual(TypeId2, raw.ItemList[1].TypeId);
  210. Assert.AreEqual(UnknownTypeId, raw.ItemList[2].TypeId);
  211. TestMessageSetExtension1 message1 = TestMessageSetExtension1.ParseFrom(raw.GetItem(0).Message.ToByteArray());
  212. Assert.AreEqual(123, message1.I);
  213. TestMessageSetExtension2 message2 = TestMessageSetExtension2.ParseFrom(raw.GetItem(1).Message.ToByteArray());
  214. Assert.AreEqual("foo", message2.Str);
  215. Assert.AreEqual("bar", raw.GetItem(2).Message.ToStringUtf8());
  216. }
  217. [Test]
  218. public void ParseMessageSet()
  219. {
  220. ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
  221. extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
  222. extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
  223. // Set up a RawMessageSet with two known messages and an unknown one.
  224. RawMessageSet raw =
  225. RawMessageSet.CreateBuilder()
  226. .AddItem(
  227. RawMessageSet.Types.Item.CreateBuilder()
  228. .SetTypeId(TypeId1)
  229. .SetMessage(
  230. TestMessageSetExtension1.CreateBuilder()
  231. .SetI(123)
  232. .Build().ToByteString())
  233. .Build())
  234. .AddItem(
  235. RawMessageSet.Types.Item.CreateBuilder()
  236. .SetTypeId(TypeId2)
  237. .SetMessage(
  238. TestMessageSetExtension2.CreateBuilder()
  239. .SetStr("foo")
  240. .Build().ToByteString())
  241. .Build())
  242. .AddItem(
  243. RawMessageSet.Types.Item.CreateBuilder()
  244. .SetTypeId(UnknownTypeId)
  245. .SetMessage(ByteString.CopyFromUtf8("bar"))
  246. .Build())
  247. .Build();
  248. ByteString data = raw.ToByteString();
  249. // Parse as a TestMessageSet and check the contents.
  250. TestMessageSet messageSet =
  251. TestMessageSet.ParseFrom(data, extensionRegistry);
  252. Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
  253. Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
  254. // Check for unknown field with type LENGTH_DELIMITED,
  255. // number UNKNOWN_TYPE_ID, and contents "bar".
  256. UnknownFieldSet unknownFields = messageSet.UnknownFields;
  257. Assert.AreEqual(1, unknownFields.FieldDictionary.Count);
  258. Assert.IsTrue(unknownFields.HasField(UnknownTypeId));
  259. UnknownField field = unknownFields[UnknownTypeId];
  260. Assert.AreEqual(1, field.LengthDelimitedList.Count);
  261. Assert.AreEqual("bar", field.LengthDelimitedList[0].ToStringUtf8());
  262. }
  263. }
  264. }