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