Skip to content

Commit f9bd53d

Browse files
authored
PR #588: Add a simple async read test for ZipFile
1 parent 06ff713 commit f9bd53d

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.IO;
77
using System.Text;
8+
using System.Threading.Tasks;
89

910
namespace ICSharpCode.SharpZipLib.Tests.Zip
1011
{
@@ -582,6 +583,29 @@ public void RoundTripInMemory()
582583
}
583584
}
584585

586+
/// <summary>
587+
/// Simple async round trip test for ZipFile class
588+
/// </summary>
589+
[TestCase(CompressionMethod.Stored)]
590+
[TestCase(CompressionMethod.Deflated)]
591+
[TestCase(CompressionMethod.BZip2)]
592+
[Category("Zip")]
593+
[Category("Async")]
594+
public async Task RoundTripInMemoryAsync(CompressionMethod compressionMethod)
595+
{
596+
var storage = new MemoryStream();
597+
MakeZipFile(storage, compressionMethod, false, "", 10, 1024, "");
598+
599+
using (ZipFile zipFile = new ZipFile(storage))
600+
{
601+
foreach (ZipEntry e in zipFile)
602+
{
603+
Stream instream = zipFile.GetInputStream(e);
604+
await CheckKnownEntryAsync(instream, 1024);
605+
}
606+
}
607+
}
608+
585609
[Test]
586610
[Category("Zip")]
587611
public void AddToEmptyArchive()

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Security;
99
using System.Text;
10+
using System.Threading.Tasks;
1011

1112
namespace ICSharpCode.SharpZipLib.Tests.Zip
1213
{
@@ -288,7 +289,7 @@ protected static byte ScatterValue(byte rhs)
288289
return (byte)((rhs * 253 + 7) & 0xff);
289290
}
290291

291-
private static void AddKnownDataToEntry(ZipOutputStream zipStream, int size)
292+
private static void AddKnownDataToEntry(Stream zipStream, int size)
292293
{
293294
if (size > 0)
294295
{
@@ -387,6 +388,27 @@ protected void MakeZipFile(Stream storage, bool isOwner,
387388
}
388389
}
389390

391+
protected void MakeZipFile(Stream storage, CompressionMethod compressionMethod, bool isOwner,
392+
string entryNamePrefix, int entries, int size, string comment)
393+
{
394+
using (ZipFile f = new ZipFile(storage, leaveOpen: !isOwner))
395+
{
396+
f.BeginUpdate();
397+
f.SetComment(comment);
398+
399+
for (int i = 0; i < entries; ++i)
400+
{
401+
var data = new MemoryStream();
402+
AddKnownDataToEntry(data, size);
403+
404+
var m = new MemoryDataSource(data.ToArray());
405+
f.Add(m, entryNamePrefix + (i + 1), compressionMethod);
406+
}
407+
408+
f.CommitUpdate();
409+
}
410+
}
411+
390412
#endregion MakeZipFile Entries
391413

392414
protected static void CheckKnownEntry(Stream inStream, int expectedCount)
@@ -408,6 +430,25 @@ protected static void CheckKnownEntry(Stream inStream, int expectedCount)
408430
Assert.AreEqual(expectedCount, total, "Wrong number of bytes read from entry");
409431
}
410432

433+
protected static async Task CheckKnownEntryAsync(Stream inStream, int expectedCount)
434+
{
435+
byte[] buffer = new byte[1024];
436+
437+
int bytesRead;
438+
int total = 0;
439+
byte nextValue = 0;
440+
while ((bytesRead = await inStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
441+
{
442+
total += bytesRead;
443+
for (int i = 0; i < bytesRead; ++i)
444+
{
445+
Assert.AreEqual(nextValue, buffer[i], "Wrong value read from entry");
446+
nextValue = ScatterValue(nextValue);
447+
}
448+
}
449+
Assert.AreEqual(expectedCount, total, "Wrong number of bytes read from entry");
450+
}
451+
411452
protected byte ReadByteChecked(Stream stream)
412453
{
413454
int rawValue = stream.ReadByte();

0 commit comments

Comments
 (0)