Path: blob/master/test/jdk/java/security/Policy/ExtensiblePolicy/ExtensiblePolicyWithJarTest.java
41153 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import TVJar.TVPermission;24import java.io.File;25import java.nio.file.Files;26import java.nio.file.Paths;27import java.security.AccessController;28import jdk.test.lib.process.ProcessTools;29import jdk.test.lib.util.JarUtils;3031/**32* @test33* @bug 805040234* @summary Check policy is extensible with user defined permissions35* @library /test/lib36* @build jdk.test.lib.util.JarUtils37* @compile TVJar/TVPermission.java38* @run main ExtensiblePolicyWithJarTest39*/40public class ExtensiblePolicyWithJarTest {4142public static void main(String args[]) throws Throwable {43final String FS = File.separator;44final String PS = File.pathSeparator;45final String POL = "ExtensiblePolicyTest3.policy";46final String JAVA_HOME = System.getProperty("test.jdk");47final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";48final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";49final String KEYSTORE = "epkeystore";50final String PASSWORD = "password";51final String ALIAS = "duke2";52final String CLASSPATH = System.getProperty("test.class.path", "");53final String TESTCLASSES = System.getProperty("test.classes", "");54final String TVPERMJAR = "tvPerm.jar";55final String PATHTOJAR = System.getProperty("user.dir", "")56+ FS + TVPERMJAR;5758// create jar file for TVpermission59new File("TVJar").mkdir();60Files.copy(Paths.get(TESTCLASSES + FS + "TVJar", "TVPermission.class"),61Paths.get("TVJar", "TVPermission.class"));62Files.copy(Paths.get(TESTCLASSES + FS + "TVJar",63"TVPermissionCollection.class"),64Paths.get("TVJar", "TVPermissionCollection.class"));65JarUtils.createJar(TVPERMJAR, "TVJar/TVPermission.class",66"TVJar/TVPermissionCollection.class");6768// create key pair for jar signing69ProcessTools.executeCommand(KEYTOOL,70"-genkey",71"-keyalg", "DSA",72"-alias", ALIAS,73"-keystore", KEYSTORE,74"-storetype", "JKS",75"-keypass", PASSWORD,76"-dname", "cn=Blah",77"-storepass", PASSWORD78).shouldHaveExitValue(0);79// sign jar80ProcessTools.executeCommand(JARSIGNER,81"-keystore", KEYSTORE,82"-storepass", PASSWORD,83"-keypass", PASSWORD,84TVPERMJAR,85ALIAS).shouldHaveExitValue(0);86// add jar file to classpath87String cp = PATHTOJAR + PS + CLASSPATH;8889// policy file grants permission signed by duke2 to watch TVChanel 590try {91String[] cmd = {92"-classpath", cp,93"-Djava.security.manager",94"-Djava.security.policy=" + POL,95"ExtensiblePolicyTest_orig$TestMain"};96ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);97} catch (Exception ex) {98System.out.println("ExtensiblePolicyWithJarTest Failed");99}100101}102103public static class TestMain {104public static void main(String args[]) {105TVPermission perm = new TVPermission("channel:5", "watch");106try {107AccessController.checkPermission(perm);108} catch (SecurityException se) {109throw new RuntimeException(se);110}111}112}113114}115116117