Path: blob/master/test/jdk/sun/security/ssl/SSLSessionImpl/HashCodeMissing.java
41152 views
/*1* Copyright (c) 2003, 2011, 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* @test25* @bug 491089226* @summary 4518403 was not properly fixed. hashcode should be hashCode.27* @run main/othervm HashCodeMissing28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;37import java.lang.reflect.*;3839public class HashCodeMissing {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = false;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../../../../javax/net/ssl/etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static String passwd = "passphrase";6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Turn on SSL debugging?69*/70static boolean debug = false;7172/*73* If the client or server is doing some kind of object creation74* that the other side depends on, and that thread prematurely75* exits, you may experience a hang. The test harness will76* terminate all hung threads after its timeout has expired,77* currently 3 minutes by default, but you might try to be78* smart about it....79*/8081/*82* Define the server side of the test.83*84* If the server prematurely exits, serverReady will be set to true85* to avoid infinite hangs.86*/87void doServerSide() throws Exception {88SSLServerSocketFactory sslssf =89(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();90SSLServerSocket sslServerSocket =91(SSLServerSocket) sslssf.createServerSocket(serverPort);9293serverPort = sslServerSocket.getLocalPort();9495/*96* Signal Client, we're ready for his connect.97*/98serverReady = true;99100SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();101InputStream sslIS = sslSocket.getInputStream();102OutputStream sslOS = sslSocket.getOutputStream();103104sslIS.read();105sslOS.write(85);106sslOS.flush();107108sslSocket.close();109}110111/*112* Define the client side of the test.113*114* If the server prematurely exits, serverReady will be set to true115* to avoid infinite hangs.116*/117void doClientSide() throws Exception {118119/*120* Wait for server to get started.121*/122while (!serverReady) {123Thread.sleep(50);124}125126SSLSocketFactory sslsf =127(SSLSocketFactory) SSLSocketFactory.getDefault();128SSLSocket sslSocket = (SSLSocket)129sslsf.createSocket("localhost", serverPort);130131InputStream sslIS = sslSocket.getInputStream();132OutputStream sslOS = sslSocket.getOutputStream();133134sslOS.write(280);135sslOS.flush();136sslIS.read();137138SSLSession sslSession = sslSocket.getSession();139140sslSocket.close();141142Class clazz = sslSession.getClass();143144/*145* Real test is done here146*/147System.out.println("Getting 'hashCode'");148Method method = clazz.getDeclaredMethod("hashCode", new Class [0]);149System.out.println("Method = " + method);150}151152/*153* =============================================================154* The remainder is just support stuff155*/156157// use any free port by default158volatile int serverPort = 0;159160volatile Exception serverException = null;161volatile Exception clientException = null;162163public static void main(String[] args) throws Exception {164String keyFilename =165System.getProperty("test.src", "./") + "/" + pathToStores +166"/" + keyStoreFile;167String trustFilename =168System.getProperty("test.src", "./") + "/" + pathToStores +169"/" + trustStoreFile;170171System.setProperty("javax.net.ssl.keyStore", keyFilename);172System.setProperty("javax.net.ssl.keyStorePassword", passwd);173System.setProperty("javax.net.ssl.trustStore", trustFilename);174System.setProperty("javax.net.ssl.trustStorePassword", passwd);175176if (debug)177System.setProperty("javax.net.debug", "all");178179/*180* Start the tests.181*/182new HashCodeMissing();183}184185Thread clientThread = null;186Thread serverThread = null;187188/*189* Primary constructor, used to drive remainder of the test.190*191* Fork off the other side, then do your work.192*/193HashCodeMissing() throws Exception {194try {195if (separateServerThread) {196startServer(true);197startClient(false);198} else {199startClient(true);200startServer(false);201}202} catch (Exception e) {203//swallow for now. Show later204}205206/*207* Wait for other side to close down.208*/209if (separateServerThread) {210serverThread.join();211} else {212clientThread.join();213}214215/*216* When we get here, the test is pretty much over.217* Which side threw the error?218*/219Exception local;220Exception remote;221String whichRemote;222223if (separateServerThread) {224remote = serverException;225local = clientException;226whichRemote = "server";227} else {228remote = clientException;229local = serverException;230whichRemote = "client";231}232233/*234* If both failed, return the curthread's exception, but also235* print the remote side Exception236*/237if ((local != null) && (remote != null)) {238System.out.println(whichRemote + " also threw:");239remote.printStackTrace();240System.out.println();241throw local;242}243244if (remote != null) {245throw remote;246}247248if (local != null) {249throw local;250}251}252253void startServer(boolean newThread) throws Exception {254if (newThread) {255serverThread = new Thread() {256public void run() {257try {258doServerSide();259} catch (Exception e) {260/*261* Our server thread just died.262*263* Release the client, if not active already...264*/265System.err.println("Server died...");266serverReady = true;267serverException = e;268}269}270};271serverThread.start();272} else {273try {274doServerSide();275} catch (Exception e) {276serverException = e;277} finally {278serverReady = true;279}280}281}282283void startClient(boolean newThread) throws Exception {284if (newThread) {285clientThread = new Thread() {286public void run() {287try {288doClientSide();289} catch (Exception e) {290/*291* Our client thread just died.292*/293System.err.println("Client died...");294clientException = e;295}296}297};298clientThread.start();299} else {300try {301doClientSide();302} catch (Exception e) {303clientException = e;304}305}306}307}308309310