Path: blob/master/test/jdk/sun/security/tools/jarsigner/AltProvider.java
41152 views
/*1* Copyright (c) 2016, 2019, 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 4906940 8130302 819415226* @summary -providerPath, -providerClass, -addprovider, and -providerArg27* @library /test/lib28* @modules java.base/jdk.internal.misc29* @build jdk.test.lib.util.JarUtils30* jdk.test.lib.compiler.CompilerUtils31* jdk.test.lib.Utils32* jdk.test.lib.Asserts33* jdk.test.lib.JDKToolFinder34* jdk.test.lib.JDKToolLauncher35* jdk.test.lib.Platform36* jdk.test.lib.process.*37* @run main AltProvider38*/3940import jdk.test.lib.JDKToolLauncher;41import jdk.test.lib.process.OutputAnalyzer;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.util.JarUtils;44import jdk.test.lib.compiler.CompilerUtils;4546import java.nio.file.*;4748public class AltProvider {4950private static final String TEST_SRC =51Paths.get(System.getProperty("test.src")).toString();5253private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "alt");54private static final Path MOD_DEST_DIR = Paths.get("mods");5556private static final String ktCommand = "-keystore x.jks " +57"-storepass changeit -storetype dummyks -list -debug";5859private static final String jsCommand = "-keystore x.jks " +60"-storepass changeit -storetype dummyks -debug x.jar x";6162public static void main(String[] args) throws Throwable {6364// Compile the provider65CompilerUtils.compile(66MOD_SRC_DIR, MOD_DEST_DIR,67"--module-source-path",68MOD_SRC_DIR.toString());6970// Create a keystore71tool("keytool", "-keystore x.jks -storetype jks -genkeypair -keyalg dsa" +72" -storepass changeit -keypass changeit -alias x -dname CN=X")73.shouldHaveExitValue(0);7475// Create a jar file76JarUtils.createJar("x.jar", "x.jks");7778// Test starts here7980// Without new provider81testBoth("", 1, "DUMMYKS not found");8283// legacy use (-providerPath only supported by keytool)84testKeytool("-providerPath mods/test.dummy " +85"-providerClass org.test.dummy.DummyProvider -providerArg full",860, "loadProviderByClass: org.test.dummy.DummyProvider");8788// legacy, on classpath89testBoth("-J-cp -Jmods/test.dummy " +90"-providerClass org.test.dummy.DummyProvider -providerArg full",910, "loadProviderByClass: org.test.dummy.DummyProvider");9293// Wrong name94testBoth("-J-cp -Jmods/test.dummy " +95"-providerClass org.test.dummy.Dummy -providerArg full",961, "Provider \"org.test.dummy.Dummy\" not found");9798// Not a provider name99testBoth("-J-cp -Jmods/test.dummy " +100"-providerClass java.lang.Object -providerArg full",1011, "java.lang.Object not a provider");102103// without arg104testBoth("-J-cp -Jmods/test.dummy " +105"-providerClass org.test.dummy.DummyProvider",1061, "DUMMYKS not found");107108// old -provider still works109testBoth("-J-cp -Jmods/test.dummy " +110"-provider org.test.dummy.DummyProvider -providerArg full",1110, "loadProviderByClass: org.test.dummy.DummyProvider");112113// name in a module114testBoth("-J--module-path=mods " +115"-addprovider Dummy -providerArg full",1160, "loadProviderByName: Dummy");117118// -providerClass does not work119testBoth("-J--module-path=mods " +120"-providerClass org.test.dummy.DummyProvider -providerArg full",1211, "Provider \"org.test.dummy.DummyProvider\" not found");122123// -addprovider with class does not work124testBoth("-J--module-path=mods " +125"-addprovider org.test.dummy.DummyProvider -providerArg full",1261, "Provider named \"org.test.dummy.DummyProvider\" not found");127128// -addprovider without arg does not work129testBoth("-J--module-path=mods " +130"-addprovider Dummy",1311, "DUMMYKS not found");132}133134// Test both tools with the same extra options135private static void testBoth(String args, int exitValue, String contains)136throws Throwable {137testKeytool(args, exitValue, contains);138testJarsigner(args, exitValue, contains);139}140141// Test keytool with extra options and check exitValue and output142private static void testKeytool(String args, int exitValue, String contains)143throws Throwable {144tool("keytool", ktCommand + " " + args)145.shouldHaveExitValue(exitValue)146.shouldContain(contains);147}148149// Test jarsigner with extra options and check exitValue and output150private static void testJarsigner(String args, int exitValue, String contains)151throws Throwable {152tool("jarsigner", jsCommand + " " + args)153.shouldHaveExitValue(exitValue)154.shouldContain(contains);155}156157// Launch a tool with args (space separated string)158private static OutputAnalyzer tool(String tool, String args)159throws Throwable {160JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK(tool);161162// Set locale to en-US so that the output are not translated into other languages.163l.addVMArg("-Duser.language=en");164l.addVMArg("-Duser.country=US");165166for (String a: args.split("\\s+")) {167if (a.startsWith("-J")) {168l.addVMArg(a.substring(2));169} else {170l.addToolArg(a);171}172}173return ProcessTools.executeCommand(l.getCommand());174}175}176177178