1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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);
  51     }
  52 
  53     public static void doTest(File f) throws Exception {
  54         if (!System.getProperty("os.name").startsWith("Windows")) {
  55             if (!f.setReadOnly())
  56                  throw new Exception(f + ": setReadOnly Failed");
  57             if (!f.setWritable(true, true) ||
  58                 !f.canWrite() ||
  59                 permission(f).charAt(2) != 'w')
  60                 throw new Exception(f + ": setWritable(true, ture) Failed");
  61             if (!f.setWritable(false, true) ||
  62                 f.canWrite() ||
  63                 permission(f).charAt(2) != '-')
  64                 throw new Exception(f + ": setWritable(false, true) Failed");
  65             if (!f.setWritable(true, false) ||
  66                 !f.canWrite() ||
  67                 !permission(f).matches(".(.w.){3}"))
  68                 throw new Exception(f + ": setWritable(true, false) Failed");
  69             if (!f.setWritable(false, false) ||
  70                 f.canWrite() ||
  71                 !permission(f).matches(".(.-.){3}"))
  72                 throw new Exception(f + ": setWritable(false, true) Failed");
  73             if (!f.setWritable(true) || !f.canWrite() ||
  74                 permission(f).charAt(2) != 'w')
  75                 throw new Exception(f + ": setWritable(true, ture) Failed");
  76             if (!f.setWritable(false) || f.canWrite() ||
  77                 permission(f).charAt(2) != '-')
  78                 throw new Exception(f + ": setWritable(false, true) Failed");
  79             if (!f.setExecutable(true, true) ||
  80                 !f.canExecute() ||
  81                 permission(f).charAt(3) != 'x')
  82                 throw new Exception(f + ": setExecutable(true, true) Failed");
  83             if (!f.setExecutable(false, true) ||
  84                 f.canExecute() ||
  85                 permission(f).charAt(3) != '-')
  86                 throw new Exception(f + ": setExecutable(false, true) Failed");
  87             if (!f.setExecutable(true, false) ||
  88                 !f.canExecute() ||
  89                 !permission(f).matches(".(..x){3}"))
  90                 throw new Exception(f + ": setExecutable(true, false) Failed");
  91             if (!f.setExecutable(false, false) ||
  92                 f.canExecute() ||
  93                 !permission(f).matches(".(..-){3}"))
  94                 throw new Exception(f + ": setExecutable(false, false) Failed");
  95             if (!f.setExecutable(true) || !f.canExecute() ||
  96                 permission(f).charAt(3) != 'x')
  97                 throw new Exception(f + ": setExecutable(true, true) Failed");
  98             if (!f.setExecutable(false) || f.canExecute() ||
  99                 permission(f).charAt(3) != '-')
 100                 throw new Exception(f + ": setExecutable(false, true) Failed");
 101             if (!f.setReadable(true, true) ||
 102                 !f.canRead() ||
 103                 permission(f).charAt(1) != 'r')
 104                 throw new Exception(f + ": setReadable(true, true) Failed");
 105             if (!f.setReadable(false, true) ||
 106                 f.canRead() ||
 107                 permission(f).charAt(1) != '-')
 108                 throw new Exception(f + ": setReadable(false, true) Failed");
 109             if (!f.setReadable(true, false) ||
 110                 !f.canRead() ||
 111                 !permission(f).matches(".(r..){3}"))
 112                 throw new Exception(f + ": setReadable(true, false) Failed");
 113             if (!f.setReadable(false, false) ||
 114                 f.canRead() ||
 115                 !permission(f).matches(".(-..){3}"))
 116                 throw new Exception(f + ": setReadable(false, false) Failed");
 117             if (!f.setReadable(true) || !f.canRead() ||
 118                 permission(f).charAt(1) != 'r')
 119                 throw new Exception(f + ": setReadable(true, true) Failed");
 120             if (!f.setReadable(false) || f.canRead() ||
 121                 permission(f).charAt(1) != '-')
 122                 throw new Exception(f + ": setReadable(false, true) Failed");
 123         } else {
 124             //Windows platform
 125             if (f.isFile()) {
 126                 if (!f.setReadOnly())
 127                     throw new Exception(f + ": setReadOnly Failed");
 128                 if (!f.setWritable(true, true) || !f.canWrite())
 129                     throw new Exception(f + ": setWritable(true, ture) Failed");
 130                 if (!f.setWritable(true, false) || !f.canWrite())
 131                     throw new Exception(f + ": setWritable(true, false) Failed");
 132                 if (!f.setWritable(true) || !f.canWrite())
 133                     throw new Exception(f + ": setWritable(true, ture) Failed");
 134                 if (!f.setExecutable(true, true) || !f.canExecute())
 135                     throw new Exception(f + ": setExecutable(true, true) Failed");
 136                 if (!f.setExecutable(true, false) || !f.canExecute())
 137                     throw new Exception(f + ": setExecutable(true, false) Failed");
 138                 if (!f.setExecutable(true) || !f.canExecute())
 139                     throw new Exception(f + ": setExecutable(true, true) Failed");
 140                 if (!f.setReadable(true, true) || !f.canRead())
 141                     throw new Exception(f + ": setReadable(true, true) Failed");
 142                 if (!f.setReadable(true, false) || !f.canRead())
 143                     throw new Exception(f + ": setReadable(true, false) Failed");
 144                 if (!f.setReadable(true) || !f.canRead())
 145                     throw new Exception(f + ": setReadable(true, true) Failed");
 146             }
 147             if (f.isDirectory()) {
 148                 // setWritable should fail on directories because the DOS readonly
 149                 // attribute prevents a directory from being deleted.
 150                 if (f.setWritable(false, true))
 151                     throw new Exception(f + ": setWritable(false, true) Succeeded");
 152                 if (f.setWritable(false, false))
 153                     throw new Exception(f + ": setWritable(false, false) Succeeded");
 154                 if (f.setWritable(false))
 155                     throw new Exception(f + ": setWritable(false) Succeeded");
 156             } else {
 157                 if (!f.setWritable(false, true) || f.canWrite())
 158                     throw new Exception(f + ": setWritable(false, true) Failed");
 159                 if (!f.setWritable(false, false) || f.canWrite())
 160                     throw new Exception(f + ": setWritable(false, false) Failed");
 161                 if (!f.setWritable(false) || f.canWrite())
 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 }