Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Store text files with LF in the repository and check them out with the platform's line ending (CRLF on Windows).
* text=auto

*.png binary
9 changes: 8 additions & 1 deletion .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- master
pull_request:
branches:
- master

defaults:
run:
Expand All @@ -17,8 +20,9 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
dotnet-version: |
10.0.x
11.0.x

- name: Restore dependencies
run: dotnet restore
Expand All @@ -30,15 +34,18 @@ jobs:
run: dotnet test -c Release --no-restore --no-build -v normal

- name: Pack NuGet Package(s)
if: github.event_name == 'push'
run: dotnet pack -c Release --no-restore --no-build --output ~/nuget-packages

- name: Upload Build Artifact(s)
if: github.event_name == 'push'
uses: actions/upload-artifact@v7
with:
name: nuget-packages
path: ~/nuget-packages

- name: Push NuGet Package(s)
if: github.event_name == 'push'
run: |
dotnet nuget push ~/nuget-packages/*.nupkg \
--source https://api.nuget.org/v3/index.json \
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
10 changes: 5 additions & 5 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<ItemGroup>
<PackageVersion Include="CommunityToolkit.HighPerformance" Version="8.4.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.15.8" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
<PackageVersion Include="xunit.v3" Version="3.2.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.11.1" />
<PackageVersion Include="xunit.v3" Version="4.0.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="4.0.1" />
<PackageVersion Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>
</Project>
</Project>
147 changes: 147 additions & 0 deletions src/OffiUtils.Tests/IDecompressorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ private static ReadOnlySpan<byte> RawData

private static IDecompressor DefaultDecompressor => DecompressorBuilder.Default.Build();

public static TheoryData<CompressionAlgorithm> DefaultAlgorithms =>
[
CompressionAlgorithm.Deflate,
CompressionAlgorithm.Gzip,
CompressionAlgorithm.Zlib,
CompressionAlgorithm.Brotli,
#if NET11_0_OR_GREATER
CompressionAlgorithm.Zstd,
#endif
];

private static byte[] Compress(CompressionAlgorithm algorithm, ReadOnlySpan<byte> data)
{
using MemoryStream output = new();
using (Stream compressor = algorithm switch
{
CompressionAlgorithm.Deflate => new DeflateStream(output, CompressionLevel.Optimal, leaveOpen: true),
CompressionAlgorithm.Gzip => new GZipStream(output, CompressionLevel.Optimal, leaveOpen: true),
CompressionAlgorithm.Zlib => new ZLibStream(output, CompressionLevel.Optimal, leaveOpen: true),
CompressionAlgorithm.Brotli => new BrotliStream(output, CompressionLevel.Optimal, leaveOpen: true),
#if NET11_0_OR_GREATER
CompressionAlgorithm.Zstd => new ZstandardStream(output, CompressionLevel.Optimal, leaveOpen: true),
#endif
_ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null),
})
{
compressor.Write(data);
}
return output.ToArray();
}

private static void AssertFails(CompressionAlgorithm algorithm, ReadOnlySpan<byte> source, Span<byte> destination)
{
Assert.False(DefaultDecompressor.TryDecompress(algorithm, source, destination, out int bytesWritten));
Assert.Equal(0, bytesWritten);
}

[Fact]
public void NotSupported()
{
Expand Down Expand Up @@ -115,4 +152,114 @@ public void TryBrotliStateful()
ReadOnlySpan<byte> decompressedData = stackBuffer.Slice(64, decompressedDataSize);
Assert.Equal(RawData, decompressedData);
}

[Theory]
[MemberData(nameof(DefaultAlgorithms))]
public void TryLargeData(CompressionAlgorithm algorithm)
{
// Big enough that the net10.0 stream fallback reads the source in many chunks. Half random, half zeros.
byte[] data = new byte[256 * 1024];
new Random(1337).NextBytes(data.AsSpan(0, data.Length / 2));
byte[] destination = new byte[data.Length];

Assert.Equal(data.Length, DefaultDecompressor.Decompress(algorithm, Compress(algorithm, data), destination));
Assert.Equal(data, destination);
}

[Theory]
[MemberData(nameof(DefaultAlgorithms))]
public void TryTrailingBytes(CompressionAlgorithm algorithm)
{
byte[] source = [.. Compress(algorithm, RawData), .. "bytes after the compressed data are ignored"u8];
Span<byte> destination = stackalloc byte[128];

Assert.True(DefaultDecompressor.TryDecompress(algorithm, source, destination, out int bytesWritten));
Assert.Equal(RawData, destination.Slice(0, bytesWritten));
}

[Theory]
[MemberData(nameof(DefaultAlgorithms))]
public void TryInvalid(CompressionAlgorithm algorithm)
{
byte[] compressed = Compress(algorithm, RawData);
byte[] destination = new byte[RawData.Length];

AssertFails(algorithm, "not compressed"u8, destination);
AssertFails(algorithm, [], destination);
AssertFails(algorithm, compressed.AsSpan(..^1), destination); // incomplete source
AssertFails(algorithm, compressed, destination.AsSpan(..^1)); // destination too small

Assert.Throws<InvalidDataException>(()
=> DefaultDecompressor.Decompress(algorithm, compressed, destination.AsSpan(..^1)));
}

[Fact]
public void TryGzipMembers()
{
// Like GZipStream, every gzip member is decompressed (RFC 1952, section 2.2).
byte[] member = Compress(CompressionAlgorithm.Gzip, RawData);
// Header, an empty fixed Huffman block, then CRC32 and ISIZE of 0.
byte[] emptyMember = [0x1F, 0x8B, 0x08, 0, 0, 0, 0, 0, 0, 0xFF, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0];
byte[] expected = [.. RawData, .. RawData];
Span<byte> destination = stackalloc byte[expected.Length];

Assert.True(DefaultDecompressor.TryDecompress(CompressionAlgorithm.Gzip, [.. member, .. member], destination, out int bytesWritten));
Assert.Equal(expected, destination.Slice(0, bytesWritten));

// An empty member still fits into a full destination.
Assert.True(DefaultDecompressor.TryDecompress(CompressionAlgorithm.Gzip, [.. member, .. emptyMember], destination.Slice(0, RawData.Length), out bytesWritten));
Assert.Equal(RawData, destination.Slice(0, bytesWritten));

// Bytes that start like a member are decompressed as one, so they have to be complete.
AssertFails(CompressionAlgorithm.Gzip, [.. member, 0x1F, 0x8B], destination);
}

#if NET11_0_OR_GREATER

[Fact]
public void TryZstdFrames()
{
// Like ZstandardStream, every frame is decompressed, skippable frames included (RFC 8878, section 3.1).
byte[] frame = Compress(CompressionAlgorithm.Zstd, RawData);
byte[] skippableFrame = [0x5E, 0x2A, 0x4D, 0x18, 0x04, 0x00, 0x00, 0x00, .. "meta"u8];
byte[] expected = [.. RawData, .. RawData];
Span<byte> destination = stackalloc byte[expected.Length];

Assert.True(DefaultDecompressor.TryDecompress(CompressionAlgorithm.Zstd,
[.. skippableFrame, .. frame, .. skippableFrame, .. frame, .. skippableFrame], destination, out int bytesWritten));
Assert.Equal(expected, destination.Slice(0, bytesWritten));

AssertFails(CompressionAlgorithm.Zstd, [.. frame, .. frame.AsSpan(..^1)], destination);
}

[Theory]
[InlineData(CompressionAlgorithm.Deflate)]
[InlineData(CompressionAlgorithm.Gzip)]
[InlineData(CompressionAlgorithm.Zlib)]
[InlineData(CompressionAlgorithm.Zstd)]
public void TryEncoder(CompressionAlgorithm algorithm)
{
Span<byte> stackBuffer = stackalloc byte[128];

int compressedDataSize = 0;
Assert.True(algorithm switch
{
CompressionAlgorithm.Deflate => DeflateEncoder.TryCompress(RawData, stackBuffer.Slice(0, 64), out compressedDataSize),
CompressionAlgorithm.Gzip => GZipEncoder.TryCompress(RawData, stackBuffer.Slice(0, 64), out compressedDataSize),
CompressionAlgorithm.Zlib => ZLibEncoder.TryCompress(RawData, stackBuffer.Slice(0, 64), out compressedDataSize),
CompressionAlgorithm.Zstd => ZstandardEncoder.TryCompress(RawData, stackBuffer.Slice(0, 64), out compressedDataSize),
_ => false,
});
ReadOnlySpan<byte> compressedData = stackBuffer.Slice(0, compressedDataSize);

Assert.True(DefaultDecompressor.TryDecompress(
algorithm,
compressedData, stackBuffer.Slice(64),
out int decompressedDataSize));

ReadOnlySpan<byte> decompressedData = stackBuffer.Slice(64, decompressedDataSize);
Assert.Equal(RawData, decompressedData);
}

#endif
}
Loading
Loading