0N/A/*
3909N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
2918N/A @bug 4167472 5097703 6216563 6284003 6728842 6464744
0N/A @summary Basic test for setWritable/Readable/Executable methods
0N/A */
0N/A
0N/Aimport java.io.*;
3471N/Aimport java.nio.file.*;
2918N/Aimport java.nio.file.attribute.*;
0N/A
0N/Apublic class SetAccess {
0N/A public static void main(String[] args) throws Exception {
0N/A File d = new File(System.getProperty("test.dir", "."));
0N/A
0N/A File f = new File(d, "x.SetAccessPermission");
0N/A if (f.exists() && !f.delete())
0N/A throw new Exception("Can't delete test file: " + f);
0N/A OutputStream o = new FileOutputStream(f);
0N/A o.write('x');
0N/A o.close();
0N/A doTest(f);
0N/A
0N/A f = new File(d, "x.SetAccessPermission.dir");
0N/A if (f.exists() && !f.delete())
0N/A throw new Exception("Can't delete test dir: " + f);
0N/A if (!f.mkdir())
0N/A throw new Exception(f + ": Cannot create directory");
0N/A doTest(f);
0N/A }
0N/A
0N/A public static void doTest(File f) throws Exception {
0N/A if (!System.getProperty("os.name").startsWith("Windows")) {
2918N/A if (!f.setReadOnly())
2918N/A throw new Exception(f + ": setReadOnly Failed");
0N/A if (!f.setWritable(true, true) ||
0N/A !f.canWrite() ||
0N/A permission(f).charAt(2) != 'w')
0N/A throw new Exception(f + ": setWritable(true, ture) Failed");
0N/A if (!f.setWritable(false, true) ||
0N/A f.canWrite() ||
0N/A permission(f).charAt(2) != '-')
0N/A throw new Exception(f + ": setWritable(false, true) Failed");
0N/A if (!f.setWritable(true, false) ||
0N/A !f.canWrite() ||
0N/A !permission(f).matches(".(.w.){3}"))
0N/A throw new Exception(f + ": setWritable(true, false) Failed");
0N/A if (!f.setWritable(false, false) ||
0N/A f.canWrite() ||
0N/A !permission(f).matches(".(.-.){3}"))
0N/A throw new Exception(f + ": setWritable(false, true) Failed");
0N/A if (!f.setWritable(true) || !f.canWrite() ||
0N/A permission(f).charAt(2) != 'w')
0N/A throw new Exception(f + ": setWritable(true, ture) Failed");
0N/A if (!f.setWritable(false) || f.canWrite() ||
0N/A permission(f).charAt(2) != '-')
0N/A throw new Exception(f + ": setWritable(false, true) Failed");
0N/A if (!f.setExecutable(true, true) ||
0N/A !f.canExecute() ||
0N/A permission(f).charAt(3) != 'x')
0N/A throw new Exception(f + ": setExecutable(true, true) Failed");
0N/A if (!f.setExecutable(false, true) ||
0N/A f.canExecute() ||
0N/A permission(f).charAt(3) != '-')
0N/A throw new Exception(f + ": setExecutable(false, true) Failed");
0N/A if (!f.setExecutable(true, false) ||
0N/A !f.canExecute() ||
0N/A !permission(f).matches(".(..x){3}"))
0N/A throw new Exception(f + ": setExecutable(true, false) Failed");
0N/A if (!f.setExecutable(false, false) ||
0N/A f.canExecute() ||
0N/A !permission(f).matches(".(..-){3}"))
0N/A throw new Exception(f + ": setExecutable(false, false) Failed");
0N/A if (!f.setExecutable(true) || !f.canExecute() ||
0N/A permission(f).charAt(3) != 'x')
0N/A throw new Exception(f + ": setExecutable(true, true) Failed");
0N/A if (!f.setExecutable(false) || f.canExecute() ||
0N/A permission(f).charAt(3) != '-')
0N/A throw new Exception(f + ": setExecutable(false, true) Failed");
0N/A if (!f.setReadable(true, true) ||
0N/A !f.canRead() ||
0N/A permission(f).charAt(1) != 'r')
0N/A throw new Exception(f + ": setReadable(true, true) Failed");
0N/A if (!f.setReadable(false, true) ||
0N/A f.canRead() ||
0N/A permission(f).charAt(1) != '-')
0N/A throw new Exception(f + ": setReadable(false, true) Failed");
0N/A if (!f.setReadable(true, false) ||
0N/A !f.canRead() ||
0N/A !permission(f).matches(".(r..){3}"))
0N/A throw new Exception(f + ": setReadable(true, false) Failed");
0N/A if (!f.setReadable(false, false) ||
0N/A f.canRead() ||
0N/A !permission(f).matches(".(-..){3}"))
0N/A throw new Exception(f + ": setReadable(false, false) Failed");
0N/A if (!f.setReadable(true) || !f.canRead() ||
0N/A permission(f).charAt(1) != 'r')
0N/A throw new Exception(f + ": setReadable(true, true) Failed");
0N/A if (!f.setReadable(false) || f.canRead() ||
0N/A permission(f).charAt(1) != '-')
0N/A throw new Exception(f + ": setReadable(false, true) Failed");
0N/A } else {
0N/A //Windows platform
2918N/A if (f.isFile()) {
2918N/A if (!f.setReadOnly())
2918N/A throw new Exception(f + ": setReadOnly Failed");
2918N/A if (!f.setWritable(true, true) || !f.canWrite())
2918N/A throw new Exception(f + ": setWritable(true, ture) Failed");
2918N/A if (!f.setWritable(true, false) || !f.canWrite())
2918N/A throw new Exception(f + ": setWritable(true, false) Failed");
2918N/A if (!f.setWritable(true) || !f.canWrite())
2918N/A throw new Exception(f + ": setWritable(true, ture) Failed");
2918N/A if (!f.setExecutable(true, true) || !f.canExecute())
2918N/A throw new Exception(f + ": setExecutable(true, true) Failed");
2918N/A if (!f.setExecutable(true, false) || !f.canExecute())
2918N/A throw new Exception(f + ": setExecutable(true, false) Failed");
2918N/A if (!f.setExecutable(true) || !f.canExecute())
2918N/A throw new Exception(f + ": setExecutable(true, true) Failed");
2918N/A if (!f.setReadable(true, true) || !f.canRead())
2918N/A throw new Exception(f + ": setReadable(true, true) Failed");
2918N/A if (!f.setReadable(true, false) || !f.canRead())
2918N/A throw new Exception(f + ": setReadable(true, false) Failed");
2918N/A if (!f.setReadable(true) || !f.canRead())
2918N/A throw new Exception(f + ": setReadable(true, true) Failed");
2918N/A }
0N/A if (f.isDirectory()) {
2918N/A // setWritable should fail on directories because the DOS readonly
2918N/A // attribute prevents a directory from being deleted.
2918N/A if (f.setWritable(false, true))
2918N/A throw new Exception(f + ": setWritable(false, true) Succeeded");
2918N/A if (f.setWritable(false, false))
2918N/A throw new Exception(f + ": setWritable(false, false) Succeeded");
2918N/A if (f.setWritable(false))
2918N/A throw new Exception(f + ": setWritable(false) Succeeded");
0N/A } else {
0N/A if (!f.setWritable(false, true) || f.canWrite())
0N/A throw new Exception(f + ": setWritable(false, true) Failed");
0N/A if (!f.setWritable(false, false) || f.canWrite())
2918N/A throw new Exception(f + ": setWritable(false, false) Failed");
0N/A if (!f.setWritable(false) || f.canWrite())
2918N/A throw new Exception(f + ": setWritable(false) Failed");
0N/A }
0N/A if (f.setExecutable(false, true))
0N/A throw new Exception(f + ": setExecutable(false, true) Failed");
0N/A if (f.setExecutable(false, false))
0N/A throw new Exception(f + ": setExecutable(false, false) Failed");
0N/A if (f.setExecutable(false))
0N/A throw new Exception(f + ": setExecutable(false, true) Failed");
0N/A if (f.setReadable(false, true))
0N/A throw new Exception(f + ": setReadable(false, true) Failed");
0N/A if (f.setReadable(false, false))
0N/A throw new Exception(f + ": setReadable(false, false) Failed");
0N/A if (f.setReadable(false))
0N/A throw new Exception(f + ": setReadable(false, true) Failed");
0N/A }
0N/A if (f.exists() && !f.delete())
0N/A throw new Exception("Can't delete test dir: " + f);
0N/A }
0N/A
0N/A private static String permission(File f) throws Exception {
3471N/A PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class);
2918N/A String type = attrs.isDirectory() ? "d" : " ";
2918N/A return type + PosixFilePermissions.toString(attrs.permissions());
0N/A }
0N/A}