Path: blob/master/test/jdk/java/io/File/SetAccess.java
41149 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4167472 5097703 6216563 6284003 6728842 646474425@summary Basic test for setWritable/Readable/Executable methods26@build SetAccess Util27@run main SetAccess28*/2930import java.io.*;31import java.nio.file.*;32import java.nio.file.attribute.*;3334public class SetAccess {35public static void main(String[] args) throws Exception {36if (Util.isPrivileged()) {37System.out.println("Unable to test file permissions when running with privileges");38return;39}4041File d = new File(System.getProperty("test.dir", "."));4243File f = new File(d, "x.SetAccessPermission");44if (f.exists() && !f.delete())45throw new Exception("Can't delete test file: " + f);46OutputStream o = new FileOutputStream(f);47o.write('x');48o.close();49doTest(f);5051f = new File(d, "x.SetAccessPermission.dir");52if (f.exists() && !f.delete())53throw new Exception("Can't delete test dir: " + f);54if (!f.mkdir())55throw new Exception(f + ": Cannot create directory");56doTest(f);57}5859public static void doTest(File f) throws Exception {60if (!System.getProperty("os.name").startsWith("Windows")) {61if (!f.setReadOnly())62throw new Exception(f + ": setReadOnly Failed");63if (!f.setWritable(true, true) ||64!f.canWrite() ||65permission(f).charAt(2) != 'w')66throw new Exception(f + ": setWritable(true, ture) Failed");67if (!f.setWritable(false, true) ||68f.canWrite() ||69permission(f).charAt(2) != '-')70throw new Exception(f + ": setWritable(false, true) Failed");71if (!f.setWritable(true, false) ||72!f.canWrite() ||73!permission(f).matches(".(.w.){3}"))74throw new Exception(f + ": setWritable(true, false) Failed");75if (!f.setWritable(false, false) ||76f.canWrite() ||77!permission(f).matches(".(.-.){3}"))78throw new Exception(f + ": setWritable(false, true) Failed");79if (!f.setWritable(true) || !f.canWrite() ||80permission(f).charAt(2) != 'w')81throw new Exception(f + ": setWritable(true, ture) Failed");82if (!f.setWritable(false) || f.canWrite() ||83permission(f).charAt(2) != '-')84throw new Exception(f + ": setWritable(false, true) Failed");85if (!f.setExecutable(true, true) ||86!f.canExecute() ||87permission(f).charAt(3) != 'x')88throw new Exception(f + ": setExecutable(true, true) Failed");89if (!f.setExecutable(false, true) ||90f.canExecute() ||91permission(f).charAt(3) != '-')92throw new Exception(f + ": setExecutable(false, true) Failed");93if (!f.setExecutable(true, false) ||94!f.canExecute() ||95!permission(f).matches(".(..x){3}"))96throw new Exception(f + ": setExecutable(true, false) Failed");97if (!f.setExecutable(false, false) ||98f.canExecute() ||99!permission(f).matches(".(..-){3}"))100throw new Exception(f + ": setExecutable(false, false) Failed");101if (!f.setExecutable(true) || !f.canExecute() ||102permission(f).charAt(3) != 'x')103throw new Exception(f + ": setExecutable(true, true) Failed");104if (!f.setExecutable(false) || f.canExecute() ||105permission(f).charAt(3) != '-')106throw new Exception(f + ": setExecutable(false, true) Failed");107if (!f.setReadable(true, true) ||108!f.canRead() ||109permission(f).charAt(1) != 'r')110throw new Exception(f + ": setReadable(true, true) Failed");111if (!f.setReadable(false, true) ||112f.canRead() ||113permission(f).charAt(1) != '-')114throw new Exception(f + ": setReadable(false, true) Failed");115if (!f.setReadable(true, false) ||116!f.canRead() ||117!permission(f).matches(".(r..){3}"))118throw new Exception(f + ": setReadable(true, false) Failed");119if (!f.setReadable(false, false) ||120f.canRead() ||121!permission(f).matches(".(-..){3}"))122throw new Exception(f + ": setReadable(false, false) Failed");123if (!f.setReadable(true) || !f.canRead() ||124permission(f).charAt(1) != 'r')125throw new Exception(f + ": setReadable(true, true) Failed");126if (!f.setReadable(false) || f.canRead() ||127permission(f).charAt(1) != '-')128throw new Exception(f + ": setReadable(false, true) Failed");129} else {130//Windows platform131if (f.isFile()) {132if (!f.setReadOnly())133throw new Exception(f + ": setReadOnly Failed");134if (!f.setWritable(true, true) || !f.canWrite())135throw new Exception(f + ": setWritable(true, ture) Failed");136if (!f.setWritable(true, false) || !f.canWrite())137throw new Exception(f + ": setWritable(true, false) Failed");138if (!f.setWritable(true) || !f.canWrite())139throw new Exception(f + ": setWritable(true, ture) Failed");140if (!f.setExecutable(true, true) || !f.canExecute())141throw new Exception(f + ": setExecutable(true, true) Failed");142if (!f.setExecutable(true, false) || !f.canExecute())143throw new Exception(f + ": setExecutable(true, false) Failed");144if (!f.setExecutable(true) || !f.canExecute())145throw new Exception(f + ": setExecutable(true, true) Failed");146if (!f.setReadable(true, true) || !f.canRead())147throw new Exception(f + ": setReadable(true, true) Failed");148if (!f.setReadable(true, false) || !f.canRead())149throw new Exception(f + ": setReadable(true, false) Failed");150if (!f.setReadable(true) || !f.canRead())151throw new Exception(f + ": setReadable(true, true) Failed");152}153if (f.isDirectory()) {154// setWritable should fail on directories because the DOS readonly155// attribute prevents a directory from being deleted.156if (f.setWritable(false, true))157throw new Exception(f + ": setWritable(false, true) Succeeded");158if (f.setWritable(false, false))159throw new Exception(f + ": setWritable(false, false) Succeeded");160if (f.setWritable(false))161throw new Exception(f + ": setWritable(false) Succeeded");162} else {163if (!f.setWritable(false, true) || f.canWrite())164throw new Exception(f + ": setWritable(false, true) Failed");165if (!f.setWritable(false, false) || f.canWrite())166throw new Exception(f + ": setWritable(false, false) Failed");167if (!f.setWritable(false) || f.canWrite())168throw new Exception(f + ": setWritable(false) Failed");169}170if (f.setExecutable(false, true))171throw new Exception(f + ": setExecutable(false, true) Failed");172if (f.setExecutable(false, false))173throw new Exception(f + ": setExecutable(false, false) Failed");174if (f.setExecutable(false))175throw new Exception(f + ": setExecutable(false, true) Failed");176if (f.setReadable(false, true))177throw new Exception(f + ": setReadable(false, true) Failed");178if (f.setReadable(false, false))179throw new Exception(f + ": setReadable(false, false) Failed");180if (f.setReadable(false))181throw new Exception(f + ": setReadable(false, true) Failed");182}183if (f.exists() && !f.delete())184throw new Exception("Can't delete test dir: " + f);185}186187private static String permission(File f) throws Exception {188PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class);189String type = attrs.isDirectory() ? "d" : " ";190return type + PosixFilePermissions.toString(attrs.permissions());191}192}193194195