From 71cc7bbd0f081079cb101185cfdf7defb3f9d1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sat, 8 Aug 2020 17:01:53 +0200 Subject: [PATCH] PR #502: Fix tests and ZipEntry DateTime Kind * Normalize DateTime kind in ZipEntry * Use Unspecifed DateTime Kind for entries * Fix tests --- src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs | 2 +- .../Zip/StreamHandling.cs | 35 +++++++++++++++++-- .../Zip/WindowsNameTransformHandling.cs | 8 +++++ .../Zip/ZipEntryFactoryHandling.cs | 1 + .../Zip/ZipNameTransformHandling.cs | 14 +++++--- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs index 14f760286..f119a1c4a 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs @@ -742,7 +742,7 @@ public long DosTime uint mon = Math.Max(1, Math.Min(12, ((uint)(value >> 21) & 0xf))); uint year = ((dosTime >> 25) & 0x7f) + 1980; int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((value >> 16) & 0x1f))); - DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Utc); + DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Unspecified); } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs index 5ba337f51..1adebe2ab 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs @@ -244,11 +244,14 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt using (var dummyZip = Utils.GetDummyFile(0)) using (var inputFile = Utils.GetDummyFile(contentLength)) { + // Filename is manually cleaned here to prevent this test from failing while ZipEntry doesn't automatically clean it + var inputFileName = ZipEntry.CleanName(inputFile.Filename); + using (var zipFileStream = File.OpenWrite(dummyZip.Filename)) using (var zipOutputStream = new ZipOutputStream(zipFileStream)) using (var inputFileStream = File.OpenRead(inputFile.Filename)) { - zipOutputStream.PutNextEntry(new ZipEntry(inputFile.Filename) + zipOutputStream.PutNextEntry(new ZipEntry(inputFileName) { CompressionMethod = CompressionMethod.Stored, }); @@ -260,7 +263,6 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt { var inputBytes = File.ReadAllBytes(inputFile.Filename); - var inputFileName = ZipEntry.CleanName(inputFile.Filename); var entry = zf.GetEntry(inputFileName); Assert.IsNotNull(entry, "No entry matching source file \"{0}\" found in archive, found \"{1}\"", inputFileName, zf[0].Name); @@ -282,6 +284,35 @@ public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLengt } } + [Test] + [Category("Zip")] + [Category("KnownBugs")] + public void ZipEntryFileNameAutoClean() + { + using (var dummyZip = Utils.GetDummyFile(0)) + using (var inputFile = Utils.GetDummyFile()) { + using (var zipFileStream = File.OpenWrite(dummyZip.Filename)) + using (var zipOutputStream = new ZipOutputStream(zipFileStream)) + using (var inputFileStream = File.OpenRead(inputFile.Filename)) + { + zipOutputStream.PutNextEntry(new ZipEntry(inputFile.Filename) + { + CompressionMethod = CompressionMethod.Stored, + }); + + inputFileStream.CopyTo(zipOutputStream); + } + + using (var zf = new ZipFile(dummyZip.Filename)) + { + Assert.AreNotEqual(ZipEntry.CleanName(inputFile.Filename), zf[0].Name, + "Entry file name \"{0}\" WAS automatically cleaned, this test should be removed", inputFile.Filename); + } + + Assert.Warn("Entry file name \"{0}\" was not automatically cleaned", inputFile.Filename); + } + } + /// /// Empty zips can be created and read? /// diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs index 7e7d440be..8e6941251 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs @@ -2,12 +2,20 @@ using NUnit.Framework; using System; using System.IO; +using System.Runtime.InteropServices; namespace ICSharpCode.SharpZipLib.Tests.Zip { [TestFixture] public class WindowsNameTransformHandling : TransformBase { + [OneTimeSetUp] + public void TestInit() { + if (Path.DirectorySeparatorChar != '\\') { + Assert.Inconclusive("WindowsNameTransform will not work on platforms not using '\\' directory separators"); + } + } + [Test] public void BasicFiles() { diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs index ab2ae3744..c1ba17f64 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs @@ -106,6 +106,7 @@ public void CreatedValues() var lastAccessTime = new DateTime(2050, 11, 3, 0, 42, 12); string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip"); + using (FileStream f = File.Create(tempFile, 1024)) { f.WriteByte(0); diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs index 634b9a0ab..498a8f723 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs @@ -80,10 +80,16 @@ public void NameTransforms() [Category("Zip")] public void FilenameCleaning() { - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName("hello"), "hello", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"z:\eccles"), "eccles", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\eccles"), "eccles", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\dir\eccles"), "dir/eccles", StringComparison.Ordinal)); + Assert.AreEqual(ZipEntry.CleanName("hello"), "hello"); + if(Environment.OSVersion.Platform == PlatformID.Win32NT) + { + Assert.AreEqual(ZipEntry.CleanName(@"z:\eccles"), "eccles"); + Assert.AreEqual(ZipEntry.CleanName(@"\\server\share\eccles"), "eccles"); + Assert.AreEqual(ZipEntry.CleanName(@"\\server\share\dir\eccles"), "dir/eccles"); + } + else { + Assert.AreEqual(ZipEntry.CleanName(@"/eccles"), "eccles"); + } } [Test]