FileDocCategorySizeDatePackage
SocketTest.javaAPI DocAndroid 1.5 API9127Wed May 06 22:42:02 BST 2009android.core

SocketTest

public class SocketTest extends TestCase
Regression tests for various socket related problems. And a few general socket tests.

Fields Summary
private static final String
NON_EXISTING_ADDRESS
private static final String
KNOW_GOOD_ADDRESS
private static final String
PACKAGE_DROPPING_ADDRESS
private Exception
serverError
private Socket
client
private Exception
error
private boolean
connected
Constructors Summary
Methods Summary
public voiddisable_testConnectWithIP4IPAddr()
Regression test for 1062928: Dotted IP addresses (e.g., 192.168.100.1) appear to be broken in the M5 SDK. Tests that a connection given a ip-addressv4 such as 192.168.100.100 does not fail - sdk m5 seems only to accept dns names instead of ip numbers. ip 209.85.129.147 (one address of www.google.com) on port 80 (http) is used to test the connection.

        // call a Google Web server
        InetSocketAddress scktAddrss = new InetSocketAddress(KNOW_GOOD_ADDRESS,
                80);
        Socket clntSckt = new Socket();
        try {
            clntSckt.connect(scktAddrss, 5000);
        } catch (Throwable e) {
            fail("connection problem:" + e.getClass().getName() + ": "
                    + e.getMessage());
        } finally {
            try {
                clntSckt.close();
            } catch (Exception e) {
                // ignore
            }
        }
    
public voiddisable_testSocketConnectClose()

        try {
            client = new Socket();
            
            new Thread() {
                @Override
                public void run() {
                    try {
                        client.connect(new InetSocketAddress(PACKAGE_DROPPING_ADDRESS, 1357));
                    } catch (Exception ex) {
                        error = ex;
                    }
                    
                    connected = true;
                }
            }.start();
            
            Thread.sleep(1000);
            
            Assert.assertNull("Connect must not fail immediately. Maybe try different address.", error);
            Assert.assertFalse("Connect must not succeed. Maybe try different address.", connected);
            
            client.close();
            
            Thread.sleep(1000);

            if (error == null) {
                fail("Socket connect still ongoing");
            } else if (!(error instanceof SocketException)) {
                fail("Socket connect interrupted with wrong error: " + error.toString());
            }
            
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
            
    
public voidtestNativeSocketChannelOpen()

        SocketChannel.open();
    
public voidtestServerSocketClose()

        ServerSocket s3 = new ServerSocket(23456);
        s3.close();
        ServerSocket s4 = new ServerSocket(23456);
        s4.close();
    
public voidtestSetReuseAddress()

    
    
         
        InetSocketAddress addr = new InetSocketAddress(8383);

        final ServerSocket serverSock = new ServerSocket();
        serverSock.setReuseAddress(true);
        serverSock.bind(addr);

        final Semaphore semThreadEnd = new Semaphore(0);
        new Thread() {
            @Override
            public void run() {
                try {
                    Socket sock = serverSock.accept();
                    sock.getInputStream().read();
                    sock.close();
                } catch (IOException e) {
                    serverError = e;
                }
                semThreadEnd.release();
            }
        }.start();

        // Give the server a bit of time for startup
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            // Ignored.
        }
        
        Socket client = new Socket("localhost", 8383);
        client.getOutputStream().write(1);
        // Just leave this connection open from the client side. It will be
        // closed from the server side so the server stays in the TIME_WAIT
        // state for a while. setReuseAddress() should be able to handle this.

        try {
            semThreadEnd.acquire();
        } catch (InterruptedException e) {
            // ignore
        }
        serverSock.close();

        ServerSocket serverSock2 = new ServerSocket();
        serverSock2.setReuseAddress(true);
        serverSock2.bind(addr);
        serverSock2.close();
        
        if (serverError != null) {
            throw new RuntimeException("Server must complete without error", serverError);
        }
    
public voidtestSocketSetSOTimeout()

        Socket sock = new Socket();
        int timeout = 5000;
        long start = System.currentTimeMillis();
        try {
            sock.connect(new InetSocketAddress(NON_EXISTING_ADDRESS, 80), timeout);
        } catch (SocketTimeoutException e) {
            // expected
            long delay = System.currentTimeMillis() - start;
            if (Math.abs(delay - timeout) > 1000) {
                fail("timeout was not accurate. expected: " + timeout
                        + " actual: " + delay + " miliseconds.");
            }
        } finally {
            try {
                sock.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    
public voidtestSocketSimple()

    
    // Test for basic bind/connect/accept behavior.
    
         
        ServerSocket ss;
        Socket s, s1;
        int port;

        IOException lastEx = null;

        ss = new ServerSocket();

        for (port = 9900; port < 9999; port++) {
            try {
                ss.bind(new InetSocketAddress("127.0.0.1", port));
                lastEx = null;
                break;
            } catch (IOException ex) {
                lastEx = ex;
            }
        }

        if (lastEx != null) {
            throw lastEx;
        }

        s = new Socket("127.0.0.1", port);

        s1 = ss.accept();

        s.getOutputStream().write(0xa5);

        assertEquals(0xa5, s1.getInputStream().read());

        s1.getOutputStream().write(0x5a);
        assertEquals(0x5a, s.getInputStream().read());
    
public voidtestTimeoutException()

        ServerSocket s = new ServerSocket(9800);
        s.setSoTimeout(2000);
        try {
            s.accept();
        } catch (SocketTimeoutException e) {
            // this is ok.
        }
    
public voidtestWildcardAddress()

        Socket s2 = new Socket();
        s2.bind(new InetSocketAddress((InetAddress) null, 12345));
        byte[] addr = s2.getLocalAddress().getAddress();
        for (int i = 0; i < 4; i++) {
            assertEquals("Not the wildcard address", 0, addr[i]);
        }