Skip to content

Commit 91050e6

Browse files
authored
PR #511: Move the 7zip helper functions into their own class for easier reuse
1 parent 1e351fc commit 91050e6

2 files changed

Lines changed: 104 additions & 96 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using NUnit.Framework;
5+
6+
namespace ICSharpCode.SharpZipLib.Tests.TestSupport
7+
{
8+
// Helper class for verifying zips with 7-zip
9+
internal static class SevenZipHelper
10+
{
11+
private static readonly string[] possible7zPaths = new[] {
12+
// Check in PATH
13+
"7z", "7za",
14+
15+
// Check in default install location
16+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "7-Zip", "7z.exe"),
17+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "7-Zip", "7z.exe"),
18+
};
19+
20+
public static bool TryGet7zBinPath(out string path7z)
21+
{
22+
var runTimeLimit = TimeSpan.FromSeconds(3);
23+
24+
foreach (var testPath in possible7zPaths)
25+
{
26+
try
27+
{
28+
var p = Process.Start(new ProcessStartInfo(testPath, "i")
29+
{
30+
RedirectStandardOutput = true,
31+
UseShellExecute = false
32+
});
33+
while (!p.StandardOutput.EndOfStream && (DateTime.Now - p.StartTime) < runTimeLimit)
34+
{
35+
p.StandardOutput.DiscardBufferedData();
36+
}
37+
if (!p.HasExited)
38+
{
39+
p.Close();
40+
Assert.Warn($"Timed out checking for 7z binary in \"{testPath}\"!");
41+
continue;
42+
}
43+
44+
if (p.ExitCode == 0)
45+
{
46+
path7z = testPath;
47+
return true;
48+
}
49+
}
50+
catch (Exception)
51+
{
52+
continue;
53+
}
54+
}
55+
path7z = null;
56+
return false;
57+
}
58+
59+
/// <summary>
60+
/// Helper function to verify the provided zip stream with 7Zip.
61+
/// </summary>
62+
/// <param name="zipStream">A stream containing the zip archive to test.</param>
63+
/// <param name="password">The password for the archive.</param>
64+
internal static void VerifyZipWith7Zip(Stream zipStream, string password)
65+
{
66+
if (TryGet7zBinPath(out string path7z))
67+
{
68+
Console.WriteLine($"Using 7z path: \"{path7z}\"");
69+
70+
var fileName = Path.GetTempFileName();
71+
72+
try
73+
{
74+
using (var fs = File.OpenWrite(fileName))
75+
{
76+
zipStream.Seek(0, SeekOrigin.Begin);
77+
zipStream.CopyTo(fs);
78+
}
79+
80+
var p = Process.Start(path7z, $"t -p{password} \"{fileName}\"");
81+
if (!p.WaitForExit(2000))
82+
{
83+
Assert.Warn("Timed out verifying zip file!");
84+
}
85+
86+
Assert.AreEqual(0, p.ExitCode, "Archive verification failed");
87+
}
88+
finally
89+
{
90+
File.Delete(fileName);
91+
}
92+
}
93+
else
94+
{
95+
Assert.Warn("Skipping file verification since 7za is not in path");
96+
}
97+
}
98+
}
99+
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs

Lines changed: 5 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using ICSharpCode.SharpZipLib.Core;
2-
using ICSharpCode.SharpZipLib.Zip;
1+
using ICSharpCode.SharpZipLib.Zip;
32
using NUnit.Framework;
43
using System;
5-
using System.Diagnostics;
64
using System.IO;
75
using System.Text;
86
using ICSharpCode.SharpZipLib.Tests.TestSupport;
@@ -74,7 +72,7 @@ public void ZipOutputStreamEncryptEmptyEntries(
7472
zipOutputStream.CloseEntry();
7573
}
7674

77-
VerifyZipWith7Zip(ms, "password");
75+
SevenZipHelper.VerifyZipWith7Zip(ms, "password");
7876
}
7977
}
8078

@@ -315,7 +313,7 @@ public void ZipFileAesAdd()
315313
}
316314

317315
// As an extra test, verify the file with 7-zip
318-
VerifyZipWith7Zip(memoryStream, password);
316+
SevenZipHelper.VerifyZipWith7Zip(memoryStream, password);
319317
}
320318
}
321319

@@ -399,7 +397,7 @@ public void ZipFileAesDelete()
399397
}
400398

401399
// As an extra test, verify the file with 7-zip
402-
VerifyZipWith7Zip(memoryStream, password);
400+
SevenZipHelper.VerifyZipWith7Zip(memoryStream, password);
403401
}
404402
}
405403

@@ -471,54 +469,6 @@ public void ZipinputStreamShouldGracefullyFailWithAESStreams()
471469
}
472470
}
473471

474-
private static readonly string[] possible7zPaths = new[] {
475-
// Check in PATH
476-
"7z", "7za",
477-
478-
// Check in default install location
479-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "7-Zip", "7z.exe"),
480-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "7-Zip", "7z.exe"),
481-
};
482-
483-
public static bool TryGet7zBinPath(out string path7z)
484-
{
485-
var runTimeLimit = TimeSpan.FromSeconds(3);
486-
487-
foreach (var testPath in possible7zPaths)
488-
{
489-
try
490-
{
491-
var p = Process.Start(new ProcessStartInfo(testPath, "i")
492-
{
493-
RedirectStandardOutput = true,
494-
UseShellExecute = false
495-
});
496-
while (!p.StandardOutput.EndOfStream && (DateTime.Now - p.StartTime) < runTimeLimit)
497-
{
498-
p.StandardOutput.DiscardBufferedData();
499-
}
500-
if (!p.HasExited)
501-
{
502-
p.Close();
503-
Assert.Warn($"Timed out checking for 7z binary in \"{testPath}\"!");
504-
continue;
505-
}
506-
507-
if (p.ExitCode == 0)
508-
{
509-
path7z = testPath;
510-
return true;
511-
}
512-
}
513-
catch (Exception)
514-
{
515-
continue;
516-
}
517-
}
518-
path7z = null;
519-
return false;
520-
}
521-
522472
public void WriteEncryptedZipToStream(Stream stream, string password, int keySize, CompressionMethod compressionMethod = CompressionMethod.Deflated)
523473
{
524474
using (var zs = new ZipOutputStream(stream))
@@ -572,51 +522,10 @@ public void CreateZipWithEncryptedEntries(string password, int keySize, Compress
572522
using (var ms = new MemoryStream())
573523
{
574524
WriteEncryptedZipToStream(ms, password, keySize, compressionMethod);
575-
VerifyZipWith7Zip(ms, password);
576-
}
577-
}
578-
579-
/// <summary>
580-
/// Helper function to verify the provided zip stream with 7Zip.
581-
/// </summary>
582-
/// <param name="zipStream">A stream containing the zip archive to test.</param>
583-
/// <param name="password">The password for the archive.</param>
584-
private void VerifyZipWith7Zip(Stream zipStream, string password)
585-
{
586-
if (TryGet7zBinPath(out string path7z))
587-
{
588-
Console.WriteLine($"Using 7z path: \"{path7z}\"");
589-
590-
var fileName = Path.GetTempFileName();
591-
592-
try
593-
{
594-
using (var fs = File.OpenWrite(fileName))
595-
{
596-
zipStream.Seek(0, SeekOrigin.Begin);
597-
zipStream.CopyTo(fs);
598-
}
599-
600-
var p = Process.Start(path7z, $"t -p{password} \"{fileName}\"");
601-
if (!p.WaitForExit(2000))
602-
{
603-
Assert.Warn("Timed out verifying zip file!");
604-
}
605-
606-
Assert.AreEqual(0, p.ExitCode, "Archive verification failed");
607-
}
608-
finally
609-
{
610-
File.Delete(fileName);
611-
}
612-
}
613-
else
614-
{
615-
Assert.Warn("Skipping file verification since 7za is not in path");
525+
SevenZipHelper.VerifyZipWith7Zip(ms, password);
616526
}
617527
}
618528

619-
620529
private const string DummyDataString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
621530
Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
622531
Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.

0 commit comments

Comments
 (0)