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