Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java
38889 views
/*1* Copyright (c) 2001, 2016, 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*/2223//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 432819531* @summary Need to include the alternate subject DN for certs,32* https should check for this33* @library /javax/net/ssl/templates34* @run main/othervm ServerIdentityTest dnsstore localhost35* @run main/othervm ServerIdentityTest ipstore 127.0.0.136*37* @author Yingxian Wang38*/3940import java.io.InputStream;41import java.io.BufferedWriter;42import java.io.OutputStreamWriter;43import java.net.HttpURLConnection;44import java.net.URL;4546import javax.net.ssl.HttpsURLConnection;47import javax.net.ssl.SSLContext;48import javax.net.ssl.SSLSocket;4950public final class ServerIdentityTest extends SSLSocketTemplate {5152private static String keystore;53private static String hostname;54private static SSLContext context;5556/*57* Run the test case.58*/59public static void main(String[] args) throws Exception {60// Get the customized arguments.61initialize(args);6263(new ServerIdentityTest()).run();64}6566@Override67protected boolean isCustomizedClientConnection() {68return true;69}7071@Override72protected void runServerApplication(SSLSocket socket) throws Exception {73InputStream sslIS = socket.getInputStream();74sslIS.read();75BufferedWriter bw = new BufferedWriter(76new OutputStreamWriter(socket.getOutputStream()));77bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");78bw.flush();79socket.getSession().invalidate();80}8182@Override83protected void runClientApplication(int serverPort) throws Exception {84URL url = new URL(85"https://" + hostname + ":" + serverPort + "/index.html");8687HttpURLConnection urlc = null;88InputStream is = null;89try {90urlc = (HttpURLConnection)url.openConnection();91is = urlc.getInputStream();92} finally {93if (is != null) {94is.close();95}96if (urlc != null) {97urlc.disconnect();98}99}100}101102@Override103protected SSLContext createServerSSLContext() throws Exception {104return context;105}106107@Override108protected SSLContext createClientSSLContext() throws Exception {109return context;110}111112private static void initialize(String[] args) throws Exception {113keystore = args[0];114hostname = args[1];115116String password = "changeit";117String keyFilename =118System.getProperty("test.src", ".") + "/" + keystore;119String trustFilename =120System.getProperty("test.src", ".") + "/" + keystore;121122System.setProperty("javax.net.ssl.keyStore", keyFilename);123System.setProperty("javax.net.ssl.keyStorePassword", password);124System.setProperty("javax.net.ssl.trustStore", trustFilename);125System.setProperty("javax.net.ssl.trustStorePassword", password);126127context = SSLContext.getDefault();128HttpsURLConnection.setDefaultSSLSocketFactory(129context.getSocketFactory());130}131}132133134