GzipStreamTest.cs 8.4 KB

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