|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace System.IO.Pipes.Tests |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// The Specific AnonymousPipe tests cover edge cases or otherwise narrow cases that |
| 10 | + /// show up within particular server/client directional combinations. |
| 11 | + /// </summary> |
| 12 | + public class AnonymousPipeTest_Specific : AnonymousPipeTestBase |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public static void DisposeLocalCopyOfClientHandle_BeforeServerRead() |
| 16 | + { |
| 17 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) |
| 18 | + { |
| 19 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) |
| 20 | + { |
| 21 | + byte[] sent = new byte[] { 123 }; |
| 22 | + byte[] received = new byte[] { 0 }; |
| 23 | + client.Write(sent, 0, 1); |
| 24 | + |
| 25 | + server.DisposeLocalCopyOfClientHandle(); |
| 26 | + |
| 27 | + Assert.Equal(1, server.Read(received, 0, 1)); |
| 28 | + Assert.Equal(sent[0], received[0]); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public static void ClonedServer_ActsAsOriginalServer() |
| 35 | + { |
| 36 | + using (AnonymousPipeServerStream serverBase = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 37 | + { |
| 38 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out, serverBase.SafePipeHandle, serverBase.ClientSafePipeHandle)) |
| 39 | + { |
| 40 | + Assert.True(server.IsConnected); |
| 41 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.GetClientHandleAsString())) |
| 42 | + { |
| 43 | + Assert.True(server.IsConnected); |
| 44 | + Assert.True(client.IsConnected); |
| 45 | + |
| 46 | + byte[] sent = new byte[] { 123 }; |
| 47 | + byte[] received = new byte[] { 0 }; |
| 48 | + server.Write(sent, 0, 1); |
| 49 | + |
| 50 | + Assert.Equal(1, client.Read(received, 0, 1)); |
| 51 | + Assert.Equal(sent[0], received[0]); |
| 52 | + } |
| 53 | + Assert.Throws<IOException>(() => server.WriteByte(5)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public static void ClonedClient_ActsAsOriginalClient() |
| 60 | + { |
| 61 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 62 | + { |
| 63 | + using (AnonymousPipeClientStream clientBase = new AnonymousPipeClientStream(PipeDirection.In, server.GetClientHandleAsString())) |
| 64 | + { |
| 65 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, clientBase.SafePipeHandle)) |
| 66 | + { |
| 67 | + Assert.True(server.IsConnected); |
| 68 | + Assert.True(client.IsConnected); |
| 69 | + |
| 70 | + byte[] sent = new byte[] { 123 }; |
| 71 | + byte[] received = new byte[] { 0 }; |
| 72 | + server.Write(sent, 0, 1); |
| 73 | + |
| 74 | + Assert.Equal(1, client.Read(received, 0, 1)); |
| 75 | + Assert.Equal(sent[0], received[0]); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + [PlatformSpecific(PlatformID.Linux)] |
| 83 | + public static void Linux_BufferSizeRoundtrips() |
| 84 | + { |
| 85 | + // On Linux, setting the buffer size of the server will also set the buffer size of the |
| 86 | + // client, regardless of the direction of the flow |
| 87 | + |
| 88 | + int desiredBufferSize; |
| 89 | + using (var server = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 90 | + { |
| 91 | + desiredBufferSize = server.OutBufferSize * 2; |
| 92 | + Assert.True(desiredBufferSize > 0); |
| 93 | + } |
| 94 | + |
| 95 | + using (var server = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None, desiredBufferSize)) |
| 96 | + using (var client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) |
| 97 | + { |
| 98 | + Assert.Equal(desiredBufferSize, server.OutBufferSize); |
| 99 | + Assert.Equal(desiredBufferSize, client.InBufferSize); |
| 100 | + } |
| 101 | + |
| 102 | + using (var server = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.None, desiredBufferSize)) |
| 103 | + using (var client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) |
| 104 | + { |
| 105 | + Assert.Equal(desiredBufferSize, server.InBufferSize); |
| 106 | + Assert.Equal(desiredBufferSize, client.OutBufferSize); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + [PlatformSpecific(~PlatformID.Linux)] |
| 112 | + public static void BufferSizeRoundtripping() |
| 113 | + { |
| 114 | + // On systems other than Linux, setting the buffer size of the server will only set |
| 115 | + // set the buffer size of the client if the flow of the pipe is towards the client i.e. |
| 116 | + // the client is defined with PipeDirection.In |
| 117 | + |
| 118 | + int desiredBufferSize = 10; |
| 119 | + using (var server = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None, desiredBufferSize)) |
| 120 | + using (var client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) |
| 121 | + { |
| 122 | + Assert.Equal(desiredBufferSize, server.OutBufferSize); |
| 123 | + Assert.Equal(desiredBufferSize, client.InBufferSize); |
| 124 | + } |
| 125 | + |
| 126 | + using (var server = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.None, desiredBufferSize)) |
| 127 | + using (var client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) |
| 128 | + { |
| 129 | + Assert.Equal(desiredBufferSize, server.InBufferSize); |
| 130 | + Assert.Equal(0, client.OutBufferSize); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void PipeTransmissionMode_Returns_Byte() |
| 136 | + { |
| 137 | + using (ServerClientPair pair = CreateServerClientPair()) |
| 138 | + { |
| 139 | + Assert.Equal(PipeTransmissionMode.Byte, pair.writeablePipe.TransmissionMode); |
| 140 | + Assert.Equal(PipeTransmissionMode.Byte, pair.readablePipe.TransmissionMode); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + [Theory] |
| 145 | + [InlineData(PipeDirection.Out, PipeDirection.In)] |
| 146 | + [InlineData(PipeDirection.In, PipeDirection.Out)] |
| 147 | + public void ReadModeToByte_Accepted(PipeDirection serverDirection, PipeDirection clientDirection) |
| 148 | + { |
| 149 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(serverDirection)) |
| 150 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(clientDirection, server.GetClientHandleAsString())) |
| 151 | + { |
| 152 | + server.ReadMode = PipeTransmissionMode.Byte; |
| 153 | + client.ReadMode = PipeTransmissionMode.Byte; |
| 154 | + Assert.Equal(PipeTransmissionMode.Byte, server.ReadMode); |
| 155 | + Assert.Equal(PipeTransmissionMode.Byte, client.ReadMode); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void MessageReadMode_Throws_NotSupportedException() |
| 161 | + { |
| 162 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 163 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.GetClientHandleAsString())) |
| 164 | + { |
| 165 | + Assert.Throws<NotSupportedException>(() => server.ReadMode = PipeTransmissionMode.Message); |
| 166 | + Assert.Throws<NotSupportedException>(() => client.ReadMode = PipeTransmissionMode.Message); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public void InvalidReadMode_Throws_ArgumentOutOfRangeException() |
| 172 | + { |
| 173 | + using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 174 | + using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.GetClientHandleAsString())) |
| 175 | + { |
| 176 | + Assert.Throws<ArgumentOutOfRangeException>(() => server.ReadMode = (PipeTransmissionMode)999); |
| 177 | + Assert.Throws<ArgumentOutOfRangeException>(() => client.ReadMode = (PipeTransmissionMode)999); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments