Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System / Test / System.IO.Compression / GzipStreamTest.cs
1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 //
3 // GZipStreamTest.cs - NUnit Test Cases for the System.IO.Compression.GZipStream class
4 //
5 // Authors:
6 //      Christopher James Lahey  <clahey@ximian.com>
7 //
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
9 // 
10
11 using NUnit.Framework;
12 using System;
13 using System.IO;
14 using System.IO.Compression;
15 using System.Text;
16
17 namespace MonoTests.System.IO.Compression
18 {
19         [TestFixture]
20         public class GZipStreamTest
21         {
22                 private static void CopyStream (Stream src, Stream dest)
23                 {
24                         byte[] array = new byte[1024];
25                         int bytes_read;
26                         bytes_read = src.Read (array, 0, 1024);
27                         while (bytes_read != 0) {
28                                 dest.Write (array, 0, bytes_read);
29                                 bytes_read = src.Read (array, 0, 1024);
30                         }
31                 }
32
33                 private static bool compare_buffers (byte[] first, byte[] second, int length)
34                 {
35                         if (first.Length < length || second.Length < length) {
36                                 return false;
37                         }
38                         for (int i = 0; i < length; i++) {
39                                 if (first[i] != second[i]) {
40                                         return false;
41                                 }
42                         }
43                         return true;
44                 }
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentNullException))]
48                 public void Constructor_Null ()
49                 {
50                         GZipStream ds = new GZipStream (null, CompressionMode.Compress);
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (ArgumentException))]
55                 public void Constructor_InvalidCompressionMode ()
56                 {
57                         GZipStream ds = new GZipStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
58                 }
59
60                 [Test]
61                 public void CheckCompressDecompress ()
62                 {
63                         byte [] data = new byte[100000];
64                         for (int i = 0; i < 100000; i++) {
65                                 data[i] = (byte) i;
66                         }
67                         MemoryStream dataStream = new MemoryStream (data);
68                         MemoryStream backing = new MemoryStream ();
69                         GZipStream compressing = new GZipStream (backing, CompressionMode.Compress, true);
70                         CopyStream (dataStream, compressing);
71                         dataStream.Close();
72                         compressing.Close();
73                         backing.Seek (0, SeekOrigin.Begin);
74                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
75                         MemoryStream output = new MemoryStream ();
76                         CopyStream (decompressing, output);
77                         Assert.IsTrue (compare_buffers (data, output.GetBuffer(), (int) output.Length));
78                         decompressing.Close();
79                         output.Close();
80                 }
81
82                 [Test]
83                 public void CheckDecompress ()
84                 {
85                         byte [] data = {0x1f, 0x8b, 0x08, 0x08, 0x70, 0xbb, 0x5d, 0x41, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00, 0x16, 0x35, 0x96, 0x31, 0x06, 0x00, 0x00, 0x00 };
86                         MemoryStream backing = new MemoryStream (data);
87                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
88                         StreamReader reader = new StreamReader (decompressing);
89                         Assert.AreEqual ("Hello", reader.ReadLine ());
90                         decompressing.Close();
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentNullException))]
95                 public void CheckNullRead ()
96                 {
97                         byte [] data = {0x1f, 0x8b, 0x08, 0x08, 0x70, 0xbb, 0x5d, 0x41, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00, 0x16, 0x35, 0x96, 0x31, 0x06, 0x00, 0x00, 0x00 };
98                         MemoryStream backing = new MemoryStream (data);
99                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
100                         decompressing.Read (null, 0, 20);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (InvalidOperationException))]
105                 public void CheckCompressingRead ()
106                 {
107                         byte [] dummy = new byte[20];
108                         MemoryStream backing = new MemoryStream ();
109                         GZipStream compressing = new GZipStream (backing, CompressionMode.Compress);
110                         compressing.Read (dummy, 0, 20);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))]
115                 public void CheckRangeRead ()
116                 {
117                         byte [] data = {0x1f, 0x8b, 0x08, 0x08, 0x70, 0xbb, 0x5d, 0x41, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00, 0x16, 0x35, 0x96, 0x31, 0x06, 0x00, 0x00, 0x00 };
118                         byte [] dummy = new byte[20];
119                         MemoryStream backing = new MemoryStream (data);
120                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
121                         decompressing.Read (dummy, 10, 20);
122                 }
123
124 #if !MOBILE
125                 [Test]
126                 [Category("NotWorking")]
127                 public void CheckInvalidDataRead ()
128                 {
129                         byte [] data = {0x1f, 0x8b, 0x08, 0x08, 0x70, 0xbb, 0x5d, 0x41, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00, 0x16, 0x35, 0x96, 0x31, 0x06, 0x00, 0x00, 0x00 };
130                         byte [] dummy = new byte[20];
131                         MemoryStream backing = new MemoryStream (data);
132                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
133                         try {
134                                 decompressing.Read (dummy, 0, 20);
135                                 Assert.Fail ();
136                         } catch (InvalidDataException) {
137                         }
138                 }
139 #endif
140
141                 [Test]
142                 public void CheckClosedRead ()
143                 {
144                         byte [] dummy = new byte[20];
145                         MemoryStream backing = new MemoryStream (compressed_data);
146                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
147                         decompressing.Close ();
148                         try {
149                                 decompressing.Read (dummy, 0, 20);
150                                 Assert.Fail ();
151                         } catch (ObjectDisposedException) {
152                         }
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ObjectDisposedException))]
157                 public void CheckClosedFlush ()
158                 {
159                         MemoryStream backing = new MemoryStream ();
160                         GZipStream compressing = new GZipStream (backing, CompressionMode.Compress);
161                         compressing.Close ();
162                         compressing.Flush ();
163                 }
164
165                 [Test]
166                 [ExpectedException (typeof (NotSupportedException))]
167                 public void CheckSeek ()
168                 {
169                         MemoryStream backing = new MemoryStream (compressed_data);
170                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
171                         decompressing.Seek (20, SeekOrigin.Current);
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (NotSupportedException))]
176                 public void CheckSetLength ()
177                 {
178                         MemoryStream backing = new MemoryStream (compressed_data);
179                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
180                         decompressing.SetLength (20);
181                 }
182
183                 [Test]
184                 public void CheckGetCanSeekProp ()
185                 {
186                         MemoryStream backing = new MemoryStream (compressed_data);
187                         GZipStream decompress = new GZipStream (backing, CompressionMode.Decompress);
188                         Assert.IsFalse (decompress.CanSeek, "#A1");
189                         Assert.IsTrue (backing.CanSeek, "#A2");
190                         decompress.Dispose ();
191                         Assert.IsFalse (decompress.CanSeek, "#A3");
192                         Assert.IsFalse (backing.CanSeek, "#A4");
193
194                         backing = new MemoryStream ();
195                         GZipStream compress = new GZipStream (backing, CompressionMode.Compress);
196                         Assert.IsFalse (compress.CanSeek, "#B1");
197                         Assert.IsTrue (backing.CanSeek, "#B2");
198                         compress.Dispose ();
199                         Assert.IsFalse (decompress.CanSeek, "#B3");
200                         Assert.IsFalse (backing.CanSeek, "#B4");
201                 }
202
203                 [Test]
204                 public void CheckGetCanReadProp ()
205                 {
206                         MemoryStream backing = new MemoryStream (compressed_data);
207                         GZipStream decompress = new GZipStream (backing, CompressionMode.Decompress);
208                         Assert.IsTrue (decompress.CanRead, "#A1");
209                         Assert.IsTrue (backing.CanRead, "#A2");
210                         decompress.Dispose ();
211                         Assert.IsFalse (decompress.CanRead, "#A3");
212                         Assert.IsFalse (backing.CanRead, "#A4");
213
214                         backing = new MemoryStream ();
215                         GZipStream compress = new GZipStream (backing, CompressionMode.Compress);
216                         Assert.IsFalse (compress.CanRead, "#B1");
217                         Assert.IsTrue (backing.CanRead, "#B2");
218                         compress.Dispose ();
219                         Assert.IsFalse (decompress.CanRead, "#B3");
220                         Assert.IsFalse (backing.CanRead, "#B4");
221                 }
222
223                 [Test]
224                 public void CheckGetCanWriteProp ()
225                 {
226                         MemoryStream backing = new MemoryStream (compressed_data);
227                         GZipStream decompress = new GZipStream (backing, CompressionMode.Decompress);
228                         Assert.IsFalse (decompress.CanWrite, "#A1");
229                         Assert.IsTrue (backing.CanWrite, "#A2");
230                         decompress.Dispose ();
231                         Assert.IsFalse (decompress.CanWrite, "#A3");
232                         Assert.IsFalse (backing.CanWrite, "#A4");
233
234                         backing = new MemoryStream ();
235                         GZipStream compress = new GZipStream (backing, CompressionMode.Compress);
236                         Assert.IsTrue (compress.CanWrite, "#B1");
237                         Assert.IsTrue (backing.CanWrite, "#B2");
238                         compress.Dispose ();
239                         Assert.IsFalse (decompress.CanWrite, "#B3");
240                         Assert.IsFalse (backing.CanWrite, "#B4");
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (NotSupportedException))]
245                 public void CheckSetLengthProp ()
246                 {
247                         MemoryStream backing = new MemoryStream (compressed_data);
248                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
249                         decompressing.SetLength (20);
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (NotSupportedException))]
254                 public void CheckGetLengthProp ()
255                 {
256                         MemoryStream backing = new MemoryStream (compressed_data);
257                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
258                         long length = decompressing.Length;
259                 }
260
261                 [Test]
262                 [ExpectedException (typeof (NotSupportedException))]
263                 public void CheckGetPositionProp ()
264                 {
265                         MemoryStream backing = new MemoryStream (compressed_data);
266                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
267                         long position = decompressing.Position;
268                 }
269
270                 [Test]
271                 public void DisposeTest ()
272                 {
273                         MemoryStream backing = new MemoryStream (compressed_data);
274                         GZipStream decompress = new GZipStream (backing, CompressionMode.Decompress);
275                         decompress.Dispose ();
276                         decompress.Dispose ();
277                 }
278
279                 [Test]
280                 public void DisposeOrderTest ()
281                 {
282                         var fs = new MemoryStream();
283                         GZipStream compressed = new GZipStream(fs, CompressionMode.Compress);
284                         byte[] buffer = new byte[1024];
285                         compressed.Write(buffer, 0, buffer.Length);
286                         compressed.Close();
287
288                         try {
289                                 fs.WriteByte(2);
290                                 Assert.Fail ();
291                         } catch (ObjectDisposedException) {
292                         }                       
293                 }
294
295                 static byte [] compressed_data = {
296                         0x1f, 0x8b, 0x08, 0x08, 0x70, 0xbb, 0x5d, 0x41, 0x00,
297                         0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xf3, 0x48, 0xcd,
298                         0xc9, 0xc9, 0xe7, 0x02, 0x00, 0x16, 0x35, 0x96, 0x31,
299                         0x06, 0x00, 0x00, 0x00};
300
301                 public MemoryStream GenerateStreamFromString(string s)
302                 {
303                         return new MemoryStream (Encoding.UTF8.GetBytes (s));
304                 }
305
306 #if NET_4_5
307                 [Test]
308                 public void CheckNet45Overloads () // Xambug #21982
309                 {
310                         MemoryStream dataStream = GenerateStreamFromString("Hello");
311                         MemoryStream backing = new MemoryStream ();
312                         GZipStream compressing = new GZipStream (backing, CompressionLevel.Fastest, true);
313                         CopyStream (dataStream, compressing);
314                         dataStream.Close();
315                         compressing.Close();
316
317                         backing.Seek (0, SeekOrigin.Begin);
318                         GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
319                         StreamReader reader = new StreamReader (decompressing);
320                         Assert.AreEqual ("Hello", reader.ReadLine ());
321                         decompressing.Close();
322                         backing.Close();
323                 }
324 #endif
325         }
326 }
327