test/jdk/tools/jlink/JLinkToolProviderTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 41559 jdk/test/tools/jlink/JLinkToolProviderTest.java@5b33f33df1fb
child 48676 f8188cc0d01d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
/*
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    31
 * @build JLinkToolProviderTest
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    32
 * @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
    33
 */
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    34
public class JLinkToolProviderTest {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    35
    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
    36
        .orElseThrow(() ->
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    37
            new RuntimeException("jlink tool not found")
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    38
        );
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
    private static void checkJlinkOptions(String... options) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    41
        StringWriter writer = new StringWriter();
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    42
        PrintWriter pw = new PrintWriter(writer);
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    43
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    44
        try {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    45
            JLINK_TOOL.run(pw, pw, options);
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    46
            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
    47
        } catch (AccessControlException ace) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    48
            if (! ace.getPermission().getClass().getName().contains("JlinkPermission")) {
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    49
                throw new AssertionError("expected JlinkPermission check failure");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    50
            }
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
    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
    55
        checkJlinkOptions("--help");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    56
        checkJlinkOptions("--list-plugins");
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    57
    }
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
diff changeset
    58
}