Path: blob/master/test/jdk/sun/security/tools/keytool/DefaultOptions.java
41152 views
/*1* Copyright (c) 2014, 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 802319726* @summary Pre-configured command line options for keytool and jarsigner27* @library /test/lib28*/2930import jdk.test.lib.SecurityTools;31import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.process.ProcessTools;3334import java.nio.file.Files;35import java.nio.file.Path;36import java.util.List;3738public class DefaultOptions {3940public static void main(String[] args) throws Throwable {4142Files.write(Path.of("kt.conf"), List.of(43"# A Pre-configured options file",44"keytool.all = -storepass:env PASS -keypass:env PASS "45+ "-keystore ${user.dir}/ks -debug",46"keytool.genkey = -keyalg ec -ext bc",47"keytool.delete = -keystore nothing"));4849// kt.conf is read50keytool("-conf kt.conf -genkeypair -dname CN=A -alias a")51.shouldHaveExitValue(0);52keytool("-conf kt.conf -list -alias a -v")53.shouldHaveExitValue(0)54.shouldMatch("Signature algorithm name.*ECDSA")55.shouldContain("BasicConstraints");5657// kt.conf is read, and dup multi-valued options processed as expected58keytool("-conf kt.conf -genkeypair -dname CN=B -alias b -ext ku=ds")59.shouldHaveExitValue(0);60keytool("-conf kt.conf -list -alias b -v")61.shouldHaveExitValue(0)62.shouldContain("BasicConstraints")63.shouldContain("DigitalSignature");6465// Single-valued option in command section override all66keytool("-conf kt.conf -delete -alias a")67.shouldNotHaveExitValue(0);6869// Single-valued option on command line overrides again70keytool("-conf kt.conf -delete -alias b -keystore ks")71.shouldHaveExitValue(0);7273// Error cases7475// File does not exist76keytool("-conf no-such-file -help -list")77.shouldNotHaveExitValue(0);7879// Cannot have both standard name (-genkeypair) and legacy name (-genkey)80Files.write(Path.of("bad.conf"), List.of(81"keytool.all = -storepass:env PASS -keypass:env PASS -keystore ks",82"keytool.genkeypair = -keyalg rsa",83"keytool.genkey = -keyalg ec"));8485keytool("-conf bad.conf -genkeypair -alias me -dname cn=me")86.shouldNotHaveExitValue(0);8788// Unknown options are rejected by tool89Files.write(Path.of("bad.conf"), List.of(90"keytool.all=-unknown"));9192keytool("-conf bad.conf -help -list").shouldNotHaveExitValue(0);9394// System property must be present95Files.write(Path.of("bad.conf"), List.of(96"keytool.all = -keystore ${no.such.prop}"));9798keytool("-conf bad.conf -help -list").shouldNotHaveExitValue(0);99}100101// Run keytool with one environment variable PASS=changeit102static OutputAnalyzer keytool(String cmd) throws Throwable {103ProcessBuilder pb = SecurityTools.getProcessBuilder(104"keytool", List.of(cmd.trim().split("\\s+")));105pb.environment().put("PASS", "changeit");106return ProcessTools.executeCommand(pb);107}108}109110111