Merge pull request #1 from apache/SLING-8815

SLING-8815 - [API Regions] Prevent from resolving to customer-provided bundles
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/AbstractResolverHookFactory.java b/src/main/java/org/apache/sling/feature/apiregions/impl/AbstractResolverHookFactory.java
new file mode 100644
index 0000000..9042090
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/AbstractResolverHookFactory.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.apiregions.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+import org.osgi.framework.hooks.resolver.ResolverHookFactory;
+
+abstract class AbstractResolverHookFactory<K, V> implements ResolverHookFactory {
+
+    static final String CLASSLOADER_PSEUDO_PROTOCOL = "classloader://";
+
+    static final String PROPERTIES_RESOURCE_PREFIX = "sling.feature.apiregions.resource.";
+
+    static final String PROPERTIES_FILE_LOCATION = "sling.feature.apiregions.location";
+
+    static final String IDBSNVER_FILENAME = "idbsnver.properties";
+
+    protected final URI getDataFileURI(BundleContext ctx, String name) throws IOException, URISyntaxException {
+        String fn = ctx.getProperty(PROPERTIES_RESOURCE_PREFIX + name);
+        if (fn == null) {
+            String loc = ctx.getProperty(PROPERTIES_FILE_LOCATION);
+            if (loc != null) {
+                fn = loc + "/" + name;
+            }
+        }
+
+        if (fn == null) {
+            throw new IOException("API Region Enforcement enabled, but no configuration found to find "
+                    + "region definition resource: " + name);
+        }
+
+        if (fn.contains(":")) {
+            if (fn.startsWith(CLASSLOADER_PSEUDO_PROTOCOL)) {
+                // It's using the 'classloader:' protocol looks up the location from the classloader
+                String loc = fn.substring(CLASSLOADER_PSEUDO_PROTOCOL.length());
+                if (!loc.startsWith("/")) {
+                    loc = "/" + loc;
+                }
+                fn = getClass().getResource(loc).toString();
+            }
+            // It's already a URL
+            return new URI(fn);
+        }
+
+        // It's a file location
+        return new File(fn).toURI();
+    }
+
+    protected final Map<K, V> readBsnVerMap(BundleContext context) throws IOException, URISyntaxException {
+        final Map<K, V> bsnVerMap = new HashMap<>();
+
+        URI idbsnverFile = getDataFileURI(context, IDBSNVER_FILENAME);
+
+        Properties properties = new Properties();
+        try (InputStream is = idbsnverFile.toURL().openStream()) {
+            properties.load(is);
+        }
+
+        for (String artifactId : properties.stringPropertyNames()) {
+            String value = properties.getProperty(artifactId);
+            if (value != null && !value.isEmpty()) { // it shouldn't happen, but...
+                int splitIndex = value.indexOf('~');
+                if (splitIndex != -1) { // again, it shouldn't happen...
+                    String bundleSymbolicName = value.substring(0, splitIndex);
+                    String bundleVersion = value.substring(splitIndex + 1);
+                    Version version = Version.valueOf(bundleVersion);
+
+                    addBsnVerArtifact(bsnVerMap, artifactId, bundleSymbolicName, version);
+                }
+            }
+        }
+
+        return bsnVerMap;
+    }
+
+    protected abstract void addBsnVerArtifact(Map<K, V> bsnVerMap,
+                                              String artifactId,
+                                              String bundleSymbolicName,
+                                              Version bundleVersion);
+
+}
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java b/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
index 190e29b..39b5a0e 100644
--- a/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
@@ -49,12 +49,16 @@
     BundleContext bundleContext;
     ServiceRegistration<ResolverHookFactory> hookRegistration;
 
+    ServiceRegistration<ResolverHookFactory> platformIsolationRegistration;
+
     @Override
     public synchronized void start(BundleContext context) throws Exception {
         bundleContext = context;
 
         registerHook();
 
+        registerPlatformIsolationEnforcer();
+
         context.addFrameworkListener(this);
     }
 
@@ -80,11 +84,29 @@
         }
     }
 
-    synchronized void unregisterHook() {
-        if (hookRegistration != null) {
-            hookRegistration.unregister();
-            hookRegistration = null;
+    synchronized void registerPlatformIsolationEnforcer() {
+        if (platformIsolationRegistration != null) {
+            return;
         }
+
+        try {
+            PlatformIsolationEnforcer enforcer = new PlatformIsolationEnforcer(bundleContext);
+            platformIsolationRegistration = bundleContext.registerService(ResolverHookFactory.class, enforcer, new Hashtable<>());
+        } catch (Exception e) {
+            RegionEnforcer.LOG.log(Level.SEVERE, "Problem activating Platform Isolation runtime enforcement component", e);
+        }
+    }
+
+    synchronized void unregisterHooks() {
+        hookRegistration = unregisterService(hookRegistration);
+        platformIsolationRegistration = unregisterService(platformIsolationRegistration);
+    }
+
+    private <S> ServiceRegistration<S> unregisterService(ServiceRegistration<S> serviceRegistration) {
+        if (serviceRegistration != null) {
+            serviceRegistration.unregister();
+        }
+        return null;
     }
 
     @Override
@@ -133,9 +155,10 @@
                                         Dictionary<?,?> props = (Dictionary<?,?>) args[0];
                                         Object disabled = props.get("disable");
                                         if ("true".equals(disabled)) {
-                                            unregisterHook();
+                                            unregisterHooks();
                                         } else {
                                             registerHook();
+                                            registerPlatformIsolationEnforcer();
                                         }
                                     }
                                 }
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcer.java b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcer.java
new file mode 100644
index 0000000..200d1e3
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcer.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.apiregions.impl;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+import org.osgi.framework.hooks.resolver.ResolverHook;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class PlatformIsolationEnforcer extends AbstractResolverHookFactory<String, Version> {
+
+    final Map<String, Version> bsnVerMap;
+
+    PlatformIsolationEnforcer(BundleContext context) throws IOException, URISyntaxException {
+        Map<String, Version> bsnVerMap = readBsnVerMap(context);
+        this.bsnVerMap = Collections.unmodifiableMap(bsnVerMap);
+    }
+
+    @Override
+    protected void addBsnVerArtifact(Map<String, Version> bsnVerMap,
+                                     String artifactId,
+                                     String bundleSymbolicName,
+                                     Version bundleVersion) {
+        bsnVerMap.put(bundleSymbolicName, bundleVersion);
+    }
+
+    @Override
+    public ResolverHook begin(Collection<BundleRevision> triggers) {
+        return new PlatformIsolationHook(bsnVerMap);
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java
new file mode 100644
index 0000000..ee1b635
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.apiregions.impl;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.osgi.framework.Version;
+import org.osgi.framework.hooks.resolver.ResolverHook;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRequirement;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class PlatformIsolationHook implements ResolverHook {
+
+    protected static final String OSGI_WIRING_PACKAGE_NAMESPACE = "osgi.wiring.package";
+
+    private final Map<String, Version> bsnVerMap;
+
+    PlatformIsolationHook(Map<String, Version> bsnVerMap) {
+        this.bsnVerMap = bsnVerMap;
+    }
+
+    @Override
+    public void filterResolvable(Collection<BundleRevision> candidates) {
+        // not used in this version
+    }
+
+    @Override
+    public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
+        // not used in this version
+    }
+
+    @Override
+    public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
+        for (Iterator<BundleCapability> it = candidates.iterator(); it.hasNext();) {
+            BundleCapability candidate = it.next();
+
+            BundleRevision rev = candidate.getRevision();
+
+            // bundle is allowed to wire to itself
+            if (requirement.getRevision().getBundle().getBundleId() == rev.getBundle().getBundleId()) {
+                continue;
+            }
+
+            // is it a restricted bundle?
+            if (filter(requirement, candidate)) {
+                it.remove();
+                // LOG.info("Prevented {} from resolving to {}", requirement, candidate);
+            }
+        }
+    }
+
+    boolean filter(BundleRequirement requirement, BundleCapability candidate) {
+        String requirementNamespace = requirement.getNamespace();
+        String candidateNamespace = candidate.getNamespace();
+        if (!OSGI_WIRING_PACKAGE_NAMESPACE.equals(requirementNamespace)
+                || !requirementNamespace.equals(candidateNamespace)) {
+            return false; // checking wiring packages only
+        }
+
+        BundleRevision candidateRevision = candidate.getRevision();
+        String candidateSymbolicName = candidateRevision.getSymbolicName();
+        Version candidateVersion = candidateRevision.getVersion();
+
+        Version expectedVersion = bsnVerMap.get(candidateSymbolicName);
+
+        return expectedVersion != null && !expectedVersion.equals(candidateVersion);
+    }
+
+    @Override
+    public void end() {
+        // not used in this version
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java b/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java
index a2dd9f8..f2aa3d9 100644
--- a/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/RegionEnforcer.java
@@ -18,13 +18,6 @@
  */
 package org.apache.sling.feature.apiregions.impl;
 
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Version;
-import org.osgi.framework.hooks.resolver.ResolverHook;
-import org.osgi.framework.hooks.resolver.ResolverHookFactory;
-import org.osgi.framework.wiring.BundleRevision;
-
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
@@ -45,15 +38,17 @@
 import java.util.Set;
 import java.util.logging.Logger;
 
-class RegionEnforcer implements ResolverHookFactory {
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+import org.osgi.framework.hooks.resolver.ResolverHook;
+import org.osgi.framework.wiring.BundleRevision;
+
+class RegionEnforcer extends AbstractResolverHookFactory<Map.Entry<String, Version>, List<String>> {
     public static final String GLOBAL_REGION = "global";
 
-    static final String CLASSLOADER_PSEUDO_PROTOCOL = "classloader://";
     static final String APIREGIONS_JOINGLOBAL = "sling.feature.apiregions.joinglobal";
-    static final String PROPERTIES_RESOURCE_PREFIX = "sling.feature.apiregions.resource.";
-    static final String PROPERTIES_FILE_LOCATION = "sling.feature.apiregions.location";
 
-    static final String IDBSNVER_FILENAME = "idbsnver.properties";
+    
     static final String BUNDLE_FEATURE_FILENAME = "bundles.properties";
     static final String FEATURE_REGION_FILENAME = "features.properties";
     static final String REGION_PACKAGE_FILENAME = "regions.properties";
@@ -71,7 +66,7 @@
         URI idbsnverFile = getDataFileURI(context, IDBSNVER_FILENAME);
         // Register the location as a service property for diagnostic purposes
         regProps.put(IDBSNVER_FILENAME, idbsnverFile.toString());
-        Map<Entry<String, Version>, List<String>> bvm = populateBSNVerMap(idbsnverFile);
+        Map<Entry<String, Version>, List<String>> bvm = readBsnVerMap(context);
 
         URI bundlesFile = getDataFileURI(context, BUNDLE_FEATURE_FILENAME);
         // Register the location as a service property for diagnostic purposes
@@ -128,28 +123,12 @@
         }
     }
 
-    private static Map<Map.Entry<String, Version>, List<String>> populateBSNVerMap(URI idbsnverFile) throws IOException {
-        Map<Map.Entry<String, Version>, List<String>> m = new HashMap<>();
-
-        Properties p = new Properties();
-        try (InputStream is = idbsnverFile.toURL().openStream()) {
-            p.load(is);
-        }
-
-        for (String n : p.stringPropertyNames()) {
-            String[] bsnver = p.getProperty(n).split("~");
-            addBsnVerArtifact(m, bsnver[0], bsnver[1], n);
-        }
-
-        return m;
-    }
-
-    private static void addBsnVerArtifact(
-            Map<Map.Entry<String, Version>, List<String>> bsnVerMap,
-            String bundleSymbolicName, String bundleVersion,
-            String artifactId) {
-        Version version = Version.valueOf(bundleVersion);
-        Map.Entry<String, Version> bsnVer = new AbstractMap.SimpleEntry<>(bundleSymbolicName, version);
+    @Override
+    protected void addBsnVerArtifact(Map<Entry<String, Version>, List<String>> bsnVerMap,
+                                     String artifactId,
+                                     String bundleSymbolicName,
+                                     Version bundleVersion) {
+        Map.Entry<String, Version> bsnVer = new AbstractMap.SimpleEntry<>(bundleSymbolicName, bundleVersion);
         List<String> l = bsnVerMap.get(bsnVer);
         if (l == null) {
             l = new ArrayList<>();
@@ -199,35 +178,6 @@
         bf.addAll(values);
     }
 
-    private URI getDataFileURI(BundleContext ctx, String name) throws IOException, URISyntaxException {
-        String fn = ctx.getProperty(PROPERTIES_RESOURCE_PREFIX + name);
-        if (fn == null) {
-            String loc = ctx.getProperty(PROPERTIES_FILE_LOCATION);
-            if (loc != null) {
-                fn = loc + "/" + name;
-            }
-        }
-
-        if (fn == null)
-            throw new IOException("API Region Enforcement enabled, but no configuration found to find "
-                    + "region definition resource: " + name);
-
-        if (fn.contains(":")) {
-            if (fn.startsWith(CLASSLOADER_PSEUDO_PROTOCOL)) {
-                // It's using the 'classloader:' protocol looks up the location from the classloader
-                String loc = fn.substring(CLASSLOADER_PSEUDO_PROTOCOL.length());
-                if (!loc.startsWith("/"))
-                    loc = "/" + loc;
-                fn = getClass().getResource(loc).toString();
-            }
-            // It's already a URL
-            return new URI(fn);
-        } else {
-            // It's a file location
-            return new File(fn).toURI();
-        }
-    }
-
     @Override
     public ResolverHook begin(Collection<BundleRevision> triggers) {
         if (enabledRegions.isEmpty())
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
index 7deb0ea..faf1098 100644
--- a/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
@@ -133,7 +133,7 @@
     @Test
     public void testUnregisterHook() {
         Activator a = new Activator();
-        a.unregisterHook(); // Should not throw an exception
+        a.unregisterHooks(); // Should not throw an exception
         assertNull(a.hookRegistration);
     }
 
@@ -145,7 +145,7 @@
         Activator a = new Activator();
         a.hookRegistration = reg;
 
-        a.unregisterHook();
+        a.unregisterHooks();
         Mockito.verify(reg).unregister();
         assertNull(a.hookRegistration);
     }
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java
new file mode 100644
index 0000000..13d1339
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.apiregions.impl;
+
+import static org.junit.Assert.*;
+import static org.apache.sling.feature.apiregions.impl.AbstractResolverHookFactory.IDBSNVER_FILENAME;
+import static org.apache.sling.feature.apiregions.impl.AbstractResolverHookFactory.PROPERTIES_RESOURCE_PREFIX;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+
+public class PlatformIsolationEnforcerTest {
+
+    @Test
+    public void testLoadBSNVerMap() throws Exception {
+        String propertiesFile = getClass().getResource("/idbsnver1.properties").getFile();
+
+        BundleContext context = mock(BundleContext.class);
+        when(context.getProperty(PROPERTIES_RESOURCE_PREFIX + IDBSNVER_FILENAME)).thenReturn(propertiesFile);
+
+        PlatformIsolationEnforcer enforcer = new PlatformIsolationEnforcer(context);
+
+        assertFalse(enforcer.bsnVerMap.isEmpty());
+        assertEquals(2, enforcer.bsnVerMap.size());
+
+        Map<String, Version> expected = new HashMap<>();
+        expected.put("b2", new Version(1, 2, 3));
+        expected.put("b1", new Version(1, 0, 0));
+
+        assertEquals(expected, enforcer.bsnVerMap);
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java
new file mode 100644
index 0000000..f495ede
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.apiregions.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.osgi.framework.Version;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRequirement;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class PlatformIsolationHookTest {
+
+    @Test
+    public void doNotFilterRequirementWithDifferentNamespace() {
+        BundleRequirement requirement = mock(BundleRequirement.class);
+        when(requirement.getNamespace()).thenReturn("something.irrilevant");
+
+        // won't be touched
+        BundleCapability candidate = newBundleCapability();
+
+        PlatformIsolationHook hook = new PlatformIsolationHook(new HashMap<String, Version>());
+        assertFalse(hook.filter(requirement, candidate));
+    }
+
+    @Test
+    public void doNotFilterCandidateWithDifferentNamespace() {
+        BundleRequirement requirement = newRequirement();
+
+        BundleCapability candidate = mock(BundleCapability.class);
+        when(candidate.getNamespace()).thenReturn("something.irrilevant");
+
+        PlatformIsolationHook hook = new PlatformIsolationHook(new HashMap<String, Version>());
+        assertFalse(hook.filter(requirement, candidate));
+    }
+
+    @Test
+    public void letUnownBundleBeResolved() {
+        Map<String, Version> bsnVerMap = new HashMap<>();
+        bsnVerMap.put("b2", new Version(1, 2, 3));
+        bsnVerMap.put("b1", new Version(1, 0, 0));
+        PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+        BundleRequirement requirement = newRequirement();
+
+        BundleCapability candidate = newBundleCapability();
+        BundleRevision revision = newBundleRevision("asd", "1.0.0.CUSTOM");
+        when(candidate.getRevision()).thenReturn(revision);
+
+        assertFalse(hook.filter(requirement, candidate));
+    }
+
+    @Test
+    public void letKnownBundleBeResolved() {
+        Map<String, Version> bsnVerMap = new HashMap<>();
+        bsnVerMap.put("b2", new Version(1, 2, 3));
+        bsnVerMap.put("b1", new Version(1, 0, 0));
+        PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+        BundleRequirement requirement = newRequirement();
+
+        BundleCapability candidate = newBundleCapability();
+        BundleRevision revision = newBundleRevision("b1", "1.0.0");
+        when(candidate.getRevision()).thenReturn(revision);
+
+        assertFalse(hook.filter(requirement, candidate));
+    }
+
+    @Test
+    public void upatedBundleFilteredOut() {
+        Map<String, Version> bsnVerMap = new HashMap<>();
+        bsnVerMap.put("b2", new Version(1, 2, 3));
+        bsnVerMap.put("b1", new Version(1, 0, 0));
+        PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+        BundleRequirement requirement = newRequirement();
+
+        BundleCapability candidate = newBundleCapability();
+        BundleRevision revision = newBundleRevision("b1", "1.0.0.CUSTOM");
+        when(candidate.getRevision()).thenReturn(revision);
+
+        assertTrue(hook.filter(requirement, candidate));
+    }
+
+    private static BundleRequirement newRequirement() {
+        BundleRequirement requirement = mock(BundleRequirement.class);
+        when(requirement.getNamespace()).thenReturn(PlatformIsolationHook.OSGI_WIRING_PACKAGE_NAMESPACE);
+        return requirement;
+    }
+
+    private static BundleCapability newBundleCapability() {
+        BundleCapability candidate = mock(BundleCapability.class);
+        when(candidate.getNamespace()).thenReturn(PlatformIsolationHook.OSGI_WIRING_PACKAGE_NAMESPACE);
+        return candidate;
+    }
+
+    private static BundleRevision newBundleRevision(String symbolicName, String version) {
+        BundleRevision revision = mock(BundleRevision.class);
+        when(revision.getSymbolicName()).thenReturn(symbolicName);
+        when(revision.getVersion()).thenReturn(Version.valueOf(version));
+        return revision;
+    }
+
+}