[PlatformSpecific(TestPlatforms.Windows)] // Win32 P/Invokes to verify the user name
        public async Task Windows_GetImpersonationUserName_Succeed(TokenImpersonationLevel level, bool expectedResult)
        {
            string pipeName = PipeStreamConformanceTests.GetUniquePipeName();

            using (var server = new NamedPipeServerStream(pipeName))
            {
                using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None, level))
                {
                    string expectedUserName;
                    Task   serverTask = server.WaitForConnectionAsync();

                    client.Connect();
                    await serverTask;

                    Assert.Equal(expectedResult, InteropTest.TryGetImpersonationUserName(server.SafePipeHandle, out expectedUserName));

                    if (!expectedResult)
                    {
                        Assert.Equal(string.Empty, expectedUserName);
                        Assert.Throws <IOException>(() => server.GetImpersonationUserName());
                    }
                    else
                    {
                        string actualUserName = server.GetImpersonationUserName();
                        Assert.NotNull(actualUserName);
                        Assert.False(string.IsNullOrWhiteSpace(actualUserName));
                        Assert.Equal(expectedUserName, actualUserName);
                    }
                }
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeClientStream client = pair.clientStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();
                if (client.CanRead)
                {
                    var  cts             = new CancellationTokenSource();
                    Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);
                }
                if (client.CanWrite)
                {
                    var  cts = new CancellationTokenSource();
                    Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);
                }
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            if ((Options & PipeOptions.Asynchronous) == 0)
            {
                // Test depends on PipeOptions.Asynchronous, as CancelIoEx is for overlapped I/O
                return;
            }

            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (client.CanRead)
            {
                var  cts             = new CancellationTokenSource();
                Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);
            }

            if (client.CanWrite)
            {
                var  cts = new CancellationTokenSource();
                Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_Server_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();

                if (server.CanRead)
                {
                    Task serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None);

                    Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);

                    Assert.True(serverReadToken.IsCanceled);
                }
                if (server.CanWrite)
                {
                    Task serverWriteToken = server.WriteAsync(buffer, 0, buffer.Length, CancellationToken.None);

                    Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);

                    Assert.True(serverWriteToken.IsCanceled);
                }
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (client.CanRead)
            {
                Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);

                Assert.True(clientReadToken.IsCanceled);
            }

            if (client.CanWrite)
            {
                Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);

                Assert.True(clientWriteToken.IsCanceled);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Server_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (server.CanRead)
            {
                var  cts             = new CancellationTokenSource();
                Task serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);
            }
            if (server.CanWrite)
            {
                var  cts = new CancellationTokenSource();
                Task serverWriteToken = server.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            var  cts = new CancellationTokenSource();
            Task waitForConnectionTask = server.WaitForConnectionAsync(cts.Token);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                var cts = new CancellationTokenSource();
                NamedPipeServerStream server = pair.serverStream;
                Task waitForConnectionTask   = server.WaitForConnectionAsync(cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException()
        {
            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            Task waitForConnectionTask = server.WaitForConnectionAsync(CancellationToken.None);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => waitForConnectionTask);

            Assert.True(waitForConnectionTask.IsCanceled);
        }
Beispiel #10
0
        [PlatformSpecific(TestPlatforms.AnyUnix)]  // Not supported pipe path throws PNSE on Unix
        public static void NotSupportedPipePath_Throws_PlatformNotSupportedException()
        {
            string hostName;

            Assert.True(InteropTest.TryGetHostName(out hostName));

            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream("foobar" + hostName, "foobar"));
            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream(hostName, "foobar" + Path.GetInvalidFileNameChars()[0]));
            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream(hostName, "/tmp/foo\0bar"));
            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream(hostName, "/tmp/foobar/"));
            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream(hostName, "/"));
            Assert.Throws <PlatformNotSupportedException>(() => new NamedPipeClientStream(hostName, "\0"));
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                Task waitForConnectionTask   = server.WaitForConnectionAsync(CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => waitForConnectionTask);

                Assert.True(waitForConnectionTask.IsCanceled);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            if ((Options & PipeOptions.Asynchronous) == 0)
            {
                // Test depends on PipeOptions.Asynchronous, as CancelIoEx is for overlapped I/O
                return;
            }

            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            var  cts = new CancellationTokenSource();
            Task waitForConnectionTask = server.WaitForConnectionAsync(cts.Token);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
        }
        [PlatformSpecific(TestPlatforms.Windows)] // Unix doesn't support MaxNumberOfServerInstances
        public async Task Windows_Get_NumberOfServerInstances_Succeed()
        {
            string pipeName = PipeStreamConformanceTests.GetUniquePipeName();

            using (var server = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 3))
            {
                using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
                {
                    Task serverTask = server.WaitForConnectionAsync();

                    client.Connect();
                    await serverTask;

                    Assert.True(InteropTest.TryGetNumberOfServerInstances(client.SafePipeHandle, out uint expectedNumberOfServerInstances), "GetNamedPipeHandleState failed");
                    Assert.Equal(expectedNumberOfServerInstances, (uint)client.NumberOfServerInstances);
                }
            }
        }