Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java
41161 views
/*1* Copyright (c) 2001, 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*/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.InetAddress;45import java.net.Proxy;46import java.net.URL;47import java.net.UnknownHostException;4849import javax.net.ssl.HttpsURLConnection;50import javax.net.ssl.SSLContext;51import javax.net.ssl.SSLSocket;5253public final class ServerIdentityTest extends SSLSocketTemplate {5455private static String keystore;56private static String hostname;57private static SSLContext context;5859/*60* Run the test case.61*/62public static void main(String[] args) throws Exception {63// Get the customized arguments.64initialize(args);6566(new ServerIdentityTest()).run();67}6869ServerIdentityTest() throws UnknownHostException {70serverAddress = InetAddress.getByName(hostname);71}7273@Override74protected boolean isCustomizedClientConnection() {75return true;76}7778@Override79protected void runServerApplication(SSLSocket socket) throws Exception {80InputStream sslIS = socket.getInputStream();81sslIS.read();82BufferedWriter bw = new BufferedWriter(83new OutputStreamWriter(socket.getOutputStream()));84bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");85bw.flush();86socket.getSession().invalidate();87}8889@Override90protected void runClientApplication(int serverPort) throws Exception {91URL url = new URL(92"https://" + hostname + ":" + serverPort + "/index.html");9394HttpURLConnection urlc = null;95InputStream is = null;96try {97urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);98is = urlc.getInputStream();99} finally {100if (is != null) {101is.close();102}103if (urlc != null) {104urlc.disconnect();105}106}107}108109@Override110protected SSLContext createServerSSLContext() throws Exception {111return context;112}113114@Override115protected SSLContext createClientSSLContext() throws Exception {116return context;117}118119private static void initialize(String[] args) throws Exception {120keystore = args[0];121hostname = args[1];122123String password = "changeit";124String keyFilename =125System.getProperty("test.src", ".") + "/" + keystore;126String trustFilename =127System.getProperty("test.src", ".") + "/" + keystore;128129System.setProperty("javax.net.ssl.keyStore", keyFilename);130System.setProperty("javax.net.ssl.keyStorePassword", password);131System.setProperty("javax.net.ssl.trustStore", trustFilename);132System.setProperty("javax.net.ssl.trustStorePassword", password);133134context = SSLContext.getDefault();135HttpsURLConnection.setDefaultSSLSocketFactory(136context.getSocketFactory());137}138}139140141