From 44b7955d85daa172c5cb8393ec6b76ff56ca84dc Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 31 Dec 2025 14:43:15 +0000 Subject: [PATCH] reader tests --- .../Mocks/AsyncOnlyStream.cs | 60 +++++++++++++++++++ .../Zip/ZipReaderAsyncTests.cs | 14 +++-- 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs diff --git a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs new file mode 100644 index 00000000..4be47432 --- /dev/null +++ b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace SharpCompress.Test.Mocks; + +public class AsyncOnlyStream : Stream +{ + private readonly Stream _stream; + + public AsyncOnlyStream(Stream stream) + { + _stream = stream; + // Console.WriteLine("AsyncOnlyStream created"); + } + + public override bool CanRead => _stream.CanRead; + public override bool CanSeek => _stream.CanSeek; + public override bool CanWrite => _stream.CanWrite; + public override long Length => _stream.Length; + public override long Position + { + get => _stream.Position; + set => _stream.Position = value; + } + + public override void Flush() => _stream.Flush(); + + public override int Read(byte[] buffer, int offset, int count) + { + throw new NotSupportedException("Synchronous Read is not supported"); + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + return _stream.ReadAsync(buffer, offset, count, cancellationToken); + } + +#if !NETFRAMEWORK && !NETSTANDARD2_0 + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + return _stream.ReadAsync(buffer, cancellationToken); + } +#endif + + public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin); + public override void SetLength(long value) => _stream.SetLength(value); + public override void Write(byte[] buffer, int offset, int count) => _stream.Write(buffer, offset, count); + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _stream.Dispose(); + } + base.Dispose(disposing); + } +} + diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index 2892be57..45d5acbb 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.Threading; using System.Threading.Tasks; using SharpCompress.Common; +using SharpCompress.IO; using SharpCompress.Readers; using SharpCompress.Readers.Zip; using SharpCompress.Test.Mocks; @@ -162,7 +164,7 @@ public class ZipReaderAsyncTests : ReaderTests public async Task Zip_Reader_Disposal_Test2_Async() { using var stream = new TestStream( - File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) + new AsyncOnlyStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))) ); var reader = ReaderFactory.Open(stream); while (await reader.MoveToNextEntryAsync()) @@ -183,9 +185,9 @@ public class ZipReaderAsyncTests : ReaderTests await Assert.ThrowsAsync(async () => { using ( - Stream stream = File.OpenRead( + Stream stream = new AsyncOnlyStream(File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.WinzipAES.zip") - ) + )) ) using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { @@ -208,9 +210,9 @@ public class ZipReaderAsyncTests : ReaderTests public async Task Zip_Deflate_WinzipAES_Read_Async() { using ( - Stream stream = File.OpenRead( + Stream stream = new AsyncOnlyStream(File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip") - ) + )) ) using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { @@ -233,7 +235,7 @@ public class ZipReaderAsyncTests : ReaderTests public async Task Zip_Deflate_ZipCrypto_Read_Async() { var count = 0; - using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip"))) + using (Stream stream = new AsyncOnlyStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip")))) using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { while (await reader.MoveToNextEntryAsync())