public void PackedParsingOfUnpackedInputExtensions()
        {
            byte[]            bytes    = TestUnpackedTypes.ParseFrom(TestUtil.GetPackedSet().ToByteArray()).ToByteArray();
            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestProtoFile.RegisterAllExtensions(registry);
            TestPackedExtensions message = TestPackedExtensions.ParseFrom(bytes, registry);

            TestUtil.AssertPackedExtensionsSet(message);
        }
Ejemplo n.º 2
0
        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();
            Assert.AreEqual(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);
            Assert.AreEqual(bits, copy.ToByteArray());

            //If we modify the object this should all continue to work as before
            copybits = copy.ToBuilder().Build().ToByteArray();
            Assert.AreEqual(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();
            Assert.AreEqual(bits, copybits);
        }
        public void ExtensionWriterTest()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder()
                                                .SetExtension(UnitTestProtoFile.DefaultBoolExtension, true)
                                                .SetExtension(UnitTestProtoFile.DefaultBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(UnitTestProtoFile.DefaultCordExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultDoubleExtension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultFixed32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultFixed64Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultFloatExtension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(UnitTestProtoFile.DefaultImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(UnitTestProtoFile.DefaultInt32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultInt64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(UnitTestProtoFile.DefaultSfixed32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSfixed64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSint32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultSint64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.DefaultStringExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultStringPieceExtension, "123")
                                                .SetExtension(UnitTestProtoFile.DefaultUint32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.DefaultUint64Extension, 123u)
                                                //Optional
                                                .SetExtension(UnitTestProtoFile.OptionalBoolExtension, true)
                                                .SetExtension(UnitTestProtoFile.OptionalBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(UnitTestProtoFile.OptionalCordExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalDoubleExtension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalFixed32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalFixed64Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalFloatExtension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(UnitTestProtoFile.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalInt64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(UnitTestProtoFile.OptionalSfixed32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSfixed64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSint32Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalSint64Extension, 123)
                                                .SetExtension(UnitTestProtoFile.OptionalStringExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalStringPieceExtension, "123")
                                                .SetExtension(UnitTestProtoFile.OptionalUint32Extension, 123u)
                                                .SetExtension(UnitTestProtoFile.OptionalUint64Extension, 123u)
                                                //Repeated
                                                .AddExtension(UnitTestProtoFile.RepeatedBoolExtension, true)
                                                .AddExtension(UnitTestProtoFile.RepeatedBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .AddExtension(UnitTestProtoFile.RepeatedCordExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedFixed32Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedFixed64Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedFloatExtension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .AddExtension(UnitTestProtoFile.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedInt64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .AddExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSint32Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedSint64Extension, 123)
                                                .AddExtension(UnitTestProtoFile.RepeatedStringExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedStringPieceExtension, "123")
                                                .AddExtension(UnitTestProtoFile.RepeatedUint32Extension, 123u)
                                                .AddExtension(UnitTestProtoFile.RepeatedUint64Extension, 123u)
            ;
            TestAllExtensions msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestProtoFile.RegisterAllExtensions(registry);

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

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

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.DefaultBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(UnitTestProtoFile.DefaultBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultCordExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.DefaultImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(UnitTestProtoFile.DefaultNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.DefaultSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultStringExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.DefaultStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.DefaultUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.OptionalBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(UnitTestProtoFile.OptionalBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalCordExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.OptionalImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.OptionalSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalStringExtension));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.OptionalStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.OptionalUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(UnitTestProtoFile.RepeatedBoolExtension, 0));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedFloatExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ,
                            copy.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedInt32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedInt64Extension, 0));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSint32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestProtoFile.RepeatedSint64Extension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedStringExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedUint32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestProtoFile.RepeatedUint64Extension, 0));
        }