Path: blob/master/test/jdk/tools/jlink/JLinkSigningTest.java
51214 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815939326* @summary Test signed jars involved in image creation27* @modules java.base/jdk.internal.jimage28* java.base/sun.security.tools.keytool29* jdk.compiler/com.sun.tools.javac30* jdk.jartool/sun.security.tools.jarsigner31* jdk.jartool/sun.tools.jar32* jdk.jlink/jdk.tools.jlink.internal33* @run main/othervm JLinkSigningTest34*/353637import java.io.File;38import java.io.IOException;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.util.Arrays;43import java.util.spi.ToolProvider;4445public class JLinkSigningTest {46private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")47.orElseThrow(() -> new RuntimeException("jar tool not found"));48private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")49.orElseThrow(() -> new RuntimeException("javac tool not found"));50private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")51.orElseThrow(() -> new RuntimeException("jlink tool not found"));5253static final String[] MODULE_INFO = {54"module test {",55"}",56};5758static final String[] TEST_CLASS = {59"package test;",60"public class test {",61" public static void main(String[] args) {",62" }",63"}",64};6566static void report(String command, String[] args) {67System.out.println(command + " " + String.join(" ", Arrays.asList(args)));68}6970static void jar(String[] args) {71report("jar", args);72JAR_TOOL.run(System.out, System.err, args);73}7475static void jarsigner(String[] args) {76report("jarsigner", args);7778try {79sun.security.tools.jarsigner.Main.main(args);80} catch (Exception ex) {81throw new RuntimeException("jarsigner not found");82}83}8485static void javac(String[] args) {86report("javac", args);87JAVAC_TOOL.run(System.out, System.err, args);88}8990static void jlink(String[] args) {91report("jlink", args);92JLINK_TOOL.run(System.out, System.err, args);93}9495static void keytool(String[] args) {96report("keytool", args);9798try {99sun.security.tools.keytool.Main.main(args);100} catch (Exception ex) {101throw new RuntimeException("keytool failed");102}103}104105public static void main(String[] args) {106final String JAVA_HOME = System.getProperty("java.home");107Path moduleInfoJavaPath = Paths.get("module-info.java");108Path moduleInfoClassPath = Paths.get("module-info.class");109Path testDirectoryPath = Paths.get("test");110Path testJavaPath = testDirectoryPath.resolve("test.java");111Path testClassPath = testDirectoryPath.resolve("test.class");112Path testModsDirectoryPath = Paths.get("testmods");113Path jmodsPath = Paths.get(JAVA_HOME, "jmods");114Path testjarPath = testModsDirectoryPath.resolve("test.jar");115String modulesPath = testjarPath.toString() +116File.pathSeparator +117jmodsPath.toString();118119try {120Files.write(moduleInfoJavaPath, Arrays.asList(MODULE_INFO));121Files.createDirectories(testDirectoryPath);122Files.write(testJavaPath, Arrays.asList(TEST_CLASS));123Files.createDirectories(testModsDirectoryPath);124} catch (IOException ex) {125throw new RuntimeException("file construction failed");126}127128javac(new String[] {129testJavaPath.toString(),130moduleInfoJavaPath.toString(),131});132133jar(new String[] {134"-c",135"-f", testjarPath.toString(),136"--module-path", jmodsPath.toString(),137testClassPath.toString(),138moduleInfoClassPath.toString(),139});140141keytool(new String[] {142"-genkey",143"-keyalg", "RSA",144"-dname", "CN=John Doe, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",145"-alias", "examplekey",146"-storepass", "password",147"-keypass", "password",148"-keystore", "examplekeystore",149"-validity", "365",150});151152jarsigner(new String[] {153"-keystore", "examplekeystore",154"-verbose", testjarPath.toString(),155"-storepass", "password",156"-keypass", "password",157"examplekey",158});159160try {161jlink(new String[] {162"--module-path", modulesPath,163"--add-modules", "test",164"--output", "foo",165});166} catch (Throwable ex) {167System.out.println("Failed as should");168}169170try {171jlink(new String[] {172"--module-path", modulesPath,173"--add-modules", "test",174"--ignore-signing-information",175"--output", "foo",176});177System.out.println("Suceeded as should");178} catch (Throwable ex) {179System.err.println("Should not have failed");180throw new RuntimeException(ex);181}182183System.out.println("Done");184}185}186187188189