Skip to content

Commit 2634862

Browse files
Copilotrzikm
andcommitted
Remove new test and fix existing NoSyncCallsWhenUsingAsync test per review feedback
Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
1 parent 5945f08 commit 2634862

File tree

3 files changed

+5
-131
lines changed

3 files changed

+5
-131
lines changed

src/libraries/Common/tests/System/IO/Compression/NoSyncCallsStream.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,9 @@ public override int Read(Span<byte> buffer) =>
8989
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.Read(buffer);
9090
public override void Write(byte[] buffer, int offset, int count)
9191
{
92-
bool isDeflateStream = false;
93-
94-
// Get the stack trace to determine the calling method
95-
var stackTrace = new System.Diagnostics.StackTrace();
96-
var callingMethod = stackTrace.GetFrame(1)?.GetMethod();
97-
98-
// Check if the calling method belongs to the DeflateStream class
99-
if (callingMethod?.DeclaringType == typeof(System.IO.Compression.DeflateStream))
100-
{
101-
isDeflateStream = true;
102-
}
103-
104-
if (!isDeflateStream && IsRestrictionEnabled)
92+
if (IsRestrictionEnabled)
10593
{
106-
throw new InvalidOperationException($"Parent class is {callingMethod.DeclaringType}");
94+
throw new InvalidOperationException();
10795
}
10896
else
10997
{

src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ public static async Task NoAsyncCallsWhenUsingSync()
15721572
public static async Task NoSyncCallsWhenUsingAsync()
15731573
{
15741574
using MemoryStream ms = new();
1575-
using NoSyncCallsStream s = new(ms); // Only allows async calls
1575+
using NoSyncCallsStream s = new(ms) { IsRestrictionEnabled = true }; // Only allows async calls
15761576

15771577
// Create mode
15781578
await using (ZipArchive archive = await ZipArchive.CreateAsync(s, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: Encoding.UTF8))
@@ -1593,8 +1593,8 @@ public static async Task NoSyncCallsWhenUsingAsync()
15931593
// Note the parent archive is not using NoSyncCallsStream, so it can be opened in sync mode
15941594
using (Stream normalEntryStream = normalEntry.Open())
15951595
{
1596-
// Note the parent archive is not using NoSyncCallsStream, so it can be copied in sync mode
1597-
normalEntryStream.CopyTo(newEntryStream);
1596+
// Use async CopyTo when writing to NoSyncCallsStream
1597+
await normalEntryStream.CopyToAsync(newEntryStream);
15981598
}
15991599
}
16001600
}

src/libraries/System.IO.Compression/tests/ZipArchive/zip_netcoreappTests.cs

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.InteropServices;
5-
using System.Threading;
65
using System.Threading.Tasks;
76
using Xunit;
87

@@ -52,118 +51,5 @@ public static async Task RoundTrips_UnixFilePermissions(int expectedAttr)
5251
}
5352
}
5453
}
55-
56-
[Fact]
57-
public static async Task AsyncOnlyStream_NoSynchronousCalls()
58-
{
59-
// This test verifies that async Zip methods don't make synchronous calls
60-
// which would fail with async-only streams (e.g., Kestrel response streams)
61-
var innerStream = new MemoryStream();
62-
var asyncOnlyStream = new AsyncOnlyStream(innerStream);
63-
byte[] testData = new byte[1024];
64-
Random.Shared.NextBytes(testData);
65-
66-
await using (var zipArchive = new ZipArchive(asyncOnlyStream, ZipArchiveMode.Create, leaveOpen: true))
67-
{
68-
var entry = zipArchive.CreateEntry("TestEntry");
69-
await using (var entryStream = await entry.OpenAsync())
70-
{
71-
await entryStream.WriteAsync(testData);
72-
await entryStream.FlushAsync();
73-
}
74-
}
75-
76-
// Verify the archive was created successfully by reading from the inner stream
77-
innerStream.Position = 0;
78-
using (var zipArchive = new ZipArchive(innerStream, ZipArchiveMode.Read))
79-
{
80-
Assert.Single(zipArchive.Entries);
81-
var entry = zipArchive.Entries[0];
82-
Assert.Equal("TestEntry", entry.Name);
83-
Assert.Equal(testData.Length, entry.Length);
84-
85-
using (var entryStream = entry.Open())
86-
{
87-
byte[] readData = new byte[testData.Length];
88-
int bytesRead = entryStream.Read(readData);
89-
Assert.Equal(testData.Length, bytesRead);
90-
Assert.Equal(testData, readData);
91-
}
92-
}
93-
}
94-
95-
private sealed class AsyncOnlyStream : Stream
96-
{
97-
private readonly MemoryStream _innerStream;
98-
99-
public AsyncOnlyStream(MemoryStream innerStream)
100-
{
101-
_innerStream = innerStream;
102-
}
103-
104-
public override void Flush()
105-
{
106-
throw new NotSupportedException("Synchronous operations not supported");
107-
}
108-
109-
public override int Read(byte[] buffer, int offset, int count)
110-
{
111-
throw new NotSupportedException("Synchronous operations not supported");
112-
}
113-
114-
public override long Seek(long offset, SeekOrigin origin)
115-
{
116-
return _innerStream.Seek(offset, origin);
117-
}
118-
119-
public override void SetLength(long value)
120-
{
121-
_innerStream.SetLength(value);
122-
}
123-
124-
public override void Write(byte[] buffer, int offset, int count)
125-
{
126-
throw new NotSupportedException("Synchronous operations not supported");
127-
}
128-
129-
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
130-
{
131-
return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken);
132-
}
133-
134-
public override Task FlushAsync(CancellationToken cancellationToken)
135-
{
136-
return _innerStream.FlushAsync(cancellationToken);
137-
}
138-
139-
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
140-
{
141-
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
142-
}
143-
144-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
145-
{
146-
return _innerStream.WriteAsync(buffer, cancellationToken);
147-
}
148-
149-
public override ValueTask DisposeAsync()
150-
{
151-
return _innerStream.DisposeAsync();
152-
}
153-
154-
public override bool CanRead => _innerStream.CanRead;
155-
156-
public override bool CanSeek => _innerStream.CanSeek;
157-
158-
public override bool CanWrite => _innerStream.CanWrite;
159-
160-
public override long Length => _innerStream.Length;
161-
162-
public override long Position
163-
{
164-
get => _innerStream.Position;
165-
set => _innerStream.Position = value;
166-
}
167-
}
16854
}
16955
}

0 commit comments

Comments
 (0)