Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2220

Calls to new ServerAddress(InetAddress) fail with numeric IPv6 addresses

      Constructing a ServerAddress using IPv6 InetAddress fails if the specified IP address resolves to its address (i.e. no reverse host mapping).

      ServerAddress assumes that IPv6 addresses are always specified in brackets which isn't the case for IPv6 addresses without reverse host mapping.

      Stack trace:

      com.mongodb.MongoException: host and port should be specified in host:port format
      
      	at com.mongodb.ServerAddress.<init>(ServerAddress.java:122)
      	at com.mongodb.ServerAddress.<init>(ServerAddress.java:58)
      

      Test case:

      import static org.junit.Assert.*;
      import java.net.InetAddress;
      import org.junit.Test;
      import com.mongodb.ServerAddress;
      
      public class ServerAddressUnitTests {
      
      	@Test
      	public void hostAndPortStringIPv6AddressShouldWork() throws Exception {
      
      		ServerAddress serverAddress = new ServerAddress("[dead:beef:affe::1]:27017");
      
      		assertEquals(27017, serverAddress.getPort());
      		assertEquals("dead:beef:affe::1", serverAddress.getHost());
      	}
      
      	@Test
      	public void hostAndPortIPv6AddressShouldWork() throws Exception {
      
      		ServerAddress serverAddress = new ServerAddress("[dead:beef:affe::1]", 27017);
      
      		assertEquals(27017, serverAddress.getPort());
      		assertEquals("dead:beef:affe::1", serverAddress.getHost());
      	}
      
      	@Test
      	public void inetAddressIPv6AddressShouldWork() throws Exception {
      
      		ServerAddress serverAddress = new ServerAddress(InetAddress.getByName("dead:beef:affe::1"));
      
      		assertEquals(27017, serverAddress.getPort());
      		assertEquals("dead:beef:affe::1", serverAddress.getHost());
      	}
      
      	@Test
      	public void inetAddressAndPortIPv6AddressShouldWork() throws Exception {
      
      		ServerAddress serverAddress = new ServerAddress(InetAddress.getByName("dead:beef:affe::1"), 27017);
      
      		assertEquals(27017, serverAddress.getPort());
      		assertEquals("dead:beef:affe::1", serverAddress.getHost());
      	}
      }
      

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            mp911de Mark Paluch
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: