mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-18 10:22:04 +00:00
Merge remote-tracking branch 'origin/release'
# Conflicts: # src/SharpCompress/Compressors/LZMA/LZ/LzOutWindow.cs # src/SharpCompress/Compressors/LZMA/LzmaStream.cs
This commit is contained in:
58
tests/SharpCompress.Test/Arc/ArcAllocationTests.cs
Normal file
58
tests/SharpCompress.Test/Arc/ArcAllocationTests.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.ArcLzw;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Arc;
|
||||
|
||||
public class ArcAllocationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Arc_Rejects_Huge_Truncated_Compressed_Data(bool useAsync)
|
||||
{
|
||||
using var input = new MemoryStream(new byte[] { 12, 65, 0 });
|
||||
using var stream = new ArcLzwStream(input, int.MaxValue);
|
||||
var buffer = new byte[1];
|
||||
if (useAsync)
|
||||
{
|
||||
await Assert.ThrowsAsync<IncompleteArchiveException>(() =>
|
||||
stream.ReadAsync(buffer, 0, 1, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<IncompleteArchiveException>(() => stream.Read(buffer, 0, 1));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Arc_Preserves_Output_Across_Small_Reads(bool useAsync)
|
||||
{
|
||||
// Two nine-bit literals, followed by a byte belonging to the next entry.
|
||||
using var input = new MemoryStream(new byte[] { 65, 132, 0, 99 });
|
||||
using var stream = new ArcLzwStream(input, 3, false);
|
||||
var buffer = new byte[3];
|
||||
foreach (var expected in new byte[] { 65, 66 })
|
||||
{
|
||||
var read = useAsync
|
||||
? await stream.ReadAsync(buffer, 1, 1, CancellationToken.None)
|
||||
: stream.Read(buffer, 1, 1);
|
||||
Assert.Equal(1, read);
|
||||
Assert.Equal(expected, buffer[1]);
|
||||
Assert.Equal(0, buffer[0]);
|
||||
}
|
||||
Assert.Equal(0, stream.Read(buffer, 0, 1));
|
||||
Assert.Equal(99, input.ReadByte());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Arc_Rejects_Negative_Compressed_Size() =>
|
||||
Assert.Throws<InvalidFormatException>(() => new ArcLzwStream(Stream.Null, -1));
|
||||
}
|
||||
@@ -6,9 +6,11 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Tar.Headers;
|
||||
using SharpCompress.Common.Zip.Headers;
|
||||
using SharpCompress.Compressors.LZMA;
|
||||
using SharpCompress.Compressors.LZMA.LZ;
|
||||
using SharpCompress.Compressors.LZMA.Utilities;
|
||||
using SharpCompress.Compressors.Shrink;
|
||||
using SharpCompress.IO;
|
||||
using Xunit;
|
||||
|
||||
@@ -16,6 +18,184 @@ namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class BufferPoolLifetimeTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false, 0, false)]
|
||||
[InlineData(true, 0, false)]
|
||||
[InlineData(false, 10000, false)]
|
||||
[InlineData(true, 10000, false)]
|
||||
[InlineData(false, 1, true)]
|
||||
[InlineData(true, 1, true)]
|
||||
public async Task Zip64_ExtensibleData_ReturnsBuffer(
|
||||
bool useAsync,
|
||||
int payloadSize,
|
||||
bool truncated
|
||||
)
|
||||
{
|
||||
var pool = new TrackingPool<byte> { ExtraCapacity = 32 };
|
||||
using var input = new MemoryStream();
|
||||
using (var writer = new BinaryWriter(input, System.Text.Encoding.UTF8, true))
|
||||
{
|
||||
writer.Write((ulong)(44 + payloadSize + (truncated ? 10000 : 0)));
|
||||
writer.Write(new byte[44 + payloadSize]);
|
||||
if (!truncated)
|
||||
{
|
||||
writer.Write((byte)99);
|
||||
}
|
||||
}
|
||||
input.Position = 0;
|
||||
var header = new Zip64DirectoryEndHeader(pool);
|
||||
if (useAsync)
|
||||
{
|
||||
using var reader = new AsyncBinaryReader(input, leaveOpen: true);
|
||||
if (truncated)
|
||||
{
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
|
||||
await header.Read(reader)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
await header.Read(reader);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using var reader = new BinaryReader(input, System.Text.Encoding.UTF8, true);
|
||||
if (truncated)
|
||||
{
|
||||
Assert.ThrowsAny<SharpCompressException>(() => header.Read(reader));
|
||||
}
|
||||
else
|
||||
{
|
||||
header.Read(reader);
|
||||
}
|
||||
}
|
||||
if (!truncated)
|
||||
{
|
||||
Assert.Equal(99, input.ReadByte());
|
||||
}
|
||||
Assert.Equal(payloadSize == 0 ? 0 : 1, pool.RentCount);
|
||||
Assert.Equal(pool.RentCount, pool.ReturnCount);
|
||||
Assert.Empty(pool.Active);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false, false)]
|
||||
[InlineData(false, true)]
|
||||
[InlineData(true, false)]
|
||||
[InlineData(true, true)]
|
||||
public async Task Shrink_Dispose_ReturnsBuffers(bool useAsync, bool asyncDispose)
|
||||
{
|
||||
var pool = new TrackingPool<byte> { ExtraCapacity = 32 };
|
||||
using var input = new MemoryStream(new byte[] { 65, 132, 0 });
|
||||
using var stream = new ShrinkStream(input, 2, pool);
|
||||
var buffer = new byte[1];
|
||||
foreach (var expected in new byte[] { 65, 66 })
|
||||
{
|
||||
var read = useAsync
|
||||
? await stream.ReadAsync(buffer, 0, 1, CancellationToken.None)
|
||||
: stream.Read(buffer, 0, 1);
|
||||
Assert.Equal(1, read);
|
||||
Assert.Equal(expected, buffer[0]);
|
||||
}
|
||||
Assert.Equal(0, stream.Read(buffer, 0, 1));
|
||||
Assert.Equal(2, pool.RentCount);
|
||||
Assert.Equal(1, pool.ReturnCount);
|
||||
Assert.Single(pool.Active);
|
||||
|
||||
#if !LEGACY_DOTNET
|
||||
if (asyncDispose)
|
||||
{
|
||||
await stream.DisposeAsync();
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
stream.Dispose();
|
||||
}
|
||||
stream.Dispose();
|
||||
Assert.Equal(2, pool.ReturnCount);
|
||||
Assert.Empty(pool.Active);
|
||||
Assert.True(input.CanRead);
|
||||
Assert.False(stream.CanRead);
|
||||
Assert.Throws<ObjectDisposedException>(() => stream.Read(buffer, 0, 1));
|
||||
await Assert.ThrowsAsync<ObjectDisposedException>(() =>
|
||||
stream.ReadAsync(buffer, 0, 1, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Shrink_InvalidData_ReturnsBuffers(bool useAsync)
|
||||
{
|
||||
var pool = new TrackingPool<byte> { ExtraCapacity = 32 };
|
||||
using var input = new MemoryStream(new byte[] { 65, 132, 0 });
|
||||
using var stream = new ShrinkStream(input, int.MaxValue, pool);
|
||||
var buffer = new byte[1];
|
||||
if (useAsync)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(() =>
|
||||
stream.ReadAsync(buffer, 0, 1, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<InvalidFormatException>(() => stream.Read(buffer, 0, 1));
|
||||
}
|
||||
Assert.Equal(2, pool.RentCount);
|
||||
Assert.Equal(2, pool.ReturnCount);
|
||||
Assert.Empty(pool.Active);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void Shrink_Growth_ReturnsBuffers(bool failGrowth)
|
||||
{
|
||||
// Literal-only nine-bit codes fill the first output rental without increasing code size.
|
||||
const int count = 70000;
|
||||
var input = new byte[(count * 9 + 7) / 8];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var bit = i * 9;
|
||||
input[bit / 8] |= (byte)(65 << (bit % 8));
|
||||
input[bit / 8 + 1] |= (byte)(65 >> (8 - bit % 8));
|
||||
}
|
||||
var pool = new TrackingPool<byte> { FailRentNumber = failGrowth ? 2 : 0 };
|
||||
if (failGrowth)
|
||||
{
|
||||
Assert.Throws<OutOfMemoryException>(() =>
|
||||
HwUnshrink.UnshrinkGrowing(input, input.Length, count, pool, out _, out _)
|
||||
);
|
||||
Assert.Equal(1, pool.ReturnCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
var status = HwUnshrink.UnshrinkGrowing(
|
||||
input,
|
||||
input.Length,
|
||||
count,
|
||||
pool,
|
||||
out var output,
|
||||
out var used
|
||||
);
|
||||
try
|
||||
{
|
||||
Assert.Equal(HwUnshrink.UnshrnkStatus.Ok, status);
|
||||
Assert.Equal(count, used);
|
||||
Assert.All(output, value => Assert.Equal(65, value));
|
||||
Assert.Equal(1, pool.ReturnCount);
|
||||
}
|
||||
finally
|
||||
{
|
||||
pool.Return(output);
|
||||
}
|
||||
Assert.Equal(2, pool.ReturnCount);
|
||||
}
|
||||
Assert.Empty(pool.Active);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
@@ -101,6 +281,74 @@ public class BufferPoolLifetimeTests
|
||||
Assert.Empty(pool.Active);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false, false)]
|
||||
[InlineData(false, true)]
|
||||
[InlineData(true, false)]
|
||||
[InlineData(true, true)]
|
||||
public async Task OutWindow_LazyGrowth_PreservesPoolOwnership(bool asyncWrite, bool failGrowth)
|
||||
{
|
||||
var pool = new TrackingPool<byte>();
|
||||
using var window = new OutWindow(pool);
|
||||
window.Create(16384);
|
||||
Assert.Equal(4096, window.BufferCapacity);
|
||||
|
||||
for (var i = 0; i < 4096; i++)
|
||||
{
|
||||
if (asyncWrite)
|
||||
{
|
||||
await window.PutByteAsync(42, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.PutByte(42);
|
||||
}
|
||||
}
|
||||
|
||||
if (failGrowth)
|
||||
{
|
||||
pool.FailNextRent = true;
|
||||
if (asyncWrite)
|
||||
{
|
||||
await Assert.ThrowsAsync<OutOfMemoryException>(async () =>
|
||||
await window.PutByteAsync(43, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<OutOfMemoryException>(() => window.PutByte(43));
|
||||
}
|
||||
Assert.Equal(0, pool.ReturnCount);
|
||||
Assert.Equal(4096, window.BufferCapacity);
|
||||
Assert.Equal(42, window.GetByte(0));
|
||||
}
|
||||
|
||||
if (asyncWrite)
|
||||
{
|
||||
await window.PutByteAsync(43, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.PutByte(43);
|
||||
}
|
||||
Assert.Equal(8192, window.BufferCapacity);
|
||||
Assert.Equal(42, window.GetByte(4096));
|
||||
Assert.Equal(43, window.GetByte(0));
|
||||
Assert.Equal(1, pool.ReturnCount);
|
||||
Assert.Single(pool.Active);
|
||||
|
||||
if (asyncWrite)
|
||||
{
|
||||
await window.DisposeAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.Dispose();
|
||||
}
|
||||
Assert.Equal(2, pool.ReturnCount);
|
||||
Assert.Empty(pool.Active);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
@@ -302,18 +550,20 @@ public class BufferPoolLifetimeTests
|
||||
internal int RentCount { get; private set; }
|
||||
internal int ReturnCount { get; private set; }
|
||||
internal bool FailNextRent { get; set; }
|
||||
internal int FailRentNumber { get; set; }
|
||||
internal int ExtraCapacity { get; set; }
|
||||
|
||||
public override T[] Rent(int minimumLength)
|
||||
{
|
||||
RentCount++;
|
||||
if (FailNextRent)
|
||||
if (FailNextRent || RentCount == FailRentNumber)
|
||||
{
|
||||
FailNextRent = false;
|
||||
#pragma warning disable CA2201 // Simulate allocation failure without exhausting process memory.
|
||||
throw new OutOfMemoryException("Injected pool allocation failure.");
|
||||
#pragma warning restore CA2201
|
||||
}
|
||||
var buffer = new T[minimumLength];
|
||||
var buffer = new T[minimumLength + ExtraCapacity];
|
||||
Assert.True(Active.Add(buffer));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
164
tests/SharpCompress.Test/Streams/Lzma2TruncationTests.cs
Normal file
164
tests/SharpCompress.Test/Streams/Lzma2TruncationTests.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.LZMA;
|
||||
using SharpCompress.Compressors.Xz;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class Lzma2TruncationTests
|
||||
{
|
||||
private const string TruncatedXzHex =
|
||||
"FD377A585A0000016922DE360200210100000000372797D6010011616C7068610A616C7068610A616C7068610A";
|
||||
private const string MutatedSevenZipBase64 =
|
||||
"N3q8ryccAATrtqxIDgAAAAAAAABaAAAAAAAAAKyqq98BAApoZWxsbyB3b3JsZAEEBgABCQ4ABwsBAAEhIQEADAwACAoBhRFKDQAABQEZDAAAAAAAAAAAAAAAABETAHQAZQBzAHQALgB0AHgAdAAAABkAFAoBAI6gjYxMV90BFQYBACAAAAAAAA==";
|
||||
|
||||
public static IEnumerable<object[]> PartialHeaders()
|
||||
{
|
||||
// Start with a complete dictionary-reset chunk so a missing control byte
|
||||
// cannot be rejected merely because the dictionary still needs resetting.
|
||||
byte[] prefix = [0x01, 0x00, 0x00, 0x58];
|
||||
yield return [prefix];
|
||||
byte[][] headers =
|
||||
[
|
||||
[0x01, 0x00, 0x00],
|
||||
[0x02, 0x00, 0x00],
|
||||
[0xE0, 0x00, 0x00, 0x00, 0x04, 0x00],
|
||||
];
|
||||
foreach (var header in headers)
|
||||
{
|
||||
for (var length = 1; length < header.Length; length++)
|
||||
{
|
||||
var data = new byte[prefix.Length + length];
|
||||
prefix.CopyTo(data, 0);
|
||||
Array.Copy(header, 0, data, prefix.Length, length);
|
||||
yield return [data];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PartialHeaders))]
|
||||
public void Lzma2_Rejects_Partial_Header(byte[] data)
|
||||
{
|
||||
using var input = new MemoryStream(data);
|
||||
using var decoder = LzmaStream.Create([0x00], input);
|
||||
Assert.ThrowsAny<SharpCompressException>(() => decoder.CopyTo(Stream.Null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PartialHeaders))]
|
||||
public void Lzma2_ReadByte_Rejects_Partial_Header(byte[] data)
|
||||
{
|
||||
using var input = new MemoryStream(data);
|
||||
using var decoder = LzmaStream.Create([0x00], input);
|
||||
Assert.Equal(0x58, decoder.ReadByte());
|
||||
Assert.ThrowsAny<SharpCompressException>(() => decoder.ReadByte());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PartialHeaders))]
|
||||
public async Task Lzma2_Async_Rejects_Partial_Header(byte[] data)
|
||||
{
|
||||
using var input = new MemoryStream(data);
|
||||
await using var decoder = await LzmaStream.CreateAsync([0x00], input, -1, -1);
|
||||
var buffer = new byte[64];
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
|
||||
await decoder.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None)
|
||||
);
|
||||
#if !LEGACY_DOTNET
|
||||
// Use a fresh decoder because parser state is undefined after a failed read.
|
||||
using var memoryInput = new MemoryStream(data);
|
||||
await using var memoryDecoder = await LzmaStream.CreateAsync([0x00], memoryInput, -1, -1);
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
|
||||
await memoryDecoder.ReadAsync(buffer.AsMemory(), CancellationToken.None)
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Xz_Rejects_Missing_Lzma2_End_Marker(bool useAsync)
|
||||
{
|
||||
var data = new byte[TruncatedXzHex.Length / 2];
|
||||
for (var i = 0; i < data.Length; i++)
|
||||
{
|
||||
data[i] = Convert.ToByte(TruncatedXzHex.Substring(i * 2, 2), 16);
|
||||
}
|
||||
using var input = new MemoryStream(data);
|
||||
#if LEGACY_DOTNET
|
||||
using var decoder = new XZStream(input);
|
||||
#else
|
||||
await using var decoder = new XZStream(input);
|
||||
#endif
|
||||
if (useAsync)
|
||||
{
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(() =>
|
||||
decoder.CopyToAsync(Stream.Null, 81920, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.ThrowsAny<SharpCompressException>(() => decoder.CopyTo(Stream.Null));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task SevenZip_Rejects_Missing_Lzma2_End_Marker(bool useAsync)
|
||||
{
|
||||
using var input = new MemoryStream(Convert.FromBase64String(MutatedSevenZipBase64));
|
||||
if (useAsync)
|
||||
{
|
||||
await using var archive = await ArchiveFactory.OpenAsyncArchive(input);
|
||||
await foreach (var entry in archive.EntriesAsync)
|
||||
{
|
||||
#if LEGACY_DOTNET
|
||||
using var stream = await entry.OpenEntryStreamAsync(CancellationToken.None);
|
||||
#else
|
||||
await using var stream = await entry.OpenEntryStreamAsync(CancellationToken.None);
|
||||
#endif
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(() =>
|
||||
stream.CopyToAsync(Stream.Null, 81920, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using var archive = ArchiveFactory.OpenArchive(input);
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
using var stream = entry.OpenEntryStream();
|
||||
Assert.ThrowsAny<SharpCompressException>(() => stream.CopyTo(Stream.Null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Lzma2_Accepts_End_Marker_After_Uncompressed_Chunks(bool useAsync)
|
||||
{
|
||||
byte[] data = [0x01, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00, 0x59, 0x00];
|
||||
using var input = new MemoryStream(data);
|
||||
using var output = new MemoryStream();
|
||||
if (useAsync)
|
||||
{
|
||||
await using var decoder = await LzmaStream.CreateAsync([0x00], input, data.Length, 2);
|
||||
await decoder.CopyToAsync(output, 1, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var decoder = LzmaStream.Create([0x00], input, data.Length, 2);
|
||||
decoder.CopyTo(output, 1);
|
||||
}
|
||||
Assert.Equal(new byte[] { 0x58, 0x59 }, output.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.LZMA;
|
||||
using SharpCompress.Compressors.LZMA.LZ;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class LzmaDictionaryAllocationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false, false)]
|
||||
[InlineData(false, true)]
|
||||
[InlineData(true, false)]
|
||||
[InlineData(true, true)]
|
||||
public async Task Lzma_Raw_Huge_Dictionary_Decodes_With_Stream_And_Direct_APIs(
|
||||
bool useAsync,
|
||||
bool direct
|
||||
)
|
||||
{
|
||||
var expected = new byte[20000];
|
||||
new Random(123).NextBytes(expected);
|
||||
using var packed = new MemoryStream();
|
||||
byte[] properties;
|
||||
using (
|
||||
var encoder = LzmaStream.Create(new LzmaEncoderProperties(false, 4096), false, packed)
|
||||
)
|
||||
{
|
||||
properties = (byte[])encoder.Properties.Clone();
|
||||
encoder.Write(expected, 0, expected.Length);
|
||||
}
|
||||
// The compressed payload needs only a small dictionary, regardless of its advertised size.
|
||||
properties[1] = 255;
|
||||
properties[2] = 255;
|
||||
properties[3] = 255;
|
||||
properties[4] = 127;
|
||||
using var input = new MemoryStream(packed.ToArray());
|
||||
using var output = new MemoryStream();
|
||||
if (direct)
|
||||
{
|
||||
using var decoder = new Decoder();
|
||||
decoder.SetDecoderProperties(properties);
|
||||
if (useAsync)
|
||||
{
|
||||
await decoder.CodeAsync(
|
||||
input,
|
||||
output,
|
||||
input.Length,
|
||||
expected.Length,
|
||||
null!,
|
||||
CancellationToken.None
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
decoder.Code(input, output, input.Length, expected.Length, null!);
|
||||
}
|
||||
}
|
||||
else if (useAsync)
|
||||
{
|
||||
using var decoder = await LzmaStream.CreateAsync(
|
||||
properties,
|
||||
input,
|
||||
input.Length,
|
||||
expected.Length
|
||||
);
|
||||
await decoder.CopyToAsync(output, 137, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var decoder = LzmaStream.Create(properties, input, input.Length, expected.Length);
|
||||
decoder.CopyTo(output, 137);
|
||||
}
|
||||
Assert.Equal(expected, output.ToArray());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Lzma_Truncated_Uncompressed_Chunk_Does_Not_Allocate_From_Read_Size(
|
||||
bool useAsync
|
||||
)
|
||||
{
|
||||
using var window = new OutWindow(ArrayPool<byte>.Create());
|
||||
window.Create(int.MaxValue);
|
||||
window.SetLimit(int.MaxValue);
|
||||
using var input = new MemoryStream(new byte[] { 1 });
|
||||
if (useAsync)
|
||||
{
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
|
||||
await window.CopyStreamAsync(input, int.MaxValue, CancellationToken.None)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.ThrowsAny<SharpCompressException>(() => window.CopyStream(input, int.MaxValue));
|
||||
}
|
||||
Assert.InRange(window.BufferCapacity, 1, 8192);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(38)]
|
||||
[InlineData(40)]
|
||||
[InlineData(64)]
|
||||
[InlineData(255)]
|
||||
public void Lzma2_Rejects_Unsupported_Dictionary_Properties(int property)
|
||||
{
|
||||
Assert.ThrowsAny<SharpCompressException>(() =>
|
||||
LzmaStream.Create(new byte[] { (byte)property }, Stream.Null)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Lzma_Huge_Dictionary_Starts_Small_And_Grows_With_History()
|
||||
{
|
||||
using var window = new OutWindow(ArrayPool<byte>.Create());
|
||||
window.Create(int.MaxValue);
|
||||
Assert.InRange(window.BufferCapacity, 1, 4096);
|
||||
Assert.Equal(0, window.GetByte(0));
|
||||
window.SetLimit(20000);
|
||||
for (var i = 0; i < 20000; i++)
|
||||
{
|
||||
window.PutByte((byte)i);
|
||||
}
|
||||
Assert.InRange(window.BufferCapacity, 20000, 32768);
|
||||
for (var i = 0; i < 20000; i++)
|
||||
{
|
||||
Assert.Equal((byte)i, window.GetByte(19999 - i));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Lzma_History_Survives_Growth_Wrap_And_Pending_Matches(bool useAsync)
|
||||
{
|
||||
using var window = new OutWindow();
|
||||
window.Create(8192);
|
||||
var output = new byte[8192];
|
||||
window.SetLimit(8192);
|
||||
for (var i = 0; i < output.Length; i++)
|
||||
{
|
||||
if (useAsync)
|
||||
{
|
||||
await window.PutByteAsync((byte)i, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.PutByte((byte)i);
|
||||
}
|
||||
}
|
||||
Assert.Equal(output.Length, window.Read(output, 0, output.Length));
|
||||
window.SetLimit(10);
|
||||
if (useAsync)
|
||||
{
|
||||
await window.CopyBlockAsync(8191, 20, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.CopyBlock(8191, 20);
|
||||
}
|
||||
Assert.True(window.HasPending);
|
||||
window.SetLimit(10);
|
||||
if (useAsync)
|
||||
{
|
||||
await window.CopyPendingAsync(CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.CopyPending();
|
||||
}
|
||||
Assert.False(window.HasPending);
|
||||
Assert.Equal(20, window.Read(output, 0, output.Length));
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
Assert.Equal((byte)i, output[i]);
|
||||
}
|
||||
window.Reset();
|
||||
Assert.Equal(0, window.Total);
|
||||
Assert.Equal(0, window.GetByte(0));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Lzma_Preset_Dictionary_Does_Not_Allocate_Declared_Window(bool useAsync)
|
||||
{
|
||||
using var window = new OutWindow(ArrayPool<byte>.Create());
|
||||
window.Create(int.MaxValue);
|
||||
using var preset = new MemoryStream(new byte[] { 1, 2, 3 });
|
||||
if (useAsync)
|
||||
{
|
||||
await window.TrainAsync(preset);
|
||||
}
|
||||
else
|
||||
{
|
||||
window.Train(preset);
|
||||
}
|
||||
Assert.Equal(3, window.GetByte(0));
|
||||
Assert.Equal(1, window.GetByte(2));
|
||||
Assert.InRange(window.BufferCapacity, 3, 4096);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Lzma2_Tiny_Stream_With_Huge_Dictionary_Decodes(bool useAsync)
|
||||
{
|
||||
using var input = new MemoryStream(new byte[] { 1, 0, 0, 65, 0 });
|
||||
using var output = new MemoryStream();
|
||||
if (useAsync)
|
||||
{
|
||||
using var decoder = await LzmaStream.CreateAsync(new byte[] { 37 }, input, -1, -1);
|
||||
await decoder.CopyToAsync(output, 81920, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var decoder = LzmaStream.Create(new byte[] { 37 }, input);
|
||||
decoder.CopyTo(output);
|
||||
}
|
||||
Assert.Equal(new byte[] { 65 }, output.ToArray());
|
||||
}
|
||||
}
|
||||
74
tests/SharpCompress.Test/Zip/ShrinkAllocationTests.cs
Normal file
74
tests/SharpCompress.Test/Zip/ShrinkAllocationTests.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.Shrink;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Zip;
|
||||
|
||||
public class ShrinkAllocationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Shrink_Rejects_Huge_Truncated_Output(bool useAsync)
|
||||
{
|
||||
using var input = new MemoryStream(new byte[] { 65, 0 });
|
||||
if (useAsync)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
|
||||
{
|
||||
using var stream = await ShrinkStream.CreateAsync(
|
||||
input,
|
||||
int.MaxValue,
|
||||
CancellationToken.None
|
||||
);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
using var stream = new ShrinkStream(input, int.MaxValue);
|
||||
Assert.Throws<InvalidFormatException>(() => stream.ReadByte());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shrink_Grows_Only_For_Actual_Output()
|
||||
{
|
||||
var pool = ArrayPool<byte>.Create();
|
||||
var status = HwUnshrink.UnshrinkGrowing(
|
||||
new byte[] { 65, 132, 0 },
|
||||
3,
|
||||
int.MaxValue,
|
||||
pool,
|
||||
out var output,
|
||||
out var used
|
||||
);
|
||||
try
|
||||
{
|
||||
Assert.Equal(HwUnshrink.UnshrnkStatus.Ok, status);
|
||||
Assert.Equal(2, used);
|
||||
Assert.Equal(65, output[0]);
|
||||
Assert.Equal(66, output[1]);
|
||||
Assert.InRange(output.Length, 2, 131072);
|
||||
}
|
||||
finally
|
||||
{
|
||||
pool.Return(output);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shrink_Preserves_Fixed_Buffer_API()
|
||||
{
|
||||
var output = new byte[2];
|
||||
Assert.Equal(
|
||||
HwUnshrink.UnshrnkStatus.Ok,
|
||||
HwUnshrink.Unshrink(new byte[] { 65, 132, 0 }, 3, out _, output, 2, out var used)
|
||||
);
|
||||
Assert.Equal(2, used);
|
||||
Assert.Equal(new byte[] { 65, 66 }, output);
|
||||
}
|
||||
}
|
||||
68
tests/SharpCompress.Test/Zip/Zip64AllocationTests.cs
Normal file
68
tests/SharpCompress.Test/Zip/Zip64AllocationTests.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Zip.Headers;
|
||||
using SharpCompress.IO;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Zip;
|
||||
|
||||
public class Zip64AllocationTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0UL)]
|
||||
[InlineData(43UL)]
|
||||
[InlineData(ulong.MaxValue)]
|
||||
public async Task Zip64_Rejects_Invalid_Record_Size(ulong size)
|
||||
{
|
||||
using var stream = CreateRecord(size, 0);
|
||||
Assert.Throws<InvalidFormatException>(() =>
|
||||
new Zip64DirectoryEndHeader().Read(new BinaryReader(stream))
|
||||
);
|
||||
stream.Position = 0;
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
|
||||
await new Zip64DirectoryEndHeader().Read(new AsyncBinaryReader(stream))
|
||||
);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(44UL, 0)]
|
||||
[InlineData(10044UL, 10000)]
|
||||
public async Task Zip64_Skips_Extensible_Data_Exactly(ulong size, int payloadSize)
|
||||
{
|
||||
using var stream = CreateRecord(size, payloadSize);
|
||||
new Zip64DirectoryEndHeader().Read(new BinaryReader(stream));
|
||||
Assert.Equal(stream.Length, stream.Position);
|
||||
stream.Position = 0;
|
||||
await new Zip64DirectoryEndHeader().Read(new AsyncBinaryReader(stream));
|
||||
Assert.Equal(stream.Length, stream.Position);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(2147483691UL)]
|
||||
[InlineData(4294967340UL)]
|
||||
public async Task Zip64_Rejects_Huge_Truncated_Extensible_Data(ulong size)
|
||||
{
|
||||
using var stream = CreateRecord(size, 1);
|
||||
Assert.ThrowsAny<SharpCompressException>(() =>
|
||||
new Zip64DirectoryEndHeader().Read(new BinaryReader(stream))
|
||||
);
|
||||
stream.Position = 0;
|
||||
await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
|
||||
await new Zip64DirectoryEndHeader().Read(new AsyncBinaryReader(stream))
|
||||
);
|
||||
}
|
||||
|
||||
private static MemoryStream CreateRecord(ulong size, int payloadSize)
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
using (var writer = new BinaryWriter(stream, System.Text.Encoding.UTF8, true))
|
||||
{
|
||||
writer.Write(size);
|
||||
writer.Write(new byte[44 + payloadSize]);
|
||||
}
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user