Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        protected override NamedPipePair CreateNamedPipePair(PipeOptions serverOptions, PipeOptions clientOptions)
        {
            NamedPipePair ret      = new NamedPipePair();
            string        pipeName = GetUniquePipeName();

            ret.serverStream  = new NamedPipeServerStream(pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, serverOptions);
            ret.clientStream  = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, clientOptions);
            ret.writeToServer = false;
            return(ret);
        }
Ejemplo n.º 3
0
        public async Task DisposedClientPipe_Throws_ObjectDisposedException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                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());
            }
        }
Ejemplo n.º 4
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());
            }
        }
Ejemplo n.º 5
0
        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(Interop.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
            }
        }
Ejemplo n.º 6
0
        public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                Task waitForConnectionTask   = server.WaitForConnectionAsync(CancellationToken.None);

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

                Assert.True(waitForConnectionTask.IsCanceled);
            }
        }
Ejemplo n.º 7
0
        public async Task DisposedServerPipe_Throws_ObjectDisposedException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream pipe = pair.serverStream;
                pipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <ObjectDisposedException>(() => pipe.Disconnect());
                Assert.Throws <ObjectDisposedException>(() => pipe.WaitForConnection());
                await Assert.ThrowsAsync <ObjectDisposedException>(() => pipe.WaitForConnectionAsync());
            }
        }
Ejemplo n.º 8
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());
            }
        }
Ejemplo n.º 9
0
        public void OperationsOnUnconnectedServer()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;

                // doesn't throw exceptions
                PipeTransmissionMode transmitMode = server.TransmissionMode;
                Assert.Throws <ArgumentOutOfRangeException>(() => server.ReadMode = (PipeTransmissionMode)999);

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

                if (pair.writeToServer)
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        Assert.Equal(0, server.OutBufferSize);
                    }
                    else
                    {
                        Assert.Throws <PlatformNotSupportedException>(() => server.OutBufferSize);
                    }

                    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
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        Assert.Equal(0, server.InBufferSize);
                    }
                    else
                    {
                        Assert.Throws <PlatformNotSupportedException>(() => server.InBufferSize);
                    }

                    PipeTransmissionMode readMode = server.ReadMode;
                    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.Disconnect());    // disconnect when not connected
                Assert.Throws <InvalidOperationException>(() => server.IsMessageComplete);
            }
        }
Ejemplo n.º 10
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());
            }
        }
Ejemplo n.º 11
0
        public async Task CancelTokenOn_ServerWaitForConnectionAsync_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                var ctx = new CancellationTokenSource();

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // [ActiveIssue(812, PlatformID.AnyUnix)] - cancellation token after the operation has been initiated
                {
                    Task serverWaitTimeout = server.WaitForConnectionAsync(ctx.Token);
                    ctx.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWaitTimeout);
                }

                ctx.Cancel();
                Assert.True(server.WaitForConnectionAsync(ctx.Token).IsCanceled);
            }
        }
        public async Task CancelTokenOn_ServerWaitForConnectionAsync_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                var ctx = new CancellationTokenSource();

                if (OperatingSystem.IsWindows()) // cancellation token after the operation has been initiated
                {
                    Task serverWaitTimeout = server.WaitForConnectionAsync(ctx.Token);
                    ctx.Cancel();
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWaitTimeout);
                }

                ctx.Cancel();
                Assert.True(server.WaitForConnectionAsync(ctx.Token).IsCanceled);
            }
        }
Ejemplo n.º 13
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);
     }
 }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
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);
     }
 }
Ejemplo 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;
                    }
                }
            }
        }
Ejemplo n.º 18
0
        public async Task ManyConcurrentOperations(bool cancelable)
        {
            using (NamedPipePair pair = CreateNamedPipePair(PipeOptions.Asynchronous, PipeOptions.Asynchronous))
            {
                await Task.WhenAll(pair.serverStream.WaitForConnectionAsync(), pair.clientStream.ConnectAsync());

                const int NumOps      = 100;
                const int DataPerOp   = 512;
                byte[]    sendingData = new byte[NumOps * DataPerOp];
                byte[]    readingData = new byte[sendingData.Length];
                new Random().NextBytes(sendingData);
                var cancellationToken = cancelable ? new CancellationTokenSource().Token : CancellationToken.None;

                Stream reader = pair.writeToServer ? (Stream)pair.clientStream : pair.serverStream;
                Stream writer = pair.writeToServer ? (Stream)pair.serverStream : pair.clientStream;

                var reads  = new Task <int> [NumOps];
                var writes = new Task[NumOps];

                for (int i = 0; i < reads.Length; i++)
                {
                    reads[i] = reader.ReadAsync(readingData, i * DataPerOp, DataPerOp, cancellationToken);
                }
                for (int i = 0; i < reads.Length; i++)
                {
                    writes[i] = writer.WriteAsync(sendingData, i * DataPerOp, DataPerOp, cancellationToken);
                }

                const int WaitTimeout = 30000;
                Assert.True(Task.WaitAll(writes, WaitTimeout));
                Assert.True(Task.WaitAll(reads, WaitTimeout));

                // The data of each write may not be written atomically, and as such some of the data may be
                // interleaved rather than entirely in the order written.
                Assert.Equal(sendingData.OrderBy(b => b), readingData.OrderBy(b => b));
            }
        }
Ejemplo n.º 19
0
        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 (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // 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);
                }
            }
        }
Ejemplo n.º 20
0
        public async Task OneWayReadWrites(PipeOptions serverOptions, PipeOptions clientOptions, bool asyncServerOps, bool asyncClientOps)
        {
            using (NamedPipePair pair = CreateNamedPipePair(serverOptions, clientOptions))
            {
                NamedPipeClientStream client = pair.clientStream;
                NamedPipeServerStream server = pair.serverStream;
                byte[] received   = new byte[] { 0 };
                Task   clientTask = Task.Run(async() =>
                {
                    if (asyncClientOps)
                    {
                        await client.ConnectAsync();

                        if (pair.writeToServer)
                        {
                            received = await ReadBytesAsync(client, sendBytes.Length);
                        }
                        else
                        {
                            await WriteBytesAsync(client, sendBytes);
                        }
                    }
                    else
                    {
                        client.Connect();
                        if (pair.writeToServer)
                        {
                            received = ReadBytes(client, sendBytes.Length);
                        }
                        else
                        {
                            WriteBytes(client, sendBytes);
                        }
                    }
                });
                if (asyncServerOps)
                {
                    await server.WaitForConnectionAsync();

                    if (pair.writeToServer)
                    {
                        await WriteBytesAsync(server, sendBytes);
                    }
                    else
                    {
                        received = await ReadBytesAsync(server, sendBytes.Length);
                    }
                }
                else
                {
                    server.WaitForConnection();
                    if (pair.writeToServer)
                    {
                        WriteBytes(server, sendBytes);
                    }
                    else
                    {
                        received = ReadBytes(server, sendBytes.Length);
                    }
                }

                await clientTask;
                Assert.Equal(sendBytes, received);

                server.Disconnect();
                Assert.False(server.IsConnected);
            }
        }
Ejemplo n.º 21
0
 protected override NamedPipePair CreateNamedPipePair(PipeOptions serverOptions, PipeOptions clientOptions)
 {
     NamedPipePair ret = new NamedPipePair();
     string pipeName = GetUniquePipeName();
     ret.serverStream = new NamedPipeServerStream(pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, serverOptions);
     ret.clientStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, clientOptions);
     ret.writeToServer = false;
     return ret;
 }