Search in sources :

Example 1 with DatagramSocketImpl

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

the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.

public void test_sendLjava_net_DatagramPacket() throws Exception {
    // Test for method void
    // java.net.DatagramSocket.send(java.net.DatagramPacket)
    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    final int portNumber = ports[0];
    class TestDGSend implements Runnable {

        Thread pThread;

        public TestDGSend(Thread t) {
            pThread = t;
        }

        public void run() {
            try {
                byte[] rbuf = new byte[1000];
                sds = new DatagramSocket(portNumber);
                DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
                sds.setSoTimeout(6000);
                sds.receive(sdp);
                retval = new String(rbuf, 0, testString.length());
                pThread.interrupt();
            } catch (java.io.InterruptedIOException e) {
                System.out.println("Recv operation timed out");
                pThread.interrupt();
                ds.close();
            } catch (Exception e) {
                System.out.println("Failed to establish Dgram server: " + e);
            }
        }
    }
    try {
        new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
        ds = new java.net.DatagramSocket(ports[1]);
        dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
        // Wait to allow send to occur
        try {
            Thread.sleep(500);
            ds.send(dp);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            ds.close();
            assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
        }
    } catch (Exception e) {
        fail("Exception during send test : " + e.getMessage());
    } finally {
        ds.close();
    }
    /*
        SecurityManager sm = new SecurityManager() {

            public void checkPermission(Permission perm) {
            }

            public void checkMulticast(InetAddress maddr) {
                throw new SecurityException();
            }

            public void checkConnect(String host,
                    int port) {
                throw new SecurityException();
            }
        };
        try {

            ds = new java.net.DatagramSocket(ports[1]);
            dp = new DatagramPacket(testString.getBytes(), testString.length(),
                    InetAddress.getLocalHost(), portNumber);

            SecurityManager oldSm = System.getSecurityManager();
            System.setSecurityManager(sm);
            try {
                ds.send(dp);
                fail("SecurityException should be thrown.");
            } catch (SecurityException e) {
                // expected
            } catch (SocketException e) {
                fail("SocketException was thrown.");
            } finally {
                System.setSecurityManager(oldSm);
            }
        } catch(Exception e) {
            fail("Unexpected exception was thrown: " + e.getMessage());
        }
        */
    DatagramSocket socket = null;
    try {
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        SocketAddress address = new InetSocketAddress(portNumber);
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);
        socket = channel.socket();
        socket.send(rdp);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } catch (IOException ioe) {
        fail("IOException was thrown: " + ioe.getMessage());
    } finally {
        socket.close();
    }
    //Regression for HARMONY-1118
    class testDatagramSocket extends DatagramSocket {

        public testDatagramSocket(DatagramSocketImpl impl) {
            super(impl);
        }
    }
    class testDatagramSocketImpl extends DatagramSocketImpl {

        protected void create() throws SocketException {
        }

        protected void bind(int arg0, InetAddress arg1) throws SocketException {
        }

        protected void send(DatagramPacket arg0) throws IOException {
        }

        protected int peek(InetAddress arg0) throws IOException {
            return 0;
        }

        protected int peekData(DatagramPacket arg0) throws IOException {
            return 0;
        }

        protected void receive(DatagramPacket arg0) throws IOException {
        }

        protected void setTTL(byte arg0) throws IOException {
        }

        protected byte getTTL() throws IOException {
            return 0;
        }

        protected void setTimeToLive(int arg0) throws IOException {
        }

        protected int getTimeToLive() throws IOException {
            return 0;
        }

        protected void join(InetAddress arg0) throws IOException {
        }

        protected void leave(InetAddress arg0) throws IOException {
        }

        protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void close() {
        }

        public void setOption(int arg0, Object arg1) throws SocketException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }
    }
    InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    //no exception expected for next line
    new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
    // Regression test for Harmony-2938
    InetAddress i = InetAddress.getByName("127.0.0.1");
    DatagramSocket d = new DatagramSocket(0, i);
    try {
        d.send(new DatagramPacket(new byte[] { 1 }, 1));
        fail("should throw NPE.");
    } catch (NullPointerException e) {
    // expected;
    } finally {
        d.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) 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) DatagramSocketImpl(java.net.DatagramSocketImpl) DatagramSocket(java.net.DatagramSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) InetAddress(java.net.InetAddress)

Example 2 with DatagramSocketImpl

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

the class OldDatagramSocketImplFactoryTest method test_createDatagramSocketImpl.

public void test_createDatagramSocketImpl() throws IllegalArgumentException, IOException {
    if (isTestable) {
        DatagramSocketImplFactory factory = new TestDatagramSocketImplFactory();
        assertFalse(isCreateDatagramSocketImpl);
        DatagramSocket.setDatagramSocketImplFactory(factory);
        try {
            new java.net.DatagramSocket();
            assertTrue(isCreateDatagramSocketImpl);
            assertTrue(isDatagramSocketImplCalled);
        } catch (Exception e) {
            fail("Exception during test : " + e.getMessage());
        }
        try {
            DatagramSocket.setDatagramSocketImplFactory(factory);
            fail("SocketException was not thrown.");
        } catch (SocketException se) {
        //expected
        }
        try {
            DatagramSocket.setDatagramSocketImplFactory(null);
            fail("SocketException was not thrown.");
        } catch (SocketException se) {
        //expected
        }
    } else {
        TestDatagramSocketImplFactory dsf = new TestDatagramSocketImplFactory();
        DatagramSocketImpl dsi = dsf.createDatagramSocketImpl();
        try {
            assertNull(dsi.getOption(0));
        } catch (SocketException e) {
            fail("SocketException was thrown.");
        }
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocketImpl(java.net.DatagramSocketImpl) DatagramSocket(java.net.DatagramSocket) DatagramSocketImplFactory(java.net.DatagramSocketImplFactory) SocketException(java.net.SocketException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 DatagramSocket (java.net.DatagramSocket)2 DatagramSocketImpl (java.net.DatagramSocketImpl)2 SocketException (java.net.SocketException)2 InterruptedIOException (java.io.InterruptedIOException)1 BindException (java.net.BindException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocketImplFactory (java.net.DatagramSocketImplFactory)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 NetworkInterface (java.net.NetworkInterface)1 PortUnreachableException (java.net.PortUnreachableException)1 SocketAddress (java.net.SocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 DatagramChannel (java.nio.channels.DatagramChannel)1 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)1