2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / Test / System.IO.Compression / DeflateStreamTest.cs
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  <clahey@ximian.com>
7 //
8 // (C) 2004 Novell, Inc. <http://www.novell.com>
9 // 
10
11 #if NET_2_0
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.IO.Compression;
17
18 namespace MonoTests.System.IO.Compression
19 {
20         [TestFixture]
21         public class DeflateStreamTest : Assertion
22         {
23                 private static void CopyStream (Stream src, Stream dest)
24                 {
25                         byte[] array = new byte[1024];
26                         int bytes_read;
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);
31                         }
32                 }
33
34                 private static bool compare_buffers (byte[] first, byte[] second, int length)
35                 {
36                         if (first.Length < length || second.Length < length) {
37                                 return false;
38                         }
39                         for (int i = 0; i < length; i++) {
40                                 if (first[i] != second[i]) {
41                                         return false;
42                                 }
43                         }
44                         return true;
45                 }
46
47                 [Test]
48                 public void CheckCompressDecompress () {
49                         byte [] data = new byte[100000];
50                         for (int i = 0; i < 100000; i++) {
51                                 data[i] = (byte) i;
52                         }
53                         MemoryStream dataStream = new MemoryStream (data);
54                         MemoryStream backing = new MemoryStream ();
55                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Compress, true);
56                         CopyStream (dataStream, compressing);
57                         dataStream.Close();
58                         compressing.Close();
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();
65                         output.Close();
66                 }
67
68                 [Test]
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();
76                 }
77
78                 [Test]
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);
85                 }
86
87                 [Test]
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);
94                 }
95
96                 [Test]
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);
104                 }
105
106                 [Test]
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);
114                 }
115
116                 [Test]
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);
125                 }
126
127                 [Test]
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 ();
134                 }
135
136                 [Test]
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);
143                 }
144
145                 [Test]
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);
152                 }
153
154                 [Test]
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);
160                 }
161
162                 [Test]
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);
168                 }
169
170                 [Test]
171                 public void CheckGetCanWriteProp () {
172                         MemoryStream backing = new MemoryStream ();
173                         DeflateStream compressing = new DeflateStream (backing, CompressionMode.Decompress);
174                         AssertEquals (compressing.CanWrite, true);
175                 }
176
177                 [Test]
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);
184                 }
185
186                 [Test]
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;
193                 }
194
195                 [Test]
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;
202                 }
203         }
204 }
205
206 #endif