csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 2 |
|
| 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 34 |
|
| 35 | #endregion
|
| 36 |
|
| 37 | using System.IO;
|
| 38 | using NUnit.Framework;
|
| 39 | using System.Collections.Generic;
|
| 40 | using Google.ProtocolBuffers.TestProtos;
|
| 41 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 42 | namespace Google.ProtocolBuffers
|
| 43 | {
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 44 | [TestFixture]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 45 | public class MissingFieldAndExtensionTest
|
| 46 | {
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 47 | [Test]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 48 | public void TestRecoverMissingExtensions()
|
| 49 | {
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 50 | const int optionalInt32 = 12345678;
|
| 51 | TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
|
| 52 | builder.SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32);
|
| 53 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.1);
|
| 54 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.2);
|
| 55 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.3);
|
| 56 | TestAllExtensions msg = builder.Build();
|
| 57 |
|
| 58 | Assert.IsTrue(msg.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 59 | Assert.AreEqual(3, msg.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 60 |
|
| 61 | byte[] bits = msg.ToByteArray();
|
| 62 | TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);
|
| 63 | Assert.IsFalse(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 64 | Assert.AreEqual(0, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 65 | Assert.AreNotEqual(msg, copy);
|
| 66 |
|
| 67 | //Even though copy does not understand the typees they serialize correctly
|
| 68 | byte[] copybits = copy.ToByteArray();
|
| 69 | Assert.AreEqual(bits, copybits);
|
| 70 |
|
| 71 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 72 | UnitTestProtoFile.RegisterAllExtensions(registry);
|
| 73 |
|
| 74 | //Now we can take those copy bits and restore the full message with extensions
|
| 75 | copy = TestAllExtensions.ParseFrom(copybits, registry);
|
| 76 | Assert.IsTrue(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 77 | Assert.AreEqual(3, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 78 |
|
| 79 | Assert.AreEqual(msg, copy);
|
| 80 | Assert.AreEqual(bits, copy.ToByteArray());
|
| 81 |
|
| 82 | //If we modify the object this should all continue to work as before
|
| 83 | copybits = copy.ToBuilder().Build().ToByteArray();
|
| 84 | Assert.AreEqual(bits, copybits);
|
| 85 |
|
| 86 | //If we replace extension the object this should all continue to work as before
|
| 87 | copybits = copy.ToBuilder()
|
| 88 | .SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32)
|
| 89 | .Build().ToByteArray();
|
| 90 | Assert.AreEqual(bits, copybits);
|
| 91 | }
|
| 92 |
|
| 93 | [Test]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 94 | public void TestRecoverMissingFields()
|
| 95 | {
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 96 | TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
|
| 97 | .SetId(1001)
|
| 98 | .SetName("Name")
|
| 99 | .SetEmail("missing@field.value")
|
| 100 | .Build();
|
| 101 |
|
| 102 | //serialize to type B and verify all fields exist
|
| 103 | TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
|
| 104 | Assert.AreEqual(1001, msgb.Id);
|
| 105 | Assert.AreEqual("Name", msgb.Name);
|
| 106 | Assert.IsFalse(msgb.HasWebsite);
|
| 107 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 108 | Assert.AreEqual("missing@field.value",
|
| 109 | msgb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 110 |
|
| 111 | //serializes exactly the same (at least for this simple example)
|
| 112 | Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
|
| 113 | Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
|
| 114 |
|
| 115 | //now re-create an exact copy of A from serialized B
|
| 116 | TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 117 | Assert.AreEqual(msga, copya);
|
| 118 | Assert.AreEqual(1001, copya.Id);
|
| 119 | Assert.AreEqual("Name", copya.Name);
|
| 120 | Assert.AreEqual("missing@field.value", copya.Email);
|
| 121 |
|
| 122 | //Now we modify B... and try again
|
| 123 | msgb = msgb.ToBuilder().SetWebsite("http://new.missing.field").Build();
|
| 124 | //Does B still have the missing field?
|
| 125 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 126 |
|
| 127 | //Convert back to A and see if all fields are there?
|
| 128 | copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 129 | Assert.AreNotEqual(msga, copya);
|
| 130 | Assert.AreEqual(1001, copya.Id);
|
| 131 | Assert.AreEqual("Name", copya.Name);
|
| 132 | Assert.AreEqual("missing@field.value", copya.Email);
|
| 133 | Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 134 | Assert.AreEqual("http://new.missing.field",
|
| 135 | copya.UnknownFields[TestMissingFieldsB.WebsiteFieldNumber].LengthDelimitedList[0].
|
| 136 | ToStringUtf8());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 137 |
|
| 138 | //Lastly we can even still trip back to type B and see all fields:
|
| 139 | TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
|
| 140 | Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
|
| 141 | Assert.AreEqual(1001, copyb.Id);
|
| 142 | Assert.AreEqual("Name", copyb.Name);
|
| 143 | Assert.AreEqual("http://new.missing.field", copyb.Website);
|
| 144 | Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 145 | Assert.AreEqual("missing@field.value",
|
| 146 | copyb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8
|
| 147 | ());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 148 | }
|
| 149 |
|
| 150 | [Test]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 151 | public void TestRecoverMissingMessage()
|
| 152 | {
|
| 153 | TestMissingFieldsA.Types.SubA suba =
|
| 154 | TestMissingFieldsA.Types.SubA.CreateBuilder().SetCount(3).AddValues("a").AddValues("b").AddValues("c").
|
| 155 | Build();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 156 | TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
|
| 157 | .SetId(1001)
|
| 158 | .SetName("Name")
|
| 159 | .SetTestA(suba)
|
| 160 | .Build();
|
| 161 |
|
| 162 | //serialize to type B and verify all fields exist
|
| 163 | TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
|
| 164 | Assert.AreEqual(1001, msgb.Id);
|
| 165 | Assert.AreEqual("Name", msgb.Name);
|
| 166 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 167 | Assert.AreEqual(suba.ToString(),
|
| 168 | TestMissingFieldsA.Types.SubA.ParseFrom(
|
| 169 | msgb.UnknownFields[TestMissingFieldsA.TestAFieldNumber].LengthDelimitedList[0]).ToString
|
| 170 | ());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 171 |
|
| 172 | //serializes exactly the same (at least for this simple example)
|
| 173 | Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
|
| 174 | Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
|
| 175 |
|
| 176 | //now re-create an exact copy of A from serialized B
|
| 177 | TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 178 | Assert.AreEqual(msga, copya);
|
| 179 | Assert.AreEqual(1001, copya.Id);
|
| 180 | Assert.AreEqual("Name", copya.Name);
|
| 181 | Assert.AreEqual(suba, copya.TestA);
|
| 182 |
|
| 183 | //Now we modify B... and try again
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 184 | TestMissingFieldsB.Types.SubB subb =
|
| 185 | TestMissingFieldsB.Types.SubB.CreateBuilder().AddValues("test-b").Build();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 186 | msgb = msgb.ToBuilder().SetTestB(subb).Build();
|
| 187 | //Does B still have the missing field?
|
| 188 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 189 |
|
| 190 | //Convert back to A and see if all fields are there?
|
| 191 | copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 192 | Assert.AreNotEqual(msga, copya);
|
| 193 | Assert.AreEqual(1001, copya.Id);
|
| 194 | Assert.AreEqual("Name", copya.Name);
|
| 195 | Assert.AreEqual(suba, copya.TestA);
|
| 196 | Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 197 | Assert.AreEqual(subb.ToByteArray(),
|
| 198 | copya.UnknownFields[TestMissingFieldsB.TestBFieldNumber].LengthDelimitedList[0].ToByteArray());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 199 |
|
| 200 | //Lastly we can even still trip back to type B and see all fields:
|
| 201 | TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
|
| 202 | Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
|
| 203 | Assert.AreEqual(1001, copyb.Id);
|
| 204 | Assert.AreEqual("Name", copyb.Name);
|
| 205 | Assert.AreEqual(subb, copyb.TestB);
|
| 206 | Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
|
| 207 | }
|
| 208 |
|
| 209 | [Test]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 210 | public void TestRestoreFromOtherType()
|
| 211 | {
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 212 | TestInteropPerson person = TestInteropPerson.CreateBuilder()
|
| 213 | .SetId(123)
|
| 214 | .SetName("abc")
|
| 215 | .SetEmail("abc@123.com")
|
| 216 | .AddRangeCodes(new[] {1, 2, 3})
|
| 217 | .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
|
| 218 | .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 219 | .AddAddresses(
|
| 220 | TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
|
| 221 | SetState("NA").SetZip(12345).Build())
|
| 222 | .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId,
|
| 223 | TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 224 | .Build();
|
| 225 | Assert.IsTrue(person.IsInitialized);
|
| 226 |
|
| 227 | TestEmptyMessage temp = TestEmptyMessage.ParseFrom(person.ToByteArray());
|
| 228 | Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
|
| 229 | temp = temp.ToBuilder().Build();
|
| 230 | Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
|
| 231 |
|
| 232 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 233 | UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
|
| 234 |
|
| 235 | TestInteropPerson copy = TestInteropPerson.ParseFrom(temp.ToByteArray(), registry);
|
| 236 | Assert.AreEqual(person, copy);
|
| 237 | Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
|
| 238 | }
|
| 239 | }
|
csharptest | cb7fc65 | 2010-11-19 10:59:50 -0600 | [diff] [blame] | 240 | } |