SecurityTools.java revision 2437:905aa8122624
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.util.ArrayList; 27import java.util.Collections; 28import java.util.List; 29import jdk.test.lib.process.OutputAnalyzer; 30import jdk.test.lib.process.ProcessTools; 31 32public class SecurityTools { 33 34 public static final String NO_ALIAS = null; 35 36 // keytool 37 38 public static OutputAnalyzer keytool(List<String> options) 39 throws Throwable { 40 41 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("keytool") 42 .addVMArg("-Duser.language=en") 43 .addVMArg("-Duser.country=US"); 44 for (String option : options) { 45 if (option.startsWith("-J")) { 46 launcher.addVMArg(option.substring(2)); 47 } else { 48 launcher.addToolArg(option); 49 } 50 } 51 return ProcessTools.executeCommand(launcher.getCommand()); 52 } 53 54 public static OutputAnalyzer keytool(String options) throws Throwable { 55 return keytool(options.split("\\s+")); 56 } 57 58 public static OutputAnalyzer keytool(String... options) throws Throwable { 59 return keytool(List.of(options)); 60 } 61 62 // jarsigner 63 64 public static OutputAnalyzer jarsigner(String jar, String alias, 65 List<String> options) throws Throwable { 66 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner") 67 .addVMArg("-Duser.language=en") 68 .addVMArg("-Duser.country=US"); 69 for (String option : options) { 70 if (option.startsWith("-J")) { 71 launcher.addVMArg(option.substring(2)); 72 } else { 73 launcher.addToolArg(option); 74 } 75 } 76 launcher.addToolArg(jar); 77 if (alias != null) { 78 launcher.addToolArg(alias); 79 } 80 return ProcessTools.executeCommand(launcher.getCommand()); 81 } 82 83 public static OutputAnalyzer jarsigner(String jar, String alias, 84 String options) throws Throwable { 85 86 return jarsigner(jar, alias, options.split("\\s+")); 87 } 88 89 public static OutputAnalyzer jarsigner(String jar, String alias, 90 String... options) throws Throwable { 91 92 return jarsigner(jar, alias, List.of(options)); 93 } 94 95 public static OutputAnalyzer sign(String jar, String alias, String... options) 96 throws Throwable { 97 98 return jarsigner(jar, alias, 99 mergeOptions("-J-Djava.security.egd=file:/dev/./urandom", options)); 100 } 101 102 public static OutputAnalyzer verify(String jar, String... options) 103 throws Throwable { 104 105 return jarsigner(jar, NO_ALIAS, mergeOptions("-verify", options)); 106 } 107 108 // helper methods 109 110 private static List<String> mergeOptions( 111 String firstOption, String... secondPart) { 112 113 return mergeOptions(List.of(firstOption), secondPart); 114 } 115 116 private static List<String> mergeOptions( 117 List<String> firstPart, String... secondPart) { 118 119 List<String> options = new ArrayList<>(firstPart); 120 Collections.addAll(options, secondPart); 121 return options; 122 } 123} 124 125