Verify scopeids are connectable using Socket before testing Kestrel (#1536)
This commit is contained in:
parent
ca5581461c
commit
6c131ea240
|
|
@ -8,6 +8,7 @@ using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
|
@ -451,7 +452,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
|
||||||
// Dynamic port
|
// Dynamic port
|
||||||
var ipv6Addresses = GetIPAddresses()
|
var ipv6Addresses = GetIPAddresses()
|
||||||
.Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6)
|
.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)
|
foreach (var ip in ipv6Addresses)
|
||||||
{
|
{
|
||||||
dataset.Add($"http://[{ip}]:0/", GetTestUrls);
|
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)]
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||||
private class PortSupportedConditionAttribute : Attribute, ITestCondition
|
private class PortSupportedConditionAttribute : Attribute, ITestCondition
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue