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.
This commit is contained in:
Armin Braun 2020-07-29 13:16:12 +02:00 committed by GitHub
parent 1f6a3765e4
commit 0778274b72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -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<NetworkInterface> 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));