DeflateStreamTest.cs 6.9 KB

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