1 /*
   2  * Copyright (c) 2015, 2017, 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 import TVJar.TVPermission;
  25 import java.io.File;
  26 import java.nio.file.Files;
  27 import java.nio.file.Paths;
  28 import java.security.AccessController;
  29 import jdk.testlibrary.ProcessTools;
  30 import jdk.test.lib.util.JarUtils;
  31 
  32 /**
  33  * @test
  34  * @bug 8050402
  35  * @summary Check policy is extensible with user defined permissions
  36  * @library /lib/testlibrary /test/lib
  37  * @build jdk.test.lib.util.JarUtils
  38  * @compile TVJar/TVPermission.java
  39  * @run main ExtensiblePolicyWithJarTest
  40  */
  41 public class ExtensiblePolicyWithJarTest {
  42 
  43     public static void main(String args[]) throws Throwable {
  44         final String FS = File.separator;
  45         final String PS = File.pathSeparator;
  46         final String POL = "ExtensiblePolicyTest3.policy";
  47         final String JAVA_HOME = System.getProperty("test.jdk");
  48         final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";
  49         final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";
  50         final String KEYSTORE = "epkeystore";
  51         final String PASSWORD = "password";
  52         final String ALIAS = "duke2";
  53         final String CLASSPATH = System.getProperty("test.class.path", "");
  54         final String TESTCLASSES = System.getProperty("test.classes", "");
  55         final String TVPERMJAR = "tvPerm.jar";
  56         final String PATHTOJAR = System.getProperty("user.dir", "")
  57                                 + FS + TVPERMJAR;
  58 
  59         // create jar file for TVpermission
  60         new File("TVJar").mkdir();
  61         Files.copy(Paths.get(TESTCLASSES + FS + "TVJar", "TVPermission.class"),
  62                 Paths.get("TVJar", "TVPermission.class"));
  63         Files.copy(Paths.get(TESTCLASSES + FS + "TVJar",
  64                 "TVPermissionCollection.class"),
  65                 Paths.get("TVJar", "TVPermissionCollection.class"));
  66         JarUtils.createJar(TVPERMJAR, "TVJar/TVPermission.class",
  67                 "TVJar/TVPermissionCollection.class");
  68 
  69         // create key pair for jar signing
  70         ProcessTools.executeCommand(KEYTOOL,
  71                 "-genkey",
  72                 "-alias", ALIAS,
  73                 "-keystore", KEYSTORE,
  74                 "-storetype", "JKS",
  75                 "-keypass", PASSWORD,
  76                 "-dname", "cn=Blah",
  77                 "-storepass", PASSWORD
  78         ).shouldHaveExitValue(0);
  79         // sign jar
  80         ProcessTools.executeCommand(JARSIGNER,
  81                 "-keystore", KEYSTORE,
  82                 "-storepass", PASSWORD,
  83                 "-keypass", PASSWORD,
  84                 TVPERMJAR,
  85                 ALIAS).shouldHaveExitValue(0);
  86         // add jar file to classpath
  87         String cp = PATHTOJAR + PS + CLASSPATH;
  88 
  89         // policy file grants permission signed by duke2 to watch TVChanel 5
  90         try {
  91             String[] cmd = {
  92             "-classpath", cp,
  93             "-Djava.security.manager",
  94             "-Djava.security.policy=" + POL,
  95             "ExtensiblePolicyTest_orig$TestMain"};
  96             ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);
  97         } catch (Exception ex) {
  98             System.out.println("ExtensiblePolicyWithJarTest Failed");
  99         }
 100 
 101     }
 102 
 103     public static class TestMain {
 104         public static void main(String args[]) {
 105             TVPermission perm = new TVPermission("channel:5", "watch");
 106             try {
 107                 AccessController.checkPermission(perm);
 108             } catch (SecurityException se) {
 109                 throw new RuntimeException(se);
 110             }
 111         }
 112     }
 113 
 114 }