Verify scopeids are connectable using Socket before testing Kestrel (#1536)

This commit is contained in:
Stephen Halter 2017-03-21 18:31:29 -07:00 committed by Cesar Blum Silveira
parent ca5581461c
commit 6c131ea240
1 changed files with 42 additions and 1 deletions

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@ -451,7 +452,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// Dynamic port
var ipv6Addresses = GetIPAddresses()
.Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6)
.Where(ip => ip.ScopeId != 0);
.Where(ip => ip.ScopeId != 0)
.Where(ip => CanBindAndConnectToEndpoint(new IPEndPoint(ip, 0)));
foreach (var ip in ipv6Addresses)
{
dataset.Add($"http://[{ip}]:0/", GetTestUrls);
@ -531,6 +534,44 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
}
}
private static bool CanBindAndConnectToEndpoint(IPEndPoint endPoint)
{
try
{
using (var serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
serverSocket.Bind(endPoint);
serverSocket.Listen(1);
var socketArgs = new SocketAsyncEventArgs
{
RemoteEndPoint = serverSocket.LocalEndPoint
};
var mre = new ManualResetEventSlim();
socketArgs.Completed += (s, e) =>
{
mre.Set();
e.ConnectSocket?.Dispose();
};
// Connect can take a couple minutes to time out.
if (Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, socketArgs))
{
return mre.Wait(5000) && socketArgs.SocketError == SocketError.Success;
}
else
{
return socketArgs.SocketError == SocketError.Success;
}
}
}
catch (SocketException)
{
return false;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
private class PortSupportedConditionAttribute : Attribute, ITestCondition
{