-
Notifications
You must be signed in to change notification settings - Fork 519
Fix edge cases with 1-byte reads #1282
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
Open
kenkendk
wants to merge
6
commits into
adamhathcock:master
Choose a base branch
from
kenkendk:bugfix/single-byte-reads
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed442de
This fixes a missing overload for `ReadByte()` that caused the librar…
kenkendk e2dac67
Addressed review concerns in test code.
kenkendk 1fadf36
Fixed follow-up failing tests. It does not look like these failures a…
kenkendk 08ed55c
Adhere to previous defaults
kenkendk 4f919f2
Fixed formatting, comments and restored default auto-close behavior
kenkendk 00ed477
Reduced tests to minimum for testing the issue
kenkendk 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
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
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
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
192 changes: 192 additions & 0 deletions
192
tests/SharpCompress.Test/Zip/ZipCompressionRoundtripTests.cs
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,192 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using SharpCompress.Archives.Zip; | ||
| using SharpCompress.Common; | ||
| using SharpCompress.Readers; | ||
| using SharpCompress.Writers; | ||
| using SharpCompress.Writers.Zip; | ||
| using Xunit; | ||
|
|
||
| namespace SharpCompress.Test.Zip; | ||
|
|
||
| public class ZipCompressionRoundtripTests : TestBase | ||
| { | ||
| private const int TestSize = 1024 * 1024; | ||
|
kenkendk marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static (byte[] TestSet1, byte[] TestSet2, MemoryStream Stream) CreateTestZip() | ||
| { | ||
| var testset1 = Enumerable | ||
| .Range(0, TestSize) | ||
| .Select(i => (byte)((i * 22695477) % 257)) | ||
| .ToArray(); | ||
| var testset2 = Enumerable | ||
| .Range(0, TestSize) | ||
| .Select(i => (byte)((i * 48271) % 257)) | ||
| .ToArray(); | ||
|
|
||
| var stream = new MemoryStream(); | ||
|
|
||
| var writerOptions = new ZipWriterOptions(CompressionType.Deflate, compressionLevel: 9); | ||
|
|
||
| using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, writerOptions)) | ||
| { | ||
| writer.Write("sample1", new MemoryStream(testset1)); | ||
| writer.Write("sample2", new MemoryStream(testset2)); | ||
| } | ||
|
|
||
| stream.Position = 0; | ||
| return (testset1, testset2, stream); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Zip_Deflate_Roundtrip_ArchiveApi_BufferedRead_Succeeds() | ||
| { | ||
| var (testset1, testset2, stream) = CreateTestZip(); | ||
|
|
||
| // Decompress and verify using Archive API with buffered read (CopyTo) | ||
| stream.Position = 0; | ||
| using (var archive = ZipArchive.OpenArchive(stream)) | ||
| { | ||
| var files = archive.Entries.Where(e => !e.IsDirectory).OrderBy(e => e.Key).ToList(); | ||
| Assert.Equal(2, files.Count); | ||
|
|
||
| // Read using CopyTo pattern (buffered - should succeed) | ||
| using (var entryStream = files[1].OpenEntryStream()) | ||
| { | ||
| using var extracted = new MemoryStream(); | ||
| entryStream.CopyTo(extracted); | ||
| Assert.Equal(testset2, extracted.ToArray()); | ||
| } | ||
|
|
||
| using (var entryStream = files[0].OpenEntryStream()) | ||
| { | ||
| using var extracted = new MemoryStream(); | ||
| entryStream.CopyTo(extracted); | ||
| Assert.Equal(testset1, extracted.ToArray()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Zip_Deflate_Roundtrip_ArchiveApi_ByteByByteRead_Succeeds() | ||
| { | ||
| var (testset1, testset2, stream) = CreateTestZip(); | ||
| using (var archive = ZipArchive.OpenArchive(stream)) | ||
| { | ||
| var files = archive.Entries.Where(e => !e.IsDirectory).OrderBy(e => e.Key).ToList(); | ||
| Assert.Equal(2, files.Count); | ||
|
|
||
| // Read second file byte by byte | ||
| using (var entryStream = files[1].OpenEntryStream()) | ||
| { | ||
| var buffer = new byte[testset2.Length]; | ||
| for (var i = 0; i < buffer.Length; i++) | ||
| { | ||
| var b = entryStream.ReadByte(); | ||
| if (b == -1) | ||
| { | ||
| throw new InvalidOperationException($"Unexpected EOF at offset {i}"); | ||
| } | ||
|
|
||
| buffer[i] = (byte)b; | ||
| } | ||
| Assert.Equal(testset2, buffer); | ||
|
|
||
| // Verify EOF | ||
| Assert.Equal(-1, entryStream.ReadByte()); | ||
| } | ||
|
|
||
| // Read first file byte by byte using All pattern | ||
| using (var entryStream = files[0].OpenEntryStream()) | ||
| { | ||
| var match = | ||
| testset1.All(b => b == entryStream.ReadByte()) && entryStream.ReadByte() == -1; | ||
| Assert.True( | ||
| match, | ||
| "Decompressed file sample1 contents do not match the source file." | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Zip_Deflate_Roundtrip_ReaderApi_BufferedRead_Succeeds() | ||
| { | ||
| var (testset1, testset2, stream) = CreateTestZip(); | ||
| using (var reader = ReaderFactory.OpenReader(stream)) | ||
| { | ||
| // Read first entry | ||
| Assert.True(reader.MoveToNextEntry()); | ||
| Assert.Equal("sample1", reader.Entry.Key); | ||
| using (var entryStream = reader.OpenEntryStream()) | ||
| { | ||
| using var extracted = new MemoryStream(); | ||
| entryStream.CopyTo(extracted); | ||
| Assert.Equal(testset1, extracted.ToArray()); | ||
| } | ||
|
|
||
| // Read second entry | ||
| Assert.True(reader.MoveToNextEntry()); | ||
| Assert.Equal("sample2", reader.Entry.Key); | ||
| using (var entryStream = reader.OpenEntryStream()) | ||
| { | ||
| using var extracted = new MemoryStream(); | ||
| entryStream.CopyTo(extracted); | ||
| Assert.Equal(testset2, extracted.ToArray()); | ||
| } | ||
|
|
||
| // No more entries | ||
| Assert.False(reader.MoveToNextEntry()); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Zip_Deflate_Roundtrip_ReaderApi_ByteByByteRead_Succeeds() | ||
| { | ||
| var (testset1, testset2, stream) = CreateTestZip(); | ||
| using (var reader = ReaderFactory.OpenReader(stream)) | ||
| { | ||
| // Read first entry byte by byte | ||
| Assert.True(reader.MoveToNextEntry()); | ||
| Assert.Equal("sample1", reader.Entry.Key); | ||
| using (var entryStream = reader.OpenEntryStream()) | ||
| { | ||
| var buffer = new byte[testset1.Length]; | ||
| for (var i = 0; i < buffer.Length; i++) | ||
| { | ||
| var b = entryStream.ReadByte(); | ||
| if (b == -1) | ||
| { | ||
| throw new InvalidOperationException($"Unexpected EOF at offset {i}"); | ||
| } | ||
| buffer[i] = (byte)b; | ||
| } | ||
| Assert.Equal(testset1, buffer); | ||
| Assert.Equal(-1, entryStream.ReadByte()); | ||
| } | ||
|
|
||
| // Read second entry byte by byte | ||
| Assert.True(reader.MoveToNextEntry()); | ||
| Assert.Equal("sample2", reader.Entry.Key); | ||
| using (var entryStream = reader.OpenEntryStream()) | ||
| { | ||
| var buffer = new byte[testset2.Length]; | ||
| for (var i = 0; i < buffer.Length; i++) | ||
| { | ||
| var b = entryStream.ReadByte(); | ||
| if (b == -1) | ||
| { | ||
| throw new InvalidOperationException($"Unexpected EOF at offset {i}"); | ||
| } | ||
| buffer[i] = (byte)b; | ||
| } | ||
| Assert.Equal(testset2, buffer); | ||
| Assert.Equal(-1, entryStream.ReadByte()); | ||
| } | ||
|
|
||
| // No more entries | ||
| Assert.False(reader.MoveToNextEntry()); | ||
| } | ||
| } | ||
| } | ||
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.