Skip to content

Commit 71cc7bb

Browse files
authored
PR #502: Fix tests and ZipEntry DateTime Kind
* Normalize DateTime kind in ZipEntry * Use Unspecifed DateTime Kind for entries * Fix tests
1 parent 24f948a commit 71cc7bb

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ public long DosTime
742742
uint mon = Math.Max(1, Math.Min(12, ((uint)(value >> 21) & 0xf)));
743743
uint year = ((dosTime >> 25) & 0x7f) + 1980;
744744
int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((value >> 16) & 0x1f)));
745-
DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Utc);
745+
DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Unspecified);
746746
}
747747
}
748748
}

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,14 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt
244244
using (var dummyZip = Utils.GetDummyFile(0))
245245
using (var inputFile = Utils.GetDummyFile(contentLength))
246246
{
247+
// Filename is manually cleaned here to prevent this test from failing while ZipEntry doesn't automatically clean it
248+
var inputFileName = ZipEntry.CleanName(inputFile.Filename);
249+
247250
using (var zipFileStream = File.OpenWrite(dummyZip.Filename))
248251
using (var zipOutputStream = new ZipOutputStream(zipFileStream))
249252
using (var inputFileStream = File.OpenRead(inputFile.Filename))
250253
{
251-
zipOutputStream.PutNextEntry(new ZipEntry(inputFile.Filename)
254+
zipOutputStream.PutNextEntry(new ZipEntry(inputFileName)
252255
{
253256
CompressionMethod = CompressionMethod.Stored,
254257
});
@@ -260,7 +263,6 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt
260263
{
261264
var inputBytes = File.ReadAllBytes(inputFile.Filename);
262265

263-
var inputFileName = ZipEntry.CleanName(inputFile.Filename);
264266
var entry = zf.GetEntry(inputFileName);
265267
Assert.IsNotNull(entry, "No entry matching source file \"{0}\" found in archive, found \"{1}\"", inputFileName, zf[0].Name);
266268

@@ -282,6 +284,35 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt
282284
}
283285
}
284286

287+
[Test]
288+
[Category("Zip")]
289+
[Category("KnownBugs")]
290+
public void ZipEntryFileNameAutoClean()
291+
{
292+
using (var dummyZip = Utils.GetDummyFile(0))
293+
using (var inputFile = Utils.GetDummyFile()) {
294+
using (var zipFileStream = File.OpenWrite(dummyZip.Filename))
295+
using (var zipOutputStream = new ZipOutputStream(zipFileStream))
296+
using (var inputFileStream = File.OpenRead(inputFile.Filename))
297+
{
298+
zipOutputStream.PutNextEntry(new ZipEntry(inputFile.Filename)
299+
{
300+
CompressionMethod = CompressionMethod.Stored,
301+
});
302+
303+
inputFileStream.CopyTo(zipOutputStream);
304+
}
305+
306+
using (var zf = new ZipFile(dummyZip.Filename))
307+
{
308+
Assert.AreNotEqual(ZipEntry.CleanName(inputFile.Filename), zf[0].Name,
309+
"Entry file name \"{0}\" WAS automatically cleaned, this test should be removed", inputFile.Filename);
310+
}
311+
312+
Assert.Warn("Entry file name \"{0}\" was not automatically cleaned", inputFile.Filename);
313+
}
314+
}
315+
285316
/// <summary>
286317
/// Empty zips can be created and read?
287318
/// </summary>

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

+8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
using NUnit.Framework;
33
using System;
44
using System.IO;
5+
using System.Runtime.InteropServices;
56

67
namespace ICSharpCode.SharpZipLib.Tests.Zip
78
{
89
[TestFixture]
910
public class WindowsNameTransformHandling : TransformBase
1011
{
12+
[OneTimeSetUp]
13+
public void TestInit() {
14+
if (Path.DirectorySeparatorChar != '\\') {
15+
Assert.Inconclusive("WindowsNameTransform will not work on platforms not using '\\' directory separators");
16+
}
17+
}
18+
1119
[Test]
1220
public void BasicFiles()
1321
{

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

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void CreatedValues()
106106
var lastAccessTime = new DateTime(2050, 11, 3, 0, 42, 12);
107107

108108
string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip");
109+
109110
using (FileStream f = File.Create(tempFile, 1024))
110111
{
111112
f.WriteByte(0);

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ public void NameTransforms()
8080
[Category("Zip")]
8181
public void FilenameCleaning()
8282
{
83-
Assert.AreEqual(0, string.Compare(ZipEntry.CleanName("hello"), "hello", StringComparison.Ordinal));
84-
Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"z:\eccles"), "eccles", StringComparison.Ordinal));
85-
Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\eccles"), "eccles", StringComparison.Ordinal));
86-
Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\dir\eccles"), "dir/eccles", StringComparison.Ordinal));
83+
Assert.AreEqual(ZipEntry.CleanName("hello"), "hello");
84+
if(Environment.OSVersion.Platform == PlatformID.Win32NT)
85+
{
86+
Assert.AreEqual(ZipEntry.CleanName(@"z:\eccles"), "eccles");
87+
Assert.AreEqual(ZipEntry.CleanName(@"\\server\share\eccles"), "eccles");
88+
Assert.AreEqual(ZipEntry.CleanName(@"\\server\share\dir\eccles"), "dir/eccles");
89+
}
90+
else {
91+
Assert.AreEqual(ZipEntry.CleanName(@"/eccles"), "eccles");
92+
}
8793
}
8894

8995
[Test]

0 commit comments

Comments
 (0)