1 /*
   2  * Copyright (c) 2016, 2017, 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 /*
  25  * @test
  26  * @bug 4906940 8130302
  27  * @summary -providerPath, -providerClass, -addprovider, and -providerArg
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  * @build jdk.test.lib.util.JarUtils
  31  *        jdk.test.lib.compiler.CompilerUtils
  32  *        jdk.test.lib.Utils
  33  *        jdk.test.lib.Asserts
  34  *        jdk.test.lib.JDKToolFinder
  35  *        jdk.test.lib.JDKToolLauncher
  36  *        jdk.test.lib.Platform
  37  *        jdk.test.lib.process.*
  38  * @run main AltProvider
  39  */
  40 
  41 import jdk.test.lib.JDKToolLauncher;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jdk.test.lib.util.JarUtils;
  45 import jdk.test.lib.compiler.CompilerUtils;
  46 
  47 import java.nio.file.*;
  48 
  49 public class AltProvider {
  50 
  51     private static final String TEST_SRC =
  52             Paths.get(System.getProperty("test.src")).toString();
  53 
  54     private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "alt");
  55     private static final Path MOD_DEST_DIR = Paths.get("mods");
  56 
  57     private static final String ktCommand = "-keystore x.jks " +
  58             "-storepass changeit -storetype dummyks -list -debug";
  59 
  60     private static final String jsCommand = "-keystore x.jks " +
  61             "-storepass changeit -storetype dummyks -debug x.jar x";
  62 
  63     public static void main(String[] args) throws Throwable {
  64 
  65         // Compile the provider
  66         CompilerUtils.compile(
  67                 MOD_SRC_DIR, MOD_DEST_DIR,
  68                 "--module-source-path",
  69                 MOD_SRC_DIR.toString());
  70 
  71         // Create a keystore
  72         tool("keytool", "-keystore x.jks -storetype jks -genkeypair" +
  73                 " -storepass changeit -keypass changeit -alias x -dname CN=X")
  74                 .shouldHaveExitValue(0);
  75 
  76         // Create a jar file
  77         JarUtils.createJar("x.jar", "x.jks");
  78 
  79         // Test starts here
  80 
  81         // Without new provider
  82         testBoth("", 1, "DUMMYKS not found");
  83 
  84         // legacy use (-providerPath only supported by keytool)
  85         testKeytool("-providerPath mods/test.dummy " +
  86                 "-providerClass org.test.dummy.DummyProvider -providerArg full",
  87                 0, "loadProviderByClass: org.test.dummy.DummyProvider");
  88 
  89         // legacy, on classpath
  90         testBoth("-J-cp -Jmods/test.dummy " +
  91                 "-providerClass org.test.dummy.DummyProvider -providerArg full",
  92                 0, "loadProviderByClass: org.test.dummy.DummyProvider");
  93 
  94         // Wrong name
  95         testBoth("-J-cp -Jmods/test.dummy " +
  96                 "-providerClass org.test.dummy.Dummy -providerArg full",
  97                 1, "Provider \"org.test.dummy.Dummy\" not found");
  98 
  99         // Not a provider name
 100         testBoth("-J-cp -Jmods/test.dummy " +
 101                 "-providerClass java.lang.Object -providerArg full",
 102                 1, "java.lang.Object not a provider");
 103 
 104         // without arg
 105         testBoth("-J-cp -Jmods/test.dummy " +
 106                 "-providerClass org.test.dummy.DummyProvider",
 107                 1, "DUMMYKS not found");
 108 
 109         // old -provider still works
 110         testBoth("-J-cp -Jmods/test.dummy " +
 111                 "-provider org.test.dummy.DummyProvider -providerArg full",
 112                 0, "loadProviderByClass: org.test.dummy.DummyProvider");
 113 
 114         // name in a module
 115         testBoth("-J--module-path=mods " +
 116                 "-addprovider Dummy -providerArg full",
 117                 0, "loadProviderByName: Dummy");
 118 
 119         // -providerClass does not work
 120         testBoth("-J--module-path=mods " +
 121                 "-providerClass org.test.dummy.DummyProvider -providerArg full",
 122                 1, "Provider \"org.test.dummy.DummyProvider\" not found");
 123 
 124         // -addprovider with class does not work
 125         testBoth("-J--module-path=mods " +
 126                 "-addprovider org.test.dummy.DummyProvider -providerArg full",
 127                 1, "Provider named \"org.test.dummy.DummyProvider\" not found");
 128 
 129         // -addprovider without arg does not work
 130         testBoth("-J--module-path=mods " +
 131                 "-addprovider Dummy",
 132                 1, "DUMMYKS not found");
 133     }
 134 
 135     // Test both tools with the same extra options
 136     private static void testBoth(String args, int exitValue, String contains)
 137             throws Throwable {
 138         testKeytool(args, exitValue, contains);
 139         testJarsigner(args, exitValue, contains);
 140     }
 141 
 142     // Test keytool with extra options and check exitValue and output
 143     private static void testKeytool(String args, int exitValue, String contains)
 144             throws Throwable {
 145         tool("keytool", ktCommand + " " + args)
 146                 .shouldHaveExitValue(exitValue)
 147                 .shouldContain(contains);
 148     }
 149 
 150     // Test jarsigner with extra options and check exitValue and output
 151     private static void testJarsigner(String args, int exitValue, String contains)
 152             throws Throwable {
 153         tool("jarsigner", jsCommand + " " + args)
 154                 .shouldHaveExitValue(exitValue)
 155                 .shouldContain(contains);
 156     }
 157 
 158     // Launch a tool with args (space separated string)
 159     private static OutputAnalyzer tool(String tool, String args)
 160             throws Throwable {
 161         JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK(tool);
 162         for (String a: args.split("\\s+")) {
 163             if (a.startsWith("-J")) {
 164                 l.addVMArg(a.substring(2));
 165             } else {
 166                 l.addToolArg(a);
 167             }
 168         }
 169         return ProcessTools.executeCommand(l.getCommand());
 170     }
 171 }