Repository: dotnet/runtime
File: src/libraries/System.IO.Compression/tests/ZipArchive/zip_UpdateTests.cs (C#)

Code before:
        [MemberData(nameof(Get_Booleans_Data))]
        public async Task Update_OpenEntryWithoutWriting_DoesNotTriggerRewrite(bool async)
        {
            // Create a valid zip file
            byte[] sampleEntryContents = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
            byte[] sampleZipFile = await CreateZipFile(3, sampleEntryContents, async);
            long originalLength = sampleZipFile.Length;

            // Use a non-expandable MemoryStream (fixed buffer)
            // This would throw NotSupportedException if Dispose tries to write/grow the stream
            using (MemoryStream ms = new MemoryStream(sampleZipFile, writable: true))
            {
                ZipArchive archive = async
                    ? await ZipArchive.CreateAsync(ms, ZipArchiveMode.Update, leaveOpen: true, entryNameEncoding: null)
                    : new ZipArchive(ms, ZipArchiveMode.Update, leaveOpen: true);

                // Open an entry and read it without writing
                ZipArchiveEntry entry = archive.Entries[0];
                Stream entryStream = async ? await entry.OpenAsync() : entry.Open();
                byte[] buffer = new byte[sampleEntryContents.Length + 1]; // +1 for the index byte added by CreateZipFile
                int bytesRead = async
                    ? await entryStream.ReadAsync(buffer)
                    : entryStream.Read(buffer, 0, buffer.Length);

                // Close the entry stream without writing anything
                if (async)
                    await entryStream.DisposeAsync();
                else
                    entryStream.Dispose();

                // Dispose should not throw NotSupportedException because no writes occurred
                // and the archive should not try to grow the stream
                if (async)
                    await archive.DisposeAsync();
                else
                    archive.Dispose();

                // Verify the stream was not modified
                Assert.Equal(originalLength, ms.Length);
            }
        }

        /// <summary>
        /// Regression test for the exact scenario from the bug report https://github.com/dotnet/runtime/issues/123419
        /// Opening a Package-like zip archive (with [Content_Types].xml) on a non-expandable MemoryStream, disposing
        /// without modifications, should not throw NotSupportedException. This mimics Package.Open behavior where simply opening
        /// and closing a package without changes should not attempt to rewrite the archive.
        /// </summary>
        [Fact]
        public void Update_PackageLikeArchive_DisposeWithoutChanges_DoesNotThrow()
        {

Review comment (bug):
`bytesRead` is assigned but never used. If warnings are treated as errors in this test project, this will break the build. Either remove the variable or assert on it (e.g., that some bytes were read) so the read has a checked outcome.
```suggestion
                    : entryStream.Read(buffer, 0, buffer.Length);
                Assert.InRange(bytesRead, 1, buffer.Length);
```

Code after:
        public async Task Update_OpenEntryWithoutWriting_DoesNotTriggerRewrite(bool async)
        {
            // Create a valid zip file
            byte[] sampleEntryContents = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
            byte[] sampleZipFile = await CreateZipFile(3, sampleEntryContents, async);
            long originalLength = sampleZipFile.Length;

            // Keep a copy of the original contents to verify no in-place rewrite occurred
            byte[] originalContents = new byte[sampleZipFile.Length];
            Array.Copy(sampleZipFile, originalContents, sampleZipFile.Length);

            // Use a non-expandable MemoryStream (fixed buffer)
            // This would throw NotSupportedException if Dispose tries to write/grow the stream
            using (MemoryStream ms = new MemoryStream(sampleZipFile, writable: true))
            {
                ZipArchive archive = async
                    ? await ZipArchive.CreateAsync(ms, ZipArchiveMode.Update, leaveOpen: true, entryNameEncoding: null)
                    : new ZipArchive(ms, ZipArchiveMode.Update, leaveOpen: true);

                // Open an entry and read it without writing
                ZipArchiveEntry entry = archive.Entries[0];
                Stream entryStream = async ? await entry.OpenAsync() : entry.Open();
                byte[] buffer = new byte[sampleEntryContents.Length + 1]; // +1 for the index byte added by CreateZipFile
                int bytesRead = async
                    ? await entryStream.ReadAsync(buffer)
                    : entryStream.Read(buffer, 0, buffer.Length);
                Assert.InRange(bytesRead, 1, buffer.Length);

                // Close the entry stream without writing anything
                if (async)
                    await entryStream.DisposeAsync();
                else
                    entryStream.Dispose();

                // Dispose should not throw NotSupportedException because no writes occurred
                // and the archive should not try to rewrite the stream
                if (async)
                    await archive.DisposeAsync();
                else
                    archive.Dispose();

                // Verify the stream was not modified - neither length nor contents
                Assert.Equal(originalLength, ms.Length);
                Assert.Equal(originalContents, sampleZipFile);
            }
        }

    }
}