2005-02-02 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / Test / System.IO.Compression / GzipStreamTest.cs
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  <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 GzipStreamTest : 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                 [Category("NotWorking")] // #72143
49                 public void CheckCompressDecompress () {
50                         byte [] data = new byte[100000];
51                         for (int i = 0; i < 100000; i++) {
52                                 data[i] = (byte) i;
53                         }
54                         MemoryStream dataStream = new MemoryStream (data);
55                         MemoryStream backing = new MemoryStream ();
56                         GzipStream compressing = new GzipStream (backing, CompressionMode.Compress, true);
57                         CopyStream (dataStream, compressing);
58                         dataStream.Close();
59                         compressing.Close();
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();
66                         output.Close();
67                 }
68
69                 [Test]
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();
77                 }
78
79                 [Test]
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);
86                 }
87
88                 [Test]
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);
96                 }
97
98                 [Test]
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);
107                 }
108
109                 [Test]
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);
117                 }
118
119                 [Test]
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);
129                 }
130
131                 [Test]
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 ();
139                 }
140
141                 [Test]
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);
148                 }
149
150                 [Test]
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);
157                 }
158
159                 [Test]
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);
165                 }
166
167                 [Test]
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);
173                 }
174
175                 [Test]
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);
181                 }
182
183                 [Test]
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);
190                 }
191
192                 [Test]
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;
199                 }
200
201                 [Test]
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;
208                 }
209         }
210 }
211
212 #endif