3681b5cc25c1af680e836b7236a9939754e78871
[mono.git] / mcs / class / System / Test / System.IO.Compression / DeflateStreamTest.cs
1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 //
3 // DeflateStreamTest.cs - NUnit Test Cases for the System.IO.Compression.DeflateStream class
4 //
5 // Authors:
6 //      Christopher James Lahey  <clahey@ximian.com>
7 //
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
9 // 
10
11
12 using NUnit.Framework;
13 using System;
14 using System.IO;
15 using System.IO.Compression;
16 using System.Text;
17
18 namespace MonoTests.System.IO.Compression
19 {
20         [TestFixture]
21         public class DeflateStreamTest
22         {
23                 private static void CopyStream (Stream src, Stream dest)
24                 {
25                         byte[] array = new byte[1024];
26                         int bytes_read;
27                         bytes_read = src.Read (array, 0, 1024);
28                         while (bytes_read != 0) {
29                                 dest.Write (array, 0, bytes_read);
30                                 bytes_read = src.Read (array, 0, 1024);
31                         }
32                 }
33
34                 private static bool compare_buffers (byte[] first, byte[] second, int length)
35                 {
36                         if (first.Length < length || second.Length < length) {
37                                 return false;
38                         }
39                         for (int i = 0; i < length; i++) {
40                                 if (first[i] != second[i]) {
41                                         return false;
42                                 }
43                         }
44                         return true;
45                 }
46
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
50                 public void Constructor_Null ()
51                 {
52                         DeflateStream ds = new DeflateStream (null, CompressionMode.Compress);
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (ArgumentException))]
57                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
58                 public void Constructor_InvalidCompressionMode ()
59                 {
60                         DeflateStream ds = new DeflateStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
61                 }
62
63                 [Test]
64                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
65                 public void CheckCompressDecompress ()
66                 {
67                         byte [] data = new byte[100000];
68                         for (int i = 0; i < 100000; i++) {
69                                 data[i] = (byte) i;
70                         }
71                         MemoryStream dataStream = new MemoryStream (data);
72                         MemoryStream backing = new MemoryStream ();
73                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
74                         CopyStream (dataStream, compressing);
75                         dataStream.Close();
76                         compressing.Close();
77                         backing.Seek (0, SeekOrigin.Begin);
78                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
79                         MemoryStream output = new MemoryStream ();
80                         CopyStream (decompressing, output);
81                         Assert.IsTrue (compare_buffers (data, output.GetBuffer(), (int) output.Length));
82                         decompressing.Close();
83                         output.Close();
84                 }
85
86                 [Test]
87                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
88                 public void CheckDecompress ()
89                 {
90                         MemoryStream backing = new MemoryStream (compressed_data);
91                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
92                         StreamReader reader = new StreamReader (decompressing);
93                         Assert.AreEqual ("Hello", reader.ReadLine ());
94                         decompressing.Close();
95                 }
96
97                 // https://bugzilla.xamarin.com/show_bug.cgi?id=22346
98                 [Test]
99                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
100                 public void CheckEmptyRead ()
101                 {
102                         byte [] dummy = new byte[1];
103                         byte [] data = new byte[0];
104                         MemoryStream backing = new MemoryStream (data);
105                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
106                         compressing.Read (dummy, 0, 1);
107                 }
108
109                 [Test]
110                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
111                 [ExpectedException (typeof (ArgumentNullException))]
112                 public void CheckNullRead ()
113                 {
114                         MemoryStream backing = new MemoryStream (compressed_data);
115                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
116                         decompressing.Read (null, 0, 20);
117                 }
118
119                 [Test]
120                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
121                 [ExpectedException (typeof (InvalidOperationException))]
122                 public void CheckCompressingRead ()
123                 {
124                         byte [] dummy = new byte[20];
125                         MemoryStream backing = new MemoryStream ();
126                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
127                         compressing.Read (dummy, 0, 20);
128                 }
129
130                 [Test]
131                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
132                 [ExpectedException (typeof (ArgumentException))]
133                 public void CheckRangeRead ()
134                 {
135                         byte [] dummy = new byte[20];
136                         MemoryStream backing = new MemoryStream (compressed_data);
137                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
138                         decompressing.Read (dummy, 10, 20);
139                 }
140
141 #if !MOBILE
142                 [Test]
143                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
144                 [Category("NotWorking")]
145                 [ExpectedException (typeof (InvalidDataException))]
146                 public void CheckInvalidDataRead ()
147                 {
148                         byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
149                         byte [] dummy = new byte[20];
150                         MemoryStream backing = new MemoryStream (data);
151                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
152                         decompressing.Read (dummy, 0, 20);
153                 }
154 #endif
155
156                 [Test]
157                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
158                 [ExpectedException (typeof (ObjectDisposedException))]
159                 public void CheckClosedRead ()
160                 {
161                         byte [] dummy = new byte[20];
162                         MemoryStream backing = new MemoryStream (compressed_data);
163                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
164                         decompressing.Close ();
165                         decompressing.Read (dummy, 0, 20);
166                 }
167
168                 [Test]
169                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
170                 [ExpectedException (typeof (ObjectDisposedException))]
171                 public void CheckClosedFlush ()
172                 {
173                         MemoryStream backing = new MemoryStream ();
174                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
175                         compressing.Close ();
176                         compressing.Flush ();
177                 }
178
179                 [Test]
180                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
181                 [ExpectedException (typeof (NotSupportedException))]
182                 public void CheckSeek ()
183                 {
184                         MemoryStream backing = new MemoryStream (compressed_data);
185                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
186                         decompressing.Seek (20, SeekOrigin.Current);
187                 }
188
189                 [Test]
190                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
191                 [ExpectedException (typeof (NotSupportedException))]
192                 public void CheckSetLength ()
193                 {
194                         MemoryStream backing = new MemoryStream (compressed_data);
195                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
196                         decompressing.SetLength (20);
197                 }
198
199                 [Test]
200                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
201                 public void CheckGetCanSeekProp ()
202                 {
203                         MemoryStream backing = new MemoryStream (compressed_data);
204                         DeflateStream decompress = new DeflateStream (backing, CompressionMode.Decompress);
205                         Assert.IsFalse (decompress.CanSeek, "#A1");
206                         Assert.IsTrue (backing.CanSeek, "#A2");
207                         decompress.Dispose ();
208                         Assert.IsFalse (decompress.CanSeek, "#A3");
209                         Assert.IsFalse (backing.CanSeek, "#A4");
210
211                         backing = new MemoryStream ();
212                         DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
213                         Assert.IsFalse (compress.CanSeek, "#B1");
214                         Assert.IsTrue (backing.CanSeek, "#B2");
215                         compress.Dispose ();
216                         Assert.IsFalse (decompress.CanSeek, "#B3");
217                         Assert.IsFalse (backing.CanSeek, "#B4");
218                 }
219
220                 [Test]
221                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
222                 public void CheckGetCanReadProp ()
223                 {
224                         MemoryStream backing = new MemoryStream (compressed_data);
225                         DeflateStream decompress = new DeflateStream (backing, CompressionMode.Decompress);
226                         Assert.IsTrue (decompress.CanRead, "#A1");
227                         Assert.IsTrue (backing.CanRead, "#A2");
228                         decompress.Dispose ();
229                         Assert.IsFalse (decompress.CanRead, "#A3");
230                         Assert.IsFalse (backing.CanRead, "#A4");
231
232                         backing = new MemoryStream ();
233                         DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
234                         Assert.IsFalse (compress.CanRead, "#B1");
235                         Assert.IsTrue (backing.CanRead, "#B2");
236                         compress.Dispose ();
237                         Assert.IsFalse (decompress.CanRead, "#B3");
238                         Assert.IsFalse (backing.CanRead, "#B4");
239                 }
240
241                 [Test]
242                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
243                 public void CheckGetCanWriteProp ()
244                 {
245                         MemoryStream backing = new MemoryStream ();
246                         DeflateStream decompress = new DeflateStream (backing, CompressionMode.Decompress);
247                         Assert.IsFalse (decompress.CanWrite, "#A1");
248                         Assert.IsTrue (backing.CanWrite, "#A2");
249                         decompress.Dispose ();
250                         Assert.IsFalse (decompress.CanWrite, "#A3");
251                         Assert.IsFalse (backing.CanWrite, "#A4");
252
253                         backing = new MemoryStream ();
254                         DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
255                         Assert.IsTrue (compress.CanWrite, "#B1");
256                         Assert.IsTrue (backing.CanWrite, "#B2");
257                         compress.Dispose ();
258                         Assert.IsFalse (decompress.CanWrite, "#B3");
259                         Assert.IsFalse (backing.CanWrite, "#B4");
260                 }
261
262                 [Test]
263                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
264                 [ExpectedException (typeof (NotSupportedException))]
265                 public void CheckSetLengthProp ()
266                 {
267                         MemoryStream backing = new MemoryStream (compressed_data);
268                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
269                         decompressing.SetLength (20);
270                 }
271
272                 [Test]
273                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
274                 [ExpectedException (typeof (NotSupportedException))]
275                 public void CheckGetLengthProp ()
276                 {
277                         MemoryStream backing = new MemoryStream (compressed_data);
278                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
279                         long length = decompressing.Length;
280                 }
281
282                 [Test]
283                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
284                 [ExpectedException (typeof (NotSupportedException))]
285                 public void CheckGetPositionProp ()
286                 {
287                         MemoryStream backing = new MemoryStream (compressed_data);
288                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
289                         long position = decompressing.Position;
290                 }
291
292                 [Test]
293                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
294                 public void DisposeTest ()
295                 {
296                         MemoryStream backing = new MemoryStream (compressed_data);
297                         DeflateStream decompress = new DeflateStream (backing, CompressionMode.Decompress);
298                         decompress.Dispose ();
299                         decompress.Dispose ();
300                 }
301
302                 static byte [] compressed_data = { 0xf3, 0x48, 0xcd, 0xc9, 0xc9,
303                         0xe7, 0x02, 0x00 };
304
305
306                 [Test]
307                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
308                 public void JunkAtTheEnd ()
309                 {
310                         // Write a deflated stream, then some additional data...
311                         using (MemoryStream ms = new MemoryStream())
312                         {
313                                 // The compressed stream
314                                 using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
315                                 {
316                                         stream.WriteByte(1);
317                                         stream.Flush();
318                                 }
319                                 // Junk
320                                 ms.WriteByte(2);
321
322                                 ms.Position = 0;
323                                 // Reading: this should not hang
324                                 using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress))
325                                 {
326                                         byte[] buffer  = new byte[512];
327                                         int len = stream.Read(buffer, 0, buffer.Length);
328                                         Console.WriteLine(len == 1);
329                                 }
330                         }
331                 }
332                 
333                 class Bug19313Stream : MemoryStream
334                 {
335                         public Bug19313Stream (byte [] buffer)
336                                 : base (buffer)
337                         {
338                         }
339
340                         public override int Read (byte [] buffer, int offset, int count)
341                         {
342                                 // Thread was blocking when DeflateStream uses a NetworkStream.
343                                 // Because the NetworkStream.Read calls Socket.Receive that
344                                 // blocks the thread waiting for at least a byte to return.
345                                 // This assert guarantees that Read is called only when there 
346                                 // is something to be read.
347                                 Assert.IsTrue (Position < Length, "Trying to read empty stream.");
348
349                                 return base.Read (buffer, offset, count);
350                         }
351                 }
352
353                 [Test]
354                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
355                 public void Bug19313 ()
356                 {
357                         byte [] buffer  = new byte [512];
358                         using (var backing = new Bug19313Stream (compressed_data))
359                         using (var decompressing = new DeflateStream (backing, CompressionMode.Decompress))
360                                 decompressing.Read (buffer, 0, buffer.Length);
361                 }
362
363                 public MemoryStream GenerateStreamFromString(string s)
364                 {
365                         return new MemoryStream (Encoding.UTF8.GetBytes (s));
366                 }
367
368                 [Test]
369                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
370                 public void CheckNet45Overloads () // Xambug #21982
371                 {
372                         MemoryStream dataStream = GenerateStreamFromString("Hello");
373                         MemoryStream backing = new MemoryStream ();
374                         DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
375                         CopyStream (dataStream, compressing);
376                         dataStream.Close();
377                         compressing.Close();
378
379                         backing.Seek (0, SeekOrigin.Begin);
380                         DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
381                         StreamReader reader = new StreamReader (decompressing);
382                         Assert.AreEqual ("Hello", reader.ReadLine ());
383                         decompressing.Close();
384                         backing.Close();
385                 }
386
387                 [Test]
388                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
389                 [ExpectedException (typeof (ArgumentException))]
390                 public void CheckBufferOverrun ()
391                 {
392                         byte[] data = new byte [20];
393                         MemoryStream backing = new MemoryStream ();
394                         DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
395                         compressing.Write (data, 0, data.Length + 1);
396                         compressing.Close ();
397                         backing.Close ();
398                 }
399
400                 [Test]
401                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
402                 public void Bug28777_EmptyFlush ()
403                 {
404                         MemoryStream backing = new MemoryStream ();
405                         DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
406                         compressing.Flush ();
407                         compressing.Close ();
408                         backing.Close ();
409                 }
410                 
411                 [Test]
412                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
413                 public void Bug28777_DoubleFlush ()
414                 {
415                         byte[] buffer = new byte [4096];
416                         MemoryStream backing = new MemoryStream ();
417                         DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
418                         compressing.Write (buffer, 0, buffer.Length);
419                         compressing.Flush ();
420                         compressing.Flush ();
421                         compressing.Close ();
422                         backing.Close ();
423                 }
424
425                 [Test]
426                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
427                 public void Bug34916_Inflate ()
428                 {
429                         var base64String = @"H4sIAAAAAAAAA6yVu27bQBBF/4VtZGHeD3ZJmhTp5C5IIUiEIcCWDEUugiD/nmEQwYRNURFAsuFwd2exZ++d+farud89davT+um5aRsC1DuEO+R7lJayRV9m5gegFqBZNB83m5fjevOzadGWUPHjaXd62XYVEy3Z04wiMTKIX0dfV0G/6FO3Pu72D/+iL916W9GbOV/X58SaS6zEKKyoGUA1eNg/nLfF2jUEBBNMtT4Wzeq567Z9HkZkE1Osf93msN/+WO32m+7zsavsh30/BUU8fy+uUCC+QIHpPQW1RAXkEGWUmSnUy2iUYSMYOGpARYViiIHcqY5kExS8rg2vY8gLGEjeYsClBVE4ORQHz3kxsEF4iS01xzBIZkgYQcYQQ7C54LQaIrxWn5+4ioT1BiRQN8Fh6MrOPjOS9Eh3M8YRJJQMZioJkUODFA8RNJ9AYuYBNyGJW5D0oi3/EpZ3dWYk5X5PN81RJGJgDATMQ5X02nFS1imVlMGvu0XwBg5/K1hY1U8tecxcNDy1/FAnG+OAQSi9PliHRaNUiuoxQYFB6T8oyAUKEu9LJ6oipbr1spyZArhWX6qbi7EOUrs7SCAoDNVgzKagMlUz+q6DQ4N8/yM=";
430
431                         byte[] byteArray = Convert.FromBase64String(base64String);
432                         string unZipped = null;
433
434                         using (var zippedMemoryStream = new MemoryStream (byteArray))
435                         using (var gZipStream = new GZipStream (zippedMemoryStream, CompressionMode.Decompress))
436                         using (var unzippedMemStream = new MemoryStream())
437                         using (var unZippedStream = new StreamReader (gZipStream, Encoding.UTF8)) {
438                                 unZipped = unZippedStream.ReadToEnd ();
439                         }
440
441                         Assert.AreEqual(1877, unZipped.Length);
442                 }
443
444                 [Test]
445                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
446                 public void Bug44994_Inflate()
447                 {
448                         var base64String = @"7cWxCQAgDACwpeBjgqsgXiHU0fd9QzBLErX1EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADepcxcuU/atm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm37zy8=";
449
450                         byte[] byteArray = Convert.FromBase64String(base64String);
451                         string unZipped = null;
452
453                         using (var zippedMemoryStream = new MemoryStream(byteArray))
454                         using (var gZipStream = new DeflateStream(zippedMemoryStream, CompressionMode.Decompress))
455                         using (var unzippedMemStream = new MemoryStream())
456                         using (var unZippedStream = new StreamReader(gZipStream, Encoding.UTF8))
457                         {
458                                 unZipped = unZippedStream.ReadToEnd();
459                         }
460
461                         Assert.AreEqual(81942, unZipped.Length);
462                 }
463
464                 [Test]
465                 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
466                 [Category ("MobileNotWorking")]
467                 public void Bug44994_InflateByteByByte()
468                 {
469                         int byteCount = 0;
470                         using (var fileStream = File.OpenRead(Path.Combine("Test", "compressed.bin")))
471                         {
472                                 using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Decompress, false))
473                                 {
474                                         while (deflateStream.ReadByte() != -1)
475                                         {
476                                                 byteCount++;
477                                         }
478                                 }
479                         }
480
481                         Assert.AreEqual(125387, byteCount);
482                 }
483         }
484 }
485
486