GzipStreamTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <[email protected]>
  7. //
  8. // (C) 2004 Novell, Inc. <http://www.novell.com>
  9. //
  10. #if NET_2_0
  11. using NUnit.Framework;
  12. using System;
  13. using System.IO;
  14. using System.IO.Compression;
  15. namespace MonoTests.System.IO.Compression
  16. {
  17. [TestFixture]
  18. public class GZipStreamTest : Assertion
  19. {
  20. private static void CopyStream (Stream src, Stream dest)
  21. {
  22. byte[] array = new byte[1024];
  23. int bytes_read;
  24. bytes_read = src.Read (array, 0, 1024);
  25. while (bytes_read != 0) {
  26. dest.Write (array, 0, bytes_read);
  27. bytes_read = src.Read (array, 0, 1024);
  28. }
  29. }
  30. private static bool compare_buffers (byte[] first, byte[] second, int length)
  31. {
  32. if (first.Length < length || second.Length < length) {
  33. return false;
  34. }
  35. for (int i = 0; i < length; i++) {
  36. if (first[i] != second[i]) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void Constructor_Null ()
  45. {
  46. GZipStream ds = new GZipStream (null, CompressionMode.Compress);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentException))]
  50. public void Constructor_InvalidCompressionMode ()
  51. {
  52. GZipStream ds = new GZipStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
  53. }
  54. [Test]
  55. [Category("NotWorking")] // #72143
  56. public void CheckCompressDecompress () {
  57. byte [] data = new byte[100000];
  58. for (int i = 0; i < 100000; i++) {
  59. data[i] = (byte) i;
  60. }
  61. MemoryStream dataStream = new MemoryStream (data);
  62. MemoryStream backing = new MemoryStream ();
  63. GZipStream compressing = new GZipStream (backing, CompressionMode.Compress, true);
  64. CopyStream (dataStream, compressing);
  65. dataStream.Close();
  66. compressing.Close();
  67. backing.Seek (0, SeekOrigin.Begin);
  68. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  69. MemoryStream output = new MemoryStream ();
  70. CopyStream (decompressing, output);
  71. Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
  72. decompressing.Close();
  73. output.Close();
  74. }
  75. [Test]
  76. public void CheckDecompress () {
  77. 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 };
  78. MemoryStream backing = new MemoryStream (data);
  79. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  80. StreamReader reader = new StreamReader (decompressing);
  81. AssertEquals (reader.ReadLine (), "Hello");
  82. decompressing.Close();
  83. }
  84. [Test]
  85. [ExpectedException (typeof (ArgumentNullException))]
  86. public void CheckNullRead () {
  87. 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 };
  88. MemoryStream backing = new MemoryStream (data);
  89. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  90. decompressing.Read (null, 0, 20);
  91. }
  92. [Test]
  93. [ExpectedException (typeof (InvalidOperationException))]
  94. public void CheckCompressingRead () {
  95. byte [] dummy = new byte[20];
  96. MemoryStream backing = new MemoryStream ();
  97. GZipStream compressing = new GZipStream (backing, CompressionMode.Compress);
  98. compressing.Read (dummy, 0, 20);
  99. }
  100. [Test]
  101. [ExpectedException (typeof (ArgumentException))]
  102. public void CheckRangeRead () {
  103. 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 };
  104. byte [] dummy = new byte[20];
  105. MemoryStream backing = new MemoryStream (data);
  106. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  107. decompressing.Read (dummy, 10, 20);
  108. }
  109. [Test]
  110. [ExpectedException (typeof (InvalidDataException))]
  111. public void CheckInvalidDataRead () {
  112. 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 };
  113. byte [] dummy = new byte[20];
  114. MemoryStream backing = new MemoryStream (data);
  115. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  116. decompressing.Read (dummy, 0, 20);
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ObjectDisposedException))]
  120. public void CheckClosedRead () {
  121. 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 };
  122. byte [] dummy = new byte[20];
  123. MemoryStream backing = new MemoryStream (data);
  124. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  125. decompressing.Close ();
  126. decompressing.Read (dummy, 0, 20);
  127. }
  128. [Test]
  129. [ExpectedException (typeof (ObjectDisposedException))]
  130. [Category("NotWorking")] // #72143
  131. public void CheckClosedFlush () {
  132. MemoryStream backing = new MemoryStream ();
  133. GZipStream compressing = new GZipStream (backing, CompressionMode.Compress);
  134. compressing.Close ();
  135. compressing.Flush ();
  136. }
  137. [Test]
  138. [ExpectedException (typeof (NotSupportedException))]
  139. public void CheckSeek () {
  140. 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 };
  141. MemoryStream backing = new MemoryStream (data);
  142. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  143. decompressing.Seek (20, SeekOrigin.Current);
  144. }
  145. [Test]
  146. [ExpectedException (typeof (NotSupportedException))]
  147. public void CheckSetLength () {
  148. 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 };
  149. MemoryStream backing = new MemoryStream (data);
  150. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  151. decompressing.SetLength (20);
  152. }
  153. [Test]
  154. public void CheckGetCanSeekProp () {
  155. 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 };
  156. MemoryStream backing = new MemoryStream (data);
  157. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  158. AssertEquals (false, decompressing.CanSeek);
  159. }
  160. [Test]
  161. public void CheckGetCanReadProp () {
  162. 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 };
  163. MemoryStream backing = new MemoryStream (data);
  164. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  165. AssertEquals (true, decompressing.CanRead);
  166. }
  167. [Test]
  168. public void CheckGetCanWriteProp () {
  169. MemoryStream backing = new MemoryStream ();
  170. GZipStream compressing = new GZipStream (backing, CompressionMode.Decompress);
  171. AssertEquals (false, compressing.CanWrite);
  172. }
  173. [Test]
  174. [ExpectedException (typeof (NotSupportedException))]
  175. public void CheckSetLengthProp () {
  176. 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 };
  177. MemoryStream backing = new MemoryStream (data);
  178. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  179. decompressing.SetLength (20);
  180. }
  181. [Test]
  182. [ExpectedException (typeof (NotSupportedException))]
  183. public void CheckGetLengthProp () {
  184. 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 };
  185. MemoryStream backing = new MemoryStream (data);
  186. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  187. long length = decompressing.Length;
  188. }
  189. [Test]
  190. [ExpectedException (typeof (NotSupportedException))]
  191. public void CheckGetPositionProp () {
  192. 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 };
  193. MemoryStream backing = new MemoryStream (data);
  194. GZipStream decompressing = new GZipStream (backing, CompressionMode.Decompress);
  195. long position = decompressing.Position;
  196. }
  197. }
  198. }
  199. #endif