From 9e5b6f4adb53fdb03053bc8ba1d57e763996b8b5 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 1 Dec 2021 17:05:19 +0100 Subject: [PATCH 1/2] add test that ensures that it's possible to zip a named pipe on Unix --- .../tests/ZipFile.Unix.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs index 0d349b5fce8487..d20e288d50b7a8 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs +++ b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs @@ -1,7 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Linq; +using System.Runtime.InteropServices; using System.Text; +using System.Threading.Tasks; using Xunit; namespace System.IO.Compression.Tests @@ -136,6 +139,41 @@ public void UnixExtractFilePermissionsCompat(string zipName, string expectedPerm } } + [Fact] + public async Task CanZipNamedPipe() + { + string destPath = Path.Combine(TestDirectory, "dest.zip"); + + string subFolderPath = Path.Combine(TestDirectory, "subfolder"); + string fifoPath = Path.Combine(subFolderPath, "namedPipe"); + Directory.CreateDirectory(subFolderPath); // mandatory before calling mkfifo + Assert.Equal(0, mkfifo(fifoPath, 438 /* 666 in octal */)); + + byte[] contentBytes = { 1, 2, 3, 4, 5 }; + + await Task.WhenAll( + Task.Run(() => + { + using FileStream fs = new (fifoPath, FileMode.Open, FileAccess.Write, FileShare.Read); + foreach (byte content in contentBytes) + { + fs.WriteByte(content); + } + }), + Task.Run(() => + { + ZipFile.CreateFromDirectory(subFolderPath, destPath); + + using ZipArchive zippedFolder = ZipFile.OpenRead(destPath); + using Stream unzippedPipe = zippedFolder.Entries.Single().Open(); + + byte[] readBytes = new byte[contentBytes.Length]; + Assert.Equal(contentBytes.Length, unzippedPipe.Read(readBytes)); + Assert.Equal(contentBytes, readBytes); + Assert.Equal(0, unzippedPipe.Read(readBytes)); // EOF + })); + } + private static string GetExpectedPermissions(string expectedPermissions) { if (string.IsNullOrEmpty(expectedPermissions)) @@ -156,5 +194,9 @@ private static string GetExpectedPermissions(string expectedPermissions) return expectedPermissions; } + + + [DllImport("libc", SetLastError = true)] + private static extern int mkfifo(string path, int mode); } } From fa1f01418378366d98d4708cd61c39fd93b21532 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 1 Dec 2021 17:35:44 +0100 Subject: [PATCH 2/2] don't run the test on WASM as it does not support multiple threads --- .../System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs index d20e288d50b7a8..2cc10837f5ffc6 100644 --- a/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs +++ b/src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Unix.cs @@ -140,6 +140,7 @@ public void UnixExtractFilePermissionsCompat(string zipName, string expectedPerm } [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] public async Task CanZipNamedPipe() { string destPath = Path.Combine(TestDirectory, "dest.zip"); @@ -195,7 +196,6 @@ private static string GetExpectedPermissions(string expectedPermissions) return expectedPermissions; } - [DllImport("libc", SetLastError = true)] private static extern int mkfifo(string path, int mode); }