Skip to content

Commit e4746f3

Browse files
authored
Add test that ensures that it's possible to zip a named pipe on Unix (#62240)
1 parent ab9ee76 commit e4746f3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Linq;
5+
using System.Runtime.InteropServices;
46
using System.Text;
7+
using System.Threading.Tasks;
58
using Xunit;
69

710
namespace System.IO.Compression.Tests
@@ -136,6 +139,42 @@ public void UnixExtractFilePermissionsCompat(string zipName, string expectedPerm
136139
}
137140
}
138141

142+
[Fact]
143+
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
144+
public async Task CanZipNamedPipe()
145+
{
146+
string destPath = Path.Combine(TestDirectory, "dest.zip");
147+
148+
string subFolderPath = Path.Combine(TestDirectory, "subfolder");
149+
string fifoPath = Path.Combine(subFolderPath, "namedPipe");
150+
Directory.CreateDirectory(subFolderPath); // mandatory before calling mkfifo
151+
Assert.Equal(0, mkfifo(fifoPath, 438 /* 666 in octal */));
152+
153+
byte[] contentBytes = { 1, 2, 3, 4, 5 };
154+
155+
await Task.WhenAll(
156+
Task.Run(() =>
157+
{
158+
using FileStream fs = new (fifoPath, FileMode.Open, FileAccess.Write, FileShare.Read);
159+
foreach (byte content in contentBytes)
160+
{
161+
fs.WriteByte(content);
162+
}
163+
}),
164+
Task.Run(() =>
165+
{
166+
ZipFile.CreateFromDirectory(subFolderPath, destPath);
167+
168+
using ZipArchive zippedFolder = ZipFile.OpenRead(destPath);
169+
using Stream unzippedPipe = zippedFolder.Entries.Single().Open();
170+
171+
byte[] readBytes = new byte[contentBytes.Length];
172+
Assert.Equal(contentBytes.Length, unzippedPipe.Read(readBytes));
173+
Assert.Equal<byte>(contentBytes, readBytes);
174+
Assert.Equal(0, unzippedPipe.Read(readBytes)); // EOF
175+
}));
176+
}
177+
139178
private static string GetExpectedPermissions(string expectedPermissions)
140179
{
141180
if (string.IsNullOrEmpty(expectedPermissions))
@@ -156,5 +195,8 @@ private static string GetExpectedPermissions(string expectedPermissions)
156195

157196
return expectedPermissions;
158197
}
198+
199+
[DllImport("libc", SetLastError = true)]
200+
private static extern int mkfifo(string path, int mode);
159201
}
160202
}

0 commit comments

Comments
 (0)