-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
CompressionStreamUnitTests.Brotli.cs
404 lines (349 loc) · 20.2 KB
/
CompressionStreamUnitTests.Brotli.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Linq;
using Xunit;
namespace System.IO.Compression
{
public class BrotliStreamUnitTests : CompressionStreamUnitTestBase
{
public override Stream CreateStream(Stream stream, CompressionMode mode) => new BrotliStream(stream, mode);
public override Stream CreateStream(Stream stream, CompressionMode mode, bool leaveOpen) => new BrotliStream(stream, mode, leaveOpen);
public override Stream CreateStream(Stream stream, CompressionLevel level) => new BrotliStream(stream, level);
public override Stream CreateStream(Stream stream, CompressionLevel level, bool leaveOpen) => new BrotliStream(stream, level, leaveOpen);
public override Stream BaseStream(Stream stream) => ((BrotliStream)stream).BaseStream;
protected override bool FlushGuaranteesAllDataWritten => false;
// The tests are relying on an implementation detail of BrotliStream, using knowledge of its internal buffer size
// in various test calculations. Currently the implementation is using the ArrayPool, which will round up to a
// power-of-2. If the buffer size employed changes (which could also mean that ArrayPool<byte>.Shared starts giving
// out different array sizes), the tests will need to be tweaked.
public override int BufferSize => 1 << 16;
protected override string CompressedTestFile(string uncompressedPath) => Path.Combine("BrotliTestData", Path.GetFileName(uncompressedPath) + ".br");
[Fact]
[OuterLoop("Test takes ~6 seconds to run")]
public override void FlushAsync_DuringWriteAsync() { base.FlushAsync_DuringWriteAsync(); }
[Theory]
[InlineData((CompressionLevel)(-1))]
[InlineData((CompressionLevel)4)]
public void Ctor_ArgumentValidation_InvalidCompressionLevel(CompressionLevel compressionLevel)
{
Assert.Throws<ArgumentException>(() => new BrotliStream(new MemoryStream(), compressionLevel));
}
[Fact]
public void InvalidQuality()
{
Assert.Throws<ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(-1, 11));
Assert.Throws<ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(12, 11));
Assert.Throws<ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan<byte>(), new Span<byte>(), out int bytesWritten, -1, 13));
Assert.Throws<ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan<byte>(), new Span<byte>(), out int bytesWritten, 12, 13));
}
[Fact]
public void InvalidWindow()
{
Assert.Throws<ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, -1));
Assert.Throws<ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 9));
Assert.Throws<ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 25));
Assert.Throws<ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan<byte>(), new Span<byte>(), out int bytesWritten, 6, -1));
Assert.Throws<ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan<byte>(), new Span<byte>(), out int bytesWritten, 6, 9));
Assert.Throws<ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan<byte>(), new Span<byte>(), out int bytesWritten, 6, 25));
}
[Fact]
public void GetMaxCompressedSize_Basic()
{
Assert.Throws<ArgumentOutOfRangeException>("inputSize", () => BrotliEncoder.GetMaxCompressedLength(-1));
Assert.Throws<ArgumentOutOfRangeException>("inputSize", () => BrotliEncoder.GetMaxCompressedLength(2147483133));
Assert.InRange(BrotliEncoder.GetMaxCompressedLength(2147483132), 0, int.MaxValue);
Assert.Equal(1, BrotliEncoder.GetMaxCompressedLength(0));
}
[Fact]
public void GetMaxCompressedSize()
{
string uncompressedFile = UncompressedTestFile();
string compressedFile = CompressedTestFile(uncompressedFile);
int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength((int)new FileInfo(uncompressedFile).Length);
int actualCompressedSize = (int)new FileInfo(compressedFile).Length;
Assert.True(maxCompressedSize >= actualCompressedSize, $"MaxCompressedSize: {maxCompressedSize}, ActualCompressedSize: {actualCompressedSize}");
}
/// <summary>
/// Test to ensure that when given an empty Destination span, the decoder will consume no input and write no output.
/// </summary>
[Fact]
public void Decompress_WithEmptyDestination()
{
string testFile = UncompressedTestFile();
byte[] sourceBytes = File.ReadAllBytes(CompressedTestFile(testFile));
byte[] destinationBytes = new byte[0];
ReadOnlySpan<byte> source = new ReadOnlySpan<byte>(sourceBytes);
Span<byte> destination = new Span<byte>(destinationBytes);
Assert.False(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress completed successfully but should have failed due to too short of a destination array");
Assert.Equal(0, bytesWritten);
BrotliDecoder decoder = default;
var result = decoder.Decompress(source, destination, out int bytesConsumed, out bytesWritten);
Assert.Equal(0, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.DestinationTooSmall, result);
}
/// <summary>
/// Test to ensure that when given an empty source span, the decoder will consume no input and write no output
/// </summary>
[Fact]
public void Decompress_WithEmptySource()
{
byte[] sourceBytes = new byte[0];
byte[] destinationBytes = new byte[100000];
ReadOnlySpan<byte> source = new ReadOnlySpan<byte>(sourceBytes);
Span<byte> destination = new Span<byte>(destinationBytes);
Assert.False(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress completed successfully but should have failed due to too short of a source array");
Assert.Equal(0, bytesWritten);
BrotliDecoder decoder = default;
var result = decoder.Decompress(source, destination, out int bytesConsumed, out bytesWritten);
Assert.Equal(0, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.NeedMoreData, result);
}
/// <summary>
/// Test to ensure that when given an empty Destination span, the encoder consume no input and write no output
/// </summary>
[Fact]
public void Compress_WithEmptyDestination()
{
string testFile = UncompressedTestFile();
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] empty = new byte[0];
ReadOnlySpan<byte> source = new ReadOnlySpan<byte>(correctUncompressedBytes);
Span<byte> destination = new Span<byte>(empty);
Assert.False(BrotliEncoder.TryCompress(source, destination, out int bytesWritten), "TryCompress completed successfully but should have failed due to too short of a destination array");
Assert.Equal(0, bytesWritten);
BrotliEncoder encoder = default;
var result = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);
Assert.Equal(0, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.DestinationTooSmall, result);
result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
Assert.Equal(0, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.DestinationTooSmall, result);
}
/// <summary>
/// Test to ensure that when given an empty source span, the decoder will consume no input and write no output (until the finishing block)
/// </summary>
[Fact]
public void Compress_WithEmptySource()
{
byte[] sourceBytes = new byte[0];
byte[] destinationBytes = new byte[100000];
ReadOnlySpan<byte> source = new ReadOnlySpan<byte>(sourceBytes);
Span<byte> destination = new Span<byte>(destinationBytes);
Assert.True(BrotliEncoder.TryCompress(source, destination, out int bytesWritten));
// The only byte written should be the Brotli end of stream byte which varies based on the window/quality
Assert.Equal(1, bytesWritten);
BrotliEncoder encoder = default;
var result = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);
Assert.Equal(0, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.Done, result);
result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
Assert.Equal(1, bytesWritten);
Assert.Equal(0, bytesConsumed);
Assert.Equal(OperationStatus.Done, result);
}
/// <summary>
/// Test that the decoder can handle partial chunks of flushed encoded data sent from the BrotliEncoder
/// </summary>
[Fact]
public void RoundTrip_Chunks()
{
int chunkSize = 100;
int totalSize = 20000;
BrotliEncoder encoder = default;
BrotliDecoder decoder = default;
for (int i = 0; i < totalSize; i += chunkSize)
{
byte[] uncompressed = new byte[chunkSize];
Random.Shared.NextBytes(uncompressed);
byte[] compressed = new byte[BrotliEncoder.GetMaxCompressedLength(chunkSize)];
byte[] decompressed = new byte[chunkSize];
var uncompressedSpan = new ReadOnlySpan<byte>(uncompressed);
var compressedSpan = new Span<byte>(compressed);
var decompressedSpan = new Span<byte>(decompressed);
int totalWrittenThisIteration = 0;
var compress = encoder.Compress(uncompressedSpan, compressedSpan, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
totalWrittenThisIteration += bytesWritten;
compress = encoder.Flush(compressedSpan.Slice(bytesWritten), out bytesWritten);
totalWrittenThisIteration += bytesWritten;
var res = decoder.Decompress(compressedSpan.Slice(0, totalWrittenThisIteration), decompressedSpan, out int decompressbytesConsumed, out int decompressbytesWritten);
Assert.Equal(totalWrittenThisIteration, decompressbytesConsumed);
Assert.Equal(bytesConsumed, decompressbytesWritten);
Assert.Equal<byte>(uncompressed, decompressedSpan.ToArray());
}
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void ReadFully(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = File.ReadAllBytes(CompressedTestFile(testFile));
byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length + 10000];
ReadOnlySpan<byte> source = new ReadOnlySpan<byte>(compressedBytes);
Span<byte> destination = new Span<byte>(actualUncompressedBytes);
Assert.True(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress did not complete successfully");
Assert.Equal(correctUncompressedBytes.Length, bytesWritten);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray());
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void ReadWithState(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = File.ReadAllBytes(CompressedTestFile(testFile));
byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length];
Decompress_WithState(compressedBytes, actualUncompressedBytes);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes);
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void ReadWithoutState(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = File.ReadAllBytes(CompressedTestFile(testFile));
byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length];
Decompress_WithoutState(compressedBytes, actualUncompressedBytes);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes);
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void WriteFully(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
byte[] actualUncompressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
Span<byte> destination = new Span<byte>(compressedBytes);
Assert.True(BrotliEncoder.TryCompress(correctUncompressedBytes, destination, out int bytesWritten));
Assert.True(BrotliDecoder.TryDecompress(destination, actualUncompressedBytes, out bytesWritten));
Assert.Equal(correctUncompressedBytes.Length, bytesWritten);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray());
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void WriteWithState(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length];
Compress_WithState(correctUncompressedBytes, compressedBytes);
Decompress_WithState(compressedBytes, actualUncompressedBytes);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes);
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void WriteWithoutState(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length];
Compress_WithoutState(correctUncompressedBytes, compressedBytes);
Decompress_WithoutState(compressedBytes, actualUncompressedBytes);
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes);
}
[Theory]
[OuterLoop("Full set of UncompressedTestFiles takes around 15s to run")]
[MemberData(nameof(UncompressedTestFiles))]
public void WriteStream(string testFile)
{
byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
byte[] compressedBytes = Compress_Stream(correctUncompressedBytes, CompressionLevel.Optimal).ToArray();
byte[] actualUncompressedBytes = Decompress_Stream(compressedBytes).ToArray();
Assert.Equal<byte>(correctUncompressedBytes, actualUncompressedBytes);
}
[Theory]
[InlineData(1000, CompressionLevel.Fastest)]
[InlineData(1000, CompressionLevel.Optimal)]
[InlineData(1000, CompressionLevel.NoCompression)]
public static void Roundtrip_WriteByte_ReadByte_Success(int totalLength, CompressionLevel level)
{
byte[] correctUncompressedBytes = Enumerable.Range(0, totalLength).Select(i => (byte)i).ToArray();
byte[] compressedBytes;
using (var ms = new MemoryStream())
{
var bs = new BrotliStream(ms, level);
foreach (byte b in correctUncompressedBytes)
{
bs.WriteByte(b);
}
bs.Dispose();
compressedBytes = ms.ToArray();
}
byte[] decompressedBytes = new byte[correctUncompressedBytes.Length];
using (var ms = new MemoryStream(compressedBytes))
using (var bs = new BrotliStream(ms, CompressionMode.Decompress))
{
for (int i = 0; i < decompressedBytes.Length; i++)
{
int b = bs.ReadByte();
Assert.InRange(b, 0, 255);
decompressedBytes[i] = (byte)b;
}
Assert.Equal(-1, bs.ReadByte());
Assert.Equal(-1, bs.ReadByte());
}
Assert.Equal<byte>(correctUncompressedBytes, decompressedBytes);
}
private static void Compress_WithState(ReadOnlySpan<byte> input, Span<byte> output)
{
BrotliEncoder encoder = default;
while (!input.IsEmpty && !output.IsEmpty)
{
encoder.Compress(input, output, out int bytesConsumed, out int written, isFinalBlock: false);
input = input.Slice(bytesConsumed);
output = output.Slice(written);
}
encoder.Compress(ReadOnlySpan<byte>.Empty, output, out int bytesConsumed2, out int bytesWritten, isFinalBlock: true);
}
private static void Decompress_WithState(ReadOnlySpan<byte> input, Span<byte> output)
{
BrotliDecoder decoder = default;
while (!input.IsEmpty && !output.IsEmpty)
{
decoder.Decompress(input, output, out int bytesConsumed, out int written);
input = input.Slice(bytesConsumed);
output = output.Slice(written);
}
}
private static void Compress_WithoutState(ReadOnlySpan<byte> input, Span<byte> output)
{
BrotliEncoder.TryCompress(input, output, out int bytesWritten);
}
private static void Decompress_WithoutState(ReadOnlySpan<byte> input, Span<byte> output)
{
BrotliDecoder.TryDecompress(input, output, out int bytesWritten);
}
private static MemoryStream Compress_Stream(ReadOnlySpan<byte> input, CompressionLevel compressionLevel)
{
using (var inputStream = new MemoryStream(input.ToArray()))
{
var outputStream = new MemoryStream();
var compressor = new BrotliStream(outputStream, compressionLevel, true);
inputStream.CopyTo(compressor);
compressor.Dispose();
return outputStream;
}
}
private static MemoryStream Decompress_Stream(ReadOnlySpan<byte> input)
{
using (var inputStream = new MemoryStream(input.ToArray()))
{
var outputStream = new MemoryStream();
var decompressor = new BrotliStream(inputStream, CompressionMode.Decompress, true);
decompressor.CopyTo(outputStream);
decompressor.Dispose();
return outputStream;
}
}
}
}