|
|
@@ -22,6 +22,10 @@ package org.elasticsearch.common.network;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+import static org.hamcrest.Matchers.containsString;
|
|
|
|
|
|
/**
|
|
|
* Tests for network utils. Please avoid using any methods that cause DNS lookups!
|
|
|
@@ -74,4 +78,30 @@ public class NetworkUtilsTests extends ESTestCase {
|
|
|
assertArrayEquals(new InetAddress[] { InetAddress.getByName("127.0.0.1") }, NetworkUtils.filterIPV4(addresses));
|
|
|
assertArrayEquals(new InetAddress[] { InetAddress.getByName("::1") }, NetworkUtils.filterIPV6(addresses));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that selecting by name is possible and properly matches the addresses on all interfaces and virtual
|
|
|
+ * interfaces.
|
|
|
+ *
|
|
|
+ * Note that to avoid that this test fails when interfaces are down or they do not have addresses assigned to them,
|
|
|
+ * they are ignored.
|
|
|
+ */
|
|
|
+ public void testAddressInterfaceLookup() throws Exception {
|
|
|
+ for (NetworkInterface netIf : NetworkUtils.getInterfaces()) {
|
|
|
+ if (!netIf.isUp() || Collections.list(netIf.getInetAddresses()).isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String name = netIf.getName();
|
|
|
+ InetAddress[] expectedAddresses = Collections.list(netIf.getInetAddresses()).toArray(new InetAddress[0]);
|
|
|
+ InetAddress[] foundAddresses = NetworkUtils.getAddressesForInterface(name);
|
|
|
+ assertArrayEquals(expectedAddresses, foundAddresses);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNonExistingInterface() throws Exception {
|
|
|
+ IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
|
|
|
+ () -> NetworkUtils.getAddressesForInterface("non-existing"));
|
|
|
+ assertThat(exception.getMessage(), containsString("No interface named 'non-existing' found"));
|
|
|
+ }
|
|
|
}
|