Search in sources :

Example 21 with SocketFactory

use of javax.net.SocketFactory in project robovm by robovm.

the class MySocketFactory method test_createSocket_StringI.

public final void test_createSocket_StringI() throws Exception {
    SocketFactory sf = SocketFactory.getDefault();
    int sport = new ServerSocket(0).getLocalPort();
    int[] invalidPorts = { Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE };
    Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
    assertNotNull(s);
    assertTrue("Failed to create socket", s.getPort() == sport);
    try {
        sf.createSocket("bla-bla", sport);
        fail("UnknownHostException wasn't thrown");
    } catch (UnknownHostException expected) {
    }
    for (int i = 0; i < invalidPorts.length; i++) {
        try {
            sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
            fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
        } catch (IllegalArgumentException expected) {
        }
    }
    try {
        sf.createSocket(InetAddress.getLocalHost().getHostName(), s.getLocalPort());
        fail("IOException wasn't thrown");
    } catch (IOException expected) {
    }
    SocketFactory f = SocketFactory.getDefault();
    try {
        f.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
        fail("IOException wasn't thrown ...");
    } catch (IOException expected) {
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) SocketFactory(javax.net.SocketFactory) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 22 with SocketFactory

use of javax.net.SocketFactory in project robovm by robovm.

the class MySocketFactory method test_createSocket_InetAddressI.

public final void test_createSocket_InetAddressI() throws Exception {
    SocketFactory sf = SocketFactory.getDefault();
    int sport = new ServerSocket(0).getLocalPort();
    int[] invalidPorts = { Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE };
    Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
    assertNotNull(s);
    assertTrue("Failed to create socket", s.getPort() == sport);
    for (int i = 0; i < invalidPorts.length; i++) {
        try {
            sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
            fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
        } catch (IllegalArgumentException expected) {
        }
    }
    try {
        sf.createSocket(InetAddress.getLocalHost(), s.getLocalPort());
        fail("IOException wasn't thrown");
    } catch (IOException expected) {
    }
    SocketFactory f = SocketFactory.getDefault();
    try {
        f.createSocket(InetAddress.getLocalHost(), 8081);
        fail("IOException wasn't thrown ...");
    } catch (IOException expected) {
    }
}
Also used : SocketFactory(javax.net.SocketFactory) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 23 with SocketFactory

use of javax.net.SocketFactory in project robovm by robovm.

the class SSLSocketFactoryTest method test_Constructor.

/**
     * javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
     */
public void test_Constructor() {
    try {
        SocketFactory sf = SSLSocketFactory.getDefault();
        assertTrue(sf instanceof SSLSocketFactory);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : SocketFactory(javax.net.SocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 24 with SocketFactory

use of javax.net.SocketFactory in project robovm by robovm.

the class SSLSocketFactoryTest method test_SSLSocketFactory_getDefault.

public void test_SSLSocketFactory_getDefault() {
    SocketFactory sf = SSLSocketFactory.getDefault();
    assertNotNull(sf);
    assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass()));
}
Also used : SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 25 with SocketFactory

use of javax.net.SocketFactory in project robovm by robovm.

the class SSLContextTest method test_SSLContext_getSocketFactory.

public void test_SSLContext_getSocketFactory() throws Exception {
    for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
        if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
            SSLContext.getInstance(protocol).getSocketFactory();
        } else {
            try {
                SSLContext.getInstance(protocol).getSocketFactory();
                fail();
            } catch (IllegalStateException expected) {
            }
        }
        SSLContext sslContext = SSLContext.getInstance(protocol);
        if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
            sslContext.init(null, null, null);
        }
        SocketFactory sf = sslContext.getSocketFactory();
        assertNotNull(sf);
        assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass()));
    }
}
Also used : SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2