Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkProcClient.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.nio.file.Paths;26import java.security.Security;27import java.util.Arrays;28import java.util.HashMap;29import java.util.List;30import java.util.Map;3132/*33* A JDK client process.34*/35public class JdkProcClient extends AbstractClient {3637private final Jdk jdk;38private final Map<String, String> props = new HashMap<>();3940private Process process;4142public JdkProcClient(Builder builder) {43this.jdk = builder.getJdk();4445if (builder.getSecPropsFile() != null) {46props.put(JdkProcUtils.PROP_SEC_PROPS_FILE,47builder.getSecPropsFile().toString());48}4950if (builder.getCertTuple() != null) {51props.put(JdkProcUtils.PROP_TRUSTED_CERTS,52JdkProcUtils.certsToStr(builder.getCertTuple().trustedCerts));53props.put(JdkProcUtils.PROP_EE_CERTS,54JdkProcUtils.certsToStr(builder.getCertTuple().endEntityCerts));55}5657if (builder.getProtocols() != null) {58props.put(JdkProcUtils.PROP_PROTOCOLS,59Utilities.join(Utilities.enumsToStrs(builder.getProtocols())));60}6162if (builder.getCipherSuites() != null) {63props.put(JdkProcUtils.PROP_CIPHER_SUITES,64Utilities.join(Utilities.enumsToStrs(builder.getCipherSuites())));65}6667if (builder.getServerNames() != null) {68props.put(JdkProcUtils.PROP_SERVER_NAMES,69Utilities.join(builder.getServerNames()));70}7172if (builder.getAppProtocols() != null) {73props.put(JdkProcUtils.PROP_APP_PROTOCOLS,74Utilities.join(builder.getAppProtocols()));75}7677if (builder.getNamedGroups() != null) {78props.put(JdkProcUtils.PROP_NAMED_GROUPS,79Utilities.join(Utilities.namedGroupsToStrs(80builder.getNamedGroups())));81}8283props.put("test.src", Utilities.TEST_SRC);84if (Utilities.DEBUG) {85props.put("javax.net.debug", "all");86}87}8889public static class Builder extends AbstractClient.Builder {9091private Jdk jdk;9293private Path secPropsFile;9495public Jdk getJdk() {96return jdk;97}9899public Builder setJdk(Jdk jdk) {100this.jdk = jdk;101return this;102}103104public Path getSecPropsFile() {105return secPropsFile;106}107108public Builder setSecPropsFile(Path secPropsFile) {109this.secPropsFile = secPropsFile;110return this;111}112113@Override114public JdkProcClient build() {115return new JdkProcClient(this);116}117}118119@Override120public Jdk getProduct() {121return jdk;122}123124@Override125public void connect(String host, int port) throws IOException {126props.put(JdkProcUtils.PROP_HOST, host);127props.put(JdkProcUtils.PROP_PORT, port + "");128129process = JdkProcUtils.java(getProduct().getPath(), getClass(), props,130getLogPath());131try {132process.waitFor();133} catch (InterruptedException e) {134throw new RuntimeException("Client was interrupted!", e);135}136137if (process.exitValue() != 0) {138throw new SSLTestException("Client exited abnormally!");139}140}141142@Override143protected Path getLogPath() {144return Paths.get("client.log");145}146147@Override148public void close() throws IOException {149printLog();150deleteLog();151}152153public static void main(String[] args) throws Exception {154String trustedCertsStr = System.getProperty(JdkProcUtils.PROP_TRUSTED_CERTS);155String eeCertsStr = System.getProperty(JdkProcUtils.PROP_EE_CERTS);156157String protocolsStr = System.getProperty(JdkProcUtils.PROP_PROTOCOLS);158String cipherSuitesStr = System.getProperty(JdkProcUtils.PROP_CIPHER_SUITES);159160String serverNamesStr = System.getProperty(JdkProcUtils.PROP_SERVER_NAMES);161String appProtocolsStr = System.getProperty(JdkProcUtils.PROP_APP_PROTOCOLS);162163// Re-enable TLSv1 and TLSv1.1 since client depends on them164removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");165166JdkClient.Builder builder = new JdkClient.Builder();167builder.setCertTuple(JdkProcUtils.createCertTuple(168trustedCertsStr, eeCertsStr));169if (!Utilities.isEmpty(protocolsStr)) {170builder.setProtocols(Utilities.strToEnums(171Protocol.class, protocolsStr));172}173if (!Utilities.isEmpty(cipherSuitesStr)) {174builder.setCipherSuites(Utilities.strToEnums(175CipherSuite.class, cipherSuitesStr));176}177if (!Utilities.isEmpty(serverNamesStr)) {178builder.setServerNames(Utilities.split(serverNamesStr));179}180if (!Utilities.isEmpty(appProtocolsStr)) {181builder.setAppProtocols(Utilities.split(appProtocolsStr));182}183184String host = System.getProperty(JdkProcUtils.PROP_HOST);185int port = Integer.getInteger(JdkProcUtils.PROP_PORT);186187try(JdkClient client = builder.build()) {188client.connect(host, port);189}190}191192/**193* Removes the specified protocols from the jdk.tls.disabledAlgorithms194* security property.195*/196private static void removeFromDisabledTlsAlgs(String... algs) {197List<String> algList = Arrays.asList(algs);198String value = Security.getProperty("jdk.tls.disabledAlgorithms");199StringBuilder newValue = new StringBuilder();200for (String constraint : value.split(",")) {201String tmp = constraint.trim();202if (!algList.contains(tmp)) {203newValue.append(tmp);204newValue.append(",");205}206}207Security.setProperty("jdk.tls.disabledAlgorithms", newValue.toString());208}209}210211212