You've already forked linux-packaging-mono
Imported Upstream version 5.0.0.42
Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
parent
1190d13a04
commit
6bdd276d05
213
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/AcceptAsync.cs
vendored
Normal file
213
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/AcceptAsync.cs
vendored
Normal file
@@ -0,0 +1,213 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class AcceptAsync
|
||||
{
|
||||
private readonly ITestOutputHelper _log;
|
||||
|
||||
public AcceptAsync(ITestOutputHelper output)
|
||||
{
|
||||
_log = TestLogging.GetInstance();
|
||||
}
|
||||
|
||||
public void OnAcceptCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
_log.WriteLine("OnAcceptCompleted event handler");
|
||||
EventWaitHandle handle = (EventWaitHandle)args.UserToken;
|
||||
handle.Set();
|
||||
}
|
||||
public void OnConnectCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
_log.WriteLine("OnConnectCompleted event handler");
|
||||
EventWaitHandle handle = (EventWaitHandle)args.UserToken;
|
||||
handle.Set();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[Trait("IPv4", "true")]
|
||||
public void AcceptAsync_IpV4_Success()
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
AutoResetEvent completedClient = new AutoResetEvent(false);
|
||||
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
int port = sock.BindToAnonymousPort(IPAddress.Loopback);
|
||||
sock.Listen(1);
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.Completed += OnAcceptCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
Assert.True(sock.AcceptAsync(args));
|
||||
_log.WriteLine("IPv4 Server: Waiting for clients.");
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
SocketAsyncEventArgs argsClient = new SocketAsyncEventArgs();
|
||||
argsClient.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port);
|
||||
argsClient.Completed += OnConnectCompleted;
|
||||
argsClient.UserToken = completedClient;
|
||||
client.ConnectAsync(argsClient);
|
||||
|
||||
_log.WriteLine("IPv4 Client: Connecting.");
|
||||
Assert.True(completed.WaitOne(5000), "IPv4: Timed out while waiting for connection");
|
||||
|
||||
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
|
||||
Assert.NotNull(args.AcceptSocket);
|
||||
Assert.True(args.AcceptSocket.Connected, "IPv4 Accept Socket was not connected");
|
||||
Assert.NotNull(args.AcceptSocket.RemoteEndPoint);
|
||||
Assert.Equal(client.LocalEndPoint, args.AcceptSocket.RemoteEndPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[Trait("IPv6", "true")]
|
||||
public void AcceptAsync_IPv6_Success()
|
||||
{
|
||||
Assert.True(Capability.IPv6Support());
|
||||
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
AutoResetEvent completedClient = new AutoResetEvent(false);
|
||||
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
int port = sock.BindToAnonymousPort(IPAddress.IPv6Loopback);
|
||||
sock.Listen(1);
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.Completed += OnAcceptCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
Assert.True(sock.AcceptAsync(args));
|
||||
_log.WriteLine("IPv6 Server: Waiting for clients.");
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
SocketAsyncEventArgs argsClient = new SocketAsyncEventArgs();
|
||||
argsClient.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, port);
|
||||
argsClient.Completed += OnConnectCompleted;
|
||||
argsClient.UserToken = completedClient;
|
||||
client.ConnectAsync(argsClient);
|
||||
|
||||
_log.WriteLine("IPv6 Client: Connecting.");
|
||||
Assert.True(completed.WaitOne(5000), "IPv6: Timed out while waiting for connection");
|
||||
|
||||
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
|
||||
Assert.NotNull(args.AcceptSocket);
|
||||
Assert.True(args.AcceptSocket.Connected, "IPv6 Accept Socket was not connected");
|
||||
Assert.NotNull(args.AcceptSocket.RemoteEndPoint);
|
||||
Assert.Equal(client.LocalEndPoint, args.AcceptSocket.RemoteEndPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void AcceptAsync_WithReceiveBuffer_Success()
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
AutoResetEvent accepted = new AutoResetEvent(false);
|
||||
|
||||
using (Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
int port = server.BindToAnonymousPort(IPAddress.Loopback);
|
||||
server.Listen(1);
|
||||
|
||||
const int acceptBufferOverheadSize = 288; // see https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.acceptasync(v=vs.110).aspx
|
||||
const int acceptBufferDataSize = 256;
|
||||
const int acceptBufferSize = acceptBufferOverheadSize + acceptBufferDataSize;
|
||||
|
||||
byte[] sendBuffer = new byte[acceptBufferDataSize];
|
||||
new Random().NextBytes(sendBuffer);
|
||||
|
||||
SocketAsyncEventArgs acceptArgs = new SocketAsyncEventArgs();
|
||||
acceptArgs.Completed += OnAcceptCompleted;
|
||||
acceptArgs.UserToken = accepted;
|
||||
acceptArgs.SetBuffer(new byte[acceptBufferSize], 0, acceptBufferSize);
|
||||
|
||||
Assert.True(server.AcceptAsync(acceptArgs));
|
||||
_log.WriteLine("IPv4 Server: Waiting for clients.");
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
client.Connect(IPAddress.Loopback, port);
|
||||
client.Send(sendBuffer);
|
||||
client.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
|
||||
Assert.True(
|
||||
accepted.WaitOne(TestSettings.PassingTestTimeout), "Test completed in alotted time");
|
||||
|
||||
Assert.Equal(
|
||||
SocketError.Success, acceptArgs.SocketError);
|
||||
|
||||
Assert.Equal(
|
||||
acceptBufferDataSize, acceptArgs.BytesTransferred);
|
||||
|
||||
Assert.Equal(
|
||||
new ArraySegment<byte>(sendBuffer),
|
||||
new ArraySegment<byte>(acceptArgs.Buffer, 0, acceptArgs.BytesTransferred));
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.AnyUnix)]
|
||||
public void AcceptAsync_WithReceiveBuffer_Failure()
|
||||
{
|
||||
//
|
||||
// Unix platforms don't yet support receiving data with AcceptAsync.
|
||||
//
|
||||
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
using (Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
int port = server.BindToAnonymousPort(IPAddress.Loopback);
|
||||
server.Listen(1);
|
||||
|
||||
SocketAsyncEventArgs acceptArgs = new SocketAsyncEventArgs();
|
||||
acceptArgs.Completed += OnAcceptCompleted;
|
||||
acceptArgs.UserToken = new ManualResetEvent(false);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
acceptArgs.SetBuffer(buffer, 0, buffer.Length);
|
||||
|
||||
Assert.Throws<PlatformNotSupportedException>(() => server.AcceptAsync(acceptArgs));
|
||||
}
|
||||
}
|
||||
|
||||
#region GC Finalizer test
|
||||
// This test assumes sequential execution of tests and that it is going to be executed after other tests
|
||||
// that used Sockets.
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void TestFinalizers()
|
||||
{
|
||||
// Making several passes through the FReachable list.
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
155
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/AgnosticListenerTest.cs
vendored
Normal file
155
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/AgnosticListenerTest.cs
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for AgnosticListenerTest
|
||||
/// </summary>
|
||||
public class AgnosticListenerTest
|
||||
{
|
||||
public AgnosticListenerTest(ITestOutputHelper _log)
|
||||
{
|
||||
Assert.True(Capability.IPv4Support() && Capability.IPv6Support());
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Create_Success()
|
||||
{
|
||||
// NOTE: the '0' below will cause the TcpListener to bind to an anonymous port.
|
||||
TcpListener listener = new TcpListener(IPAddress.IPv6Any, 0);
|
||||
listener.Server.DualMode = true;
|
||||
|
||||
listener.Start();
|
||||
listener.Stop();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void ConnectWithV4_Success()
|
||||
{
|
||||
int port;
|
||||
TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
|
||||
IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);
|
||||
|
||||
TcpClient client = new TcpClient(AddressFamily.InterNetwork);
|
||||
client.ConnectAsync(IPAddress.Loopback, port).GetAwaiter().GetResult();
|
||||
|
||||
TcpClient acceptedClient = listener.EndAcceptTcpClient(asyncResult);
|
||||
client.Dispose();
|
||||
acceptedClient.Dispose();
|
||||
listener.Stop();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void ConnectWithV6_Success()
|
||||
{
|
||||
int port;
|
||||
TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
|
||||
IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);
|
||||
|
||||
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
|
||||
client.ConnectAsync(IPAddress.IPv6Loopback, port).GetAwaiter().GetResult();
|
||||
|
||||
TcpClient acceptedClient = listener.EndAcceptTcpClient(asyncResult);
|
||||
client.Dispose();
|
||||
acceptedClient.Dispose();
|
||||
listener.Stop();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void ConnectWithV4AndV6_Success()
|
||||
{
|
||||
int port;
|
||||
TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
|
||||
IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);
|
||||
|
||||
TcpClient v6Client = new TcpClient(AddressFamily.InterNetworkV6);
|
||||
v6Client.ConnectAsync(IPAddress.IPv6Loopback, port).GetAwaiter().GetResult();
|
||||
|
||||
TcpClient acceptedV6Client = listener.EndAcceptTcpClient(asyncResult);
|
||||
Assert.Equal(AddressFamily.InterNetworkV6, acceptedV6Client.Client.RemoteEndPoint.AddressFamily);
|
||||
Assert.Equal(AddressFamily.InterNetworkV6, v6Client.Client.RemoteEndPoint.AddressFamily);
|
||||
|
||||
asyncResult = listener.BeginAcceptTcpClient(null, null);
|
||||
|
||||
TcpClient v4Client = new TcpClient(AddressFamily.InterNetwork);
|
||||
v4Client.ConnectAsync(IPAddress.Loopback, port).GetAwaiter().GetResult();
|
||||
|
||||
TcpClient acceptedV4Client = listener.EndAcceptTcpClient(asyncResult);
|
||||
Assert.Equal(AddressFamily.InterNetworkV6, acceptedV4Client.Client.RemoteEndPoint.AddressFamily);
|
||||
Assert.Equal(AddressFamily.InterNetwork, v4Client.Client.RemoteEndPoint.AddressFamily);
|
||||
|
||||
v6Client.Dispose();
|
||||
acceptedV6Client.Dispose();
|
||||
|
||||
v4Client.Dispose();
|
||||
acceptedV4Client.Dispose();
|
||||
|
||||
listener.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCreate_Success()
|
||||
{
|
||||
TcpListener listener = TcpListener.Create(0);
|
||||
|
||||
IPEndPoint ep = (IPEndPoint)listener.LocalEndpoint;
|
||||
Assert.Equal(ep.Address, IPAddress.IPv6Any);
|
||||
Assert.Equal(ep.Port, 0);
|
||||
Assert.True(listener.Server.DualMode);
|
||||
|
||||
listener.Start();
|
||||
listener.Stop();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
[InlineData(true, IPProtectionLevel.Unrestricted)]
|
||||
[InlineData(false, IPProtectionLevel.EdgeRestricted)]
|
||||
public void AllowNatTraversal_Windows(bool allow, IPProtectionLevel resultLevel)
|
||||
{
|
||||
var l = new TcpListener(IPAddress.Any, 0);
|
||||
l.AllowNatTraversal(allow);
|
||||
Assert.Equal((int)resultLevel, (int)l.Server.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.IPProtectionLevel));
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[PlatformSpecific(TestPlatforms.AnyUnix)]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public void AllowNatTraversal_AnyUnix(bool allow)
|
||||
{
|
||||
var l = new TcpListener(IPAddress.Any, 0);
|
||||
Assert.Throws<PlatformNotSupportedException>(() => l.AllowNatTraversal(allow));
|
||||
}
|
||||
|
||||
|
||||
#region GC Finalizer test
|
||||
// This test assumes sequential execution of tests and that it is going to be executed after other tests
|
||||
// that used Sockets.
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void TestFinalizers()
|
||||
{
|
||||
// Making several passes through the FReachable list.
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
1312
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ArgumentValidationTests.cs
vendored
Normal file
1312
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ArgumentValidationTests.cs
vendored
Normal file
File diff suppressed because it is too large
Load Diff
41
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Close.cs
vendored
Normal file
41
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Close.cs
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class CloseTests
|
||||
{
|
||||
[Fact]
|
||||
public static void Close()
|
||||
{
|
||||
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
s.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
public static void Close_Timeout(int timeout)
|
||||
{
|
||||
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
s.Close(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void Close_BadTimeout_Throws()
|
||||
{
|
||||
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => s.Close(-2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Configurations.props
vendored
Normal file
8
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Configurations.props
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<BuildConfigurations>
|
||||
netstandard;
|
||||
</BuildConfigurations>
|
||||
</PropertyGroup>
|
||||
</Project>
|
85
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ConnectAsync.cs
vendored
Normal file
85
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ConnectAsync.cs
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class ConnectAsync
|
||||
{
|
||||
private readonly ITestOutputHelper _log;
|
||||
|
||||
public ConnectAsync(ITestOutputHelper output)
|
||||
{
|
||||
_log = TestLogging.GetInstance();
|
||||
Assert.True(Capability.IPv4Support() || Capability.IPv6Support());
|
||||
}
|
||||
|
||||
public void OnConnectCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
EventWaitHandle handle = (EventWaitHandle)args.UserToken;
|
||||
handle.Set();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[Trait("IPv4", "true")]
|
||||
public void ConnectAsync_IPv4_Success(SocketImplementationType type)
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
|
||||
int port;
|
||||
using (SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port);
|
||||
args.Completed += OnConnectCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "IPv4: Timed out while waiting for connection");
|
||||
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[Trait("IPv6", "true")]
|
||||
public void ConnectAsync_IPv6_Success(SocketImplementationType type)
|
||||
{
|
||||
Assert.True(Capability.IPv6Support());
|
||||
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
|
||||
int port;
|
||||
using (SocketTestServer.SocketTestServerFactory(type, IPAddress.IPv6Loopback, out port))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, port);
|
||||
args.Completed += OnConnectCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "IPv6: Timed out while waiting for connection");
|
||||
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
94
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ConnectExTest.cs
vendored
Normal file
94
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ConnectExTest.cs
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class ConnectExTest
|
||||
{
|
||||
private readonly ITestOutputHelper _log;
|
||||
|
||||
public ConnectExTest(ITestOutputHelper output)
|
||||
{
|
||||
_log = TestLogging.GetInstance();
|
||||
}
|
||||
|
||||
private static void OnConnectAsyncCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
ManualResetEvent complete = (ManualResetEvent)args.UserToken;
|
||||
complete.Set();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[Trait("IPv4", "true")]
|
||||
[Trait("IPv6", "true")]
|
||||
public void ConnectEx_Success(SocketImplementationType type)
|
||||
{
|
||||
Assert.True(Capability.IPv4Support() && Capability.IPv6Support());
|
||||
|
||||
int port;
|
||||
SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);
|
||||
|
||||
int port6;
|
||||
SocketTestServer server6 = SocketTestServer.SocketTestServerFactory(type, IPAddress.IPv6Loopback, out port6);
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
try
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
Assert.True(sock.ConnectAsync(args));
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "IPv4: Timed out while waiting for connection");
|
||||
Assert.True(args.SocketError == SocketError.Success);
|
||||
|
||||
sock.Dispose();
|
||||
|
||||
sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, port6);
|
||||
complete.Reset();
|
||||
|
||||
Assert.True(sock.ConnectAsync(args));
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "IPv6: Timed out while waiting for connection");
|
||||
Assert.True(args.SocketError == SocketError.Success);
|
||||
}
|
||||
finally
|
||||
{
|
||||
sock.Dispose();
|
||||
|
||||
server.Dispose();
|
||||
server6.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#region GC Finalizer test
|
||||
// This test assumes sequential execution of tests and that it is going to be executed after other tests
|
||||
// that used Sockets.
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void TestFinalizers()
|
||||
{
|
||||
// Making several passes through the FReachable list.
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
92
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs
vendored
Normal file
92
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class CreateSocket
|
||||
{
|
||||
public static object[][] DualModeSuccessInputs = {
|
||||
new object[] { SocketType.Stream, ProtocolType.Tcp },
|
||||
new object[] { SocketType.Dgram, ProtocolType.Udp },
|
||||
};
|
||||
|
||||
public static object[][] DualModeFailureInputs = {
|
||||
new object[] { SocketType.Dgram, ProtocolType.Tcp },
|
||||
|
||||
new object[] { SocketType.Rdm, ProtocolType.Tcp },
|
||||
new object[] { SocketType.Seqpacket, ProtocolType.Tcp },
|
||||
new object[] { SocketType.Unknown, ProtocolType.Tcp },
|
||||
new object[] { SocketType.Rdm, ProtocolType.Udp },
|
||||
new object[] { SocketType.Seqpacket, ProtocolType.Udp },
|
||||
new object[] { SocketType.Stream, ProtocolType.Udp },
|
||||
new object[] { SocketType.Unknown, ProtocolType.Udp },
|
||||
/*
|
||||
Disabling these test cases because it actually passes in some cases
|
||||
see https://github.com/dotnet/corefx/issues/3726
|
||||
new object[] { SocketType.Raw, ProtocolType.Tcp },
|
||||
new object[] { SocketType.Raw, ProtocolType.Udp },
|
||||
*/
|
||||
};
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory, MemberData(nameof(DualModeSuccessInputs))]
|
||||
public void DualMode_Success(SocketType socketType, ProtocolType protocolType)
|
||||
{
|
||||
using (new Socket(socketType, protocolType))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory, MemberData(nameof(DualModeFailureInputs))]
|
||||
public void DualMode_Failure(SocketType socketType, ProtocolType protocolType)
|
||||
{
|
||||
Assert.Throws<SocketException>(() => new Socket(socketType, protocolType));
|
||||
}
|
||||
|
||||
public static object[][] CtorSuccessInputs = {
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp },
|
||||
new object[] { AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp },
|
||||
};
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory, MemberData(nameof(CtorSuccessInputs))]
|
||||
public void Ctor_Success(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
|
||||
{
|
||||
using (new Socket(addressFamily, socketType, protocolType))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static object[][] CtorFailureInputs = {
|
||||
new object[] { AddressFamily.Unknown, SocketType.Stream, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.Unknown, SocketType.Dgram, ProtocolType.Udp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Rdm, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Seqpacket, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Unknown, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Rdm, ProtocolType.Udp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Seqpacket, ProtocolType.Udp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Unknown, ProtocolType.Udp },
|
||||
/*
|
||||
Disabling these test cases because it actually passes in some cases
|
||||
see https://github.com/dotnet/corefx/issues/3726
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp },
|
||||
new object[] { AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Udp },
|
||||
*/
|
||||
};
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory, MemberData(nameof(CtorFailureInputs))]
|
||||
public void Ctor_Failure(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
|
||||
{
|
||||
Assert.Throws<SocketException>(() => new Socket(addressFamily, socketType, protocolType));
|
||||
}
|
||||
}
|
||||
}
|
163
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DisconnectTest.cs
vendored
Normal file
163
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DisconnectTest.cs
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class DisconnectTest
|
||||
{
|
||||
private readonly ITestOutputHelper _log;
|
||||
|
||||
public DisconnectTest(ITestOutputHelper output)
|
||||
{
|
||||
_log = TestLogging.GetInstance();
|
||||
Assert.True(Capability.IPv4Support() || Capability.IPv6Support());
|
||||
}
|
||||
public void OnCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
EventWaitHandle handle = (EventWaitHandle)args.UserToken;
|
||||
handle.Set();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[OuterLoop("Issue #11345")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Disconnect_Success()
|
||||
{
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
|
||||
IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.Completed += OnCompleted;
|
||||
args.UserToken = completed;
|
||||
args.RemoteEndPoint = server1.EndPoint;
|
||||
args.DisconnectReuseSocket = true;
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
|
||||
client.Disconnect(true);
|
||||
|
||||
args.RemoteEndPoint = server2.EndPoint;
|
||||
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[OuterLoop("Issue #11345")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void DisconnectAsync_Success()
|
||||
{
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
|
||||
IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.Completed += OnCompleted;
|
||||
args.UserToken = completed;
|
||||
args.RemoteEndPoint = server1.EndPoint;
|
||||
args.DisconnectReuseSocket = true;
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
|
||||
Assert.True(client.DisconnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
|
||||
args.RemoteEndPoint = server2.EndPoint;
|
||||
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[OuterLoop("Issue #11345")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void BeginDisconnect_Success()
|
||||
{
|
||||
AutoResetEvent completed = new AutoResetEvent(false);
|
||||
|
||||
IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.Completed += OnCompleted;
|
||||
args.UserToken = completed;
|
||||
args.RemoteEndPoint = server1.EndPoint;
|
||||
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
|
||||
client.EndDisconnect(client.BeginDisconnect(true, null, null));
|
||||
|
||||
args.RemoteEndPoint = server2.EndPoint;
|
||||
|
||||
Assert.True(client.ConnectAsync(args));
|
||||
completed.WaitOne();
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[PlatformSpecific(~TestPlatforms.Windows)]
|
||||
public void Disconnect_NonWindows_NotSupported()
|
||||
{
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.Throws<PlatformNotSupportedException>(() => client.Disconnect(true));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[PlatformSpecific(~TestPlatforms.Windows)]
|
||||
public void DisconnectAsync_NonWindows_NotSupported()
|
||||
{
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.DisconnectReuseSocket = true;
|
||||
Assert.Throws<PlatformNotSupportedException>(() => client.DisconnectAsync(args));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[PlatformSpecific(~TestPlatforms.Windows)]
|
||||
public void BeginDisconnect_NonWindows_NotSupported()
|
||||
{
|
||||
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
Assert.Throws<PlatformNotSupportedException>(() => client.BeginDisconnect(true, null, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
729
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.cs
vendored
Normal file
729
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DisposedSocketTests.cs
vendored
Normal file
File diff suppressed because it is too large
Load Diff
412
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DnsEndPointTest.cs
vendored
Normal file
412
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/DnsEndPointTest.cs
vendored
Normal file
@@ -0,0 +1,412 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class DnsEndPointTest
|
||||
{
|
||||
// Port 8 is unassigned as per https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
|
||||
private const int UnusedPort = 8;
|
||||
|
||||
private readonly ITestOutputHelper _log;
|
||||
|
||||
public DnsEndPointTest(ITestOutputHelper output)
|
||||
{
|
||||
_log = TestLogging.GetInstance();
|
||||
}
|
||||
|
||||
private void OnConnectAsyncCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
ManualResetEvent complete = (ManualResetEvent)args.UserToken;
|
||||
complete.Set();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_ConnectDnsEndPoint_Success(SocketImplementationType type)
|
||||
{
|
||||
int port;
|
||||
SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
sock.Connect(new DnsEndPoint("localhost", port));
|
||||
|
||||
sock.Dispose();
|
||||
server.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_ConnectDnsEndPoint_Failure()
|
||||
{
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
SocketException ex = Assert.ThrowsAny<SocketException>(() =>
|
||||
{
|
||||
sock.Connect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort));
|
||||
});
|
||||
|
||||
SocketError errorCode = ex.SocketErrorCode;
|
||||
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
|
||||
"SocketErrorCode: {0}" + errorCode);
|
||||
|
||||
ex = Assert.ThrowsAny<SocketException>(() =>
|
||||
{
|
||||
sock.Connect(new DnsEndPoint("localhost", UnusedPort));
|
||||
});
|
||||
|
||||
Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_SendToDnsEndPoint_ArgumentException()
|
||||
{
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
sock.SendTo(new byte[10], new DnsEndPoint("localhost", UnusedPort));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_ReceiveFromDnsEndPoint_ArgumentException()
|
||||
{
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = sock.BindToAnonymousPort(IPAddress.Loopback);
|
||||
EndPoint endpoint = new DnsEndPoint("localhost", port);
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
sock.ReceiveFrom(new byte[10], ref endpoint);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_BeginConnectDnsEndPoint_Success(SocketImplementationType type)
|
||||
{
|
||||
int port;
|
||||
SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", port), null, null);
|
||||
sock.EndConnect(result);
|
||||
|
||||
sock.Dispose();
|
||||
server.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_BeginConnectDnsEndPoint_Failure()
|
||||
{
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
SocketException ex = Assert.ThrowsAny<SocketException>(() =>
|
||||
{
|
||||
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort), null, null);
|
||||
sock.EndConnect(result);
|
||||
});
|
||||
|
||||
SocketError errorCode = ex.SocketErrorCode;
|
||||
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
|
||||
"SocketErrorCode: {0}" + errorCode);
|
||||
|
||||
ex = Assert.ThrowsAny<SocketException>(() =>
|
||||
{
|
||||
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", UnusedPort), null, null);
|
||||
sock.EndConnect(result);
|
||||
});
|
||||
|
||||
Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_BeginSendToDnsEndPoint_ArgumentException()
|
||||
{
|
||||
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
sock.BeginSendTo(new byte[10], 0, 0, SocketFlags.None, new DnsEndPoint("localhost", UnusedPort), null, null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[Trait("IPv4", "true")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_ConnectAsyncDnsEndPoint_Success(SocketImplementationType type)
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
int port;
|
||||
SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("localhost", port);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
bool willRaiseEvent = sock.ConnectAsync(args);
|
||||
if (willRaiseEvent)
|
||||
{
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
}
|
||||
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
Assert.Null(args.ConnectByNameError);
|
||||
|
||||
complete.Dispose();
|
||||
sock.Dispose();
|
||||
server.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[Trait("IPv4", "true")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_ConnectAsyncDnsEndPoint_HostNotFound()
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
bool willRaiseEvent = sock.ConnectAsync(args);
|
||||
if (willRaiseEvent)
|
||||
{
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
}
|
||||
|
||||
AssertHostNotFoundOrNoData(args);
|
||||
|
||||
complete.Dispose();
|
||||
sock.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[Trait("IPv4", "true")]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public void Socket_ConnectAsyncDnsEndPoint_ConnectionRefused()
|
||||
{
|
||||
Assert.True(Capability.IPv4Support());
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("localhost", UnusedPort);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
bool willRaiseEvent = sock.ConnectAsync(args);
|
||||
if (willRaiseEvent)
|
||||
{
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
}
|
||||
|
||||
Assert.Equal(SocketError.ConnectionRefused, args.SocketError);
|
||||
Assert.True(args.ConnectByNameError is SocketException);
|
||||
Assert.Equal(SocketError.ConnectionRefused, ((SocketException)args.ConnectByNameError).SocketErrorCode);
|
||||
|
||||
complete.Dispose();
|
||||
sock.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(SocketImplementationType.APM)]
|
||||
[InlineData(SocketImplementationType.Async)]
|
||||
[Trait("IPv4", "true")]
|
||||
[Trait("IPv6", "true")]
|
||||
[ActiveIssue(4002, TestPlatforms.AnyUnix)]
|
||||
public void Socket_StaticConnectAsync_Success(SocketImplementationType type)
|
||||
{
|
||||
Assert.True(Capability.IPv4Support() && Capability.IPv6Support());
|
||||
|
||||
int port4, port6;
|
||||
SocketTestServer server4 = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port4);
|
||||
SocketTestServer server6 = SocketTestServer.SocketTestServerFactory(type, IPAddress.IPv6Loopback, out port6);
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("localhost", port4);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
Assert.True(Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args));
|
||||
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
Assert.Null(args.ConnectByNameError);
|
||||
Assert.NotNull(args.ConnectSocket);
|
||||
Assert.True(args.ConnectSocket.AddressFamily == AddressFamily.InterNetwork);
|
||||
Assert.True(args.ConnectSocket.Connected);
|
||||
|
||||
args.ConnectSocket.Dispose();
|
||||
|
||||
args.RemoteEndPoint = new DnsEndPoint("localhost", port6);
|
||||
complete.Reset();
|
||||
|
||||
Assert.True(Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args));
|
||||
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
|
||||
Assert.Equal(SocketError.Success, args.SocketError);
|
||||
Assert.Null(args.ConnectByNameError);
|
||||
Assert.NotNull(args.ConnectSocket);
|
||||
Assert.True(args.ConnectSocket.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
Assert.True(args.ConnectSocket.Connected);
|
||||
|
||||
args.ConnectSocket.Dispose();
|
||||
|
||||
server4.Dispose();
|
||||
server6.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_StaticConnectAsync_HostNotFound()
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
bool willRaiseEvent = Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args);
|
||||
if (!willRaiseEvent)
|
||||
{
|
||||
OnConnectAsyncCompleted(null, args);
|
||||
}
|
||||
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
|
||||
AssertHostNotFoundOrNoData(args);
|
||||
|
||||
Assert.Null(args.ConnectSocket);
|
||||
|
||||
complete.Dispose();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_StaticConnectAsync_ConnectionRefused()
|
||||
{
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("localhost", UnusedPort);
|
||||
args.Completed += OnConnectAsyncCompleted;
|
||||
|
||||
ManualResetEvent complete = new ManualResetEvent(false);
|
||||
args.UserToken = complete;
|
||||
|
||||
bool willRaiseEvent = Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args);
|
||||
if (!willRaiseEvent)
|
||||
{
|
||||
OnConnectAsyncCompleted(null, args);
|
||||
}
|
||||
|
||||
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
|
||||
|
||||
Assert.Equal(SocketError.ConnectionRefused, args.SocketError);
|
||||
Assert.True(args.ConnectByNameError is SocketException);
|
||||
Assert.Equal(SocketError.ConnectionRefused, ((SocketException)args.ConnectByNameError).SocketErrorCode);
|
||||
Assert.Null(args.ConnectSocket);
|
||||
|
||||
complete.Dispose();
|
||||
}
|
||||
|
||||
public void CallbackThatShouldNotBeCalled(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
throw new ShouldNotBeInvokedException();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[Trait("IPv6", "true")]
|
||||
public void Socket_StaticConnectAsync_SyncFailure()
|
||||
{
|
||||
Assert.True(Capability.IPv6Support()); // IPv6 required because we use AF.InterNetworkV6
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new DnsEndPoint("127.0.0.1", UnusedPort, AddressFamily.InterNetworkV6);
|
||||
args.Completed += CallbackThatShouldNotBeCalled;
|
||||
|
||||
Assert.False(Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args));
|
||||
|
||||
Assert.Equal(SocketError.NoData, args.SocketError);
|
||||
Assert.Null(args.ConnectSocket);
|
||||
}
|
||||
|
||||
private static void AssertHostNotFoundOrNoData(SocketAsyncEventArgs args)
|
||||
{
|
||||
SocketError errorCode = args.SocketError;
|
||||
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
|
||||
"SocketError: " + errorCode);
|
||||
|
||||
Assert.True(args.ConnectByNameError is SocketException);
|
||||
errorCode = ((SocketException)args.ConnectByNameError).SocketErrorCode;
|
||||
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
|
||||
"SocketError: " + errorCode);
|
||||
}
|
||||
|
||||
#region GC Finalizer test
|
||||
// This test assumes sequential execution of tests and that it is going to be executed after other tests
|
||||
// that used Sockets.
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void TestFinalizers()
|
||||
{
|
||||
// Making several passes through the FReachable list.
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
0e069ad23d843bacbda6500a150dc1333799deaf
|
31
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Handle.cs
vendored
Normal file
31
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/Handle.cs
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class HandleTests
|
||||
{
|
||||
[Fact]
|
||||
public static void ValidHandle_NotNegativeOne()
|
||||
{
|
||||
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
Assert.NotEqual((IntPtr)(-1), s.Handle);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.Windows)]
|
||||
public static void ValidHandle_NotZero()
|
||||
{
|
||||
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
Assert.NotEqual(IntPtr.Zero, s.Handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
87
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/IPPacketInformationTest.cs
vendored
Normal file
87
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/IPPacketInformationTest.cs
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class IPPacketInformationTest
|
||||
{
|
||||
[Fact]
|
||||
public void Equals_DefaultValues_Success()
|
||||
{
|
||||
Assert.Equal(default(IPPacketInformation), default(IPPacketInformation));
|
||||
Assert.True(default(IPPacketInformation) == default(IPPacketInformation));
|
||||
Assert.False(default(IPPacketInformation) != default(IPPacketInformation));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_DefaultValues_Success()
|
||||
{
|
||||
Assert.Equal(default(IPPacketInformation).GetHashCode(), default(IPPacketInformation).GetHashCode());
|
||||
}
|
||||
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Equals_NonDefaultValue_Success()
|
||||
{
|
||||
IPPacketInformation packetInfo = GetNonDefaultIPPacketInformation();
|
||||
IPPacketInformation packetInfoCopy = packetInfo;
|
||||
|
||||
Assert.Equal(packetInfo, packetInfoCopy);
|
||||
Assert.True(packetInfo == packetInfoCopy);
|
||||
Assert.False(packetInfo != packetInfoCopy);
|
||||
|
||||
Assert.NotEqual(packetInfo, default(IPPacketInformation));
|
||||
Assert.False(packetInfo == default(IPPacketInformation));
|
||||
Assert.True(packetInfo != default(IPPacketInformation));
|
||||
}
|
||||
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void GetHashCode_NonDefaultValue_Succes()
|
||||
{
|
||||
IPPacketInformation packetInfo = GetNonDefaultIPPacketInformation();
|
||||
|
||||
Assert.Equal(packetInfo.GetHashCode(), packetInfo.GetHashCode());
|
||||
}
|
||||
|
||||
private IPPacketInformation GetNonDefaultIPPacketInformation()
|
||||
{
|
||||
const int ReceiveTimeout = 5000;
|
||||
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
var waitHandle = new ManualResetEvent(false);
|
||||
|
||||
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
|
||||
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
|
||||
UserToken = waitHandle
|
||||
};
|
||||
|
||||
receiveArgs.SetBuffer(new byte[1], 0, 1);
|
||||
receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();
|
||||
|
||||
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));
|
||||
|
||||
// Send a few packets, in case they aren't delivered reliably.
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
|
||||
}
|
||||
|
||||
Assert.True(waitHandle.WaitOne(ReceiveTimeout));
|
||||
|
||||
return receiveArgs.ReceiveMessageFromPacketInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
105
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/LingerStateTest.cs
vendored
Normal file
105
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/LingerStateTest.cs
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Threading;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class LingerStateTest
|
||||
{
|
||||
private void TestLingerState_Success(Socket sock, bool enabled, int lingerTime)
|
||||
{
|
||||
sock.LingerState = new LingerOption(enabled, lingerTime);
|
||||
|
||||
Assert.Equal<bool>(enabled, sock.LingerState.Enabled);
|
||||
Assert.Equal<int>(lingerTime, sock.LingerState.LingerTime);
|
||||
}
|
||||
|
||||
private void TestLingerState_ArgumentException(Socket sock, bool enabled, int lingerTime)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
sock.LingerState = new LingerOption(enabled, lingerTime);
|
||||
});
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public void Socket_LingerState_Common_Boundaries_CorrectBehavior()
|
||||
{
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
Assert.False(sock.LingerState.Enabled, "Linger was turned on by default!");
|
||||
Assert.Equal<int>(sock.LingerState.LingerTime, 0);
|
||||
|
||||
TestLingerState_ArgumentException(sock, true, -1);
|
||||
|
||||
TestLingerState_Success(sock, true, 0);
|
||||
TestLingerState_Success(sock, true, 120);
|
||||
|
||||
TestLingerState_ArgumentException(sock, true, UInt16.MaxValue + 1);
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(~TestPlatforms.OSX)]
|
||||
public void Socket_LingerState_Upper_Boundaries_CorrectBehavior()
|
||||
{
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
TestLingerState_Success(sock, true, Int16.MaxValue);
|
||||
TestLingerState_Success(sock, true, Int16.MaxValue + 1);
|
||||
TestLingerState_Success(sock, true, UInt16.MaxValue);
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
[PlatformSpecific(TestPlatforms.OSX)]
|
||||
public void Socket_LingerState_Upper_Boundaries_CorrectBehavior_OSX()
|
||||
{
|
||||
// The upper bound for linger time is drastically different on OS X.
|
||||
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
Assert.Throws<SocketException>(() =>
|
||||
{
|
||||
sock.LingerState = new LingerOption(true, Int16.MaxValue);
|
||||
});
|
||||
|
||||
Assert.Throws<SocketException>(() =>
|
||||
{
|
||||
sock.LingerState = new LingerOption(true, Int16.MaxValue + 1);
|
||||
});
|
||||
|
||||
Assert.Throws<SocketException>(() =>
|
||||
{
|
||||
sock.LingerState = new LingerOption(true, UInt16.MaxValue);
|
||||
});
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[InlineData(false, 0)]
|
||||
[InlineData(true, 0)]
|
||||
[InlineData(true, 1)]
|
||||
public void SetLingerAfterServerClosed(bool linger, int timeout)
|
||||
{
|
||||
using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
int port = server.BindToAnonymousPort(IPAddress.Loopback);
|
||||
server.Listen(1);
|
||||
|
||||
var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
{
|
||||
client.Connect(IPAddress.Loopback, port);
|
||||
|
||||
server.Dispose();
|
||||
Thread.Sleep(10); // give the server socket time to close
|
||||
|
||||
client.LingerState = new LingerOption(linger, timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
47
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/LoggingTest.cs
vendored
Normal file
47
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/LoggingTest.cs
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.Tracing;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class LoggingTest : RemoteExecutorTestBase
|
||||
{
|
||||
[Fact]
|
||||
public static void EventSource_ExistsWithCorrectId()
|
||||
{
|
||||
Type esType = typeof(Socket).Assembly.GetType("System.Net.NetEventSource", throwOnError: true, ignoreCase: false);
|
||||
Assert.NotNull(esType);
|
||||
|
||||
Assert.Equal("Microsoft-System-Net-Sockets", EventSource.GetName(esType));
|
||||
Assert.Equal(Guid.Parse("e03c0352-f9c9-56ff-0ea7-b94ba8cabc6b"), EventSource.GetGuid(esType));
|
||||
|
||||
Assert.NotEmpty(EventSource.GenerateManifest(esType, esType.Assembly.Location));
|
||||
}
|
||||
|
||||
[OuterLoop]
|
||||
[Fact]
|
||||
public void EventSource_EventsRaisedAsExpected()
|
||||
{
|
||||
RemoteInvoke(() =>
|
||||
{
|
||||
using (var listener = new TestEventListener("Microsoft-System-Net-Sockets", EventLevel.Verbose))
|
||||
{
|
||||
var events = new ConcurrentQueue<EventWrittenEventArgs>();
|
||||
listener.RunWithCallback(events.Enqueue, () =>
|
||||
{
|
||||
// Invoke a test that'll cause some events to be generated
|
||||
new NetworkStreamTest().CopyToAsync_AllDataCopied(4096).GetAwaiter().GetResult();
|
||||
});
|
||||
Assert.DoesNotContain(events, ev => ev.EventId == 0); // errors from the EventSource itself
|
||||
Assert.InRange(events.Count, 1, int.MaxValue);
|
||||
}
|
||||
return SuccessExitCode;
|
||||
}).Dispose();
|
||||
}
|
||||
}
|
||||
}
|
130
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/NetworkStreamTest.cs
vendored
Normal file
130
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/NetworkStreamTest.cs
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class NetworkStreamTest
|
||||
{
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Theory]
|
||||
[MemberData(nameof(NonCanceledTokens))]
|
||||
public async Task ReadWriteAsync_NonCanceled_Success(CancellationToken nonCanceledToken)
|
||||
{
|
||||
await RunWithConnectedNetworkStreamsAsync(async (server, client) =>
|
||||
{
|
||||
var clientData = new byte[] { 42 };
|
||||
await client.WriteAsync(clientData, 0, clientData.Length, nonCanceledToken);
|
||||
|
||||
var serverData = new byte[clientData.Length];
|
||||
Assert.Equal(serverData.Length, await server.ReadAsync(serverData, 0, serverData.Length, nonCanceledToken));
|
||||
|
||||
Assert.Equal(clientData, serverData);
|
||||
});
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[Fact]
|
||||
public async Task ReadWriteAsync_Canceled_ThrowsOperationCanceledException()
|
||||
{
|
||||
await RunWithConnectedNetworkStreamsAsync(async (server, client) =>
|
||||
{
|
||||
var canceledToken = new CancellationToken(canceled: true);
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => client.WriteAsync(new byte[1], 0, 1, canceledToken));
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => server.ReadAsync(new byte[1], 0, 1, canceledToken));
|
||||
});
|
||||
}
|
||||
|
||||
public static object[][] NonCanceledTokens = new object[][]
|
||||
{
|
||||
new object[] { CancellationToken.None }, // CanBeCanceled == false
|
||||
new object[] { new CancellationTokenSource().Token } // CanBeCanceled == true
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
[InlineData(1024)]
|
||||
[InlineData(4096)]
|
||||
[InlineData(4095)]
|
||||
[InlineData(1024*1024)]
|
||||
public async Task CopyToAsync_AllDataCopied(int byteCount)
|
||||
{
|
||||
await RunWithConnectedNetworkStreamsAsync(async (server, client) =>
|
||||
{
|
||||
var results = new MemoryStream();
|
||||
byte[] dataToCopy = new byte[byteCount];
|
||||
new Random().NextBytes(dataToCopy);
|
||||
|
||||
Task copyTask = client.CopyToAsync(results);
|
||||
await server.WriteAsync(dataToCopy, 0, dataToCopy.Length);
|
||||
server.Dispose();
|
||||
await copyTask;
|
||||
|
||||
Assert.Equal(dataToCopy, results.ToArray());
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CopyToAsync_InvalidArguments_Throws()
|
||||
{
|
||||
await RunWithConnectedNetworkStreamsAsync((stream, _) =>
|
||||
{
|
||||
// Null destination
|
||||
Assert.Throws<ArgumentNullException>("destination", () => { stream.CopyToAsync(null); });
|
||||
|
||||
// Buffer size out-of-range
|
||||
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { stream.CopyToAsync(new MemoryStream(), 0); });
|
||||
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { stream.CopyToAsync(new MemoryStream(), -1, CancellationToken.None); });
|
||||
|
||||
// Copying to non-writable stream
|
||||
Assert.Throws<NotSupportedException>(() => { stream.CopyToAsync(new MemoryStream(new byte[0], writable: false)); });
|
||||
|
||||
// Copying after disposing the stream
|
||||
stream.Dispose();
|
||||
Assert.Throws<ObjectDisposedException>(() => { stream.CopyToAsync(new MemoryStream()); });
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a pair of connected NetworkStreams and invokes the provided <paramref name="func"/>
|
||||
/// with them as arguments.
|
||||
/// </summary>
|
||||
private static async Task RunWithConnectedNetworkStreamsAsync(Func<NetworkStream, NetworkStream, Task> func)
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
try
|
||||
{
|
||||
listener.Start(1);
|
||||
var clientEndpoint = (IPEndPoint)listener.LocalEndpoint;
|
||||
|
||||
using (var client = new TcpClient(clientEndpoint.AddressFamily))
|
||||
{
|
||||
Task<TcpClient> remoteTask = listener.AcceptTcpClientAsync();
|
||||
Task clientConnectTask = client.ConnectAsync(clientEndpoint.Address, clientEndpoint.Port);
|
||||
|
||||
await Task.WhenAll(remoteTask, clientConnectTask);
|
||||
|
||||
using (TcpClient remote = remoteTask.Result)
|
||||
using (NetworkStream serverStream = remote.GetStream())
|
||||
using (NetworkStream clientStream = client.GetStream())
|
||||
{
|
||||
await func(serverStream, clientStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
listener.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
151
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs
vendored
Normal file
151
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs
vendored
Normal file
@@ -0,0 +1,151 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class ReceiveMessageFrom
|
||||
{
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success()
|
||||
{
|
||||
if (Socket.OSSupportsIPv4)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
|
||||
}
|
||||
|
||||
IPPacketInformation packetInformation;
|
||||
SocketFlags flags = SocketFlags.None;
|
||||
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
int len = receiver.ReceiveMessageFrom(new byte[1024], 0, 1024, ref flags, ref remoteEP, out packetInformation);
|
||||
|
||||
Assert.Equal(1024, len);
|
||||
Assert.Equal(sender.LocalEndPoint, remoteEP);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, packetInformation.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success_IPv6()
|
||||
{
|
||||
if (Socket.OSSupportsIPv6)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.IPv6Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));
|
||||
}
|
||||
|
||||
IPPacketInformation packetInformation;
|
||||
SocketFlags flags = SocketFlags.None;
|
||||
EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
|
||||
|
||||
int len = receiver.ReceiveMessageFrom(new byte[1024], 0, 1024, ref flags, ref remoteEP, out packetInformation);
|
||||
|
||||
Assert.Equal(1024, len);
|
||||
Assert.Equal(sender.LocalEndPoint, remoteEP);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, packetInformation.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success_APM()
|
||||
{
|
||||
if (Socket.OSSupportsIPv4)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
|
||||
}
|
||||
|
||||
IPPacketInformation packetInformation;
|
||||
SocketFlags flags = SocketFlags.None;
|
||||
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
IAsyncResult ar = receiver.BeginReceiveMessageFrom(new byte[1024], 0, 1024, flags, ref remoteEP, null, null);
|
||||
ar.AsyncWaitHandle.WaitOne();
|
||||
int len = receiver.EndReceiveMessageFrom(ar, ref flags, ref remoteEP, out packetInformation);
|
||||
|
||||
Assert.Equal(1024, len);
|
||||
Assert.Equal(sender.LocalEndPoint, remoteEP);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, packetInformation.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success_APM_IPv6()
|
||||
{
|
||||
if (Socket.OSSupportsIPv6)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.IPv6Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));
|
||||
}
|
||||
|
||||
IPPacketInformation packetInformation;
|
||||
SocketFlags flags = SocketFlags.None;
|
||||
EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
|
||||
|
||||
IAsyncResult ar = receiver.BeginReceiveMessageFrom(new byte[1024], 0, 1024, flags, ref remoteEP, null, null);
|
||||
ar.AsyncWaitHandle.WaitOne();
|
||||
int len = receiver.EndReceiveMessageFrom(ar, ref flags, ref remoteEP, out packetInformation);
|
||||
|
||||
Assert.Equal(1024, len);
|
||||
Assert.Equal(sender.LocalEndPoint, remoteEP);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, packetInformation.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
108
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFromAsync.cs
vendored
Normal file
108
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFromAsync.cs
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Net.Test.Common;
|
||||
using System.Threading;
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class ReceiveMessageFromAsync
|
||||
{
|
||||
public void OnCompleted(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
EventWaitHandle handle = (EventWaitHandle)args.UserToken;
|
||||
handle.Set();
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success()
|
||||
{
|
||||
ManualResetEvent completed = new ManualResetEvent(false);
|
||||
|
||||
if (Socket.OSSupportsIPv4)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
|
||||
}
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
args.SetBuffer(new byte[1024], 0, 1024);
|
||||
args.Completed += OnCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
bool pending = receiver.ReceiveMessageFromAsync(args);
|
||||
if (!pending)
|
||||
{
|
||||
OnCompleted(null, args);
|
||||
}
|
||||
|
||||
Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
|
||||
|
||||
Assert.Equal(1024, args.BytesTransferred);
|
||||
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[OuterLoop] // TODO: Issue #11345
|
||||
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/987
|
||||
public void Success_IPv6()
|
||||
{
|
||||
ManualResetEvent completed = new ManualResetEvent(false);
|
||||
|
||||
if (Socket.OSSupportsIPv6)
|
||||
{
|
||||
using (Socket receiver = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int port = receiver.BindToAnonymousPort(IPAddress.IPv6Loopback);
|
||||
receiver.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
|
||||
|
||||
Socket sender = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||
sender.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));
|
||||
}
|
||||
|
||||
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
||||
args.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
|
||||
args.SetBuffer(new byte[1024], 0, 1024);
|
||||
args.Completed += OnCompleted;
|
||||
args.UserToken = completed;
|
||||
|
||||
bool pending = receiver.ReceiveMessageFromAsync(args);
|
||||
if (!pending)
|
||||
{
|
||||
OnCompleted(null, args);
|
||||
}
|
||||
|
||||
Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
|
||||
|
||||
Assert.Equal(1024, args.BytesTransferred);
|
||||
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
|
||||
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
|
||||
|
||||
sender.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
337
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/SelectAndPollTests.cs
vendored
Normal file
337
external/corefx/src/System.Net.Sockets/tests/FunctionalTests/SelectAndPollTests.cs
vendored
Normal file
@@ -0,0 +1,337 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Test.Common;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace System.Net.Sockets.Tests
|
||||
{
|
||||
public class SelectAndPollTests
|
||||
{
|
||||
const int SelectTimeout = 100;
|
||||
const int SelectSuccessTimeoutMicroseconds = 5*1000*1000; // 5 seconds
|
||||
|
||||
[Fact]
|
||||
public void SelectNone_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => Socket.Select(null, null, null, SelectSuccessTimeoutMicroseconds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Select_Read_NotASocket_Throws()
|
||||
{
|
||||
var list = new List<object> { new object() };
|
||||
Assert.Throws<ArgumentException>(() => Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectRead_Single_Success()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int receiverPort = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
var receiverEndpoint = new IPEndPoint(IPAddress.Loopback, receiverPort);
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);
|
||||
}
|
||||
|
||||
var list = new List<Socket> { receiver };
|
||||
Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds);
|
||||
|
||||
Assert.Equal(1, list.Count);
|
||||
Assert.Equal(receiver, list[0]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectRead_Single_Timeout()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
var list = new List<Socket> { receiver };
|
||||
Socket.Select(list, null, null, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectRead_Multiple_Success()
|
||||
{
|
||||
using (var firstReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var secondReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int firstReceiverPort = firstReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
var firstReceiverEndpoint = new IPEndPoint(IPAddress.Loopback, firstReceiverPort);
|
||||
|
||||
int secondReceiverPort = secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
var secondReceiverEndpoint = new IPEndPoint(IPAddress.Loopback, secondReceiverPort);
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1], SocketFlags.None, firstReceiverEndpoint);
|
||||
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
Assert.True(SpinWait.SpinUntil(() =>
|
||||
{
|
||||
var list = new List<Socket> { firstReceiver, secondReceiver };
|
||||
Socket.Select(list, null, null, Math.Max((int)(SelectSuccessTimeoutMicroseconds - (sw.Elapsed.TotalSeconds * 1000000)), 0));
|
||||
Assert.True(list.Count <= 2);
|
||||
if (list.Count == 2)
|
||||
{
|
||||
Assert.Equal(firstReceiver, list[0]);
|
||||
Assert.Equal(secondReceiver, list[1]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}, SelectSuccessTimeoutMicroseconds / 1000), "Failed to select both items within allotted time");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectRead_Multiple_Timeout()
|
||||
{
|
||||
using (var firstReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var secondReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
firstReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
var list = new List<Socket> { firstReceiver, secondReceiver };
|
||||
Socket.Select(list, null, null, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectRead_Multiple_Mixed()
|
||||
{
|
||||
using (var firstReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var secondReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
firstReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
int secondReceiverPort = secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
var secondReceiverEndpoint = new IPEndPoint(IPAddress.Loopback, secondReceiverPort);
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);
|
||||
}
|
||||
|
||||
var list = new List<Socket> { firstReceiver, secondReceiver };
|
||||
Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds);
|
||||
|
||||
Assert.Equal(1, list.Count);
|
||||
Assert.Equal(secondReceiver, list[0]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Select_Write_NotASocket_Throws()
|
||||
{
|
||||
var list = new List<object> { new object() };
|
||||
Assert.Throws<ArgumentException>(() => Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWrite_Single_Success()
|
||||
{
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
var list = new List<Socket> { sender };
|
||||
Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds);
|
||||
|
||||
Assert.Equal(1, list.Count);
|
||||
Assert.Equal(sender, list[0]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWrite_Single_Timeout()
|
||||
{
|
||||
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
listener.BindToAnonymousPort(IPAddress.Loopback);
|
||||
listener.Listen(1);
|
||||
listener.AcceptAsync();
|
||||
|
||||
var list = new List<Socket> { listener };
|
||||
Socket.Select(null, list, null, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWrite_Multiple_Success()
|
||||
{
|
||||
using (var firstSender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var secondSender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
var list = new List<Socket> { firstSender, secondSender };
|
||||
Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds);
|
||||
|
||||
Assert.Equal(2, list.Count);
|
||||
Assert.Equal(firstSender, list[0]);
|
||||
Assert.Equal(secondSender, list[1]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWrite_Multiple_Timeout()
|
||||
{
|
||||
using (var firstListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
using (var secondListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
firstListener.BindToAnonymousPort(IPAddress.Loopback);
|
||||
firstListener.Listen(1);
|
||||
firstListener.AcceptAsync();
|
||||
|
||||
secondListener.BindToAnonymousPort(IPAddress.Loopback);
|
||||
secondListener.Listen(1);
|
||||
secondListener.AcceptAsync();
|
||||
|
||||
var list = new List<Socket> { firstListener, secondListener };
|
||||
Socket.Select(null, list, null, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWrite_Multiple_Mixed()
|
||||
{
|
||||
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
listener.BindToAnonymousPort(IPAddress.Loopback);
|
||||
listener.Listen(1);
|
||||
listener.AcceptAsync();
|
||||
|
||||
var list = new List<Socket> { listener, sender };
|
||||
Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds);
|
||||
|
||||
Assert.Equal(1, list.Count);
|
||||
Assert.Equal(sender, list[0]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Select_Error_NotASocket_Throws()
|
||||
{
|
||||
var list = new List<object> { new object() };
|
||||
Assert.Throws<ArgumentException>(() => Socket.Select(null, null, list, SelectSuccessTimeoutMicroseconds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectError_Single_Timeout()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
var list = new List<Socket> { receiver };
|
||||
Socket.Select(null, null, list, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectError_Multiple_Timeout()
|
||||
{
|
||||
using (var firstReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var secondReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
firstReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
var list = new List<Socket> { firstReceiver, secondReceiver };
|
||||
Socket.Select(null, null, list, SelectTimeout);
|
||||
|
||||
Assert.Equal(0, list.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PollRead_Single_Success()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
int receiverPort = receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
var receiverEndpoint = new IPEndPoint(IPAddress.Loopback, receiverPort);
|
||||
|
||||
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
|
||||
{
|
||||
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);
|
||||
}
|
||||
|
||||
Assert.True(receiver.Poll(SelectSuccessTimeoutMicroseconds, SelectMode.SelectRead));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PollRead_Single_Timeout()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
Assert.False(receiver.Poll(SelectTimeout, SelectMode.SelectRead));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PollWrite_Single_Success()
|
||||
{
|
||||
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
Assert.True(sender.Poll(SelectSuccessTimeoutMicroseconds, SelectMode.SelectWrite));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PollWrite_Single_Timeout()
|
||||
{
|
||||
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
listener.BindToAnonymousPort(IPAddress.Loopback);
|
||||
listener.Listen(1);
|
||||
listener.AcceptAsync();
|
||||
|
||||
Assert.False(listener.Poll(SelectTimeout, SelectMode.SelectWrite));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PollError_Single_Timeout()
|
||||
{
|
||||
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
receiver.BindToAnonymousPort(IPAddress.Loopback);
|
||||
|
||||
Assert.False(receiver.Poll(SelectTimeout, SelectMode.SelectError));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user