test/jdk/java/security/Policy/ExtensiblePolicy/ExtensiblePolicyWithJarTest.java
author weijun
Fri, 15 Nov 2019 09:06:58 +0800
changeset 59104 046e4024e55a
parent 51675 b487c1e914d0
permissions -rw-r--r--
8214024: Remove the default keytool -keyalg value Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32775
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     1
/*
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 51675
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
32775
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     4
 *
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     8
 *
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    13
 * accompanied this code).
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    14
 *
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    18
 *
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    21
 * questions.
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    22
 */
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    23
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    24
import TVJar.TVPermission;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    25
import java.io.File;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    26
import java.nio.file.Files;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    27
import java.nio.file.Paths;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    28
import java.security.AccessController;
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
    29
import jdk.test.lib.process.ProcessTools;
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 32775
diff changeset
    30
import jdk.test.lib.util.JarUtils;
32775
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    31
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    32
/**
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    33
 * @test
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    34
 * @bug 8050402
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    35
 * @summary Check policy is extensible with user defined permissions
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
    36
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    37
 * @build jdk.test.lib.util.JarUtils
32775
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    38
 * @compile TVJar/TVPermission.java
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    39
 * @run main ExtensiblePolicyWithJarTest
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    40
 */
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    41
public class ExtensiblePolicyWithJarTest {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    42
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    43
    public static void main(String args[]) throws Throwable {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    44
        final String FS = File.separator;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    45
        final String PS = File.pathSeparator;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    46
        final String POL = "ExtensiblePolicyTest3.policy";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    47
        final String JAVA_HOME = System.getProperty("test.jdk");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    48
        final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    49
        final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    50
        final String KEYSTORE = "epkeystore";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    51
        final String PASSWORD = "password";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    52
        final String ALIAS = "duke2";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    53
        final String CLASSPATH = System.getProperty("test.class.path", "");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    54
        final String TESTCLASSES = System.getProperty("test.classes", "");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    55
        final String TVPERMJAR = "tvPerm.jar";
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    56
        final String PATHTOJAR = System.getProperty("user.dir", "")
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    57
                                + FS + TVPERMJAR;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    58
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    59
        // create jar file for TVpermission
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    60
        new File("TVJar").mkdir();
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    61
        Files.copy(Paths.get(TESTCLASSES + FS + "TVJar", "TVPermission.class"),
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    62
                Paths.get("TVJar", "TVPermission.class"));
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    63
        Files.copy(Paths.get(TESTCLASSES + FS + "TVJar",
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    64
                "TVPermissionCollection.class"),
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    65
                Paths.get("TVJar", "TVPermissionCollection.class"));
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    66
        JarUtils.createJar(TVPERMJAR, "TVJar/TVPermission.class",
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    67
                "TVJar/TVPermissionCollection.class");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    68
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    69
        // create key pair for jar signing
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    70
        ProcessTools.executeCommand(KEYTOOL,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    71
                "-genkey",
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 51675
diff changeset
    72
                "-keyalg", "DSA",
32775
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    73
                "-alias", ALIAS,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    74
                "-keystore", KEYSTORE,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    75
                "-storetype", "JKS",
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    76
                "-keypass", PASSWORD,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    77
                "-dname", "cn=Blah",
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    78
                "-storepass", PASSWORD
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    79
        ).shouldHaveExitValue(0);
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    80
        // sign jar
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    81
        ProcessTools.executeCommand(JARSIGNER,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    82
                "-keystore", KEYSTORE,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    83
                "-storepass", PASSWORD,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    84
                "-keypass", PASSWORD,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    85
                TVPERMJAR,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    86
                ALIAS).shouldHaveExitValue(0);
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    87
        // add jar file to classpath
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    88
        String cp = PATHTOJAR + PS + CLASSPATH;
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    89
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    90
        // policy file grants permission signed by duke2 to watch TVChanel 5
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    91
        try {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    92
            String[] cmd = {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    93
            "-classpath", cp,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    94
            "-Djava.security.manager",
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    95
            "-Djava.security.policy=" + POL,
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    96
            "ExtensiblePolicyTest_orig$TestMain"};
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    97
            ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    98
        } catch (Exception ex) {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
    99
            System.out.println("ExtensiblePolicyWithJarTest Failed");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   100
        }
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   101
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   102
    }
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   103
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   104
    public static class TestMain {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   105
        public static void main(String args[]) {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   106
            TVPermission perm = new TVPermission("channel:5", "watch");
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   107
            try {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   108
                AccessController.checkPermission(perm);
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   109
            } catch (SecurityException se) {
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   110
                throw new RuntimeException(se);
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   111
            }
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   112
        }
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   113
    }
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   114
cab3b72f31b7 8050402: Tests to check for use of policy files
amjiang
parents:
diff changeset
   115
}