FileDocCategorySizeDatePackage
JavaSecuritySecurityTest.javaAPI DocAndroid 1.5 API7276Wed May 06 22:41:06 BST 2009tests.security.permissions

JavaSecuritySecurityTest

public class JavaSecuritySecurityTest 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_Provider()

        class TestSecurityManager extends SecurityManager {
            boolean called = false;
            Set<String> targets = new HashSet<String>();
            void reset(){
                called = false;
                targets.clear();
            }
            @Override
            public void checkSecurityAccess(String target) {
                called = true;       
                this.targets.add(target);
                super.checkSecurityAccess(target);
            }         
            
            @Override
            public void checkPermission(Permission permission) {
            }
        }
        
        class MyProvider extends Provider {
            private static final long serialVersionUID = 1L;
            MyProvider(){
                super("DummyProvider", 1.0, "Provider for test purposes only");
            }
        }
        
        Provider p = new MyProvider();
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();        
        Security.addProvider(p);
        assertTrue("java.security.Security.addProvider() must call checkSecurityAccess on security manager", s.called);
        assertTrue("Argument of checkSecurityAccess is not correct", s.targets.contains("insertProvider.DummyProvider"));
        
        s.reset();        
        Security.removeProvider(p.getName());
        assertTrue("java.security.Security.removeProvider() must call checkSecurityAccess on security manager", s.called);
        assertTrue("Argument of checkSecurityAccess is not correct", s.targets.contains("removeProvider.DummyProvider"));

        s.reset();        
        Security.insertProviderAt(p, 0);
        assertTrue("java.security.Security.insertProviderAt() must call checkSecurityAccess on security manager", s.called);
        assertTrue("Argument of checkSecurityAccess is not correct", s.targets.contains("insertProvider.DummyProvider"));

        Security.removeProvider(p.getName());
    
public voidtest_getProperty()

        class TestSecurityManager extends SecurityManager {
            boolean called = false;
            String target = null;
            void reset() {
                called = false;
                target = null;
            }

            @Override
            public void checkPermission(Permission permission) {
                if (permission instanceof SecurityPermission) {
                  target = permission.getName();
                    if (target.equals("getProperty.key")) {
                        called = true;
                    }
                }
            }

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

        s.reset();
        Security.getProperty("key");
        assertTrue("java.security.Security.getProperty() must call checkSecurityAccess on security manager", s.called);
        assertEquals("Argument of checkSecurityAccess is not correct", "getProperty.key", s.target);
    
public voidtest_setProperty()

        class TestSecurityManager extends SecurityManager {
            boolean called = false;
            String target = null;
            void reset(){
                called = false;
                target = null;
            }
            
            @Override
            public void checkPermission(Permission permission) {
                if (permission instanceof SecurityPermission) {
                  target = permission.getName();
                    if (target.equals("setProperty.key")) {
                        called = true;
                    }
                }
            }
        }
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        Security.setProperty("key", "value");
        assertTrue("java.security.Security.setProperty() must call checkSecurityAccess on security manager", s.called);
        assertEquals("Argument of checkSecurityAccess is not correct", "setProperty.key", s.target);