[System.IO.Compression] Fixed Zip entries with unset last write time to return the...
[mono.git] / mcs / class / System.IO.Compression / Test / System.IO.Compression / ZipTest.cs
1 //
2 // ZipTests.cs
3 //
4 // Author:
5 //         Joao Matos <joao.matos@xamarin.com>
6 //
7 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.IO;
29 using System.IO.Compression;
30 using System.Linq;
31 using System.Security.Cryptography;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.IO.Compression
35 {
36         [TestFixture]
37         public class ZipArchiveTests
38         {
39                 static string GetSHA1HashFromFile(Stream stream)
40                 {
41                         using (var sha1 = SHA1.Create())
42                         {
43                                 return BitConverter.ToString(sha1.ComputeHash(stream))
44                                         .Replace("-", string.Empty);
45                         }
46                 }
47
48                 [Test]
49                 public void ZipGetEntryReadMode()
50                 {
51                         File.Copy("archive.zip", "test.zip", overwrite: true);
52                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
53                                 ZipArchiveMode.Read))
54                         {
55                                 var entry = archive.GetEntry("foo.txt");
56                                 Assert.IsNotNull(entry);
57
58                                 var nullEntry = archive.GetEntry("nonexisting");
59                                 Assert.IsNull(nullEntry);
60                         }
61
62                         File.Delete ("test.zip");
63                 }
64
65                 [Test]
66                 public void ZipGetEntryCreateMode()
67                 {
68                         File.Copy("archive.zip", "test.zip", overwrite: true);
69                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
70                                 ZipArchiveMode.Create))
71                         {
72                                 try {
73                                         archive.GetEntry("foo");
74                                 } catch(NotSupportedException ex) {
75                                         return;
76                                 }
77
78                                 Assert.Fail();
79                         }
80
81                         File.Delete ("test.zip");
82                 }
83
84                 [Test]
85                 public void ZipGetEntryUpdateMode()
86                 {
87                         File.Copy("archive.zip", "test.zip", overwrite: true);
88                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
89                                 ZipArchiveMode.Read))
90                         {
91                                 var entry = archive.GetEntry("foo.txt");
92                                 Assert.IsNotNull(entry);
93
94                                 var nullEntry = archive.GetEntry("nonexisting");
95                                 Assert.IsNull(nullEntry);
96                         }
97
98                         File.Delete ("test.zip");
99                 }
100
101                 [Test]
102                 public void ZipGetEntryOpen()
103                 {
104                         File.Copy("archive.zip", "test.zip", overwrite: true);
105                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
106                                 ZipArchiveMode.Read))
107                         {
108                                 var entry = archive.GetEntry("foo.txt");
109                                 Assert.IsNotNull(entry);
110
111                                 var foo = entry.Open();
112                         }
113
114                         File.Delete ("test.zip");
115                 }
116
117                 [Test]
118                 public void ZipOpenAndReopenEntry()
119                 {
120                         try {
121                                 File.Copy("archive.zip", "test.zip", overwrite: true);
122                                 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
123                                         ZipArchiveMode.Update))
124                                 {
125                                         var entry = archive.GetEntry("foo.txt");
126                                         Assert.IsNotNull(entry);
127
128                                         var stream = entry.Open();
129
130                                         try {
131                                                 stream = entry.Open();
132                                         } catch (global::System.IO.IOException ex) {
133                                                 return;
134                                         }
135
136                                         Assert.Fail();
137                                 }
138                         } finally {
139                                 File.Delete ("test.zip");
140                         }
141                 }
142
143
144                 [Test]
145                 public void ZipOpenCloseAndReopenEntry()
146                 {
147                         File.Copy("archive.zip", "test.zip", overwrite: true);
148                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
149                                 ZipArchiveMode.Update))
150                         {
151                                 var entry = archive.GetEntry("foo.txt");
152                                 Assert.IsNotNull(entry);
153
154                                 var stream = entry.Open();
155                                 stream.Dispose();
156                                 stream = entry.Open();
157                         }
158
159                         File.Delete ("test.zip");
160                 }
161
162                 [Test]
163                 public void ZipGetEntryDeleteReadMode()
164                 {
165                         File.Copy("archive.zip", "delete.zip", overwrite: true);
166                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
167                                 ZipArchiveMode.Update))
168                         {
169                                 var entry = archive.GetEntry("foo.txt");
170                                 Assert.IsNotNull(entry);
171
172                                 entry.Delete();
173                         }
174
175                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
176                                 ZipArchiveMode.Read))
177                         {
178                                 var entry = archive.GetEntry("foo.txt");
179                                 Assert.IsNull(entry);
180                         }
181
182                         File.Delete ("delete.zip");
183                 }
184
185                 [Test]
186                 public void ZipGetEntryDeleteUpdateMode()
187                 {
188                         File.Copy("archive.zip", "delete.zip", overwrite: true);
189                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
190                                 ZipArchiveMode.Update))
191                         {
192                                 var entry = archive.GetEntry("foo.txt");
193                                 Assert.IsNotNull(entry);
194
195                                 entry.Delete();
196                         }
197
198                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
199                                 ZipArchiveMode.Read))
200                         {
201                                 var entry = archive.GetEntry("foo.txt");
202                                 Assert.IsNull(entry);
203                         }
204
205                         File.Delete ("delete.zip");
206                 }
207
208                 [Test]
209                 public void ZipCreateArchive()
210                 {
211                         using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Create),
212                                 ZipArchiveMode.Create))
213                         {
214                                 var dir = archive.CreateEntry("foobar/");
215
216                                 var entry = archive.CreateEntry("foo.txt");
217                                 using (var stream = entry.Open())
218                                 {
219                                         using (var streamWriter = new StreamWriter(stream))
220                                                 streamWriter.Write("foo");
221                                 }
222                         }
223
224                         using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Open),
225                                 ZipArchiveMode.Read))
226                         {
227                                 Assert.IsNotNull(archive.GetEntry("foobar/"));
228
229                                 var entry = archive.GetEntry("foo.txt");
230                                 Assert.IsNotNull(entry);
231
232                                 var streamReader = new StreamReader(entry.Open());
233                                 var text = streamReader.ReadToEnd();
234
235                                 Assert.AreEqual("foo", text);
236                         }
237
238                         File.Delete ("create.zip");
239                 }
240
241                 [Test]
242                 public void ZipEnumerateEntriesModifiedTime()
243                 {
244                         File.Copy("archive.zip", "test.zip", overwrite: true);
245                         var date = DateTimeOffset.Now;
246                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
247                                 ZipArchiveMode.Update))
248                         {
249                                 var entry = archive.GetEntry("foo.txt");
250                                 entry.LastWriteTime = date;
251                         }
252
253                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
254                                 ZipArchiveMode.Read))
255                         {
256                                 var entry = archive.GetEntry("foo.txt");
257                                 Assert.AreEqual(entry.LastWriteTime.Year, date.Year);
258                                 Assert.AreEqual(entry.LastWriteTime.Month, date.Month);
259                                 Assert.AreEqual(entry.LastWriteTime.Day, date.Day);
260
261                         }
262
263                         File.Delete ("test.zip");
264                 }               
265
266                 [Test]
267                 public void ZipEnumerateArchiveDefaultLastWriteTime()
268                 {
269                         using (var archive = new ZipArchive(File.Open("test.nupkg", FileMode.Open),
270                                 ZipArchiveMode.Read))
271                         {
272                                 var entry = archive.GetEntry("_rels/.rels");
273                                 Assert.AreEqual(new DateTime(624511296000000000).Ticks, entry.LastWriteTime.Ticks);
274                                 Assert.IsNotNull(entry);
275                         }
276                 }
277
278                 [Test]
279                 public void ZipEnumerateEntriesReadMode()
280                 {
281                         File.Copy("archive.zip", "test.zip", overwrite: true);
282                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
283                                 ZipArchiveMode.Read))
284                         {
285                                 var entries = archive.Entries;
286                                 Assert.AreEqual(5, entries.Count);
287
288                                 Assert.AreEqual("bar.txt", entries[0].FullName);
289                                 Assert.AreEqual("foo.txt", entries[1].FullName);
290                                 Assert.AreEqual("foobar/", entries[2].FullName);
291                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
292                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
293                         }
294
295                         File.Delete ("test.zip");
296                 }
297
298                 [Test]
299                 public void ZipEnumerateEntriesUpdateMode()
300                 {
301                         File.Copy("archive.zip", "test.zip", overwrite: true);
302                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
303                                 ZipArchiveMode.Read))
304                         {
305                                 var entries = archive.Entries;
306                                 Assert.AreEqual(5, entries.Count);
307
308                                 Assert.AreEqual("bar.txt", entries[0].FullName);
309                                 Assert.AreEqual("foo.txt", entries[1].FullName);
310                                 Assert.AreEqual("foobar/", entries[2].FullName);
311                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
312                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
313                         }
314
315                         File.Delete ("test.zip");
316                 }
317
318                 [Test]
319                 public void ZipEnumerateEntriesCreateMode()
320                 {
321                         File.Copy("archive.zip", "test.zip", overwrite: true);
322                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
323                                 ZipArchiveMode.Create))
324                         {
325                                 try {
326                                         archive.Entries.ToList();
327                                 } catch(NotSupportedException ex) {
328                                         return;
329                                 }
330                                 
331                                 Assert.Fail();                          
332                         }
333
334                         File.Delete ("test.zip");
335                 }
336
337                 [Test]
338                 public void ZipUpdateEmptyArchive()
339                 {
340                         File.WriteAllText("empty.zip", string.Empty);
341                         using (var archive = new ZipArchive(File.Open("empty.zip", FileMode.Open),
342                                 ZipArchiveMode.Update))
343                         {
344                         }
345                         File.Delete ("empty.zip");
346                 }
347         }
348 }