예제 #1
0
        public void TestIBuilderLiteWeakMergeFromByteStringExtensions()
        {
            TestAllExtensionsLite copy,
                                  msg = TestAllExtensionsLite.CreateBuilder()
                                        .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite,
                                                      "Should be merged.").Build();

            copy = TestAllExtensionsLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy =
                (TestAllExtensionsLite)
                ((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), ExtensionRegistry.Empty).WeakBuild();
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy =
                (TestAllExtensionsLite)
                ((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), registry).WeakBuild();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
            Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
        }
예제 #2
0
        public void TestConvertFromFullComplete()
        {
            TestInteropPerson person = TestInteropPerson.CreateBuilder()
                                       .SetId(123)
                                       .SetName("abc")
                                       .SetEmail("*****@*****.**")
                                       .AddRangeCodes(new[] { 1, 2, 3 })
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
                                       .AddAddresses(
                TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
                SetState("NA").SetZip(12345).Build())
                                       .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId,
                                                     TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
                                       .Build();

            Assert.IsTrue(person.IsInitialized);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestExtrasLiteProtoFile.RegisterAllExtensions(registry);

            TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray(), registry);

            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());
        }
        public void TestRestoreFromOtherType()
        {
            TestInteropPerson person = TestInteropPerson.CreateBuilder()
                                       .SetId(123)
                                       .SetName("abc")
                                       .SetEmail("*****@*****.**")
                                       .AddRangeCodes(new[] { 1, 2, 3 })
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
                                       .AddAddresses(
                TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
                SetState("NA").SetZip(12345).Build())
                                       .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId,
                                                     TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
                                       .Build();

            Assert.IsTrue(person.IsInitialized);

            TestEmptyMessage temp = TestEmptyMessage.ParseFrom(person.ToByteArray());

            Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
            temp = temp.ToBuilder().Build();
            Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);

            TestInteropPerson copy = TestInteropPerson.ParseFrom(temp.ToByteArray(), registry);

            Assert.AreEqual(person, copy);
            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());
        }
        public void TestRecoverMissingMessage()
        {
            TestMissingFieldsA.Types.SubA suba =
                TestMissingFieldsA.Types.SubA.CreateBuilder().SetCount(3).AddValues("a").AddValues("b").AddValues("c").
                Build();
            TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
                                      .SetId(1001)
                                      .SetName("Name")
                                      .SetTestA(suba)
                                      .Build();

            //serialize to type B and verify all fields exist
            TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());

            Assert.AreEqual(1001, msgb.Id);
            Assert.AreEqual("Name", msgb.Name);
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual(suba.ToString(),
                            TestMissingFieldsA.Types.SubA.ParseFrom(
                                msgb.UnknownFields[TestMissingFieldsA.TestAFieldNumber].LengthDelimitedList[0]).ToString
                                ());

            //serializes exactly the same (at least for this simple example)
            TestUtil.AssertBytesEqual(msga.ToByteArray(), msgb.ToByteArray());
            Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));

            //now re-create an exact copy of A from serialized B
            TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());

            Assert.AreEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual(suba, copya.TestA);

            //Now we modify B... and try again
            TestMissingFieldsB.Types.SubB subb =
                TestMissingFieldsB.Types.SubB.CreateBuilder().AddValues("test-b").Build();
            msgb = msgb.ToBuilder().SetTestB(subb).Build();
            //Does B still have the missing field?
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);

            //Convert back to A and see if all fields are there?
            copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
            Assert.AreNotEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual(suba, copya.TestA);
            Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
            TestUtil.AssertBytesEqual(subb.ToByteArray(),
                                      copya.UnknownFields[TestMissingFieldsB.TestBFieldNumber].LengthDelimitedList[0].ToByteArray());

            //Lastly we can even still trip back to type B and see all fields:
            TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());

            Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
            Assert.AreEqual(1001, copyb.Id);
            Assert.AreEqual("Name", copyb.Name);
            Assert.AreEqual(subb, copyb.TestB);
            Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
        }
        public void FromBase64_WithText()
        {
            byte[]     data   = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
            string     base64 = Convert.ToBase64String(data);
            ByteString bs     = ByteString.FromBase64(base64);

            TestUtil.AssertBytesEqual(data, bs.ToByteArray());
        }
        public void TestRecoverMissingFields()
        {
            TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
                                      .SetId(1001)
                                      .SetName("Name")
                                      .SetEmail("*****@*****.**")
                                      .Build();

            //serialize to type B and verify all fields exist
            TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());

            Assert.AreEqual(1001, msgb.Id);
            Assert.AreEqual("Name", msgb.Name);
            Assert.IsFalse(msgb.HasWebsite);
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("*****@*****.**",
                            msgb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());

            //serializes exactly the same (at least for this simple example)
            TestUtil.AssertBytesEqual(msga.ToByteArray(), msgb.ToByteArray());
            Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));

            //now re-create an exact copy of A from serialized B
            TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());

            Assert.AreEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual("*****@*****.**", copya.Email);

            //Now we modify B... and try again
            msgb = msgb.ToBuilder().SetWebsite("http://new.missing.field").Build();
            //Does B still have the missing field?
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);

            //Convert back to A and see if all fields are there?
            copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
            Assert.AreNotEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual("*****@*****.**", copya.Email);
            Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("http://new.missing.field",
                            copya.UnknownFields[TestMissingFieldsB.WebsiteFieldNumber].LengthDelimitedList[0].
                            ToStringUtf8());

            //Lastly we can even still trip back to type B and see all fields:
            TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());

            Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
            Assert.AreEqual(1001, copyb.Id);
            Assert.AreEqual("Name", copyb.Name);
            Assert.AreEqual("http://new.missing.field", copyb.Website);
            Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("*****@*****.**",
                            copyb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8
                                ());
        }
예제 #7
0
        public void TestAllTypesDefaultedRoundTrip()
        {
            TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;

            Assert.IsTrue(msg.IsInitialized);
            TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #8
0
        public void TestWriteToProtoType()
        {
            MemoryStream ms = new MemoryStream();

            Extensions.WriteTo(TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build(),
                               new MessageFormatOptions(), "application/vnd.google.protobuf", ms);

            byte[] bytes = TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build().ToByteArray();
            TestUtil.AssertBytesEqual(bytes, ms.ToArray());
        }
예제 #9
0
        public void TestIBuilderLiteWeakClear()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.DefaultInstance;

            copy = msg.ToBuilder().SetOptionalString("Should be removed.").Build();
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakClear().WeakBuild();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #10
0
        public void TestIBuilderLiteWeakClone()
        {
            TestRequiredLite msg = TestRequiredLite.CreateBuilder()
                                   .SetD(1).SetEn(ExtraEnum.EXLITE_BAR).Build();

            Assert.IsTrue(msg.IsInitialized);

            IMessageLite copy = ((IBuilderLite)msg.ToBuilder()).WeakClone().WeakBuild();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #11
0
        public void TestMessageLiteWeakToBuilder()
        {
            IMessageLite msg = TestRequiredLite.CreateBuilder()
                               .SetD(42)
                               .SetEn(ExtraEnum.EXLITE_BAZ)
                               .Build();

            IMessageLite copy = msg.WeakToBuilder().WeakBuild();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #12
0
        public void TestIBuilderLiteWeakBuildPartial()
        {
            IBuilderLite builder = TestRequiredLite.CreateBuilder();

            Assert.IsFalse(builder.IsInitialized);

            IMessageLite msg = builder.WeakBuildPartial();

            Assert.IsFalse(msg.IsInitialized);

            TestUtil.AssertBytesEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
        }
예제 #13
0
        public void TestMessageLiteWriteTo()
        {
            TestRequiredLite msg = TestRequiredLite.CreateBuilder()
                                   .SetD(42)
                                   .SetEn(ExtraEnum.EXLITE_BAZ)
                                   .Build();

            MemoryStream ms = new MemoryStream();

            msg.WriteTo(ms);
            TestUtil.AssertBytesEqual(msg.ToByteArray(), ms.ToArray());
        }
예제 #14
0
        public void TestIBuilderLiteWeakMergeFromByteString()
        {
            TestAllTypesLite copy,
                             msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString()).WeakBuild();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #15
0
        public void TestGeneratedBuilderLiteAddRange()
        {
            TestAllTypesLite copy,
                             msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalUint32(123)
                                   .AddRepeatedInt32(1)
                                   .AddRepeatedInt32(2)
                                   .AddRepeatedInt32(3)
                                   .Build();

            copy = msg.DefaultInstanceForType.ToBuilder().MergeFrom(msg).Build();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #16
0
        public void TestBuilderLiteMergeFromCodedInputStream()
        {
            TestAllTypesLite copy,
                             msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy =
                copy.ToBuilder().MergeFrom(CodedInputStream.CreateInstance(new MemoryStream(msg.ToByteArray()))).Build();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #17
0
        public void TestConvertFromLiteMinimal()
        {
            TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
                                           .SetId(123)
                                           .SetName("abc")
                                           .Build();

            Assert.IsTrue(person.IsInitialized);

            TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray());

            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());
        }
예제 #18
0
        public void TestCompareStringValues()
        {
            TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
                                           .SetId(123)
                                           .SetName("abc")
                                           .SetEmail("*****@*****.**")
                                           .AddRangeCodes(new[] { 1, 2, 3 })
                                           .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                           .AddPhone(
                TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber(
                    System.Text.Encoding.UTF8.GetString(AllBytes.ToByteArray(), 0, AllBytes.Length)).Build())
                                           .AddAddresses(
                TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
                .SetState("NA").SetZip(12345).Build())
                                           .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
                                                         TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
                                           .Build();

            Assert.IsTrue(person.IsInitialized);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);

            TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);

            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());

            TestInteropPerson.Builder copyBuilder = TestInteropPerson.CreateBuilder();
            TextFormat.Merge(
                person.ToString().Replace("[protobuf_unittest_extra.employee_id_lite]",
                                          "[protobuf_unittest_extra.employee_id]"), registry, copyBuilder);

            copy = copyBuilder.Build();
            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());

            string liteText = person.ToString().TrimEnd().Replace("\r", "");
            string fullText = copy.ToString().TrimEnd().Replace("\r", "");

            //map the extension type
            liteText = liteText.Replace("[protobuf_unittest_extra.employee_id_lite]",
                                        "[protobuf_unittest_extra.employee_id]");
            //lite version does not indent
            while (fullText.IndexOf("\n ") >= 0)
            {
                fullText = fullText.Replace("\n ", "\n");
            }

            Assert.AreEqual(fullText, liteText);
        }
예제 #19
0
        public void ExtensionWriterTestMessages()
        {
            TestAllExtensionsLite.Builder b = TestAllExtensionsLite.CreateBuilder().SetExtension(
                UnitTestLiteProtoFile.OptionalForeignMessageExtensionLite,
                ForeignMessageLite.CreateBuilder().SetC(123).Build());
            TestAllExtensionsLite copy, msg = b.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy = TestAllExtensionsLite.ParseFrom(msg.ToByteArray(), registry);
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #20
0
        public void TestBuilderLiteMergeDelimitedFrom()
        {
            TestAllTypesLite copy,
                             msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
            Stream s = new MemoryStream();

            msg.WriteDelimitedTo(s);
            s.Position = 0;
            copy       = copy.ToBuilder().MergeDelimitedFrom(s).Build();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
        /// <summary>
        /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
        /// checks that the result matches the given bytes
        /// </summary>
        private static void AssertWriteVarint(byte[] data, ulong value)
        {
            // Only do 32-bit write if the value fits in 32 bits.
            if ((value >> 32) == 0)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);
                output.WriteRawVarint32((uint)value);
                output.Flush();
                TestUtil.AssertBytesEqual(data, rawOutput.ToArray());
                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint)value));
            }

            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);
                output.WriteRawVarint64(value);
                output.Flush();
                TestUtil.AssertBytesEqual(data, rawOutput.ToArray());

                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
            }

            // Try different buffer sizes.
            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
            {
                // Only do 32-bit write if the value fits in 32 bits.
                if ((value >> 32) == 0)
                {
                    MemoryStream      rawOutput = new MemoryStream();
                    CodedOutputStream output    =
                        CodedOutputStream.CreateInstance(rawOutput, bufferSize);
                    output.WriteRawVarint32((uint)value);
                    output.Flush();
                    TestUtil.AssertBytesEqual(data, rawOutput.ToArray());
                }

                {
                    MemoryStream      rawOutput = new MemoryStream();
                    CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
                    output.WriteRawVarint64(value);
                    output.Flush();
                    TestUtil.AssertBytesEqual(data, rawOutput.ToArray());
                }
            }
        }
예제 #22
0
        public void ExtensionWriterTestPacked()
        {
            TestPackedExtensionsLite msg = BuildPackedExtensions();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            TestPackedExtensionsLite.Builder copyBuilder =
                TestPackedExtensionsLite.CreateBuilder().MergeFrom(msg.ToByteArray(), registry);
            TestPackedExtensionsLite copy = copyBuilder.Build();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());

            AssertPackedExtensions(copy);
        }
예제 #23
0
        public void TestUnpackedFromPackedInput()
        {
            byte[] packedData = BuildPackedExtensions().ToByteArray();

            TestUnpackedTypesLite unpacked = TestUnpackedTypesLite.ParseFrom(packedData);
            TestPackedTypesLite   packed   = TestPackedTypesLite.ParseFrom(unpacked.ToByteArray());

            TestUtil.AssertBytesEqual(packedData, packed.ToByteArray());

            unpacked = TestUnpackedTypesLite.ParseFrom(packed.ToByteArray());

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);
            AssertPackedExtensions(TestPackedExtensionsLite.ParseFrom(unpacked.ToByteArray(), registry));
        }
        public void TestRecoverMissingExtensions()
        {
            const int optionalInt32 = 12345678;

            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            builder.SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32);
            builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.1);
            builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.2);
            builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.3);
            TestAllExtensions msg = builder.Build();

            Assert.IsTrue(msg.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
            Assert.AreEqual(3, msg.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));

            byte[]            bits = msg.ToByteArray();
            TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);

            Assert.IsFalse(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
            Assert.AreEqual(0, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
            Assert.AreNotEqual(msg, copy);

            //Even though copy does not understand the typees they serialize correctly
            byte[] copybits = copy.ToByteArray();
            TestUtil.AssertBytesEqual(bits, copybits);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestProtoFile.RegisterAllExtensions(registry);

            //Now we can take those copy bits and restore the full message with extensions
            copy = TestAllExtensions.ParseFrom(copybits, registry);
            Assert.IsTrue(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
            Assert.AreEqual(3, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));

            Assert.AreEqual(msg, copy);
            TestUtil.AssertBytesEqual(bits, copy.ToByteArray());

            //If we modify the object this should all continue to work as before
            copybits = copy.ToBuilder().Build().ToByteArray();
            TestUtil.AssertBytesEqual(bits, copybits);

            //If we replace extension the object this should all continue to work as before
            copybits = copy.ToBuilder()
                       .SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32)
                       .Build().ToByteArray();
            TestUtil.AssertBytesEqual(bits, copybits);
        }
예제 #25
0
        public void TestMergeFromCodedInputStream()
        {
            TestAllTypesLite copy,
                             msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalUint32(uint.MaxValue).Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            using (MemoryStream ms = new MemoryStream(msg.ToByteArray()))
            {
                CodedInputStream ci = CodedInputStream.CreateInstance(ms);
                copy = copy.ToBuilder().MergeFrom(ci).Build();
            }

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #26
0
        public void TestMessageLiteWriteDelimitedTo()
        {
            TestRequiredLite msg = TestRequiredLite.CreateBuilder()
                                   .SetD(42)
                                   .SetEn(ExtraEnum.EXLITE_BAZ)
                                   .Build();

            MemoryStream ms = new MemoryStream();

            msg.WriteDelimitedTo(ms);
            byte[] buffer = ms.ToArray();

            Assert.AreEqual(5, buffer.Length);
            Assert.AreEqual(4, buffer[0]);
            byte[] msgBytes = new byte[4];
            Array.Copy(buffer, 1, msgBytes, 0, 4);
            TestUtil.AssertBytesEqual(msg.ToByteArray(), msgBytes);
        }
예제 #27
0
        public void TestUnpackedAndPackedExtensions()
        {
            TestPackedExtensionsLite original = BuildPackedExtensions();

            AssertPackedExtensions(original);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);
            UnitTestExtrasLiteProtoFile.RegisterAllExtensions(registry);

            TestUnpackedExtensionsLite unpacked = TestUnpackedExtensionsLite.ParseFrom(original.ToByteArray(), registry);

            TestPackedExtensionsLite packed = TestPackedExtensionsLite.ParseFrom(unpacked.ToByteArray(), registry);

            Assert.AreEqual(original, packed);
            TestUtil.AssertBytesEqual(original.ToByteArray(), packed.ToByteArray());
            AssertPackedExtensions(packed);
        }
        /// <summary>
        /// Parses the given bytes using WriteRawLittleEndian64() and checks
        /// that the result matches the given value.
        /// </summary>
        private static void AssertWriteLittleEndian64(byte[] data, ulong value)
        {
            MemoryStream      rawOutput = new MemoryStream();
            CodedOutputStream output    = CodedOutputStream.CreateInstance(rawOutput);

            output.WriteRawLittleEndian64(value);
            output.Flush();
            TestUtil.AssertBytesEqual(data, rawOutput.ToArray());

            // Try different block sizes.
            for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
            {
                rawOutput = new MemoryStream();
                output    = CodedOutputStream.CreateInstance(rawOutput, blockSize);
                output.WriteRawLittleEndian64(value);
                output.Flush();
                TestUtil.AssertBytesEqual(data, rawOutput.ToArray());
            }
        }
예제 #29
0
        public void ExtensionWriterTestSetExtensionLists()
        {
            TestAllExtensionsLite msg, copy;

            TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedBoolExtensionLite, new[] { true, false })
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedCordExtensionLite, new[] { "123", "456" })
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedForeignEnumExtensionLite,
                                                                  new[] { ForeignEnumLite.FOREIGN_LITE_BAZ, ForeignEnumLite.FOREIGN_LITE_FOO })
            ;

            msg = builder.Build();
            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy = TestAllExtensionsLite.ParseFrom(msg.ToByteArray(), registry);
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());

            Assert.AreEqual(ForeignEnumLite.FOREIGN_LITE_FOO,
                            copy.GetExtension(UnitTestLiteProtoFile.RepeatedForeignEnumExtensionLite, 1));
        }
예제 #30
0
        public void TestAllTypesModifiedRoundTrip()
        {
            TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;

            msg.ToBuilder()
            .SetOptionalBool(true)
            .SetOptionalCord("Hi")
            .SetOptionalDouble(1.123)
            .SetOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_FOO)
            .SetOptionalForeignMessage(ForeignMessageLite.CreateBuilder().SetC('c').Build())
            .SetOptionalGroup(TestAllTypesLite.Types.OptionalGroup.CreateBuilder().SetA('a').Build())
            .SetOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR)
            .SetOptionalInt32(32)
            .SetOptionalInt64(64)
            .SetOptionalNestedEnum(TestAllTypesLite.Types.NestedEnum.FOO)
            .SetOptionalString("SetOptionalString")
            .AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('a').Build())
            .AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('A').Build())
            ;
            TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }