// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using Xunit; namespace System.IO.Compression.Tests { public partial class ZipTest { private static void ConstructorThrows(Func constructor, string Message = "") where TException : Exception { Assert.Throws(() => { using (ZipArchive archive = constructor()) { } }); } [Fact] public void InvalidInstanceMethods() { string zipFileName = CreateTempCopyFile(zfile("normal.zip")); using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Update)) { //non-existent entry Assert.True(null == archive.GetEntry("nonExistentEntry")); //null/empty string Assert.Throws(() => archive.GetEntry(null)); ZipArchiveEntry entry = archive.GetEntry("first.txt"); //null/empty string Assert.Throws(() => archive.CreateEntry("")); Assert.Throws(() => archive.CreateEntry(null)); } } [Fact] public void InvalidConstructors() { //out of range enum values ConstructorThrows(() => ZipFile.Open("bad file", (ZipArchiveMode)(10))); } [Fact] public void InvalidFiles() { ConstructorThrows(() => ZipFile.OpenRead(bad("EOCDmissing.zip"))); ConstructorThrows(() => ZipFile.Open(bad("EOCDmissing.zip"), ZipArchiveMode.Update)); ConstructorThrows(() => ZipFile.OpenRead(bad("CDoffsetOutOfBounds.zip"))); ConstructorThrows(() => ZipFile.Open(bad("CDoffsetOutOfBounds.zip"), ZipArchiveMode.Update)); using (ZipArchive archive = ZipFile.OpenRead(bad("CDoffsetInBoundsWrong.zip"))) { Assert.Throws(() => { var x = archive.Entries; }); } ConstructorThrows(() => ZipFile.Open(bad("CDoffsetInBoundsWrong.zip"), ZipArchiveMode.Update)); using (ZipArchive archive = ZipFile.OpenRead(bad("numberOfEntriesDifferent.zip"))) { Assert.Throws(() => { var x = archive.Entries; }); } ConstructorThrows(() => ZipFile.Open(bad("numberOfEntriesDifferent.zip"), ZipArchiveMode.Update)); //read mode on empty file ConstructorThrows(() => new ZipArchive(new MemoryStream())); //offset out of bounds using (ZipArchive archive = ZipFile.OpenRead(bad("localFileOffsetOutOfBounds.zip"))) { ZipArchiveEntry e = archive.Entries[0]; Assert.Throws(() => e.Open()); } ConstructorThrows(() => ZipFile.Open(bad("localFileOffsetOutOfBounds.zip"), ZipArchiveMode.Update)); //compressed data offset + compressed size out of bounds using (ZipArchive archive = ZipFile.OpenRead(bad("compressedSizeOutOfBounds.zip"))) { ZipArchiveEntry e = archive.Entries[0]; Assert.Throws(() => e.Open()); } ConstructorThrows(() => ZipFile.Open(bad("compressedSizeOutOfBounds.zip"), ZipArchiveMode.Update)); //signature wrong using (ZipArchive archive = ZipFile.OpenRead(bad("localFileHeaderSignatureWrong.zip"))) { ZipArchiveEntry e = archive.Entries[0]; Assert.Throws(() => e.Open()); } ConstructorThrows(() => ZipFile.Open(bad("localFileHeaderSignatureWrong.zip"), ZipArchiveMode.Update)); } [Fact] public void UnsupportedCompression() { //lzma compression method UnsupportedCompressionRoutine(bad("LZMA.zip"), true); UnsupportedCompressionRoutine(bad("invalidDeflate.zip"), false); } private void UnsupportedCompressionRoutine(string filename, Boolean throwsOnOpen) { using (ZipArchive archive = ZipFile.OpenRead(filename)) { ZipArchiveEntry e = archive.Entries[0]; if (throwsOnOpen) { Assert.Throws(() => e.Open()); } else { using (Stream s = e.Open()) { Assert.Throws(() => s.ReadByte()); } } } string updatedCopyName = CreateTempCopyFile(filename); string name; Int64 length, compressedLength; DateTimeOffset lastWriteTime; using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update)) { ZipArchiveEntry e = archive.Entries[0]; name = e.FullName; lastWriteTime = e.LastWriteTime; length = e.Length; compressedLength = e.CompressedLength; Assert.Throws(() => e.Open()); } //make sure that update mode preserves that unreadable file using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update)) { ZipArchiveEntry e = archive.Entries[0]; Assert.Equal(name, e.FullName); Assert.Equal(lastWriteTime, e.LastWriteTime); Assert.Equal(length, e.Length); Assert.Equal(compressedLength, e.CompressedLength); Assert.Throws(() => e.Open()); } } [Fact] public void InvalidDates() { using (ZipArchive archive = ZipFile.OpenRead(bad("invaliddate.zip"))) { Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime); } FileInfo fileWithBadDate = new FileInfo(GetTmpFilePath()); fileWithBadDate.Create().Dispose(); fileWithBadDate.LastWriteTimeUtc = new DateTime(1970, 1, 1, 1, 1, 1); string archivePath = GetTmpFilePath(); using (FileStream output = File.Open(archivePath, FileMode.Create)) using (ZipArchive archive = new ZipArchive(output, ZipArchiveMode.Create)) { archive.CreateEntryFromFile(fileWithBadDate.FullName, "SomeEntryName"); } using (ZipArchive archive = ZipFile.OpenRead(archivePath)) { Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime); } } [Fact] public void FilesOutsideDirectory() { string archivePath = GetTmpFilePath(); using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) using (StreamWriter writer = new StreamWriter(archive.CreateEntry(Path.Combine("..", "entry1"), CompressionLevel.Optimal).Open())) { writer.Write("This is a test."); } Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath())); } [Fact] public void DirectoryEntryWithData() { string archivePath = GetTmpFilePath(); using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) using (StreamWriter writer = new StreamWriter(archive.CreateEntry("testdir" + Path.DirectorySeparatorChar, CompressionLevel.Optimal).Open())) { writer.Write("This is a test."); } Assert.Throws(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath())); } /// /// This test ensures that a zipfile with path names that are invalid to this OS will throw errors /// when an attempt is made to extract them. /// [Theory] [InlineData("NullCharFileName_FromWindows")] [InlineData("NullCharFileName_FromUnix")] [PlatformSpecific(PlatformID.AnyUnix)] public void Unix_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName) { Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTmpDirPath())); } [Theory] [InlineData("backslashes_FromUnix", "aa\\bb\\cc\\dd")] [InlineData("backslashes_FromWindows", "aa\\bb\\cc\\dd")] [InlineData("WindowsInvalid_FromUnix", "aad")] [InlineData("WindowsInvalid_FromWindows", "aad")] [PlatformSpecific(PlatformID.AnyUnix)] public void Unix_ZipWithOSSpecificFileNames(string zipName, string fileName) { string tempDir = GetTmpDirPath(); ZipFile.ExtractToDirectory(compat(zipName) + ".zip", tempDir); string[] results = Directory.GetFiles(tempDir, "*", SearchOption.AllDirectories); Assert.Equal(1, results.Length); Assert.Equal(fileName, Path.GetFileName(results[0])); } /// /// This test ensures that a zipfile with path names that are invalid to this OS will throw errors /// when an attempt is made to extract them. /// [Theory] [InlineData("WindowsInvalid_FromUnix")] [InlineData("WindowsInvalid_FromWindows")] [InlineData("NullCharFileName_FromWindows")] [InlineData("NullCharFileName_FromUnix")] [PlatformSpecific(PlatformID.Windows)] public void Windows_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName) { Assert.Throws(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTmpDirPath())); } [Theory] [InlineData("backslashes_FromUnix", "dd")] [InlineData("backslashes_FromWindows", "dd")] [PlatformSpecific(PlatformID.Windows)] public void Windows_ZipWithOSSpecificFileNames(string zipName, string fileName) { string tempDir = GetTmpDirPath(); ZipFile.ExtractToDirectory(compat(zipName) + ".zip", tempDir); string[] results = Directory.GetFiles(tempDir, "*", SearchOption.AllDirectories); Assert.Equal(1, results.Length); Assert.Equal(fileName, Path.GetFileName(results[0])); } } }