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 [ExpectedException (typeof (ArgumentNullException))]
\r
49 public void Constructor_Null ()
\r
51 DeflateStream ds = new DeflateStream (null, CompressionMode.Compress);
\r
55 [ExpectedException (typeof (ArgumentException))]
\r
56 public void Constructor_InvalidCompressionMode ()
\r
58 DeflateStream ds = new DeflateStream (new MemoryStream (), (CompressionMode)Int32.MinValue);
\r
62 public void CheckCompressDecompress () {
63 byte [] data = new byte[100000];
64 for (int i = 0; i < 100000; i++) {
67 MemoryStream dataStream = new MemoryStream (data);
68 MemoryStream backing = new MemoryStream ();
69 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
70 CopyStream (dataStream, compressing);
73 backing.Seek (0, SeekOrigin.Begin);
74 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
75 MemoryStream output = new MemoryStream ();
76 CopyStream (decompressing, output);
77 Assert (compare_buffers (data, output.GetBuffer(), (int) output.Length));
78 decompressing.Close();
83 public void CheckDecompress () {
84 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
85 MemoryStream backing = new MemoryStream (data);
86 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
87 StreamReader reader = new StreamReader (decompressing);
88 AssertEquals (reader.ReadLine (), "Hello");
89 decompressing.Close();
93 [ExpectedException (typeof (ArgumentNullException))]
94 public void CheckNullRead () {
95 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
96 MemoryStream backing = new MemoryStream (data);
97 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
98 decompressing.Read (null, 0, 20);
102 [ExpectedException (typeof (InvalidOperationException))]
103 public void CheckCompressingRead () {
104 byte [] dummy = new byte[20];
105 MemoryStream backing = new MemoryStream ();
106 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
107 compressing.Read (dummy, 0, 20);
111 [ExpectedException (typeof (ArgumentException))]
112 public void CheckRangeRead () {
113 byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
114 byte [] dummy = new byte[20];
115 MemoryStream backing = new MemoryStream (data);
116 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
117 decompressing.Read (dummy, 10, 20);
121 [ExpectedException (typeof (InvalidDataException))]
122 public void CheckInvalidDataRead () {
123 byte [] data = {0x11, 0x78, 0x89, 0x91, 0xbe, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
124 byte [] dummy = new byte[20];
125 MemoryStream backing = new MemoryStream (data);
126 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
127 decompressing.Read (dummy, 0, 20);
131 [ExpectedException (typeof (ObjectDisposedException))]
132 public void CheckClosedRead () {
133 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
134 byte [] dummy = new byte[20];
135 MemoryStream backing = new MemoryStream (data);
136 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
137 decompressing.Close ();
138 decompressing.Read (dummy, 0, 20);
142 [ExpectedException (typeof (ObjectDisposedException))]
143 public void CheckClosedFlush () {
144 MemoryStream backing = new MemoryStream ();
145 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress);
146 compressing.Close ();
147 compressing.Flush ();
151 [ExpectedException (typeof (NotSupportedException))]
152 public void CheckSeek () {
153 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
154 MemoryStream backing = new MemoryStream (data);
155 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
156 decompressing.Seek (20, SeekOrigin.Current);
160 [ExpectedException (typeof (NotSupportedException))]
161 public void CheckSetLength () {
162 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
163 MemoryStream backing = new MemoryStream (data);
164 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
165 decompressing.SetLength (20);
169 public void CheckGetCanSeekProp () {
170 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
171 MemoryStream backing = new MemoryStream (data);
172 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
173 AssertEquals (false, decompressing.CanSeek);
177 public void CheckGetCanReadProp () {
178 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
179 MemoryStream backing = new MemoryStream (data);
180 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
181 AssertEquals (true, decompressing.CanRead);
185 public void CheckGetCanWriteProp () {
186 MemoryStream backing = new MemoryStream ();
187 DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
188 AssertEquals (false, compressing.CanWrite);
192 [ExpectedException (typeof (NotSupportedException))]
193 public void CheckSetLengthProp () {
194 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
195 MemoryStream backing = new MemoryStream (data);
196 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
197 decompressing.SetLength (20);
201 [ExpectedException (typeof (NotSupportedException))]
202 public void CheckGetLengthProp () {
203 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
204 MemoryStream backing = new MemoryStream (data);
205 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
206 long length = decompressing.Length;
210 [ExpectedException (typeof (NotSupportedException))]
211 public void CheckGetPositionProp () {
212 byte [] data = {0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };
213 MemoryStream backing = new MemoryStream (data);
214 DeflateStream decompressing = new DeflateStream (backing, CompressionMode.Decompress);
215 long position = decompressing.Position;