1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System ;
5
- using System . Collections . Generic ;
4
+ using System . Reflection ;
6
5
using System . Text ;
7
6
using System . Threading . Tasks ;
8
- using System . IO ;
9
- using System . Reflection ;
10
- using System . IO . Compression ;
11
7
using Xunit ;
12
8
13
9
namespace System . IO . Compression . Tests
14
10
{
15
- public partial class DeflateStreamTests
11
+ public class ZLibDeflateStreamTests : DeflateStreamTests , IDisposable
12
+ {
13
+ public ZLibDeflateStreamTests ( ) { SetWorkerMode ( "zlib" ) ; }
14
+ public void Dispose ( ) { SetWorkerMode ( "unknown" ) ; }
15
+ }
16
+
17
+ public class ManagedDeflateStreamTests : DeflateStreamTests , IDisposable
18
+ {
19
+ public ManagedDeflateStreamTests ( ) { SetWorkerMode ( "managed" ) ; }
20
+ public void Dispose ( ) { SetWorkerMode ( "unknown" ) ; }
21
+ }
22
+
23
+ public abstract class DeflateStreamTests
16
24
{
17
25
static string gzTestFile ( String fileName ) { return Path . Combine ( "GZTestData" , fileName ) ; }
18
26
27
+ internal static void SetWorkerMode ( string mode )
28
+ {
29
+ FieldInfo forceType = typeof ( DeflateStream ) . GetTypeInfo ( ) . GetDeclaredField ( "s_forcedTestingDeflaterType" ) ;
30
+ if ( forceType != null )
31
+ {
32
+ forceType . SetValue ( null , mode == "zlib" ? ( byte ) 2 : mode == "managed" ? ( byte ) 1 : ( byte ) 0 ) ;
33
+ }
34
+ }
35
+
19
36
[ Fact ]
20
- public static void BaseStream1 ( )
37
+ public void BaseStream1 ( )
21
38
{
22
39
var writeStream = new MemoryStream ( ) ;
23
40
var zip = new DeflateStream ( writeStream , CompressionMode . Compress ) ;
@@ -27,7 +44,7 @@ public static void BaseStream1()
27
44
}
28
45
29
46
[ Fact ]
30
- public static void BaseStream2 ( )
47
+ public void BaseStream2 ( )
31
48
{
32
49
var ms = new MemoryStream ( ) ;
33
50
var zip = new DeflateStream ( ms , CompressionMode . Decompress ) ;
@@ -37,7 +54,7 @@ public static void BaseStream2()
37
54
}
38
55
39
56
[ Fact ]
40
- public static async Task ModifyBaseStream ( )
57
+ public async Task ModifyBaseStream ( )
41
58
{
42
59
var ms = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.txt.gz" ) ) ;
43
60
var newMs = StripHeaderAndFooter . Strip ( ms ) ;
@@ -52,7 +69,7 @@ public static async Task ModifyBaseStream()
52
69
}
53
70
54
71
[ Fact ]
55
- public static void DecompressCanRead ( )
72
+ public void DecompressCanRead ( )
56
73
{
57
74
var ms = new MemoryStream ( ) ;
58
75
var zip = new DeflateStream ( ms , CompressionMode . Decompress ) ;
@@ -64,7 +81,7 @@ public static void DecompressCanRead()
64
81
}
65
82
66
83
[ Fact ]
67
- public static void CompressCanWrite ( )
84
+ public void CompressCanWrite ( )
68
85
{
69
86
var ms = new MemoryStream ( ) ;
70
87
var zip = new DeflateStream ( ms , CompressionMode . Compress ) ;
@@ -75,15 +92,15 @@ public static void CompressCanWrite()
75
92
}
76
93
77
94
[ Fact ]
78
- public static void CanDisposeBaseStream ( )
95
+ public void CanDisposeBaseStream ( )
79
96
{
80
97
var ms = new MemoryStream ( ) ;
81
98
var zip = new DeflateStream ( ms , CompressionMode . Compress ) ;
82
99
ms . Dispose ( ) ; // This would throw if this was invalid
83
100
}
84
101
85
102
[ Fact ]
86
- public static void CanDisposeDeflateStream ( )
103
+ public void CanDisposeDeflateStream ( )
87
104
{
88
105
var ms = new MemoryStream ( ) ;
89
106
var zip = new DeflateStream ( ms , CompressionMode . Compress ) ;
@@ -96,7 +113,7 @@ public static void CanDisposeDeflateStream()
96
113
}
97
114
98
115
[ Fact ]
99
- public static async Task CanReadBaseStreamAfterDispose ( )
116
+ public async Task CanReadBaseStreamAfterDispose ( )
100
117
{
101
118
var ms = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.txt.gz" ) ) ;
102
119
var newMs = StripHeaderAndFooter . Strip ( ms ) ;
@@ -114,7 +131,7 @@ public static async Task CanReadBaseStreamAfterDispose()
114
131
}
115
132
116
133
[ Fact ]
117
- public static async Task DecompressWorks ( )
134
+ public async Task DecompressWorks ( )
118
135
{
119
136
var compareStream = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.txt" ) ) ;
120
137
var gzStream = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.txt.gz" ) ) ;
@@ -123,7 +140,7 @@ public static async Task DecompressWorks()
123
140
}
124
141
125
142
[ Fact ]
126
- public static async Task DecompressWorksWithBinaryFile ( )
143
+ public async Task DecompressWorksWithBinaryFile ( )
127
144
{
128
145
var compareStream = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.doc" ) ) ;
129
146
var gzStream = await LocalMemoryStream . readAppFileAsync ( gzTestFile ( "GZTestDocument.doc.gz" ) ) ;
@@ -132,7 +149,7 @@ public static async Task DecompressWorksWithBinaryFile()
132
149
}
133
150
134
151
// Making this async since regular read/write are tested below
135
- private static async Task DecompressAsync ( MemoryStream compareStream , MemoryStream gzStream )
152
+ private async Task DecompressAsync ( MemoryStream compareStream , MemoryStream gzStream )
136
153
{
137
154
var strippedMs = StripHeaderAndFooter . Strip ( gzStream ) ;
138
155
@@ -167,8 +184,9 @@ private static async Task DecompressAsync(MemoryStream compareStream, MemoryStre
167
184
Assert . Equal ( compareArray [ i ] , writtenArray [ i ] ) ;
168
185
}
169
186
}
187
+
170
188
[ Fact ]
171
- public static async Task DecompressFailsWithRealGzStream ( )
189
+ public async Task DecompressFailsWithRealGzStream ( )
172
190
{
173
191
String [ ] files = { gzTestFile ( "GZTestDocument.doc.gz" ) , gzTestFile ( "GZTestDocument.txt.gz" ) } ;
174
192
foreach ( String fileName in files )
@@ -181,8 +199,9 @@ public static async Task DecompressFailsWithRealGzStream()
181
199
zip . Dispose ( ) ;
182
200
}
183
201
}
202
+
184
203
[ Fact ]
185
- public static void NullBaseStreamThrows ( )
204
+ public void NullBaseStreamThrows ( )
186
205
{
187
206
Assert . Throws < ArgumentNullException > ( ( ) =>
188
207
{
@@ -194,8 +213,9 @@ public static void NullBaseStreamThrows()
194
213
var deflate = new DeflateStream ( null , CompressionMode . Compress ) ;
195
214
} ) ;
196
215
}
216
+
197
217
[ Fact ]
198
- public static void DisposedBaseStreamThrows ( )
218
+ public void DisposedBaseStreamThrows ( )
199
219
{
200
220
var ms = new MemoryStream ( ) ;
201
221
ms . Dispose ( ) ;
@@ -209,8 +229,9 @@ public static void DisposedBaseStreamThrows()
209
229
var deflate = new DeflateStream ( ms , CompressionMode . Compress ) ;
210
230
} ) ;
211
231
}
232
+
212
233
[ Fact ]
213
- public static void ReadOnlyStreamThrowsOnCompress ( )
234
+ public void ReadOnlyStreamThrowsOnCompress ( )
214
235
{
215
236
var ms = new LocalMemoryStream ( ) ;
216
237
ms . SetCanWrite ( false ) ;
@@ -220,8 +241,9 @@ public static void ReadOnlyStreamThrowsOnCompress()
220
241
var gzip = new DeflateStream ( ms , CompressionMode . Compress ) ;
221
242
} ) ;
222
243
}
244
+
223
245
[ Fact ]
224
- public static void WriteOnlyStreamThrowsOnDecompress ( )
246
+ public void WriteOnlyStreamThrowsOnDecompress ( )
225
247
{
226
248
var ms = new LocalMemoryStream ( ) ;
227
249
ms . SetCanRead ( false ) ;
@@ -231,8 +253,9 @@ public static void WriteOnlyStreamThrowsOnDecompress()
231
253
var gzip = new DeflateStream ( ms , CompressionMode . Decompress ) ;
232
254
} ) ;
233
255
}
256
+
234
257
[ Fact ]
235
- public static void TestCtors ( )
258
+ public void TestCtors ( )
236
259
{
237
260
CompressionLevel [ ] legalValues = new CompressionLevel [ ] { CompressionLevel . Optimal , CompressionLevel . Fastest , CompressionLevel . NoCompression } ;
238
261
@@ -246,23 +269,26 @@ public static void TestCtors()
246
269
}
247
270
}
248
271
}
272
+
249
273
[ Fact ]
250
- public static void TestLevelOptimial ( )
274
+ public void TestLevelOptimial ( )
251
275
{
252
276
TestCtor ( CompressionLevel . Optimal ) ;
253
277
}
278
+
254
279
[ Fact ]
255
- public static void TestLevelNoCompression ( )
280
+ public void TestLevelNoCompression ( )
256
281
{
257
282
TestCtor ( CompressionLevel . NoCompression ) ;
258
283
}
284
+
259
285
[ Fact ]
260
- public static void TestLevelFastest ( )
286
+ public void TestLevelFastest ( )
261
287
{
262
288
TestCtor ( CompressionLevel . Fastest ) ;
263
289
}
264
290
265
- public static void TestCtor ( CompressionLevel level , bool ? leaveOpen = null )
291
+ private static void TestCtor ( CompressionLevel level , bool ? leaveOpen = null )
266
292
{
267
293
//Create the DeflateStream
268
294
int _bufferSize = 1024 ;
@@ -314,7 +340,7 @@ public static void TestCtor(CompressionLevel level, bool? leaveOpen = null)
314
340
}
315
341
316
342
[ Fact ]
317
- public static async Task Flush ( )
343
+ public async Task Flush ( )
318
344
{
319
345
var ms = new MemoryStream ( ) ;
320
346
var ds = new DeflateStream ( ms , CompressionMode . Compress ) ;
@@ -323,16 +349,18 @@ public static async Task Flush()
323
349
324
350
// Just ensuring Flush doesn't throw
325
351
}
352
+
326
353
[ Fact ]
327
- public static void FlushFailsAfterDispose ( )
354
+ public void FlushFailsAfterDispose ( )
328
355
{
329
356
var ms = new MemoryStream ( ) ;
330
357
var ds = new DeflateStream ( ms , CompressionMode . Compress ) ;
331
358
ds . Dispose ( ) ;
332
359
Assert . Throws < ObjectDisposedException > ( ( ) => { ds . Flush ( ) ; } ) ;
333
360
}
361
+
334
362
[ Fact ]
335
- public static async Task FlushAsyncFailsAfterDispose ( )
363
+ public async Task FlushAsyncFailsAfterDispose ( )
336
364
{
337
365
338
366
var ms = new MemoryStream ( ) ;
@@ -346,7 +374,7 @@ await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
346
374
}
347
375
348
376
[ Fact ]
349
- public static void TestSeekMethodsDecompress ( )
377
+ public void TestSeekMethodsDecompress ( )
350
378
{
351
379
var ms = new MemoryStream ( ) ;
352
380
var zip = new DeflateStream ( ms , CompressionMode . Decompress ) ;
@@ -360,8 +388,9 @@ public static void TestSeekMethodsDecompress()
360
388
//Should we try all the enums? doesn't seem necessary
361
389
Assert . Throws < NotSupportedException > ( delegate { zip . Seek ( 100L , SeekOrigin . Begin ) ; } ) ;
362
390
}
391
+
363
392
[ Fact ]
364
- public static void TestSeekMethodsCompress ( )
393
+ public void TestSeekMethodsCompress ( )
365
394
{
366
395
var ms = new MemoryStream ( ) ;
367
396
var zip = new DeflateStream ( ms , CompressionMode . Compress ) ;
0 commit comments