From bfe1f06938eeb68f62898963c69a6e7d4a87a20a Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Tue, 14 Feb 2017 10:53:06 -0800 Subject: [PATCH] Fix AddressRegistrationTests.RegisterAddresses_IPv6ScopeId_Success (#1363). --- .../AddressRegistrationTests.cs | 1 + .../IPv6ScopeIdPresentConditionAttribute.cs | 35 +++++++++++++++++++ .../IPv6SupportedConditionAttribute.cs | 16 ++------- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6ScopeIdPresentConditionAttribute.cs diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index 2abb129b52..ecdd2e97ba 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -70,6 +70,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests [ConditionalTheory, MemberData(nameof(AddressRegistrationDataIPv6ScopeId))] [IPv6SupportedCondition] + [IPv6ScopeIdPresentCondition] public async Task RegisterAddresses_IPv6ScopeId_Success(string addressInput, Func testUrls) { await RegisterAddresses_Success(addressInput, testUrls); diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6ScopeIdPresentConditionAttribute.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6ScopeIdPresentConditionAttribute.cs new file mode 100644 index 0000000000..f1705b9c7b --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6ScopeIdPresentConditionAttribute.cs @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Linq; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using Microsoft.AspNetCore.Testing.xunit; + +namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests +{ + public class IPv6ScopeIdPresentConditionAttribute : Attribute, ITestCondition + { + private static readonly Lazy _ipv6ScopeIdPresent = new Lazy(IPv6ScopeIdAddressPresent); + + public bool IsMet => _ipv6ScopeIdPresent.Value; + + public string SkipReason => "No IPv6 addresses with scope IDs were found on the host."; + + private static bool IPv6ScopeIdAddressPresent() + { + try + { + return NetworkInterface.GetAllNetworkInterfaces() + .Where(iface => iface.OperationalStatus == OperationalStatus.Up) + .SelectMany(iface => iface.GetIPProperties().UnicastAddresses) + .Any(addrInfo => addrInfo.Address.AddressFamily == AddressFamily.InterNetworkV6 && addrInfo.Address.ScopeId != 0); + } + catch (SocketException) + { + return false; + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6SupportedConditionAttribute.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6SupportedConditionAttribute.cs index ca2f2bc6b9..815a271825 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6SupportedConditionAttribute.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/IPv6SupportedConditionAttribute.cs @@ -13,21 +13,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { private static readonly Lazy _ipv6Supported = new Lazy(CanBindToIPv6Address); - public bool IsMet - { - get - { - return _ipv6Supported.Value; - } - } + public bool IsMet => _ipv6Supported.Value; - public string SkipReason - { - get - { - return "IPv6 not supported on the host."; - } - } + public string SkipReason => "IPv6 not supported on the host."; private static bool CanBindToIPv6Address() {