[System.Xml] Use profile-specific file names for generated files.
[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
63                 [Test]
64                 public void ZipGetEntryCreateMode()
65                 {
66                         File.Copy("archive.zip", "test.zip", overwrite: true);
67                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
68                                 ZipArchiveMode.Create))
69                         {
70                                 try {
71                                         archive.GetEntry("foo");
72                                 } catch(NotSupportedException ex) {
73                                         return;
74                                 }
75
76                                 Assert.Fail();
77                         }
78                 }
79
80                 [Test]
81                 public void ZipGetEntryUpdateMode()
82                 {
83                         File.Copy("archive.zip", "test.zip", overwrite: true);
84                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
85                                 ZipArchiveMode.Read))
86                         {
87                                 var entry = archive.GetEntry("foo.txt");
88                                 Assert.IsNotNull(entry);
89
90                                 var nullEntry = archive.GetEntry("nonexisting");
91                                 Assert.IsNull(nullEntry);
92                         }
93                 }
94
95                 [Test]
96                 public void ZipGetEntryOpen()
97                 {
98                         File.Copy("archive.zip", "test.zip", overwrite: true);
99                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
100                                 ZipArchiveMode.Read))
101                         {
102                                 var entry = archive.GetEntry("foo.txt");
103                                 Assert.IsNotNull(entry);
104
105                                 var foo = entry.Open();
106                                 Assert.AreEqual("F1D2D2F924E986AC86FDF7B36C94BCDF32BEEC15",
107                                         GetSHA1HashFromFile(foo));
108                         }
109                 }
110
111                 [Test]
112                 public void ZipGetEntryDeleteReadMode()
113                 {
114                         File.Copy("archive.zip", "delete.zip", overwrite: true);
115                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
116                                 ZipArchiveMode.Update))
117                         {
118                                 var entry = archive.GetEntry("foo.txt");
119                                 Assert.IsNotNull(entry);
120
121                                 entry.Delete();
122                         }
123
124                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
125                                 ZipArchiveMode.Read))
126                         {
127                                 var entry = archive.GetEntry("foo.txt");
128                                 Assert.IsNull(entry);
129                         }
130                 }
131
132                 [Test]
133                 public void ZipGetEntryDeleteUpdateMode()
134                 {
135                         File.Copy("archive.zip", "delete.zip", overwrite: true);
136                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
137                                 ZipArchiveMode.Update))
138                         {
139                                 var entry = archive.GetEntry("foo.txt");
140                                 Assert.IsNotNull(entry);
141
142                                 entry.Delete();
143                         }
144
145                         using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
146                                 ZipArchiveMode.Read))
147                         {
148                                 var entry = archive.GetEntry("foo.txt");
149                                 Assert.IsNull(entry);
150                         }
151                 }
152
153                 [Test]
154                 public void ZipCreateArchive()
155                 {
156                         using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Create),
157                                 ZipArchiveMode.Create))
158                         {
159                                 var entry = archive.CreateEntry("foo.txt");
160                                 using (var stream = entry.Open())
161                                 {
162                                 }
163                         }
164
165                         using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Open),
166                                 ZipArchiveMode.Read))
167                         {
168                                 var entry = archive.GetEntry("foo.txt");
169                                 Assert.IsNotNull(entry);
170                         }
171                 }
172
173                 [Test]
174                 public void ZipEnumerateEntriesReadMode()
175                 {
176                         File.Copy("archive.zip", "test.zip", overwrite: true);
177                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
178                                 ZipArchiveMode.Read))
179                         {
180                                 var entries = archive.Entries;
181                                 Assert.AreEqual(5, entries.Count);
182
183                                 Assert.AreEqual("bar.txt", entries[0].FullName);
184                                 Assert.AreEqual("foo.txt", entries[1].FullName);
185                                 Assert.AreEqual("foobar/", entries[2].FullName);
186                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
187                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
188                         }
189                 }
190
191                 [Test]
192                 public void ZipEnumerateEntriesUpdateMode()
193                 {
194                         File.Copy("archive.zip", "test.zip", overwrite: true);
195                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
196                                 ZipArchiveMode.Read))
197                         {
198                                 var entries = archive.Entries;
199                                 Assert.AreEqual(5, entries.Count);
200
201                                 Assert.AreEqual("bar.txt", entries[0].FullName);
202                                 Assert.AreEqual("foo.txt", entries[1].FullName);
203                                 Assert.AreEqual("foobar/", entries[2].FullName);
204                                 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
205                                 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
206                         }
207                 }
208
209                 [Test]
210                 public void ZipEnumerateEntriesCreateMode()
211                 {
212                         File.Copy("archive.zip", "test.zip", overwrite: true);
213                         using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
214                                 ZipArchiveMode.Create))
215                         {
216                                 try {
217                                         archive.Entries.ToList();
218                                 } catch(NotSupportedException ex) {
219                                         return;
220                                 }
221                                 
222                                 Assert.Fail();                          
223                         }
224                 }
225         }
226 }