public void WrongExtensionTypeTreatedAsUnknown()
        {
            // Test that fields of the wrong wire type are treated like unknown fields
            // when parsing extensions.

            ByteString        bizarroData          = GetBizarroData();
            TestAllExtensions allExtensionsMessage = TestAllExtensions.ParseFrom(bizarroData);
            TestEmptyMessage  emptyMessage         = TestEmptyMessage.ParseFrom(bizarroData);

            // All fields should have been interpreted as unknown, so the debug strings
            // should be the same.
            Assert.AreEqual(emptyMessage.ToString(),
                            allExtensionsMessage.ToString());
        }
        public void ParseUnknownEnumValue()
        {
            FieldDescriptor singularField =
                TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("optional_nested_enum");
            FieldDescriptor repeatedField =
                TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("repeated_nested_enum");

            Assert.IsNotNull(singularField);
            Assert.IsNotNull(repeatedField);

            ByteString data =
                UnknownFieldSet.CreateBuilder()
                .AddField(singularField.FieldNumber,
                          UnknownField.CreateBuilder()
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.BAR)
                          .AddVarint(5)         // not valid
                          .Build())
                .AddField(repeatedField.FieldNumber,
                          UnknownField.CreateBuilder()
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.FOO)
                          .AddVarint(4)         // not valid
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.BAZ)
                          .AddVarint(6)         // not valid
                          .Build())
                .Build()
                .ToByteString();

            {
                TestAllTypes message = TestAllTypes.ParseFrom(data);
                Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                                message.OptionalNestedEnum);
                TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ },
                                     message.RepeatedNestedEnumList);
                TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList);
                TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList);
            }

            {
                TestAllExtensions message =
                    TestAllExtensions.ParseFrom(data, TestUtil.CreateExtensionRegistry());
                Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                                message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
                TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ },
                                     message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension));
                TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList);
                TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList);
            }
        }
Beispiel #3
0
        public void TestRecoverMissingExtensions()
        {
            const int optionalInt32 = 12345678;

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

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

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

            Assert.IsFalse(copy.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(0, copy.GetExtensionCount(Unittest.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();

            Unittest.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(Unittest.OptionalInt32Extension));
            Assert.AreEqual(3, copy.GetExtensionCount(Unittest.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(Unittest.OptionalInt32Extension, optionalInt32)
                       .Build().ToByteArray();
            Assert.AreEqual(bits, copybits);
        }
Beispiel #4
0
        public void ParseExtensions()
        {
            // TestAllTypes and TestAllExtensions should have compatible wire formats,
            // so if we serealize a TestAllTypes then parse it as TestAllExtensions
            // it should work.

            TestAllTypes message  = TestUtil.GetAllSet();
            ByteString   rawBytes = message.ToByteString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            TestUtil.RegisterAllExtensions(registry);
            registry = registry.AsReadOnly();

            TestAllExtensions message2 = TestAllExtensions.ParseFrom(rawBytes, registry);

            TestUtil.AssertAllExtensionsSet(message2);
        }