Ejemplo n.º 1
0
        public void TestPackedRepeatedFieldCollectionNotAllocatedWhenLengthExceedsBuffer()
        {
            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  codec  = FieldCodec.ForFixed32(tag);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteString("A long string");
            output.WriteTag(codec.Tag);
            output.WriteRawVarint32((uint)codec.FixedSize);
            // Note that there is no content for the packed field.
            // The field length exceeds the remaining length of content.
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);

            input.ReadTag();
            input.ReadString();
            input.ReadTag();
            var field = new RepeatedField <uint>();

            Assert.Throws <InvalidProtocolBufferException>(() => field.AddEntriesFrom(input, codec));

            // Collection was not pre-initialized
            Assert.AreEqual(0, field.Count);
        }
Ejemplo n.º 2
0
        public void TestPackedRepeatedFieldCollectionNonDivisibleLength()
        {
            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  codec  = FieldCodec.ForFixed32(tag);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteString("A long string");
            output.WriteTag(codec.Tag);
            output.WriteRawVarint32((uint)codec.FixedSize - 1); // Length not divisible by FixedSize
            output.WriteFixed32(uint.MaxValue);
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);

            input.ReadTag();
            input.ReadString();
            input.ReadTag();
            var field = new RepeatedField <uint>();

            Assert.Throws <InvalidProtocolBufferException>(() => field.AddEntriesFrom(input, codec));

            // Collection was not pre-initialized
            Assert.AreEqual(0, field.Count);
        }
Ejemplo n.º 3
0
        public void TestPackedRepeatedFieldCollectionNotAllocatedWhenLengthExceedsRemainingData()
        {
            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  codec  = FieldCodec.ForFixed32(tag);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteString("A long string");
            output.WriteTag(codec.Tag);
            output.WriteRawVarint32((uint)codec.FixedSize);
            // Note that there is no content for the packed field.
            // The field length exceeds the remaining length of the buffer.
            output.Flush();
            stream.Position = 0;

            var sequence = ReadOnlySequenceFactory.CreateWithContent(stream.ToArray());

            ParseContext.Initialize(sequence, out ParseContext ctx);

            ctx.ReadTag();
            ctx.ReadString();
            ctx.ReadTag();
            var field = new RepeatedField <uint>();

            try
            {
                field.AddEntriesFrom(ref ctx, codec);
                Assert.Fail();
            }
            catch (InvalidProtocolBufferException)
            {
            }

            // Collection was not pre-initialized
            Assert.AreEqual(0, field.Count);
        }