mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-04 05:25:00 +00:00
Remove duplicate test that doesn't verify exception case
Removed Rar_StreamValidation_CorrectExceptionBehavior test as it duplicated the validation in Rar_StreamValidation_OnlyThrowsOnPrematureEnd without actually testing the exception case. The remaining test adequately validates that the fix works correctly across multiple RAR formats. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
This commit is contained in:
@@ -680,53 +680,4 @@ public class RarArchiveTests : ArchiveTests
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test that InvalidOperationException is correctly thrown only when decompression
|
||||
/// ends prematurely (position < expected length), not when position >= expected length.
|
||||
/// This verifies the fix where the condition was changed from (_position != Length)
|
||||
/// to (_position < Length).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Rar_StreamValidation_CorrectExceptionBehavior()
|
||||
{
|
||||
// This test documents the expected exception behavior:
|
||||
// - OLD behavior: Threw when _position != Length (including position > Length)
|
||||
// This incorrectly threw exceptions for valid RAR files where actual size > header size
|
||||
// - NEW behavior: Only throws when _position < Length (premature termination)
|
||||
// This correctly allows files where actual size >= header size
|
||||
|
||||
var testFile = "Rar.rar";
|
||||
using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testFile));
|
||||
using var archive = RarArchive.Open(stream);
|
||||
|
||||
var entry = archive.Entries.FirstOrDefault(e => !e.IsDirectory);
|
||||
Assert.NotNull(entry);
|
||||
|
||||
using var entryStream = entry!.OpenEntryStream();
|
||||
|
||||
// Read all data from the stream completely
|
||||
var buffer = new byte[4096];
|
||||
long totalBytesRead = 0;
|
||||
int bytesRead;
|
||||
while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
totalBytesRead += bytesRead;
|
||||
}
|
||||
|
||||
// Verify we read data
|
||||
Assert.True(totalBytesRead > 0, "Should have read some data from the entry");
|
||||
|
||||
// After reading to EOF, further reads should return 0 without throwing
|
||||
// This is the key fix: the old code would throw if position != expected length,
|
||||
// but the new code only throws if position < expected length
|
||||
bytesRead = entryStream.Read(buffer, 0, buffer.Length);
|
||||
Assert.Equal(0, bytesRead);
|
||||
|
||||
// Note: To test the actual exception case (position < Length), we would need
|
||||
// a truncated or corrupted RAR file where the unpacker stops before reaching
|
||||
// the expected length. Such a file would trigger:
|
||||
// throw new InvalidOperationException($"unpacked file size does not match header: expected {Length} found {_position}")
|
||||
// when count > 0 && outTotal == 0 && _position < Length
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user