Files
sharpcompress/tests/SharpCompress.Test/Ace/AceReaderAsyncTests.cs

132 lines
4.1 KiB
C#
Raw Permalink Normal View History

2026-01-14 14:33:20 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Readers;
using SharpCompress.Readers.Ace;
using SharpCompress.Test.Mocks;
using Xunit;
2026-02-04 09:25:48 +00:00
namespace SharpCompress.Test.Ace;
public class AceReaderAsyncTests : ReaderTests
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
public AceReaderAsyncTests()
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
UseExtensionInsteadOfNameToVerify = true;
UseCaseInsensitiveToVerify = true;
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
[Fact]
public async ValueTask Ace_Uncompressed_Read_Async() =>
await ReadAsync("Ace.store.ace", CompressionType.None);
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
[Fact]
public async ValueTask Ace_Encrypted_Read_Async()
{
var exception = await Assert.ThrowsAsync<CryptographicException>(() =>
ReadAsync("Ace.encrypted.ace")
);
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
[Theory]
[InlineData("Ace.method1.ace", CompressionType.AceLZ77)]
[InlineData("Ace.method1-solid.ace", CompressionType.AceLZ77)]
[InlineData("Ace.method2.ace", CompressionType.AceLZ77)]
[InlineData("Ace.method2-solid.ace", CompressionType.AceLZ77)]
public async ValueTask Ace_Unsupported_ShouldThrow_Async(
string fileName,
CompressionType compressionType
)
{
var exception = await Assert.ThrowsAsync<NotSupportedException>(() =>
ReadAsync(fileName, compressionType)
);
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
[Theory]
[InlineData("Ace.store.largefile.ace", CompressionType.None)]
public async ValueTask Ace_LargeFileTest_Read_Async(
string fileName,
CompressionType compressionType
)
{
await ReadForBufferBoundaryCheckAsync(fileName, compressionType);
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
[Fact]
public async ValueTask Ace_Multi_Reader_Async()
{
var exception = await Assert.ThrowsAsync<MultiVolumeExtractionException>(() =>
DoMultiReaderAsync(
new[] { "Ace.store.split.ace", "Ace.store.split.c01" },
streams => AceReader.OpenAsyncReader(streams, null)
)
);
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
private async Task ReadAsync(string testArchive, CompressionType expectedCompression)
{
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
using Stream stream = File.OpenRead(testArchive);
await using var reader = await ReaderFactory.OpenAsyncReader(
new AsyncOnlyStream(stream),
new ReaderOptions()
);
while (await reader.MoveToNextEntryAsync())
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
if (!reader.Entry.IsDirectory)
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
Assert.Equal(expectedCompression, reader.Entry.CompressionType);
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH);
2026-01-14 14:33:20 +00:00
}
}
2026-02-04 09:25:48 +00:00
VerifyFiles();
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
private async Task ReadForBufferBoundaryCheckAsync(
string testArchive,
CompressionType expectedCompression
)
{
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
using Stream stream = File.OpenRead(testArchive);
await using var reader = await ReaderFactory.OpenAsyncReader(
new AsyncOnlyStream(stream),
new ReaderOptions { LookForHeader = true }
);
while (await reader.MoveToNextEntryAsync())
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
if (!reader.Entry.IsDirectory)
2026-01-14 14:33:20 +00:00
{
2026-02-04 09:25:48 +00:00
Assert.Equal(expectedCompression, reader.Entry.CompressionType);
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH);
2026-01-14 14:33:20 +00:00
}
}
2026-02-04 09:25:48 +00:00
CompareFilesByPath(
Path.Combine(SCRATCH_FILES_PATH, "alice29.txt"),
Path.Combine(MISC_TEST_FILES_PATH, "alice29.txt")
);
}
2026-01-14 14:33:20 +00:00
2026-02-04 09:25:48 +00:00
private async Task DoMultiReaderAsync(
string[] archives,
Func<IEnumerable<Stream>, IAsyncReader> readerFactory
)
{
await using var reader = readerFactory(
archives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)).Select(File.OpenRead)
);
2026-01-16 10:49:18 +00:00
2026-02-04 09:25:48 +00:00
while (await reader.MoveToNextEntryAsync())
{
if (!reader.Entry.IsDirectory)
2026-01-16 11:12:26 +00:00
{
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH);
2026-01-16 11:12:26 +00:00
}
2026-01-14 14:33:20 +00:00
}
}
}