Search in sources :

Example 66 with InetAddress

use of java.net.InetAddress in project guava by google.

the class InetAddressesTest method testForStringIPv6EightColons.

public void testForStringIPv6EightColons() throws UnknownHostException {
    String[] eightColons = { "::7:6:5:4:3:2:1", "::7:6:5:4:3:2:0", "7:6:5:4:3:2:1::", "0:6:5:4:3:2:1::" };
    for (int i = 0; i < eightColons.length; i++) {
        InetAddress ipv6Addr = null;
        // Shouldn't hit DNS, because it's an IP string literal.
        ipv6Addr = InetAddress.getByName(eightColons[i]);
        assertEquals(ipv6Addr, InetAddresses.forString(eightColons[i]));
        assertTrue(InetAddresses.isInetAddress(eightColons[i]));
    }
}
Also used : InetAddress(java.net.InetAddress)

Example 67 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class DatagramChannelTest method test_bind_IPv6.

public void test_bind_IPv6() throws Exception {
    InetAddress bindAddress = getNonLoopbackNetworkInterfaceAddress(false);
    test_bind(bindAddress);
}
Also used : InetAddress(java.net.InetAddress)

Example 68 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class TestSSLContext method clientAuth.

/**
     * Returns an SSLSocketFactory that calls setWantClientAuth and
     * setNeedClientAuth as specified on all returned sockets.
     */
public static SSLSocketFactory clientAuth(final SSLSocketFactory sf, final boolean want, final boolean need) {
    return new SSLSocketFactory() {

        private SSLSocket set(Socket socket) {
            SSLSocket s = (SSLSocket) socket;
            s.setWantClientAuth(want);
            s.setNeedClientAuth(need);
            return s;
        }

        public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            return set(sf.createSocket(host, port));
        }

        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
            return set(sf.createSocket(host, port, localHost, localPort));
        }

        public Socket createSocket(InetAddress host, int port) throws IOException {
            return set(sf.createSocket(host, port));
        }

        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
            return set(sf.createSocket(address, port));
        }

        public String[] getDefaultCipherSuites() {
            return sf.getDefaultCipherSuites();
        }

        public String[] getSupportedCipherSuites() {
            return sf.getSupportedCipherSuites();
        }

        public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
            return set(sf.createSocket(s, host, port, autoClose));
        }
    };
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) InetAddress(java.net.InetAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 69 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class TestSSLContext method create.

/**
     * TestSSLContext creation method that allows separate creation of client and server key store
     */
public static TestSSLContext create(KeyStore clientKeyStore, char[] clientStorePassword, KeyStore serverKeyStore, char[] serverStorePassword, KeyManager[] clientKeyManagers, KeyManager[] serverKeyManagers, TrustManager clientTrustManagers, TrustManager serverTrustManagers, SSLContext clientContext, SSLContext serverContext) {
    try {
        SSLServerSocket serverSocket = (SSLServerSocket) serverContext.getServerSocketFactory().createServerSocket(0);
        InetAddress host = InetAddress.getLocalHost();
        int port = serverSocket.getLocalPort();
        return new TestSSLContext(clientKeyStore, clientStorePassword, serverKeyStore, serverStorePassword, clientKeyManagers, serverKeyManagers, clientContext, serverContext, serverSocket, host, port);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SSLServerSocket(javax.net.ssl.SSLServerSocket) InetAddress(java.net.InetAddress) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException)

Example 70 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class SocketTest method checkSocketLocalAndRemoteAddresses.

// http://code.google.com/p/android/issues/detail?id=3123
// http://code.google.com/p/android/issues/detail?id=1933
/* Fails on machines with IPv6 connections, both on JRE and j2objc.
    public void test_socketLocalAndRemoteAddresses() throws Exception {
        checkSocketLocalAndRemoteAddresses(false);
        checkSocketLocalAndRemoteAddresses(true);
    }
    */
public void checkSocketLocalAndRemoteAddresses(boolean setOptions) throws Exception {
    InetAddress host = InetAddress.getLocalHost();
    // Open a local server port.
    ServerSocketChannel ssc = ServerSocketChannel.open();
    InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
    ssc.bind(listenAddr, 0);
    ServerSocket ss = ssc.socket();
    // Open a socket to the local port.
    SocketChannel out = SocketChannel.open();
    out.configureBlocking(false);
    if (setOptions) {
        out.socket().setTcpNoDelay(false);
    }
    InetSocketAddress addr = new InetSocketAddress(host, ssc.socket().getLocalPort());
    out.connect(addr);
    while (!out.finishConnect()) {
        Thread.sleep(1);
    }
    SocketChannel in = ssc.accept();
    if (setOptions) {
        in.socket().setTcpNoDelay(false);
    }
    InetSocketAddress listenAddress = (InetSocketAddress) in.getLocalAddress();
    InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
    InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
    InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
    InetSocketAddress inRemoteAddress = (InetSocketAddress) in.socket().getRemoteSocketAddress();
    //        System.err.println("listenAddress: " + listenAddr);
    //        System.err.println("inLocalAddress: " + inLocalAddress);
    //        System.err.println("inRemoteAddress: " + inRemoteAddress);
    //        System.err.println("outLocalAddress: " + outLocalAddress);
    //        System.err.println("outRemoteAddress: " + outRemoteAddress);
    assertFalse(ssc.socket().isClosed());
    assertTrue(ssc.socket().isBound());
    assertTrue(in.isConnected());
    assertTrue(in.socket().isConnected());
    assertTrue(out.socket().isConnected());
    assertTrue(out.isConnected());
    in.close();
    out.close();
    ssc.close();
    assertTrue(ssc.socket().isClosed());
    assertTrue(ssc.socket().isBound());
    assertFalse(in.isConnected());
    assertFalse(in.socket().isConnected());
    assertFalse(out.socket().isConnected());
    assertFalse(out.isConnected());
    assertNull(in.socket().getRemoteSocketAddress());
    assertNull(out.socket().getRemoteSocketAddress());
    // As per docs and RI - server socket local address methods continue to return the bind()
    // addresses even after close().
    assertEquals(listenAddress, ssc.socket().getLocalSocketAddress());
    // As per docs and RI - socket local address methods return the wildcard address before
    // bind() and after close(), but the port will be the same as it was before close().
    InetSocketAddress inLocalAddressAfterClose = (InetSocketAddress) in.socket().getLocalSocketAddress();
    assertTrue(inLocalAddressAfterClose.getAddress().isAnyLocalAddress());
    assertEquals(inLocalAddress.getPort(), inLocalAddressAfterClose.getPort());
    InetSocketAddress outLocalAddressAfterClose = (InetSocketAddress) out.socket().getLocalSocketAddress();
    assertTrue(outLocalAddressAfterClose.getAddress().isAnyLocalAddress());
    assertEquals(outLocalAddress.getPort(), outLocalAddressAfterClose.getPort());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

InetAddress (java.net.InetAddress)2225 UnknownHostException (java.net.UnknownHostException)423 IOException (java.io.IOException)295 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)251 NetworkInterface (java.net.NetworkInterface)192 ArrayList (java.util.ArrayList)174 SocketException (java.net.SocketException)154 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)75 LinkAddress (android.net.LinkAddress)70 DatagramSocket (java.net.DatagramSocket)67 Token (org.apache.cassandra.dht.Token)67 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52