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