mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 23:15:20 +00:00
BZip2, GZip and XZ done
This commit is contained in:
45
tests/SharpCompress.Test/BZip2/BZip2StreamTests.cs
Normal file
45
tests/SharpCompress.Test/BZip2/BZip2StreamTests.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
90
tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs
Normal file
90
tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user