893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
1319N/A * @bug 4313887 6838333
893N/A * @summary Unit test for java.nio.file.attribute.PosixFileAttributeView
893N/A * @library ../..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.LinkOption.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/A/**
893N/A * Unit test for PosixFileAttributeView, passing silently if this attribute
893N/A * view is not available.
893N/A */
893N/A
893N/Apublic class Basic {
893N/A
893N/A /**
893N/A * Use view to update permission to the given mode and check that the
893N/A * permissions have been updated.
893N/A */
1319N/A static void testPermissions(Path file, String mode) throws IOException {
893N/A System.out.format("change mode: %s\n", mode);
893N/A Set<PosixFilePermission> perms = PosixFilePermissions.fromString(mode);
893N/A
893N/A // change permissions and re-read them.
3471N/A Files.setPosixFilePermissions(file, perms);
3471N/A Set<PosixFilePermission> current = Files.getPosixFilePermissions(file);
893N/A if (!current.equals(perms)) {
893N/A throw new RuntimeException("Actual permissions: " +
893N/A PosixFilePermissions.toString(current) + ", expected: " +
893N/A PosixFilePermissions.toString(perms));
893N/A }
893N/A
893N/A // repeat test using setAttribute/getAttribute
3471N/A Files.setAttribute(file, "posix:permissions", perms);
3471N/A current = (Set<PosixFilePermission>)Files.getAttribute(file, "posix:permissions");
893N/A if (!current.equals(perms)) {
893N/A throw new RuntimeException("Actual permissions: " +
893N/A PosixFilePermissions.toString(current) + ", expected: " +
893N/A PosixFilePermissions.toString(perms));
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Check that the actual permissions of a file match or make it more
893N/A * secure than requested
893N/A */
893N/A static void checkSecure(Set<PosixFilePermission> requested,
893N/A Set<PosixFilePermission> actual)
893N/A {
893N/A for (PosixFilePermission perm: actual) {
893N/A if (!requested.contains(perm)) {
893N/A throw new RuntimeException("Actual permissions: " +
893N/A PosixFilePermissions.toString(actual) + ", requested: " +
893N/A PosixFilePermissions.toString(requested) +
893N/A " - file is less secure than requested");
893N/A }
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Create file with given mode and check that the file is created with a
893N/A * mode that is not less secure
893N/A */
893N/A static void createWithPermissions(Path file,
893N/A String mode)
893N/A throws IOException
893N/A {
893N/A Set<PosixFilePermission> requested = PosixFilePermissions.fromString(mode);
893N/A FileAttribute<Set<PosixFilePermission>> attr =
893N/A PosixFilePermissions.asFileAttribute(requested);
893N/A System.out.format("create file with mode: %s\n", mode);
3471N/A Files.createFile(file, attr);
893N/A try {
3471N/A checkSecure(requested,
3471N/A Files.getFileAttributeView(file, PosixFileAttributeView.class)
3471N/A .readAttributes()
3471N/A .permissions());
893N/A } finally {
3471N/A Files.delete(file);
893N/A }
893N/A
893N/A System.out.format("create directory with mode: %s\n", mode);
3471N/A Files.createDirectory(file, attr);
893N/A try {
3471N/A checkSecure(requested,
3471N/A Files.getFileAttributeView(file, PosixFileAttributeView.class)
3471N/A .readAttributes()
3471N/A .permissions());
893N/A } finally {
3471N/A Files.delete(file);
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Test the setPermissions/permissions methods.
893N/A */
893N/A static void permissionTests(Path dir)
893N/A throws IOException
893N/A {
893N/A System.out.println("-- Permission Tests --");
893N/A
893N/A // create file and test updating and reading its permissions
893N/A Path file = dir.resolve("foo");
893N/A System.out.format("create %s\n", file);
3471N/A Files.createFile(file);
893N/A try {
893N/A // get initial permissions so that we can restore them later
3471N/A PosixFileAttributeView view =
3471N/A Files.getFileAttributeView(file, PosixFileAttributeView.class);
893N/A Set<PosixFilePermission> save = view.readAttributes()
893N/A .permissions();
893N/A
893N/A // test various modes
893N/A try {
1319N/A testPermissions(file, "---------");
1319N/A testPermissions(file, "r--------");
1319N/A testPermissions(file, "-w-------");
1319N/A testPermissions(file, "--x------");
1319N/A testPermissions(file, "rwx------");
1319N/A testPermissions(file, "---r-----");
1319N/A testPermissions(file, "----w----");
1319N/A testPermissions(file, "-----x---");
1319N/A testPermissions(file, "---rwx---");
1319N/A testPermissions(file, "------r--");
1319N/A testPermissions(file, "-------w-");
1319N/A testPermissions(file, "--------x");
1319N/A testPermissions(file, "------rwx");
1319N/A testPermissions(file, "r--r-----");
1319N/A testPermissions(file, "r--r--r--");
1319N/A testPermissions(file, "rw-rw----");
1319N/A testPermissions(file, "rwxrwx---");
1319N/A testPermissions(file, "rw-rw-r--");
1319N/A testPermissions(file, "r-xr-x---");
1319N/A testPermissions(file, "r-xr-xr-x");
1319N/A testPermissions(file, "rwxrwxrwx");
893N/A } finally {
893N/A view.setPermissions(save);
893N/A }
893N/A } finally {
3471N/A Files.delete(file);
893N/A }
893N/A
893N/A // create link (to file that doesn't exist) and test reading of
893N/A // permissions
893N/A if (TestUtil.supportsLinks(dir)) {
893N/A Path link = dir.resolve("link");
893N/A System.out.format("create link %s\n", link);
3471N/A Files.createSymbolicLink(link, file);
893N/A try {
3471N/A PosixFileAttributes attrs =
3471N/A Files.getFileAttributeView(link,
3471N/A PosixFileAttributeView.class,
3471N/A NOFOLLOW_LINKS)
3471N/A .readAttributes();
893N/A if (!attrs.isSymbolicLink()) {
893N/A throw new RuntimeException("not a link");
893N/A }
893N/A } finally {
3471N/A Files.delete(link);
893N/A }
893N/A }
893N/A
893N/A System.out.println("OKAY");
893N/A }
893N/A
893N/A /**
893N/A * Test creating a file and directory with initial permissios
893N/A */
893N/A static void createTests(Path dir)
893N/A throws IOException
893N/A {
893N/A System.out.println("-- Create Tests --");
893N/A
893N/A Path file = dir.resolve("foo");
893N/A
893N/A createWithPermissions(file, "---------");
893N/A createWithPermissions(file, "r--------");
893N/A createWithPermissions(file, "-w-------");
893N/A createWithPermissions(file, "--x------");
893N/A createWithPermissions(file, "rwx------");
893N/A createWithPermissions(file, "---r-----");
893N/A createWithPermissions(file, "----w----");
893N/A createWithPermissions(file, "-----x---");
893N/A createWithPermissions(file, "---rwx---");
893N/A createWithPermissions(file, "------r--");
893N/A createWithPermissions(file, "-------w-");
893N/A createWithPermissions(file, "--------x");
893N/A createWithPermissions(file, "------rwx");
893N/A createWithPermissions(file, "r--r-----");
893N/A createWithPermissions(file, "r--r--r--");
893N/A createWithPermissions(file, "rw-rw----");
893N/A createWithPermissions(file, "rwxrwx---");
893N/A createWithPermissions(file, "rw-rw-r--");
893N/A createWithPermissions(file, "r-xr-x---");
893N/A createWithPermissions(file, "r-xr-xr-x");
893N/A createWithPermissions(file, "rwxrwxrwx");
893N/A
893N/A System.out.println("OKAY");
893N/A }
893N/A
893N/A /**
893N/A * Test setOwner/setGroup methods - this test simply exercises the
893N/A * methods to avoid configuration.
893N/A */
893N/A static void ownerTests(Path dir)
893N/A throws IOException
893N/A {
893N/A System.out.println("-- Owner Tests --");
893N/A
893N/A Path file = dir.resolve("gus");
893N/A System.out.format("create %s\n", file);
893N/A
3471N/A Files.createFile(file);
893N/A try {
893N/A
893N/A // read attributes of directory to get owner/group
3471N/A PosixFileAttributeView view =
3471N/A Files.getFileAttributeView(file, PosixFileAttributeView.class);
893N/A PosixFileAttributes attrs = view.readAttributes();
893N/A
893N/A // set to existing owner/group
893N/A view.setOwner(attrs.owner());
893N/A view.setGroup(attrs.group());
893N/A
1319N/A // repeat test using set/getAttribute
3471N/A UserPrincipal owner = (UserPrincipal)Files.getAttribute(file, "posix:owner");
3471N/A Files.setAttribute(file, "posix:owner", owner);
3471N/A UserPrincipal group = (UserPrincipal)Files.getAttribute(file, "posix:group");
3471N/A Files.setAttribute(file, "posix:group", group);
893N/A
893N/A } finally {
3471N/A Files.delete(file);
893N/A }
893N/A
893N/A System.out.println("OKAY");
893N/A }
893N/A
893N/A /**
893N/A * Test the lookupPrincipalByName/lookupPrincipalByGroupName methods
893N/A */
893N/A static void lookupPrincipalTests(Path dir)
893N/A throws IOException
893N/A {
893N/A System.out.println("-- Lookup UserPrincipal Tests --");
893N/A
893N/A UserPrincipalLookupService lookupService = dir.getFileSystem()
893N/A .getUserPrincipalLookupService();
893N/A
893N/A // read attributes of directory to get owner/group
3471N/A PosixFileAttributes attrs = Files.readAttributes(dir, PosixFileAttributes.class);
893N/A
893N/A // lookup owner and check it matches file's owner
893N/A System.out.format("lookup: %s\n", attrs.owner().getName());
893N/A try {
893N/A UserPrincipal owner = lookupService.lookupPrincipalByName(attrs.owner().getName());
893N/A if (owner instanceof GroupPrincipal)
893N/A throw new RuntimeException("owner is a group?");
893N/A if (!owner.equals(attrs.owner()))
893N/A throw new RuntimeException("owner different from file owner");
893N/A } catch (UserPrincipalNotFoundException x) {
893N/A System.out.println("user not found - test skipped");
893N/A }
893N/A
893N/A // lookup group and check it matches file's group-owner
893N/A System.out.format("lookup group: %s\n", attrs.group().getName());
893N/A try {
893N/A GroupPrincipal group = lookupService.lookupPrincipalByGroupName(attrs.group().getName());
893N/A if (!group.equals(attrs.group()))
893N/A throw new RuntimeException("group different from file group-owner");
893N/A } catch (UserPrincipalNotFoundException x) {
893N/A System.out.println("group not found - test skipped");
893N/A }
893N/A
893N/A // test that UserPrincipalNotFoundException is thrown
893N/A String invalidPrincipal = "scumbag99";
893N/A try {
893N/A System.out.format("lookup: %s\n", invalidPrincipal);
893N/A lookupService.lookupPrincipalByName(invalidPrincipal);
893N/A throw new RuntimeException("'" + invalidPrincipal + "' is a valid user?");
893N/A } catch (UserPrincipalNotFoundException x) {
893N/A }
893N/A try {
893N/A System.out.format("lookup group: %s\n", invalidPrincipal);
893N/A lookupService.lookupPrincipalByGroupName("idonotexist");
893N/A throw new RuntimeException("'" + invalidPrincipal + "' is a valid group?");
893N/A } catch (UserPrincipalNotFoundException x) {
893N/A }
893N/A System.out.println("OKAY");
893N/A }
893N/A
893N/A /**
893N/A * Test various exceptions are thrown as expected
893N/A */
893N/A @SuppressWarnings("unchecked")
893N/A static void exceptionsTests(Path dir)
893N/A throws IOException
893N/A {
893N/A System.out.println("-- Exceptions --");
893N/A
3471N/A PosixFileAttributeView view =
3471N/A Files.getFileAttributeView(dir,PosixFileAttributeView.class);
893N/A
893N/A // NullPointerException
893N/A try {
893N/A view.setOwner(null);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A try {
893N/A view.setGroup(null);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A
893N/A UserPrincipalLookupService lookupService = dir.getFileSystem()
893N/A .getUserPrincipalLookupService();
893N/A try {
893N/A lookupService.lookupPrincipalByName(null);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A try {
893N/A lookupService.lookupPrincipalByGroupName(null);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A try {
893N/A view.setPermissions(null);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A try {
3471N/A Set<PosixFilePermission> perms = new HashSet<>();
893N/A perms.add(null);
893N/A view.setPermissions(perms);
893N/A throw new RuntimeException("NullPointerException not thrown");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A
893N/A // ClassCastException
893N/A try {
893N/A Set perms = new HashSet(); // raw type
893N/A perms.add(new Object());
893N/A view.setPermissions(perms);
893N/A throw new RuntimeException("ClassCastException not thrown");
893N/A } catch (ClassCastException x) {
893N/A }
893N/A
893N/A System.out.println("OKAY");
893N/A }
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
3471N/A if (!Files.getFileStore(dir).supportsFileAttributeView("posix")) {
893N/A System.out.println("PosixFileAttributeView not supported");
893N/A return;
893N/A }
893N/A
893N/A permissionTests(dir);
893N/A createTests(dir);
893N/A ownerTests(dir);
893N/A lookupPrincipalTests(dir);
893N/A exceptionsTests(dir);
893N/A
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}