Skip to content

Commit

Permalink
Remove a test that creates a SecurityManager.
Browse files Browse the repository at this point in the history
This test checked that if `File.exists(someFile)` throws `SecurityException`, `ClassPath` will behave as if `someFile` doesn't exist. This can only happen if there is a `SecurityManager` and it rejects the attempt to check whether `someFile` exists. `SecurityManager` functionality is being removed from Java, so this test won't work, but also the situation it is covering won't happen. While we could do more work to preserve the test on Java versions that still have a `SecurityManager`, it's probably not worth the effort.

RELNOTES=n/a
PiperOrigin-RevId: 703103233
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Dec 5, 2024
1 parent 5d6e2bd commit 6832b54
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -456,50 +453,6 @@ public void testScanAllResources() throws IOException {
.contains("com/google/common/reflect/ClassPathTest.class");
}


public void testExistsThrowsSecurityException() throws IOException, URISyntaxException {
SecurityManager oldSecurityManager = System.getSecurityManager();
try {
doTestExistsThrowsSecurityException();
} finally {
System.setSecurityManager(oldSecurityManager);
}
}

private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
File file = null;
// In Java 9, Logger may read the TZ database. Only disallow reading the class path URLs.
final PermissionCollection readClassPathFiles =
new FilePermission("", "read").newPermissionCollection();
for (URL url : ClassPath.parseJavaClassPath()) {
if (url.getProtocol().equalsIgnoreCase("file")) {
file = new File(url.toURI());
readClassPathFiles.add(new FilePermission(file.getAbsolutePath(), "read"));
}
}
assertThat(file).isNotNull();
SecurityManager disallowFilesSecurityManager =
new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (readClassPathFiles.implies(p)) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
file.exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(getClass().getClassLoader());
// ClassPath may contain resources from the boot class loader; just not from the class path.
for (ResourceInfo resource : classPath.getResources()) {
assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/");
}
}

private static ClassPath.ClassInfo findClass(
Iterable<ClassPath.ClassInfo> classes, Class<?> cls) {
for (ClassPath.ClassInfo classInfo : classes) {
Expand Down
47 changes: 0 additions & 47 deletions guava-tests/test/com/google/common/reflect/ClassPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -522,50 +519,6 @@ public void testScanAllResources() throws IOException {
.contains("com/google/common/reflect/ClassPathTest.class");
}


public void testExistsThrowsSecurityException() throws IOException, URISyntaxException {
SecurityManager oldSecurityManager = System.getSecurityManager();
try {
doTestExistsThrowsSecurityException();
} finally {
System.setSecurityManager(oldSecurityManager);
}
}

private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
File file = null;
// In Java 9, Logger may read the TZ database. Only disallow reading the class path URLs.
final PermissionCollection readClassPathFiles =
new FilePermission("", "read").newPermissionCollection();
for (URL url : ClassPath.parseJavaClassPath()) {
if (url.getProtocol().equalsIgnoreCase("file")) {
file = new File(url.toURI());
readClassPathFiles.add(new FilePermission(file.getAbsolutePath(), "read"));
}
}
assertThat(file).isNotNull();
SecurityManager disallowFilesSecurityManager =
new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (readClassPathFiles.implies(p)) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
file.exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(getClass().getClassLoader());
// ClassPath may contain resources from the boot class loader; just not from the class path.
for (ResourceInfo resource : classPath.getResources()) {
assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/");
}
}

private static ClassPath.ClassInfo findClass(
Iterable<ClassPath.ClassInfo> classes, Class<?> cls) {
for (ClassPath.ClassInfo classInfo : classes) {
Expand Down

0 comments on commit 6832b54

Please sign in to comment.