< prev index next >

test/jdk/java/io/File/GetXSpace.java

Print this page




   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 /**
  25  * @test
  26  * @bug 4057701 6286712 6364377
  27  * @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")
  28  * @run build GetXSpace
  29  * @run shell GetXSpace.sh
  30  * @summary Basic functionality of File.get-X-Space methods.
  31  * @key randomness
  32  */
  33 
  34 import java.io.BufferedReader;
  35 import java.io.File;
  36 import java.io.FilePermission;
  37 import java.io.InputStreamReader;
  38 import java.io.IOException;
  39 import java.nio.file.Files;
  40 import java.nio.file.FileStore;

  41 import java.security.Permission;
  42 import java.util.ArrayList;
  43 import java.util.regex.Matcher;
  44 import java.util.regex.Pattern;
  45 

  46 import static java.lang.System.out;
  47 
  48 public class GetXSpace {
  49 
  50     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
  51                                               new DenyRead() };
  52 
  53     private static final String OS_NAME = System.getProperty("os.name");
  54     private static final boolean IS_MAC = OS_NAME.startsWith("Mac");
  55     private static final boolean IS_WIN = OS_NAME.startsWith("Windows");
  56 
  57     // FileSystem Total Used Available Use% MountedOn
  58     private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
  59 
  60     private static int fail = 0;
  61     private static int pass = 0;
  62     private static Throwable first;
  63 






  64     static void pass() {
  65         pass++;
  66     }
  67 
  68     static void fail(String p) {
  69         setFirst(p);
  70         System.err.format("FAILED: %s%n", p);
  71         fail++;
  72     }
  73 
  74     static void fail(String p, long exp, String cmp, long got) {
  75         String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
  76         setFirst(s);
  77         System.err.format("FAILED: %s%n", s);
  78         fail++;
  79     }
  80 
  81     private static void fail(String p, Class ex) {
  82         String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
  83         setFirst(s);


 321             if (p.implies(new RuntimePermission("getFileSystemAttributes")))
 322                 throw new SecurityException(err);
 323             super.checkPermission(p);
 324         }
 325 
 326         public void checkPermission(Permission p, Object context) {
 327             if (p.implies(new RuntimePermission("getFileSystemAttributes")))
 328                 throw new SecurityException(err);
 329             super.checkPermission(p, context);
 330         }
 331     }
 332 
 333     private static class DenyRead extends Deny {
 334         private String err = "sorry - checkRead()";
 335 
 336         public void checkRead(String file) {
 337             throw new SecurityException(err);
 338         }
 339     }
 340 
 341     private static void testFile(String dirName) {

 342         out.format("--- Testing %s%n", dirName);
 343         ArrayList<Space> l;
 344         try {
 345             l = space(dirName);
 346         } catch (IOException x) {
 347             throw new RuntimeException(dirName + " can't get file system information", x);
 348         }
 349         compare(l.get(0));






 350     }
 351 
 352     private static void testDF() {



 353         out.println("--- Testing df");
 354         // Find all of the partitions on the machine and verify that the size
 355         // returned by "df" is equivalent to File.getXSpace() values.
 356         ArrayList<Space> l;
 357         try {
 358             l = space(null);
 359         } catch (IOException x) {
 360             throw new RuntimeException("can't get file system information", x);
 361         }
 362         if (l.size() == 0)
 363             throw new RuntimeException("no partitions?");
 364 
 365         for (int i = 0; i < sma.length; i++) {
 366             System.setSecurityManager(sma[i]);
 367             SecurityManager sm = System.getSecurityManager();
 368             if (sma[i] != null && sm == null)
 369                 throw new RuntimeException("Test configuration error "
 370                                            + " - can't set security manager");
 371 
 372             out.format("%nSecurityManager = %s%n" ,
 373                        (sm == null ? "null" : sm.getClass().getName()));
 374             for (var s : l) {
 375                 if (sm instanceof Deny) {
 376                     tryCatch(s);
 377                 } else {
 378                     compare(s);
 379                     compareZeroNonExist();
 380                     compareZeroExist();
 381                 }
 382             }
 383         }
 384     }
 385 
 386     public static void main(String [] args) {
 387         if (args.length > 0) {
 388             testFile(args[0]);
 389         } else {
 390             testDF();
 391         }
 392 
 393         if (fail != 0) {
 394             throw new RuntimeException((fail + pass) + " tests: "
 395                                        + fail + " failure(s), first", first);
 396         } else {
 397             out.format("all %d tests passed%n", fail + pass);
 398         }





































 399     }
 400 }


   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 /**
  25  * @test
  26  * @bug 4057701 6286712 6364377
  27  * @requires (os.family == "linux" | os.family == "mac" |
  28  *            os.family == "windows")

  29  * @summary Basic functionality of File.get-X-Space methods.

  30  */
  31 
  32 import java.io.BufferedReader;
  33 import java.io.File;
  34 import java.io.FilePermission;
  35 import java.io.InputStreamReader;
  36 import java.io.IOException;
  37 import java.nio.file.Files;
  38 import java.nio.file.FileStore;
  39 import java.nio.file.Path;
  40 import java.security.Permission;
  41 import java.util.ArrayList;
  42 import java.util.regex.Matcher;
  43 import java.util.regex.Pattern;
  44 
  45 import static java.lang.System.err;
  46 import static java.lang.System.out;
  47 
  48 public class GetXSpace {
  49 
  50     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
  51                                               new DenyRead() };
  52 
  53     private static final String OS_NAME = System.getProperty("os.name");
  54     private static final boolean IS_MAC = OS_NAME.startsWith("Mac");
  55     private static final boolean IS_WIN = OS_NAME.startsWith("Windows");
  56 
  57     // FileSystem Total Used Available Use% MountedOn
  58     private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
  59 
  60     private static int fail = 0;
  61     private static int pass = 0;
  62     private static Throwable first;
  63 
  64     static void reset() {
  65         fail = 0;
  66         pass = 0;
  67         first = null;
  68     }
  69 
  70     static void pass() {
  71         pass++;
  72     }
  73 
  74     static void fail(String p) {
  75         setFirst(p);
  76         System.err.format("FAILED: %s%n", p);
  77         fail++;
  78     }
  79 
  80     static void fail(String p, long exp, String cmp, long got) {
  81         String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
  82         setFirst(s);
  83         System.err.format("FAILED: %s%n", s);
  84         fail++;
  85     }
  86 
  87     private static void fail(String p, Class ex) {
  88         String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
  89         setFirst(s);


 327             if (p.implies(new RuntimePermission("getFileSystemAttributes")))
 328                 throw new SecurityException(err);
 329             super.checkPermission(p);
 330         }
 331 
 332         public void checkPermission(Permission p, Object context) {
 333             if (p.implies(new RuntimePermission("getFileSystemAttributes")))
 334                 throw new SecurityException(err);
 335             super.checkPermission(p, context);
 336         }
 337     }
 338 
 339     private static class DenyRead extends Deny {
 340         private String err = "sorry - checkRead()";
 341 
 342         public void checkRead(String file) {
 343             throw new SecurityException(err);
 344         }
 345     }
 346 
 347     private static int testFile(Path dir) {
 348         String dirName = dir.toString();
 349         out.format("--- Testing %s%n", dirName);
 350         ArrayList<Space> l;
 351         try {
 352             l = space(dirName);
 353         } catch (IOException x) {
 354             throw new RuntimeException(dirName + " can't get file system information", x);
 355         }
 356         compare(l.get(0));
 357 
 358         if (fail != 0) {
 359             err.format("%d tests: %d failure(s); first: %s%n",
 360                 fail + pass, fail, first);
 361         } else {
 362             out.format("all %d tests passed%n", fail + pass);
 363         }
 364 
 365         return fail != 0 ? 1 : 0;
 366     }
 367 
 368     private static int testDF() {
 369         out.println("--- Testing df");
 370         // Find all of the partitions on the machine and verify that the size
 371         // returned by "df" is equivalent to File.getXSpace() values.
 372         ArrayList<Space> l;
 373         try {
 374             l = space(null);
 375         } catch (IOException x) {
 376             throw new RuntimeException("can't get file system information", x);
 377         }
 378         if (l.size() == 0)
 379             throw new RuntimeException("no partitions?");
 380 
 381         for (int i = 0; i < sma.length; i++) {
 382             System.setSecurityManager(sma[i]);
 383             SecurityManager sm = System.getSecurityManager();
 384             if (sma[i] != null && sm == null)
 385                 throw new RuntimeException("Test configuration error "
 386                                            + " - can't set security manager");
 387 
 388             out.format("%nSecurityManager = %s%n" ,
 389                        (sm == null ? "null" : sm.getClass().getName()));
 390             for (var s : l) {
 391                 if (sm instanceof Deny) {
 392                     tryCatch(s);
 393                 } else {
 394                     compare(s);
 395                     compareZeroNonExist();
 396                     compareZeroExist();
 397                 }
 398             }
 399         }

 400 
 401         System.setSecurityManager(null);





 402 
 403         if (fail != 0) {
 404             err.format("%d tests: %d failure(s); first: %s%n",
 405                 fail + pass, fail, first);
 406         } else {
 407             out.format("all %d tests passed%n", fail + pass);
 408         }
 409 
 410         return fail != 0 ? 1 : 0;
 411     }
 412 
 413     private static void perms(File file, boolean allow) throws IOException {
 414         file.setExecutable(allow, false);
 415         file.setReadable(allow, false);
 416         file.setWritable(allow, false);
 417     }
 418 
 419     private static void deny(Path path) throws IOException {
 420         perms(path.toFile(), false);
 421     }
 422 
 423     private static void allow(Path path) throws IOException {
 424         perms(path.toFile(), true);
 425     }
 426 
 427     public static void main(String[] args) throws Exception {
 428         int failedTests = testDF();
 429         reset();
 430 
 431         Path tmpDir = Files.createTempDirectory(null);
 432         Path tmpSubdir = Files.createTempDirectory(tmpDir, null);
 433         Path tmpFile = Files.createTempFile(tmpSubdir, "foo", null);
 434 
 435         deny(tmpSubdir);
 436         failedTests += testFile(tmpFile);
 437 
 438         allow(tmpSubdir);
 439         Files.delete(tmpFile);
 440         Files.delete(tmpSubdir);
 441         Files.delete(tmpDir);
 442 
 443         if (failedTests > 0) {
 444             throw new RuntimeException(failedTests + " test(s) failed");
 445         }
 446     }
 447 }
< prev index next >