1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 // GzipStreamTest.cs - NUnit Test Cases for the System.IO.Compression.GzipStream class
6 // Christopher James Lahey <clahey@ximian.com>
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
13 using NUnit.Framework;
16 using System.IO.Compression;
18 namespace MonoTests.System.IO.Compression
21 public class GzipStreamTest : Assertion
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 [Category("NotWorking")] // #72143
49 public void CheckCompressDecompress () {
50 byte [] data = new byte[100000];
51 for (int i = 0; i < 100000; i++) {
54 MemoryStream dataStream = new MemoryStream (data);
55 MemoryStream backing = new MemoryStream ();
56 GzipStream compressing = new GzipStream (backing, CompressionMode.Compress, true);
57 CopyStream (dataStream, compressing);
60 backing.Seek (0, SeekOrigin.Begin);
61 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
62 MemoryStream output = new MemoryStream ();
63 CopyStream (decompressing, output);
64 Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
65 decompressing.Close();
70 public void CheckDecompress () {
71 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 };
72 MemoryStream backing = new MemoryStream (data);
73 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
74 StreamReader reader = new StreamReader (decompressing);
75 AssertEquals (reader.ReadLine (), "Hello");
76 decompressing.Close();
80 [ExpectedException (typeof (ArgumentNullException))]
81 public void CheckNullRead () {
82 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 };
83 MemoryStream backing = new MemoryStream (data);
84 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
85 decompressing.Read (null, 0, 20);
89 [ExpectedException (typeof (InvalidOperationException))]
90 [Category("NotWorking")] // #72143
91 public void CheckCompressingRead () {
92 byte [] dummy = new byte[20];
93 MemoryStream backing = new MemoryStream ();
94 GzipStream compressing = new GzipStream (backing, CompressionMode.Compress);
95 compressing.Read (dummy, 0, 20);
99 [ExpectedException (typeof (ArgumentNullException))]
100 [Category("NotWorking")] // #72143
101 public void CheckRangeRead () {
102 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 };
103 byte [] dummy = new byte[20];
104 MemoryStream backing = new MemoryStream (data);
105 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
106 decompressing.Read (dummy, 10, 20);
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);
120 [ExpectedException (typeof (ObjectDisposedException))]
121 [Category("NotWorking")] // #72143
122 public void CheckClosedRead () {
123 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 };
124 byte [] dummy = new byte[20];
125 MemoryStream backing = new MemoryStream (data);
126 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
127 decompressing.Close ();
128 decompressing.Read (dummy, 0, 20);
132 [ExpectedException (typeof (ObjectDisposedException))]
133 [Category("NotWorking")] // #72143
134 public void CheckClosedFlush () {
135 MemoryStream backing = new MemoryStream ();
136 GzipStream compressing = new GzipStream (backing, CompressionMode.Compress);
137 compressing.Close ();
138 compressing.Flush ();
142 [ExpectedException (typeof (NotSupportedException))]
143 public void CheckSeek () {
144 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 };
145 MemoryStream backing = new MemoryStream (data);
146 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
147 decompressing.Seek (20, SeekOrigin.Current);
151 [ExpectedException (typeof (NotSupportedException))]
152 public void CheckSetLength () {
153 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 };
154 MemoryStream backing = new MemoryStream (data);
155 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
156 decompressing.SetLength (20);
160 public void CheckGetCanSeekProp () {
161 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 };
162 MemoryStream backing = new MemoryStream (data);
163 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
164 AssertEquals (decompressing.CanSeek, false);
168 public void CheckGetCanReadProp () {
169 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 };
170 MemoryStream backing = new MemoryStream (data);
171 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
172 AssertEquals (decompressing.CanRead, true);
176 [Category("NotWorking")] // #72143
177 public void CheckGetCanWriteProp () {
178 MemoryStream backing = new MemoryStream ();
179 GzipStream compressing = new GzipStream (backing, CompressionMode.Decompress);
180 AssertEquals (compressing.CanWrite, true);
184 [ExpectedException (typeof (NotSupportedException))]
185 public void CheckSetLengthProp () {
186 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 };
187 MemoryStream backing = new MemoryStream (data);
188 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
189 decompressing.SetLength (20);
193 [ExpectedException (typeof (NotSupportedException))]
194 public void CheckGetLengthProp () {
195 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 };
196 MemoryStream backing = new MemoryStream (data);
197 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
198 long length = decompressing.Length;
202 [ExpectedException (typeof (NotSupportedException))]
203 public void CheckGetPositionProp () {
204 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 };
205 MemoryStream backing = new MemoryStream (data);
206 GzipStream decompressing = new GzipStream (backing, CompressionMode.Decompress);
207 long position = decompressing.Position;