1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.PrintWriter;
  25 import java.io.StringWriter;
  26 import java.security.AccessControlException;
  27 import java.util.spi.ToolProvider;
  28 
  29 /*
  30  * @test
  31  * @modules jdk.jlink
  32  * @build JLinkToolProviderTest
  33  * @run main/othervm/java.security.policy=toolprovider.policy JLinkToolProviderTest
  34  */
  35 public class JLinkToolProviderTest {
  36     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
  37         .orElseThrow(() ->
  38             new RuntimeException("jlink tool not found")
  39         );
  40 
  41     private static void checkJlinkOptions(String... options) {
  42         StringWriter writer = new StringWriter();
  43         PrintWriter pw = new PrintWriter(writer);
  44 
  45         try {
  46             JLINK_TOOL.run(pw, pw, options);
  47             throw new AssertionError("SecurityException should have been thrown!");
  48         } catch (AccessControlException ace) {
  49             if (! ace.getPermission().getClass().getName().contains("JlinkPermission")) {
  50                 throw new AssertionError("expected JlinkPermission check failure");
  51             }
  52         }
  53     }
  54 
  55     public static void main(String[] args) throws Exception {
  56         checkJlinkOptions("--help");
  57         checkJlinkOptions("--list-plugins");
  58     }
  59 }