Path: blob/master/test/jdk/java/nio/file/attribute/PosixFileAttributeView/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 683833325* @summary Unit test for java.nio.file.attribute.PosixFileAttributeView26* @library ../..27*/2829import java.nio.file.*;30import static java.nio.file.LinkOption.*;31import java.nio.file.attribute.*;32import java.io.IOException;33import java.util.*;3435/**36* Unit test for PosixFileAttributeView, passing silently if this attribute37* view is not available.38*/3940public class Basic {4142/**43* Use view to update permission to the given mode and check that the44* permissions have been updated.45*/46static void testPermissions(Path file, String mode) throws IOException {47System.out.format("change mode: %s\n", mode);48Set<PosixFilePermission> perms = PosixFilePermissions.fromString(mode);4950// change permissions and re-read them.51Files.setPosixFilePermissions(file, perms);52Set<PosixFilePermission> current = Files.getPosixFilePermissions(file);53if (!current.equals(perms)) {54throw new RuntimeException("Actual permissions: " +55PosixFilePermissions.toString(current) + ", expected: " +56PosixFilePermissions.toString(perms));57}5859// repeat test using setAttribute/getAttribute60Files.setAttribute(file, "posix:permissions", perms);61current = (Set<PosixFilePermission>)Files.getAttribute(file, "posix:permissions");62if (!current.equals(perms)) {63throw new RuntimeException("Actual permissions: " +64PosixFilePermissions.toString(current) + ", expected: " +65PosixFilePermissions.toString(perms));66}67}6869/**70* Check that the actual permissions of a file match or make it more71* secure than requested72*/73static void checkSecure(Set<PosixFilePermission> requested,74Set<PosixFilePermission> actual)75{76for (PosixFilePermission perm: actual) {77if (!requested.contains(perm)) {78throw new RuntimeException("Actual permissions: " +79PosixFilePermissions.toString(actual) + ", requested: " +80PosixFilePermissions.toString(requested) +81" - file is less secure than requested");82}83}84}8586/**87* Create file with given mode and check that the file is created with a88* mode that is not less secure89*/90static void createWithPermissions(Path file,91String mode)92throws IOException93{94Set<PosixFilePermission> requested = PosixFilePermissions.fromString(mode);95FileAttribute<Set<PosixFilePermission>> attr =96PosixFilePermissions.asFileAttribute(requested);97System.out.format("create file with mode: %s\n", mode);98Files.createFile(file, attr);99try {100checkSecure(requested,101Files.getFileAttributeView(file, PosixFileAttributeView.class)102.readAttributes()103.permissions());104} finally {105Files.delete(file);106}107108System.out.format("create directory with mode: %s\n", mode);109Files.createDirectory(file, attr);110try {111checkSecure(requested,112Files.getFileAttributeView(file, PosixFileAttributeView.class)113.readAttributes()114.permissions());115} finally {116Files.delete(file);117}118}119120/**121* Test the setPermissions/permissions methods.122*/123static void permissionTests(Path dir)124throws IOException125{126System.out.println("-- Permission Tests --");127128// create file and test updating and reading its permissions129Path file = dir.resolve("foo");130System.out.format("create %s\n", file);131Files.createFile(file);132try {133// get initial permissions so that we can restore them later134PosixFileAttributeView view =135Files.getFileAttributeView(file, PosixFileAttributeView.class);136Set<PosixFilePermission> save = view.readAttributes()137.permissions();138139// test various modes140try {141testPermissions(file, "---------");142testPermissions(file, "r--------");143testPermissions(file, "-w-------");144testPermissions(file, "--x------");145testPermissions(file, "rwx------");146testPermissions(file, "---r-----");147testPermissions(file, "----w----");148testPermissions(file, "-----x---");149testPermissions(file, "---rwx---");150testPermissions(file, "------r--");151testPermissions(file, "-------w-");152testPermissions(file, "--------x");153testPermissions(file, "------rwx");154testPermissions(file, "r--r-----");155testPermissions(file, "r--r--r--");156testPermissions(file, "rw-rw----");157testPermissions(file, "rwxrwx---");158testPermissions(file, "rw-rw-r--");159testPermissions(file, "r-xr-x---");160testPermissions(file, "r-xr-xr-x");161testPermissions(file, "rwxrwxrwx");162} finally {163view.setPermissions(save);164}165} finally {166Files.delete(file);167}168169// create link (to file that doesn't exist) and test reading of170// permissions171if (TestUtil.supportsLinks(dir)) {172Path link = dir.resolve("link");173System.out.format("create link %s\n", link);174Files.createSymbolicLink(link, file);175try {176PosixFileAttributes attrs =177Files.getFileAttributeView(link,178PosixFileAttributeView.class,179NOFOLLOW_LINKS)180.readAttributes();181if (!attrs.isSymbolicLink()) {182throw new RuntimeException("not a link");183}184} finally {185Files.delete(link);186}187}188189System.out.println("OKAY");190}191192/**193* Test creating a file and directory with initial permissios194*/195static void createTests(Path dir)196throws IOException197{198System.out.println("-- Create Tests --");199200Path file = dir.resolve("foo");201202createWithPermissions(file, "---------");203createWithPermissions(file, "r--------");204createWithPermissions(file, "-w-------");205createWithPermissions(file, "--x------");206createWithPermissions(file, "rwx------");207createWithPermissions(file, "---r-----");208createWithPermissions(file, "----w----");209createWithPermissions(file, "-----x---");210createWithPermissions(file, "---rwx---");211createWithPermissions(file, "------r--");212createWithPermissions(file, "-------w-");213createWithPermissions(file, "--------x");214createWithPermissions(file, "------rwx");215createWithPermissions(file, "r--r-----");216createWithPermissions(file, "r--r--r--");217createWithPermissions(file, "rw-rw----");218createWithPermissions(file, "rwxrwx---");219createWithPermissions(file, "rw-rw-r--");220createWithPermissions(file, "r-xr-x---");221createWithPermissions(file, "r-xr-xr-x");222createWithPermissions(file, "rwxrwxrwx");223224System.out.println("OKAY");225}226227/**228* Test setOwner/setGroup methods - this test simply exercises the229* methods to avoid configuration.230*/231static void ownerTests(Path dir)232throws IOException233{234System.out.println("-- Owner Tests --");235236Path file = dir.resolve("gus");237System.out.format("create %s\n", file);238239Files.createFile(file);240try {241242// read attributes of directory to get owner/group243PosixFileAttributeView view =244Files.getFileAttributeView(file, PosixFileAttributeView.class);245PosixFileAttributes attrs = view.readAttributes();246247// set to existing owner/group248view.setOwner(attrs.owner());249view.setGroup(attrs.group());250251// repeat test using set/getAttribute252UserPrincipal owner = (UserPrincipal)Files.getAttribute(file, "posix:owner");253Files.setAttribute(file, "posix:owner", owner);254UserPrincipal group = (UserPrincipal)Files.getAttribute(file, "posix:group");255Files.setAttribute(file, "posix:group", group);256257} finally {258Files.delete(file);259}260261System.out.println("OKAY");262}263264/**265* Test the lookupPrincipalByName/lookupPrincipalByGroupName methods266*/267static void lookupPrincipalTests(Path dir)268throws IOException269{270System.out.println("-- Lookup UserPrincipal Tests --");271272UserPrincipalLookupService lookupService = dir.getFileSystem()273.getUserPrincipalLookupService();274275// read attributes of directory to get owner/group276PosixFileAttributes attrs = Files.readAttributes(dir, PosixFileAttributes.class);277278// lookup owner and check it matches file's owner279System.out.format("lookup: %s\n", attrs.owner().getName());280try {281UserPrincipal owner = lookupService.lookupPrincipalByName(attrs.owner().getName());282if (owner instanceof GroupPrincipal)283throw new RuntimeException("owner is a group?");284if (!owner.equals(attrs.owner()))285throw new RuntimeException("owner different from file owner");286} catch (UserPrincipalNotFoundException x) {287System.out.println("user not found - test skipped");288}289290// lookup group and check it matches file's group-owner291System.out.format("lookup group: %s\n", attrs.group().getName());292try {293GroupPrincipal group = lookupService.lookupPrincipalByGroupName(attrs.group().getName());294if (!group.equals(attrs.group()))295throw new RuntimeException("group different from file group-owner");296} catch (UserPrincipalNotFoundException x) {297System.out.println("group not found - test skipped");298}299300// test that UserPrincipalNotFoundException is thrown301String invalidPrincipal = "scumbag99";302try {303System.out.format("lookup: %s\n", invalidPrincipal);304lookupService.lookupPrincipalByName(invalidPrincipal);305throw new RuntimeException("'" + invalidPrincipal + "' is a valid user?");306} catch (UserPrincipalNotFoundException x) {307}308try {309System.out.format("lookup group: %s\n", invalidPrincipal);310lookupService.lookupPrincipalByGroupName("idonotexist");311throw new RuntimeException("'" + invalidPrincipal + "' is a valid group?");312} catch (UserPrincipalNotFoundException x) {313}314System.out.println("OKAY");315}316317/**318* Test various exceptions are thrown as expected319*/320@SuppressWarnings("unchecked")321static void exceptionsTests(Path dir)322throws IOException323{324System.out.println("-- Exceptions --");325326PosixFileAttributeView view =327Files.getFileAttributeView(dir,PosixFileAttributeView.class);328329// NullPointerException330try {331view.setOwner(null);332throw new RuntimeException("NullPointerException not thrown");333} catch (NullPointerException x) {334}335try {336view.setGroup(null);337throw new RuntimeException("NullPointerException not thrown");338} catch (NullPointerException x) {339}340341UserPrincipalLookupService lookupService = dir.getFileSystem()342.getUserPrincipalLookupService();343try {344lookupService.lookupPrincipalByName(null);345throw new RuntimeException("NullPointerException not thrown");346} catch (NullPointerException x) {347}348try {349lookupService.lookupPrincipalByGroupName(null);350throw new RuntimeException("NullPointerException not thrown");351} catch (NullPointerException x) {352}353try {354view.setPermissions(null);355throw new RuntimeException("NullPointerException not thrown");356} catch (NullPointerException x) {357}358try {359Set<PosixFilePermission> perms = new HashSet<>();360perms.add(null);361view.setPermissions(perms);362throw new RuntimeException("NullPointerException not thrown");363} catch (NullPointerException x) {364}365366// ClassCastException367try {368Set perms = new HashSet(); // raw type369perms.add(new Object());370view.setPermissions(perms);371throw new RuntimeException("ClassCastException not thrown");372} catch (ClassCastException x) {373}374375System.out.println("OKAY");376}377378public static void main(String[] args) throws IOException {379Path dir = TestUtil.createTemporaryDirectory();380try {381if (!Files.getFileStore(dir).supportsFileAttributeView("posix")) {382System.out.println("PosixFileAttributeView not supported");383return;384}385386permissionTests(dir);387createTests(dir);388ownerTests(dir);389lookupPrincipalTests(dir);390exceptionsTests(dir);391392} finally {393TestUtil.removeAll(dir);394}395}396}397398399