Exemplo n.º 1
0
        public async Task 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 ctx1 = new CancellationTokenSource();

                    Task serverReadToken = client.ReadAsync(buffer, 0, buffer.Length, ctx1.Token);
                    ctx1.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);

                    Assert.True(client.ReadAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }

                if (client.CanWrite)
                {
                    var ctx1 = new CancellationTokenSource();
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // On Unix WriteAsync's aren't cancelable once initiated
                    {
                        Task serverWriteToken = client.WriteAsync(buffer, 0, buffer.Length, ctx1.Token);
                        ctx1.Cancel();
                        await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);
                    }
                    ctx1.Cancel();
                    Assert.True(client.WriteAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }
            }
        }
Exemplo n.º 2
0
        public override async Task OperationsOnDisconnectedClient()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                await base.OperationsOnDisconnectedClient();
            }
            else
            {
                using (NamedPipePair pair = CreateNamedPipePair())
                {
                    NamedPipeServerStream server = pair.serverStream;
                    NamedPipeClientStream client = pair.clientStream;
                    pair.Connect();

                    Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
                    Assert.Throws <InvalidOperationException>(() => client.Connect());
                    await Assert.ThrowsAsync <InvalidOperationException>(() => client.ConnectAsync());

                    server.Disconnect();

                    byte[] buffer = new byte[] { 0, 0, 0, 0 };

                    // The pipe is broken, but Unix InOut will allow the writes regardless
                    client.Write(buffer, 0, buffer.Length);
                    client.WriteByte(5);
                    await client.WriteAsync(buffer, 0, buffer.Length);

                    client.Flush();
                    Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
                    Assert.Throws <PlatformNotSupportedException>(() => client.NumberOfServerInstances);
                }
            }
        }
Exemplo n.º 3
0
        public virtual async Task OperationsOnDisconnectedClient()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                NamedPipeClientStream client = pair.clientStream;
                pair.Connect();

                Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
                Assert.Throws <InvalidOperationException>(() => client.Connect());
                await Assert.ThrowsAsync <InvalidOperationException>(() => client.ConnectAsync());

                server.Disconnect();

                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                if (!pair.writeToServer)
                {
                    // Pipe is broken
                    Assert.Throws <IOException>(() => client.Write(buffer, 0, buffer.Length));
                    Assert.Throws <IOException>(() => client.WriteByte(5));
                    Assert.Throws <IOException>(() => { client.WriteAsync(buffer, 0, buffer.Length); });
                    Assert.Throws <IOException>(() => client.Flush());
                }
                else
                {
                    // Nothing for the client to read, but no exception throwing
                    Assert.Equal(0, client.Read(buffer, 0, buffer.Length));
                    Assert.Equal(-1, client.ReadByte());
                }

                Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
            }
        }
Exemplo n.º 4
0
        public async Task CancelTokenOff_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)
                {
                    Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None);

                    Assert.True(Interop.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(Interop.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);

                    Assert.True(clientWriteToken.IsCanceled);
                }
            }
        }
Exemplo n.º 5
0
        public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_IOException()
        {
            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(Interop.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAsync <IOException>(() => clientReadToken);
                }
                if (client.CanWrite)
                {
                    var  cts = new CancellationTokenSource();
                    Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(Interop.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAsync <IOException>(() => clientWriteToken);
                }
            }
        }
Exemplo n.º 6
0
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_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)
                {
                    var  cts             = new CancellationTokenSource();
                    Task serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(Interop.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(Interop.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);
                }
            }
        }
Exemplo n.º 7
0
        [ActiveIssue(812, PlatformID.AnyUnix)] // the cancellation token is ignored after the operation is initiated, due to base Stream's implementation
        public async Task 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  ctx1            = new CancellationTokenSource();
                    Task serverReadToken = client.ReadAsync(buffer, 0, buffer.Length, ctx1.Token);
                    ctx1.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);

                    Assert.True(client.ReadAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }
                if (client.CanWrite)
                {
                    var  ctx1             = new CancellationTokenSource();
                    Task serverWriteToken = client.WriteAsync(buffer, 0, buffer.Length, ctx1.Token);
                    ctx1.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);

                    Assert.True(client.WriteAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }
            }
        }
Exemplo n.º 8
0
        public async Task OperationsOnDisconnectedServer()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                pair.Connect();

                Assert.Throws <InvalidOperationException>(() => server.IsMessageComplete);
                Assert.Throws <InvalidOperationException>(() => server.WaitForConnection());
                await Assert.ThrowsAsync <InvalidOperationException>(() => server.WaitForConnectionAsync()); // fails because allowed connections is set to 1

                server.Disconnect();

                Assert.Throws <InvalidOperationException>(() => server.Disconnect());    // double disconnect

                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                if (pair.writeToServer)
                {
                    Assert.Throws <InvalidOperationException>(() => server.Write(buffer, 0, buffer.Length));
                    Assert.Throws <InvalidOperationException>(() => server.WriteByte(5));
                    Assert.Throws <InvalidOperationException>(() => { server.WriteAsync(buffer, 0, buffer.Length); });
                }
                else
                {
                    Assert.Throws <InvalidOperationException>(() => server.Read(buffer, 0, buffer.Length));
                    Assert.Throws <InvalidOperationException>(() => server.ReadByte());
                    Assert.Throws <InvalidOperationException>(() => { server.ReadAsync(buffer, 0, buffer.Length); });
                }

                Assert.Throws <InvalidOperationException>(() => server.Flush());
                Assert.Throws <InvalidOperationException>(() => server.IsMessageComplete);
                Assert.Throws <InvalidOperationException>(() => server.GetImpersonationUserName());
            }
        }
Exemplo n.º 9
0
        public async Task Windows_OperationsOnNamedServerWithDisposedClient()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                pair.Connect();
                pair.clientStream.Dispose();

                Assert.Throws <IOException>(() => server.WaitForConnection());
                await Assert.ThrowsAsync <IOException>(() => server.WaitForConnectionAsync());
            }
        }
Exemplo n.º 10
0
        public async Task Unix_OperationsOnNamedServerWithDisposedClient()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                pair.Connect();
                pair.clientStream.Dispose();

                // On Unix, the server still thinks that it is connected after client Disposal.
                Assert.Throws <InvalidOperationException>(() => server.WaitForConnection());
                await Assert.ThrowsAsync <InvalidOperationException>(() => server.WaitForConnectionAsync());
            }
        }
Exemplo n.º 11
0
        public async Task DisposedClientPipe_Throws_ObjectDisposedException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                pair.Connect();
                NamedPipeClientStream pipe = pair.clientStream;
                pipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <ObjectDisposedException>(() => pipe.Connect());
                await Assert.ThrowsAsync <ObjectDisposedException>(() => pipe.ConnectAsync());

                Assert.Throws <ObjectDisposedException>(() => pipe.NumberOfServerInstances);
            }
        }
Exemplo n.º 12
0
        public void WaitForConnectionOnAlreadyConnectedServer_Throws_InvalidOperationException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                NamedPipeClientStream client = pair.clientStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();

                Assert.True(client.IsConnected);
                Assert.True(server.IsConnected);

                Assert.Throws <InvalidOperationException>(() => server.WaitForConnection());
            }
        }
Exemplo n.º 13
0
 public async Task WriteAsync_DisconnectDuringWrite_Throws()
 {
     using (NamedPipePair pair = CreateNamedPipePair())
     {
         pair.Connect();
         Task writeTask;
         if (pair.clientStream.CanWrite)
         {
             writeTask = pair.clientStream.WriteAsync(new byte[1], 0, 1);
             pair.serverStream.Dispose();
         }
         else
         {
             writeTask = pair.serverStream.WriteAsync(new byte[1], 0, 1);
             pair.clientStream.Dispose();
         }
         await Assert.ThrowsAsync <IOException>(() => writeTask);
     }
 }
Exemplo n.º 14
0
 public async Task ReadAsync_DisconnectDuringRead_Returns0()
 {
     using (NamedPipePair pair = CreateNamedPipePair())
     {
         pair.Connect();
         Task <int> readTask;
         if (pair.clientStream.CanRead)
         {
             readTask = pair.clientStream.ReadAsync(new byte[1], 0, 1);
             pair.serverStream.Dispose();
         }
         else
         {
             readTask = pair.serverStream.ReadAsync(new byte[1], 0, 1);
             pair.clientStream.Dispose();
         }
         Assert.Equal(0, await readTask);
     }
 }
Exemplo n.º 15
0
        public virtual async Task OperationsOnDisconnectedClient()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                NamedPipeClientStream client = pair.clientStream;
                pair.Connect();

                Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
                Assert.Throws <InvalidOperationException>(() => client.Connect());
                await Assert.ThrowsAsync <InvalidOperationException>(() => client.ConnectAsync());

                server.Disconnect();

                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                if (!pair.writeToServer)
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // writes on Unix may still succeed after other end disconnects, due to socket being used
                    {
                        // Pipe is broken
                        Assert.Throws <IOException>(() => client.Write(buffer, 0, buffer.Length));
                        Assert.Throws <IOException>(() => client.WriteByte(5));
                        Assert.Throws <IOException>(() => { client.WriteAsync(buffer, 0, buffer.Length); });
                        Assert.Throws <IOException>(() => client.Flush());
                        Assert.Throws <IOException>(() => client.NumberOfServerInstances);
                    }
                }
                else
                {
                    // Nothing for the client to read, but no exception throwing
                    Assert.Equal(0, client.Read(buffer, 0, buffer.Length));
                    Assert.Equal(-1, client.ReadByte());

                    if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // NumberOfServerInstances not supported on Unix
                    {
                        Assert.Throws <PlatformNotSupportedException>(() => client.NumberOfServerInstances);
                    }
                }

                Assert.Throws <InvalidOperationException>(() => client.IsMessageComplete);
            }
        }
Exemplo n.º 16
0
        public async Task ClonedServer_ActsAsOriginalServer()
        {
            byte[] msg1      = new byte[] { 5, 7, 9, 10 };
            byte[] received1 = new byte[] { 0, 0, 0, 0 };

            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream serverBase = pair.serverStream;
                NamedPipeClientStream client     = pair.clientStream;
                pair.Connect();

                if (pair.writeToServer)
                {
                    Task <int> clientTask = client.ReadAsync(received1, 0, received1.Length);
                    using (NamedPipeServerStream server = new NamedPipeServerStream(PipeDirection.Out, false, true, serverBase.SafePipeHandle))
                    {
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            Assert.Equal(1, client.NumberOfServerInstances);
                        }
                        server.Write(msg1, 0, msg1.Length);
                        int receivedLength = await clientTask;
                        Assert.Equal(msg1.Length, receivedLength);
                        Assert.Equal(msg1, received1);
                    }
                }
                else
                {
                    Task clientTask = client.WriteAsync(msg1, 0, msg1.Length);
                    using (NamedPipeServerStream server = new NamedPipeServerStream(PipeDirection.In, false, true, serverBase.SafePipeHandle))
                    {
                        int receivedLength = server.Read(received1, 0, msg1.Length);
                        Assert.Equal(msg1.Length, receivedLength);
                        Assert.Equal(msg1, received1);
                        await clientTask;
                    }
                }
            }
        }
        public async Task ClonedClient_ActsAsOriginalClient()
        {
            byte[] msg1      = new byte[] { 5, 7, 9, 10 };
            byte[] received1 = new byte[] { 0, 0, 0, 0 };

            using (NamedPipePair pair = CreateNamedPipePair())
            {
                pair.Connect();
                NamedPipeServerStream server = pair.serverStream;
                if (pair.writeToServer)
                {
                    using (NamedPipeClientStream client = new NamedPipeClientStream(PipeDirection.In, false, true, pair.clientStream.SafePipeHandle))
                    {
                        if (OperatingSystem.IsWindows())
                        {
                            Assert.Equal(1, client.NumberOfServerInstances);
                        }
                        Task <int> clientTask = client.ReadAsync(received1, 0, received1.Length);
                        server.Write(msg1, 0, msg1.Length);
                        int receivedLength = await clientTask;
                        Assert.Equal(msg1.Length, receivedLength);
                        Assert.Equal(msg1, received1);
                    }
                }
                else
                {
                    using (NamedPipeClientStream client = new NamedPipeClientStream(PipeDirection.Out, false, true, pair.clientStream.SafePipeHandle))
                    {
                        Task clientTask     = client.WriteAsync(msg1, 0, msg1.Length);
                        int  receivedLength = server.Read(received1, 0, msg1.Length);
                        Assert.Equal(msg1.Length, receivedLength);
                        Assert.Equal(msg1, received1);
                        await clientTask;
                    }
                }
            }
        }
        public async Task Server_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                NamedPipeClientStream client = pair.clientStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();

                if (server.CanRead && client.CanWrite)
                {
                    var ctx1 = new CancellationTokenSource();

                    Task <int> serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, ctx1.Token);
                    ctx1.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);

                    ctx1.Cancel();
                    Assert.True(server.ReadAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }

                if (server.CanWrite)
                {
                    var ctx1 = new CancellationTokenSource();
                    if (OperatingSystem.IsWindows()) // On Unix WriteAsync's aren't cancelable once initiated
                    {
                        Task serverWriteToken = server.WriteAsync(buffer, 0, buffer.Length, ctx1.Token);
                        ctx1.Cancel();
                        await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);
                    }
                    ctx1.Cancel();
                    Assert.True(server.WriteAsync(buffer, 0, buffer.Length, ctx1.Token).IsCanceled);
                }
            }
        }