test/jdk/java/security/AccessController/DoPrivAccompliceTest.java
author shade
Tue, 15 Oct 2019 19:38:59 +0200
changeset 58604 791217cdc433
parent 51675 b487c1e914d0
permissions -rw-r--r--
8232205: Shenandoah: missing "Update References" -> "Update Roots" tracing Reviewed-by: rkennke, zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     1
/*
49883
d7fe9d3e7bf3 8196540: [Testbug] java/security/AccessController/DoPrivAccompliceTest.java doesn't handle unrelated warnings
vtewari
parents: 47216
diff changeset
     2
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     4
 *
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    10
 *
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    15
 * accompanied this code).
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    16
 *
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    20
 *
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    23
 * questions.
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    24
 */
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    25
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    26
import jdk.test.lib.process.ProcessTools;
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 45282
diff changeset
    27
import jdk.test.lib.util.JarUtils;
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    28
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    29
import java.io.FileWriter;
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    30
import java.io.IOException;
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    31
import java.nio.file.Path;
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    32
import java.nio.file.Paths;
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    33
41226
da753c438a07 8166378: Missing dependencies in several java/security tests
skovalev
parents: 26362
diff changeset
    34
/*
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    35
 * @test
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    36
 * @bug 8048362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    37
 * @summary Tests the doPrivileged with accomplice Generate two jars
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    38
 * (DoPrivTest.jar and DoPrivAccomplice.jar) and grant permission to
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    39
 * DoPrivAccmplice.jar for reading user.name property from a PrivilagedAction.
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    40
 * Run DoPrivTest.jar and try to access user.name property using
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    41
 * DoPrivAccmplice.jar.
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    42
 *
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 45282
diff changeset
    43
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    44
 * @build jdk.test.lib.util.JarUtils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    45
 *        jdk.test.lib.Utils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    46
 *        jdk.test.lib.Asserts
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    47
 *        jdk.test.lib.JDKToolFinder
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    48
 *        jdk.test.lib.JDKToolLauncher
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    49
 *        jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    50
 *        jdk.test.lib.process.*
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    51
 * @run main/othervm DoPrivAccompliceTest
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    52
 */
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    53
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    54
public class DoPrivAccompliceTest {
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    55
    private static final String ACTION_SOURCE = DoPrivAccomplice.class.getName();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    56
    private static final String TEST_SOURCE = DoPrivTest.class.getName();
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    57
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    58
    private static void createPolicyFile(Path jarFile, Path policy) {
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    59
        String codebase = jarFile.toFile().toURI().toString();
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    60
        String quotes = "\"";
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    61
        StringBuilder policyFile = new StringBuilder();
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    62
        policyFile.append("grant codeBase ")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    63
                  .append(quotes).append(codebase).append(quotes)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    64
                  .append("{\n")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    65
                  .append("permission java.util.PropertyPermission ")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    66
                  .append(quotes).append("user.name").append(quotes)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    67
                  .append(",")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    68
                  .append(quotes).append("read").append(quotes)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    69
                  .append(";\n};");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    70
        try (FileWriter writer = new FileWriter(policy.toFile())) {
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    71
            writer.write(policyFile.toString());
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    72
        } catch (IOException e) {
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    73
            throw new Error("Error while creating policy file " + policy, e);
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    74
        }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    75
    }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    76
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    77
    public static void main(String[] args) throws Exception {
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    78
        // copy class files to pwd
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    79
        ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    80
        Path pwd = Paths.get(".");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    81
        Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    82
        Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    83
        Path policy = pwd.resolve("java.policy").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    84
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    85
        JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    86
        System.out.println("Created jar file " + jarFile1);
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    87
        JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    88
        System.out.println("Created jar file " + jarFile2);
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    89
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    90
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    91
        String pathSepartor = System.getProperty("path.separator");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    92
        String[] commands = {
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    93
                "-Djava.security.manager",
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    94
                "-Djava.security.policy=" + policy,
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    95
                "-classpath", jarFile1 + pathSepartor + jarFile2,
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    96
                TEST_SOURCE
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    97
        };
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    98
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    99
        String userName = System.getProperty("user.name");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   100
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   101
        createPolicyFile(jarFile1, policy);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   102
        System.out.println("Created policy for " + jarFile1);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   103
        ProcessTools.executeTestJava(commands)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   104
                    .shouldHaveExitValue(0)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   105
                    .shouldContain(userName)
49883
d7fe9d3e7bf3 8196540: [Testbug] java/security/AccessController/DoPrivAccompliceTest.java doesn't handle unrelated warnings
vtewari
parents: 47216
diff changeset
   106
                    .stderrShouldBeEmptyIgnoreVMWarnings();
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   107
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   108
        createPolicyFile(jarFile2, policy);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   109
        System.out.println("Created policy for " + jarFile2);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   110
        ProcessTools.executeTestJava(commands)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   111
                    .shouldNotHaveExitValue(0)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   112
                    .shouldNotContain(userName)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   113
                    .stderrShouldContain("java.security.AccessControlException");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   114
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   115
        System.out.println("Test PASSES");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
   116
    }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
   117
}