Skip to content

Commit

Permalink
PR #503: Consider AES overhead when testing encrypted folder entries
Browse files Browse the repository at this point in the history
* Extend the ZipFile.TestEncryptedDirectoryEntry test to test AES encrypted directories

* Consider AES overhead when testing encrypted folder entries
  • Loading branch information
Numpsy committed Aug 16, 2020
1 parent 8eb94b1 commit 61d3a21
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Expand Up @@ -1289,15 +1289,15 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
// If so until details are known we will be strict.
if (entry.IsCrypted)
{
if (compressedSize > ZipConstants.CryptoHeaderSize + 2)
if (compressedSize > entry.EncryptionOverheadSize + 2)
{
throw new ZipException("Directory compressed size invalid");
}
}
else if (compressedSize > 2)
{
// When not compressed the directory size can validly be 2 bytes
// if the true size wasnt known when data was originally being written.
// if the true size wasn't known when data was originally being written.
// NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes
throw new ZipException("Directory compressed size invalid");
}
Expand Down
10 changes: 5 additions & 5 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs
Expand Up @@ -875,13 +875,13 @@ public void TestDirectoryEntry()
TestDirectoryEntry(new MemoryStreamWithoutSeek());
}

private void TestEncryptedDirectoryEntry(MemoryStream s)
private void TestEncryptedDirectoryEntry(MemoryStream s, int aesKeySize)
{
var outStream = new ZipOutputStream(s);
outStream.Password = "Tonto hand me a beer";

outStream.IsStreamOwner = false;
outStream.PutNextEntry(new ZipEntry("YeUnreadableDirectory/"));
outStream.PutNextEntry(new ZipEntry("YeUnreadableDirectory/") { AESKeySize = aesKeySize } );
outStream.Close();

var ms2 = new MemoryStream(s.ToArray());
Expand All @@ -893,10 +893,10 @@ private void TestEncryptedDirectoryEntry(MemoryStream s)

[Test]
[Category("Zip")]
public void TestEncryptedDirectoryEntry()
public void TestEncryptedDirectoryEntry([Values(0, 128, 256)]int aesKeySize)
{
TestEncryptedDirectoryEntry(new MemoryStream());
TestEncryptedDirectoryEntry(new MemoryStreamWithoutSeek());
TestEncryptedDirectoryEntry(new MemoryStream(), aesKeySize);
TestEncryptedDirectoryEntry(new MemoryStreamWithoutSeek(), aesKeySize);
}

[Test]
Expand Down

0 comments on commit 61d3a21

Please sign in to comment.