Skip to content

Commit

Permalink
Skip Security-Manager related tests if Security-Manager is disallowed
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Dec 15, 2024
1 parent 3723380 commit b9383ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.eclipse.osgi.container.Module;
import org.eclipse.osgi.internal.framework.EquinoxBundle;
import org.eclipse.osgi.signedcontent.SignedContent;
import org.eclipse.osgi.tests.securityadmin.SecurityManagerTests;
import org.junit.Assume;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
Expand All @@ -40,6 +42,8 @@ public void testAdapt_Module() throws Exception {
@Test
@SuppressWarnings({ "deprecation", "removal" }) // SecurityManager
public void testAdapt_ProtectionDomain() throws Exception {
Assume.assumeTrue("Security-Manager is disallowed", SecurityManagerTests.isSecurityManagerAllowed());

Bundle bundle = installer.installBundle("test");
SecurityManager previousSM = System.getSecurityManager();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
import org.eclipse.osgi.storage.url.reference.Handler;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.security.BaseSecurityTest;
import org.eclipse.osgi.tests.securityadmin.SecurityManagerTests;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.Bundle;
Expand Down Expand Up @@ -1427,6 +1429,8 @@ public void testBug432632() throws BundleException, IOException {

@Test
public void testDynamicSecurityManager() throws BundleException {
Assume.assumeTrue("Security-Manager is disallowed", SecurityManagerTests.isSecurityManagerAllowed());

SecurityManager sm = System.getSecurityManager();
assertNull("SecurityManager must be null to test.", sm);
try {
Expand Down Expand Up @@ -3267,16 +3271,21 @@ public void testDeleteBundleFile() throws IOException, BundleException {

Map<String, String> launchProps = new HashMap<>();
launchProps.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
SecurityManager sm = new SecurityManager() {
public void checkPermission(Permission perm) {
// do nothing;
}
};
System.setSecurityManager(sm);
boolean unsetSecurityManager = false;
if (SecurityManagerTests.isSecurityManagerAllowed()) {
unsetSecurityManager = true;
System.setSecurityManager(new SecurityManager() {
public void checkPermission(Permission perm) {
// do nothing;
}
});
}
try {
equinox = new Equinox(Collections.singletonMap(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath()));
} finally {
System.setSecurityManager(null);
if (unsetSecurityManager) {
System.setSecurityManager(null);
}
}
equinox.start();
stop(equinox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.eclipse.osgi.launch.Equinox;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.AbstractBundleTests;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
Expand Down Expand Up @@ -108,6 +110,11 @@ public class SecurityAdminUnitTests extends AbstractBundleTests {
private ConditionalPermissionAdmin cpa;
private PermissionAdmin pa;

@BeforeClass
public static void setupClass() {
Assume.assumeTrue("Security-Manager is disallowed", SecurityManagerTests.isSecurityManagerAllowed());
}

@Override
public void setUp() throws Exception {
previousPolicy = Policy.getPolicy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.AbstractBundleTests;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -81,9 +83,13 @@ public class SecurityManagerTests extends AbstractBundleTests {
PackagePermission.class.getName(), "org.osgi.framework", "import"); //$NON-NLS-1$ //$NON-NLS-2$
private Policy previousPolicy;

@BeforeClass
public static void setupClass() {
Assume.assumeTrue("Security-Manager is disallowed", SecurityManagerTests.isSecurityManagerAllowed());
}

@Override
public void setUp() throws Exception {
assertNull("Cannot test with security manager set", getSecurityManager());
previousPolicy = Policy.getPolicy();
final Permission allPermission = new AllPermission();
final PermissionCollection allPermissions = new PermissionCollection() {
Expand Down Expand Up @@ -721,4 +727,21 @@ public Map<String, Object> getAttributes() {
}
}
}

public static boolean isSecurityManagerAllowed() {
SecurityManager original = System.getSecurityManager();
try { // Try to set a dummy to provoke an UnsupportedOperationException if disallowed
System.setSecurityManager(new SecurityManager() {
@Override
public void checkPermission(Permission perm) {
// Permit everything
}
});
System.setSecurityManager(original); // restore original
return true;
} catch (UnsupportedOperationException e) {
return false; // security-manager is not allowed
}
}

}

0 comments on commit b9383ac

Please sign in to comment.