Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/compatibility/Server.java
38853 views
/*1* Copyright (c) 2017, 2019, 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.File;24import java.io.FileWriter;25import java.io.IOException;26import java.io.InputStream;27import java.io.OutputStream;2829import javax.net.ssl.SSLContext;30import javax.net.ssl.SSLParameters;31import javax.net.ssl.SSLServerSocket;32import javax.net.ssl.SSLServerSocketFactory;33import javax.net.ssl.SSLSocket;3435/*36* A simple SSL socket server.37*/38public class Server {3940private final SSLServerSocket serverSocket;4142public Server(SSLContext context, int port) throws Exception {43SSLServerSocketFactory serverFactory = context.getServerSocketFactory();44serverSocket = (SSLServerSocket) serverFactory.createServerSocket(port);45serverSocket.setSoTimeout(Utils.TIMEOUT);46}4748public Server(Cert[] certs, int port) throws Exception {49this(Utils.createSSLContext(certs), port);50}5152public Server(Cert[] certs) throws Exception {53this(certs, 0);54}5556private void setEnabledCipherSuites(String... cipherSuites) {57serverSocket.setEnabledCipherSuites(cipherSuites);58}5960private void setEnabledProtocols(String... protocols) {61serverSocket.setEnabledProtocols(protocols);62}6364private void setNeedClientAuth(boolean needClientAuth) {65serverSocket.setNeedClientAuth(needClientAuth);66}6768private void setApplicationProtocols(String... protocols) {69SSLParameters params = serverSocket.getSSLParameters();70params.setApplicationProtocols(protocols);71serverSocket.setSSLParameters(params);72}7374public int getPort() {75return serverSocket.getLocalPort();76}7778private void accept() throws IOException {79SSLSocket socket = null;80try {81socket = (SSLSocket) serverSocket.accept();8283InputStream in = socket.getInputStream();84in.read();8586OutputStream out = socket.getOutputStream();87out.write('S');88out.flush();89} finally {90if (socket != null) {91socket.close();92}93}94}9596public void close() throws IOException {97serverSocket.close();98}99100public static void main(String[] args) throws IOException {101System.out.println("----- Server start -----");102String protocol = System.getProperty(Utils.PROP_PROTOCOL);103String cipherSuite = System.getProperty(Utils.PROP_CIPHER_SUITE);104boolean clientAuth105= Boolean.getBoolean(Utils.PROP_CLIENT_AUTH);106String appProtocols = System.getProperty(Utils.PROP_APP_PROTOCOLS);107boolean supportsALPN108= Boolean.getBoolean(Utils.PROP_SUPPORTS_ALPN_ON_SERVER);109boolean negativeCase110= Boolean.getBoolean(Utils.PROP_NEGATIVE_CASE_ON_SERVER);111112System.out.println(Utils.join(Utils.PARAM_DELIMITER,113"ServerJDK=" + System.getProperty(Utils.PROP_SERVER_JDK),114"Protocol=" + protocol,115"CipherSuite=" + cipherSuite,116"ClientAuth=" + clientAuth,117"AppProtocols=" + appProtocols));118119Status status = Status.SUCCESS;120Server server = null;121try {122server = new Server(Cert.getCerts(CipherSuite.cipherSuite(cipherSuite)));123System.out.println("port=" + server.getPort());124server.setNeedClientAuth(clientAuth);125server.setEnabledProtocols(protocol);126server.setEnabledCipherSuites(cipherSuite);127if (appProtocols != null) {128if (supportsALPN) {129server.setApplicationProtocols(130Utils.split(appProtocols, Utils.VALUE_DELIMITER));131} else {132System.out.println(133"Ignored due to server doesn't support ALPN.");134}135}136137savePort(server.getPort());138server.accept();139140status = negativeCase ? Status.UNEXPECTED_SUCCESS : Status.SUCCESS;141} catch (Exception exception) {142status = Utils.handleException(exception, negativeCase);143} finally {144if (server != null) {145server.close();146}147148deletePortFile();149}150151System.out.println("STATUS: " + status);152System.out.println("----- Server end -----");153}154155private static void deletePortFile() {156File portFile = new File(Utils.PORT_LOG);157if (portFile.exists() && !portFile.delete()) {158throw new RuntimeException("Cannot delete port log");159}160}161162private static void savePort(int port) throws IOException {163FileWriter writer = null;164try {165writer = new FileWriter(new File(Utils.PORT_LOG));166writer.write(port + "");167} finally {168if (writer != null) {169writer.close();170}171}172}173}174175176