Search in sources :

Example 1 with ResourceInfo

use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.

the class ClassPathTest method doTestExistsThrowsSecurityException.

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/");
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) ResourceInfo(com.google.common.reflect.ClassPath.ResourceInfo) FilePermission(java.io.FilePermission) Permission(java.security.Permission) Files.createFile(java.nio.file.Files.createFile) File(java.io.File) FilePermission(java.io.FilePermission) URL(java.net.URL)

Example 2 with ResourceInfo

use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.

the class ClassPathTest method testScanDirectory_symlinkToRootCycle.

// Path (for symlink creation)
@AndroidIncompatible
public void testScanDirectory_symlinkToRootCycle() throws IOException {
    ClassLoader loader = ClassPathTest.class.getClassLoader();
    // directory with a cycle,
    // /root
    // /child
    // /[grandchild -> root]
    java.nio.file.Path root = createTempDirectory("ClassPathTest");
    try {
        createFile(root.resolve("some.txt"));
        java.nio.file.Path child = createDirectory(root.resolve("child"));
        createSymbolicLink(child.resolve("grandchild"), root);
        assertEquals(ImmutableSet.of(new ResourceInfo(FILE, "some.txt", loader)), new ClassPath.LocationInfo(root.toFile(), loader).scanResources());
    } finally {
        deleteRecursivelyOrLog(root);
    }
}
Also used : ResourceInfo(com.google.common.reflect.ClassPath.ResourceInfo) URLClassLoader(java.net.URLClassLoader)

Example 3 with ResourceInfo

use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.

the class ClassPathTest method testScanDirectory_symlinkCycle.

// Path (for symlink creation)
@AndroidIncompatible
public void testScanDirectory_symlinkCycle() throws IOException {
    ClassLoader loader = ClassPathTest.class.getClassLoader();
    // directory with a cycle,
    // /root
    // /left
    // /[sibling -> right]
    // /right
    // /[sibling -> left]
    java.nio.file.Path root = createTempDirectory("ClassPathTest");
    try {
        java.nio.file.Path left = createDirectory(root.resolve("left"));
        createFile(left.resolve("some.txt"));
        java.nio.file.Path right = createDirectory(root.resolve("right"));
        createFile(right.resolve("another.txt"));
        createSymbolicLink(left.resolve("sibling"), right);
        createSymbolicLink(right.resolve("sibling"), left);
        assertEquals(ImmutableSet.of(new ResourceInfo(FILE, "left/some.txt", loader), new ResourceInfo(FILE, "left/sibling/another.txt", loader), new ResourceInfo(FILE, "right/another.txt", loader), new ResourceInfo(FILE, "right/sibling/some.txt", loader)), new ClassPath.LocationInfo(root.toFile(), loader).scanResources());
    } finally {
        deleteRecursivelyOrLog(root);
    }
}
Also used : ResourceInfo(com.google.common.reflect.ClassPath.ResourceInfo) URLClassLoader(java.net.URLClassLoader)

Aggregations

ResourceInfo (com.google.common.reflect.ClassPath.ResourceInfo)3 URLClassLoader (java.net.URLClassLoader)2 File (java.io.File)1 FilePermission (java.io.FilePermission)1 URL (java.net.URL)1 Files.createFile (java.nio.file.Files.createFile)1 Permission (java.security.Permission)1 PermissionCollection (java.security.PermissionCollection)1