test/jdk/internal/jrtfs/WithSecurityManager.java

Print this page




  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  * @run main/othervm WithSecurityManager allow
  27  * @run main/othervm WithSecurityManager deny
  28  */
  29 
  30 import java.net.URI;
  31 import java.nio.file.FileSystems;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;

  34 
  35 public class WithSecurityManager {
  36     public static void main(String[] args) throws Exception {
  37         boolean allow = args[0].equals("allow");
  38 
  39         // set security policy to allow access
  40         if (allow) {
  41             String testSrc = System.getProperty("test.src");
  42             if (testSrc == null)
  43                 testSrc = ".";
  44             Path policyFile = Paths.get(testSrc, "java.policy");
  45             System.setProperty("java.security.policy", policyFile.toString());
  46         }
  47 
  48         // make sure that jrt:/ has been created before we have a security manager
  49         FileSystems.getFileSystem(URI.create("jrt:/"));
  50 
  51         System.setSecurityManager(new SecurityManager());
  52 
  53         // check FileSystems.getFileSystem
  54         try {
  55             FileSystems.getFileSystem(URI.create("jrt:/"));
  56             if (!allow) throw new RuntimeException("access not expected");
  57         } catch (SecurityException se) {
  58             if (allow)
  59                 throw se;
  60         }
  61 
  62         // check FileSystems.newFileSystem
  63         try {
  64             FileSystems.newFileSystem(URI.create("jrt:/"), null);
  65             if (!allow) throw new RuntimeException("access not expected");
  66         } catch (SecurityException se) {
  67             if (allow)
  68                 throw se;
  69         }
  70 
  71         // check Paths.get
  72         try {
  73             Paths.get(URI.create("jrt:/java.base/java/lang/Object.class"));
  74             if (!allow) throw new RuntimeException("access not expected");
  75         } catch (SecurityException se) {
  76             if (allow)
  77                 throw se;
  78         }
  79     }
  80 }


  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  * @run main/othervm WithSecurityManager allow
  27  * @run main/othervm WithSecurityManager deny
  28  */
  29 
  30 import java.net.URI;
  31 import java.nio.file.FileSystems;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.util.Collections;
  35 
  36 public class WithSecurityManager {
  37     public static void main(String[] args) throws Exception {
  38         boolean allow = args[0].equals("allow");
  39 
  40         // set security policy to allow access
  41         if (allow) {
  42             String testSrc = System.getProperty("test.src");
  43             if (testSrc == null)
  44                 testSrc = ".";
  45             Path policyFile = Paths.get(testSrc, "java.policy");
  46             System.setProperty("java.security.policy", policyFile.toString());
  47         }
  48 
  49         // make sure that jrt:/ has been created before we have a security manager
  50         FileSystems.getFileSystem(URI.create("jrt:/"));
  51 
  52         System.setSecurityManager(new SecurityManager());
  53 
  54         // check FileSystems.getFileSystem
  55         try {
  56             FileSystems.getFileSystem(URI.create("jrt:/"));
  57             if (!allow) throw new RuntimeException("access not expected");
  58         } catch (SecurityException se) {
  59             if (allow)
  60                 throw se;
  61         }
  62 
  63         // check FileSystems.newFileSystem
  64         try {
  65             FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap());
  66             if (!allow) throw new RuntimeException("access not expected");
  67         } catch (SecurityException se) {
  68             if (allow)
  69                 throw se;
  70         }
  71 
  72         // check Paths.get
  73         try {
  74             Paths.get(URI.create("jrt:/java.base/java/lang/Object.class"));
  75             if (!allow) throw new RuntimeException("access not expected");
  76         } catch (SecurityException se) {
  77             if (allow)
  78                 throw se;
  79         }
  80     }
  81 }