Skip to content

Commit

Permalink
#579: Implement very simple ReadAsync in ZipAESStream
Browse files Browse the repository at this point in the history
* add an extra unit test for doing an async read on a ZipFile InputStream
* Implement ReadAsync in ZipAESStream, extra simple version
  • Loading branch information
Numpsy authored May 8, 2021
1 parent 99170e1 commit 7ac1fda
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

Expand Down Expand Up @@ -91,6 +93,13 @@ public override int Read(byte[] buffer, int offset, int count)
return nBytes;
}

/// <inheritdoc/>
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
var readCount = Read(buffer, offset, count);
return Task.FromResult(readCount);
}

// Read data from the underlying stream and decrypt it
private int ReadAndTransform(byte[] buffer, int offset, int count)
{
Expand Down
65 changes: 56 additions & 9 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Tests.TestSupport;
using System.Threading.Tasks;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
Expand Down Expand Up @@ -149,14 +150,9 @@ public void ZipFileStoreAes()
{
string password = "password";

using (var memoryStream = new MemoryStream())
// Make an encrypted zip file
using (var memoryStream = MakeAESEncryptedZipStream(password))
{
// Try to create a zip stream
WriteEncryptedZipToStream(memoryStream, password, 256, CompressionMethod.Stored);

// reset
memoryStream.Seek(0, SeekOrigin.Begin);

// try to read it
var zipFile = new ZipFile(memoryStream, leaveOpen: true)
{
Expand All @@ -180,6 +176,57 @@ public void ZipFileStoreAes()
}
}

/// <summary>
/// As <see cref="ZipFileStoreAes"/>, but with Async reads
/// </summary>
[Test]
[Category("Encryption")]
[Category("Zip")]
public async Task ZipFileStoreAesAsync()
{
string password = "password";

// Make an encrypted zip file
using (var memoryStream = MakeAESEncryptedZipStream(password))
{
// try to read it
var zipFile = new ZipFile(memoryStream, leaveOpen: true)
{
Password = password
};

foreach (ZipEntry entry in zipFile)
{
// Should be stored rather than deflated
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Stored), "Entry should be stored");

using (var zis = zipFile.GetInputStream(entry))
{
using (var inputStream = zipFile.GetInputStream(entry))
using (var sr = new StreamReader(zis, Encoding.UTF8))
{
var content = await sr.ReadToEndAsync();
Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data");
}
}
}
}
}

// Shared helper for the ZipFileStoreAes tests
private static Stream MakeAESEncryptedZipStream(string password)
{
var memoryStream = new MemoryStream();

// Try to create a zip stream
WriteEncryptedZipToStream(memoryStream, password, 256, CompressionMethod.Stored);

// reset
memoryStream.Seek(0, SeekOrigin.Begin);

return memoryStream;
}

/// <summary>
/// Test using AES encryption on a file whose contents are Stored rather than deflated
/// </summary>
Expand Down Expand Up @@ -469,7 +516,7 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
}
}

public void WriteEncryptedZipToStream(Stream stream, string password, int keySize, CompressionMethod compressionMethod = CompressionMethod.Deflated)
public static void WriteEncryptedZipToStream(Stream stream, string password, int keySize, CompressionMethod compressionMethod = CompressionMethod.Deflated)
{
using (var zs = new ZipOutputStream(stream))
{
Expand All @@ -496,7 +543,7 @@ public void WriteEncryptedZipToStream(Stream stream, int entryCount, string pass
}
}

private void AddEncrypedEntryToStream(ZipOutputStream zipOutputStream, string entryName, int keySize, CompressionMethod compressionMethod)
private static void AddEncrypedEntryToStream(ZipOutputStream zipOutputStream, string entryName, int keySize, CompressionMethod compressionMethod)
{
ZipEntry zipEntry = new ZipEntry(entryName)
{
Expand Down

0 comments on commit 7ac1fda

Please sign in to comment.