|
@@ -595,6 +595,95 @@ namespace Google.Protobuf.Collections
|
|
|
Assert.AreEqual(((SampleEnum)(-5)), values[5]);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
// Fairly perfunctory tests for the non-generic IList implementation
|
|
|
[Test]
|
|
|
public void IList_Indexer()
|