FileDocCategorySizeDatePackage
DomainCombinerTest.javaAPI DocAndroid 1.5 API5251Wed May 06 22:41:06 BST 2009tests.api.java.security

DomainCombinerTest

public class DomainCombinerTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
public voidtest_combine$Ljava_security_ProtectionDomain$Ljava_security_ProtectionDomain()

tests
java.security.DomainCombiner#combine(java.security.ProtectionDomain[], java.security.ProtectionDomain[])

        final boolean[] calledDomainCombiner = new boolean[] { false, false };

        class MyCombiner implements DomainCombiner {
            int i;

            MyCombiner(int i) {
                this.i = i;
            }

            public ProtectionDomain[] combine(
                    ProtectionDomain[] executionDomains,
                    ProtectionDomain[] parentDomains) {
                calledDomainCombiner[i] = true;
                PermissionCollection pc = new Permissions();
                pc.add(new AllPermission());
                ProtectionDomain pd;
                // if run with the system classloader then there will be no
                // execution domains 
                if (executionDomains.length > 0) {
                    pd = new ProtectionDomain(executionDomains[0]
                            .getCodeSource(), pc);
                } else {
                    pd = new ProtectionDomain(parentDomains[0].getCodeSource(),
                            pc);
                }
                return new ProtectionDomain[] { pd };
            }
        }

        ProtectionDomain[] domains = new ProtectionDomain[] { new ProtectionDomain(
                new CodeSource(null, (Certificate[]) null), new Permissions()) };

        AccessControlContext parent = new AccessControlContext(domains);
        AccessControlContext c0 = new AccessControlContext(parent,
                new MyCombiner(0));
        final AccessControlContext c1 = new AccessControlContext(parent,
                new MyCombiner(1));

                class TestPermission extends BasicPermission {
                    TestPermission(String s) {
                        super(s);
                    }
                }
        
        SecurityManager sm = new SecurityManager() {
            public void checkPermission(Permission p) {
                if( p instanceof TestPermission ) {
                    super.checkPermission(p);   
                }
            }
        };
        sm.checkPermission(new SecurityPermission("let it load"));
        
        System.setSecurityManager(sm);
        try {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    // AccessController.getContext();
                    AccessController.checkPermission(new TestPermission(
                            "MyTest"));

                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            AccessController
                                    .checkPermission(new TestPermission(
                                            "MyTest"));
                            return null;
                        }
                    }, c1);
                    return null;
                }
            }, c0);
            assertTrue("Failed to combine domains for security permission",
                    calledDomainCombiner[0]);
            assertTrue("Failed to combine domains for security permission",
                    calledDomainCombiner[1]);
        } finally {
            System.setSecurityManager(null);
        }