ServerIdentityTest.java revision 16238:0ad126a1f49e
1/* 2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24// 25// SunJSSE does not support dynamic system properties, no way to re-use 26// system properties in samevm/agentvm mode. 27// 28 29/* 30 * @test 31 * @bug 4328195 32 * @summary Need to include the alternate subject DN for certs, 33 * https should check for this 34 * @library /javax/net/ssl/templates 35 * @run main/othervm ServerIdentityTest dnsstore localhost 36 * @run main/othervm ServerIdentityTest ipstore 127.0.0.1 37 * 38 * @author Yingxian Wang 39 */ 40 41import java.io.InputStream; 42import java.io.BufferedWriter; 43import java.io.OutputStreamWriter; 44import java.net.HttpURLConnection; 45import java.net.URL; 46import javax.net.ssl.HttpsURLConnection; 47import javax.net.ssl.SSLContext; 48import javax.net.ssl.SSLSocket; 49 50public final class ServerIdentityTest extends SSLSocketTemplate { 51 52 private static String keystore; 53 private static String hostname; 54 private static SSLContext context; 55 56 /* 57 * Run the test case. 58 */ 59 public static void main(String[] args) throws Exception { 60 // Get the customized arguments. 61 initialize(args); 62 63 (new ServerIdentityTest()).run(); 64 } 65 66 @Override 67 protected boolean isCustomizeClientConnection() { 68 return true; 69 } 70 71 @Override 72 protected void runServerApplication(SSLSocket socket) throws Exception { 73 BufferedWriter bw = new BufferedWriter( 74 new OutputStreamWriter(socket.getOutputStream())); 75 bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n"); 76 bw.flush(); 77 socket.getSession().invalidate(); 78 } 79 80 @Override 81 protected void runClientApplication(int serverPort) throws Exception { 82 URL url = new URL( 83 "https://" + hostname + ":" + serverPort + "/index.html"); 84 85 HttpURLConnection urlc = null; 86 InputStream is = null; 87 try { 88 urlc = (HttpURLConnection)url.openConnection(); 89 is = urlc.getInputStream(); 90 } finally { 91 if (is != null) { 92 is.close(); 93 } 94 if (urlc != null) { 95 urlc.disconnect(); 96 } 97 } 98 } 99 100 @Override 101 protected SSLContext createServerSSLContext() throws Exception { 102 return context; 103 } 104 105 @Override 106 protected SSLContext createClientSSLContext() throws Exception { 107 return context; 108 } 109 110 private static void initialize(String[] args) throws Exception { 111 keystore = args[0]; 112 hostname = args[1]; 113 114 String password = "changeit"; 115 String keyFilename = 116 System.getProperty("test.src", ".") + "/" + keystore; 117 String trustFilename = 118 System.getProperty("test.src", ".") + "/" + keystore; 119 120 System.setProperty("javax.net.ssl.keyStore", keyFilename); 121 System.setProperty("javax.net.ssl.keyStorePassword", password); 122 System.setProperty("javax.net.ssl.trustStore", trustFilename); 123 System.setProperty("javax.net.ssl.trustStorePassword", password); 124 125 context = SSLContext.getDefault(); 126 HttpsURLConnection.setDefaultSSLSocketFactory( 127 context.getSocketFactory()); 128 } 129} 130