Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify tests and exception message #42363

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void AssertDecodeFinished()
{
if (_streamFooterLength > 0 && !_processedFooter)
{
throw Errors.InvalidStructuredMessage("Missing or incomplete trailer.");
throw Errors.InvalidStructuredMessage("Premature end of stream.");
}
_processedFooter = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,21 @@ public void BadStreamWrongContentLength()
Assert.That(async () => await CopyStream(decodingStream, Stream.Null), Throws.InnerException.TypeOf<InvalidDataException>());
}

[Test]
public void BadStreamWrongSegmentCount()
[TestCase(-1)]
[TestCase(1)]
public void BadStreamWrongSegmentCount(int difference)
{
byte[] originalData = new byte[1024];
const int dataSize = 1024;
const int segmentSize = 256;
const int numSegments = 4;

byte[] originalData = new byte[dataSize];
new Random().NextBytes(originalData);
byte[] encodedData = StructuredMessageHelper.MakeEncodedData(originalData, 256, Flags.StorageCrc64);
byte[] encodedData = StructuredMessageHelper.MakeEncodedData(originalData, segmentSize, Flags.StorageCrc64);

BinaryPrimitives.WriteInt16LittleEndian(new Span<byte>(encodedData, V1_0.StreamHeaderSegmentCountOffset, 2), 123);
// rewrite the segment count to be different than the actual number of segments
BinaryPrimitives.WriteInt16LittleEndian(
new Span<byte>(encodedData, V1_0.StreamHeaderSegmentCountOffset, 2), (short)(numSegments + difference));

Stream decodingStream = new StructuredMessageDecodingStream(new MemoryStream(encodedData));
Assert.That(async () => await CopyStream(decodingStream, Stream.Null), Throws.InnerException.TypeOf<InvalidDataException>());
Expand Down
Loading