SetAccess.java revision 0
1067N/A/*
1067N/A * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
1067N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1067N/A *
1067N/A * This code is free software; you can redistribute it and/or modify it
1067N/A * under the terms of the GNU General Public License version 2 only, as
1067N/A * published by the Free Software Foundation.
1067N/A *
1067N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1067N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1067N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1067N/A * version 2 for more details (a copy is included in the LICENSE file that
1067N/A * accompanied this code).
1067N/A *
1067N/A * You should have received a copy of the GNU General Public License version
1067N/A * 2 along with this work; if not, write to the Free Software Foundation,
1067N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1067N/A *
1067N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1067N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1067N/A * have any questions.
1067N/A */
1067N/A
1067N/A/* @test
1067N/A @bug 4167472 5097703 6216563 6284003
1067N/A @summary Basic test for setWritable/Readable/Executable methods
1067N/A */
1067N/A
1067N/Aimport java.io.*;
1067N/A
1067N/Apublic class SetAccess {
1067N/A public static void main(String[] args) throws Exception {
1067N/A File d = new File(System.getProperty("test.dir", "."));
1067N/A
1067N/A File f = new File(d, "x.SetAccessPermission");
1067N/A if (f.exists() && !f.delete())
1067N/A throw new Exception("Can't delete test file: " + f);
1067N/A OutputStream o = new FileOutputStream(f);
1067N/A o.write('x');
1067N/A o.close();
1067N/A doTest(f);
1067N/A
1067N/A f = new File(d, "x.SetAccessPermission.dir");
1067N/A if (f.exists() && !f.delete())
1067N/A throw new Exception("Can't delete test dir: " + f);
1067N/A if (!f.mkdir())
1067N/A throw new Exception(f + ": Cannot create directory");
1067N/A doTest(f);
1067N/A }
1067N/A
1067N/A public static void doTest(File f) throws Exception {
1067N/A f.setReadOnly();
1067N/A if (!System.getProperty("os.name").startsWith("Windows")) {
1067N/A if (!f.setWritable(true, true) ||
1067N/A !f.canWrite() ||
1067N/A permission(f).charAt(2) != 'w')
1067N/A throw new Exception(f + ": setWritable(true, ture) Failed");
1067N/A if (!f.setWritable(false, true) ||
1067N/A f.canWrite() ||
1067N/A permission(f).charAt(2) != '-')
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(true, false) ||
1067N/A !f.canWrite() ||
1067N/A !permission(f).matches(".(.w.){3}"))
1067N/A throw new Exception(f + ": setWritable(true, false) Failed");
1067N/A if (!f.setWritable(false, false) ||
1067N/A f.canWrite() ||
1067N/A !permission(f).matches(".(.-.){3}"))
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(true) || !f.canWrite() ||
1067N/A permission(f).charAt(2) != 'w')
1067N/A throw new Exception(f + ": setWritable(true, ture) Failed");
1067N/A if (!f.setWritable(false) || f.canWrite() ||
1067N/A permission(f).charAt(2) != '-')
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setExecutable(true, true) ||
1067N/A !f.canExecute() ||
1067N/A permission(f).charAt(3) != 'x')
1067N/A throw new Exception(f + ": setExecutable(true, true) Failed");
1067N/A if (!f.setExecutable(false, true) ||
1067N/A f.canExecute() ||
1067N/A permission(f).charAt(3) != '-')
1067N/A throw new Exception(f + ": setExecutable(false, true) Failed");
1067N/A if (!f.setExecutable(true, false) ||
1067N/A !f.canExecute() ||
1067N/A !permission(f).matches(".(..x){3}"))
1067N/A throw new Exception(f + ": setExecutable(true, false) Failed");
1067N/A if (!f.setExecutable(false, false) ||
1067N/A f.canExecute() ||
1067N/A !permission(f).matches(".(..-){3}"))
1067N/A throw new Exception(f + ": setExecutable(false, false) Failed");
1067N/A if (!f.setExecutable(true) || !f.canExecute() ||
1067N/A permission(f).charAt(3) != 'x')
1067N/A throw new Exception(f + ": setExecutable(true, true) Failed");
1067N/A if (!f.setExecutable(false) || f.canExecute() ||
1067N/A permission(f).charAt(3) != '-')
1067N/A throw new Exception(f + ": setExecutable(false, true) Failed");
1067N/A if (!f.setReadable(true, true) ||
1067N/A !f.canRead() ||
1067N/A permission(f).charAt(1) != 'r')
1067N/A throw new Exception(f + ": setReadable(true, true) Failed");
1067N/A if (!f.setReadable(false, true) ||
1067N/A f.canRead() ||
1067N/A permission(f).charAt(1) != '-')
1067N/A throw new Exception(f + ": setReadable(false, true) Failed");
1067N/A if (!f.setReadable(true, false) ||
1067N/A !f.canRead() ||
1067N/A !permission(f).matches(".(r..){3}"))
1067N/A throw new Exception(f + ": setReadable(true, false) Failed");
1067N/A if (!f.setReadable(false, false) ||
1067N/A f.canRead() ||
1067N/A !permission(f).matches(".(-..){3}"))
1067N/A throw new Exception(f + ": setReadable(false, false) Failed");
1067N/A if (!f.setReadable(true) || !f.canRead() ||
1067N/A permission(f).charAt(1) != 'r')
1067N/A throw new Exception(f + ": setReadable(true, true) Failed");
1067N/A if (!f.setReadable(false) || f.canRead() ||
1067N/A permission(f).charAt(1) != '-')
1067N/A throw new Exception(f + ": setReadable(false, true) Failed");
1067N/A } else {
1067N/A //Windows platform
1067N/A if (!f.setWritable(true, true) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(true, ture) Failed");
1067N/A if (!f.setWritable(true, false) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(true, false) Failed");
1067N/A if (!f.setWritable(true) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(true, ture) Failed");
1067N/A if (!f.setExecutable(true, true) || !f.canExecute())
1067N/A throw new Exception(f + ": setExecutable(true, true) Failed");
1067N/A if (!f.setExecutable(true, false) || !f.canExecute())
1067N/A throw new Exception(f + ": setExecutable(true, false) Failed");
1067N/A if (!f.setExecutable(true) || !f.canExecute())
1067N/A throw new Exception(f + ": setExecutable(true, true) Failed");
1067N/A if (!f.setReadable(true, true) || !f.canRead())
1067N/A throw new Exception(f + ": setReadable(true, true) Failed");
1067N/A if (!f.setReadable(true, false) || !f.canRead())
1067N/A throw new Exception(f + ": setReadable(true, false) Failed");
1067N/A if (!f.setReadable(true) || !f.canRead())
1067N/A throw new Exception(f + ": setReadable(true, true) Failed");
1067N/A if (f.isDirectory()) {
1067N/A //All directories on Windows always have read&write access perm,
1067N/A //setting a directory to "unwritable" actually means "not deletable"
1067N/A if (!f.setWritable(false, true) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(false, false) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(false) || !f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A } else {
1067N/A if (!f.setWritable(false, true) || f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(false, false) || f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A if (!f.setWritable(false) || f.canWrite())
1067N/A throw new Exception(f + ": setWritable(false, true) Failed");
1067N/A }
1067N/A if (f.setExecutable(false, true))
1067N/A throw new Exception(f + ": setExecutable(false, true) Failed");
1067N/A if (f.setExecutable(false, false))
1067N/A throw new Exception(f + ": setExecutable(false, false) Failed");
1067N/A if (f.setExecutable(false))
1067N/A throw new Exception(f + ": setExecutable(false, true) Failed");
1067N/A if (f.setReadable(false, true))
1067N/A throw new Exception(f + ": setReadable(false, true) Failed");
1067N/A if (f.setReadable(false, false))
1067N/A throw new Exception(f + ": setReadable(false, false) Failed");
1067N/A if (f.setReadable(false))
1067N/A throw new Exception(f + ": setReadable(false, true) Failed");
1067N/A }
1067N/A if (f.exists() && !f.delete())
1067N/A throw new Exception("Can't delete test dir: " + f);
1067N/A }
1067N/A
1067N/A private static String permission(File f) throws Exception {
1067N/A byte[] bb = new byte[1024];
1067N/A String command = f.isDirectory()?"ls -dl ":"ls -l ";
1067N/A int len = Runtime.getRuntime()
1067N/A .exec(command + f.getPath())
1067N/A .getInputStream()
1067N/A .read(bb, 0, 1024);
1067N/A if (len > 0)
1067N/A return new String(bb, 0, len).substring(0, 10);
1067N/A return "";
1067N/A }
1067N/A}
1067N/A