Search in sources :

Example 31 with Permission

use of java.security.Permission in project rt.equinox.framework by eclipse.

the class PermissionTests method testSerialization.

protected void testSerialization(Permission p1) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(p1);
        out.flush();
        out.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bais);
        Permission p2 = (Permission) in.readObject();
        shouldEqual(p1, p2);
        shouldImply(p1, p2);
        shouldImply(p2, p1);
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : PackagePermission(org.osgi.framework.PackagePermission) Permission(java.security.Permission) ServicePermission(org.osgi.framework.ServicePermission) NoSuchElementException(java.util.NoSuchElementException)

Example 32 with Permission

use of java.security.Permission in project rt.equinox.framework by eclipse.

the class PackagePermissionTests method testPackagePermission.

public void testPackagePermission() {
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "x");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "   get  ,  x   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "      ");
    // $NON-NLS-1$
    badPackagePermission("a.b.c", null);
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", ",");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", ",xxx");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "xxx,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "import,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "export,   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "importme,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "exportme,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", ",import");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", ",export");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "   importme   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "   exportme     ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "   impor");
    // $NON-NLS-1$ //$NON-NLS-2$
    badPackagePermission("a.b.c", "   expor");
    // $NON-NLS-1$ //$NON-NLS-2$
    Permission op = new PropertyPermission("java.home", "read");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p11 = new PackagePermission("com.foo.service1", "    IMPORT,export   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p12 = new PackagePermission("com.foo.service1", "EXPORT  ,   import");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p13 = new PackagePermission("com.foo.service1", "expORT   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p14 = new PackagePermission("com.foo.service1", "    Import    ");
    shouldImply(p11, p11);
    shouldImply(p11, p12);
    shouldImply(p11, p13);
    shouldImply(p11, p14);
    shouldImply(p12, p11);
    shouldImply(p12, p12);
    shouldImply(p12, p13);
    shouldImply(p12, p14);
    shouldImply(p13, p11);
    shouldImply(p13, p12);
    shouldImply(p13, p13);
    shouldImply(p13, p14);
    shouldImply(p14, p14);
    shouldNotImply(p14, p11);
    shouldNotImply(p14, p12);
    shouldNotImply(p14, p13);
    shouldNotImply(p11, op);
    shouldEqual(p11, p11);
    shouldEqual(p11, p12);
    shouldEqual(p11, p13);
    shouldEqual(p12, p11);
    shouldEqual(p12, p12);
    shouldEqual(p12, p13);
    shouldEqual(p13, p11);
    shouldEqual(p13, p12);
    shouldEqual(p13, p13);
    shouldNotEqual(p11, p14);
    shouldNotEqual(p12, p14);
    shouldNotEqual(p13, p14);
    shouldNotEqual(p14, p11);
    shouldNotEqual(p14, p12);
    shouldNotEqual(p14, p13);
    PermissionCollection pc = p13.newPermissionCollection();
    checkEnumeration(pc.elements(), true);
    shouldNotImply(pc, p11);
    shouldAdd(pc, p14);
    shouldImply(pc, p14);
    shouldNotImply(pc, p11);
    shouldNotImply(pc, p12);
    shouldNotImply(pc, p13);
    shouldAdd(pc, p13);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    shouldNotAdd(pc, op);
    pc = p13.newPermissionCollection();
    shouldAdd(pc, p13);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    pc = p11.newPermissionCollection();
    shouldAdd(pc, p11);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    pc.setReadOnly();
    shouldNotAdd(pc, p12);
    checkEnumeration(pc.elements(), false);
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p21 = new PackagePermission("com.foo.service2", "import");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p22 = new PackagePermission("com.foo.*", "import");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p23 = new PackagePermission("com.*", "import");
    // $NON-NLS-1$ //$NON-NLS-2$
    PackagePermission p24 = new PackagePermission("*", "import");
    shouldImply(p21, p21);
    shouldImply(p22, p21);
    shouldImply(p23, p21);
    shouldImply(p24, p21);
    shouldImply(p22, p22);
    shouldImply(p23, p22);
    shouldImply(p24, p22);
    shouldImply(p23, p23);
    shouldImply(p24, p23);
    shouldImply(p24, p24);
    shouldNotImply(p21, p22);
    shouldNotImply(p21, p23);
    shouldNotImply(p21, p24);
    shouldNotImply(p22, p23);
    shouldNotImply(p22, p24);
    shouldNotImply(p23, p24);
    pc = p21.newPermissionCollection();
    shouldAdd(pc, p21);
    shouldImply(pc, p21);
    shouldNotImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p22);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p23);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p24);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldImply(pc, p24);
    pc = p22.newPermissionCollection();
    shouldAdd(pc, p22);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    pc = p23.newPermissionCollection();
    shouldAdd(pc, p23);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldNotImply(pc, p24);
    pc = p24.newPermissionCollection();
    shouldAdd(pc, p24);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldImply(pc, p24);
    testSerialization(p11);
    testSerialization(p12);
    testSerialization(p13);
    testSerialization(p14);
    testSerialization(p21);
    testSerialization(p22);
    testSerialization(p23);
    testSerialization(p24);
}
Also used : PermissionCollection(java.security.PermissionCollection) PropertyPermission(java.util.PropertyPermission) PackagePermission(org.osgi.framework.PackagePermission) Permission(java.security.Permission) PropertyPermission(java.util.PropertyPermission) PackagePermission(org.osgi.framework.PackagePermission)

Example 33 with Permission

use of java.security.Permission in project rt.equinox.framework by eclipse.

the class ServicePermissionTests method testServicePermission.

public void testServicePermission() {
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "x");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "   get  ,  x   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "      ");
    // $NON-NLS-1$
    badServicePermission("a.b.c", null);
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", ",");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", ",xxx");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "xxx,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "get,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "register,   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "getme,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "registerme,");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", ",get");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", ",register");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "   getme   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "   registerme     ");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "   ge");
    // $NON-NLS-1$ //$NON-NLS-2$
    badServicePermission("a.b.c", "   registe");
    // $NON-NLS-1$ //$NON-NLS-2$
    Permission op = new PropertyPermission("java.home", "read");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p11 = new ServicePermission("com.foo.service1", "    GET,register   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p12 = new ServicePermission("com.foo.service1", "REGISTER  ,   get");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p13 = new ServicePermission("com.foo.service1", "regisTER   ");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p14 = new ServicePermission("com.foo.service1", "    Get    ");
    shouldImply(p11, p11);
    shouldImply(p11, p12);
    shouldImply(p11, p13);
    shouldImply(p11, p14);
    shouldImply(p12, p11);
    shouldImply(p12, p12);
    shouldImply(p12, p13);
    shouldImply(p12, p14);
    shouldImply(p13, p13);
    shouldImply(p14, p14);
    shouldNotImply(p13, p11);
    shouldNotImply(p13, p12);
    shouldNotImply(p14, p11);
    shouldNotImply(p14, p12);
    shouldNotImply(p13, p14);
    shouldNotImply(p14, p13);
    shouldNotImply(p11, op);
    shouldEqual(p11, p11);
    shouldEqual(p11, p12);
    shouldEqual(p12, p11);
    shouldEqual(p12, p12);
    shouldEqual(p13, p13);
    shouldEqual(p14, p14);
    shouldNotEqual(p11, p13);
    shouldNotEqual(p11, p14);
    shouldNotEqual(p12, p13);
    shouldNotEqual(p12, p14);
    shouldNotEqual(p13, p11);
    shouldNotEqual(p13, p12);
    shouldNotEqual(p13, p14);
    shouldNotEqual(p14, p11);
    shouldNotEqual(p14, p12);
    shouldNotEqual(p14, p13);
    PermissionCollection pc = p13.newPermissionCollection();
    checkEnumeration(pc.elements(), true);
    shouldNotImply(pc, p11);
    shouldAdd(pc, p14);
    shouldImply(pc, p14);
    shouldNotImply(pc, p11);
    shouldNotImply(pc, p12);
    shouldNotImply(pc, p13);
    shouldAdd(pc, p13);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    shouldNotAdd(pc, op);
    pc = p13.newPermissionCollection();
    shouldAdd(pc, p13);
    shouldImply(pc, p13);
    shouldNotImply(pc, p11);
    shouldNotImply(pc, p12);
    shouldNotImply(pc, p14);
    shouldAdd(pc, p14);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    pc = p11.newPermissionCollection();
    shouldAdd(pc, p11);
    shouldImply(pc, p11);
    shouldImply(pc, p12);
    shouldImply(pc, p13);
    shouldImply(pc, p14);
    pc.setReadOnly();
    shouldNotAdd(pc, p12);
    checkEnumeration(pc.elements(), false);
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p21 = new ServicePermission("com.foo.service2", "get");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p22 = new ServicePermission("com.foo.*", "get");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p23 = new ServicePermission("com.*", "get");
    // $NON-NLS-1$ //$NON-NLS-2$
    ServicePermission p24 = new ServicePermission("*", "get");
    shouldImply(p21, p21);
    shouldImply(p22, p21);
    shouldImply(p23, p21);
    shouldImply(p24, p21);
    shouldImply(p22, p22);
    shouldImply(p23, p22);
    shouldImply(p24, p22);
    shouldImply(p23, p23);
    shouldImply(p24, p23);
    shouldImply(p24, p24);
    shouldNotImply(p21, p22);
    shouldNotImply(p21, p23);
    shouldNotImply(p21, p24);
    shouldNotImply(p22, p23);
    shouldNotImply(p22, p24);
    shouldNotImply(p23, p24);
    pc = p21.newPermissionCollection();
    shouldAdd(pc, p21);
    shouldImply(pc, p21);
    shouldNotImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p22);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p23);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldNotImply(pc, p24);
    shouldAdd(pc, p24);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldImply(pc, p24);
    pc = p22.newPermissionCollection();
    shouldAdd(pc, p22);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldNotImply(pc, p23);
    shouldNotImply(pc, p24);
    pc = p23.newPermissionCollection();
    shouldAdd(pc, p23);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldNotImply(pc, p24);
    pc = p24.newPermissionCollection();
    shouldAdd(pc, p24);
    shouldImply(pc, p21);
    shouldImply(pc, p22);
    shouldImply(pc, p23);
    shouldImply(pc, p24);
    testSerialization(p11);
    testSerialization(p12);
    testSerialization(p13);
    testSerialization(p14);
    testSerialization(p21);
    testSerialization(p22);
    testSerialization(p23);
    testSerialization(p24);
}
Also used : PermissionCollection(java.security.PermissionCollection) PropertyPermission(java.util.PropertyPermission) ServicePermission(org.osgi.framework.ServicePermission) Permission(java.security.Permission) ServicePermission(org.osgi.framework.ServicePermission) PropertyPermission(java.util.PropertyPermission)

Example 34 with Permission

use of java.security.Permission in project rt.equinox.framework by eclipse.

the class PermissionsHash method implies.

/**
 * Indicates whether the argument permission is implied
 * by the permissions contained in the receiver.
 *
 * @return		boolean
 *					<code>true</code> if the argument permission
 *					is implied by the permissions in the receiver,
 *					and <code>false</code> if it is not.
 * @param		perm java.security.Permission
 *					the permission to check
 */
public boolean implies(Permission perm) {
    Permission p = perms.get(perm);
    if ((p != null) && p.implies(perm)) {
        return true;
    }
    Enumeration<Permission> permsEnum = elements();
    while (permsEnum.hasMoreElements()) {
        if (permsEnum.nextElement().implies(perm)) {
            return true;
        }
    }
    return false;
}
Also used : Permission(java.security.Permission)

Example 35 with Permission

use of java.security.Permission in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testDynamicSecurityManager.

public void testDynamicSecurityManager() throws BundleException {
    SecurityManager sm = System.getSecurityManager();
    assertNull("SecurityManager must be null to test.", sm);
    try {
        // $NON-NLS-1$
        File config = OSGiTestsActivator.getContext().getDataFile(getName());
        Map<String, Object> configuration = new HashMap<String, Object>();
        configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
        Equinox equinox = new Equinox(configuration);
        try {
            equinox.start();
        } catch (BundleException e) {
            // $NON-NLS-1$
            fail("Unexpected exception in start()", e);
        }
        Bundle substitutesA = null;
        try {
            // $NON-NLS-1$
            substitutesA = equinox.getBundleContext().installBundle(installer.getBundleLocation("substitutes.a"));
        } catch (BundleException e1) {
            // $NON-NLS-1$
            fail("failed to install a bundle", e1);
        }
        assertTrue("BundleCould not resolve.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(substitutesA)));
        substitutesA.adapt(BundleWiring.class).findEntries("/", null, 0);
        // set security manager after resolving
        System.setSecurityManager(new SecurityManager() {

            @Override
            public void checkPermission(Permission perm, Object context) {
            // do nothing
            }

            @Override
            public void checkPermission(Permission perm) {
            // do nothing
            }
        });
        equinox.stop();
        try {
            FrameworkEvent event = equinox.waitForStop(10000);
            assertEquals("Wrong event.", FrameworkEvent.STOPPED, event.getType());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            fail("Unexpected interruption.", e);
        }
        // $NON-NLS-1$
        assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
    } finally {
        System.setSecurityManager(null);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Equinox(org.eclipse.osgi.launch.Equinox) Permission(java.security.Permission) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Aggregations

Permission (java.security.Permission)236 Test (org.junit.Test)55 PermissionCollection (java.security.PermissionCollection)39 FilePermission (java.io.FilePermission)38 Permissions (java.security.Permissions)31 ProtectionDomain (java.security.ProtectionDomain)27 IOException (java.io.IOException)20 AllPermission (java.security.AllPermission)20 QuickTest (com.hazelcast.test.annotation.QuickTest)17 File (java.io.File)17 URL (java.net.URL)16 AccessControlException (java.security.AccessControlException)14 Principal (java.security.Principal)14 PropertyPermission (java.util.PropertyPermission)14 Policy (java.security.Policy)13 MBeanPermission (javax.management.MBeanPermission)13 AccessControlContext (java.security.AccessControlContext)12 CodeSource (java.security.CodeSource)11 SecurityPermission (java.security.SecurityPermission)11 ArrayList (java.util.ArrayList)10