Skip to content

Commit 88302c5

Browse files
authored
Merge PR #265, Add AES encryption tests
Add ZIP encryption tests for AES128 and AES256
1 parent 8d217ae commit 88302c5

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using ICSharpCode.SharpZipLib.Zip;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Text;
8+
9+
namespace ICSharpCode.SharpZipLib.Tests.Zip
10+
{
11+
[TestFixture]
12+
public class ZipEncryptionHandling
13+
{
14+
[Test]
15+
[Category("Encryption")]
16+
[Category("Zip")]
17+
public void Aes128Encryption()
18+
{
19+
CreateZipWithEncryptedEntries("foo", 128);
20+
}
21+
22+
[Test]
23+
[Category("Encryption")]
24+
[Category("Zip")]
25+
public void Aes256Encryption()
26+
{
27+
CreateZipWithEncryptedEntries("foo", 256);
28+
}
29+
30+
private static readonly string[] possible7zPaths = new[] {
31+
// Check in PATH
32+
"7z", "7za",
33+
34+
// Check in default install location
35+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "7-Zip", "7z.exe"),
36+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "7-Zip", "7z.exe"),
37+
};
38+
39+
public static bool TryGet7zBinPath(out string path7z)
40+
{
41+
var runTimeLimit = TimeSpan.FromSeconds(3);
42+
43+
foreach (var testPath in possible7zPaths)
44+
{
45+
try
46+
{
47+
var p = Process.Start(new ProcessStartInfo(testPath, "i")
48+
{
49+
RedirectStandardOutput = true
50+
});
51+
while (!p.StandardOutput.EndOfStream && (DateTime.Now - p.StartTime) < runTimeLimit)
52+
{
53+
p.StandardOutput.DiscardBufferedData();
54+
}
55+
if (!p.HasExited)
56+
{
57+
p.Close();
58+
Assert.Warn($"Timed out checking for 7z binary in \"{testPath}\"!");
59+
continue;
60+
}
61+
62+
if (p.ExitCode == 0)
63+
{
64+
path7z = testPath;
65+
return true;
66+
}
67+
}
68+
catch (Exception)
69+
{
70+
continue;
71+
}
72+
}
73+
path7z = null;
74+
return false;
75+
}
76+
77+
public void CreateZipWithEncryptedEntries(string password, int keySize)
78+
{
79+
using (var ms = new MemoryStream())
80+
{
81+
using (var zs = new ZipOutputStream(ms))
82+
{
83+
zs.IsStreamOwner = false;
84+
zs.SetLevel(9); // 0-9, 9 being the highest level of compression
85+
zs.Password = password; // optional. Null is the same as not setting. Required if using AES.
86+
87+
ZipEntry zipEntry = new ZipEntry("test");
88+
zipEntry.AESKeySize = keySize;
89+
zipEntry.DateTime = DateTime.Now;
90+
91+
zs.PutNextEntry(zipEntry);
92+
93+
byte[] dummyData = Encoding.UTF8.GetBytes(@"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
94+
Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
95+
Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.
96+
Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero.");
97+
98+
using (var dummyStream = new MemoryStream(dummyData))
99+
{
100+
dummyStream.CopyTo(zs);
101+
}
102+
103+
zs.CloseEntry();
104+
}
105+
106+
if (TryGet7zBinPath(out string path7z))
107+
{
108+
Console.WriteLine($"Using 7z path: \"{path7z}\"");
109+
110+
ms.Seek(0, SeekOrigin.Begin);
111+
112+
var fileName = Path.GetTempFileName();
113+
114+
try
115+
{
116+
117+
using (var fs = File.OpenWrite(fileName))
118+
{
119+
ms.CopyTo(fs);
120+
}
121+
122+
var p = Process.Start(path7z, $"t -p{password} {fileName}");
123+
if (!p.WaitForExit(2000))
124+
{
125+
Assert.Warn("Timed out verifying zip file!");
126+
}
127+
128+
Assert.AreEqual(0, p.ExitCode, "Archive verification failed");
129+
130+
}
131+
finally
132+
{
133+
File.Delete(fileName);
134+
}
135+
}
136+
else
137+
{
138+
Assert.Warn("Skipping file verification since 7za is not in path");
139+
}
140+
}
141+
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)