SecurityTools.java revision 2455:5991cc73ea0b
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 24package jdk.test.lib; 25 26import java.io.File; 27import java.io.IOException; 28import java.nio.file.Files; 29import java.nio.file.Path; 30import java.nio.file.Paths; 31import java.util.Arrays; 32import java.util.List; 33import java.util.stream.Collectors; 34import java.util.stream.Stream; 35 36import jdk.test.lib.process.OutputAnalyzer; 37import jdk.test.lib.process.ProcessTools; 38 39public class SecurityTools { 40 41 public static final String RESPONSE_FILE = "security_tools_response.txt"; 42 43 private static ProcessBuilder getProcessBuilder(String tool, List<String> args) { 44 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(tool) 45 .addVMArg("-Duser.language=en") 46 .addVMArg("-Duser.country=US") 47 .addVMArg("-Djava.security.egd=file:/dev/./urandom"); 48 for (String arg : args) { 49 if (arg.startsWith("-J")) { 50 launcher.addVMArg(arg.substring(2)); 51 } else { 52 launcher.addToolArg(arg); 53 } 54 } 55 String[] cmds = launcher.getCommand(); 56 String cmdLine = Arrays.stream(cmds).collect(Collectors.joining(" ")); 57 System.out.println("Command line: [" + cmdLine + "]"); 58 return new ProcessBuilder(cmds); 59 } 60 61 // keytool 62 63 public static OutputAnalyzer keytool(List<String> args) 64 throws Exception { 65 66 ProcessBuilder pb = getProcessBuilder("keytool", args); 67 68 Path p = Paths.get(RESPONSE_FILE); 69 if (!Files.exists(p)) { 70 Files.createFile(p); 71 } 72 pb.redirectInput(ProcessBuilder.Redirect.from(new File(RESPONSE_FILE))); 73 74 try { 75 return ProcessTools.executeProcess(pb); 76 } finally { 77 Files.delete(p); 78 } 79 } 80 81 // Only call this if there is no white space in every argument 82 public static OutputAnalyzer keytool(String args) throws Exception { 83 return keytool(args.split("\\s+")); 84 } 85 86 public static OutputAnalyzer keytool(String... args) throws Exception { 87 return keytool(List.of(args)); 88 } 89 90 public static void setResponse(String... responses) throws IOException { 91 String text; 92 if (responses.length > 0) { 93 text = Stream.of(responses).collect( 94 Collectors.joining("\n", "", "\n")); 95 } else { 96 text = ""; 97 } 98 Files.write(Paths.get(RESPONSE_FILE), text.getBytes()); 99 } 100 101 // jarsigner 102 103 public static OutputAnalyzer jarsigner(List<String> args) 104 throws Exception { 105 return ProcessTools.executeProcess( 106 getProcessBuilder("jarsigner", args)); 107 } 108 109 // Only call this if there is no white space in every argument 110 public static OutputAnalyzer jarsigner(String args) throws Exception { 111 112 return jarsigner(args.split("\\s+")); 113 } 114 115 public static OutputAnalyzer jarsigner(String... args) throws Exception { 116 return jarsigner(List.of(args)); 117 } 118} 119 120