jdk/test/java/io/File/SetAccess.java
changeset 6884 65b1fa0a0fae
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
6883:bbd0cba11983 6884:65b1fa0a0fae
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25    @bug 4167472 5097703 6216563 6284003
    25    @bug 4167472 5097703 6216563 6284003 6728842 6464744
    26    @summary Basic test for setWritable/Readable/Executable methods
    26    @summary Basic test for setWritable/Readable/Executable methods
    27  */
    27  */
    28 
    28 
    29 import java.io.*;
    29 import java.io.*;
       
    30 import java.nio.file.attribute.*;
    30 
    31 
    31 public class SetAccess {
    32 public class SetAccess {
    32     public static void main(String[] args) throws Exception {
    33     public static void main(String[] args) throws Exception {
    33         File d = new File(System.getProperty("test.dir", "."));
    34         File d = new File(System.getProperty("test.dir", "."));
    34 
    35 
    47             throw new Exception(f + ": Cannot create directory");
    48             throw new Exception(f + ": Cannot create directory");
    48         doTest(f);
    49         doTest(f);
    49     }
    50     }
    50 
    51 
    51     public static void doTest(File f) throws Exception {
    52     public static void doTest(File f) throws Exception {
    52         f.setReadOnly();
       
    53         if (!System.getProperty("os.name").startsWith("Windows")) {
    53         if (!System.getProperty("os.name").startsWith("Windows")) {
       
    54             if (!f.setReadOnly())
       
    55                  throw new Exception(f + ": setReadOnly Failed");
    54             if (!f.setWritable(true, true) ||
    56             if (!f.setWritable(true, true) ||
    55                 !f.canWrite() ||
    57                 !f.canWrite() ||
    56                 permission(f).charAt(2) != 'w')
    58                 permission(f).charAt(2) != 'w')
    57                 throw new Exception(f + ": setWritable(true, ture) Failed");
    59                 throw new Exception(f + ": setWritable(true, ture) Failed");
    58             if (!f.setWritable(false, true) ||
    60             if (!f.setWritable(false, true) ||
   117             if (!f.setReadable(false) || f.canRead() ||
   119             if (!f.setReadable(false) || f.canRead() ||
   118                 permission(f).charAt(1) != '-')
   120                 permission(f).charAt(1) != '-')
   119                 throw new Exception(f + ": setReadable(false, true) Failed");
   121                 throw new Exception(f + ": setReadable(false, true) Failed");
   120         } else {
   122         } else {
   121             //Windows platform
   123             //Windows platform
   122             if (!f.setWritable(true, true) || !f.canWrite())
   124             if (f.isFile()) {
   123                 throw new Exception(f + ": setWritable(true, ture) Failed");
   125                 if (!f.setReadOnly())
   124             if (!f.setWritable(true, false) || !f.canWrite())
   126                     throw new Exception(f + ": setReadOnly Failed");
   125                 throw new Exception(f + ": setWritable(true, false) Failed");
   127                 if (!f.setWritable(true, true) || !f.canWrite())
   126             if (!f.setWritable(true) || !f.canWrite())
   128                     throw new Exception(f + ": setWritable(true, ture) Failed");
   127                 throw new Exception(f + ": setWritable(true, ture) Failed");
   129                 if (!f.setWritable(true, false) || !f.canWrite())
   128             if (!f.setExecutable(true, true) || !f.canExecute())
   130                     throw new Exception(f + ": setWritable(true, false) Failed");
   129                 throw new Exception(f + ": setExecutable(true, true) Failed");
   131                 if (!f.setWritable(true) || !f.canWrite())
   130             if (!f.setExecutable(true, false) || !f.canExecute())
   132                     throw new Exception(f + ": setWritable(true, ture) Failed");
   131                 throw new Exception(f + ": setExecutable(true, false) Failed");
   133                 if (!f.setExecutable(true, true) || !f.canExecute())
   132             if (!f.setExecutable(true) || !f.canExecute())
   134                     throw new Exception(f + ": setExecutable(true, true) Failed");
   133                 throw new Exception(f + ": setExecutable(true, true) Failed");
   135                 if (!f.setExecutable(true, false) || !f.canExecute())
   134             if (!f.setReadable(true, true) || !f.canRead())
   136                     throw new Exception(f + ": setExecutable(true, false) Failed");
   135                 throw new Exception(f + ": setReadable(true, true) Failed");
   137                 if (!f.setExecutable(true) || !f.canExecute())
   136             if (!f.setReadable(true, false) || !f.canRead())
   138                     throw new Exception(f + ": setExecutable(true, true) Failed");
   137                 throw new Exception(f + ": setReadable(true, false) Failed");
   139                 if (!f.setReadable(true, true) || !f.canRead())
   138             if (!f.setReadable(true) || !f.canRead())
   140                     throw new Exception(f + ": setReadable(true, true) Failed");
   139                 throw new Exception(f + ": setReadable(true, true) Failed");
   141                 if (!f.setReadable(true, false) || !f.canRead())
       
   142                     throw new Exception(f + ": setReadable(true, false) Failed");
       
   143                 if (!f.setReadable(true) || !f.canRead())
       
   144                     throw new Exception(f + ": setReadable(true, true) Failed");
       
   145             }
   140             if (f.isDirectory()) {
   146             if (f.isDirectory()) {
   141                 //All directories on Windows always have read&write access perm,
   147                 // setWritable should fail on directories because the DOS readonly
   142                 //setting a directory to "unwritable" actually means "not deletable"
   148                 // attribute prevents a directory from being deleted.
   143                 if (!f.setWritable(false, true) || !f.canWrite())
   149                 if (f.setWritable(false, true))
   144                     throw new Exception(f + ": setWritable(false, true) Failed");
   150                     throw new Exception(f + ": setWritable(false, true) Succeeded");
   145                 if (!f.setWritable(false, false) || !f.canWrite())
   151                 if (f.setWritable(false, false))
   146                     throw new Exception(f + ": setWritable(false, true) Failed");
   152                     throw new Exception(f + ": setWritable(false, false) Succeeded");
   147                 if (!f.setWritable(false) || !f.canWrite())
   153                 if (f.setWritable(false))
   148                     throw new Exception(f + ": setWritable(false, true) Failed");
   154                     throw new Exception(f + ": setWritable(false) Succeeded");
   149             } else {
   155             } else {
   150                 if (!f.setWritable(false, true) || f.canWrite())
   156                 if (!f.setWritable(false, true) || f.canWrite())
   151                     throw new Exception(f + ": setWritable(false, true) Failed");
   157                     throw new Exception(f + ": setWritable(false, true) Failed");
   152                 if (!f.setWritable(false, false) || f.canWrite())
   158                 if (!f.setWritable(false, false) || f.canWrite())
   153                     throw new Exception(f + ": setWritable(false, true) Failed");
   159                     throw new Exception(f + ": setWritable(false, false) Failed");
   154                 if (!f.setWritable(false) || f.canWrite())
   160                 if (!f.setWritable(false) || f.canWrite())
   155                     throw new Exception(f + ": setWritable(false, true) Failed");
   161                     throw new Exception(f + ": setWritable(false) Failed");
   156             }
   162             }
   157             if (f.setExecutable(false, true))
   163             if (f.setExecutable(false, true))
   158                 throw new Exception(f + ": setExecutable(false, true) Failed");
   164                 throw new Exception(f + ": setExecutable(false, true) Failed");
   159             if (f.setExecutable(false, false))
   165             if (f.setExecutable(false, false))
   160                 throw new Exception(f + ": setExecutable(false, false) Failed");
   166                 throw new Exception(f + ": setExecutable(false, false) Failed");
   170         if (f.exists() && !f.delete())
   176         if (f.exists() && !f.delete())
   171             throw new Exception("Can't delete test dir: " + f);
   177             throw new Exception("Can't delete test dir: " + f);
   172     }
   178     }
   173 
   179 
   174     private static String permission(File f) throws Exception {
   180     private static String permission(File f) throws Exception {
   175         byte[] bb = new byte[1024];
   181         PosixFileAttributes attrs = Attributes.readPosixFileAttributes(f.toPath());
   176         String command = f.isDirectory()?"ls -dl ":"ls -l ";
   182         String type = attrs.isDirectory() ? "d" : " ";
   177         int len = Runtime.getRuntime()
   183         return type + PosixFilePermissions.toString(attrs.permissions());
   178                          .exec(command + f.getPath())
       
   179                          .getInputStream()
       
   180                          .read(bb, 0, 1024);
       
   181         if (len > 0)
       
   182             return new String(bb, 0, len).substring(0, 10);
       
   183         return "";
       
   184     }
   184     }
   185 }
   185 }