FileDocCategorySizeDatePackage
JavaLangRuntimeTest.javaAPI DocAndroid 1.5 API11097Wed May 06 22:41:06 BST 2009tests.security.permissions

JavaLangRuntimeTest

public class JavaLangRuntimeTest 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_exec()

        class TestSecurityManager extends SecurityManager {
            boolean called;
            String cmd;
            void reset(){
                called = false;
                cmd = null;
            }
            @Override
            public void checkExec(String cmd) {
                called = true;
                this.cmd = cmd;
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }
        
        String cmd = "ls";
        String arg = "-al";
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        Runtime.getRuntime().exec(cmd);
        assertTrue("Runtime.exec must call checkExcec on security manager", s.called);
        assertEquals("Argument of checkExec is not correct", cmd, s.cmd);
        
        s.reset();
        Runtime.getRuntime().exec(cmd, null);
        assertTrue("Runtime.exec must call checkExcec on security manager", s.called);
        assertEquals("Argument of checkExec is not correct", cmd, s.cmd);
        
        s.reset();
        Runtime.getRuntime().exec(new String[]{cmd, arg});
        assertTrue("Runtime.exec must call checkExcec on security manager", s.called);
        assertEquals("Argument of checkExec is not correct", cmd, s.cmd);
        
        s.reset();
        Runtime.getRuntime().exec(new String[]{cmd, arg}, null);
        assertTrue("Runtime.exec must call checkExcec on security manager", s.called);
        assertEquals("Argument of checkExec is not correct", cmd, s.cmd);
    
public voidtest_exit()

        class ExitNotAllowedException extends RuntimeException {}
        class TestSecurityManager extends SecurityManager {
            boolean called;
            int status;
            void reset(){
                called = false;
                status = -1;
            }
            @Override
            public void checkExit(int status){
                this.called = true;
                this.status = status;
                throw new ExitNotAllowedException(); // prevent that the system is shut down
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);

        s.reset();
        try {
            Runtime.getRuntime().exit(11);
            fail("Runtime.exit must call checkExit on security manager with a RuntimePermission");
        }
        catch(ExitNotAllowedException e){
             // expected exception
        }
        assertTrue("Runtime.exit must call checkExit on security manager with a RuntimePermission", s.called);
        assertEquals("Argument of checkExit is not correct", 11, s.status);
    
public voidtest_load()

        final String library = "library";
        
        class CheckLinkCalledException extends RuntimeException {}
        
        class TestSecurityManager extends SecurityManager {
            @Override
            public void checkLink(String lib){
                if(library.equals(lib)){
                    throw new CheckLinkCalledException();
                }
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);

        try {
            Runtime.getRuntime().load(library);
            fail("System.load must call checkLink on security manager with argument "+library);
        }
        catch(CheckLinkCalledException e){
            // ok
        }
        catch(Throwable t){
            fail("System.load must call checkLink on security manager with argument "+library);
        }
        
        try {
            Runtime.getRuntime().loadLibrary(library);
            fail("System.load must call checkLink on security manager with argument "+library);
        }
        catch(CheckLinkCalledException e){
            // ok
        }
        catch(Throwable t){
            fail("System.load must call checkLink on security manager with argument "+library);
        }
    
public voidtest_runFinalizersOnExit()

        class TestSecurityManager extends SecurityManager {
            boolean called;
            int status;
            void reset(){
                called = false;
                status = -1;
            }
            @Override
            public void checkExit(int status){
                this.called = true;
                this.status = status;
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);

        s.reset();
        Runtime.runFinalizersOnExit(true);
        assertTrue("Runtime.runFinalizersOnExit must call checkExit on security manager with a RuntimePermission", s.called);
        assertEquals("Argument of checkExit is not correct", 0, s.status);

        s.reset();
        Runtime.runFinalizersOnExit(false);
        assertTrue("Runtime.runFinalizersOnExit must call checkExit on security manager with a RuntimePermission", s.called);
        assertEquals("Argument of checkExit is not correct", 0, s.status);
    
public voidtest_shutdownHook()

        class TestSecurityManager extends SecurityManager {
            boolean called;
            Permission permission;
            void reset(){
                called = false;
                permission = null;
            }
            @Override
            public void checkPermission(Permission permission){
                if(permission instanceof RuntimePermission){
                    called = true;
                    this.permission = permission;
                }
            }
        }
        
        Thread hook = new Thread(){};

        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);

        s.reset();
        Runtime.getRuntime().addShutdownHook(hook);
        assertTrue("Runtime.addShutdownHook must call checkPermission on security manager with a RuntimePermission", s.called);
        assertEquals("Name of RuntimePermission passed to checkPermission is not correct", "shutdownHooks", s.permission.getName());

        s.reset();
        Runtime.getRuntime().removeShutdownHook(hook);
        assertTrue("Runtime.removeShutdownHook must call checkPermission on security manager with a RuntimePermission", s.called);
        assertEquals("Name of RuntimePermission passed to checkPermission is not correct", "shutdownHooks", s.permission.getName());