DeflateStreamTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <[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 DeflateStreamTest : 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. DeflateStream ds = new DeflateStream (null, CompressionMode.Compress);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentException))]
  50. public void Constructor_InvalidCompressionMode ()
  51. {
  52. DeflateStream ds = new DeflateStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
  53. }
  54. [Test]
  55. public void CheckCompressDecompress () {
  56. byte [] data = new byte[100000];
  57. for (int i = 0; i < 100000; i++) {
  58. data[i] = (byte) i;
  59. }
  60. MemoryStream dataStream = new MemoryStream (data);
  61. MemoryStream backing = new MemoryStream ();
  62. DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
  63. CopyStream (dataStream, compressing);
  64. dataStream.Close();
  65. compressing.Close();
  66. backing.Seek (0, SeekOrigin.Begin);
  67. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  68. MemoryStream output = new MemoryStream ();
  69. CopyStream (decompressing, output);
  70. Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
  71. decompressing.Close();
  72. output.Close();
  73. }
  74. [Test]
  75. public void CheckDecompress () {
  76. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  77. MemoryStream backing = new MemoryStream (data);
  78. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  79. StreamReader reader = new StreamReader (decompressing);
  80. AssertEquals (reader.ReadLine (), "Hello");
  81. decompressing.Close();
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentNullException))]
  85. public void CheckNullRead () {
  86. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  87. MemoryStream backing = new MemoryStream (data);
  88. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  89. decompressing.Read (null, 0, 20);
  90. }
  91. [Test]
  92. [ExpectedException (typeof (InvalidOperationException))]
  93. public void CheckCompressingRead () {
  94. byte [] dummy = new byte[20];
  95. MemoryStream backing = new MemoryStream ();
  96. DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
  97. compressing.Read (dummy, 0, 20);
  98. }
  99. [Test]
  100. [ExpectedException (typeof (ArgumentException))]
  101. public void CheckRangeRead () {
  102. byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  103. byte [] dummy = new byte[20];
  104. MemoryStream backing = new MemoryStream (data);
  105. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  106. decompressing.Read (dummy, 10, 20);
  107. }
  108. [Test]
  109. [ExpectedException (typeof (InvalidDataException))]
  110. public void CheckInvalidDataRead () {
  111. byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  112. byte [] dummy = new byte[20];
  113. MemoryStream backing = new MemoryStream (data);
  114. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  115. decompressing.Read (dummy, 0, 20);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ObjectDisposedException))]
  119. public void CheckClosedRead () {
  120. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  121. byte [] dummy = new byte[20];
  122. MemoryStream backing = new MemoryStream (data);
  123. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  124. decompressing.Close ();
  125. decompressing.Read (dummy, 0, 20);
  126. }
  127. [Test]
  128. [ExpectedException (typeof (ObjectDisposedException))]
  129. [Category("NotWorking")] // #72143
  130. public void CheckClosedFlush () {
  131. MemoryStream backing = new MemoryStream ();
  132. DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
  133. compressing.Close ();
  134. compressing.Flush ();
  135. }
  136. [Test]
  137. [ExpectedException (typeof (NotSupportedException))]
  138. public void CheckSeek () {
  139. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  140. MemoryStream backing = new MemoryStream (data);
  141. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  142. decompressing.Seek (20, SeekOrigin.Current);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (NotSupportedException))]
  146. public void CheckSetLength () {
  147. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  148. MemoryStream backing = new MemoryStream (data);
  149. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  150. decompressing.SetLength (20);
  151. }
  152. [Test]
  153. public void CheckGetCanSeekProp () {
  154. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  155. MemoryStream backing = new MemoryStream (data);
  156. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  157. AssertEquals (false, decompressing.CanSeek);
  158. }
  159. [Test]
  160. public void CheckGetCanReadProp () {
  161. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  162. MemoryStream backing = new MemoryStream (data);
  163. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  164. AssertEquals (true, decompressing.CanRead);
  165. }
  166. [Test]
  167. public void CheckGetCanWriteProp () {
  168. MemoryStream backing = new MemoryStream ();
  169. DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
  170. AssertEquals (false, compressing.CanWrite);
  171. }
  172. [Test]
  173. [ExpectedException (typeof (NotSupportedException))]
  174. public void CheckSetLengthProp () {
  175. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  176. MemoryStream backing = new MemoryStream (data);
  177. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  178. decompressing.SetLength (20);
  179. }
  180. [Test]
  181. [ExpectedException (typeof (NotSupportedException))]
  182. public void CheckGetLengthProp () {
  183. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  184. MemoryStream backing = new MemoryStream (data);
  185. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  186. long length = decompressing.Length;
  187. }
  188. [Test]
  189. [ExpectedException (typeof (NotSupportedException))]
  190. public void CheckGetPositionProp () {
  191. byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
  192. MemoryStream backing = new MemoryStream (data);
  193. DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
  194. long position = decompressing.Position;
  195. }
  196. }
  197. }
  198. #endif