Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a simple async read test for ZipFile #588

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
Expand Down Expand Up @@ -582,6 +583,29 @@ public void RoundTripInMemory()
}
}

/// <summary>
/// Simple async round trip test for ZipFile class
/// </summary>
[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()
Expand Down
43 changes: 42 additions & 1 deletion test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -387,6 +388,27 @@ protected void MakeZipFile(string name, string entryNamePrefix, int entries, int
}
}

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)
Expand All @@ -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();
Expand Down