From 93265d2453e7904d8153d0c0a546943857d9cc76 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Mon, 1 Mar 2021 15:18:28 +0000 Subject: [PATCH] add a simple async read test for ZipFile --- .../Zip/ZipFileHandling.cs | 24 +++++++++++ .../Zip/ZipTests.cs | 43 ++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index c92dae280..9d50fb844 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -5,6 +5,7 @@ using System; using System.IO; using System.Text; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -582,6 +583,29 @@ public void RoundTripInMemory() } } + /// + /// Simple async round trip test for ZipFile class + /// + [TestCase(CompressionMethod.Stored)] + [TestCase(CompressionMethod.Deflated)] + [TestCase(CompressionMethod.BZip2)] + [Category("Zip")] + [Category("Async")] + public async Task RoundTripInMemoryAsync(CompressionMethod compressionMethod) + { + var storage = new MemoryStream(); + MakeZipFile(storage, compressionMethod, false, "", 10, 1024, ""); + + using (ZipFile zipFile = new ZipFile(storage)) + { + foreach (ZipEntry e in zipFile) + { + Stream instream = zipFile.GetInputStream(e); + await CheckKnownEntryAsync(instream, 1024); + } + } + } + [Test] [Category("Zip")] public void AddToEmptyArchive() diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs index eff2e007b..4a0c9954f 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs @@ -7,6 +7,7 @@ using System.IO; using System.Security; using System.Text; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -288,7 +289,7 @@ protected static byte ScatterValue(byte rhs) return (byte)((rhs * 253 + 7) & 0xff); } - private static void AddKnownDataToEntry(ZipOutputStream zipStream, int size) + private static void AddKnownDataToEntry(Stream zipStream, int size) { if (size > 0) { @@ -387,6 +388,27 @@ protected void MakeZipFile(Stream storage, bool isOwner, } } + protected void MakeZipFile(Stream storage, CompressionMethod compressionMethod, bool isOwner, + string entryNamePrefix, int entries, int size, string comment) + { + using (ZipFile f = new ZipFile(storage, leaveOpen: !isOwner)) + { + f.BeginUpdate(); + f.SetComment(comment); + + for (int i = 0; i < entries; ++i) + { + var data = new MemoryStream(); + AddKnownDataToEntry(data, size); + + var m = new MemoryDataSource(data.ToArray()); + f.Add(m, entryNamePrefix + (i + 1), compressionMethod); + } + + f.CommitUpdate(); + } + } + #endregion MakeZipFile Entries protected static void CheckKnownEntry(Stream inStream, int expectedCount) @@ -408,6 +430,25 @@ protected static void CheckKnownEntry(Stream inStream, int expectedCount) Assert.AreEqual(expectedCount, total, "Wrong number of bytes read from entry"); } + protected static async Task CheckKnownEntryAsync(Stream inStream, int expectedCount) + { + byte[] buffer = new byte[1024]; + + int bytesRead; + int total = 0; + byte nextValue = 0; + while ((bytesRead = await inStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + { + total += bytesRead; + for (int i = 0; i < bytesRead; ++i) + { + Assert.AreEqual(nextValue, buffer[i], "Wrong value read from entry"); + nextValue = ScatterValue(nextValue); + } + } + Assert.AreEqual(expectedCount, total, "Wrong number of bytes read from entry"); + } + protected byte ReadByteChecked(Stream stream) { int rawValue = stream.ReadByte();