From 83b11254db4744171cded2d73d2206df5edbc042 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 4 Jan 2026 09:33:31 +0000 Subject: [PATCH] Add test for InvalidOperationException fix in RAR extraction Added Rar_ExtractionCompletesWithoutInvalidOperationException test that verifies RAR extraction completes successfully without throwing InvalidOperationException when reading streams to EOF. The test validates the fix works across RAR, RAR5, RAR4, and RAR2 formats by reading all entries completely and ensuring no exceptions are thrown. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> --- .../SharpCompress.Test/Rar/RarArchiveTests.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs index 991f18e5..dea60f7e 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -642,4 +642,42 @@ public class RarArchiveTests : ArchiveTests ); Assert.True(passwordProtectedFilesArchive.IsEncrypted); } + + /// + /// Test for issue: InvalidOperationException when extracting RAR files. + /// This test verifies that RAR extraction completes successfully without throwing + /// InvalidOperationException when the actual decompressed size may differ from + /// the header-specified size. The fix changed the validation from (_position != Length) + /// to (_position < Length) to only catch premature stream termination, not oversized decompression. + /// + [Fact] + public void Rar_ExtractionCompletesWithoutInvalidOperationException() + { + // Test multiple RAR formats to ensure the fix works across all versions + var testFiles = new[] { "Rar.rar", "Rar5.rar", "Rar4.rar", "Rar2.rar" }; + + foreach (var testFile in testFiles) + { + using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testFile)); + using var archive = RarArchive.Open(stream); + + // Extract all entries and read them completely + // This ensures we read to the end of each entry stream without throwing + foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) + { + using var entryStream = entry.OpenEntryStream(); + using var ms = new MemoryStream(); + + // Read the entire stream - this is where the exception would occur + // if the validation was too strict (_position != Length instead of _position < Length) + entryStream.CopyTo(ms); + + // Verify we read some data + Assert.True( + ms.Length > 0, + $"Failed to extract data from {entry.Key} in {testFile}" + ); + } + } + } }