1
0
Эх сурвалжийг харах

add more tests to network service

Follow up for #15340

We test that bind with wilcard IP + fixed IP it raises an exception
We test binding multiple IPs

(cherry picked from commit 2cc5bb7)
David Pilato 10 жил өмнө
parent
commit
f675801b26

+ 43 - 20
core/src/test/java/org/elasticsearch/common/network/NetworkServiceTests.java

@@ -24,14 +24,16 @@ import org.elasticsearch.test.ESTestCase;
 
 import java.net.InetAddress;
 
+import static org.hamcrest.Matchers.is;
+
 /**
  * Tests for network service... try to keep them safe depending upon configuration
  * please don't actually bind to anything, just test the addresses.
  */
 public class NetworkServiceTests extends ESTestCase {
 
-    /** 
-     * ensure exception if we bind to multicast ipv4 address 
+    /**
+     * ensure exception if we bind to multicast ipv4 address
      */
     public void testBindMulticastV4() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
@@ -42,9 +44,8 @@ public class NetworkServiceTests extends ESTestCase {
             assertTrue(e.getMessage().contains("invalid: multicast"));
         }
     }
-    
-    /** 
-     * ensure exception if we bind to multicast ipv6 address 
+    /**
+     * ensure exception if we bind to multicast ipv6 address
      */
     public void testBindMulticastV6() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
@@ -55,9 +56,9 @@ public class NetworkServiceTests extends ESTestCase {
             assertTrue(e.getMessage().contains("invalid: multicast"));
         }
     }
-    
-    /** 
-     * ensure exception if we publish to multicast ipv4 address 
+
+    /**
+     * ensure exception if we publish to multicast ipv4 address
      */
     public void testPublishMulticastV4() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
@@ -68,9 +69,9 @@ public class NetworkServiceTests extends ESTestCase {
             assertTrue(e.getMessage().contains("invalid: multicast"));
         }
     }
-    
-    /** 
-     * ensure exception if we publish to multicast ipv6 address 
+
+    /**
+     * ensure exception if we publish to multicast ipv6 address
      */
     public void testPublishMulticastV6() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
@@ -82,24 +83,24 @@ public class NetworkServiceTests extends ESTestCase {
         }
     }
 
-    /** 
-     * ensure specifying wildcard ipv4 address will bind to all interfaces 
+    /**
+     * ensure specifying wildcard ipv4 address will bind to all interfaces
      */
     public void testBindAnyLocalV4() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
         assertEquals(InetAddress.getByName("0.0.0.0"), service.resolveBindHostAddresses(new String[] { "0.0.0.0" })[0]);
     }
-    
-    /** 
-     * ensure specifying wildcard ipv6 address will bind to all interfaces 
+
+    /**
+     * ensure specifying wildcard ipv6 address will bind to all interfaces
      */
     public void testBindAnyLocalV6() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
         assertEquals(InetAddress.getByName("::"), service.resolveBindHostAddresses(new String[] { "::" })[0]);
     }
 
-    /** 
-     * ensure specifying wildcard ipv4 address selects reasonable publish address 
+    /**
+     * ensure specifying wildcard ipv4 address selects reasonable publish address
      */
     public void testPublishAnyLocalV4() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
@@ -107,12 +108,34 @@ public class NetworkServiceTests extends ESTestCase {
         assertFalse(address.isAnyLocalAddress());
     }
 
-    /** 
-     * ensure specifying wildcard ipv6 address selects reasonable publish address 
+    /**
+     * ensure specifying wildcard ipv6 address selects reasonable publish address
      */
     public void testPublishAnyLocalV6() throws Exception {
         NetworkService service = new NetworkService(Settings.EMPTY);
         InetAddress address = service.resolvePublishHostAddresses(new String[] { "::" });
         assertFalse(address.isAnyLocalAddress());
     }
+
+    /**
+     * ensure we can bind to multiple addresses
+     */
+    public void testBindMultipleAddresses() throws Exception {
+        NetworkService service = new NetworkService(Settings.EMPTY);
+        InetAddress[] addresses = service.resolveBindHostAddresses(new String[]{"127.0.0.1", "127.0.0.2"});
+        assertThat(addresses.length, is(2));
+    }
+
+    /**
+     * ensure we can't bind to multiple addresses when using wildcard
+     */
+    public void testBindMultipleAddressesWithWildcard() throws Exception {
+        NetworkService service = new NetworkService(Settings.EMPTY);
+        try {
+            service.resolveBindHostAddresses(new String[]{"0.0.0.0", "127.0.0.1"});
+            fail("should have hit exception");
+        } catch (IllegalArgumentException e) {
+            assertTrue(e.getMessage().contains("is wildcard, but multiple addresses specified"));
+        }
+    }
 }