Files
sharpcompress/tests/SharpCompress.Test/Xz/XZBlockAsyncTests.cs

126 lines
4.2 KiB
C#
Raw Permalink Normal View History

2025-11-01 10:35:07 +00:00
using System.IO;
using System.Text;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz;
using Xunit;
namespace SharpCompress.Test.Xz;
public class XzBlockAsyncTests : XzTestsBase
{
protected override void Rewind(Stream stream) => stream.Position = 12;
protected override void RewindIndexed(Stream stream) => stream.Position = 12;
private static async Task<byte[]> ReadBytesAsync(XZBlock block, int bytesToRead)
{
var buffer = new byte[bytesToRead];
var read = await block.ReadAsync(buffer, 0, bytesToRead).ConfigureAwait(false);
if (read != bytesToRead)
{
throw new EndOfStreamException();
}
return buffer;
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask OnFindIndexBlockThrowAsync()
2025-11-01 10:35:07 +00:00
{
var bytes = new byte[] { 0 };
using Stream indexBlockStream = new MemoryStream(bytes);
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(indexBlockStream, CheckType.CRC64, 8);
2025-11-01 10:35:07 +00:00
await Assert.ThrowsAsync<XZIndexMarkerReachedException>(async () =>
{
await ReadBytesAsync(xzBlock, 1).ConfigureAwait(false);
});
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask CrcIncorrectThrowsAsync()
2025-11-01 10:35:07 +00:00
{
var bytes = (byte[])Compressed.Clone();
bytes[20]++;
using Stream badCrcStream = new MemoryStream(bytes);
Rewind(badCrcStream);
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(badCrcStream, CheckType.CRC64, 8);
2025-11-01 10:35:07 +00:00
var ex = await Assert.ThrowsAsync<InvalidFormatException>(async () =>
{
await ReadBytesAsync(xzBlock, 1).ConfigureAwait(false);
});
Assert.Equal("Block header corrupt", ex.Message);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask CanReadMAsync()
2025-11-01 10:35:07 +00:00
{
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(CompressedStream, CheckType.CRC64, 8);
2025-11-01 10:35:07 +00:00
Assert.Equal(
Encoding.ASCII.GetBytes("M"),
await ReadBytesAsync(xzBlock, 1).ConfigureAwait(false)
);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask CanReadMaryAsync()
2025-11-01 10:35:07 +00:00
{
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(CompressedStream, CheckType.CRC64, 8);
2025-11-01 10:35:07 +00:00
Assert.Equal(
Encoding.ASCII.GetBytes("M"),
await ReadBytesAsync(xzBlock, 1).ConfigureAwait(false)
);
Assert.Equal(
Encoding.ASCII.GetBytes("a"),
await ReadBytesAsync(xzBlock, 1).ConfigureAwait(false)
);
Assert.Equal(
Encoding.ASCII.GetBytes("ry"),
await ReadBytesAsync(xzBlock, 2).ConfigureAwait(false)
);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask CanReadPoemWithStreamReaderAsync()
2025-11-01 10:35:07 +00:00
{
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(CompressedStream, CheckType.CRC64, 8);
using var sr = new StreamReader(xzBlock);
2025-11-01 10:35:07 +00:00
Assert.Equal(await sr.ReadToEndAsync().ConfigureAwait(false), Original);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask NoopWhenNoPaddingAsync()
2025-11-01 10:35:07 +00:00
{
// CompressedStream's only block has no padding.
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(CompressedStream, CheckType.CRC64, 8);
using var sr = new StreamReader(xzBlock);
2025-11-01 10:35:07 +00:00
await sr.ReadToEndAsync().ConfigureAwait(false);
Assert.Equal(0L, CompressedStream.Position % 4L);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask SkipsPaddingWhenPresentAsync()
2025-11-01 10:35:07 +00:00
{
// CompressedIndexedStream's first block has 1-byte padding.
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(CompressedIndexedStream, CheckType.CRC64, 8);
using var sr = new StreamReader(xzBlock);
2025-11-01 10:35:07 +00:00
await sr.ReadToEndAsync().ConfigureAwait(false);
Assert.Equal(0L, CompressedIndexedStream.Position % 4L);
}
[Fact]
2026-01-08 12:35:12 +00:00
public async ValueTask HandlesPaddingInUnalignedBlockAsync()
2025-11-01 10:35:07 +00:00
{
var compressedUnaligned = new byte[Compressed.Length + 1];
Compressed.CopyTo(compressedUnaligned, 1);
var compressedUnalignedStream = new MemoryStream(compressedUnaligned);
compressedUnalignedStream.Position = 13;
// Compressed's only block has no padding.
2026-02-13 14:26:54 +00:00
using var xzBlock = new XZBlock(compressedUnalignedStream, CheckType.CRC64, 8);
using var sr = new StreamReader(xzBlock);
2025-11-01 10:35:07 +00:00
await sr.ReadToEndAsync().ConfigureAwait(false);
Assert.Equal(1L, compressedUnalignedStream.Position % 4L);
}
}