BZip2, GZip and XZ done

This commit is contained in:
Adam Hathcock
2026-06-15 09:59:48 +01:00
parent e9e05520d6
commit 808524d917
8 changed files with 335 additions and 7 deletions

View File

@@ -0,0 +1,45 @@
using System.IO;
using System.Text;
using SharpCompress.Common;
using SharpCompress.Compressors.BZip2;
using Xunit;
namespace SharpCompress.Test.BZip2;
public class BZip2StreamTests
{
[Fact]
public void BZip2Stream_Throws_On_Corrupt_Checksum()
{
var compressed = Compress("BZip2 checksum validation test data.");
compressed[^5] ^= 1;
using var stream = BZip2Stream.Create(
new MemoryStream(compressed),
SharpCompress.Compressors.CompressionMode.Decompress,
false
);
using var output = new MemoryStream();
Assert.Throws<ArchiveOperationException>(() => stream.CopyTo(output));
}
private static byte[] Compress(string value)
{
using var memoryStream = new MemoryStream();
using (
var bzip2Stream = BZip2Stream.Create(
memoryStream,
SharpCompress.Compressors.CompressionMode.Compress,
false,
leaveOpen: true
)
)
{
var bytes = Encoding.ASCII.GetBytes(value);
bzip2Stream.Write(bytes, 0, bytes.Length);
}
return memoryStream.ToArray();
}
}

View File

@@ -0,0 +1,90 @@
using System;
using System.Buffers.Binary;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SharpCompress.Archives;
using SharpCompress.Archives.GZip;
using SharpCompress.Common;
using SharpCompress.Readers;
using SharpCompress.Readers.GZip;
using SharpCompress.Test.Mocks;
using Xunit;
namespace SharpCompress.Test.GZip;
public class GZipCrcExtractionTests : TestBase
{
[Fact]
public void GZipArchive_WriteToFile_Throws_On_Crc_Mismatch()
{
using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true));
using var archive = GZipArchive.OpenArchive(stream);
var entry = archive.Entries.Single();
var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
Assert.Throws<InvalidFormatException>(() => entry.WriteToFile(destination));
}
[Fact]
public void GZipArchive_WriteToFile_Throws_On_Size_Mismatch()
{
using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: false));
using var archive = GZipArchive.OpenArchive(stream);
var entry = archive.Entries.Single();
var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
Assert.Throws<InvalidFormatException>(() => entry.WriteToFile(destination));
}
[Fact]
public void GZipReader_WriteEntryToFile_Throws_On_NonSeekable_Crc_Mismatch()
{
using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true));
using var nonSeekableStream = new ForwardOnlyStream(stream);
using var reader = GZipReader.OpenReader(nonSeekableStream);
var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
Assert.True(reader.MoveToNextEntry());
Assert.Throws<InvalidFormatException>(() => reader.WriteEntryToFile(destination));
}
[Fact]
public void GZipArchive_WriteToFile_Skips_Trailer_Validation_When_CheckCrc_Is_False()
{
using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true));
using var archive = GZipArchive.OpenArchive(stream);
var entry = archive.Entries.Single();
var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
entry.WriteToFile(destination, new ExtractionOptions { CheckCrc = false });
Assert.Equal(
new FileInfo(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")).Length,
new FileInfo(destination).Length
);
}
[Fact]
public async Task GZipArchive_WriteToFileAsync_Throws_On_Crc_Mismatch()
{
await using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true));
await using var archive = await GZipArchive.OpenAsyncArchive(stream);
var entry = await archive.EntriesAsync.SingleAsync();
var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
await entry.WriteToFileAsync(destination)
);
}
private static byte[] ReadCorruptedGZipTrailer(bool corruptCrc)
{
var bytes = File.ReadAllBytes(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
var trailer = bytes.AsSpan(bytes.Length - 8);
var offset = corruptCrc ? 0 : 4;
var value = BinaryPrimitives.ReadUInt32LittleEndian(trailer[offset..]);
BinaryPrimitives.WriteUInt32LittleEndian(trailer[offset..], value + 1);
return bytes;
}
}

View File

@@ -1,4 +1,5 @@
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz;
using SharpCompress.IO;
using SharpCompress.Test.Mocks;
@@ -54,4 +55,15 @@ public class XzStreamTests : XzTestsBase
var uncompressed = sr.ReadToEnd();
Assert.Equal(OriginalEmpty, uncompressed);
}
[Fact]
public void Throws_On_Corrupt_Block_Check()
{
var compressed = (byte[])Compressed.Clone();
compressed[compressed.Length - 29] ^= 1;
using var xz = new XZStream(new MemoryStream(compressed));
using var output = new MemoryStream();
Assert.Throws<InvalidFormatException>(() => xz.CopyTo(output));
}
}