public void EqualsAndHashCode()
        {
            UnknownField fixed32Field         = UnknownField.CreateBuilder().AddFixed32(1).Build();
            UnknownField fixed64Field         = UnknownField.CreateBuilder().AddFixed64(1).Build();
            UnknownField varIntField          = UnknownField.CreateBuilder().AddVarint(1).Build();
            UnknownField lengthDelimitedField =
                UnknownField.CreateBuilder().AddLengthDelimited(ByteString.Empty).Build();
            UnknownField groupField = UnknownField.CreateBuilder().AddGroup(unknownFields).Build();

            UnknownFieldSet a = UnknownFieldSet.CreateBuilder().AddField(1, fixed32Field).Build();
            UnknownFieldSet b = UnknownFieldSet.CreateBuilder().AddField(1, fixed64Field).Build();
            UnknownFieldSet c = UnknownFieldSet.CreateBuilder().AddField(1, varIntField).Build();
            UnknownFieldSet d = UnknownFieldSet.CreateBuilder().AddField(1, lengthDelimitedField).Build();
            UnknownFieldSet e = UnknownFieldSet.CreateBuilder().AddField(1, groupField).Build();

            CheckEqualsIsConsistent(a);
            CheckEqualsIsConsistent(b);
            CheckEqualsIsConsistent(c);
            CheckEqualsIsConsistent(d);
            CheckEqualsIsConsistent(e);

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(d, e);
        }
        public void Clear()
        {
            UnknownFieldSet fields =
                UnknownFieldSet.CreateBuilder().MergeFrom(unknownFields).Clear().Build();

            Assert.AreEqual(0, fields.FieldDictionary.Count);
        }
Esempio n. 3
0
 public override Builder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry)
 {
     UnknownFieldSet.Builder unknownFieldsBuilder = UnknownFieldSet.CreateBuilder(unknownFields);
     unknownFieldsBuilder.MergeFrom(input, extensionRegistry, this);
     unknownFields = unknownFieldsBuilder.Build();
     return(this);
 }
        public void MergeFrom()
        {
            TestEmptyMessage source =
                TestEmptyMessage.CreateBuilder()
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(2,
                              UnknownField.CreateBuilder()
                              .AddVarint(2).Build())
                    .AddField(3,
                              UnknownField.CreateBuilder()
                              .AddVarint(4).Build())
                    .Build())
                .Build();
            TestEmptyMessage destination =
                TestEmptyMessage.CreateBuilder()
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(1,
                              UnknownField.CreateBuilder()
                              .AddVarint(1).Build())
                    .AddField(3,
                              UnknownField.CreateBuilder()
                              .AddVarint(3).Build())
                    .Build())
                .MergeFrom(source)
                .Build();

            Assert.AreEqual(
                "1: 1\n" +
                "2: 2\n" +
                "3: 3\n" +
                "3: 4\n",
                destination.ToString());
        }
Esempio n. 5
0
        public void SkipWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();

            // Create two parallel inputs.  Parse one as unknown fields while using
            // skipField() to skip each field on the other.  Expect the same tags.
            CodedInputStream input1 = CodedInputStream.CreateInstance(rawBytes);
            CodedInputStream input2 = CodedInputStream.CreateInstance(rawBytes);

            UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder();

            uint   tag;
            string name;

            while (input1.ReadTag(out tag, out name))
            {
                uint tag2;
                Assert.IsTrue(input2.ReadTag(out tag2, out name));
                Assert.AreEqual(tag, tag2);

                unknownFields.MergeFieldFrom(tag, input1);
                input2.SkipField();
            }
        }
Esempio n. 6
0
 public virtual TBuilder MergeUnknownFields(UnknownFieldSet unknownFields)
 {
     UnknownFields = UnknownFieldSet.CreateBuilder(UnknownFields)
                     .MergeFrom(unknownFields)
                     .Build();
     return(ThisBuilder);
 }
Esempio n. 7
0
 public virtual TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry)
 {
     UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder(UnknownFields);
     unknownFields.MergeFrom(input, extensionRegistry, this);
     UnknownFields = unknownFields.Build();
     return(ThisBuilder);
 }
 public override TBuilder MergeUnknownFields(UnknownFieldSet unknownFields)
 {
     if (unknownFields != UnknownFieldSet.DefaultInstance)
     {
         TMessage result = MessageBeingBuilt;
         result.SetUnknownFields(UnknownFieldSet.CreateBuilder(result.UnknownFields)
                                 .MergeFrom(unknownFields)
                                 .Build());
     }
     return(ThisBuilder);
 }
        /**
         * Asserts that the given field sets are equal and have identical hash codes.
         */

        private static void CheckEqualsIsConsistent(UnknownFieldSet set)
        {
            // Object should be equal to itself.
            Assert.AreEqual(set, set);

            // Object should be equal to a copy of itself.
            UnknownFieldSet copy = UnknownFieldSet.CreateBuilder(set).Build();

            Assert.AreEqual(set, copy);
            Assert.AreEqual(copy, set);
            Assert.AreEqual(set.GetHashCode(), copy.GetHashCode());
        }
Esempio n. 10
0
        public void PrintUnknownFields()
        {
            TestEmptyMessage message =
                TestEmptyMessage.CreateBuilder()
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(5,
                              UnknownField.CreateBuilder()
                              .AddVarint(1)
                              .AddFixed32(2)
                              .AddFixed64(3)
                              .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
                              .AddGroup(
                                  UnknownFieldSet.CreateBuilder()
                                  .AddField(10,
                                            UnknownField.CreateBuilder()
                                            .AddVarint(5)
                                            .Build())
                                  .Build())
                              .Build())
                    .AddField(8,
                              UnknownField.CreateBuilder()
                              .AddVarint(1)
                              .AddVarint(2)
                              .AddVarint(3)
                              .Build())
                    .AddField(15,
                              UnknownField.CreateBuilder()
                              .AddVarint(0xABCDEF1234567890L)
                              .AddFixed32(0xABCD1234)
                              .AddFixed64(0xABCDEF1234567890L)
                              .Build())
                    .Build())
                .Build();

            Assert.AreEqual(
                "5: 1\n" +
                "5: 0x00000002\n" +
                "5: 0x0000000000000003\n" +
                "5: \"4\"\n" +
                "5 {\n" +
                "  10: 5\n" +
                "}\n" +
                "8: 1\n" +
                "8: 2\n" +
                "8: 3\n" +
                "15: 12379813812177893520\n" +
                "15: 0xabcd1234\n" +
                "15: 0xabcdef1234567890\n",
                TextFormat.PrintToString(message));
        }
        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);
            }
        }
        public void LargeVarint()
        {
            ByteString data =
                UnknownFieldSet.CreateBuilder()
                .AddField(1,
                          UnknownField.CreateBuilder()
                          .AddVarint(0x7FFFFFFFFFFFFFFFL)
                          .Build())
                .Build()
                .ToByteString();
            UnknownFieldSet parsed = UnknownFieldSet.ParseFrom(data);
            UnknownField    field  = parsed[1];

            Assert.AreEqual(1, field.VarintList.Count);
            Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, field.VarintList[0]);
        }
Esempio n. 13
0
        public void SerializeMessageSet()
        {
            // Set up a TestMessageSet with two known messages and an unknown one.
            TestMessageSet messageSet =
                TestMessageSet.CreateBuilder()
                .SetExtension(
                    TestMessageSetExtension1.MessageSetExtension,
                    TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
                .SetExtension(
                    TestMessageSetExtension2.MessageSetExtension,
                    TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(UnknownTypeId,
                              UnknownField.CreateBuilder()
                              .AddLengthDelimited(ByteString.CopyFromUtf8("bar"))
                              .Build())
                    .Build())
                .Build();

            ByteString data = messageSet.ToByteString();

            // Parse back using RawMessageSet and check the contents.
            RawMessageSet raw = RawMessageSet.ParseFrom(data);

            Assert.AreEqual(0, raw.UnknownFields.FieldDictionary.Count);

            Assert.AreEqual(3, raw.ItemCount);
            Assert.AreEqual(TypeId1, raw.ItemList[0].TypeId);
            Assert.AreEqual(TypeId2, raw.ItemList[1].TypeId);
            Assert.AreEqual(UnknownTypeId, raw.ItemList[2].TypeId);

            TestMessageSetExtension1 message1 = TestMessageSetExtension1.ParseFrom(raw.GetItem(0).Message.ToByteArray());

            Assert.AreEqual(123, message1.I);

            TestMessageSetExtension2 message2 = TestMessageSetExtension2.ParseFrom(raw.GetItem(1).Message.ToByteArray());

            Assert.AreEqual("foo", message2.Str);

            Assert.AreEqual("bar", raw.GetItem(2).Message.ToStringUtf8());
        }
        public void ParseKnownAndUnknown()
        {
            // Test mixing known and unknown fields when parsing.

            UnknownFieldSet fields =
                UnknownFieldSet.CreateBuilder(unknownFields)
                .AddField(123456,
                          UnknownField.CreateBuilder().AddVarint(654321).Build())
                .Build();

            ByteString   data        = fields.ToByteString();
            TestAllTypes destination = TestAllTypes.ParseFrom(data);

            TestUtil.AssertAllFieldsSet(destination);
            Assert.AreEqual(1, destination.UnknownFields.FieldDictionary.Count);

            UnknownField field = destination.UnknownFields[123456];

            Assert.AreEqual(1, field.VarintList.Count);
            Assert.AreEqual(654321, (long)field.VarintList[0]);
        }
        /// <summary>
        /// Constructs a protocol buffer which contains fields with all the same
        /// numbers as allFieldsData except that each field is some other wire
        /// type.
        /// </summary>
        private ByteString GetBizarroData()
        {
            UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.CreateBuilder();

            UnknownField varintField  = UnknownField.CreateBuilder().AddVarint(1).Build();
            UnknownField fixed32Field = UnknownField.CreateBuilder().AddFixed32(1).Build();

            foreach (KeyValuePair <int, UnknownField> entry in unknownFields.FieldDictionary)
            {
                if (entry.Value.VarintList.Count == 0)
                {
                    // Original field is not a varint, so use a varint.
                    bizarroFields.AddField(entry.Key, varintField);
                }
                else
                {
                    // Original field *is* a varint, so use something else.
                    bizarroFields.AddField(entry.Key, fixed32Field);
                }
            }

            return(bizarroFields.Build().ToByteString());
        }
Esempio n. 16
0
        public void ToBuilder()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            reflectionTester.SetAllFieldsViaReflection(builder);
            int   unknownFieldNum = 9;
            ulong unknownFieldVal = 90;

            builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
                                     .AddField(unknownFieldNum,
                                               UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
                                     .Build());
            DynamicMessage message = builder.Build();

            DynamicMessage derived = message.ToBuilder().Build();

            reflectionTester.AssertAllFieldsSetViaReflection(derived);

            IList <ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;

            Assert.AreEqual(1, values.Count);
            Assert.AreEqual(unknownFieldVal, values[0]);
        }