| 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 Microsoft.Win32.SafeHandles; |
| 6 |
using Xunit; |
| 7 |
|
| 8 |
namespace System.IO.Pipes.Tests |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// Tests for the constructors of AnonymousPipeServerStream |
| 12 |
/// </summary> |
| 13 |
public class AnonymousPipeTest_CreateServer : AnonymousPipeTestBase |
| 14 |
{ |
| 15 |
[Fact] |
| 16 |
public static void InOutPipeDirection_Throws_NotSupportedException() |
| 17 |
{ |
| 18 |
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut)); |
| 19 |
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, HandleInheritability.None)); |
| 20 |
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, HandleInheritability.None, 500)); |
| 21 |
|
| 22 |
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 23 |
{ |
| 24 |
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, dummyserver.SafePipeHandle, null)); |
| 25 |
} |
| 26 |
|
| 27 |
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.In)) |
| 28 |
{ |
| 29 |
Assert.Throws<NotSupportedException>(() => new AnonymousPipeServerStream(PipeDirection.InOut, dummyserver.SafePipeHandle, null)); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
[Theory] |
| 34 |
[InlineData(PipeDirection.In, 999)] |
| 35 |
[InlineData(PipeDirection.Out, 999)] |
| 36 |
public static void ServerBadInheritabilityThrows(PipeDirection direction, HandleInheritability inheritability) |
| 37 |
{ |
| 38 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new AnonymousPipeServerStream(direction, inheritability)); |
| 39 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new AnonymousPipeServerStream(direction, inheritability, 500)); |
| 40 |
} |
| 41 |
|
| 42 |
[Theory] |
| 43 |
[InlineData(PipeDirection.In, -500)] |
| 44 |
[InlineData(PipeDirection.Out, -500)] |
| 45 |
[InlineData(PipeDirection.InOut, -500)] //bufferSize will cause an exception before InOut will |
| 46 |
public static void InvalidBufferSize_Throws_ArgumentOutOfRangeException(PipeDirection direction, int bufferSize) |
| 47 |
{ |
| 48 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("bufferSize", () => new AnonymousPipeServerStream(direction, HandleInheritability.None, bufferSize)); |
| 49 |
} |
| 50 |
|
| 51 |
[Fact] |
| 52 |
public static void InvalidPipeDirection_Throws_ArgumentOutOfRangeException() |
| 53 |
{ |
| 54 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, HandleInheritability.None, 500)); |
| 55 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, (HandleInheritability)999, -500)); |
| 56 |
AssertExtensions.Throws<ArgumentOutOfRangeException>("direction", () => new AnonymousPipeServerStream((PipeDirection)123, HandleInheritability.None, - 500)); |
| 57 |
} |
| 58 |
|
| 59 |
[Fact] |
| 60 |
public static void InvalidPipeHandle_Throws() |
| 61 |
{ |
| 62 |
using (AnonymousPipeServerStream dummyserver = new AnonymousPipeServerStream(PipeDirection.Out)) |
| 63 |
{ |
| 64 |
AssertExtensions.Throws<ArgumentNullException>("serverSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, null, dummyserver.ClientSafePipeHandle)); |
| 65 |
|
| 66 |
AssertExtensions.Throws<ArgumentNullException>("clientSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, dummyserver.SafePipeHandle, null)); |
| 67 |
|
| 68 |
SafePipeHandle pipeHandle = new SafePipeHandle(new IntPtr(-1), true); |
| 69 |
AssertExtensions.Throws<ArgumentException>("serverSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, pipeHandle, dummyserver.ClientSafePipeHandle)); |
| 70 |
|
| 71 |
AssertExtensions.Throws<ArgumentException>("clientSafePipeHandle", () => new AnonymousPipeServerStream(PipeDirection.Out, dummyserver.SafePipeHandle, pipeHandle)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
[Fact] |
| 76 |
public static void ValidConstructors() |
| 77 |
{ |
| 78 |
new AnonymousPipeServerStream().Dispose(); |
| 79 |
new AnonymousPipeServerStream(PipeDirection.Out).Dispose(); |
| 80 |
new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None).Dispose(); |
| 81 |
new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable).Dispose(); |
| 82 |
new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None, 0).Dispose(); |
| 83 |
} |
| 84 |
} |
| 85 |
} |