Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkProcUtils.java
41154 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.nio.file.Path;25import java.util.ArrayList;26import java.util.List;27import java.util.Map;28import java.util.StringJoiner;2930/*31* Utilities for JDK process peers.32*/33public class JdkProcUtils {3435public static final String PROP_HOST = "test.host";36public static final String PROP_PORT = "test.port";3738public static final String PROP_SEC_PROPS_FILE = "java.security.properties";39public static final String PROP_PROTOCOLS = "test.protocols";40public static final String PROP_CIPHER_SUITES = "test.cipher.suites";41public static final String PROP_TRUSTED_CERTS = "test.trusted.certs";42public static final String PROP_EE_CERTS = "test.ee.certs";43public static final String PROP_CLIENT_AUTH = "test.client.auth";44public static final String PROP_SERVER_NAMES = "test.server.names";45public static final String PROP_APP_PROTOCOLS = "test.app.protocols";46public static final String PROP_NAMED_GROUPS = "jdk.tls.namedGroups";4748/*49* Converts a Cert instance to a string, which contains the field values of50* the Cert. The values are separated by comma.51*/52public static String certToStr(Cert cert) {53return Utilities.join(cert.keyAlgo, cert.sigAlgo, cert.hashAlgo,54cert.certMaterials, cert.keyMaterials);55}5657/*58* Converts multiple Certs to a string. The value strings of the Certs are59* separated by semicolon.60*/61public static String certsToStr(Cert[] certs) {62StringJoiner joiner = new StringJoiner(Utilities.PARAM_DELIMITER);63for (Cert cert : certs) {64joiner.add(certToStr(cert));65}66return joiner.toString();67}6869/*70* Converts a string, which contains the field values of a Cert,71* to a Cert instance.72*/73public static Cert strToCert(String certStr) {74String[] values = Utilities.split(certStr);75String keyAlgo = values[0];76String sigAlgo = values[1];77String hashAlgo = values[2];78String certMaterials = values[3];79String keyMaterials = values.length == 5 ? values[4] : null;80return new Cert(KeyAlgorithm.valueOf(keyAlgo),81SignatureAlgorithm.valueOf(sigAlgo),82HashAlgorithm.valueOf(hashAlgo), certMaterials, keyMaterials);83}8485/*86* Converts a string to multiple Certs.87*/88public static Cert[] strToCerts(String certsStr) {89String[] certStrs = Utilities.split(certsStr, Utilities.PARAM_DELIMITER);90Cert[] certs = new Cert[certStrs.length];91for(int i = 0; i < certStrs.length; i++) {92certs[i] = strToCert(certStrs[i]);93}94return certs;95}9697public static CertTuple createCertTuple(String trustedCertsStr,98String eeCertsStr) {99Cert[] trustedCerts = strToCerts(trustedCertsStr);100Cert[] eeCerts = strToCerts(eeCertsStr);101return new CertTuple(trustedCerts, eeCerts);102}103104/*105* Executes java program.106* It can redirect the output to a local file if necessary,107* and will returns the process for later application.108*/109public static Process java(Path javaPath, Class<?> clazz,110Map<String, String> props, Path outputPath) throws IOException {111ProcessBuilder pb = createProcessBuilder(javaPath, clazz, props);112if (outputPath != null) {113pb.redirectOutput(outputPath.toFile());114}115return pb.start();116}117118private static ProcessBuilder createProcessBuilder(Path javaPath,119Class<?> clazz, Map<String, String> props) {120List<String> cmds = new ArrayList<>();121cmds.add(javaPath.toString());122123if (props != null) {124for (Map.Entry<String, String> prop : props.entrySet()) {125cmds.add("-D" + prop.getKey() + "=" + prop.getValue());126}127}128129cmds.add("-cp");130cmds.add(Utilities.TEST_CLASSPATH);131cmds.add(clazz.getName());132ProcessBuilder pb = new ProcessBuilder(cmds);133pb.redirectErrorStream(true);134return pb;135}136}137138139