From 61d3a210800251491b8afa198bbc4ec2935efc9a Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Sun, 16 Aug 2020 14:03:24 +0100 Subject: [PATCH] PR #503: Consider AES overhead when testing encrypted folder entries * Extend the ZipFile.TestEncryptedDirectoryEntry test to test AES encrypted directories * Consider AES overhead when testing encrypted folder entries --- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 4 ++-- .../Zip/ZipFileHandling.cs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index 688142f95..02fd30778 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -1289,7 +1289,7 @@ 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"); } @@ -1297,7 +1297,7 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests) 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"); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index 996b09213..7ae234523 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -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()); @@ -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]