File size: 2,007 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) Microsoft Corporation.
// 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()
        {
            const string 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}";
        }
    }
}