1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 // DeflateStreamTest.cs - NUnit Test Cases for the System.IO.Compression.DeflateStream class
6 // Christopher James Lahey <clahey@ximian.com>
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
12 using NUnit.Framework;
15 using System.IO.Compression;
18 namespace MonoTests.System.IO.Compression
21 public class DeflateStreamTest
23 private static void CopyStream (Stream src, Stream dest)
25 byte[] array = new byte[1024];
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);
34 private static bool compare_buffers (byte[] first, byte[] second, int length)
36 if (first.Length < length || second.Length < length) {
39 for (int i = 0; i < length; i++) {
40 if (first[i] != second[i]) {
48 [ExpectedException (typeof (ArgumentNullException))]
49 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
50 public void Constructor_Null ()
52 DeflateStream ds = new DeflateStream (null, CompressionMode.Compress);
56 [ExpectedException (typeof (ArgumentException))]
57 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
58 public void Constructor_InvalidCompressionMode ()
60 DeflateStream ds = new DeflateStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
64 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
65 public void CheckCompressDecompress ()
67 byte [] data = new byte[100000];
68 for (int i = 0; i < 100000; i++) {
71 MemoryStream dataStream = new MemoryStream (data);
72 MemoryStream backing = new MemoryStream ();
73 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
74 CopyStream (dataStream, compressing);
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();
87 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
88 public void CheckDecompress ()
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();
97 // https://bugzilla.xamarin.com/show_bug.cgi?id=22346
99 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
100 public void CheckEmptyRead ()
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);
110 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
111 [ExpectedException (typeof (ArgumentNullException))]
112 public void CheckNullRead ()
114 MemoryStream backing = new MemoryStream (compressed_data);
115 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
116 decompressing.Read (null, 0, 20);
120 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
121 [ExpectedException (typeof (InvalidOperationException))]
122 public void CheckCompressingRead ()
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);
131 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
132 [ExpectedException (typeof (ArgumentException))]
133 public void CheckRangeRead ()
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);
143 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
144 [Category("NotWorking")]
145 [ExpectedException (typeof (InvalidDataException))]
146 public void CheckInvalidDataRead ()
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);
157 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
158 [ExpectedException (typeof (ObjectDisposedException))]
159 public void CheckClosedRead ()
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);
169 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
170 [ExpectedException (typeof (ObjectDisposedException))]
171 public void CheckClosedFlush ()
173 MemoryStream backing = new MemoryStream ();
174 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
175 compressing.Close ();
176 compressing.Flush ();
180 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
181 [ExpectedException (typeof (NotSupportedException))]
182 public void CheckSeek ()
184 MemoryStream backing = new MemoryStream (compressed_data);
185 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
186 decompressing.Seek (20, SeekOrigin.Current);
190 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
191 [ExpectedException (typeof (NotSupportedException))]
192 public void CheckSetLength ()
194 MemoryStream backing = new MemoryStream (compressed_data);
195 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
196 decompressing.SetLength (20);
200 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
201 public void CheckGetCanSeekProp ()
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");
211 backing = new MemoryStream ();
212 DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
213 Assert.IsFalse (compress.CanSeek, "#B1");
214 Assert.IsTrue (backing.CanSeek, "#B2");
216 Assert.IsFalse (decompress.CanSeek, "#B3");
217 Assert.IsFalse (backing.CanSeek, "#B4");
221 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
222 public void CheckGetCanReadProp ()
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");
232 backing = new MemoryStream ();
233 DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
234 Assert.IsFalse (compress.CanRead, "#B1");
235 Assert.IsTrue (backing.CanRead, "#B2");
237 Assert.IsFalse (decompress.CanRead, "#B3");
238 Assert.IsFalse (backing.CanRead, "#B4");
242 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
243 public void CheckGetCanWriteProp ()
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");
253 backing = new MemoryStream ();
254 DeflateStream compress = new DeflateStream (backing, CompressionMode.Compress);
255 Assert.IsTrue (compress.CanWrite, "#B1");
256 Assert.IsTrue (backing.CanWrite, "#B2");
258 Assert.IsFalse (decompress.CanWrite, "#B3");
259 Assert.IsFalse (backing.CanWrite, "#B4");
263 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
264 [ExpectedException (typeof (NotSupportedException))]
265 public void CheckSetLengthProp ()
267 MemoryStream backing = new MemoryStream (compressed_data);
268 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
269 decompressing.SetLength (20);
273 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
274 [ExpectedException (typeof (NotSupportedException))]
275 public void CheckGetLengthProp ()
277 MemoryStream backing = new MemoryStream (compressed_data);
278 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
279 long length = decompressing.Length;
283 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
284 [ExpectedException (typeof (NotSupportedException))]
285 public void CheckGetPositionProp ()
287 MemoryStream backing = new MemoryStream (compressed_data);
288 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
289 long position = decompressing.Position;
293 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
294 public void DisposeTest ()
296 MemoryStream backing = new MemoryStream (compressed_data);
297 DeflateStream decompress = new DeflateStream (backing, CompressionMode.Decompress);
298 decompress.Dispose ();
299 decompress.Dispose ();
302 static byte [] compressed_data = { 0xf3, 0x48, 0xcd, 0xc9, 0xc9,
307 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
308 public void JunkAtTheEnd ()
310 // Write a deflated stream, then some additional data...
311 using (MemoryStream ms = new MemoryStream())
313 // The compressed stream
314 using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
323 // Reading: this should not hang
324 using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress))
326 byte[] buffer = new byte[512];
327 int len = stream.Read(buffer, 0, buffer.Length);
328 Console.WriteLine(len == 1);
333 class Bug19313Stream : MemoryStream
335 public Bug19313Stream (byte [] buffer)
340 public override int Read (byte [] buffer, int offset, int count)
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.");
349 return base.Read (buffer, offset, count);
354 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
355 public void Bug19313 ()
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);
363 public MemoryStream GenerateStreamFromString(string s)
365 return new MemoryStream (Encoding.UTF8.GetBytes (s));
369 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
370 public void CheckNet45Overloads () // Xambug #21982
372 MemoryStream dataStream = GenerateStreamFromString("Hello");
373 MemoryStream backing = new MemoryStream ();
374 DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
375 CopyStream (dataStream, compressing);
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();
388 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
389 [ExpectedException (typeof (ArgumentException))]
390 public void CheckBufferOverrun ()
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 ();
401 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
402 public void Bug28777_EmptyFlush ()
404 MemoryStream backing = new MemoryStream ();
405 DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true);
406 compressing.Flush ();
407 compressing.Close ();
412 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
413 public void Bug28777_DoubleFlush ()
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 ();
426 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
427 public void Bug34916_Inflate ()
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=";
431 byte[] byteArray = Convert.FromBase64String(base64String);
432 string unZipped = null;
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 ();
441 Assert.AreEqual(1877, unZipped.Length);
445 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
446 public void Bug44994_Inflate()
448 var base64String = @"7cWxCQAgDACwpeBjgqsgXiHU0fd9QzBLErX1EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADepcxcuU/atm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm3btm37zy8=";
450 byte[] byteArray = Convert.FromBase64String(base64String);
451 string unZipped = null;
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))
458 unZipped = unZippedStream.ReadToEnd();
461 Assert.AreEqual(81942, unZipped.Length);
465 [Category ("StaticLinkedAotNotWorking")] // Native MPH loading issues
466 [Category ("MobileNotWorking")]
467 public void Bug44994_InflateByteByByte()
470 using (var fileStream = File.OpenRead(Path.Combine("Test", "compressed.bin")))
472 using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Decompress, false))
474 while (deflateStream.ReadByte() != -1)
481 Assert.AreEqual(125387, byteCount);