1 /* 2 * Copyright (c) 2020, 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 import java.io.IOException; 25 import java.nio.file.Path; 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Map; 29 import java.util.StringJoiner; 30 31 /* 32 * Utilities for JDK process peers. 33 */ 34 public class JdkProcUtils { 35 36 public static final String PROP_HOST = "test.host"; 37 public static final String PROP_PORT = "test.port"; 38 39 public static final String PROP_SEC_PROPS_FILE = "java.security.properties"; 40 public static final String PROP_PROTOCOLS = "test.protocols"; 41 public static final String PROP_CIPHER_SUITES = "test.cipher.suites"; 42 public static final String PROP_TRUSTED_CERTS = "test.trusted.certs"; 43 public static final String PROP_EE_CERTS = "test.ee.certs"; 44 public static final String PROP_CLIENT_AUTH = "test.client.auth"; 45 public static final String PROP_SERVER_NAMES = "test.server.names"; 46 public static final String PROP_APP_PROTOCOLS = "test.app.protocols"; 47 public static final String PROP_NAMED_GROUPS = "jdk.tls.namedGroups"; 48 49 /* 50 * Converts a Cert instance to a string, which contains the field values of 51 * the Cert. The values are separated by comma. 52 */ certToStr(Cert cert)53 public static String certToStr(Cert cert) { 54 return Utilities.join(cert.keyAlgo, cert.sigAlgo, cert.hashAlgo, 55 cert.certMaterials, cert.keyMaterials); 56 } 57 58 /* 59 * Converts multiple Certs to a string. The value strings of the Certs are 60 * separated by semicolon. 61 */ certsToStr(Cert[] certs)62 public static String certsToStr(Cert[] certs) { 63 StringJoiner joiner = new StringJoiner(Utilities.PARAM_DELIMITER); 64 for (Cert cert : certs) { 65 joiner.add(certToStr(cert)); 66 } 67 return joiner.toString(); 68 } 69 70 /* 71 * Converts a string, which contains the field values of a Cert, 72 * to a Cert instance. 73 */ strToCert(String certStr)74 public static Cert strToCert(String certStr) { 75 String[] values = Utilities.split(certStr); 76 String keyAlgo = values[0]; 77 String sigAlgo = values[1]; 78 String hashAlgo = values[2]; 79 String certMaterials = values[3]; 80 String keyMaterials = values.length == 5 ? values[4] : null; 81 return new Cert(KeyAlgorithm.valueOf(keyAlgo), 82 SignatureAlgorithm.valueOf(sigAlgo), 83 HashAlgorithm.valueOf(hashAlgo), certMaterials, keyMaterials); 84 } 85 86 /* 87 * Converts a string to multiple Certs. 88 */ strToCerts(String certsStr)89 public static Cert[] strToCerts(String certsStr) { 90 String[] certStrs = Utilities.split(certsStr, Utilities.PARAM_DELIMITER); 91 Cert[] certs = new Cert[certStrs.length]; 92 for(int i = 0; i < certStrs.length; i++) { 93 certs[i] = strToCert(certStrs[i]); 94 } 95 return certs; 96 } 97 createCertTuple(String trustedCertsStr, String eeCertsStr)98 public static CertTuple createCertTuple(String trustedCertsStr, 99 String eeCertsStr) { 100 Cert[] trustedCerts = strToCerts(trustedCertsStr); 101 Cert[] eeCerts = strToCerts(eeCertsStr); 102 return new CertTuple(trustedCerts, eeCerts); 103 } 104 105 /* 106 * Executes java program. 107 * It can redirect the output to a local file if necessary, 108 * and will returns the process for later application. 109 */ java(Path javaPath, Class<?> clazz, Map<String, String> props, Path outputPath)110 public static Process java(Path javaPath, Class<?> clazz, 111 Map<String, String> props, Path outputPath) throws IOException { 112 ProcessBuilder pb = createProcessBuilder(javaPath, clazz, props); 113 if (outputPath != null) { 114 pb.redirectOutput(outputPath.toFile()); 115 } 116 return pb.start(); 117 } 118 createProcessBuilder(Path javaPath, Class<?> clazz, Map<String, String> props)119 private static ProcessBuilder createProcessBuilder(Path javaPath, 120 Class<?> clazz, Map<String, String> props) { 121 List<String> cmds = new ArrayList<>(); 122 cmds.add(javaPath.toString()); 123 124 if (props != null) { 125 for (Map.Entry<String, String> prop : props.entrySet()) { 126 cmds.add("-D" + prop.getKey() + "=" + prop.getValue()); 127 } 128 } 129 130 cmds.add("-cp"); 131 cmds.add(Utilities.TEST_CLASSPATH); 132 cmds.add(clazz.getName()); 133 ProcessBuilder pb = new ProcessBuilder(cmds); 134 pb.redirectErrorStream(true); 135 return pb; 136 } 137 } 138