MissingFieldAndExtensionTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 NUnit.Framework;
  36. using System.Collections.Generic;
  37. using Google.ProtocolBuffers.TestProtos;
  38. namespace Google.ProtocolBuffers {
  39. [TestFixture]
  40. public class MissingFieldAndExtensionTest {
  41. [Test]
  42. public void TestRecoverMissingExtensions() {
  43. const int optionalInt32 = 12345678;
  44. TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
  45. builder.SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32);
  46. builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.1);
  47. builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.2);
  48. builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.3);
  49. TestAllExtensions msg = builder.Build();
  50. Assert.IsTrue(msg.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
  51. Assert.AreEqual(3, msg.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
  52. byte[] bits = msg.ToByteArray();
  53. TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);
  54. Assert.IsFalse(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
  55. Assert.AreEqual(0, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
  56. Assert.AreNotEqual(msg, copy);
  57. //Even though copy does not understand the typees they serialize correctly
  58. byte[] copybits = copy.ToByteArray();
  59. Assert.AreEqual(bits, copybits);
  60. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  61. UnitTestProtoFile.RegisterAllExtensions(registry);
  62. //Now we can take those copy bits and restore the full message with extensions
  63. copy = TestAllExtensions.ParseFrom(copybits, registry);
  64. Assert.IsTrue(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
  65. Assert.AreEqual(3, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
  66. Assert.AreEqual(msg, copy);
  67. Assert.AreEqual(bits, copy.ToByteArray());
  68. //If we modify the object this should all continue to work as before
  69. copybits = copy.ToBuilder().Build().ToByteArray();
  70. Assert.AreEqual(bits, copybits);
  71. //If we replace extension the object this should all continue to work as before
  72. copybits = copy.ToBuilder()
  73. .SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32)
  74. .Build().ToByteArray();
  75. Assert.AreEqual(bits, copybits);
  76. }
  77. [Test]
  78. public void TestRecoverMissingFields() {
  79. TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
  80. .SetId(1001)
  81. .SetName("Name")
  82. .SetEmail("missing@field.value")
  83. .Build();
  84. //serialize to type B and verify all fields exist
  85. TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
  86. Assert.AreEqual(1001, msgb.Id);
  87. Assert.AreEqual("Name", msgb.Name);
  88. Assert.IsFalse(msgb.HasWebsite);
  89. Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
  90. Assert.AreEqual("missing@field.value", msgb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());
  91. //serializes exactly the same (at least for this simple example)
  92. Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
  93. Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
  94. //now re-create an exact copy of A from serialized B
  95. TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
  96. Assert.AreEqual(msga, copya);
  97. Assert.AreEqual(1001, copya.Id);
  98. Assert.AreEqual("Name", copya.Name);
  99. Assert.AreEqual("missing@field.value", copya.Email);
  100. //Now we modify B... and try again
  101. msgb = msgb.ToBuilder().SetWebsite("http://new.missing.field").Build();
  102. //Does B still have the missing field?
  103. Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
  104. //Convert back to A and see if all fields are there?
  105. copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
  106. Assert.AreNotEqual(msga, copya);
  107. Assert.AreEqual(1001, copya.Id);
  108. Assert.AreEqual("Name", copya.Name);
  109. Assert.AreEqual("missing@field.value", copya.Email);
  110. Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
  111. Assert.AreEqual("http://new.missing.field", copya.UnknownFields[TestMissingFieldsB.WebsiteFieldNumber].LengthDelimitedList[0].ToStringUtf8());
  112. //Lastly we can even still trip back to type B and see all fields:
  113. TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
  114. Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
  115. Assert.AreEqual(1001, copyb.Id);
  116. Assert.AreEqual("Name", copyb.Name);
  117. Assert.AreEqual("http://new.missing.field", copyb.Website);
  118. Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
  119. Assert.AreEqual("missing@field.value", copyb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());
  120. }
  121. [Test]
  122. public void TestRecoverMissingMessage() {
  123. TestMissingFieldsA.Types.SubA suba = TestMissingFieldsA.Types.SubA.CreateBuilder().SetCount(3).AddValues("a").AddValues("b").AddValues("c").Build();
  124. TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
  125. .SetId(1001)
  126. .SetName("Name")
  127. .SetTestA(suba)
  128. .Build();
  129. //serialize to type B and verify all fields exist
  130. TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
  131. Assert.AreEqual(1001, msgb.Id);
  132. Assert.AreEqual("Name", msgb.Name);
  133. Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
  134. Assert.AreEqual(suba.ToString(), TestMissingFieldsA.Types.SubA.ParseFrom(msgb.UnknownFields[TestMissingFieldsA.TestAFieldNumber].LengthDelimitedList[0]).ToString());
  135. //serializes exactly the same (at least for this simple example)
  136. Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
  137. Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
  138. //now re-create an exact copy of A from serialized B
  139. TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
  140. Assert.AreEqual(msga, copya);
  141. Assert.AreEqual(1001, copya.Id);
  142. Assert.AreEqual("Name", copya.Name);
  143. Assert.AreEqual(suba, copya.TestA);
  144. //Now we modify B... and try again
  145. TestMissingFieldsB.Types.SubB subb = TestMissingFieldsB.Types.SubB.CreateBuilder().AddValues("test-b").Build();
  146. msgb = msgb.ToBuilder().SetTestB(subb).Build();
  147. //Does B still have the missing field?
  148. Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
  149. //Convert back to A and see if all fields are there?
  150. copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
  151. Assert.AreNotEqual(msga, copya);
  152. Assert.AreEqual(1001, copya.Id);
  153. Assert.AreEqual("Name", copya.Name);
  154. Assert.AreEqual(suba, copya.TestA);
  155. Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
  156. Assert.AreEqual(subb.ToByteArray(), copya.UnknownFields[TestMissingFieldsB.TestBFieldNumber].LengthDelimitedList[0].ToByteArray());
  157. //Lastly we can even still trip back to type B and see all fields:
  158. TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
  159. Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
  160. Assert.AreEqual(1001, copyb.Id);
  161. Assert.AreEqual("Name", copyb.Name);
  162. Assert.AreEqual(subb, copyb.TestB);
  163. Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
  164. }
  165. [Test]
  166. public void TestRestoreFromOtherType() {
  167. TestInteropPerson person = TestInteropPerson.CreateBuilder()
  168. .SetId(123)
  169. .SetName("abc")
  170. .SetEmail("abc@123.com")
  171. .AddRangeCodes(new[] {1, 2, 3})
  172. .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
  173. .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
  174. .AddAddresses(TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").SetState("NA").SetZip(12345).Build())
  175. .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId, TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
  176. .Build();
  177. Assert.IsTrue(person.IsInitialized);
  178. TestEmptyMessage temp = TestEmptyMessage.ParseFrom(person.ToByteArray());
  179. Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
  180. temp = temp.ToBuilder().Build();
  181. Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
  182. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  183. UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
  184. TestInteropPerson copy = TestInteropPerson.ParseFrom(temp.ToByteArray(), registry);
  185. Assert.AreEqual(person, copy);
  186. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  187. }
  188. }
  189. }