test/jdk/tools/jlink/JLinkToolProviderTest.java
author dmarkov
Mon, 23 Sep 2019 17:18:03 +0100
changeset 58328 fe46ee1d42ee
parent 48676 f8188cc0d01d
permissions -rw-r--r--
8230782: Robot.createScreenCapture() fails if “awt.robot.gtk” is set to false Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     1
/*
48676
f8188cc0d01d 8179294: several langtools tests depend on jar, jlink, javac but do not declare that dependency
anazarov
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     4
 *
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     8
 *
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    13
 * accompanied this code).
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    14
 *
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    18
 *
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    21
 * questions.
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    22
 */
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    23
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    24
import java.io.PrintWriter;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    25
import java.io.StringWriter;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    26
import java.security.AccessControlException;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    27
import java.util.spi.ToolProvider;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    28
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    29
/*
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    30
 * @test
48676
f8188cc0d01d 8179294: several langtools tests depend on jar, jlink, javac but do not declare that dependency
anazarov
parents: 47216
diff changeset
    31
 * @modules jdk.jlink
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    32
 * @build JLinkToolProviderTest
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    33
 * @run main/othervm/java.security.policy=toolprovider.policy JLinkToolProviderTest
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    34
 */
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    35
public class JLinkToolProviderTest {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    36
    static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    37
        .orElseThrow(() ->
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    38
            new RuntimeException("jlink tool not found")
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    39
        );
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    40
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    41
    private static void checkJlinkOptions(String... options) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    42
        StringWriter writer = new StringWriter();
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    43
        PrintWriter pw = new PrintWriter(writer);
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    44
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    45
        try {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    46
            JLINK_TOOL.run(pw, pw, options);
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    47
            throw new AssertionError("SecurityException should have been thrown!");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    48
        } catch (AccessControlException ace) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    49
            if (! ace.getPermission().getClass().getName().contains("JlinkPermission")) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    50
                throw new AssertionError("expected JlinkPermission check failure");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    51
            }
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    52
        }
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    53
    }
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    54
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    56
        checkJlinkOptions("--help");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    57
        checkJlinkOptions("--list-plugins");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    58
    }
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    59
}