PowerShell/test/xUnit/csharp/test_NamedPipe.cs
Tyler James Leonhardt 23eccfd641 [feature] Add -CustomPipeName to pwsh and Enter-PSHostProcess (#8889)
This allows a user to start PowerShell up with the name of the named pipe that is used for cross process communication (I.e. Enter-PSHostProcess).
2019-02-22 23:40:03 +05:00

58 lines
2 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using Xunit;
namespace PSTests.Parallel
{
public class NamedPipeTests
{
[Fact]
public void TestCustomPipeNameCreation()
{
string pipeNameForFirstCall = Path.GetRandomFileName();
string pipeNameForSecondCall = Path.GetRandomFileName();
RemoteSessionNamedPipeServer.CreateCustomNamedPipeServer(pipeNameForFirstCall);
Assert.True(File.Exists(GetPipePath(pipeNameForFirstCall)));
// The second call to this method would override the first named pipe.
RemoteSessionNamedPipeServer.CreateCustomNamedPipeServer(pipeNameForSecondCall);
Assert.True(File.Exists(GetPipePath(pipeNameForSecondCall)));
// Previous pipe should have been cleaned up.
Assert.False(File.Exists(GetPipePath(pipeNameForFirstCall)));
}
[Fact]
public void TestCustomPipeNameCreationTooLongOnNonWindows()
{
var longPipeName = "DoggoipsumwaggywagssmolborkingdoggowithalongsnootforpatsdoingmeafrightenporgoYapperporgolongwatershoobcloudsbigolpupperlengthboy";
if (!Platform.IsWindows)
{
Assert.Throws<InvalidOperationException>(() =>
RemoteSessionNamedPipeServer.CreateCustomNamedPipeServer(longPipeName));
}
else
{
RemoteSessionNamedPipeServer.CreateCustomNamedPipeServer(longPipeName);
Assert.True(File.Exists(GetPipePath(longPipeName)));
}
}
private static string GetPipePath(string pipeName)
{
if (Platform.IsWindows)
{
return $@"\\.\pipe\{pipeName}";
}
return $@"{Path.GetTempPath()}CoreFxPipe_{pipeName}";
}
}
}