Merge pull request #3056 from BrzVlad/fix-multiple-binprot
[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 ZipEnumerateEntriesReadMode()
268                 {
269                         File.Copy("archive.zip", "test.zip", overwrite: true);
270                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
271                                 ZipArchiveMode.Read))
272                         {
273                                 var entries = archive.Entries;
274                                 Assert.AreEqual(5, entries.Count);
275
276                                 Assert.AreEqual("bar.txt", entries[0].FullName);
277                                 Assert.AreEqual("foo.txt", entries[1].FullName);
278                                 Assert.AreEqual("foobar/", entries[2].FullName);
279                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
280                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
281                         }
282
283                         File.Delete ("test.zip");
284                 }
285
286                 [Test]
287                 public void ZipEnumerateEntriesUpdateMode()
288                 {
289                         File.Copy("archive.zip", "test.zip", overwrite: true);
290                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
291                                 ZipArchiveMode.Read))
292                         {
293                                 var entries = archive.Entries;
294                                 Assert.AreEqual(5, entries.Count);
295
296                                 Assert.AreEqual("bar.txt", entries[0].FullName);
297                                 Assert.AreEqual("foo.txt", entries[1].FullName);
298                                 Assert.AreEqual("foobar/", entries[2].FullName);
299                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
300                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
301                         }
302
303                         File.Delete ("test.zip");
304                 }
305
306                 [Test]
307                 public void ZipEnumerateEntriesCreateMode()
308                 {
309                         File.Copy("archive.zip", "test.zip", overwrite: true);
310                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
311                                 ZipArchiveMode.Create))
312                         {
313                                 try {
314                                         archive.Entries.ToList();
315                                 } catch(NotSupportedException ex) {
316                                         return;
317                                 }
318                                 
319                                 Assert.Fail();                          
320                         }
321
322                         File.Delete ("test.zip");
323                 }
324
325                 [Test]
326                 public void ZipUpdateEmptyArchive()
327                 {
328                         File.WriteAllText("empty.zip", string.Empty);
329                         using (var archive = new ZipArchive(File.Open("empty.zip", FileMode.Open),
330                                 ZipArchiveMode.Update))
331                         {
332                         }
333                         File.Delete ("empty.zip");
334                 }
335         }
336 }