MissingFieldAndExtensionTest.cs 12 KB

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