Ejemplo n.º 1
0
        public static void ServerReadNegativeOffsetThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                Assert.Throws <ArgumentOutOfRangeException>(() => server.Read(new byte[5], -1, 1));

                // array is checked first
                Assert.Throws <ArgumentNullException>(() => server.Read(null, -1, 1));
            }
        }
Ejemplo n.º 2
0
 public static void ServerReadBufferNullThrows()
 {
     using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
     {
         Assert.Throws <ArgumentNullException>(() => server.Read(null, 0, 1));
     }
 }
Ejemplo n.º 3
0
    public static void ClientSendsByteServerReceives()
    {
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
        {
            Assert.True(server.IsConnected);
            server.ReadMode = PipeTransmissionMode.Byte;

            using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle))
            {
                Assert.True(server.IsConnected);
                Assert.True(client.IsConnected);
                client.ReadMode = PipeTransmissionMode.Byte;

                byte[] sent     = new byte[] { 123 };
                byte[] received = new byte[] { 0 };
                client.Write(sent, 0, 1);

                server.DisposeLocalCopyOfClientHandle();

                Assert.Equal(1, server.Read(received, 0, 1));
                Assert.Equal(sent[0], received[0]);
            }
            // not sure why the following isn't thrown because pipe is broken
            //Assert.Throws<System.IO.IOException>(() => server.ReadByte());
        }
    }
Ejemplo n.º 4
0
 internal static bool ReadFile(AnonymousPipeServerStream hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped)
 {
     ErrorUtilities.VerifyThrow(
         lpOverlapped == NullIntPtr,
         "This method should only be passed NullIntPtr, but was passed {0} instead!",
         lpOverlapped
         );
     lpNumberOfBytesRead = (uint)hFile.Read(lpBuffer, 0, (int)nNumberOfBytesToRead);
     return(true);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Get an object from the specified anonymous in pipe.
        /// </summary>
        /// <param name="pipeReader">The pipe to read from.</param>
        /// <returns>The object or null if no object read.</returns>
        public static object GetObjectFromPipe(AnonymousPipeServerStream pipeReader)
        {
            // Read number of bytes.
            var intBuffer = new byte[4];

            pipeReader.Read(intBuffer, 0, 4);
            var numBytes = BitConverter.ToInt32(intBuffer, 0);

            if (numBytes > 0)
            {
                // Read bytes for object.
                var buffer = new byte[numBytes];
                pipeReader.Read(buffer, 0, numBytes);

                // Convert bytes to object.
                return(ReflectionUtilities.BinaryDeserialise(new MemoryStream(buffer)));
            }
            return(null);
        }
Ejemplo n.º 6
0
        public static void ServerWriteOnlyThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out))
            {
                Assert.Throws <NotSupportedException>(() => server.Read(new byte[5], 0, 5));

                Assert.Throws <NotSupportedException>(() => server.ReadByte());

                Assert.Throws <NotSupportedException>(() => server.InBufferSize);
            }
        }
Ejemplo n.º 7
0
        public static void ServerReadNegativeCountThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                Assert.Throws <System.ArgumentOutOfRangeException>(() => server.Read(new byte[5], 0, -1));

                // offset is checked before count
                Assert.Throws <ArgumentOutOfRangeException>(() => server.Read(new byte[1], -1, -1));

                // array is checked first
                Assert.Throws <ArgumentNullException>(() => server.Read(null, -1, -1));

                Assert.Throws <System.ArgumentOutOfRangeException>(() => NotReachable(server.ReadAsync(new byte[5], 0, -1)));

                // offset is checked before count
                Assert.Throws <ArgumentOutOfRangeException>(() => NotReachable(server.ReadAsync(new byte[1], -1, -1)));

                // array is checked first
                Assert.Throws <ArgumentNullException>(() => NotReachable(server.ReadAsync(null, -1, -1)));
            }
        }
Ejemplo n.º 8
0
        public static void DisposeLocalCopyOfClientHandle_BeforeServerRead()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
                using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle))
                {
                    byte[] sent     = new byte[] { 123 };
                    byte[] received = new byte[] { 0 };
                    client.Write(sent, 0, 1);

                    server.DisposeLocalCopyOfClientHandle();

                    Assert.Equal(1, server.Read(received, 0, 1));
                    Assert.Equal(sent[0], received[0]);
                }
        }
Ejemplo n.º 9
0
        private static void AnonymousReader()
        {
            using (var reader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
            {
                WriteLine("using anonymous pipe");
                string pipeHandle = reader.GetClientHandleAsString();
                WriteLine($"pipe handle: {pipeHandle}");

                byte[] buffer = new byte[256];
                int    nRead  = reader.Read(buffer, 0, 256);

                string line = Encoding.UTF8.GetString(buffer, 0, 256);
                WriteLine(line);
            }
        }
        public static void DisposeLocalCopyOfClientHandle_BeforeServerRead()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle))
                {
                    byte[] sent = new byte[] { 123 };
                    byte[] received = new byte[] { 0 };
                    client.Write(sent, 0, 1);

                    server.DisposeLocalCopyOfClientHandle();

                    Assert.Equal(1, server.Read(received, 0, 1));
                    Assert.Equal(sent[0], received[0]);
                }
            }
        }
        //匿名管道服务端
        private void AnonymousReader()
        {
            using (var reader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
            {
                Console.WriteLine("using anonymous pipe");
                //需要知道客户端的句柄,这个句柄在GetClientHandleAsString方法中转换为一个字符串
                //这个变量以后由充当写入器的客户端使用
                string pipeHandle = reader.GetClientHandleAsString();
                Console.WriteLine($"pipe handle: {pipeHandle}");

                byte[] buffer = new byte[256];
                int    nRead  = reader.Read(buffer, 0, 256);

                string line = Encoding.UTF8.GetString(buffer, 0, 256);
                Console.WriteLine(line);
            }
        }
Ejemplo n.º 12
0
    public static void ClientSendsByteServerReceives()
    {
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
        {
            Assert.True(server.IsConnected);
            using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle))
            {
                Assert.True(server.IsConnected);
                Assert.True(client.IsConnected);

                byte[] sent     = new byte[] { 123 };
                byte[] received = new byte[] { 0 };
                client.Write(sent, 0, 1);

                Assert.Equal(1, server.Read(received, 0, 1));
                Assert.Equal(sent[0], received[0]);
            }
        }
    }
Ejemplo n.º 13
0
    public static void ServerPInvokeChecks()
    {
        // calling every API related to server and client to detect any bad PInvokes
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out))
        {
            Task clientTask = Task.Run(() => StartClient(PipeDirection.In, server.ClientSafePipeHandle));

            Assert.False(server.CanRead);
            Assert.False(server.CanSeek);
            Assert.False(server.CanTimeout);
            Assert.True(server.CanWrite);
            Assert.False(string.IsNullOrWhiteSpace(server.GetClientHandleAsString()));
            Assert.False(server.IsAsync);
            Assert.True(server.IsConnected);
            Assert.Equal(0, server.OutBufferSize);
            Assert.Equal(PipeTransmissionMode.Byte, server.ReadMode);
            Assert.NotNull(server.SafePipeHandle);
            Assert.Equal(PipeTransmissionMode.Byte, server.TransmissionMode);

            server.Write(new byte[] { 123 }, 0, 1);
            server.WriteAsync(new byte[] { 124 }, 0, 1).Wait();
            server.Flush();
            server.WaitForPipeDrain();

            clientTask.Wait();
            server.DisposeLocalCopyOfClientHandle();
        }

        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
        {
            Task clientTask = Task.Run(() => StartClient(PipeDirection.Out, server.ClientSafePipeHandle));

            Assert.Equal(4096, server.InBufferSize);
            byte[] readData = new byte[] { 0, 1 };
            Assert.Equal(1, server.Read(readData, 0, 1));
            Assert.Equal(1, server.ReadAsync(readData, 1, 1).Result);
            Assert.Equal(123, readData[0]);
            Assert.Equal(124, readData[1]);

            clientTask.Wait();
        }
    }
Ejemplo n.º 14
0
        public static void ServerReadArrayOutOfBoundsThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                // offset out of bounds
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[1], 1, 1));

                // offset out of bounds for 0 count read
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[1], 2, 0));

                // offset out of bounds even for 0 length buffer
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[0], 1, 0));

                // combination offset and count out of bounds
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[2], 1, 2));

                // edges
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[0], int.MaxValue, 0));
                Assert.Throws <ArgumentException>(null, () => server.Read(new byte[0], int.MaxValue, int.MaxValue));

                Assert.Throws <ArgumentException>(() => server.Read(new byte[5], 3, 4));

                // offset out of bounds
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[1], 1, 1)));

                // offset out of bounds for 0 count read
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[1], 2, 0)));

                // offset out of bounds even for 0 length buffer
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], 1, 0)));

                // combination offset and count out of bounds
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[2], 1, 2)));

                // edges
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], int.MaxValue, 0)));
                Assert.Throws <ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], int.MaxValue, int.MaxValue)));

                Assert.Throws <ArgumentException>(() => NotReachable(server.ReadAsync(new byte[5], 3, 4)));
            }
        }
Ejemplo n.º 15
0
    public static void ServerPInvokeChecks()
    {
        // calling every API related to server and client to detect any bad PInvokes
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out))
        {
            Task clientTask = StartClientAsync(PipeDirection.In, server.ClientSafePipeHandle);

            Console.WriteLine("server.CanRead = {0}", server.CanRead);
            Console.WriteLine("server.CanSeek = {0}", server.CanSeek);
            Console.WriteLine("server.CanTimeout = {0}", server.CanTimeout);
            Console.WriteLine("server.CanWrite = {0}", server.CanWrite);
            Console.WriteLine("server.GetClientHandleAsString() = {0}", server.GetClientHandleAsString());
            Console.WriteLine("server.IsAsync = {0}", server.IsAsync);
            Console.WriteLine("server.IsConnected = {0}", server.IsConnected);
            Console.WriteLine("server.OutBufferSize = {0}", server.OutBufferSize);
            Console.WriteLine("server.ReadMode = {0}", server.ReadMode);
            Console.WriteLine("server.SafePipeHandle = {0}", server.SafePipeHandle);
            Console.WriteLine("server.TransmissionMode = {0}", server.TransmissionMode);
            server.Write(new byte[] { 123 }, 0, 1);
            server.WriteAsync(new byte[] { 124 }, 0, 1).Wait();
            server.Flush();
            Console.WriteLine("Waiting for Pipe Drain.");
            server.WaitForPipeDrain();
            clientTask.Wait();
            server.DisposeLocalCopyOfClientHandle();
        }
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
        {
            Task clientTask = StartClientAsync(PipeDirection.Out, server.ClientSafePipeHandle);

            Console.WriteLine("server.InBufferSize = {0}", server.InBufferSize);
            byte[] readData = new byte[] { 0, 1 };
            server.Read(readData, 0, 1);
            server.ReadAsync(readData, 1, 1).Wait();
            Assert.Equal(123, readData[0]);
            Assert.Equal(124, readData[1]);
        }
    }
Ejemplo n.º 16
0
        public static void ServerWriteOnlyThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out))
            {
                Assert.Throws<NotSupportedException>(() => server.Read(new byte[5], 0, 5));

                Assert.Throws<NotSupportedException>(() => server.ReadByte());

                Assert.Throws<NotSupportedException>(() => server.InBufferSize);

                Assert.Throws<NotSupportedException>(() => NotReachable(server.ReadAsync(new byte[5], 0, 5)));
            }
        }
Ejemplo n.º 17
0
    public static async Task ServerPInvokeChecks()
    {
        // calling every API related to server and client to detect any bad PInvokes
        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None, 4096))
        {
            Task clientTask = Task.Run(() => StartClient(PipeDirection.In, server.ClientSafePipeHandle));

            Assert.False(server.CanRead);
            Assert.False(server.CanSeek);
            Assert.False(server.CanTimeout);
            Assert.True(server.CanWrite);
            Assert.False(string.IsNullOrWhiteSpace(server.GetClientHandleAsString()));
            Assert.False(server.IsAsync);
            Assert.True(server.IsConnected);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
                RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Assert.True(server.OutBufferSize > 0);
            }
            else
            {
                Assert.Throws <PlatformNotSupportedException>(() => server.OutBufferSize);
            }
            Assert.Equal(PipeTransmissionMode.Byte, server.ReadMode);
            Assert.NotNull(server.SafePipeHandle);
            Assert.Equal(PipeTransmissionMode.Byte, server.TransmissionMode);

            server.Write(new byte[] { 123 }, 0, 1);
            await server.WriteAsync(new byte[] { 124 }, 0, 1);

            server.Flush();
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                server.WaitForPipeDrain();
            }
            else
            {
                Assert.Throws <PlatformNotSupportedException>(() => server.WaitForPipeDrain());
            }

            await clientTask;
            server.DisposeLocalCopyOfClientHandle();
        }

        using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
        {
            Task clientTask = Task.Run(() => StartClient(PipeDirection.Out, server.ClientSafePipeHandle));

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.Equal(4096, server.InBufferSize);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Assert.True(server.InBufferSize > 0);
            }
            else
            {
                Assert.Throws <PlatformNotSupportedException>(() => server.InBufferSize);
            }
            byte[] readData = new byte[] { 0, 1 };
            Assert.Equal(1, server.Read(readData, 0, 1));
            Assert.Equal(1, server.ReadAsync(readData, 1, 1).Result);
            Assert.Equal(123, readData[0]);
            Assert.Equal(124, readData[1]);

            await clientTask;
        }
    }
Ejemplo n.º 18
0
        public static void ServerReadArrayOutOfBoundsThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                // offset out of bounds
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[1], 1, 1));

                // offset out of bounds for 0 count read
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[1], 2, 0));

                // offset out of bounds even for 0 length buffer
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[0], 1, 0));

                // combination offset and count out of bounds
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[2], 1, 2));

                // edges
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[0], int.MaxValue, 0));
                Assert.Throws<ArgumentException>(null, () => server.Read(new byte[0], int.MaxValue, int.MaxValue));

                Assert.Throws<ArgumentException>(() => server.Read(new byte[5], 3, 4));

                // offset out of bounds
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[1], 1, 1)));

                // offset out of bounds for 0 count read
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[1], 2, 0)));

                // offset out of bounds even for 0 length buffer
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], 1, 0)));

                // combination offset and count out of bounds
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[2], 1, 2)));

                // edges
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], int.MaxValue, 0)));
                Assert.Throws<ArgumentException>(null, () => NotReachable(server.ReadAsync(new byte[0], int.MaxValue, int.MaxValue)));

                Assert.Throws<ArgumentException>(() => NotReachable(server.ReadAsync(new byte[5], 3, 4)));
            }
        }
Ejemplo n.º 19
0
        public static void ServerReadNegativeCountThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                Assert.Throws<System.ArgumentOutOfRangeException>(() => server.Read(new byte[5], 0, -1));

                // offset is checked before count
                Assert.Throws<ArgumentOutOfRangeException>(() => server.Read(new byte[1], -1, -1));

                // array is checked first
                Assert.Throws<ArgumentNullException>(() => server.Read(null, -1, -1));

                Assert.Throws<System.ArgumentOutOfRangeException>(() => NotReachable(server.ReadAsync(new byte[5], 0, -1)));

                // offset is checked before count
                Assert.Throws<ArgumentOutOfRangeException>(() => NotReachable(server.ReadAsync(new byte[1], -1, -1)));

                // array is checked first
                Assert.Throws<ArgumentNullException>(() => NotReachable(server.ReadAsync(null, -1, -1)));
            }
        }
Ejemplo n.º 20
0
        public static void ServerReadBufferNullThrows()
        {
            using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In))
            {
                Assert.Throws<ArgumentNullException>(() => server.Read(null, 0, 1));

                Assert.Throws<ArgumentNullException>(() => NotReachable(server.ReadAsync(null, 0, 1)));
            }
        }