public void Clear()
        {
            AbstractMessageWrapper message =
                new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder(TestUtil.GetAllSet())).Clear().Build();

            TestUtil.AssertClear((TestAllTypes)message.WrappedMessage);
        }
Esempio n. 2
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. 3
0
        public void ReadHugeBlob()
        {
            // Allocate and initialize a 1MB blob.
            byte[] blob = new byte[1 << 20];
            for (int i = 0; i < blob.Length; i++)
            {
                blob[i] = (byte)i;
            }

            // Make a message containing it.
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            builder.SetOptionalBytes(ByteString.CopyFrom(blob));
            TestAllTypes message = builder.Build();

            // Serialize and parse it.  Make sure to parse from an InputStream, not
            // directly from a ByteString, so that CodedInputStream uses buffered
            // reading.
            TestAllTypes message2 = TestAllTypes.ParseFrom(message.ToByteString().CreateCodedInput());

            Assert.AreEqual(message.OptionalBytes, message2.OptionalBytes);

            // Make sure all the other fields were parsed correctly.
            TestAllTypes message3 = TestAllTypes.CreateBuilder(message2)
                                    .SetOptionalBytes(TestUtil.GetAllSet().OptionalBytes)
                                    .Build();

            TestUtil.AssertAllFieldsSet(message3);
        }
 public void PrintMessage()
 {
     TestUtil.TestInMultipleCultures(() => {
         string text = TextFormat.PrintToString(TestUtil.GetAllSet());
         Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
     });
 }
        public void SerializedSize()
        {
            TestAllTypes message         = TestUtil.GetAllSet();
            IMessage     abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet());

            Assert.AreEqual(message.SerializedSize, abstractMessage.SerializedSize);
        }
        public void Serialization()
        {
            IMessage abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet());

            TestUtil.AssertAllFieldsSet(TestAllTypes.ParseFrom(abstractMessage.ToByteString()));
            Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), abstractMessage.ToByteString());
        }
Esempio n. 7
0
        public void ManyMessagesShouldNotTriggerSizeAlert()
        {
            int messageSize = TestUtil.GetAllSet().SerializedSize;
            // Enough messages to trigger the alert unless we've reset the size
            // Note that currently we need to make this big enough to copy two whole buffers,
            // as otherwise when we refill the buffer the second type, the alert triggers instantly.
            int correctCount = (CodedInputStream.BufferSize * 2) / messageSize + 1;

            using (MemoryStream stream = new MemoryStream())
            {
                MessageStreamWriter <TestAllTypes> writer = new MessageStreamWriter <TestAllTypes>(stream);
                for (int i = 0; i < correctCount; i++)
                {
                    writer.Write(TestUtil.GetAllSet());
                }
                writer.Flush();

                stream.Position = 0;

                int count = 0;
                foreach (var message in MessageStreamIterator <TestAllTypes> .FromStreamProvider(() => stream)
                         .WithSizeLimit(CodedInputStream.BufferSize * 2))
                {
                    count++;
                    TestUtil.AssertAllFieldsSet(message);
                }
                Assert.AreEqual(correctCount, count);
            }
        }
        public void Parsing()
        {
            IBuilder builder = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder());
            AbstractMessageWrapper message =
                (AbstractMessageWrapper)builder.WeakMergeFrom(TestUtil.GetAllSet().ToByteString()).WeakBuild();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
 public void SetUp()
 {
     descriptor    = TestAllTypes.Descriptor;
     allFields     = TestUtil.GetAllSet();
     allFieldsData = allFields.ToByteString();
     emptyMessage  = TestEmptyMessage.ParseFrom(allFieldsData);
     unknownFields = emptyMessage.UnknownFields;
 }
Esempio n. 10
0
 public void PrintBuilder()
 {
     TestUtil.TestInMultipleCultures(() =>
     {
         string messageText = TextFormat.PrintToString(TestUtil.GetAllSet());
         string builderText = TextFormat.PrintToString(TestUtil.GetAllSet().ToBuilder());
         Assert.AreEqual(messageText, builderText);
     });
 }
Esempio n. 11
0
        public void SizeLimit()
        {
            // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
            // apply to the latter case.
            MemoryStream     ms    = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
            CodedInputStream input = CodedInputStream.CreateInstance(ms);

            input.SetSizeLimit(16);

            Assert.Throws <InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input));
        }
Esempio n. 12
0
        public void DynamicMessageSerializedSize()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
            IMessage dynamicMessage = dynamicBuilder.WeakBuild();

            Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
        }
Esempio n. 13
0
        public void Serialization()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            ByteString rawBytes = message.ToByteString();

            Assert.AreEqual(rawBytes.Length, message.SerializedSize);

            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);
        }
        public void EqualsAndHashCode()
        {
            TestAllTypes      a = TestUtil.GetAllSet();
            TestAllTypes      b = TestAllTypes.CreateBuilder().Build();
            TestAllTypes      c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
            TestAllTypes      d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
            TestAllExtensions e = TestUtil.GetAllExtensionsSet();
            TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
                                  .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();

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

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(a, f);

            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(b, f);

            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(c, f);

            CheckNotEqual(d, e);
            CheckNotEqual(d, f);

            CheckNotEqual(e, f);

            // Deserializing into the TestEmptyMessage such that every field is an UnknownFieldSet.Field
            TestEmptyMessage eUnknownFields = TestEmptyMessage.ParseFrom(e.ToByteArray());
            TestEmptyMessage fUnknownFields = TestEmptyMessage.ParseFrom(f.ToByteArray());

            CheckNotEqual(eUnknownFields, fUnknownFields);
            CheckEqualsIsConsistent(eUnknownFields);
            CheckEqualsIsConsistent(fUnknownFields);

            // Subseqent reconstitutions should be identical
            TestEmptyMessage eUnknownFields2 = TestEmptyMessage.ParseFrom(e.ToByteArray());

            CheckEqualsIsConsistent(eUnknownFields, eUnknownFields2);
        }
Esempio n. 15
0
        public void DynamicMessageSerialization()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            ByteString   rawBytes = message.ToByteString();
            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);

            // In fact, the serialized forms should be exactly the same, byte-for-byte.
            Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
        }
        public void SizeLimit()
        {
            // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
            // apply to the latter case.
            MemoryStream     ms    = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
            CodedInputStream input = CodedInputStream.CreateInstance(ms);

            input.SetSizeLimit(16);

            try {
                TestAllTypes.ParseFrom(input);
                Assert.Fail("Should have thrown an exception!");
            } catch (InvalidProtocolBufferException) {
                // success.
            }
        }
Esempio n. 17
0
        public void ReadWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            Assert.AreEqual(rawBytes.Length, message.SerializedSize);
            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
                TestUtil.AssertAllFieldsSet(message2);
            }
        }
Esempio n. 18
0
        public void SerializeDelimited()
        {
            MemoryStream stream = new MemoryStream();

            TestUtil.GetAllSet().WriteDelimitedTo(stream);
            stream.WriteByte(12);
            TestUtil.GetPackedSet().WriteDelimitedTo(stream);
            stream.WriteByte(34);

            stream.Position = 0;

            TestUtil.AssertAllFieldsSet(TestAllTypes.ParseDelimitedFrom(stream));
            Assert.AreEqual(12, stream.ReadByte());
            TestUtil.AssertPackedFieldsSet(TestPackedTypes.ParseDelimitedFrom(stream));
            Assert.AreEqual(34, stream.ReadByte());
            Assert.AreEqual(-1, stream.ReadByte());
        }
Esempio n. 19
0
        public void WriteWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    =
                    CodedOutputStream.CreateInstance(rawOutput, blockSize);
                message.WriteTo(output);
                output.Flush();
                TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray());
            }
        }
Esempio n. 20
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);
        }
        public void EqualsAndHashCode()
        {
            TestAllTypes      a = TestUtil.GetAllSet();
            TestAllTypes      b = TestAllTypes.CreateBuilder().Build();
            TestAllTypes      c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
            TestAllTypes      d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
            TestAllExtensions e = TestUtil.GetAllExtensionsSet();
            TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
                                  .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();

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

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(a, f);

            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(b, f);

            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(c, f);

            CheckNotEqual(d, e);
            CheckNotEqual(d, f);

            CheckNotEqual(e, f);
        }
Esempio n. 22
0
 public void ExtensionsSerializedSize()
 {
     Assert.AreEqual(TestUtil.GetAllSet().SerializedSize, TestUtil.GetAllExtensionsSet().SerializedSize);
 }
        public void Copy()
        {
            AbstractMessageWrapper message =
                new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder()).MergeFrom(TestUtil.GetAllSet()).Build();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
Esempio n. 24
0
 public void ExtensionsSerializedSize()
 {
     Assert.IsTrue(TestUtil.GetAllSet().SerializedSize < TestUtil.GetAllExtensionsSet().SerializedSize);
 }