Skip to content

Commit

Permalink
test: Try to fix test for windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 8, 2023
1 parent bc692c1 commit 2e16710
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/libs/H.Pipes.AccessControl/PipeServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public static void SetPipeSecurity<T>(this IPipeServer<T> server, PipeSecurity p
server = server ?? throw new ArgumentNullException(nameof(server));
pipeSecurity = pipeSecurity ?? throw new ArgumentNullException(nameof(pipeSecurity));

server.CreatePipeStreamFunc = pipeName => NamedPipeServerStreamConstructors.New(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0, pipeSecurity);
server.CreatePipeStreamFunc = pipeName => NamedPipeServerStreamConstructors.New(
pipeName: pipeName,
direction: PipeDirection.InOut,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0,
pipeSecurity: pipeSecurity);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/libs/H.Pipes/PipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ await handshakeWrapper.WriteAsync(Encoding.UTF8.GetBytes(connectionPipeName), to
}
// Wait for the client to connect to the data pipe
var connectionStream = (CreatePipeStreamForConnectionFunc ?? CreatePipeStreamFunc)?.Invoke(connectionPipeName) ?? PipeServerFactory.Create(connectionPipeName);
var connectionStream = (CreatePipeStreamForConnectionFunc ?? CreatePipeStreamFunc)?
.Invoke(connectionPipeName) ?? PipeServerFactory.Create(connectionPipeName);
PipeStreamInitializeAction?.Invoke(connectionStream);
Expand Down
39 changes: 33 additions & 6 deletions src/tests/H.Pipes.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Concurrent;
using System.IO.Pipes;
using System.Net;
using System.Security.AccessControl;
using System.Security.Principal;

namespace H.Pipes.Tests;

Expand Down Expand Up @@ -141,12 +143,37 @@ public async Task WriteOnlyServer()
Console.WriteLine($"PipeName: {pipeName}");

await using var server = new PipeServer<byte[]>(pipeName);
server.CreatePipeStreamFunc = static pipeName => new NamedPipeServerStream(
pipeName,
PipeDirection.Out,
1,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous | PipeOptions.WriteThrough);
if (OperatingSystem.IsWindows())
{
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
PipeAccessRights.ReadWrite,
AccessControlType.Allow));

#pragma warning disable CA1416
server.CreatePipeStreamFunc = name => NamedPipeServerStreamAcl.Create(
pipeName: name,
direction: PipeDirection.Out,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0,
pipeSecurity: pipeSecurity);
#pragma warning restore CA1416
}
else
{
server.CreatePipeStreamFunc = static pipeName => new NamedPipeServerStream(
pipeName: pipeName,
direction: PipeDirection.Out,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0);
}

server.ClientConnected += async (_, args) =>
{
Expand Down

0 comments on commit 2e16710

Please sign in to comment.