Imported Upstream version 5.8.0.22

Former-commit-id: df344e34b07851d296efb3e6604c8db42b6f7aa3
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-10-19 20:04:20 +00:00
parent 5f4a27cc8a
commit 7d05485754
5020 changed files with 114082 additions and 186061 deletions

View File

@@ -288,7 +288,7 @@ namespace System.Net.Sockets.Tests
byte[] buffer = new byte[1];
acceptArgs.SetBuffer(buffer, 0, buffer.Length);
Assert.Throws<ArgumentException>(() => server.AcceptAsync(acceptArgs));
AssertExtensions.Throws<ArgumentException>(null, () => server.AcceptAsync(acceptArgs));
}
}

View File

@@ -200,7 +200,7 @@ namespace System.Net.Sockets.Tests
[Fact]
public void Connect_IPAddresses_EmptyArray_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().Connect(new IPAddress[0], 1));
AssertExtensions.Throws<ArgumentException>("addresses", () => GetSocket().Connect(new IPAddress[0], 1));
}
[Fact]
@@ -261,7 +261,7 @@ namespace System.Net.Sockets.Tests
public void Send_Buffers_EmptyBuffers_Throws_Argument()
{
SocketError errorCode;
Assert.Throws<ArgumentException>(() => GetSocket().Send(new List<ArraySegment<byte>>(), SocketFlags.None, out errorCode));
AssertExtensions.Throws<ArgumentException>("buffers", () => GetSocket().Send(new List<ArraySegment<byte>>(), SocketFlags.None, out errorCode));
}
[Fact]
@@ -328,7 +328,7 @@ namespace System.Net.Sockets.Tests
public void Receive_Buffers_EmptyBuffers_Throws_Argument()
{
SocketError errorCode;
Assert.Throws<ArgumentException>(() => GetSocket().Receive(new List<ArraySegment<byte>>(), SocketFlags.None, out errorCode));
AssertExtensions.Throws<ArgumentException>("buffers", () => GetSocket().Receive(new List<ArraySegment<byte>>(), SocketFlags.None, out errorCode));
}
[Fact]
@@ -349,7 +349,7 @@ namespace System.Net.Sockets.Tests
public void ReceiveFrom_AddressFamily_Throws_Argument()
{
EndPoint endpoint = new IPEndPoint(IPAddress.IPv6Loopback, 1);
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).ReceiveFrom(s_buffer, 0, 0, SocketFlags.None, ref endpoint));
AssertExtensions.Throws<ArgumentException>("remoteEP", () => GetSocket(AddressFamily.InterNetwork).ReceiveFrom(s_buffer, 0, 0, SocketFlags.None, ref endpoint));
}
[Fact]
@@ -403,7 +403,7 @@ namespace System.Net.Sockets.Tests
EndPoint remote = new IPEndPoint(IPAddress.IPv6Loopback, 1);
IPPacketInformation packetInfo;
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).ReceiveMessageFrom(s_buffer, 0, 0, ref flags, ref remote, out packetInfo));
AssertExtensions.Throws<ArgumentException>("remoteEP", () => GetSocket(AddressFamily.InterNetwork).ReceiveMessageFrom(s_buffer, 0, 0, ref flags, ref remote, out packetInfo));
}
[Fact]
@@ -448,34 +448,34 @@ namespace System.Net.Sockets.Tests
[Fact]
public void SetSocketOption_Linger_NotLingerOption_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new object()));
}
[Fact]
public void SetSocketOption_Linger_InvalidLingerTime_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, -1)));
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, (int)ushort.MaxValue + 1)));
AssertExtensions.Throws<ArgumentException>("optionValue.LingerTime", () => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, -1)));
AssertExtensions.Throws<ArgumentException>("optionValue.LingerTime", () => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, (int)ushort.MaxValue + 1)));
}
[Fact]
public void SetSocketOption_IPMulticast_NotIPMulticastOption_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new object()));
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new object()));
}
[Fact]
public void SetSocketOption_IPv6Multicast_NotIPMulticastOption_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new object()));
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new object()));
}
[Fact]
public void SetSocketOption_Object_InvalidOptionName_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, new object()));
AssertExtensions.Throws<ArgumentException>("optionValue", () => GetSocket().SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, new object()));
}
[Fact]
@@ -517,7 +517,7 @@ namespace System.Net.Sockets.Tests
BufferList = s_buffers
};
Assert.Throws<ArgumentException>(() => GetSocket().AcceptAsync(eventArgs));
AssertExtensions.Throws<ArgumentException>("BufferList", () => GetSocket().AcceptAsync(eventArgs));
}
[Fact]
@@ -550,7 +550,7 @@ namespace System.Net.Sockets.Tests
BufferList = s_buffers
};
Assert.Throws<ArgumentException>(() => GetSocket().ConnectAsync(eventArgs));
AssertExtensions.Throws<ArgumentException>("BufferList", () => GetSocket().ConnectAsync(eventArgs));
}
[Fact]
@@ -601,7 +601,7 @@ namespace System.Net.Sockets.Tests
BufferList = s_buffers
};
Assert.Throws<ArgumentException>(() => Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, eventArgs));
AssertExtensions.Throws<ArgumentException>("BufferList", () => Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, eventArgs));
}
[Fact]
@@ -637,7 +637,7 @@ namespace System.Net.Sockets.Tests
RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, 1)
};
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).ReceiveFromAsync(eventArgs));
AssertExtensions.Throws<ArgumentException>("RemoteEndPoint", () => GetSocket(AddressFamily.InterNetwork).ReceiveFromAsync(eventArgs));
}
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Bug in ReceiveMessageFromAsync that dereferences null SAEA argument")]
@@ -660,7 +660,7 @@ namespace System.Net.Sockets.Tests
RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, 1)
};
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).ReceiveMessageFromAsync(eventArgs));
AssertExtensions.Throws<ArgumentException>("RemoteEndPoint", () => GetSocket(AddressFamily.InterNetwork).ReceiveMessageFromAsync(eventArgs));
}
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Bug in SendAsync that dereferences null SAEA argument")]
@@ -1056,8 +1056,8 @@ namespace System.Net.Sockets.Tests
[Fact]
public void BeginConnect_IPAddresses_EmptyIPAddresses_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().BeginConnect(new IPAddress[0], 1, TheAsyncCallback, null));
Assert.Throws<ArgumentException>(() => { GetSocket().ConnectAsync(new IPAddress[0], 1); });
AssertExtensions.Throws<ArgumentException>("addresses", () => GetSocket().BeginConnect(new IPAddress[0], 1, TheAsyncCallback, null));
AssertExtensions.Throws<ArgumentException>("addresses", () => { GetSocket().ConnectAsync(new IPAddress[0], 1); });
}
[Theory]
@@ -1096,7 +1096,7 @@ namespace System.Net.Sockets.Tests
[Fact]
public void EndConnect_UnrelatedAsyncResult_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().EndConnect(Task.CompletedTask));
AssertExtensions.Throws<ArgumentException>("asyncResult", () => GetSocket().EndConnect(Task.CompletedTask));
}
[Fact]
@@ -1138,8 +1138,8 @@ namespace System.Net.Sockets.Tests
[Fact]
public void BeginSend_Buffers_EmptyBuffers_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().BeginSend(new List<ArraySegment<byte>>(), SocketFlags.None, TheAsyncCallback, null));
Assert.Throws<ArgumentException>(() => { GetSocket().SendAsync(new List<ArraySegment<byte>>(), SocketFlags.None); });
AssertExtensions.Throws<ArgumentException>("buffers", () => GetSocket().BeginSend(new List<ArraySegment<byte>>(), SocketFlags.None, TheAsyncCallback, null));
AssertExtensions.Throws<ArgumentException>("buffers", () => { GetSocket().SendAsync(new List<ArraySegment<byte>>(), SocketFlags.None); });
}
[Fact]
@@ -1151,7 +1151,7 @@ namespace System.Net.Sockets.Tests
[Fact]
public void EndSend_UnrelatedAsyncResult_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().EndSend(Task.CompletedTask));
AssertExtensions.Throws<ArgumentException>("asyncResult", () => GetSocket().EndSend(Task.CompletedTask));
}
[Fact]
@@ -1203,7 +1203,7 @@ namespace System.Net.Sockets.Tests
[Fact]
public void EndSendto_UnrelatedAsyncResult_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().EndSendTo(Task.CompletedTask));
AssertExtensions.Throws<ArgumentException>("asyncResult", () => GetSocket().EndSendTo(Task.CompletedTask));
}
[Fact]
@@ -1245,8 +1245,8 @@ namespace System.Net.Sockets.Tests
[Fact]
public void BeginReceive_Buffers_EmptyBuffers_Throws_Argument()
{
Assert.Throws<ArgumentException>(() => GetSocket().BeginReceive(new List<ArraySegment<byte>>(), SocketFlags.None, TheAsyncCallback, null));
Assert.Throws<ArgumentException>(() => { GetSocket().ReceiveAsync(new List<ArraySegment<byte>>(), SocketFlags.None); });
AssertExtensions.Throws<ArgumentException>("buffers", () => GetSocket().BeginReceive(new List<ArraySegment<byte>>(), SocketFlags.None, TheAsyncCallback, null));
AssertExtensions.Throws<ArgumentException>("buffers", () => { GetSocket().ReceiveAsync(new List<ArraySegment<byte>>(), SocketFlags.None); });
}
[Fact]
@@ -1275,8 +1275,8 @@ namespace System.Net.Sockets.Tests
public void BeginReceiveFrom_AddressFamily_Throws_Argument()
{
EndPoint endpoint = new IPEndPoint(IPAddress.IPv6Loopback, 1);
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).BeginReceiveFrom(s_buffer, 0, 0, SocketFlags.None, ref endpoint, TheAsyncCallback, null));
Assert.Throws<ArgumentException>(() => { GetSocket(AddressFamily.InterNetwork).ReceiveFromAsync(new ArraySegment<byte>(s_buffer, 0, 0), SocketFlags.None, endpoint); });
AssertExtensions.Throws<ArgumentException>("remoteEP", () => GetSocket(AddressFamily.InterNetwork).BeginReceiveFrom(s_buffer, 0, 0, SocketFlags.None, ref endpoint, TheAsyncCallback, null));
AssertExtensions.Throws<ArgumentException>("remoteEP", () => { GetSocket(AddressFamily.InterNetwork).ReceiveFromAsync(new ArraySegment<byte>(s_buffer, 0, 0), SocketFlags.None, endpoint); });
}
[Fact]
@@ -1343,8 +1343,8 @@ namespace System.Net.Sockets.Tests
{
EndPoint remote = new IPEndPoint(IPAddress.IPv6Loopback, 1);
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).BeginReceiveMessageFrom(s_buffer, 0, 0, SocketFlags.None, ref remote, TheAsyncCallback, null));
Assert.Throws<ArgumentException>(() => { GetSocket(AddressFamily.InterNetwork).ReceiveMessageFromAsync(new ArraySegment<byte>(s_buffer, 0, 0), SocketFlags.None, remote); });
AssertExtensions.Throws<ArgumentException>("remoteEP", () => GetSocket(AddressFamily.InterNetwork).BeginReceiveMessageFrom(s_buffer, 0, 0, SocketFlags.None, ref remote, TheAsyncCallback, null));
AssertExtensions.Throws<ArgumentException>("remoteEP", () => { GetSocket(AddressFamily.InterNetwork).ReceiveMessageFromAsync(new ArraySegment<byte>(s_buffer, 0, 0), SocketFlags.None, remote); });
}
[Fact]
@@ -1399,7 +1399,7 @@ namespace System.Net.Sockets.Tests
EndPoint remote = new IPEndPoint(IPAddress.IPv6Loopback, 1);
IPPacketInformation packetInfo;
Assert.Throws<ArgumentException>(() => GetSocket(AddressFamily.InterNetwork).EndReceiveMessageFrom(null, ref flags, ref remote, out packetInfo));
AssertExtensions.Throws<ArgumentException>("endPoint", () => GetSocket(AddressFamily.InterNetwork).EndReceiveMessageFrom(null, ref flags, ref remote, out packetInfo));
}
[Fact]

View File

@@ -82,7 +82,7 @@ namespace System.Net.Sockets.Tests
{
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("remoteEP", () =>
{
sock.SendTo(new byte[10], new DnsEndPoint("localhost", UnusedPort));
});
@@ -98,7 +98,7 @@ namespace System.Net.Sockets.Tests
int port = sock.BindToAnonymousPort(IPAddress.Loopback);
EndPoint endpoint = new DnsEndPoint("localhost", port);
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("remoteEP", () =>
{
sock.ReceiveFrom(new byte[10], ref endpoint);
});
@@ -174,7 +174,7 @@ namespace System.Net.Sockets.Tests
{
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("remoteEP", () =>
{
sock.BeginSendTo(new byte[10], 0, 0, SocketFlags.None, new DnsEndPoint("localhost", UnusedPort), null, null);
});

View File

@@ -1 +1 @@
a5661e52c3bb75899c93187c9542f6ba0e8f84aa
ef120ada456f292695655c3e53e9b882ec68bd5d

View File

@@ -19,7 +19,7 @@ namespace System.Net.Sockets.Tests
private void TestLingerState_ArgumentException(Socket sock, bool enabled, int lingerTime)
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("optionValue.LingerTime", () =>
{
sock.LingerState = new LingerOption(enabled, lingerTime);
});

View File

@@ -13,7 +13,8 @@ namespace System.Net.Sockets.Tests
public class LoggingTest : RemoteExecutorTestBase
{
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NetEventSource is only part of .NET Core")]
[ActiveIssue(20470, TargetFrameworkMonikers.UapAot)]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NetEventSource is only part of .NET Core.")]
public static void EventSource_ExistsWithCorrectId()
{
Type esType = typeof(Socket).Assembly.GetType("System.Net.NetEventSource", throwOnError: true, ignoreCase: false);
@@ -27,7 +28,8 @@ namespace System.Net.Sockets.Tests
[OuterLoop]
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NetEventSource is only part of .NET Core")]
[ActiveIssue(20470, TargetFrameworkMonikers.UapAot)]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NetEventSource is only part of .NET Core.")]
public void EventSource_EventsRaisedAsExpected()
{
RemoteInvoke(() =>

View File

@@ -10,96 +10,52 @@ 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
[Fact]
public void Success_IPv4()
[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void ReceiveSentMessages_SocketAsyncEventArgs_Success(bool ipv4, bool changeReceiveBufferEachCall)
{
ManualResetEvent completed = new ManualResetEvent(false);
const int DataLength = 1024;
AddressFamily family = ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
IPAddress loopback = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
if (Socket.OSSupportsIPv4)
var completed = new ManualResetEventSlim(false);
using (var sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
using (var receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
{
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
sender.Bind(new IPEndPoint(loopback, 0));
receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
int port = receiver.BindToAnonymousPort(loopback);
var args = new SocketAsyncEventArgs() { RemoteEndPoint = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0) };
args.Completed += (s,e) => completed.Set();
args.SetBuffer(new byte[DataLength], 0, DataLength);
for (int iters = 0; iters < 5; iters++)
{
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));
sender.SendTo(new byte[DataLength], new IPEndPoint(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)
if (changeReceiveBufferEachCall)
{
OnCompleted(null, args);
args.SetBuffer(new byte[DataLength], 0, DataLength);
}
Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
if (!receiver.ReceiveMessageFromAsync(args))
{
completed.Set();
}
Assert.True(completed.Wait(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
completed.Reset();
Assert.Equal(1024, args.BytesTransferred);
Assert.Equal(DataLength, args.BytesTransferred);
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
sender.Dispose();
}
}
}
[OuterLoop] // TODO: Issue #11345
[Fact]
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();
}
}
}
@@ -108,30 +64,33 @@ namespace System.Net.Sockets.Tests
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Task_Success(bool ipv4)
public async Task ReceiveSentMessages_Tasks_Success(bool ipv4)
{
const int DataLength = 1024;
AddressFamily family = ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
IPAddress loopback = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
using (Socket receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
using (Socket sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
using (var receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
using (var sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(loopback);
receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
sender.Bind(new IPEndPoint(loopback, 0));
receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
int port = receiver.BindToAnonymousPort(loopback);
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
for (int iters = 0; iters < 5; iters++)
{
sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));
}
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));
}
SocketReceiveMessageFromResult result = await receiver.ReceiveMessageFromAsync(
new ArraySegment<byte>(new byte[1024], 0, 1024), SocketFlags.None,
new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
Assert.Equal(1024, result.ReceivedBytes);
Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, result.PacketInformation.Address);
SocketReceiveMessageFromResult result = await receiver.ReceiveMessageFromAsync(
new ArraySegment<byte>(new byte[DataLength], 0, DataLength), SocketFlags.None,
new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
Assert.Equal(DataLength, result.ReceivedBytes);
Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, result.PacketInformation.Address);
}
}
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="System.Net.Sockets.Tests">
<Assembly Name="System.Net.Sockets">
<!-- Needed for 2 tests that require inline metadata -->
<Namespace Name="System.Net.Sockets" Dynamic="Required All" />
</Assembly>
</Library>
</Directives>

View File

@@ -25,7 +25,7 @@ namespace System.Net.Sockets.Tests
public void Select_Read_NotASocket_Throws()
{
var list = new List<object> { new object() };
Assert.Throws<ArgumentException>(() => Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds));
AssertExtensions.Throws<ArgumentException>("socketList", () => Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds));
}
[Fact]
@@ -145,7 +145,7 @@ namespace System.Net.Sockets.Tests
public void Select_Write_NotASocket_Throws()
{
var list = new List<object> { new object() };
Assert.Throws<ArgumentException>(() => Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds));
AssertExtensions.Throws<ArgumentException>("socketList", () => Socket.Select(null, list, null, SelectSuccessTimeoutMicroseconds));
}
[Fact]
@@ -235,7 +235,7 @@ namespace System.Net.Sockets.Tests
public void Select_Error_NotASocket_Throws()
{
var list = new List<object> { new object() };
Assert.Throws<ArgumentException>(() => Socket.Select(null, null, list, SelectSuccessTimeoutMicroseconds));
AssertExtensions.Throws<ArgumentException>("socketList", () => Socket.Select(null, null, list, SelectSuccessTimeoutMicroseconds));
}
[Fact]

View File

@@ -16,8 +16,9 @@ namespace System.Net.Sockets.Tests
private readonly ITestOutputHelper _log;
private IPAddress _serverAddress = IPAddress.IPv6Loopback;
// In the current directory
private const string TestFileName = "NCLTest.Socket.SendPacketsAsync.testpayload";
// Accessible directories for UWP app:
// C:\Users\<UserName>\AppData\Local\Packages\<ApplicationPackageName>\
private string TestFileName = Environment.GetEnvironmentVariable("LocalAppData") + @"\NCLTest.Socket.SendPacketsAsync.testpayload";
private static int s_testFileSize = 1024;
#region Additional test attributes
@@ -88,12 +89,8 @@ namespace System.Net.Sockets.Tests
using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
sock.Connect(new IPEndPoint(_serverAddress, port));
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() =>
{
sock.SendPacketsAsync(null);
});
Assert.Equal("e", ex.ParamName);
AssertExtensions.Throws<ArgumentNullException>("e", () => sock.SendPacketsAsync(null));
}
}
}
@@ -117,12 +114,7 @@ namespace System.Net.Sockets.Tests
[InlineData(SocketImplementationType.Async)]
public void NullList_Throws(SocketImplementationType type)
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() =>
{
SendPackets(type, (SendPacketsElement[])null, SocketError.Success, 0);
});
Assert.Equal("e.SendPacketsElements", ex.ParamName);
AssertExtensions.Throws<ArgumentNullException>("e.SendPacketsElements", () => SendPackets(type, (SendPacketsElement[])null, SocketError.Success, 0));
}
[OuterLoop] // TODO: Issue #11345
@@ -279,7 +271,7 @@ namespace System.Net.Sockets.Tests
[InlineData(SocketImplementationType.Async)]
public void SendPacketsElement_EmptyFileName_Throws(SocketImplementationType type)
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("path", null, () =>
{
SendPackets(type, new SendPacketsElement(String.Empty), 0);
});
@@ -291,7 +283,7 @@ namespace System.Net.Sockets.Tests
[PlatformSpecific(TestPlatforms.Windows)] // whitespace-only is a valid name on Unix
public void SendPacketsElement_BlankFileName_Throws(SocketImplementationType type)
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("path", null, () =>
{
// Existence is validated on send
SendPackets(type, new SendPacketsElement(" \t "), 0);
@@ -304,7 +296,7 @@ namespace System.Net.Sockets.Tests
[PlatformSpecific(TestPlatforms.Windows)] // valid filename chars on Unix
public void SendPacketsElement_BadCharactersFileName_Throws(SocketImplementationType type)
{
Assert.Throws<ArgumentException>(() =>
AssertExtensions.Throws<ArgumentException>("path", null, () =>
{
// Existence is validated on send
SendPackets(type, new SendPacketsElement("blarkd@dfa?/sqersf"), 0);

View File

@@ -699,6 +699,35 @@ namespace System.Net.Sockets.Tests
}
}
[Fact]
public async Task Receive0ByteReturns_WhenPeerDisconnects()
{
using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
Task<Socket> acceptTask = AcceptAsync(listener);
await Task.WhenAll(
acceptTask,
ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndPoint).Port)));
using (Socket server = await acceptTask)
{
// Have the client do a 0-byte receive. No data is available, so this should pend.
Task<int> receive = ReceiveAsync(client, new ArraySegment<byte>(Array.Empty<byte>()));
Assert.False(receive.IsCompleted, $"Task should not have been completed, was {receive.Status}");
// Disconnect the client
server.Close();
// The client should now wake up
Assert.Equal(0, await receive);
}
}
}
[Theory]
[InlineData(false, 1)]
[InlineData(true, 1)]

View File

@@ -156,6 +156,25 @@ namespace System.Net.Sockets.Tests
}
}
[Fact]
public void SetBuffer_NoBuffer_ResetsCountOffset()
{
using (var saea = new SocketAsyncEventArgs())
{
saea.SetBuffer(42, 84);
Assert.Equal(0, saea.Offset);
Assert.Equal(0, saea.Count);
saea.SetBuffer(new byte[3], 1, 2);
Assert.Equal(1, saea.Offset);
Assert.Equal(2, saea.Count);
saea.SetBuffer(null, 1, 2);
Assert.Equal(0, saea.Offset);
Assert.Equal(0, saea.Count);
}
}
[Fact]
public void SetBufferListWhenBufferSet_Throws()
{
@@ -165,7 +184,7 @@ namespace System.Net.Sockets.Tests
byte[] buffer = new byte[1];
saea.SetBuffer(buffer, 0, 1);
Assert.Throws<ArgumentException>(() => saea.BufferList = bufferList);
AssertExtensions.Throws<ArgumentException>(null, () => saea.BufferList = bufferList);
Assert.Same(buffer, saea.Buffer);
Assert.Null(saea.BufferList);
@@ -181,7 +200,7 @@ namespace System.Net.Sockets.Tests
{
var bufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[1]) };
saea.BufferList = bufferList;
Assert.Throws<ArgumentException>(() => saea.SetBuffer(new byte[1], 0, 1));
AssertExtensions.Throws<ArgumentException>(null, () => saea.SetBuffer(new byte[1], 0, 1));
Assert.Same(bufferList, saea.BufferList);
Assert.Null(saea.Buffer);

View File

@@ -55,7 +55,7 @@ namespace System.Net.Sockets.Tests
// TODO: Issue #4887
// The socket option 'ReuseUnicastPost' only works on Windows 10 systems. In addition, setting the option
// is a no-op unless specialized network settings using PowerShell configuration are first applied to the
// machine. This is currently difficult to test in the CI environment. So, this ests will be disabled for now
// machine. This is currently difficult to test in the CI environment. So, this test will be disabled for now
[OuterLoop] // TODO: Issue #11345
[ActiveIssue(4887)]
public void ReuseUnicastPort_CreateSocketSetOptionToOneAndGetOption_SocketsReuseUnicastPortSupport_OptionIsOne()
@@ -83,6 +83,7 @@ namespace System.Net.Sockets.Tests
}
[OuterLoop] // TODO: Issue #11345
[Fact]
public async Task MulticastInterface_Set_AnyInterface_Succeeds()
{
// On all platforms, index 0 means "any interface"
@@ -92,10 +93,11 @@ namespace System.Net.Sockets.Tests
[OuterLoop] // TODO: Issue #11345
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // see comment below
[ActiveIssue(21327, TargetFrameworkMonikers.Uap)] // UWP Apps are forbidden to send network traffic to the local Computer.
public async Task MulticastInterface_Set_Loopback_Succeeds()
{
// On Windows, we can apparently assume interface 1 is "loopback." On other platforms, this is not a
// valid assumption. We could maybe use NetworkInterface.LoopbackInterfaceIndex to get the index, but
// On Windows, we can apparently assume interface 1 is "loopback." On other platforms, this is not a
// valid assumption. We could maybe use NetworkInterface.LoopbackInterfaceIndex to get the index, but
// this would introduce a dependency on System.Net.NetworkInformation, which depends on System.Net.Sockets,
// which is what we're testing here.... So for now, we'll just assume "loopback == 1" and run this on
// Windows only.

View File

@@ -71,9 +71,6 @@
<Link>SocketCommon\SocketImplementationType.cs</Link>
</Compile>
<!-- Common test files -->
<Compile Include="$(CommonTestPath)\System\AssertExtensions.cs">
<Link>Common\System\AssertExtensions.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\PlatformDetection.cs">
<Link>Common\System\PlatformDetection.cs</Link>
</Compile>
@@ -98,12 +95,6 @@
<Compile Include="$(CommonTestPath)\System\Diagnostics\Tracing\TestEventListener.cs">
<Link>Common\System\Diagnostics\Tracing\TestEventListener.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorTestBase.cs">
<Link>Common\System\Diagnostics\RemoteExecutorTestBase.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\IO\FileCleanupTestBase.cs">
<Link>Common\System\IO\FileCleanupTestBase.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorConsoleApp\RemoteExecutorConsoleApp.csproj">
@@ -111,5 +102,8 @@
<Name>RemoteExecutorConsoleApp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -28,7 +28,7 @@ namespace System.Net.Sockets.Tests
[InlineData(AddressFamily.Unknown)]
public void Ctor_InvalidFamily_Throws(AddressFamily family)
{
Assert.Throws<ArgumentException>(() => new TcpClient(family));
AssertExtensions.Throws<ArgumentException>("family", () => new TcpClient(family));
}
[Fact]

View File

@@ -91,8 +91,8 @@ namespace System.Net.Sockets.Tests
Assert.Throws<ArgumentNullException>(() => listener.EndAcceptSocket(null));
Assert.Throws<ArgumentNullException>(() => listener.EndAcceptTcpClient(null));
Assert.Throws<ArgumentException>(() => listener.EndAcceptSocket(Task.CompletedTask));
Assert.Throws<ArgumentException>(() => listener.EndAcceptTcpClient(Task.CompletedTask));
AssertExtensions.Throws<ArgumentException>("asyncResult", () => listener.EndAcceptSocket(Task.CompletedTask));
AssertExtensions.Throws<ArgumentException>("asyncResult", () => listener.EndAcceptTcpClient(Task.CompletedTask));
}
[Fact]