From 0778274b72436275893ee37f8db6dd8ad9ad83c6 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 29 Jul 2020 13:16:12 +0200 Subject: [PATCH] Fix IPV6 Scope Id in InetAddressesTests (#60368) (#60369) Follow up to #60360, turns out at times the name of an interface that isn't loopback is not a valid scope id. --- .../common/network/InetAddressesTests.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server/src/test/java/org/elasticsearch/common/network/InetAddressesTests.java b/server/src/test/java/org/elasticsearch/common/network/InetAddressesTests.java index 7922d3eadc4..0fc03040e6d 100644 --- a/server/src/test/java/org/elasticsearch/common/network/InetAddressesTests.java +++ b/server/src/test/java/org/elasticsearch/common/network/InetAddressesTests.java @@ -23,6 +23,7 @@ import org.hamcrest.Matchers; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; +import java.util.Enumeration; public class InetAddressesTests extends ESTestCase { public void testForStringBogusInput() { @@ -130,7 +131,17 @@ public class InetAddressesTests extends ESTestCase { } public void testForStringIPv6WithScopeIdInput() throws java.io.IOException { - String ipStr = "0:0:0:0:0:0:0:1%" + NetworkInterface.getNetworkInterfaces().nextElement().getName(); + final Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + String scopeId = null; + while (interfaces.hasMoreElements()) { + final NetworkInterface nint = interfaces.nextElement(); + if (nint.isLoopback()) { + scopeId = nint.getName(); + break; + } + } + assertNotNull(scopeId); + String ipStr = "0:0:0:0:0:0:0:1%" + scopeId; InetAddress ipv6Addr = InetAddress.getByName(ipStr); assertEquals(ipv6Addr, InetAddresses.forString(ipStr)); assertTrue(InetAddresses.isInetAddress(ipStr));