Helper methods to create sample instances of types generated from unit test messages.
コード例 #1
0
        public void WriteContext_WritesWithFlushes()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            MemoryStream      expectedOutput = new MemoryStream();
            CodedOutputStream output         = new CodedOutputStream(expectedOutput);

            output.WriteMessage(message);
            output.Flush();
            byte[] expectedBytes1 = expectedOutput.ToArray();

            output.WriteMessage(message);
            output.Flush();
            byte[] expectedBytes2 = expectedOutput.ToArray();

            var bufferWriter = new TestArrayBufferWriter <byte>();

            WriteContext.Initialize(bufferWriter, out WriteContext ctx);
            ctx.WriteMessage(message);
            ctx.Flush();
            Assert.AreEqual(expectedBytes1, bufferWriter.WrittenSpan.ToArray());

            ctx.WriteMessage(message);
            ctx.Flush();
            Assert.AreEqual(expectedBytes2, bufferWriter.WrittenSpan.ToArray());
        }
コード例 #2
0
ファイル: GeneratedMessageTest.cs プロジェクト: junfff/mygame
 public void DiscardUnknownFields_AllTypes()
 {
     // Simple way of ensuring we can skip all kinds of fields.
     var data  = SampleMessages.CreateFullTestAllTypes().ToByteArray();
     var empty = Empty.Parser.ParseFrom(data);
     // TODO(jieluo): Add test back when DiscardUnknownFields API is supported.
     // Assert.AreNotEqual(new Empty(), empty);
 }
コード例 #3
0
        /// <returns>A serialized big message</returns>
        private static byte[] GenerateBigSerializedMessage()
        {
            byte[]       value   = new byte[16 * 1024 * 1024];
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            message.SingleBytes = ByteString.CopyFrom(value);
            return(message.ToByteArray());
        }
コード例 #4
0
        public void IgnoreUnknownFields_AllTypes()
        {
            // Simple way of ensuring we can skip all kinds of fields.
            var data  = SampleMessages.CreateFullTestAllTypes().ToByteArray();
            var empty = Empty.Parser.ParseFrom(data);

            Assert.AreEqual(new Empty(), empty);
        }
コード例 #5
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(SampleMessages.CreateFullTestAllTypes().ToByteArray());
            CodedInputStream input = CodedInputStream.CreateWithLimits(ms, 16, 100);

            Assert.Throws <InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(input));
        }
コード例 #6
0
        public void TestHashCode()
        {
            var message      = SampleMessages.CreateFullTestAllTypes();
            var emptyMessage = new TestEmptyMessage();
            int hashCode     = emptyMessage.GetHashCode();

            emptyMessage.MergeFrom(message.ToByteArray());
            Assert.AreNotEqual(hashCode, emptyMessage.GetHashCode());
        }
コード例 #7
0
        public void TestEquals()
        {
            var message           = SampleMessages.CreateFullTestAllTypes();
            var emptyMessage      = new TestEmptyMessage();
            var otherEmptyMessage = new TestEmptyMessage();

            Assert.AreEqual(emptyMessage, otherEmptyMessage);
            emptyMessage.MergeFrom(message.ToByteArray());
            Assert.AreNotEqual(emptyMessage.CalculateSize(),
                               otherEmptyMessage.CalculateSize());
            Assert.AreNotEqual(emptyMessage, otherEmptyMessage);
        }
コード例 #8
0
ファイル: GeneratedMessageTest.cs プロジェクト: junfff/mygame
        public void ExtraEndGroupThrows()
        {
            var message = SampleMessages.CreateFullTestAllTypes();
            var stream  = new MemoryStream();
            var output  = new CodedOutputStream(stream);

            output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32);
            output.WriteFixed32(123);
            output.WriteTag(100, WireFormat.WireType.EndGroup);

            output.Flush();

            stream.Position = 0;
            Assert.Throws <IOException>(() => TestAllTypes.Parser.ParseFrom(stream));
        }
コード例 #9
0
        public void TestMergeCodedInput()
        {
            var message      = SampleMessages.CreateFullTestAllTypes();
            var emptyMessage = new TestEmptyMessage();

            emptyMessage.MergeFrom(message.ToByteArray());
            Assert.AreEqual(message.CalculateSize(), emptyMessage.CalculateSize());
            Assert.AreEqual(message.ToByteArray(), emptyMessage.ToByteArray());

            var newMessage = new TestAllTypes();

            newMessage.MergeFrom(emptyMessage.ToByteArray());
            Assert.AreEqual(message, newMessage);
            Assert.AreEqual(message.CalculateSize(), newMessage.CalculateSize());
        }
コード例 #10
0
        public void ExtraEndGroupThrows()
        {
            var message = SampleMessages.CreateFullTestAllTypes();
            var stream  = new MemoryStream();
            var output  = new CodedOutputStream(stream);

            output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32);
            output.WriteFixed32(123);
            output.WriteTag(100, WireFormat.WireType.EndGroup);

            output.Flush();

            stream.Position = 0;
            MessageParsingHelpers.AssertReadingMessageThrows <TestAllTypes, InvalidProtocolBufferException>(TestAllTypes.Parser, stream.ToArray());
        }
コード例 #11
0
        public void WriteWholeMessage_VaryingBlockSizes()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            byte[] rawBytes = message.ToByteArray();

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    = new CodedOutputStream(rawOutput, blockSize);
                message.WriteTo(output);
                output.Flush();
                Assert.AreEqual(rawBytes, rawOutput.ToArray());
            }
        }
コード例 #12
0
        public void ReadWholeMessage_VaryingBlockSizes_FromSequence()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

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

            Assert.AreEqual(message, message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.Parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(rawBytes, blockSize));
                Assert.AreEqual(message, message2);
            }
        }
コード例 #13
0
        public void ReadWholeMessage_VaryingBlockSizes()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

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

            Assert.AreEqual(message, message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.Parser.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
                Assert.AreEqual(message, message2);
            }
        }
コード例 #14
0
        public void TestDiscardUnknownFields()
        {
            var message            = SampleMessages.CreateFullTestAllTypes();
            var goldenEmptyMessage = new TestEmptyMessage();

            byte[] data     = message.ToByteArray();
            int    fullSize = message.CalculateSize();

            Action <IMessage> assertEmpty = msg =>
            {
                Assert.AreEqual(0, msg.CalculateSize());
                Assert.AreEqual(goldenEmptyMessage, msg);
            };

            Action <IMessage> assertFull = msg => Assert.AreEqual(fullSize, msg.CalculateSize());

            // Test the behavior of the parsers with and without discarding, both generic and non-generic.
            MessageParser <TestEmptyMessage> retainingParser1 = TestEmptyMessage.Parser;
            MessageParser retainingParser2 = retainingParser1;
            MessageParser <TestEmptyMessage> discardingParser1 = retainingParser1.WithDiscardUnknownFields(true);
            MessageParser discardingParser2 = retainingParser2.WithDiscardUnknownFields(true);

            // Test parse from byte[]
            assertFull(retainingParser1.ParseFrom(data));
            assertFull(retainingParser2.ParseFrom(data));
            assertEmpty(discardingParser1.ParseFrom(data));
            assertEmpty(discardingParser2.ParseFrom(data));

            // Test parse from byte[] with offset
            assertFull(retainingParser1.ParseFrom(data, 0, data.Length));
            assertFull(retainingParser2.ParseFrom(data, 0, data.Length));
            assertEmpty(discardingParser1.ParseFrom(data, 0, data.Length));
            assertEmpty(discardingParser2.ParseFrom(data, 0, data.Length));

            // Test parse from CodedInputStream
            assertFull(retainingParser1.ParseFrom(new CodedInputStream(data)));
            assertFull(retainingParser2.ParseFrom(new CodedInputStream(data)));
            assertEmpty(discardingParser1.ParseFrom(new CodedInputStream(data)));
            assertEmpty(discardingParser2.ParseFrom(new CodedInputStream(data)));

            // Test parse from Stream
            assertFull(retainingParser1.ParseFrom(new MemoryStream(data)));
            assertFull(retainingParser2.ParseFrom(new MemoryStream(data)));
            assertEmpty(discardingParser1.ParseFrom(new MemoryStream(data)));
            assertEmpty(discardingParser2.ParseFrom(new MemoryStream(data)));
        }
コード例 #15
0
        public void IgnoreUnknownFields_RealDataStillRead()
        {
            var message           = SampleMessages.CreateFullTestAllTypes();
            var stream            = new MemoryStream();
            var output            = new CodedOutputStream(stream);
            var unusedFieldNumber = 23456;

            Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber));
            output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteString("ignore me");
            message.WriteTo(output);
            output.Flush();

            stream.Position = 0;
            var parsed = TestAllTypes.Parser.ParseFrom(stream);

            Assert.AreEqual(message, parsed);
        }
コード例 #16
0
        public void ExtraEndGroupSkipped()
        {
            var message = SampleMessages.CreateFullTestAllTypes();
            var stream  = new MemoryStream();
            var output  = new CodedOutputStream(stream);

            output.WriteTag(100, WireFormat.WireType.EndGroup);
            output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32);
            output.WriteFixed32(123);

            output.Flush();

            stream.Position = 0;
            var parsed = TestAllTypes.Parser.ParseFrom(stream);

            Assert.AreEqual(new TestAllTypes {
                SingleFixed32 = 123
            }, parsed);
        }
コード例 #17
0
        public void DiscardUnknownFields_RealDataStillRead()
        {
            var message           = SampleMessages.CreateFullTestAllTypes();
            var stream            = new MemoryStream();
            var output            = new CodedOutputStream(stream);
            var unusedFieldNumber = 23456;

            Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber));
            output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteString("ignore me");
            message.WriteTo(output);
            output.Flush();

            MessageParsingHelpers.AssertReadingMessage(
                TestAllTypes.Parser,
                stream.ToArray(),
                parsed =>
            {
                // TODO(jieluo): Add test back when DiscardUnknownFields API is supported.
                // Assert.AreEqual(message, parsed);
            });
        }
コード例 #18
0
 public void AllTypesRoundtrip()
 {
     AssertRoundtrip(SampleMessages.CreateFullTestAllTypes());
 }