test/java/nio/file/Files/walkFileTree/WalkWithSecurity.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 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  */


  30  * @run main/othervm WalkWithSecurity grantTopOnly.policy top_only
  31  */
  32 
  33 import java.nio.file.*;
  34 import java.nio.file.attribute.BasicFileAttributes;
  35 import java.io.IOException;
  36 
  37 public class WalkWithSecurity {
  38 
  39     public static void main(String[] args) throws IOException {
  40         String policyFile = args[0];
  41         ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());
  42 
  43         String here = System.getProperty("user.dir");
  44         String testSrc = System.getProperty("test.src");
  45         if (testSrc == null)
  46             throw new RuntimeException("This test must be run by jtreg");
  47         Path dir = Paths.get(testSrc);
  48 
  49         // Sanity check the environment
  50         if (Paths.get(here).isSameFile(dir))
  51             throw new RuntimeException("Working directory cannot be " + dir);
  52         DirectoryStream<Path> stream = dir.newDirectoryStream();
  53         try {
  54             if (!stream.iterator().hasNext())
  55                 throw new RuntimeException(testSrc + " is empty");
  56         } finally {
  57             stream.close();
  58         }
  59 
  60         // Install security manager with the given policy file
  61         System.setProperty("java.security.policy",
  62             dir.resolve(policyFile).toString());
  63         System.setSecurityManager(new SecurityManager());
  64 
  65         // Walk the source tree
  66         CountingVisitor visitor = new CountingVisitor();
  67         SecurityException exception = null;
  68         try {
  69             Files.walkFileTree(dir, visitor);
  70         } catch (SecurityException se) {
  71             exception = se;
  72         }
  73 
  74         // Check result
  75         switch (expectedResult) {
  76             case PASS:
  77                 if (exception != null) {


   1 /*
   2  * Copyright (c) 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  */


  30  * @run main/othervm WalkWithSecurity grantTopOnly.policy top_only
  31  */
  32 
  33 import java.nio.file.*;
  34 import java.nio.file.attribute.BasicFileAttributes;
  35 import java.io.IOException;
  36 
  37 public class WalkWithSecurity {
  38 
  39     public static void main(String[] args) throws IOException {
  40         String policyFile = args[0];
  41         ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());
  42 
  43         String here = System.getProperty("user.dir");
  44         String testSrc = System.getProperty("test.src");
  45         if (testSrc == null)
  46             throw new RuntimeException("This test must be run by jtreg");
  47         Path dir = Paths.get(testSrc);
  48 
  49         // Sanity check the environment
  50         if (Files.isSameFile(Paths.get(here), dir))
  51             throw new RuntimeException("Working directory cannot be " + dir);
  52         try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {

  53             if (!stream.iterator().hasNext())
  54                 throw new RuntimeException(testSrc + " is empty");


  55         }
  56 
  57         // Install security manager with the given policy file
  58         System.setProperty("java.security.policy",
  59             dir.resolve(policyFile).toString());
  60         System.setSecurityManager(new SecurityManager());
  61 
  62         // Walk the source tree
  63         CountingVisitor visitor = new CountingVisitor();
  64         SecurityException exception = null;
  65         try {
  66             Files.walkFileTree(dir, visitor);
  67         } catch (SecurityException se) {
  68             exception = se;
  69         }
  70 
  71         // Check result
  72         switch (expectedResult) {
  73             case PASS:
  74                 if (exception != null) {