Skip to content

Commit 1a51788

Browse files
authored
Zip async implementation follow-up: Fix failures in wasm and add extra tests (#114798)
* Remove `.Result` calls in async tests failing in wasm. * Uncomment the async tests for the latest bug fixes, make sure they count the Async write calls, not the sync ones, and add the rest of the async methods. * Revert skip on wasm platform * Modify NoSyncCallsStream to throw also on BeginRead, EndRead, BeginWrite and EndWrite. * Remove .Result from ZipTestHelper methods and propagate proper async usage. * spacing * Simplify object[] enumerables returning lists of ints.
1 parent 7e297e6 commit 1a51788

File tree

7 files changed

+476
-253
lines changed

7 files changed

+476
-253
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ internal sealed class NoSyncCallsStream : Stream
2424
public override long Position { get => _s.Position; set => _s.Position = value; }
2525
public override int ReadTimeout { get => _s.ReadTimeout; set => _s.ReadTimeout = value; }
2626
public override int WriteTimeout { get => _s.WriteTimeout; set => _s.WriteTimeout = value; }
27-
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginRead(buffer, offset, count, callback, state);
28-
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginWrite(buffer, offset, count, callback, state);
2927
public override void Close() => _s.Close();
30-
public override int EndRead(IAsyncResult asyncResult) => _s.EndRead(asyncResult);
31-
public override void EndWrite(IAsyncResult asyncResult) => _s.EndWrite(asyncResult);
3228
public override bool Equals(object? obj) => _s.Equals(obj);
3329
public override int GetHashCode() => _s.GetHashCode();
3430
public override int ReadByte() => _s.ReadByte();
@@ -37,6 +33,10 @@ internal sealed class NoSyncCallsStream : Stream
3733
public override string? ToString() => _s.ToString();
3834

3935
// Sync
36+
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
37+
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.BeginRead(buffer, offset, count, callback, state);
38+
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
39+
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.BeginRead(buffer, offset, count, callback, state);
4040
public override void CopyTo(Stream destination, int bufferSize)
4141
{
4242
if (IsRestrictionEnabled)
@@ -60,6 +60,18 @@ protected override void Dispose(bool disposing)
6060
_s.Dispose();
6161
}
6262
}
63+
public override int EndRead(IAsyncResult asyncResult) => IsRestrictionEnabled ? throw new InvalidOperationException() : _s.EndRead(asyncResult);
64+
public override void EndWrite(IAsyncResult asyncResult)
65+
{
66+
if (IsRestrictionEnabled)
67+
{
68+
throw new InvalidOperationException();
69+
}
70+
else
71+
{
72+
_s.EndWrite(asyncResult);
73+
}
74+
}
6375
public override void Flush()
6476
{
6577
if (IsRestrictionEnabled)

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,16 @@ private static string FlipSlashes(string name)
322322
public static FileStream CreateFileStreamRead(bool async, string fileName) =>
323323
new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: async);
324324

325-
public static void DirsEqual(string actual, string expected)
325+
public static async Task DirsEqual(string actual, string expected)
326326
{
327327
var expectedList = FileData.InPath(expected);
328328
var actualList = Directory.GetFiles(actual, "*.*", SearchOption.AllDirectories);
329329
var actualFolders = Directory.GetDirectories(actual, "*.*", SearchOption.AllDirectories);
330330
var actualCount = actualList.Length + actualFolders.Length;
331331
Assert.Equal(expectedList.Count, actualCount);
332332

333-
ItemEqual(actualList, expectedList, isFile: true);
334-
ItemEqual(actualFolders, expectedList, isFile: false);
333+
await ItemEqual(actualList, expectedList, isFile: true);
334+
await ItemEqual(actualFolders, expectedList, isFile: false);
335335
}
336336

337337
public static void DirFileNamesEqual(string actual, string expected)
@@ -341,7 +341,7 @@ public static void DirFileNamesEqual(string actual, string expected)
341341
AssertExtensions.SequenceEqual(expectedEntries.Select(Path.GetFileName).ToArray(), actualEntries.Select(Path.GetFileName).ToArray());
342342
}
343343

344-
private static void ItemEqual(string[] actualList, List<FileData> expectedList, bool isFile)
344+
private static async Task ItemEqual(string[] actualList, List<FileData> expectedList, bool isFile)
345345
{
346346
for (int i = 0; i < actualList.Length; i++)
347347
{
@@ -362,8 +362,8 @@ private static void ItemEqual(string[] actualList, List<FileData> expectedList,
362362
//contents same
363363
if (isFile)
364364
{
365-
Stream sa = StreamHelpers.CreateTempCopyStream(aEntry).Result;
366-
Stream sb = StreamHelpers.CreateTempCopyStream(bEntry).Result;
365+
Stream sa = await StreamHelpers.CreateTempCopyStream(aEntry);
366+
Stream sb = await StreamHelpers.CreateTempCopyStream(bEntry);
367367
StreamsEqual(sa, sb); // Not testing zip features, can always be async
368368
}
369369
}
@@ -597,9 +597,9 @@ public static IEnumerable<object[]> Utf8Comment_Data()
597597
}
598598

599599
foreach (object[] e in SharedComment_Data())
600-
{
601-
yield return e;
602-
}
600+
{
601+
yield return e;
602+
}
603603
}
604604

605605
// Returns pairs as expected by Latin1
@@ -617,9 +617,9 @@ public static IEnumerable<object[]> Latin1Comment_Data()
617617
}
618618

619619
foreach (object[] e in SharedComment_Data())
620-
{
621-
yield return e;
622-
}
620+
{
621+
yield return e;
622+
}
623623
}
624624

625625
// Returns pairs encoded with Latin1, but decoded with UTF8.

src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extract.Stream.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace System.IO.Compression.Tests;
1010

11-
[SkipOnPlatform(TestPlatforms.Browser, "https://github.com/dotnet/runtime/issues/114769")]
1211
public class ZipFile_Extract_Stream : ZipFileTestBase
1312
{
1413
[Fact]
@@ -47,7 +46,7 @@ public async Task ExtractToDirectoryNormal(string file, string folder, bool asyn
4746
string folderName = zfolder(folder);
4847
using TempDirectory tempFolder = new(GetTestFilePath());
4948
await CallZipFileExtractToDirectory(async, source, tempFolder.Path);
50-
DirsEqual(tempFolder.Path, folderName);
49+
await DirsEqual(tempFolder.Path, folderName);
5150
await DisposeStream(async, source);
5251
}
5352

@@ -74,7 +73,7 @@ public async Task ExtractToDirectoryNormal_Unwritable_Unseekable(string file, st
7473
string folderName = zfolder(folder);
7574
using TempDirectory tempFolder = new(GetTestFilePath());
7675
await CallZipFileExtractToDirectory(async, source, tempFolder.Path);
77-
DirsEqual(tempFolder.Path, folderName);
76+
await DirsEqual(tempFolder.Path, folderName);
7877
await DisposeStream(async, fs);
7978
}
8079

@@ -200,7 +199,7 @@ public async Task ExtractToDirectoryOverwrite(bool async)
200199
source.Position = 0;
201200
await CallZipFileExtractToDirectory(async, source, tempFolder.Path, overwriteFiles: true);
202201

203-
DirsEqual(tempFolder.Path, folderName);
202+
await DirsEqual(tempFolder.Path, folderName);
204203

205204
await DisposeStream(async, source);
206205
}
@@ -222,7 +221,7 @@ public async Task ExtractToDirectoryOverwriteEncoding(bool async)
222221
source.Position = 0;
223222
await CallZipFileExtractToDirectory(async, source, tempFolder.Path, Encoding.UTF8, overwriteFiles: true);
224223

225-
DirsEqual(tempFolder.Path, folderName);
224+
await DirsEqual(tempFolder.Path, folderName);
226225
}
227226

228227
[Theory]

src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extract.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace System.IO.Compression.Tests
1010
{
11-
[SkipOnPlatform(TestPlatforms.Browser, "https://github.com/dotnet/runtime/issues/114769")]
1211
public class ZipFile_Extract : ZipFileTestBase
1312
{
1413
public static IEnumerable<object[]> Get_ExtractToDirectoryNormal_Data()
@@ -33,7 +32,7 @@ public async Task ExtractToDirectoryNormal(string file, string folder, bool asyn
3332
string folderName = zfolder(folder);
3433
using TempDirectory tempFolder = new TempDirectory(GetTestFilePath());
3534
await CallZipFileExtractToDirectory(async, zipFileName, tempFolder.Path);
36-
DirsEqual(tempFolder.Path, folderName);
35+
await DirsEqual(tempFolder.Path, folderName);
3736
}
3837

3938
[Theory]
@@ -157,7 +156,7 @@ public async Task ExtractToDirectoryOverwrite(bool async)
157156
await Assert.ThrowsAsync<IOException>(() => CallZipFileExtractToDirectory(async, zipFileName, tempFolder.Path, overwriteFiles: false));
158157
await CallZipFileExtractToDirectory(async, zipFileName, tempFolder.Path, overwriteFiles: true);
159158

160-
DirsEqual(tempFolder.Path, folderName);
159+
await DirsEqual(tempFolder.Path, folderName);
161160
}
162161

163162
[Theory]
@@ -174,7 +173,7 @@ public async Task ExtractToDirectoryOverwriteEncoding(bool async)
174173
await Assert.ThrowsAsync<IOException>(() => CallZipFileExtractToDirectory(async, zipFileName, tempFolder.Path, Encoding.UTF8, overwriteFiles: false));
175174
await CallZipFileExtractToDirectory(async, zipFileName, tempFolder.Path, Encoding.UTF8, overwriteFiles: true);
176175

177-
DirsEqual(tempFolder.Path, folderName);
176+
await DirsEqual(tempFolder.Path, folderName);
178177
}
179178

180179
[Theory]

src/libraries/System.IO.Compression.ZipFile/tests/ZipFileExtensions.ZipArchive.Extract.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public async Task ExtractToDirectoryExtension(bool async)
1919
await Assert.ThrowsAsync<ArgumentNullException>(() => CallZipFileExtensionsExtractToDirectory(async, archive, null));
2020
await CallZipFileExtensionsExtractToDirectory(async, archive, tempFolder);
2121

22-
DirsEqual(tempFolder, zfolder("normal"));
22+
await DirsEqual(tempFolder, zfolder("normal"));
2323

2424
await DisposeZipArchive(async, archive);
2525
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,9 @@ public static async Task ZipArchiveEntry_CorruptedStream_ReadMode_Read_UpToUncom
281281

282282
[Theory]
283283
[MemberData(nameof(Get_Booleans_Data))]
284-
[SkipOnPlatform(TestPlatforms.Browser, "https://github.com/dotnet/runtime/issues/114769")]
285284
public static async Task ZipArchiveEntry_CorruptedStream_EnsureNoExtraBytesReadOrOverWritten(bool async)
286285
{
287-
MemoryStream stream = PopulateStream().Result;
286+
MemoryStream stream = await LocalMemoryStream.ReadAppFileAsync(zfile("normal.zip"));
288287

289288
int nameOffset = PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 8); // patch uncompressed size in file header
290289
PatchDataRelativeToFileName(Encoding.ASCII.GetBytes(s_tamperedFileName), stream, 22, nameOffset + s_tamperedFileName.Length); // patch in central directory too
@@ -315,11 +314,6 @@ public static async Task ZipArchiveEntry_CorruptedStream_EnsureNoExtraBytesReadO
315314
await DisposeZipArchive(async, archive);
316315
}
317316

318-
private static async Task<MemoryStream> PopulateStream()
319-
{
320-
return await LocalMemoryStream.ReadAppFileAsync(zfile("normal.zip"));
321-
}
322-
323317
[Theory]
324318
[MemberData(nameof(Get_Booleans_Data))]
325319
public static async Task Zip64ArchiveEntry_CorruptedStream_CopyTo_UpToUncompressedSize(bool async)

0 commit comments

Comments
 (0)