jdk/test/java/lang/invoke/lambda/LambdaAccessControlDoPrivilegedTest.java
author shurailine
Tue, 27 Oct 2015 20:06:02 -0700
changeset 33402 1156d495a525
parent 30820 0d4717a011d3
child 36237 963d1115678b
permissions -rw-r--r--
8140336: Add @modules for exported dependencies to jdk_core tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     1
/*
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     4
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     8
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    13
 * accompanied this code).
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    14
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    18
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    21
 * questions.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    22
 */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    23
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    24
/*
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    25
 * @test
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    26
 * @bug 8003881
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    27
 * @summary tests DoPrivileged action (implemented as lambda expressions) by
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    28
 * inserting them into the BootClassPath.
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
    29
 * @modules jdk.compiler
16062
c64ef2b01401 8006000: TEST_BUG: java/lang/invoke/lambda/LambdaAccessControlTest.java fails intermittently
alanb
parents: 14762
diff changeset
    30
 * @compile -XDignore.symbol.file LambdaAccessControlDoPrivilegedTest.java LUtils.java
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    31
 * @run main/othervm LambdaAccessControlDoPrivilegedTest
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    32
 */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    33
import java.io.File;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    34
import java.util.ArrayList;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    35
import java.util.List;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    36
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    37
public class LambdaAccessControlDoPrivilegedTest extends LUtils {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    38
    public static void main(String... args) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    39
        final List<String> scratch = new ArrayList();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    40
        scratch.clear();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    41
        scratch.add("import java.security.*;");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    42
        scratch.add("public class DoPriv {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    43
        scratch.add("public static void main(String... args) {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    44
        scratch.add("String prop = AccessController.doPrivileged((PrivilegedAction<String>) () -> {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    45
        scratch.add("return System.getProperty(\"user.home\");");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    46
        scratch.add("});");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    47
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    48
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    49
        File doprivJava = new File("DoPriv.java");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    50
        File doprivClass = getClassFile(doprivJava);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    51
        createFile(doprivJava, scratch);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    52
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    53
        scratch.clear();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    54
        scratch.add("public class Bar {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    55
        scratch.add("public static void main(String... args) {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    56
        scratch.add("System.out.println(\"sun.boot.class.path\" + \"=\" +");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    57
        scratch.add("    System.getProperty(\"sun.boot.class.path\", \"\"));");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    58
        scratch.add("System.setSecurityManager(new SecurityManager());");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    59
        scratch.add("DoPriv.main();");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    60
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    61
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    62
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    63
        File barJava = new File("Bar.java");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    64
        File barClass = getClassFile(barJava);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    65
        createFile(barJava, scratch);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    66
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    67
        String[] javacArgs = {barJava.getName(), doprivJava.getName()};
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    68
        compile(javacArgs);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    69
        File jarFile = new File("foo.jar");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    70
        String[] jargs = {"cvf", jarFile.getName(), doprivClass.getName()};
26635
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    71
        TestResult tr = doExec(JAR_CMD.getAbsolutePath(),
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    72
                                "cvf", jarFile.getName(),
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    73
                                doprivClass.getName());
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    74
        if (tr.exitValue != 0){
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    75
            throw new RuntimeException(tr.toString());
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    76
        }
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    77
        doprivJava.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    78
        doprivClass.delete();
26635
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    79
        tr = doExec(JAVA_CMD.getAbsolutePath(),
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 26635
diff changeset
    80
                    "-Xbootclasspath/a:foo.jar",
26635
2038308888ac 8058569: Update java/lang/invoke/lambda tests to eliminate dependency on sun.tools.jar.Main
dholmes
parents: 23010
diff changeset
    81
                    "-cp", ".", "Bar");
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    82
        tr.assertZero("testDoPrivileged fails");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    83
        barJava.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    84
        barClass.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    85
        jarFile.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    86
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    87
}