jdk/test/java/security/AccessController/DoPrivAccompliceTest.java
author iignatyev
Fri, 26 May 2017 22:30:19 -0700
changeset 45282 e1cef38d150d
parent 41226 da753c438a07
child 45287 e0bb5f83e17a
permissions -rw-r--r--
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved Reviewed-by: asmotrak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
     1
/*
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
     2
 * Copyright (c) 2007, 2017, 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.OutputAnalyzer;
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    27
import jdk.test.lib.process.ProcessTools;
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    28
import jdk.testlibrary.JarUtils;
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    29
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    30
import java.io.FileWriter;
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    31
import java.io.IOException;
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    32
import java.nio.file.Path;
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    33
import java.nio.file.Paths;
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    34
41226
da753c438a07 8166378: Missing dependencies in several java/security tests
skovalev
parents: 26362
diff changeset
    35
/*
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    36
 * @test
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    37
 * @bug 8048362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    38
 * @summary Tests the doPrivileged with accomplice Generate two jars
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    39
 * (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
    40
 * 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
    41
 * Run DoPrivTest.jar and try to access user.name property using
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    42
 * DoPrivAccmplice.jar.
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    43
 *
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    44
 * @library /test/lib /lib/testlibrary
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    45
 *
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    46
 * @run main/othervm DoPrivAccompliceTest
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    47
 */
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    48
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    49
public class DoPrivAccompliceTest {
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    50
    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
    51
    private static final String TEST_SOURCE = DoPrivTest.class.getName();
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    52
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    53
    private static void createPolicyFile(Path jarFile, Path policy) {
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    54
        String codebase = jarFile.toFile().toURI().toString();
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    55
        String quotes = "\"";
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    56
        StringBuilder policyFile = new StringBuilder();
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    57
        policyFile.append("grant codeBase ")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    58
                  .append(quotes).append(codebase).append(quotes)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    59
                  .append("{\n")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    60
                  .append("permission java.util.PropertyPermission ")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    61
                  .append(quotes).append("user.name").append(quotes)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    62
                  .append(",")
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    63
                  .append(quotes).append("read").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
        try (FileWriter writer = new FileWriter(policy.toFile())) {
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    66
            writer.write(policyFile.toString());
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    67
        } catch (IOException e) {
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    68
            throw new Error("Error while creating policy file " + policy, e);
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    69
        }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    70
    }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    71
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    72
    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
    73
        // copy class files to pwd
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    74
        ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    75
        Path pwd = Paths.get(".");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    76
        Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    77
        Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    78
        Path policy = pwd.resolve("java.policy").toAbsolutePath();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    79
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    80
        JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    81
        System.out.println("Created jar file " + jarFile1);
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    82
        JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    83
        System.out.println("Created jar file " + jarFile2);
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    84
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
    85
45282
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    86
        String pathSepartor = System.getProperty("path.separator");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    87
        String[] commands = {
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    88
                "-Djava.security.manager",
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    89
                "-Djava.security.policy=" + policy,
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    90
                "-classpath", jarFile1 + pathSepartor + jarFile2,
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    91
                TEST_SOURCE
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    92
        };
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    93
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    94
        String userName = System.getProperty("user.name");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    95
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    96
        createPolicyFile(jarFile1, policy);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    97
        System.out.println("Created policy for " + jarFile1);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    98
        ProcessTools.executeTestJava(commands)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
    99
                    .shouldHaveExitValue(0)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   100
                    .shouldContain(userName)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   101
                    .stderrShouldBeEmpty();
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   102
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   103
        createPolicyFile(jarFile2, policy);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   104
        System.out.println("Created policy for " + jarFile2);
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   105
        ProcessTools.executeTestJava(commands)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   106
                    .shouldNotHaveExitValue(0)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   107
                    .shouldNotContain(userName)
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   108
                    .stderrShouldContain("java.security.AccessControlException");
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   109
e1cef38d150d 8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents: 41226
diff changeset
   110
        System.out.println("Test PASSES");
26362
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
   111
    }
b1e9b551e10a 8048362: Tests for doPrivileged with accomplice
xuelei
parents:
diff changeset
   112
}