diff options
9 files changed, 94 insertions, 688 deletions
diff --git a/luni/src/test/java/libcore/java/nio/file/DefaultFileSystemProvider2Test.java b/luni/src/test/java/libcore/java/nio/file/DefaultFileSystemProvider2Test.java index dbbfeae449..2916b038f0 100644 --- a/luni/src/test/java/libcore/java/nio/file/DefaultFileSystemProvider2Test.java +++ b/luni/src/test/java/libcore/java/nio/file/DefaultFileSystemProvider2Test.java @@ -263,24 +263,35 @@ public class DefaultFileSystemProvider2Test { @Test public void test_getFileStore() throws IOException { - FileStore fileStore = provider.getFileStore(filesSetup.getDataFilePath()); - assertNotNull(fileStore); - } + try { + provider.getFileStore(filesSetup.getDataFilePath()); + fail(); + } catch (SecurityException expected) { + } - @Test - public void test_getFileStore_NPE() throws IOException { try { provider.getFileStore(null); fail(); - } catch(NullPointerException expected) {} + } catch (SecurityException expected) { + } } @Test public void test_isHidden() throws IOException { assertFalse(provider.isHidden(filesSetup.getDataFilePath())); - Files.setAttribute(filesSetup.getDataFilePath(), "dos:hidden", true); - // Files can't be hid. + // Files can't be hidden using the "dos" view, which is unsupported since it relies + // on a custom xattr, which may or may not be available on all FSs. + // + // Note that this weirdly asymmetric : setting the hidden attribute uses xattrs to + // emulate dos attributes whereas isHidden checks whether the the file name begins with a + // leading period. <shrug> + try { + Files.setAttribute(filesSetup.getDataFilePath(), "dos:hidden", true); + fail(); + } catch (UnsupportedOperationException expected) { + } + assertFalse(provider.isHidden(filesSetup.getDataFilePath())); } diff --git a/luni/src/test/java/libcore/java/nio/file/Files2Test.java b/luni/src/test/java/libcore/java/nio/file/Files2Test.java index 394643a308..436d3e309f 100644 --- a/luni/src/test/java/libcore/java/nio/file/Files2Test.java +++ b/luni/src/test/java/libcore/java/nio/file/Files2Test.java @@ -138,9 +138,12 @@ public class Files2Test { @Test public void test_getFileStore() throws IOException { - FileStore mockFileStore = mock(FileStore.class); - when(mockFileSystemProvider.getFileStore(mockPath)).thenReturn(mockFileStore); - assertEquals(mockFileStore, Files.getFileStore(mockPath)); + when(mockFileSystemProvider.getFileStore(mockPath)).thenThrow(new SecurityException()); + try { + Files.getFileStore(mockPath); + fail(); + } catch (SecurityException expected) { + } } @Test diff --git a/ojluni/src/main/java/sun/nio/fs/LinuxFileSystemProvider.java b/ojluni/src/main/java/sun/nio/fs/LinuxFileSystemProvider.java index 8f1fa477f1..e6468d1ad9 100644 --- a/ojluni/src/main/java/sun/nio/fs/LinuxFileSystemProvider.java +++ b/ojluni/src/main/java/sun/nio/fs/LinuxFileSystemProvider.java @@ -46,7 +46,11 @@ public class LinuxFileSystemProvider extends UnixFileSystemProvider { @Override LinuxFileStore getFileStore(UnixPath path) throws IOException { - return new LinuxFileStore(path); + // Android-changed: Complete information about file systems is neither available to regular + // apps nor the system server due to SELinux policies. + // return new LinuxFileStore(path); + + throw new SecurityException("getFileStore"); } @Override @@ -55,14 +59,18 @@ public class LinuxFileSystemProvider extends UnixFileSystemProvider { Class<V> type, LinkOption... options) { - if (type == DosFileAttributeView.class) { - return (V) new LinuxDosFileAttributeView(UnixPath.toUnixPath(obj), - Util.followLinks(options)); - } - if (type == UserDefinedFileAttributeView.class) { - return (V) new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj), - Util.followLinks(options)); - } + // Android-changed: Delegate to UnixFileSystemProvider, remove support for "dos" and + // "user" file attribute views which are not supported. + // + // if (type == DosFileAttributeView.class) { + // return (V) new LinuxDosFileAttributeView(UnixPath.toUnixPath(obj), + // Util.followLinks(options)); + // } + // if (type == UserDefinedFileAttributeView.class) { + // return (V) new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj), + // Util.followLinks(options)); + // } + return super.getFileAttributeView(obj, type, options); } @@ -71,14 +79,18 @@ public class LinuxFileSystemProvider extends UnixFileSystemProvider { String name, LinkOption... options) { - if (name.equals("dos")) { - return new LinuxDosFileAttributeView(UnixPath.toUnixPath(obj), - Util.followLinks(options)); - } - if (name.equals("user")) { - return new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj), - Util.followLinks(options)); - } + // Android-changed: Delegate to UnixFileSystemProvider, remove support for "dos" and + // "user" file attribute views which are not supported. + // + // if (name.equals("dos")) { + // return new LinuxDosFileAttributeView(UnixPath.toUnixPath(obj), + // Util.followLinks(options)); + // } + // if (name.equals("user")) { + // return new LinuxUserDefinedFileAttributeView(UnixPath.toUnixPath(obj), + // Util.followLinks(options)); + // } + return super.getFileAttributeView(obj, name, options); } @@ -89,13 +101,17 @@ public class LinuxFileSystemProvider extends UnixFileSystemProvider { LinkOption... options) throws IOException { - if (type == DosFileAttributes.class) { - DosFileAttributeView view = - getFileAttributeView(file, DosFileAttributeView.class, options); - return (A) view.readAttributes(); - } else { - return super.readAttributes(file, type, options); - } + // Android-changed: Delegate to UnixFileSystemProvider, remove support for "dos" and + // "user" file attribute views which are not supported. + // + // if (type == DosFileAttributes.class) { + // DosFileAttributeView view = + // getFileAttributeView(file, DosFileAttributeView.class, options); + // return (A) view.readAttributes(); + // } else { + // return super.readAttributes(file, type, options); + // } + return super.readAttributes(file, type, options); } @Override diff --git a/ojluni/src/main/java/sun/nio/fs/UnixFileSystemProvider.java b/ojluni/src/main/java/sun/nio/fs/UnixFileSystemProvider.java index 182a57b94c..37249560b0 100644 --- a/ojluni/src/main/java/sun/nio/fs/UnixFileSystemProvider.java +++ b/ojluni/src/main/java/sun/nio/fs/UnixFileSystemProvider.java @@ -359,13 +359,17 @@ public abstract class UnixFileSystemProvider @Override public FileStore getFileStore(Path obj) throws IOException { - UnixPath file = UnixPath.toUnixPath(obj); - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPermission(new RuntimePermission("getFileStoreAttributes")); - file.checkRead(); - } - return getFileStore(file); + // Android-changed: Complete information about file systems is neither available to regular + // apps nor the system server due to SELinux policies. + // + // UnixPath file = UnixPath.toUnixPath(obj); + // SecurityManager sm = System.getSecurityManager(); + // if (sm != null) { + // sm.checkPermission(new RuntimePermission("getFileStoreAttributes")); + // file.checkRead(); + // } + // return getFileStore(file); + throw new SecurityException("getFileStore"); } @Override diff --git a/ojluni/src/test/java/nio/file/attribute/AclFileAttributeViewTest.java b/ojluni/src/test/java/nio/file/attribute/AclFileAttributeViewTest.java deleted file mode 100644 index 2d9e32c7a6..0000000000 --- a/ojluni/src/test/java/nio/file/attribute/AclFileAttributeViewTest.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. - * 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. - * - * 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. - */ - -/* @test - * @bug 4313887 6838333 6891404 - * @summary Unit test for java.nio.file.attribute.AclFileAttribueView - * @library ../.. - */ -// Android-changed: Adapted from -// jdk/test/java/nio/file/attribute/AclFileAttributeView/Basic.java -// Android-changed: Added package & Test import -package test.java.nio.file.attribute; -import org.testng.annotations.Test; -import test.java.nio.file.TestUtil; - -import java.nio.file.*; -import java.nio.file.attribute.*; -import java.io.IOException; -import java.util.*; - -import static java.nio.file.attribute.AclEntryType.*; -import static java.nio.file.attribute.AclEntryPermission.*; -import static java.nio.file.attribute.AclEntryFlag.*; - -// Android-changed: Renamed from "Basic" -public class AclFileAttributeViewTest { - - static void printAcl(List<AclEntry> acl) { - for (AclEntry entry: acl) { - System.out.format(" %s%n", entry); - } - } - - // sanity check read and writing ACL - static void testReadWrite(Path dir) throws IOException { - Path file = dir.resolve("foo"); - if (Files.notExists(file)) - Files.createFile(file); - - AclFileAttributeView view = - Files.getFileAttributeView(file, AclFileAttributeView.class); - - // print existing ACL - List<AclEntry> acl = view.getAcl(); - System.out.println(" -- current ACL --"); - printAcl(acl); - - // insert entry to grant owner read access - UserPrincipal owner = view.getOwner(); - AclEntry entry = AclEntry.newBuilder() - .setType(ALLOW) - .setPrincipal(owner) - .setPermissions(READ_DATA, READ_ATTRIBUTES) - .build(); - System.out.println(" -- insert (entry 0) --"); - System.out.format(" %s%n", entry); - acl.add(0, entry); - view.setAcl(acl); - - // re-ACL and check entry - List<AclEntry> newacl = view.getAcl(); - System.out.println(" -- current ACL --"); - printAcl(acl); - if (!newacl.get(0).equals(entry)) { - throw new RuntimeException("Entry 0 is not expected"); - } - - // if PosixFileAttributeView then repeat test with OWNER@ - if (Files.getFileStore(file).supportsFileAttributeView("posix")) { - owner = file.getFileSystem().getUserPrincipalLookupService() - .lookupPrincipalByName("OWNER@"); - entry = AclEntry.newBuilder(entry).setPrincipal(owner).build(); - - System.out.println(" -- replace (entry 0) --"); - System.out.format(" %s%n", entry); - - acl.set(0, entry); - view.setAcl(acl); - newacl = view.getAcl(); - System.out.println(" -- current ACL --"); - printAcl(acl); - if (!newacl.get(0).equals(entry)) { - throw new RuntimeException("Entry 0 is not expected"); - } - } - } - - static FileAttribute<List<AclEntry>> asAclAttribute(final List<AclEntry> acl) { - return new FileAttribute<List<AclEntry>>() { - public String name() { return "acl:acl"; } - public List<AclEntry> value() { return acl; } - }; - } - - static void assertEquals(List<AclEntry> actual, List<AclEntry> expected) { - if (!actual.equals(expected)) { - System.err.format("Actual: %s\n", actual); - System.err.format("Expected: %s\n", expected); - throw new RuntimeException("ACL not expected"); - } - } - - // sanity check create a file or directory with initial ACL - static void testCreateFile(Path dir) throws IOException { - UserPrincipal user = Files.getOwner(dir); - AclFileAttributeView view; - - // create file with initial ACL - System.out.println("-- create file with initial ACL --"); - Path file = dir.resolve("gus"); - List<AclEntry> fileAcl = Arrays.asList( - AclEntry.newBuilder() - .setType(AclEntryType.ALLOW) - .setPrincipal(user) - .setPermissions(SYNCHRONIZE, READ_DATA, WRITE_DATA, - READ_ATTRIBUTES, READ_ACL, WRITE_ATTRIBUTES, DELETE) - .build()); - Files.createFile(file, asAclAttribute(fileAcl)); - view = Files.getFileAttributeView(file, AclFileAttributeView.class); - assertEquals(view.getAcl(), fileAcl); - - // create directory with initial ACL - System.out.println("-- create directory with initial ACL --"); - Path subdir = dir.resolve("stuff"); - List<AclEntry> dirAcl = Arrays.asList( - AclEntry.newBuilder() - .setType(AclEntryType.ALLOW) - .setPrincipal(user) - .setPermissions(SYNCHRONIZE, ADD_FILE, DELETE) - .build(), - AclEntry.newBuilder(fileAcl.get(0)) - .setFlags(FILE_INHERIT) - .build()); - Files.createDirectory(subdir, asAclAttribute(dirAcl)); - view = Files.getFileAttributeView(subdir, AclFileAttributeView.class); - assertEquals(view.getAcl(), dirAcl); - } - - // Android-changed: Removed args & added @Test - @Test - public static void main() throws IOException { - // use work directory rather than system temporary directory to - // improve chances that ACLs are supported - // Android-changed: Switched to temp dir due to permissions - // Path dir = Paths.get("./work" + new Random().nextInt()); - // Files.createTempDirectory(dir); - Path dir = Files.createTempDirectory("acl"); - try { - if (!Files.getFileStore(dir).supportsFileAttributeView("acl")) { - System.out.println("ACLs not supported - test skipped!"); - return; - } - testReadWrite(dir); - - // only currently feasible on Windows - if (System.getProperty("os.name").startsWith("Windows")) - testCreateFile(dir); - - } finally { - TestUtil.removeAll(dir); - } - } -} diff --git a/ojluni/src/test/java/nio/file/attribute/BasicFileAttributeViewCreationTimeTest.java b/ojluni/src/test/java/nio/file/attribute/BasicFileAttributeViewCreationTimeTest.java index b393981fe2..c2a30bb58a 100644 --- a/ojluni/src/test/java/nio/file/attribute/BasicFileAttributeViewCreationTimeTest.java +++ b/ojluni/src/test/java/nio/file/attribute/BasicFileAttributeViewCreationTimeTest.java @@ -80,15 +80,17 @@ public class BasicFileAttributeViewCreationTimeTest { boolean supportsCreationTimeRead = false; boolean supportsCreationTimeWrite = false; String os = System.getProperty("os.name"); - if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) { - supportsCreationTimeRead = true; - } else if (os.startsWith("Windows")) { - String type = Files.getFileStore(file).type(); - if (type.equals("NTFS") || type.equals("FAT")) { - supportsCreationTimeRead = true; - supportsCreationTimeWrite = true; - } - } + // Android-changed: This test is never run on Mac OS or windows hosts. + // + // if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) { + // supportsCreationTimeRead = true; + // } else if (os.startsWith("Windows")) { + // String type = Files.getFileStore(file).type(); + // if (type.equals("NTFS") || type.equals("FAT")) { + // supportsCreationTimeRead = true; + // supportsCreationTimeWrite = true; + // } + // } /** * If the creation-time attribute is supported then change the file's diff --git a/ojluni/src/test/java/nio/file/attribute/DosFileAttributeViewTest.java b/ojluni/src/test/java/nio/file/attribute/DosFileAttributeViewTest.java deleted file mode 100644 index 8386a314ee..0000000000 --- a/ojluni/src/test/java/nio/file/attribute/DosFileAttributeViewTest.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. - * 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. - * - * 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. - */ - -/* @test - * @bug 4313887 6838333 - * @summary Unit test for java.nio.file.attribute.DosFileAttributeView - * @library ../.. - */ -// Android-changed: Adapted from -// jdk/test/java/nio/file/attribute/DosFileAttributeView/Basic.java -// Android-changed: Added package & Test import -package test.java.nio.file.attribute; -import org.testng.annotations.Test; -import test.java.nio.file.TestUtil; - -import java.nio.file.*; -import static java.nio.file.LinkOption.*; -import java.nio.file.attribute.*; -import java.util.*; -import java.io.IOException; - -// Android-changed: Renamed from "Basic" -public class DosFileAttributeViewTest { - - static void check(boolean okay) { - if (!okay) - throw new RuntimeException("Test failed"); - } - - // exercise each setter/getter method, leaving all attributes unset - static void testAttributes(DosFileAttributeView view) throws IOException { - view.setReadOnly(true); - check(view.readAttributes().isReadOnly()); - view.setReadOnly(false); - check(!view.readAttributes().isReadOnly()); - view.setHidden(true); - check(view.readAttributes().isHidden()); - view.setHidden(false); - check(!view.readAttributes().isHidden()); - view.setArchive(true); - check(view.readAttributes().isArchive()); - view.setArchive(false); - check(!view.readAttributes().isArchive()); - view.setSystem(true); - check(view.readAttributes().isSystem()); - view.setSystem(false); - check(!view.readAttributes().isSystem()); - } - - // set the value of all attributes - static void setAll(DosFileAttributeView view, boolean value) - throws IOException - { - view.setReadOnly(value); - view.setHidden(value); - view.setArchive(value); - view.setSystem(value); - } - - // read and write FAT attributes - static void readWriteTests(Path dir) throws IOException { - - // create "foo" and test that we can read/write each FAT attribute - Path file = Files.createFile(dir.resolve("foo")); - try { - testAttributes(Files.getFileAttributeView(file, DosFileAttributeView.class)); - - // Following tests use a symbolic link so skip if not supported - if (!TestUtil.supportsLinks(dir)) - return; - - Path link = dir.resolve("link"); - Files.createSymbolicLink(link, file); - - // test following links - testAttributes(Files.getFileAttributeView(link, DosFileAttributeView.class)); - - // test not following links - try { - try { - testAttributes(Files - .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS)); - } catch (IOException x) { - // access to link attributes not supported - return; - } - - // set all attributes on link - // run test on target of link (which leaves them all un-set) - // check that attributes of link remain all set - setAll(Files - .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), true); - testAttributes(Files - .getFileAttributeView(link, DosFileAttributeView.class)); - DosFileAttributes attrs = - Files.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS) - .readAttributes(); - check(attrs.isReadOnly()); - check(attrs.isHidden()); - check(attrs.isArchive()); - check(attrs.isSystem()); - setAll(Files - .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), false); - - // set all attributes on target - // run test on link (which leaves them all un-set) - // check that attributes of target remain all set - setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), true); - testAttributes(Files - .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS)); - attrs = Files.getFileAttributeView(link, DosFileAttributeView.class).readAttributes(); - check(attrs.isReadOnly()); - check(attrs.isHidden()); - check(attrs.isArchive()); - check(attrs.isSystem()); - setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), false); - } finally { - TestUtil.deleteUnchecked(link); - } - } finally { - TestUtil.deleteUnchecked(file); - } - } - - // Android-changed: Removed args & added @Test - @Test - public static void main() throws IOException { - // create temporary directory to run tests - Path dir = TestUtil.createTemporaryDirectory(); - - try { - // skip test if DOS file attributes not supported - if (!Files.getFileStore(dir).supportsFileAttributeView("dos")) { - System.out.println("DOS file attribute not supported."); - return; - } - readWriteTests(dir); - } finally { - TestUtil.removeAll(dir); - } - } -} diff --git a/ojluni/src/test/java/nio/file/attribute/PosixFileAttributeViewTest.java b/ojluni/src/test/java/nio/file/attribute/PosixFileAttributeViewTest.java index 782583790a..4355b31a35 100644 --- a/ojluni/src/test/java/nio/file/attribute/PosixFileAttributeViewTest.java +++ b/ojluni/src/test/java/nio/file/attribute/PosixFileAttributeViewTest.java @@ -388,10 +388,13 @@ public class PosixFileAttributeViewTest { public static void main() throws IOException { Path dir = TestUtil.createTemporaryDirectory(); try { - if (!Files.getFileStore(dir).supportsFileAttributeView("posix")) { - System.out.println("PosixFileAttributeView not supported"); - return; - } + // Android-changed: PosixFileAttributeViews are unconditionally supported in + // all writeable partitions. + // + // if (!Files.getFileStore(dir).supportsFileAttributeView("posix")) { + // System.out.println("PosixFileAttributeView not supported"); + // return; + // } permissionTests(dir); createTests(dir); diff --git a/ojluni/src/test/java/nio/file/attribute/UserDefinedFileAttributeViewTest.java b/ojluni/src/test/java/nio/file/attribute/UserDefinedFileAttributeViewTest.java deleted file mode 100644 index 6fb3c7828d..0000000000 --- a/ojluni/src/test/java/nio/file/attribute/UserDefinedFileAttributeViewTest.java +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. - * 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. - * - * 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. - */ - -/* @test - * @bug 4313887 6838333 - * @summary Unit test for java.nio.file.attribute.UserDefinedFileAttributeView - * @library ../.. - */ -// Android-changed: Adapted from -// jdk/test/java/nio/file/attribute/UserDefinedFileAttributeView/Basic.java -// Android-changed: Added package & Test import -package test.java.nio.file.attribute; -import org.testng.annotations.Test; -import test.java.nio.file.TestUtil; - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.nio.file.*; -import static java.nio.file.LinkOption.*; -import java.nio.file.attribute.*; -import java.util.Arrays; -import java.util.Map; -import java.util.Random; -import java.io.IOException; - -// Android-changed: Renamed from "Basic" -public class UserDefinedFileAttributeViewTest { - - private static Random rand = new Random(); - - private static final String ATTR_NAME = "mime_type"; - private static final String ATTR_VALUE = "text/plain"; - private static final String ATTR_VALUE2 = "text/html"; - - static interface Task { - void run() throws Exception; - } - - static void tryCatch(Class<? extends Throwable> ex, Task task) { - boolean caught = false; - try { - task.run(); - } catch (Throwable x) { - if (ex.isAssignableFrom(x.getClass())) { - caught = true; - } else { - throw new RuntimeException(x); - } - } - if (!caught) - throw new RuntimeException(ex.getName() + " expected"); - } - - static void expectNullPointerException(Task task) { - tryCatch(NullPointerException.class, task); - } - - static boolean hasAttribute(UserDefinedFileAttributeView view, String attr) - throws IOException - { - for (String name: view.list()) { - if (name.equals(ATTR_NAME)) - return true; - } - return false; - } - - static void test(Path file, LinkOption... options) throws IOException { - final UserDefinedFileAttributeView view = - Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options); - ByteBuffer buf = rand.nextBoolean() ? - ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100); - - // Test: write - buf.put(ATTR_VALUE.getBytes()).flip(); - int size = buf.remaining(); - int nwrote = view.write(ATTR_NAME, buf); - if (nwrote != size) - throw new RuntimeException("Unexpected number of bytes written"); - - // Test: size - if (view.size(ATTR_NAME) != size) - throw new RuntimeException("Unexpected size"); - - // Test: read - buf.clear(); - int nread = view.read(ATTR_NAME, buf); - if (nread != size) - throw new RuntimeException("Unexpected number of bytes read"); - buf.flip(); - String value = Charset.defaultCharset().decode(buf).toString(); - if (!value.equals(ATTR_VALUE)) - throw new RuntimeException("Unexpected attribute value"); - - // Test: read with insufficient space - tryCatch(IOException.class, new Task() { - public void run() throws IOException { - view.read(ATTR_NAME, ByteBuffer.allocateDirect(1)); - }}); - - // Test: replace value - buf.clear(); - buf.put(ATTR_VALUE2.getBytes()).flip(); - size = buf.remaining(); - view.write(ATTR_NAME, buf); - if (view.size(ATTR_NAME) != size) - throw new RuntimeException("Unexpected size"); - - // Test: list - if (!hasAttribute(view, ATTR_NAME)) - throw new RuntimeException("Attribute name not in list"); - - // Test: delete - view.delete(ATTR_NAME); - if (hasAttribute(view, ATTR_NAME)) - throw new RuntimeException("Attribute name in list"); - - // Test: dynamic access - String name = "user:" + ATTR_NAME; - byte[] valueAsBytes = ATTR_VALUE.getBytes(); - Files.setAttribute(file, name, valueAsBytes); - byte[] actualAsBytes = (byte[])Files.getAttribute(file, name); - if (!Arrays.equals(valueAsBytes, actualAsBytes)) - throw new RuntimeException("Unexpected attribute value"); - Map<String,?> map = Files.readAttributes(file, name); - if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) - throw new RuntimeException("Unexpected attribute value"); - map = Files.readAttributes(file, "user:*"); - if (!Arrays.equals(valueAsBytes, (byte[])map.get(ATTR_NAME))) - throw new RuntimeException("Unexpected attribute value"); - } - - static void miscTests(final Path file) throws IOException { - final UserDefinedFileAttributeView view = - Files.getFileAttributeView(file, UserDefinedFileAttributeView.class); - view.write(ATTR_NAME, ByteBuffer.wrap(ATTR_VALUE.getBytes())); - - // NullPointerException - final ByteBuffer buf = ByteBuffer.allocate(100); - - expectNullPointerException(new Task() { - public void run() throws IOException { - view.read(null, buf); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - view.read(ATTR_NAME, null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - view.write(null, buf); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - view.write(ATTR_NAME, null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - view.size(null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - view.delete(null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.getAttribute(file, null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.getAttribute(file, "user:" + ATTR_NAME, (LinkOption[])null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.setAttribute(file, "user:" + ATTR_NAME, null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.setAttribute(file, null, new byte[0]); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.setAttribute(file, "user: " + ATTR_NAME, new byte[0], (LinkOption[])null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.readAttributes(file, (String)null); - }}); - expectNullPointerException(new Task() { - public void run() throws IOException { - Files.readAttributes(file, "*", (LinkOption[])null); - }}); - - // Read-only buffer - tryCatch(IllegalArgumentException.class, new Task() { - public void run() throws IOException { - ByteBuffer buf = ByteBuffer.wrap(ATTR_VALUE.getBytes()).asReadOnlyBuffer(); - view.write(ATTR_NAME, buf); - buf.flip(); - view.read(ATTR_NAME, buf); - }}); - - // Zero bytes remaining - tryCatch(IOException.class, new Task() { - public void run() throws IOException { - ByteBuffer buf = buf = ByteBuffer.allocateDirect(100); - buf.position(buf.capacity()); - view.read(ATTR_NAME, buf); - }}); - } - - // Android-changed: Removed args & added @Test - @Test - public static void main() throws IOException { - // create temporary directory to run tests - Path dir = TestUtil.createTemporaryDirectory(); - try { - if (!Files.getFileStore(dir).supportsFileAttributeView("user")) { - System.out.println("UserDefinedFileAttributeView not supported - skip test"); - return; - } - - // test access to user defined attributes of regular file - Path file = dir.resolve("foo.html"); - Files.createFile(file); - try { - test(file); - } finally { - Files.delete(file); - } - - // test access to user defined attributes of directory - Path subdir = dir.resolve("foo"); - Files.createDirectory(subdir); - try { - test(subdir); - } finally { - Files.delete(subdir); - } - - // test access to user defined attributes of sym link - if (TestUtil.supportsLinks(dir)) { - Path target = dir.resolve("doesnotexist"); - Path link = dir.resolve("link"); - Files.createSymbolicLink(link, target); - try { - test(link, NOFOLLOW_LINKS); - } catch (IOException x) { - // access to attributes of sym link may not be supported - } finally { - Files.delete(link); - } - } - - // misc. tests - try { - file = dir.resolve("foo.txt"); - Files.createFile(file); - miscTests(dir); - } finally { - Files.delete(file); - } - - } finally { - TestUtil.removeAll(dir); - } - } - } |