Repository navigation
Add ZLibStream polyfill #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e49b29d
Add ZLibStream polyfill with tests
Copilot 8d3dc48
Address ZLibStream review feedback
Copilot c452da7
Address ZLibStream review feedback
Copilot ce86d00
Refine ZLibStream tests per review
Copilot bb86315
Remove low-value ZLibStream interop test
Copilot cb3bb0c
Merge remote-tracking branch 'origin/prime' into copilot/add-polyfill…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.IO.Compression; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net60; | ||
|
|
||
| public class ZLibStreamTests | ||
| { | ||
| [Fact] | ||
| public void Constructor_NullStream_Test() | ||
| { | ||
| // Act | ||
| var act1 = () => new ZLibStream(null!, CompressionMode.Compress); | ||
| var act2 = () => new ZLibStream(null!, CompressionLevel.Optimal); | ||
|
|
||
| // Assert | ||
| act1.Should().Throw<ArgumentNullException>(); | ||
| act2.Should().Throw<ArgumentNullException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Compress_Empty_Test() | ||
| { | ||
| // Arrange | ||
| using var destination = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using ( | ||
| var zLibStream = new ZLibStream(destination, CompressionMode.Compress, leaveOpen: true) | ||
| ) | ||
| { | ||
| // No data written | ||
| } | ||
|
|
||
| // Assert | ||
| destination.ToArray().Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(CompressionLevel.Optimal, 0x78, 0x9C)] | ||
| [InlineData(CompressionLevel.Fastest, 0x78, 0x01)] | ||
| [InlineData(CompressionLevel.NoCompression, 0x78, 0x01)] | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
| public void Compress_Header_Test(CompressionLevel level, byte expectedCmf, byte expectedFlg) | ||
| { | ||
| // Arrange | ||
| using var destination = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using (var zLibStream = new ZLibStream(destination, level, leaveOpen: true)) | ||
| zLibStream.Write(new byte[] { 1, 2, 3 }, 0, 3); | ||
|
|
||
| // Assert | ||
| var result = destination.ToArray(); | ||
| result[0].Should().Be(expectedCmf); | ||
| result[1].Should().Be(expectedFlg); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Compress_Decompress_RoundTrip_Test() | ||
| { | ||
| // Arrange | ||
| var data = new byte[10000]; | ||
| new Random(1234567).NextBytes(data); | ||
|
|
||
| using var compressed = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using ( | ||
| var zLibStream = new ZLibStream(compressed, CompressionLevel.Optimal, leaveOpen: true) | ||
| ) | ||
| zLibStream.Write(data, 0, data.Length); | ||
|
|
||
| compressed.Position = 0; | ||
|
|
||
| using var decompressed = new MemoryStream(); | ||
| using ( | ||
| var zLibStream = new ZLibStream(compressed, CompressionMode.Decompress, leaveOpen: true) | ||
| ) | ||
| zLibStream.CopyTo(decompressed); | ||
|
|
||
| // Assert | ||
| decompressed.ToArray().Should().Equal(data); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Compress_Decompress_SmallWrites_Test() | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | ||
|
|
||
| using var compressed = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using ( | ||
| var zLibStream = new ZLibStream(compressed, CompressionLevel.Optimal, leaveOpen: true) | ||
| ) | ||
| { | ||
| foreach (var b in data) | ||
| zLibStream.WriteByte(b); | ||
| } | ||
|
|
||
| compressed.Position = 0; | ||
|
|
||
| using var decompressed = new MemoryStream(); | ||
| using ( | ||
| var zLibStream = new ZLibStream(compressed, CompressionMode.Decompress, leaveOpen: true) | ||
| ) | ||
| zLibStream.CopyTo(decompressed); | ||
|
|
||
| // Assert | ||
| decompressed.ToArray().Should().Equal(data); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Decompress_InteropWithDeflateStream_Test() | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Arrange | ||
| var data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| using var compressed = new MemoryStream(); | ||
| using ( | ||
| var deflateStream = new DeflateStream( | ||
| compressed, | ||
| CompressionLevel.Optimal, | ||
| leaveOpen: true | ||
| ) | ||
| ) | ||
| deflateStream.Write(data, 0, data.Length); | ||
|
|
||
| var rawDeflate = compressed.ToArray(); | ||
|
|
||
| // Construct a valid zlib stream manually: header + raw deflate payload + Adler-32 trailer | ||
| using var zlibBytes = new MemoryStream(); | ||
| zlibBytes.WriteByte(0x78); | ||
| zlibBytes.WriteByte(0x9C); | ||
| zlibBytes.Write(rawDeflate, 0, rawDeflate.Length); | ||
|
|
||
| var a = 1u; | ||
| var b = 0u; | ||
| foreach (var value in data) | ||
| { | ||
| a = (a + value) % 65521; | ||
| b = (b + a) % 65521; | ||
| } | ||
|
|
||
| var checksum = (b << 16) | a; | ||
| zlibBytes.WriteByte((byte)(checksum >> 24)); | ||
| zlibBytes.WriteByte((byte)(checksum >> 16)); | ||
| zlibBytes.WriteByte((byte)(checksum >> 8)); | ||
| zlibBytes.WriteByte((byte)checksum); | ||
|
|
||
| zlibBytes.Position = 0; | ||
|
|
||
| // Act | ||
| using var decompressed = new MemoryStream(); | ||
| using ( | ||
| var zLibStream = new ZLibStream(zlibBytes, CompressionMode.Decompress, leaveOpen: true) | ||
| ) | ||
| zLibStream.CopyTo(decompressed); | ||
|
|
||
| // Assert | ||
| decompressed.ToArray().Should().Equal(data); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Decompress_InvalidHeader_Test() | ||
| { | ||
| // Arrange | ||
| using var source = new MemoryStream(new byte[] { 0x00, 0x00, 0x01, 0x02 }); | ||
| using var zLibStream = new ZLibStream(source, CompressionMode.Decompress, leaveOpen: true); | ||
|
|
||
| // Act | ||
| var act = () => zLibStream.Read(new byte[10], 0, 10); | ||
|
|
||
| // Assert | ||
| act.Should().Throw<InvalidDataException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRead_CanWrite_CanSeek_Test() | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Arrange | ||
| using var destination = new MemoryStream(); | ||
|
|
||
| // Act & assert | ||
| using ( | ||
| var compressStream = new ZLibStream( | ||
| destination, | ||
| CompressionMode.Compress, | ||
| leaveOpen: true | ||
| ) | ||
| ) | ||
| { | ||
| compressStream.CanWrite.Should().BeTrue(); | ||
| compressStream.CanRead.Should().BeFalse(); | ||
| compressStream.CanSeek.Should().BeFalse(); | ||
| } | ||
|
|
||
| destination.Position = 0; | ||
|
|
||
| using ( | ||
| var decompressStream = new ZLibStream( | ||
| destination, | ||
| CompressionMode.Decompress, | ||
| leaveOpen: true | ||
| ) | ||
| ) | ||
| { | ||
| decompressStream.CanRead.Should().BeTrue(); | ||
| decompressStream.CanWrite.Should().BeFalse(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Unsupported_Members_Test() | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Arrange | ||
| using var destination = new MemoryStream(); | ||
| using var zLibStream = new ZLibStream( | ||
| destination, | ||
| CompressionMode.Compress, | ||
| leaveOpen: true | ||
| ); | ||
|
|
||
| // Act | ||
| var lengthAct = () => | ||
| { | ||
| _ = zLibStream.Length; | ||
| }; | ||
| var positionGetAct = () => | ||
| { | ||
| _ = zLibStream.Position; | ||
| }; | ||
| var positionSetAct = () => zLibStream.Position = 0; | ||
| var seekAct = () => zLibStream.Seek(0, SeekOrigin.Begin); | ||
| var setLengthAct = () => zLibStream.SetLength(0); | ||
|
|
||
| // Assert | ||
| lengthAct.Should().Throw<NotSupportedException>(); | ||
| positionGetAct.Should().Throw<NotSupportedException>(); | ||
| positionSetAct.Should().Throw<NotSupportedException>(); | ||
| seekAct.Should().Throw<NotSupportedException>(); | ||
| setLengthAct.Should().Throw<NotSupportedException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LeaveOpen_False_Test() | ||
| { | ||
| // Arrange | ||
| var destination = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using ( | ||
| var zLibStream = new ZLibStream(destination, CompressionLevel.Optimal, leaveOpen: false) | ||
| ) | ||
| zLibStream.Write(new byte[] { 1, 2, 3 }, 0, 3); | ||
|
|
||
| // Assert | ||
| var act = () => destination.WriteByte(0); | ||
| act.Should().Throw<ObjectDisposedException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LeaveOpen_True_Test() | ||
| { | ||
| // Arrange | ||
| using var destination = new MemoryStream(); | ||
|
|
||
| // Act | ||
| using ( | ||
| var zLibStream = new ZLibStream(destination, CompressionLevel.Optimal, leaveOpen: true) | ||
| ) | ||
| zLibStream.Write(new byte[] { 1, 2, 3 }, 0, 3); | ||
|
|
||
| // Assert | ||
| destination.WriteByte(0); // should not throw | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.