1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 // DeflateStreamTest.cs - NUnit Test Cases for the System.IO.Compression.DeflateStream 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 DeflateStreamTest : 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 public void CheckCompressDecompress () {
49 byte [] data = new byte[100000];
50 for (int i = 0; i < 100000; i++) {
53 MemoryStream dataStream = new MemoryStream (data);
54 MemoryStream backing = new MemoryStream ();
55 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
56 CopyStream (dataStream, compressing);
59 backing.Seek (0, SeekOrigin.Begin);
60 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
61 MemoryStream output = new MemoryStream ();
62 CopyStream (decompressing, output);
63 Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
64 decompressing.Close();
69 public void CheckDecompress () {
70 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
71 MemoryStream backing = new MemoryStream (data);
72 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
73 StreamReader reader = new StreamReader (decompressing);
74 AssertEquals (reader.ReadLine (), "Hello");
75 decompressing.Close();
79 [ExpectedException (typeof (ArgumentNullException))]
80 public void CheckNullRead () {
81 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
82 MemoryStream backing = new MemoryStream (data);
83 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
84 decompressing.Read (null, 0, 20);
88 [ExpectedException (typeof (InvalidOperationException))]
89 public void CheckCompressingRead () {
90 byte [] dummy = new byte[20];
91 MemoryStream backing = new MemoryStream ();
92 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
93 compressing.Read (dummy, 0, 20);
97 [ExpectedException (typeof (ArgumentNullException))]
98 public void CheckRangeRead () {
99 byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 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, 10, 20);
107 [ExpectedException (typeof (InvalidDataException))]
108 public void CheckInvalidDataRead () {
109 byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
110 byte [] dummy = new byte[20];
111 MemoryStream backing = new MemoryStream (data);
112 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
113 decompressing.Read (dummy, 0, 20);
117 [ExpectedException (typeof (ObjectDisposedException))]
118 public void CheckClosedRead () {
119 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
120 byte [] dummy = new byte[20];
121 MemoryStream backing = new MemoryStream (data);
122 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
123 decompressing.Close ();
124 decompressing.Read (dummy, 0, 20);
128 [ExpectedException (typeof (ObjectDisposedException))]
129 public void CheckClosedFlush () {
130 MemoryStream backing = new MemoryStream ();
131 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
132 compressing.Close ();
133 compressing.Flush ();
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);
146 [ExpectedException (typeof (NotSupportedException))]
147 public void CheckSetLength () {
148 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
149 MemoryStream backing = new MemoryStream (data);
150 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
151 decompressing.SetLength (20);
155 public void CheckGetCanSeekProp () {
156 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
157 MemoryStream backing = new MemoryStream (data);
158 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
159 AssertEquals (decompressing.CanSeek, false);
163 public void CheckGetCanReadProp () {
164 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
165 MemoryStream backing = new MemoryStream (data);
166 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
167 AssertEquals (decompressing.CanRead, true);
171 public void CheckGetCanWriteProp () {
172 MemoryStream backing = new MemoryStream ();
173 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
174 AssertEquals (compressing.CanWrite, true);
178 [ExpectedException (typeof (NotSupportedException))]
179 public void CheckSetLengthProp () {
180 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
181 MemoryStream backing = new MemoryStream (data);
182 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
183 decompressing.SetLength (20);
187 [ExpectedException (typeof (NotSupportedException))]
188 public void CheckGetLengthProp () {
189 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
190 MemoryStream backing = new MemoryStream (data);
191 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
192 long length = decompressing.Length;
196 [ExpectedException (typeof (NotSupportedException))]
197 public void CheckGetPositionProp () {
198 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
199 MemoryStream backing = new MemoryStream (data);
200 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
201 long position = decompressing.Position;