SecurityTools.java revision 2455:5991cc73ea0b
174462Salfred/*
274462Salfred * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
374462Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4261057Smav *
5261057Smav * This code is free software; you can redistribute it and/or modify it
6261057Smav * under the terms of the GNU General Public License version 2 only, as
7261057Smav * published by the Free Software Foundation.
8261057Smav *
9261057Smav * This code is distributed in the hope that it will be useful, but WITHOUT
10261057Smav * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11261057Smav * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12261057Smav * version 2 for more details (a copy is included in the LICENSE file that
13261057Smav * accompanied this code).
14261057Smav *
15261057Smav * You should have received a copy of the GNU General Public License version
16261057Smav * 2 along with this work; if not, write to the Free Software Foundation,
17261057Smav * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18261057Smav *
19261057Smav * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20261057Smav * or visit www.oracle.com if you need additional information or have any
21261057Smav * questions.
22261057Smav */
23261057Smav
24261057Smavpackage jdk.test.lib;
25261057Smav
26261057Smavimport java.io.File;
27261057Smavimport java.io.IOException;
28261057Smavimport java.nio.file.Files;
29261057Smavimport java.nio.file.Path;
3074462Salfredimport java.nio.file.Paths;
3174462Salfredimport java.util.Arrays;
3274462Salfredimport java.util.List;
3374462Salfredimport java.util.stream.Collectors;
3474462Salfredimport java.util.stream.Stream;
3574462Salfred
3674462Salfredimport jdk.test.lib.process.OutputAnalyzer;
3774462Salfredimport jdk.test.lib.process.ProcessTools;
3874462Salfred
3974462Salfredpublic class SecurityTools {
4074462Salfred
4174462Salfred    public static final String RESPONSE_FILE = "security_tools_response.txt";
4274462Salfred
4374462Salfred    private static ProcessBuilder getProcessBuilder(String tool, List<String> args) {
4474462Salfred        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(tool)
4574462Salfred                .addVMArg("-Duser.language=en")
4674462Salfred                .addVMArg("-Duser.country=US")
4774462Salfred                .addVMArg("-Djava.security.egd=file:/dev/./urandom");
4874462Salfred        for (String arg : args) {
4974462Salfred            if (arg.startsWith("-J")) {
5074462Salfred                launcher.addVMArg(arg.substring(2));
5174462Salfred            } else {
5274462Salfred                launcher.addToolArg(arg);
5374462Salfred            }
5474462Salfred        }
5574462Salfred        String[] cmds = launcher.getCommand();
5674462Salfred        String cmdLine = Arrays.stream(cmds).collect(Collectors.joining(" "));
5774462Salfred        System.out.println("Command line: [" + cmdLine + "]");
5874462Salfred        return new ProcessBuilder(cmds);
5974462Salfred    }
6074462Salfred
6174462Salfred    // keytool
6274462Salfred
6374462Salfred    public static OutputAnalyzer keytool(List<String> args)
6474462Salfred            throws Exception {
6574462Salfred
6674462Salfred        ProcessBuilder pb = getProcessBuilder("keytool", args);
6793032Simp
6893032Simp        Path p = Paths.get(RESPONSE_FILE);
6974462Salfred        if (!Files.exists(p)) {
7074462Salfred            Files.createFile(p);
7174462Salfred        }
7274462Salfred        pb.redirectInput(ProcessBuilder.Redirect.from(new File(RESPONSE_FILE)));
7374462Salfred
7474462Salfred        try {
7574462Salfred            return ProcessTools.executeProcess(pb);
7674462Salfred        } finally {
7774462Salfred            Files.delete(p);
7874462Salfred        }
7993032Simp    }
8074462Salfred
8174462Salfred    // Only call this if there is no white space in every argument
8274462Salfred    public static OutputAnalyzer keytool(String args) throws Exception {
8374462Salfred        return keytool(args.split("\\s+"));
8474462Salfred    }
8574462Salfred
8674462Salfred    public static OutputAnalyzer keytool(String... args) throws Exception {
8793032Simp        return keytool(List.of(args));
8874462Salfred    }
8974462Salfred
9074462Salfred    public static void setResponse(String... responses) throws IOException {
9174462Salfred        String text;
9274462Salfred        if (responses.length > 0) {
9374462Salfred            text = Stream.of(responses).collect(
9474462Salfred                    Collectors.joining("\n", "", "\n"));
9593032Simp        } else {
9693032Simp            text = "";
9793032Simp        }
9874462Salfred        Files.write(Paths.get(RESPONSE_FILE), text.getBytes());
9974462Salfred    }
10074462Salfred
10174462Salfred    // jarsigner
10274462Salfred
10374462Salfred    public static OutputAnalyzer jarsigner(List<String> args)
10474462Salfred            throws Exception {
10593032Simp        return ProcessTools.executeProcess(
10674462Salfred                getProcessBuilder("jarsigner", args));
10774462Salfred    }
10874462Salfred
10974462Salfred    // Only call this if there is no white space in every argument
11074462Salfred    public static OutputAnalyzer jarsigner(String args) throws Exception {
11174462Salfred
11293032Simp        return jarsigner(args.split("\\s+"));
11374462Salfred    }
11474462Salfred
11574462Salfred    public static OutputAnalyzer jarsigner(String... args) throws Exception {
116        return jarsigner(List.of(args));
117    }
118}
119
120