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