Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 999c624

Browse files
committed
Merge pull request #2730 from ianhays/pipes
Reworked the tests for System.IO.Pipes
2 parents a3b4cf2 + 4d6d556 commit 999c624

23 files changed

+2527
-2408
lines changed

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private NamedPipeServerStream(String pipeName, PipeDirection direction, int maxN
108108
{
109109
throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", SR.ArgumentOutOfRange_MaxNumServerInstances);
110110
}
111+
// inheritability will always be None since this private constructor is only called from other constructors from which
112+
// inheritability is always set to None. Desktop has a public constructor to allow setting it to something else, but Core
113+
// doesnt.
111114
if (inheritability < HandleInheritability.None || inheritability > HandleInheritability.Inheritable)
112115
{
113116
throw new ArgumentOutOfRangeException("inheritability", SR.ArgumentOutOfRange_HandleInheritabilityNoneOrInheritable);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 Microsoft.Win32.SafeHandles;
5+
using Xunit;
6+
7+
namespace System.IO.Pipes.Tests
8+
{
9+
/// <summary>
10+
/// Tests for the constructors of AnonymousPipeClientStream
11+
/// </summary>
12+
public class AnonymousPipeTest_CreateClient : AnonymousPipeTestBase
13+
{
14+
[Fact]
15+
public static void NullParameters_Throws_ArgumentNullException()
16+
{
17+
Assert.Throws<ArgumentNullException>("pipeHandleAsString", () => new AnonymousPipeClientStream((string)null));
18+
Assert.Throws<ArgumentNullException>("pipeHandleAsString", () => new AnonymousPipeClientStream(PipeDirection.Out, (string)null));
19+
Assert.Throws<ArgumentNullException>("safePipeHandle", () => new AnonymousPipeClientStream(PipeDirection.In, (SafePipeHandle)null));
20+
}
21+
22+
[Fact]
23+
public static void CreateClientStreamFromStringHandle_Valid()
24+
{
25+
using (var server = new AnonymousPipeServerStream(PipeDirection.Out))
26+
using (var client = new AnonymousPipeClientStream(server.GetClientHandleAsString()))
27+
{ }
28+
}
29+
30+
[Theory]
31+
[InlineData("abc")]
32+
[InlineData("-1")]
33+
public static void InvalidStringParameters_Throws_ArgumentException(string handle)
34+
{
35+
// Parameters must be nonnegative numeric characters
36+
Assert.Throws<ArgumentException>("pipeHandleAsString", () => new AnonymousPipeClientStream(handle));
37+
Assert.Throws<ArgumentException>("pipeHandleAsString", () => new AnonymousPipeClientStream(PipeDirection.Out, handle));
38+
}
39+
40+
[Fact]
41+
public static void InvalidPipeHandle_Throws_ArgumentException()
42+
{
43+
SafePipeHandle pipeHandle = new SafePipeHandle(new IntPtr(-1), true);
44+
Assert.Throws<ArgumentException>("safePipeHandle", () => new AnonymousPipeClientStream(PipeDirection.In, pipeHandle));
45+
}
46+
47+
[Fact]
48+
public static void InOutPipeDirection_Throws_NotSupportedException()
49+
{
50+
// Anonymous pipes can't be made with PipeDirection.InOut
51+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeClientStream(PipeDirection.InOut, "123"));
52+
53+
SafePipeHandle pipeHandle = new SafePipeHandle(new IntPtr(-1), true);
54+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeClientStream(PipeDirection.InOut, pipeHandle));
55+
}
56+
}
57+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Microsoft.Win32.SafeHandles;
5+
using Xunit;
6+
7+
namespace System.IO.Pipes.Tests
8+
{
9+
/// <summary>
10+
/// Tests for the constructors of AnonymousPipeServerStream
11+
/// </summary>
12+
public class AnonymousPipeTest_CreateServer : AnonymousPipeTestBase
13+
{
14+
[Fact]
15+
public static void InOutPipeDirection_Throws_NotSupportedException()
16+
{
17+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut));
18+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, HandleInheritability.None));
19+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, HandleInheritability.None, 500));
20+
21+
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.Out))
22+
{
23+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, dummyserver.SafePipeHandle, null));
24+
}
25+
26+
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.In))
27+
{
28+
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, dummyserver.SafePipeHandle, null));
29+
}
30+
}
31+
32+
[Theory]
33+
[InlineData(PipeDirection.In, 999)]
34+
[InlineData(PipeDirection.Out, 999)]
35+
public static void ServerBadInheritabilityThrows(PipeDirection direction, HandleInheritability inheritability)
36+
{
37+
Assert.Throws<ArgumentOutOfRangeException>("inheritability", () => new AnonymousPipeServerStream(direction, inheritability));
38+
Assert.Throws<ArgumentOutOfRangeException>("inheritability", () => new AnonymousPipeServerStream(direction, inheritability, 500));
39+
}
40+
41+
[Theory]
42+
[InlineData(PipeDirection.In, -500)]
43+
[InlineData(PipeDirection.Out, -500)]
44+
[InlineData(PipeDirection.InOut, -500)] //bufferSize will cause an exception before InOut will
45+
public static void InvalidBufferSize_Throws_ArgumentOutOfRangeException(PipeDirection direction, int bufferSize)
46+
{
47+
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => new AnonymousPipeServerStream(direction, HandleInheritability.None, bufferSize));
48+
}
49+
50+
[Fact]
51+
public static void InvalidPipeDirection_Throws_ArgumentOutOfRangeException()
52+
{
53+
Assert.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, HandleInheritability.None, 500));
54+
Assert.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, (HandleInheritability)999, -500));
55+
Assert.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, HandleInheritability.None, - 500));
56+
}
57+
58+
[Fact]
59+
public static void InvalidPipeHandle_Throws()
60+
{
61+
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.Out))
62+
{
63+
Assert.Throws<ArgumentNullException>("serverSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, null, dummyserver.ClientSafePipeHandle));
64+
65+
Assert.Throws<ArgumentNullException>("clientSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, dummyserver.SafePipeHandle, null));
66+
67+
SafePipeHandle pipeHandle = new SafePipeHandle(new IntPtr(-1), true);
68+
Assert.Throws<ArgumentException>("serverSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, pipeHandle, dummyserver.ClientSafePipeHandle));
69+
70+
Assert.Throws<ArgumentException>("clientSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, dummyserver.SafePipeHandle, pipeHandle));
71+
}
72+
}
73+
74+
[Fact]
75+
public static void ValidConstructors()
76+
{
77+
new AnonymousPipeServerStream().Dispose();
78+
new AnonymousPipeServerStream(PipeDirection.Out).Dispose();
79+
new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None).Dispose();
80+
new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None, 0).Dispose();
81+
}
82+
}
83+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
public class AnonymousPipeTest_Read_ServerIn_ClientOut : PipeTest_Read
9+
{
10+
protected override ServerClientPair CreateServerClientPair()
11+
{
12+
ServerClientPair ret = new ServerClientPair();
13+
ret.readablePipe = new AnonymousPipeServerStream(PipeDirection.In);
14+
ret.writeablePipe = new AnonymousPipeClientStream(PipeDirection.Out, ((AnonymousPipeServerStream)ret.readablePipe).ClientSafePipeHandle);
15+
return ret;
16+
}
17+
}
18+
19+
public class AnonymousPipeTest_Read_ServerOut_ClientIn : PipeTest_Read
20+
{
21+
protected override ServerClientPair CreateServerClientPair()
22+
{
23+
ServerClientPair ret = new ServerClientPair();
24+
ret.writeablePipe = new AnonymousPipeServerStream(PipeDirection.Out);
25+
ret.readablePipe = new AnonymousPipeClientStream(PipeDirection.In, ((AnonymousPipeServerStream)ret.writeablePipe).ClientSafePipeHandle);
26+
return ret;
27+
}
28+
}
29+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
public class AnonymousPipeTest_Write_ServerIn_ClientOut : PipeTest_Write
9+
{
10+
protected override ServerClientPair CreateServerClientPair()
11+
{
12+
ServerClientPair ret = new ServerClientPair();
13+
ret.readablePipe = new AnonymousPipeServerStream(PipeDirection.In);
14+
ret.writeablePipe = new AnonymousPipeClientStream(PipeDirection.Out, ((AnonymousPipeServerStream)ret.readablePipe).ClientSafePipeHandle);
15+
return ret;
16+
}
17+
}
18+
19+
public class AnonymousPipeTest_Write_ServerOut_ClientIn : PipeTest_Write
20+
{
21+
protected override ServerClientPair CreateServerClientPair()
22+
{
23+
ServerClientPair ret = new ServerClientPair();
24+
ret.writeablePipe = new AnonymousPipeServerStream(PipeDirection.Out);
25+
ret.readablePipe = new AnonymousPipeClientStream(PipeDirection.In, ((AnonymousPipeServerStream)ret.writeablePipe).ClientSafePipeHandle);
26+
return ret;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)