FileDocCategorySizeDatePackage
JavaNetSocketTest.javaAPI DocAndroid 1.5 API8048Wed May 06 22:41:06 BST 2009tests.security.permissions

JavaNetSocketTest

public class JavaNetSocketTest extends TestCase

Fields Summary
SecurityManager
old
Constructors Summary
Methods Summary
protected voidsetUp()

        old = System.getSecurityManager();
        super.setUp();
    
protected voidtearDown()

        System.setSecurityManager(old);
        super.tearDown();
    
public voidtest_ctor()

        class TestSecurityManager extends SecurityManager {
            boolean called = false;
            String host = null;
            int port = -1;
            void reset(){
                called = false;
                host = null;
                port = -1;
            }
            @Override
            public void checkConnect(String host, int port) {
                this.called = true;
                this.port = port;
                this.host = host;
            }
            @Override
            public void checkPermission(Permission permission) {
                
            }
        }
        
        String host = "www.google.ch";
        int port = 80;
        String hostAddress = InetAddress.getByName(host).getHostAddress();

        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        new Socket(host, port);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);
        
        s.reset();
        new Socket(host, port, true);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);

        s.reset();
        new Socket(host, port, InetAddress.getByAddress(new byte[] {0,0,0,0}), 0);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);
        
        s.reset();
        new Socket(InetAddress.getByName(host), port);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);
        
        s.reset();
        new Socket(InetAddress.getByName(host), port, true);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);
        
        s.reset();
        new Socket(InetAddress.getByName(host), port,  InetAddress.getByAddress(new byte[] {0,0,0,0}), 0);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);
    
public voidtest_ctor2()

        class TestSecurityManager extends SecurityManager {
            boolean called = false;
            String host = null;
            int port = -1;
            void reset(){
                called = false;
                host = null;
                port = -1;
            }
            @Override
            public void checkConnect(String host, int port) {
                this.called = true;
                this.port = port;
                this.host = host;
            }
            @Override
            public void checkPermission(Permission permission) {
                
            }
        }
        
        String host = "www.google.ch";
        int port = 80;
        String hostAddress = InetAddress.getByName(host).getHostAddress();

        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        new Socket(InetAddress.getByName(host), port,  InetAddress.getLocalHost(), 0);
        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
        assertEquals("Argument of checkConnect is not correct", port, s.port);