Add tests for compressed TAR detection and fix stream disposal

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-11 14:07:01 +00:00
parent c8ca687dc2
commit e55af11800
2 changed files with 44 additions and 1 deletions

View File

@@ -54,7 +54,7 @@ public class TarArchive : AbstractWritableArchive<TarArchiveEntry, TarVolume>
{
// Open the file to check for compression
using var testStream = fileInfo.OpenRead();
var rewindableStream = SharpCompressStream.Create(testStream, leaveOpen: false);
using var rewindableStream = SharpCompressStream.Create(testStream, leaveOpen: false);
var streamStack = (IStreamStack)rewindableStream;
var startPos = streamStack.GetPosition();

View File

@@ -295,4 +295,47 @@ public class TarArchiveTests : ArchiveTests
Assert.False(isTar);
}
[Fact]
public void TarArchive_Open_Compressed_XZ_Throws()
{
var exception = Assert.Throws<InvalidFormatException>(() =>
TarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.xz"))
);
Assert.Contains("XZ", exception.Message);
Assert.Contains("TarReader.Open", exception.Message);
}
[Fact]
public void TarArchive_Open_Compressed_GZip_Throws()
{
var exception = Assert.Throws<InvalidFormatException>(() =>
TarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))
);
Assert.Contains("GZip", exception.Message);
Assert.Contains("TarReader.Open", exception.Message);
}
[Fact]
public void TarArchive_Open_Compressed_BZip2_Throws()
{
var exception = Assert.Throws<InvalidFormatException>(() =>
TarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2"))
);
Assert.Contains("BZip2", exception.Message);
Assert.Contains("TarReader.Open", exception.Message);
}
[Fact]
public void TarArchive_Open_Compressed_Stream_XZ_Throws()
{
using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.xz"));
var exception = Assert.Throws<InvalidFormatException>(() => TarArchive.Open(stream));
Assert.Contains("XZ", exception.Message);
Assert.Contains("TarReader.Open", exception.Message);
}
}