Search in sources :

Example 66 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardingFindHookTest method testNullFilter.

@Test
public void testNullFilter() throws Exception {
    BundleContext hookBC = mockBundleContext(5L);
    GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
    GuardingFindHook gfh = new GuardingFindHook(hookBC, gpc, null);
    // should just do nothing
    gfh.find(null, null, null, true, null);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 67 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testAssignRoles3.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testAssignRoles3() throws Exception {
    abstract class MyAbstractClass implements TestServiceAPI, TestServiceAPI2 {
    }
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(Constants.SERVICE_PID, "foobar");
    config.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
    config.put("doit", "X");
    BundleContext bc = mockConfigAdminBundleContext(config);
    Map<ServiceReference, Object> serviceMap = new HashMap<>();
    testCreateProxy(bc, new Class[] { TestServiceAPI.class, TestServiceAPI2.class }, new MyAbstractClass() {

        @Override
        public String doit() {
            return null;
        }

        @Override
        public String doit(String s) {
            return null;
        }
    }, serviceMap);
    assertEquals(1, serviceMap.size());
    assertEquals(Collections.singleton("X"), serviceMap.keySet().iterator().next().getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 68 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testAssignRoles4.

@SuppressWarnings("unchecked")
@Test
public void testAssignRoles4() throws Exception {
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(Constants.SERVICE_PID, "foobar");
    config.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
    config.put("somemethod", "b");
    config.put("someOtherMethod", "b");
    config.put("somethingelse", "*");
    BundleContext bc = mockConfigAdminBundleContext(config);
    Dictionary<String, Object> proxyProps = testCreateProxy(bc, TestServiceAPI.class, new TestService());
    Collection<String> result = (Collection<String>) proxyProps.get(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY);
    assertEquals(1, result.size());
    assertEquals("b", result.iterator().next());
}
Also used : Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 69 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testGuardProxyCatalog.

@Test
public void testGuardProxyCatalog() throws Exception {
    Bundle b = EasyMock.createMock(Bundle.class);
    EasyMock.expect(b.getBundleId()).andReturn(9823L).anyTimes();
    EasyMock.replay(b);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
    bc.addServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    String caFilter = "(&(objectClass=org.osgi.service.cm.ConfigurationAdmin)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(caFilter)).andReturn(FrameworkUtil.createFilter(caFilter)).anyTimes();
    String pmFilter = "(&(objectClass=org.apache.aries.proxy.ProxyManager)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(pmFilter)).andReturn(FrameworkUtil.createFilter(pmFilter)).anyTimes();
    EasyMock.replay(bc);
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    assertTrue("Service Tracker for ConfigAdmin should be opened", gpc.configAdminTracker.getTrackingCount() != -1);
    assertTrue("Service Tracker for ProxyManager should be opened", gpc.proxyManagerTracker.getTrackingCount() != -1);
    EasyMock.verify(bc);
    // Add some more behaviour checks to the bundle context
    EasyMock.reset(bc);
    bc.removeServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    EasyMock.replay(bc);
    gpc.close();
    assertEquals("Service Tracker for ConfigAdmin should be closed", -1, gpc.configAdminTracker.getTrackingCount());
    assertEquals("Service Tracker for ProxyManager should be closed", -1, gpc.proxyManagerTracker.getTrackingCount());
    EasyMock.verify(bc);
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 70 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testCustomRole.

@SuppressWarnings("unchecked")
@Test
public void testCustomRole() throws Exception {
    class MyRolePrincipal implements Principal {

        @Override
        public String getName() {
            return "role1";
        }
    }
    Dictionary<String, Object> c1 = new Hashtable<>();
    c1.put(Constants.SERVICE_PID, "foobar");
    c1.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
    c1.put("doit", MyRolePrincipal.class.getName() + ":role1");
    BundleContext bc = mockConfigAdminBundleContext(c1);
    final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI.class }, new TestService());
    Subject s1 = new Subject();
    s1.getPrincipals().add(new RolePrincipal("role1"));
    Subject.doAs(s1, (PrivilegedAction<Object>) () -> {
        try {
            ((TestServiceAPI) proxy).doit();
            fail("Should have prevented this invocation as the custom role is required");
        } catch (SecurityException se) {
        }
        return null;
    });
    Subject s2 = new Subject();
    s2.getPrincipals().add(new MyRolePrincipal());
    Subject.doAs(s2, (PrivilegedAction<Object>) () -> {
        ((TestServiceAPI) proxy).doit();
        return null;
    });
    Subject s3 = new Subject();
    s3.getPrincipals().add(new MyRolePrincipal());
    s3.getPrincipals().add(new RolePrincipal("role1"));
    Subject.doAs(s3, (PrivilegedAction<Object>) () -> {
        ((TestServiceAPI) proxy).doit();
        return null;
    });
}
Also used : Hashtable(java.util.Hashtable) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Principal(java.security.Principal) Subject(javax.security.auth.Subject) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)524 Test (org.junit.Test)186 Bundle (org.osgi.framework.Bundle)175 ServiceReference (org.osgi.framework.ServiceReference)126 File (java.io.File)82 Hashtable (java.util.Hashtable)75 HashMap (java.util.HashMap)70 Equinox (org.eclipse.osgi.launch.Equinox)51 BundleException (org.osgi.framework.BundleException)51 ArrayList (java.util.ArrayList)50 LinkedHashMap (java.util.LinkedHashMap)45 ServiceRegistration (org.osgi.framework.ServiceRegistration)41 IOException (java.io.IOException)40 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)38 URL (java.net.URL)33 Dictionary (java.util.Dictionary)32 Matchers.anyString (org.mockito.Matchers.anyString)28 Before (org.junit.Before)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)26 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18