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