Search in sources :

Example 1 with PortUnreachableException

use of java.net.PortUnreachableException in project mobile-center-sdk-android by Microsoft.

the class HttpUtilsAndroidTest method isRecoverableErrorTest.

@Test
public void isRecoverableErrorTest() {
    assertTrue(isRecoverableError(new EOFException()));
    assertTrue(isRecoverableError(new InterruptedIOException()));
    assertTrue(isRecoverableError(new SocketTimeoutException()));
    assertTrue(isRecoverableError(new SocketException()));
    assertTrue(isRecoverableError(new PortUnreachableException()));
    assertTrue(isRecoverableError(new UnknownHostException()));
    assertTrue(isRecoverableError(new RejectedExecutionException()));
    assertFalse(isRecoverableError(new MalformedURLException()));
    assertFalse(isRecoverableError(new IOException()));
    assertTrue(isRecoverableError(new IOException(new EOFException())));
    assertFalse(isRecoverableError(new IOException(new Exception())));
    for (int i = 0; i <= 4; i++) assertTrue(isRecoverableError(new HttpException(500 + i)));
    for (int i = 0; i <= 6; i++) assertFalse(isRecoverableError(new HttpException(400 + i)));
    assertTrue(isRecoverableError(new HttpException(408)));
    assertFalse(isRecoverableError(new HttpException(413)));
    assertTrue(isRecoverableError(new HttpException(429)));
    assertTrue(isRecoverableError(new SSLException("Write error: ssl=0x59c28f90: I/O error during system call, Connection timed out")));
    assertFalse(isRecoverableError(new SSLHandshakeException("java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.")));
    assertFalse(isRecoverableError(new SSLException(null, new CertPathValidatorException("Trust anchor for certification path not found."))));
    assertFalse(isRecoverableError(new SSLException("java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty")));
    assertTrue(isRecoverableError(new SSLException("Read error: ssl=0x9dd07200: I/O error during system call, Connection reset by peer")));
    assertTrue(isRecoverableError(new SSLException("SSL handshake aborted: ssl=0x1cc160: I/O error during system call, Connection reset by peer")));
    assertTrue(isRecoverableError(new SSLHandshakeException("javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x870c918: Failure in SSL library, usually a protocol error\nerror:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:658 0xb7c393a1:0x00000000)")));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) PortUnreachableException(java.net.PortUnreachableException) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) SSLException(javax.net.ssl.SSLException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MalformedURLException(java.net.MalformedURLException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) EOFException(java.io.EOFException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) CertPathValidatorException(java.security.cert.CertPathValidatorException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) CertPathValidatorException(java.security.cert.CertPathValidatorException) SocketTimeoutException(java.net.SocketTimeoutException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 2 with PortUnreachableException

use of java.net.PortUnreachableException in project robovm by robovm.

the class OldDatagramSocketTest method test_connectLjava_net_InetAddressI.

public void test_connectLjava_net_InetAddressI() throws UnknownHostException, SocketException {
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
        assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(inetAddress));
        assertTrue("Incorrect Port", ds.getPort() == portNumber);
        ds.disconnect();
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    System.out.println("Running test_connectLjava_net_InetAddressI" + "(DatagramSocketTest) with IPv6GlobalAddressJcl4: " + Support_Configuration.IPv6GlobalAddressJcl4);
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getByName(Support_Configuration.IPv6GlobalAddressJcl4);
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
        assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(inetAddress));
        assertTrue("Incorrect Port", ds.getPort() == portNumber);
        ds.disconnect();
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    try {
        // Create a connected datagram socket to test
        // PlainDatagramSocketImpl.peek()
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket();
        int port = ds.getLocalPort();
        ds.connect(localHost, port);
        DatagramPacket send = new DatagramPacket(new byte[10], 10, localHost, port);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("Wrong size: " + receive.getLength(), receive.getLength() == 10);
        assertTrue("Wrong receiver", receive.getAddress().equals(localHost));
    } catch (IOException e) {
        fail("Unexpected IOException : " + e.getMessage());
    }
    class DatagramServer extends Thread {

        public DatagramSocket ms;

        boolean running = true;

        public byte[] rbuf = new byte[512];

        DatagramPacket rdp = null;

        public void run() {
            try {
                while (running) {
                    try {
                        ms.receive(rdp);
                        // echo the packet back
                        ms.send(rdp);
                    } catch (java.io.InterruptedIOException e) {
                        Thread.yield();
                    }
                }
            } catch (java.io.IOException e) {
                System.out.println("Multicast server failed: " + e);
            } finally {
                ms.close();
            }
        }

        public void stopServer() {
            running = false;
        }

        public DatagramServer(int aPort, InetAddress address) throws java.io.IOException {
            rbuf = new byte[512];
            rbuf[0] = -1;
            rdp = new DatagramPacket(rbuf, rbuf.length);
            ms = new DatagramSocket(aPort, address);
            ms.setSoTimeout(2000);
        }
    }
    // send a dgram to a server that is not running and then do a recv
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
        DatagramPacket send = new DatagramPacket(new byte[10], 10);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(10000);
        ds.receive(receive);
        ds.close();
        fail("No PortUnreachableException when connected at native level on recv ");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to connect at native level on recv: " + e.toString(), (e instanceof PortUnreachableException));
    }
    // validate that we can send/receive with datagram sockets connected at
    // the native level
    DatagramServer server = null;
    int[] ports = Support_PortManager.getNextPortsForUDP(3);
    int serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(localHost, serverPortNumber);
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can disconnect
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
        ds.disconnect();
        ds.close();
    } catch (Exception e) {
        assertTrue("Unexpected exception when trying to connect at native" + e.toString(), (e instanceof PortUnreachableException));
    }
    // validate that once connected we cannot send to another address
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
        DatagramPacket send = new DatagramPacket(new byte[10], 10, inetAddress, portNumber + 1);
        ds.send(send);
        ds.close();
        fail("No Exception when trying to send to a different address on a connected socket ");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to send to a different address on a connected socket: " + e.toString(), (e instanceof IllegalArgumentException));
    }
    // validate that we can connect, then disconnect, then connect then
    // send/recv
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(localHost, serverPortNumber + 1);
        ds.disconnect();
        ds.connect(localHost, serverPortNumber);
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/disconnect/connect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/disconnect/connect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/disconnect/connect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can connect/disconnect then send/recv to any address
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(localHost, serverPortNumber + 1);
        ds.disconnect();
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length, localHost, serverPortNumber);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/disconnect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/disconnect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/disconnect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/disconnect:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can connect on an allready connected socket and then
    // send/recv
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(localHost, serverPortNumber + 1);
        ds.connect(localHost, serverPortNumber);
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/connect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/connect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/connect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/connect: " + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // there should be no exception
    try {
        ds = new java.net.DatagramSocket();
        byte[] addressBytes = { 0, 0, 0, 0 };
        InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
    } catch (Exception e) {
        fail("Unexcpected exception when trying to connect at native level with bad address for signature with no exception to be returned: " + e.toString());
    }
    System.out.println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6 address");
    try {
        ds = new java.net.DatagramSocket();
        byte[] addressBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(inetAddress, portNumber);
    } catch (Exception e) {
        fail("Unexcpected exception when trying to connect at native level with bad IPv6 address for signature with no exception to be returned: " + e.toString());
    }
}
Also used : PortUnreachableException(java.net.PortUnreachableException) IOException(java.io.IOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) DatagramSocket(java.net.DatagramSocket) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Example 3 with PortUnreachableException

use of java.net.PortUnreachableException in project webpieces by deanhiller.

the class TestUdpIntegration method setupPortUnreachable.

/**
 * @param svrAddr
 * @throws IOException
 * @throws InterruptedException
 */
private InetSocketAddress setupPortUnreachable(InetSocketAddress svrAddr) throws IOException, InterruptedException {
    InetAddress localhost = InetAddress.getLocalHost();
    client.bind(new InetSocketAddress(localhost, 0));
    InetSocketAddress clientAddr = client.getLocalAddress();
    client.oldConnect(svrAddr);
    client.registerForReads((DataListener) clientHandler);
    String msg = "aaaaa";
    // should result in port unreachable
    writePacket(client, msg);
    // expect the exception
    CalledMethod m = clientHandler.expect("failure");
    PortUnreachableException exc = (PortUnreachableException) m.getAllParams()[2];
    log.log(Level.FINE, "this is expected", exc);
    return clientAddr;
}
Also used : PortUnreachableException(java.net.PortUnreachableException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 4 with PortUnreachableException

use of java.net.PortUnreachableException in project pinpoint by naver.

the class UdpDataSender method sendPacket.

private void sendPacket(T message) {
    final InetSocketAddress inetSocketAddress = socketAddressProvider.resolve();
    if (inetSocketAddress.getAddress() == null) {
        logger.info("dns lookup fail host:{}", inetSocketAddress);
        return;
    }
    final ByteMessage byteMessage = messageSerializer.serializer(message);
    if (byteMessage == null) {
        logger.warn("sendPacket fail. message:{}", message != null ? message.getClass() : null);
        if (logger.isDebugEnabled()) {
            logger.debug("unknown message:{}", message);
        }
        return;
    }
    final DatagramPacket packet = preparePacket(inetSocketAddress, byteMessage);
    try {
        udpSocket.send(packet);
        if (isDebug) {
            logger.debug("Data sent. size:{}, {}", byteMessage.getLength(), message);
        }
    } catch (PortUnreachableException pe) {
        this.socketAddressProvider.handlePortUnreachable();
        logger.info("packet send error. size:{}, {}", byteMessage.getLength(), message, pe);
    } catch (IOException e) {
        logger.info("packet send error. size:{}, {}", byteMessage.getLength(), message, e);
    }
}
Also used : PortUnreachableException(java.net.PortUnreachableException) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 5 with PortUnreachableException

use of java.net.PortUnreachableException in project robovm by robovm.

the class OldDatagramSocketTest method test_connectLjava_net_SocketAddress.

public void test_connectLjava_net_SocketAddress() {
    // send a dgram to a server that is not running and then do a recv
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(new InetSocketAddress(inetAddress, portNumber));
        DatagramPacket send = new DatagramPacket(new byte[10], 10);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(10000);
        ds.receive(receive);
        ds.close();
        fail("No PortUnreachableException when connected at native level on recv ");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to connect at native level on recv: " + e.toString(), (e instanceof PortUnreachableException));
    }
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(new InetSocketAddress("asdfasdf", 1));
        ds.close();
        fail("SocketException was not thrown.");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to connect to unknown host: " + e.toString(), (e instanceof SocketException));
    }
    // validate that we can send/receive with datagram sockets connected at
    // the native level
    DatagramServer server = null;
    int[] ports = Support_PortManager.getNextPortsForUDP(3);
    int serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(new InetSocketAddress(localHost, serverPortNumber));
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can disconnect
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(new InetSocketAddress(inetAddress, portNumber));
        ds.disconnect();
        ds.close();
    } catch (Exception e) {
        assertTrue("Unexpected exception when trying to connect at native" + e.toString(), (e instanceof PortUnreachableException));
    }
    // validate that once connected we cannot send to another address
    try {
        ds = new java.net.DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        int portNumber = Support_PortManager.getNextPortForUDP();
        ds.connect(new InetSocketAddress(inetAddress, portNumber));
        DatagramPacket send = new DatagramPacket(new byte[10], 10, inetAddress, portNumber + 1);
        ds.send(send);
        ds.close();
        fail("No Exception when trying to send to a different address on a connected socket ");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to send to a different address on a connected socket: " + e.toString(), (e instanceof IllegalArgumentException));
    }
    // validate that we can connect, then disconnect, then connect then
    // send/recv
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
        ds.disconnect();
        ds.connect(new InetSocketAddress(localHost, serverPortNumber));
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/disconnect/connect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/disconnect/connect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/disconnect/connect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can connect/disconnect then send/recv to any address
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
        ds.disconnect();
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length, localHost, serverPortNumber);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/disconnect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/disconnect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/disconnect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/disconnect:" + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // validate that we can connect on an allready connected socket and then
    // send/recv
    server = null;
    ports = Support_PortManager.getNextPortsForUDP(3);
    serverPortNumber = ports[0];
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramSocket ds = new DatagramSocket(ports[1]);
        DatagramSocket ds2 = new DatagramSocket(ports[2]);
        try {
            server = new DatagramServer(serverPortNumber, localHost);
            server.start();
            Thread.sleep(1000);
        } catch (Exception e) {
            fail("Failed to set up datagram server for native connected Dgram socket test ");
        }
        int port = ds.getLocalPort();
        ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
        ds.connect(new InetSocketAddress(localHost, serverPortNumber));
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        DatagramPacket receive = new DatagramPacket(new byte[20], 20);
        ds.setSoTimeout(2000);
        ds.receive(receive);
        ds.close();
        assertTrue("connect/connect - Wrong size data received: " + receive.getLength(), receive.getLength() == sendBytes.length);
        assertTrue("connect/connect - Wrong data received" + new String(receive.getData(), 0, receive.getLength()) + ":" + new String(sendBytes), new String(receive.getData(), 0, receive.getLength()).equals(new String(sendBytes)));
        assertTrue("connect/connect - Wrong receiver:" + receive.getAddress() + ":" + localHost, receive.getAddress().equals(localHost));
    } catch (Exception e) {
        fail("Unexpected exception when sending data on dgram connected at native level after connect/connect: " + e.toString());
    }
    if (server != null) {
        server.stopServer();
    }
    // connected at the Java level.
    try {
        ds = new java.net.DatagramSocket();
        byte[] addressBytes = { 0, 0, 0, 0 };
        InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
        int portNumber = Support_PortManager.getNextPortForUDP();
        InetAddress localHost = InetAddress.getLocalHost();
        ds.connect(new InetSocketAddress(inetAddress, portNumber));
        assertTrue("Is not connected after connect to inaddr any", ds.isConnected());
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length, localHost, portNumber);
        ds.send(send);
        fail("No exception when trying to connect at native level with bad address (exception from send)  ");
    } catch (Exception e) {
        assertTrue("Wrong exception when trying to connect at native level with bad address (exception from send): " + e.toString(), (e instanceof IllegalArgumentException));
    }
}
Also used : SocketException(java.net.SocketException) PortUnreachableException(java.net.PortUnreachableException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) DatagramSocket(java.net.DatagramSocket) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Aggregations

PortUnreachableException (java.net.PortUnreachableException)16 IOException (java.io.IOException)13 InterruptedIOException (java.io.InterruptedIOException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 DatagramPacket (java.net.DatagramPacket)6 SocketException (java.net.SocketException)6 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 DatagramSocket (java.net.DatagramSocket)4 InetAddress (java.net.InetAddress)4 EOFException (java.io.EOFException)3 MalformedURLException (java.net.MalformedURLException)3 ByteBuffer (java.nio.ByteBuffer)3 CertPathValidatorException (java.security.cert.CertPathValidatorException)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 SSLException (javax.net.ssl.SSLException)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 Test (org.junit.Test)3 BindException (java.net.BindException)2 NoRouteToHostException (java.net.NoRouteToHostException)2