test/java/io/File/SetAccess.java

Print this page




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4167472 5097703 6216563 6284003 6728842 6464744
  26    @summary Basic test for setWritable/Readable/Executable methods
  27  */
  28 
  29 import java.io.*;

  30 import java.nio.file.attribute.*;
  31 
  32 public class SetAccess {
  33     public static void main(String[] args) throws Exception {
  34         File d = new File(System.getProperty("test.dir", "."));
  35 
  36         File f = new File(d, "x.SetAccessPermission");
  37         if (f.exists() && !f.delete())
  38             throw new Exception("Can't delete test file: " + f);
  39         OutputStream o = new FileOutputStream(f);
  40         o.write('x');
  41         o.close();
  42         doTest(f);
  43 
  44         f = new File(d, "x.SetAccessPermission.dir");
  45         if (f.exists() && !f.delete())
  46             throw new Exception("Can't delete test dir: " + f);
  47         if (!f.mkdir())
  48             throw new Exception(f + ": Cannot create directory");
  49         doTest(f);


 161                     throw new Exception(f + ": setWritable(false) Failed");
 162             }
 163             if (f.setExecutable(false, true))
 164                 throw new Exception(f + ": setExecutable(false, true) Failed");
 165             if (f.setExecutable(false, false))
 166                 throw new Exception(f + ": setExecutable(false, false) Failed");
 167             if (f.setExecutable(false))
 168                 throw new Exception(f + ": setExecutable(false, true) Failed");
 169             if (f.setReadable(false, true))
 170                 throw new Exception(f + ": setReadable(false, true) Failed");
 171             if (f.setReadable(false, false))
 172                 throw new Exception(f + ": setReadable(false, false) Failed");
 173             if (f.setReadable(false))
 174                 throw new Exception(f + ": setReadable(false, true) Failed");
 175         }
 176         if (f.exists() && !f.delete())
 177             throw new Exception("Can't delete test dir: " + f);
 178     }
 179 
 180     private static String permission(File f) throws Exception {
 181         PosixFileAttributes attrs = Attributes.readPosixFileAttributes(f.toPath());
 182         String type = attrs.isDirectory() ? "d" : " ";
 183         return type + PosixFilePermissions.toString(attrs.permissions());
 184     }
 185 }


  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4167472 5097703 6216563 6284003 6728842 6464744
  26    @summary Basic test for setWritable/Readable/Executable methods
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.file.*;
  31 import java.nio.file.attribute.*;
  32 
  33 public class SetAccess {
  34     public static void main(String[] args) throws Exception {
  35         File d = new File(System.getProperty("test.dir", "."));
  36 
  37         File f = new File(d, "x.SetAccessPermission");
  38         if (f.exists() && !f.delete())
  39             throw new Exception("Can't delete test file: " + f);
  40         OutputStream o = new FileOutputStream(f);
  41         o.write('x');
  42         o.close();
  43         doTest(f);
  44 
  45         f = new File(d, "x.SetAccessPermission.dir");
  46         if (f.exists() && !f.delete())
  47             throw new Exception("Can't delete test dir: " + f);
  48         if (!f.mkdir())
  49             throw new Exception(f + ": Cannot create directory");
  50         doTest(f);


 162                     throw new Exception(f + ": setWritable(false) Failed");
 163             }
 164             if (f.setExecutable(false, true))
 165                 throw new Exception(f + ": setExecutable(false, true) Failed");
 166             if (f.setExecutable(false, false))
 167                 throw new Exception(f + ": setExecutable(false, false) Failed");
 168             if (f.setExecutable(false))
 169                 throw new Exception(f + ": setExecutable(false, true) Failed");
 170             if (f.setReadable(false, true))
 171                 throw new Exception(f + ": setReadable(false, true) Failed");
 172             if (f.setReadable(false, false))
 173                 throw new Exception(f + ": setReadable(false, false) Failed");
 174             if (f.setReadable(false))
 175                 throw new Exception(f + ": setReadable(false, true) Failed");
 176         }
 177         if (f.exists() && !f.delete())
 178             throw new Exception("Can't delete test dir: " + f);
 179     }
 180 
 181     private static String permission(File f) throws Exception {
 182         PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class);
 183         String type = attrs.isDirectory() ? "d" : " ";
 184         return type + PosixFilePermissions.toString(attrs.permissions());
 185     }
 186 }