From 67a8fe2fb6fe0e9a78d8e77b85abd974cfb6bc98 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= <nils@piksel.se>
Date: Sun, 16 Sep 2018 15:33:01 +0200
Subject: [PATCH] Add test for AES decompress

---
 .../Zip/ZipEncryptionHandling.cs              | 79 ++++++++++++++-----
 1 file changed, 58 insertions(+), 21 deletions(-)

diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
index 956a5ca9d..cef7e7985 100644
--- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
+++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
@@ -27,6 +27,36 @@ public void Aes256Encryption()
 			CreateZipWithEncryptedEntries("foo", 256);
 		}
 
+		[Test]
+		[Category("Encryption")]
+		[Category("Zip")]
+		public void ZipFileAesDecryption()
+		{
+			var password = "password";
+
+			using (var ms = new MemoryStream())
+			{
+				WriteEncryptedZipToStream(ms, password, 256);
+
+				var zipFile = new ZipFile(ms)
+				{
+					Password = password
+				};
+
+				foreach (ZipEntry entry in zipFile)
+				{
+					if (!entry.IsFile) continue;
+					
+					using(var zis = zipFile.GetInputStream(entry))
+					using (var sr = new StreamReader(zis, Encoding.UTF8))
+					{
+						var content = sr.ReadToEnd();
+						Assert.AreEqual(DummyDataString, content, "Decompressed content does not match input data");
+					}
+				}
+			}
+		}
+
 		private static readonly string[] possible7zPaths = new[] {
 			// Check in PATH
 			"7z", "7za",
@@ -74,35 +104,37 @@ public static bool TryGet7zBinPath(out string path7z)
 			return false;
 		}
 
-		public void CreateZipWithEncryptedEntries(string password, int keySize)
+		public void WriteEncryptedZipToStream(Stream stream, string password, int keySize)
 		{
-			using (var ms = new MemoryStream())
+			using (var zs = new ZipOutputStream(stream))
 			{
-				using (var zs = new ZipOutputStream(ms))
-				{
-					zs.IsStreamOwner = false;
-					zs.SetLevel(9); // 0-9, 9 being the highest level of compression
-					zs.Password = password;  // optional. Null is the same as not setting. Required if using AES.
+				zs.IsStreamOwner = false;
+				zs.SetLevel(9); // 0-9, 9 being the highest level of compression
+				zs.Password = password;  // optional. Null is the same as not setting. Required if using AES.
 
-					ZipEntry zipEntry = new ZipEntry("test");
-					zipEntry.AESKeySize = keySize;
-					zipEntry.DateTime = DateTime.Now;
+				ZipEntry zipEntry = new ZipEntry("test");
+				zipEntry.AESKeySize = keySize;
+				zipEntry.DateTime = DateTime.Now;
 
-					zs.PutNextEntry(zipEntry);
+				zs.PutNextEntry(zipEntry);
 
-					byte[] dummyData = Encoding.UTF8.GetBytes(@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
-Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu. 
-Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem. 
-Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero.");
+				byte[] dummyData = Encoding.UTF8.GetBytes(DummyDataString);
 
-					using (var dummyStream = new MemoryStream(dummyData))
-					{
-						dummyStream.CopyTo(zs);
-					}
-
-					zs.CloseEntry();
+				using (var dummyStream = new MemoryStream(dummyData))
+				{
+					dummyStream.CopyTo(zs);
 				}
 
+				zs.CloseEntry();
+			}
+		}
+
+		public void CreateZipWithEncryptedEntries(string password, int keySize)
+		{
+			using (var ms = new MemoryStream())
+			{
+				WriteEncryptedZipToStream(ms, password, keySize);
+
 				if (TryGet7zBinPath(out string path7z))
 				{
 					Console.WriteLine($"Using 7z path: \"{path7z}\"");
@@ -140,5 +172,10 @@ public void CreateZipWithEncryptedEntries(string password, int keySize)
 			}
 
 		}
+
+		const string DummyDataString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
+Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu. 
+Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem. 
+Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero.";
 	}
 }
