SecurityTools.java revision 2437:905aa8122624
1193323Sed/*
2193323Sed * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16193323Sed * 2 along with this work; if not, write to the Free Software Foundation,
17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18193323Sed *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20193323Sed * or visit www.oracle.com if you need additional information or have any
21193323Sed * questions.
22193323Sed */
23193323Sed
24193323Sedpackage jdk.test.lib;
25193323Sed
26193323Sedimport java.util.ArrayList;
27193323Sedimport java.util.Collections;
28193323Sedimport java.util.List;
29193323Sedimport jdk.test.lib.process.OutputAnalyzer;
30193323Sedimport jdk.test.lib.process.ProcessTools;
31193323Sed
32193323Sedpublic class SecurityTools {
33193323Sed
34193323Sed    public static final String NO_ALIAS = null;
35193323Sed
36193323Sed    // keytool
37193323Sed
38193323Sed    public static OutputAnalyzer keytool(List<String> options)
39193323Sed            throws Throwable {
40193323Sed
41193323Sed        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("keytool")
42193323Sed                .addVMArg("-Duser.language=en")
43193323Sed                .addVMArg("-Duser.country=US");
44193323Sed        for (String option : options) {
45193323Sed            if (option.startsWith("-J")) {
46193323Sed                launcher.addVMArg(option.substring(2));
47193323Sed            } else {
48193323Sed                launcher.addToolArg(option);
49193323Sed            }
50193323Sed        }
51193323Sed        return ProcessTools.executeCommand(launcher.getCommand());
52194612Sed    }
53193323Sed
54193323Sed    public static OutputAnalyzer keytool(String options) throws Throwable {
55193323Sed        return keytool(options.split("\\s+"));
56193323Sed    }
57193323Sed
58193323Sed    public static OutputAnalyzer keytool(String... options) throws Throwable {
59193323Sed        return keytool(List.of(options));
60193323Sed    }
61193323Sed
62193323Sed    // jarsigner
63193323Sed
64193323Sed    public static OutputAnalyzer jarsigner(String jar, String alias,
65193323Sed            List<String> options) throws Throwable {
66193323Sed        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner")
67193323Sed                .addVMArg("-Duser.language=en")
68193323Sed                .addVMArg("-Duser.country=US");
69193323Sed        for (String option : options) {
70193323Sed            if (option.startsWith("-J")) {
71193323Sed                launcher.addVMArg(option.substring(2));
72193323Sed            } else {
73193323Sed                launcher.addToolArg(option);
74193323Sed            }
75193323Sed        }
76193323Sed        launcher.addToolArg(jar);
77193323Sed        if (alias != null) {
78193323Sed            launcher.addToolArg(alias);
79193323Sed        }
80193323Sed        return ProcessTools.executeCommand(launcher.getCommand());
81193323Sed    }
82193323Sed
83193323Sed    public static OutputAnalyzer jarsigner(String jar, String alias,
84193323Sed            String options) throws Throwable {
85193323Sed
86193323Sed        return jarsigner(jar, alias, options.split("\\s+"));
87193323Sed    }
88193323Sed
89193323Sed    public static OutputAnalyzer jarsigner(String jar, String alias,
90193323Sed            String... options) throws Throwable {
91193323Sed
92193323Sed        return jarsigner(jar, alias, List.of(options));
93193323Sed    }
94193323Sed
95193323Sed    public static OutputAnalyzer sign(String jar, String alias, String... options)
96193323Sed            throws Throwable {
97193323Sed
98193323Sed        return jarsigner(jar, alias,
99193323Sed                mergeOptions("-J-Djava.security.egd=file:/dev/./urandom", options));
100193323Sed    }
101193323Sed
102193323Sed    public static OutputAnalyzer verify(String jar, String... options)
103193323Sed            throws Throwable {
104193323Sed
105193323Sed        return jarsigner(jar, NO_ALIAS, mergeOptions("-verify", options));
106193323Sed    }
107193323Sed
108193323Sed    // helper methods
109193323Sed
110193323Sed    private static List<String> mergeOptions(
111193323Sed            String firstOption, String... secondPart) {
112193323Sed
113193323Sed        return mergeOptions(List.of(firstOption), secondPart);
114193323Sed    }
115193323Sed
116193323Sed    private static List<String> mergeOptions(
117193323Sed            List<String> firstPart, String... secondPart) {
118193323Sed
119193323Sed        List<String> options = new ArrayList<>(firstPart);
120193323Sed        Collections.addAll(options, secondPart);
121193323Sed        return options;
122193323Sed    }
123193323Sed}
124193323Sed
125193323Sed