Path: blob/master/test/jdk/java/nio/file/attribute/AclFileAttributeView/Basic.java
41159 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 6838333 689140425* @summary Unit test for java.nio.file.attribute.AclFileAttribueView26* @library ../..27* @key randomness28*/2930import java.nio.file.*;31import java.nio.file.attribute.*;32import java.io.IOException;33import java.util.*;3435import static java.nio.file.attribute.AclEntryType.*;36import static java.nio.file.attribute.AclEntryPermission.*;37import static java.nio.file.attribute.AclEntryFlag.*;3839public class Basic {4041static void printAcl(List<AclEntry> acl) {42for (AclEntry entry: acl) {43System.out.format(" %s%n", entry);44}45}4647// sanity check read and writing ACL48static void testReadWrite(Path dir) throws IOException {49Path file = dir.resolve("foo");50if (Files.notExists(file))51Files.createFile(file);5253AclFileAttributeView view =54Files.getFileAttributeView(file, AclFileAttributeView.class);5556// print existing ACL57List<AclEntry> acl = view.getAcl();58System.out.println(" -- current ACL --");59printAcl(acl);6061// insert entry to grant owner read access62UserPrincipal owner = view.getOwner();63AclEntry entry = AclEntry.newBuilder()64.setType(ALLOW)65.setPrincipal(owner)66.setPermissions(READ_DATA, READ_ATTRIBUTES)67.build();68System.out.println(" -- insert (entry 0) --");69System.out.format(" %s%n", entry);70acl.add(0, entry);71view.setAcl(acl);7273// re-ACL and check entry74List<AclEntry> newacl = view.getAcl();75System.out.println(" -- current ACL --");76printAcl(acl);77if (!newacl.get(0).equals(entry)) {78throw new RuntimeException("Entry 0 is not expected");79}8081// if PosixFileAttributeView then repeat test with OWNER@82if (Files.getFileStore(file).supportsFileAttributeView("posix")) {83owner = file.getFileSystem().getUserPrincipalLookupService()84.lookupPrincipalByName("OWNER@");85entry = AclEntry.newBuilder(entry).setPrincipal(owner).build();8687System.out.println(" -- replace (entry 0) --");88System.out.format(" %s%n", entry);8990acl.set(0, entry);91view.setAcl(acl);92newacl = view.getAcl();93System.out.println(" -- current ACL --");94printAcl(acl);95if (!newacl.get(0).equals(entry)) {96throw new RuntimeException("Entry 0 is not expected");97}98}99}100101static FileAttribute<List<AclEntry>> asAclAttribute(final List<AclEntry> acl) {102return new FileAttribute<List<AclEntry>>() {103public String name() { return "acl:acl"; }104public List<AclEntry> value() { return acl; }105};106}107108static void assertEquals(List<AclEntry> actual, List<AclEntry> expected) {109if (!actual.equals(expected)) {110System.err.format("Actual: %s\n", actual);111System.err.format("Expected: %s\n", expected);112throw new RuntimeException("ACL not expected");113}114}115116// sanity check create a file or directory with initial ACL117static void testCreateFile(Path dir) throws IOException {118UserPrincipal user = Files.getOwner(dir);119AclFileAttributeView view;120121// create file with initial ACL122System.out.println("-- create file with initial ACL --");123Path file = dir.resolve("gus");124List<AclEntry> fileAcl = Arrays.asList(125AclEntry.newBuilder()126.setType(AclEntryType.ALLOW)127.setPrincipal(user)128.setPermissions(SYNCHRONIZE, READ_DATA, WRITE_DATA,129READ_ATTRIBUTES, READ_ACL, WRITE_ATTRIBUTES, DELETE)130.build());131Files.createFile(file, asAclAttribute(fileAcl));132view = Files.getFileAttributeView(file, AclFileAttributeView.class);133assertEquals(view.getAcl(), fileAcl);134135// create directory with initial ACL136System.out.println("-- create directory with initial ACL --");137Path subdir = dir.resolve("stuff");138List<AclEntry> dirAcl = Arrays.asList(139AclEntry.newBuilder()140.setType(AclEntryType.ALLOW)141.setPrincipal(user)142.setPermissions(SYNCHRONIZE, ADD_FILE, DELETE)143.build(),144AclEntry.newBuilder(fileAcl.get(0))145.setFlags(FILE_INHERIT)146.build());147Files.createDirectory(subdir, asAclAttribute(dirAcl));148view = Files.getFileAttributeView(subdir, AclFileAttributeView.class);149assertEquals(view.getAcl(), dirAcl);150}151152public static void main(String[] args) throws IOException {153// use work directory rather than system temporary directory to154// improve chances that ACLs are supported155Path dir = Paths.get("./work" + new Random().nextInt());156Files.createDirectory(dir);157try {158if (!Files.getFileStore(dir).supportsFileAttributeView("acl")) {159System.out.println("ACLs not supported - test skipped!");160return;161}162testReadWrite(dir);163164// only currently feasible on Windows165if (System.getProperty("os.name").startsWith("Windows"))166testCreateFile(dir);167168} finally {169TestUtil.removeAll(dir);170}171}172}173174175