Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkProcServer.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.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.util.HashMap;28import java.util.Map;2930/*31* A JDK server process.32*/33public class JdkProcServer extends AbstractServer {3435public static final Path PORT_LOG = Paths.get("port.log");3637private final Jdk jdk;38private final Map<String, String> props = new HashMap<>();3940private Process process;4142public JdkProcServer(Builder builder) throws Exception {43jdk = 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}6667props.put(JdkProcUtils.PROP_CLIENT_AUTH,68String.valueOf(builder.getClientAuth()));6970if (builder.getServerNames() != null) {71props.put(JdkProcUtils.PROP_SERVER_NAMES,72Utilities.join(builder.getServerNames()));73}7475if (builder.getAppProtocols() != null) {76props.put(JdkProcUtils.PROP_APP_PROTOCOLS,77Utilities.join(builder.getAppProtocols()));78}7980if (builder.getNamedGroups() != null) {81props.put(JdkProcUtils.PROP_NAMED_GROUPS,82Utilities.join(Utilities.namedGroupsToStrs(83builder.getNamedGroups())));84}8586props.put("test.src", Utilities.TEST_SRC);87if (Utilities.DEBUG) {88props.put("javax.net.debug", "all");89}90}9192public static class Builder extends AbstractServer.Builder {9394private Jdk jdk;9596private Path secPropsFile;9798public Jdk getJdk() {99return jdk;100}101102public Builder setJdk(Jdk jdk) {103this.jdk = jdk;104return this;105}106107public Path getSecPropsFile() {108return secPropsFile;109}110111public Builder setSecPropsFile(Path secPropsFile) {112this.secPropsFile = secPropsFile;113return this;114}115116@Override117public JdkProcServer build() throws Exception {118return new JdkProcServer(this);119}120}121122@Override123public Product getProduct() {124return jdk;125}126127@Override128public int getPort() throws IOException {129System.out.println("Waiting for port log...");130if (!Utilities.waitFor(server -> server.isAlive() && readPort() > 0, this)) {131throw new RuntimeException("Server doesn't start in time.");132}133134return readPort();135}136137@Override138public boolean isAlive() {139return Utilities.isAliveProcess(process);140}141142@Override143public void accept() throws IOException {144process = JdkProcUtils.java(getProduct().getPath(), getClass(), props,145getLogPath());146try {147process.waitFor();148} catch (InterruptedException e) {149throw new RuntimeException("Server was interrupted!", e);150}151152if (process.exitValue() != 0) {153throw new SSLTestException("Server exited abnormally!");154}155}156157@Override158public void signalStop() {159if (isAlive()) {160Utilities.destroyProcess(process);161}162}163164@Override165public void close() throws IOException {166printLog();167deletePort();168deleteLog();169}170171private static int readPort() {172try {173return Integer.valueOf(new String(Files.readAllBytes(PORT_LOG)));174} catch (Exception e) {175return 0;176}177}178179private static void deletePort() throws IOException {180Utilities.deleteFile(PORT_LOG);181}182183private static void savePort(int port) throws IOException {184Files.write(PORT_LOG, String.valueOf(port).getBytes(Utilities.CHARSET));185}186187public static void main(String[] args) throws Exception {188String trustedCertsStr = System.getProperty(JdkProcUtils.PROP_TRUSTED_CERTS);189String eeCertsStr = System.getProperty(JdkProcUtils.PROP_EE_CERTS);190191String protocolsStr = System.getProperty(JdkProcUtils.PROP_PROTOCOLS);192String cipherSuitesStr = System.getProperty(JdkProcUtils.PROP_CIPHER_SUITES);193194boolean clientAuth = Boolean.getBoolean(JdkProcUtils.PROP_CLIENT_AUTH);195String serverNamesStr = System.getProperty(JdkProcUtils.PROP_SERVER_NAMES);196String appProtocolsStr = System.getProperty(JdkProcUtils.PROP_APP_PROTOCOLS);197198JdkServer.Builder builder = new JdkServer.Builder();199builder.setCertTuple(JdkProcUtils.createCertTuple(200trustedCertsStr, eeCertsStr));201if (!Utilities.isEmpty(protocolsStr)) {202builder.setProtocols(Utilities.strToEnums(203Protocol.class, protocolsStr));204}205if (!Utilities.isEmpty(cipherSuitesStr)) {206builder.setCipherSuites(Utilities.strToEnums(207CipherSuite.class, cipherSuitesStr));208}209builder.setClientAuth(clientAuth);210if (!Utilities.isEmpty(serverNamesStr)) {211builder.setServerNames(Utilities.split(serverNamesStr));212}213if (!Utilities.isEmpty(appProtocolsStr)) {214builder.setAppProtocols(Utilities.split(appProtocolsStr));215}216217try (JdkServer server = builder.build()) {218int port = server.getPort();219System.out.println("port=" + port);220savePort(port);221server.accept();222}223}224}225226227