Skip to content

Commit

Permalink
Add test and probable fix for Issue 617
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Nov 22, 2021
1 parent a1e7c00 commit 05208cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
5 changes: 0 additions & 5 deletions src/SharpCompress/Algorithms/Alder32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public static uint Calculate(ReadOnlySpan<byte> buffer)
/// <returns>The <see cref="uint"/>.</returns>
public static uint Calculate(uint adler, ReadOnlySpan<byte> buffer)
{
if (buffer.IsEmpty)
{
return SeedValue;
}

#if !NETSTANDARD2_0 && !NETSTANDARD2_1 && !NETFRAMEWORK
if (Sse3.IsSupported && buffer.Length >= MinBufferSize)
{
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/SharpCompress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<AssemblyTitle>SharpCompress - Pure C# Decompression/Compression</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>0.30.0</VersionPrefix>
<AssemblyVersion>0.30.0</AssemblyVersion>
<FileVersion>0.30.0</FileVersion>
<VersionPrefix>0.30.1</VersionPrefix>
<AssemblyVersion>0.30.1</AssemblyVersion>
<FileVersion>0.30.1</FileVersion>
<Authors>Adam Hathcock</Authors>
<TargetFrameworks>net461;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
3 changes: 2 additions & 1 deletion tests/SharpCompress.Test/SharpCompress.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
<ProjectReference Include="..\..\src\SharpCompress\SharpCompress.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>
Expand Down
53 changes: 53 additions & 0 deletions tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.IO;
using System.Text;
using FluentAssertions;
using SharpCompress.Compressors;
using SharpCompress.Compressors.Deflate;
using SharpCompress.IO;
using Xunit;

namespace SharpCompress.Test.Streams
Expand Down Expand Up @@ -31,5 +34,55 @@ public void TestChunkedZlibCompressesEverything()

Assert.Equal(total, realCompressedSize);
}

[Fact]
public void Zlib_should_read_the_previously_written_message()
{
var message = new string('a', 131073); // 131073 causes the failure, but 131072 (-1) doesn't
var bytes = Encoding.ASCII.GetBytes(message);

using (var inputStream = new MemoryStream(bytes))
{
using (var compressedStream = new MemoryStream())
using (var byteBufferStream = new BufferedStream(inputStream)) // System.IO
{
Compress(byteBufferStream, compressedStream, compressionLevel: 1);
compressedStream.Position = 0;

using (var decompressedStream = new MemoryStream())
{
Decompress(compressedStream, decompressedStream);

byteBufferStream.Position = 0;
var result = Encoding.ASCII.GetString(GetBytes(byteBufferStream));
result.Should().Be(message);
}
}
}
}

public void Compress(Stream input, Stream output, int compressionLevel)
{
using (var zlibStream = new ZlibStream(new NonDisposingStream(output), CompressionMode.Compress, (CompressionLevel)compressionLevel))
{
zlibStream.FlushMode = FlushType.Sync;
input.CopyTo(zlibStream);
}
}

public void Decompress(Stream input, Stream output)
{
using (var zlibStream = new ZlibStream(new NonDisposingStream(input), CompressionMode.Decompress))
{
zlibStream.CopyTo(output);
}
}

byte[] GetBytes(BufferedStream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
}
}
}

0 comments on commit 05208cc

Please sign in to comment.