Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit baa3151

Browse files
committed
Merge pull request #1948 from stephentoub/zip_file_test_tmps
Modify ZipFile tests to remove temporary garbage
2 parents fe41c84 + 28d96af commit baa3151

File tree

6 files changed

+178
-83
lines changed

6 files changed

+178
-83
lines changed

src/Common/tests/Compression/Utilities/StreamHelpers.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,11 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
7-
using System.Reflection;
86
using System.Threading.Tasks;
97

108
public static partial class StreamHelpers
119
{
12-
public static String GetTmpFileName()
13-
{
14-
return Path.GetRandomFileName();
15-
}
16-
17-
private const string dirPrefix = "ZipTests";
18-
private static int s_dirCount = 1;
19-
public static String GetTmpPath(bool create = false)
20-
{
21-
var root = Path.GetTempPath();
22-
string subDir = dirPrefix + s_dirCount.ToString();
23-
var tempPath = Path.Combine(root, subDir);
24-
s_dirCount++;
25-
26-
while (Directory.Exists(tempPath))
27-
{
28-
subDir = dirPrefix + s_dirCount.ToString();
29-
tempPath = Path.Combine(root, subDir);
30-
s_dirCount++;
31-
}
32-
33-
if (create)
34-
{
35-
Directory.CreateDirectory(tempPath);
36-
}
37-
38-
return tempPath;
39-
}
40-
4110
public static async Task<Stream> CreateTempCopyStream(String path)
4211
{
4312
var bytes = File.ReadAllBytes(path);
@@ -48,15 +17,4 @@ public static async Task<Stream> CreateTempCopyStream(String path)
4817

4918
return ms;
5019
}
51-
52-
public static String CreateTempCopyFile(String path)
53-
{
54-
var bytes = File.ReadAllBytes(path);
55-
56-
var dir = Path.GetDirectoryName(path);
57-
var newN = Path.Combine(dir, GetTmpFileName());
58-
File.WriteAllBytes(newN, bytes);
59-
60-
return newN;
61-
}
6220
}

src/System.IO.Compression.ZipFile/tests/System.IO.Compression.ZipFile.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PropertyGroup>
55
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
66
<Platform Condition="'$(Platform)' == ''">AnyCPU</Platform>
7-
<ProjectGuid>{ED453083-A80F-480c-AD25-5B8618E8A303}</ProjectGuid>
7+
<ProjectGuid>{ED453083-A80F-480C-AD25-5B8618E8A303}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AssemblyName>System.IO.Compression.ZipFile.Tests</AssemblyName>
1010
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -20,6 +20,7 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<Compile Include="ZipFileConvenienceMethods.cs" />
23+
<Compile Include="ZipFileTests.cs" />
2324
<Compile Include="ZipFileInvalidFileTests.cs" />
2425
<Compile Include="ZipFileReadOpenUpdateTests.cs" />
2526
<Compile Include="$(CommonTestPath)\Compression\Utilities\CRC.cs" />

src/System.IO.Compression.ZipFile/tests/ZipFileConvenienceMethods.cs

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.IO;
4+
using System.Text;
55
using System.Threading.Tasks;
66
using Xunit;
77

@@ -10,7 +10,7 @@ namespace System.IO.Compression.Test
1010
public partial class ZipTest
1111
{
1212
[Fact]
13-
public static async Task CreateFromDirectoryNormal()
13+
public async Task CreateFromDirectoryNormal()
1414
{
1515
await TestCreateDirectory(zfolder("normal"), true);
1616
if (Interop.IsWindows) // [ActiveIssue(846, PlatformID.AnyUnix)]
@@ -19,22 +19,22 @@ public static async Task CreateFromDirectoryNormal()
1919
}
2020
}
2121

22-
private static async Task TestCreateDirectory(String folderName, Boolean testWithBaseDir)
22+
private async Task TestCreateDirectory(string folderName, Boolean testWithBaseDir)
2323
{
24-
String noBaseDir = StreamHelpers.GetTmpFileName();
24+
string noBaseDir = GetTmpFilePath();
2525
ZipFile.CreateFromDirectory(folderName, noBaseDir);
2626

2727
await IsZipSameAsDirAsync(noBaseDir, folderName, ZipArchiveMode.Read, true, true);
2828

2929
if (testWithBaseDir)
3030
{
31-
String withBaseDir = StreamHelpers.GetTmpFileName();
31+
string withBaseDir = GetTmpFilePath();
3232
ZipFile.CreateFromDirectory(folderName, withBaseDir, CompressionLevel.Optimal, true);
3333
SameExceptForBaseDir(noBaseDir, withBaseDir, folderName);
3434
}
3535
}
3636

37-
private static void SameExceptForBaseDir(String zipNoBaseDir, String zipBaseDir, String baseDir)
37+
private static void SameExceptForBaseDir(string zipNoBaseDir, string zipBaseDir, string baseDir)
3838
{
3939
//b has the base dir
4040
using (ZipArchive a = ZipFile.Open(zipNoBaseDir, ZipArchiveMode.Read),
@@ -62,7 +62,7 @@ private static void SameExceptForBaseDir(String zipNoBaseDir, String zipBaseDir,
6262
}
6363

6464
[Fact]
65-
public static void ExtractToDirectoryNormal()
65+
public void ExtractToDirectoryNormal()
6666
{
6767
TestExtract(zfile("normal.zip"), zfolder("normal"));
6868
if (Interop.IsWindows) // [ActiveIssue(846, PlatformID.AnyUnix)]
@@ -77,38 +77,54 @@ public static void ExtractToDirectoryNormal()
7777
TestExtract(zfile("noexplicitdir.zip"), zfolder("explicitdir"));
7878
}
7979

80-
private static void TestExtract(String zipFileName, String folderName)
80+
private void TestExtract(string zipFileName, string folderName)
8181
{
82-
String tempFolder = StreamHelpers.GetTmpPath(true);
82+
string tempFolder = GetTmpDirPath(true);
8383
ZipFile.ExtractToDirectory(zipFileName, tempFolder);
8484
DirsEqual(tempFolder, folderName);
85+
86+
Assert.Throws<ArgumentNullException>(() => ZipFile.ExtractToDirectory(null, tempFolder));
8587
}
8688

8789
#region "Extension Methods"
8890

89-
[Fact]
90-
public static async Task CreateEntryFromFileTest()
91+
[Theory]
92+
[InlineData(true)]
93+
[InlineData(false)]
94+
public async Task CreateEntryFromFileTest(bool withCompressionLevel)
9195
{
9296
//add file
93-
String testArchive = StreamHelpers.CreateTempCopyFile(zfile("normal.zip"));
97+
string testArchive = CreateTempCopyFile(zfile("normal.zip"));
9498

9599
using (ZipArchive archive = ZipFile.Open(testArchive, ZipArchiveMode.Update))
96100
{
97-
ZipArchiveEntry e = archive.CreateEntryFromFile(zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
101+
string entryName = "added.txt";
102+
string sourceFilePath = zmodified(Path.Combine("addFile", entryName));
103+
104+
Assert.Throws<ArgumentNullException>(() => ((ZipArchive)null).CreateEntryFromFile(sourceFilePath, entryName));
105+
Assert.Throws<ArgumentNullException>(() => archive.CreateEntryFromFile(null, entryName));
106+
Assert.Throws<ArgumentNullException>(() => archive.CreateEntryFromFile(sourceFilePath, null));
107+
108+
ZipArchiveEntry e = withCompressionLevel ?
109+
archive.CreateEntryFromFile(sourceFilePath, entryName) :
110+
archive.CreateEntryFromFile(sourceFilePath, entryName, CompressionLevel.Fastest);
98111
Assert.NotNull(e);
99112
}
100113

101114
await IsZipSameAsDirAsync(testArchive, zmodified("addFile"), ZipArchiveMode.Read, true, true);
102115
}
103116

104117
[Fact]
105-
public static void ExtractToFileTest()
118+
public void ExtractToFileTest()
106119
{
107120
using (ZipArchive archive = ZipFile.Open(zfile("normal.zip"), ZipArchiveMode.Read))
108121
{
109-
String file = StreamHelpers.GetTmpFileName();
122+
string file = GetTmpFilePath();
110123
ZipArchiveEntry e = archive.GetEntry("first.txt");
111124

125+
Assert.Throws<ArgumentNullException>(() => ((ZipArchiveEntry)null).ExtractToFile(file));
126+
Assert.Throws<ArgumentNullException>(() => e.ExtractToFile(null));
127+
112128
//extract when there is nothing there
113129
e.ExtractToFile(file);
114130

@@ -133,11 +149,13 @@ public static void ExtractToFileTest()
133149
}
134150

135151
[Fact]
136-
public static void ExtractToDirectoryTest()
152+
public void ExtractToDirectoryTest()
137153
{
138154
using (ZipArchive archive = ZipFile.Open(zfile("normal.zip"), ZipArchiveMode.Read))
139155
{
140-
String tempFolder = StreamHelpers.GetTmpPath(false);
156+
string tempFolder = GetTmpDirPath(false);
157+
Assert.Throws<ArgumentNullException>(() => ((ZipArchive)null).ExtractToDirectory(tempFolder));
158+
Assert.Throws<ArgumentNullException>(() => archive.ExtractToDirectory(null));
141159
archive.ExtractToDirectory(tempFolder);
142160

143161
DirsEqual(tempFolder, zfolder("normal"));
@@ -147,14 +165,48 @@ public static void ExtractToDirectoryTest()
147165
{
148166
using (ZipArchive archive = ZipFile.OpenRead(zfile("unicode.zip")))
149167
{
150-
String tempFolder = StreamHelpers.GetTmpPath(false);
168+
string tempFolder = GetTmpDirPath(false);
151169
archive.ExtractToDirectory(tempFolder);
152170

153171
DirsEqual(tempFolder, zfolder("unicode"));
154172
}
155173
}
156174
}
157175

176+
[Fact]
177+
public void CreatedEmptyDirectoriesRoundtrip()
178+
{
179+
DirectoryInfo rootDir = new DirectoryInfo(GetTmpDirPath(create: true));
180+
rootDir.CreateSubdirectory("empty1");
181+
182+
string archivePath = GetTmpFilePath();
183+
ZipFile.CreateFromDirectory(
184+
rootDir.FullName, archivePath,
185+
CompressionLevel.Optimal, false, Encoding.UTF8);
186+
187+
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
188+
{
189+
Assert.Equal(1, archive.Entries.Count);
190+
Assert.True(archive.Entries[0].FullName.StartsWith("empty1"));
191+
}
192+
}
193+
194+
[Fact]
195+
public void CreatedEmptyRootDirectoryRoundtrips()
196+
{
197+
DirectoryInfo emptyRoot = new DirectoryInfo(GetTmpDirPath(create: true));
198+
199+
string archivePath = GetTmpFilePath();
200+
ZipFile.CreateFromDirectory(
201+
emptyRoot.FullName, archivePath,
202+
CompressionLevel.Optimal, true);
203+
204+
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
205+
{
206+
Assert.Equal(1, archive.Entries.Count);
207+
}
208+
}
209+
158210
#endregion
159211
}
160212
}

src/System.IO.Compression.ZipFile/tests/ZipFileInvalidFileTests.cs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.IO;
5-
using System.Threading.Tasks;
64
using Xunit;
75

86
namespace System.IO.Compression.Test
97
{
108
public partial class ZipTest
119
{
12-
private static void ConstructorThrows<TException>(Func<ZipArchive> constructor, String Message = "") where TException : Exception
10+
private static void ConstructorThrows<TException>(Func<ZipArchive> constructor, string Message = "") where TException : Exception
1311
{
1412
Assert.Throws<TException>(() =>
1513
{
@@ -18,9 +16,9 @@ private static void ConstructorThrows<TException>(Func<ZipArchive> constructor,
1816
}
1917

2018
[Fact]
21-
public static void InvalidInstanceMethods()
19+
public void InvalidInstanceMethods()
2220
{
23-
String zipFileName = StreamHelpers.CreateTempCopyFile(zfile("normal.zip"));
21+
string zipFileName = CreateTempCopyFile(zfile("normal.zip"));
2422
using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Update))
2523
{
2624
//non-existent entry
@@ -37,15 +35,15 @@ public static void InvalidInstanceMethods()
3735
}
3836

3937
[Fact]
40-
public static void InvalidConstructors()
38+
public void InvalidConstructors()
4139
{
4240
//out of range enum values
4341
ConstructorThrows<ArgumentOutOfRangeException>(() =>
4442
ZipFile.Open("bad file", (ZipArchiveMode)(10)));
4543
}
4644

4745
[Fact]
48-
public static void InvalidFiles()
46+
public void InvalidFiles()
4947
{
5048
ConstructorThrows<InvalidDataException>(() => ZipFile.OpenRead(bad("EOCDmissing.zip")));
5149
ConstructorThrows<InvalidDataException>(() => ZipFile.Open(bad("EOCDmissing.zip"), ZipArchiveMode.Update));
@@ -99,15 +97,15 @@ public static void InvalidFiles()
9997
}
10098

10199
[Fact]
102-
public static void UnsupportedCompression()
100+
public void UnsupportedCompression()
103101
{
104102
//lzma compression method
105103
UnsupportedCompressionRoutine(bad("LZMA.zip"), true);
106104

107105
UnsupportedCompressionRoutine(bad("invalidDeflate.zip"), false);
108106
}
109107

110-
private static void UnsupportedCompressionRoutine(String filename, Boolean throwsOnOpen)
108+
private void UnsupportedCompressionRoutine(string filename, Boolean throwsOnOpen)
111109
{
112110
using (ZipArchive archive = ZipFile.OpenRead(filename))
113111
{
@@ -125,8 +123,8 @@ private static void UnsupportedCompressionRoutine(String filename, Boolean throw
125123
}
126124
}
127125

128-
String updatedCopyName = StreamHelpers.CreateTempCopyFile(filename);
129-
String name;
126+
string updatedCopyName = CreateTempCopyFile(filename);
127+
string name;
130128
Int64 length, compressedLength;
131129
DateTimeOffset lastWriteTime;
132130
using (ZipArchive archive = ZipFile.Open(updatedCopyName, ZipArchiveMode.Update))
@@ -152,13 +150,52 @@ private static void UnsupportedCompressionRoutine(String filename, Boolean throw
152150
}
153151

154152
[Fact]
155-
public static void InvalidDates()
153+
public void InvalidDates()
156154
{
157155
using (ZipArchive archive = ZipFile.OpenRead(bad("invaliddate.zip")))
158156
{
159157
Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime);
160158
}
159+
160+
FileInfo fileWithBadDate = new FileInfo(GetTmpFilePath());
161+
fileWithBadDate.Create().Dispose();
162+
fileWithBadDate.LastWriteTimeUtc = new DateTime(1970, 1, 1, 1, 1, 1);
163+
164+
string archivePath = GetTmpFilePath();
165+
using (FileStream output = File.Open(archivePath, FileMode.Create))
166+
using (ZipArchive archive = new ZipArchive(output, ZipArchiveMode.Create))
167+
{
168+
archive.CreateEntryFromFile(fileWithBadDate.FullName, "SomeEntryName");
169+
}
170+
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
171+
{
172+
Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime);
173+
}
161174
}
175+
176+
[Fact]
177+
public void FilesOutsideDirectory()
178+
{
179+
string archivePath = GetTmpFilePath();
180+
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
181+
using (StreamWriter writer = new StreamWriter(archive.CreateEntry(Path.Combine("..", "entry1"), CompressionLevel.Optimal).Open()))
182+
{
183+
writer.Write("This is a test.");
184+
}
185+
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath()));
186+
}
187+
188+
[Fact]
189+
public void DirectoryEntryWithData()
190+
{
191+
string archivePath = GetTmpFilePath();
192+
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
193+
using (StreamWriter writer = new StreamWriter(archive.CreateEntry("testdir" + Path.DirectorySeparatorChar, CompressionLevel.Optimal).Open()))
194+
{
195+
writer.Write("This is a test.");
196+
}
197+
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(archivePath, GetTmpDirPath()));
198+
}
199+
162200
}
163201
}
164-

0 commit comments

Comments
 (0)