From 1889def48bee21ce7e145de60694649ff8966f8a Mon Sep 17 00:00:00 2001 From: robm Date: Wed, 15 Aug 2012 22:46:35 +0100 Subject: [PATCH] 6931128: (spec) File attribute tests fail when run as root. Reviewed-by: alanb --- src/share/classes/java/io/File.java | 51 +++++++++++++++++++++-------- test/java/io/File/Basic.java | 6 ++-- test/java/io/File/SetAccess.java | 9 ++++- test/java/io/File/SetReadOnly.java | 9 ++++- test/java/io/File/SymLinks.java | 6 ++-- test/java/io/File/Util.java | 45 +++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 test/java/io/File/Util.java diff --git a/src/share/classes/java/io/File.java b/src/share/classes/java/io/File.java index 0dbb307c7..3c0fafca5 100644 --- a/src/share/classes/java/io/File.java +++ b/src/share/classes/java/io/File.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2012, 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 @@ -711,7 +711,10 @@ public class File /** * Tests whether the application can read the file denoted by this - * abstract pathname. + * abstract pathname. On some platforms it may be possible to start the + * Java virtual machine with special privileges that allow it to read + * files that are marked as unreadable. Consequently this method may return + * {@code true} even though the file does not have read permissions. * * @return true if and only if the file specified by this * abstract pathname exists and can be read by the @@ -732,7 +735,10 @@ public class File /** * Tests whether the application can modify the file denoted by this - * abstract pathname. + * abstract pathname. On some platforms it may be possible to start the + * Java virtual machine with special privileges that allow it to modify + * files that are marked read-only. Consequently this method may return + * {@code true} even though the file is marked read-only. * * @return true if and only if the file system actually * contains a file denoted by this abstract pathname and @@ -1351,9 +1357,11 @@ public class File /** * Marks the file or directory named by this abstract pathname so that - * only read operations are allowed. After invoking this method the file - * or directory is guaranteed not to change until it is either deleted or - * marked to allow write access. Whether or not a read-only file or + * only read operations are allowed. After invoking this method the file + * or directory will not change until it is either deleted or marked + * to allow write access. On some platforms it may be possible to start the + * Java virtual machine with special privileges that allow it to modify + * files that are marked read-only. Whether or not a read-only file or * directory may be deleted depends upon the underlying system. * * @return true if and only if the operation succeeded; @@ -1376,7 +1384,9 @@ public class File /** * Sets the owner's or everybody's write permission for this abstract - * pathname. + * pathname. On some platforms it may be possible to start the Java virtual + * machine with special privileges that allow it to modify files that + * disallow write operations. * *

The {@link java.nio.file.Files} class defines methods that operate on * file attributes including file permissions. This may be used when finer @@ -1414,7 +1424,9 @@ public class File /** * A convenience method to set the owner's write permission for this abstract - * pathname. + * pathname. On some platforms it may be possible to start the Java virtual + * machine with special privileges that allow it to modify files that + * disallow write operations. * *

An invocation of this method of the form file.setWritable(arg) * behaves in exactly the same way as the invocation @@ -1443,7 +1455,9 @@ public class File /** * Sets the owner's or everybody's read permission for this abstract - * pathname. + * pathname. On some platforms it may be possible to start the Java virtual + * machine with special privileges that allow it to read files that are + * marked as unreadable. * *

The {@link java.nio.file.Files} class defines methods that operate on * file attributes including file permissions. This may be used when finer @@ -1484,7 +1498,9 @@ public class File /** * A convenience method to set the owner's read permission for this abstract - * pathname. + * pathname. On some platforms it may be possible to start the Java virtual + * machine with special privileges that allow it to read files that that are + * marked as unreadable. * *

An invocation of this method of the form file.setReadable(arg) * behaves in exactly the same way as the invocation @@ -1516,7 +1532,9 @@ public class File /** * Sets the owner's or everybody's execute permission for this abstract - * pathname. + * pathname. On some platforms it may be possible to start the Java virtual + * machine with special privileges that allow it to execute files that are + * not marked executable. * *

The {@link java.nio.file.Files} class defines methods that operate on * file attributes including file permissions. This may be used when finer @@ -1556,8 +1574,10 @@ public class File } /** - * A convenience method to set the owner's execute permission for this abstract - * pathname. + * A convenience method to set the owner's execute permission for this + * abstract pathname. On some platforms it may be possible to start the Java + * virtual machine with special privileges that allow it to execute files + * that are not marked executable. * *

An invocation of this method of the form file.setExcutable(arg) * behaves in exactly the same way as the invocation @@ -1589,7 +1609,10 @@ public class File /** * Tests whether the application can execute the file denoted by this - * abstract pathname. + * abstract pathname. On some platforms it may be possible to start the + * Java virtual machine with special privileges that allow it to execute + * files that are not marked executable. Consequently this method may return + * {@code true} even though the file does not have execute permissions. * * @return true if and only if the abstract pathname exists * and the application is allowed to execute the file diff --git a/test/java/io/File/Basic.java b/test/java/io/File/Basic.java index a1e3ee34e..8a0943623 100644 --- a/test/java/io/File/Basic.java +++ b/test/java/io/File/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2012, 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 @@ -25,7 +25,7 @@ @bug 4165666 4203706 4288670 4290024 @summary Basic heartbeat test for File methods that access the filesystem - @build Basic + @build Basic Util @run shell basic.sh */ @@ -73,7 +73,7 @@ public class Basic { if (!f.isFile()) fail(f, "is not a file"); if (f.isDirectory()) fail(f, "is a directory"); if (!f.canRead()) fail(f, "is not readable"); - if (f.canWrite() != writeable) + if (!Util.isPrivileged() && f.canWrite() != writeable) fail(f, writeable ? "is not writeable" : "is writeable"); int rwLen = 6; if (f.length() != length) fail(f, "has wrong length"); diff --git a/test/java/io/File/SetAccess.java b/test/java/io/File/SetAccess.java index f9287ee53..1fc4e971a 100644 --- a/test/java/io/File/SetAccess.java +++ b/test/java/io/File/SetAccess.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2012, 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 @@ -24,6 +24,8 @@ /* @test @bug 4167472 5097703 6216563 6284003 6728842 6464744 @summary Basic test for setWritable/Readable/Executable methods + @build SetAccess Util + @run main SetAccess */ import java.io.*; @@ -32,6 +34,11 @@ import java.nio.file.attribute.*; public class SetAccess { public static void main(String[] args) throws Exception { + if (Util.isPrivileged()) { + System.out.println("Unable to test file permissions when running with privileges"); + return; + } + File d = new File(System.getProperty("test.dir", ".")); File f = new File(d, "x.SetAccessPermission"); diff --git a/test/java/io/File/SetReadOnly.java b/test/java/io/File/SetReadOnly.java index f06837a47..03a3d1680 100644 --- a/test/java/io/File/SetReadOnly.java +++ b/test/java/io/File/SetReadOnly.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2012, 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 @@ -24,6 +24,8 @@ /* @test @bug 4091757 4939819 6728842 @summary Basic test for setReadOnly method + @build SetReadOnly Util + @run main SetReadOnly */ import java.io.*; @@ -32,6 +34,11 @@ import java.io.*; public class SetReadOnly { public static void main(String[] args) throws Exception { + if (Util.isPrivileged()) { + System.out.println("Unable to test file permissions when running with privileges"); + return; + } + File d = new File(System.getProperty("test.dir", ".")); File f = new File(d, "x.SetReadOnly"); diff --git a/test/java/io/File/SymLinks.java b/test/java/io/File/SymLinks.java index df78f1552..04ac95eae 100644 --- a/test/java/io/File/SymLinks.java +++ b/test/java/io/File/SymLinks.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2012, 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 @@ -24,6 +24,8 @@ /* @test * @bug 6595866 * @summary Test java.io.File operations with sym links + * @build SymLinks Util + * @run main SymLinks */ import java.io.*; @@ -338,7 +340,7 @@ public class SymLinks { assertTrue(link2link2file.canRead()); assertTrue(link2link2file.canWrite()); - if (file.setReadOnly()) { + if (!Util.isPrivileged() && file.setReadOnly()) { assertFalse(file.canWrite()); assertFalse(link2file.canWrite()); assertFalse(link2link2file.canWrite()); diff --git a/test/java/io/File/Util.java b/test/java/io/File/Util.java new file mode 100644 index 000000000..2410655f6 --- /dev/null +++ b/test/java/io/File/Util.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012, 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. + */ + +import java.io.File; + +/** + * Utility class used by tests for java.io.File. + */ + +class Util { + private Util() { } + + /** + * Returns {@code true} if the Java virtual machine is running with + * privileges (loosely defined here as have write access to /etc/passwd). + */ + static boolean isPrivileged() { + String osname = System.getProperty("os.name"); + if (osname.startsWith("Windows")) + return false; + + return (new File("/etc/passwd")).canWrite(); + } +} + -- GitLab