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
1757N/A * @bug 4313887 6838333 6891404
893N/A * @summary Unit test for java.nio.file.attribute.AclFileAttribueView
893N/A * @library ../..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/Aimport static java.nio.file.attribute.AclEntryType.*;
893N/Aimport static java.nio.file.attribute.AclEntryPermission.*;
893N/Aimport static java.nio.file.attribute.AclEntryFlag.*;
893N/A
893N/Apublic class Basic {
893N/A
893N/A static void printAcl(List<AclEntry> acl) {
893N/A for (AclEntry entry: acl) {
893N/A System.out.format(" %s%n", entry);
893N/A }
893N/A }
893N/A
893N/A // sanity check read and writing ACL
893N/A static void testReadWrite(Path dir) throws IOException {
893N/A Path file = dir.resolve("foo");
3471N/A if (Files.notExists(file))
3471N/A Files.createFile(file);
893N/A
3471N/A AclFileAttributeView view =
3471N/A Files.getFileAttributeView(file, AclFileAttributeView.class);
893N/A
893N/A // print existing ACL
893N/A List<AclEntry> acl = view.getAcl();
893N/A System.out.println(" -- current ACL --");
893N/A printAcl(acl);
893N/A
893N/A // insert entry to grant owner read access
893N/A UserPrincipal owner = view.getOwner();
893N/A AclEntry entry = AclEntry.newBuilder()
893N/A .setType(ALLOW)
893N/A .setPrincipal(owner)
893N/A .setPermissions(READ_DATA, READ_ATTRIBUTES)
893N/A .build();
893N/A System.out.println(" -- insert (entry 0) --");
893N/A System.out.format(" %s%n", entry);
893N/A acl.add(0, entry);
893N/A view.setAcl(acl);
893N/A
893N/A // re-ACL and check entry
893N/A List<AclEntry> newacl = view.getAcl();
893N/A System.out.println(" -- current ACL --");
893N/A printAcl(acl);
893N/A if (!newacl.get(0).equals(entry)) {
893N/A throw new RuntimeException("Entry 0 is not expected");
893N/A }
893N/A
893N/A // if PosixFileAttributeView then repeat test with OWNER@
3471N/A if (Files.getFileStore(file).supportsFileAttributeView("posix")) {
893N/A owner = file.getFileSystem().getUserPrincipalLookupService()
893N/A .lookupPrincipalByName("OWNER@");
893N/A entry = AclEntry.newBuilder(entry).setPrincipal(owner).build();
893N/A
893N/A System.out.println(" -- replace (entry 0) --");
893N/A System.out.format(" %s%n", entry);
893N/A
893N/A acl.set(0, entry);
893N/A view.setAcl(acl);
893N/A newacl = view.getAcl();
893N/A System.out.println(" -- current ACL --");
893N/A printAcl(acl);
893N/A if (!newacl.get(0).equals(entry)) {
893N/A throw new RuntimeException("Entry 0 is not expected");
893N/A }
893N/A }
893N/A }
893N/A
893N/A static FileAttribute<List<AclEntry>> asAclAttribute(final List<AclEntry> acl) {
893N/A return new FileAttribute<List<AclEntry>>() {
893N/A public String name() { return "acl:acl"; }
893N/A public List<AclEntry> value() { return acl; }
893N/A };
893N/A }
893N/A
893N/A static void assertEquals(List<AclEntry> actual, List<AclEntry> expected) {
893N/A if (!actual.equals(expected)) {
893N/A System.err.format("Actual: %s\n", actual);
893N/A System.err.format("Expected: %s\n", expected);
893N/A throw new RuntimeException("ACL not expected");
893N/A }
893N/A }
893N/A
893N/A // sanity check create a file or directory with initial ACL
893N/A static void testCreateFile(Path dir) throws IOException {
3471N/A UserPrincipal user = Files.getOwner(dir);
3471N/A AclFileAttributeView view;
893N/A
893N/A // create file with initial ACL
893N/A System.out.println("-- create file with initial ACL --");
893N/A Path file = dir.resolve("gus");
893N/A List<AclEntry> fileAcl = Arrays.asList(
893N/A AclEntry.newBuilder()
893N/A .setType(AclEntryType.ALLOW)
893N/A .setPrincipal(user)
893N/A .setPermissions(SYNCHRONIZE, READ_DATA, WRITE_DATA,
893N/A READ_ATTRIBUTES, READ_ACL, WRITE_ATTRIBUTES, DELETE)
893N/A .build());
3471N/A Files.createFile(file, asAclAttribute(fileAcl));
3471N/A view = Files.getFileAttributeView(file, AclFileAttributeView.class);
3471N/A assertEquals(view.getAcl(), fileAcl);
893N/A
893N/A // create directory with initial ACL
893N/A System.out.println("-- create directory with initial ACL --");
893N/A Path subdir = dir.resolve("stuff");
893N/A List<AclEntry> dirAcl = Arrays.asList(
893N/A AclEntry.newBuilder()
893N/A .setType(AclEntryType.ALLOW)
893N/A .setPrincipal(user)
893N/A .setPermissions(SYNCHRONIZE, ADD_FILE, DELETE)
893N/A .build(),
893N/A AclEntry.newBuilder(fileAcl.get(0))
893N/A .setFlags(FILE_INHERIT)
893N/A .build());
3471N/A Files.createDirectory(subdir, asAclAttribute(dirAcl));
3471N/A view = Files.getFileAttributeView(subdir, AclFileAttributeView.class);
3471N/A assertEquals(view.getAcl(), dirAcl);
893N/A }
893N/A
893N/A public static void main(String[] args) throws IOException {
1319N/A // use work directory rather than system temporary directory to
1319N/A // improve chances that ACLs are supported
3471N/A Path dir = Paths.get("./work" + new Random().nextInt());
3471N/A Files.createDirectory(dir);
893N/A try {
3471N/A if (!Files.getFileStore(dir).supportsFileAttributeView("acl")) {
893N/A System.out.println("ACLs not supported - test skipped!");
893N/A return;
893N/A }
893N/A testReadWrite(dir);
893N/A
893N/A // only currently feasible on Windows
893N/A if (System.getProperty("os.name").startsWith("Windows"))
893N/A testCreateFile(dir);
893N/A
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}