Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SetClientMode.java
41152 views
/*1* Copyright (c) 2005, 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// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 622362429* @ignore this test does not grant to work. The handshake may have completed30* when getSession() return. Please update or remove this test case.31* @summary SSLSocket.setUseClientMode() fails to throw expected32* IllegalArgumentException33* @run main/othervm SetClientMode34*/3536/*37* Attempts to replicate a TCK test failure which creates SSLServerSockets38* and then runs client threads which connect and start handshaking. Once39* handshaking is begun the server side attempts to invoke40* SSLSocket.setUseClientMode() on one or the other of the ends of the41* connection, expecting an IllegalArgumentException.42*43* If the server side of the connection tries setUseClientMode() we44* see the expected exception. If the setting is tried on the45* client side SSLSocket, we do *not* see the exception, except46* occasionally on the very first iteration.47*/4849import java.io.*;50import java.lang.*;51import java.net.*;52import javax.net.ssl.*;53import java.security.*;54import java.security.cert.*;5556public class SetClientMode {57private static String[] algorithms = {"TLS", "SSL", "SSLv3", "TLS"};58volatile int serverPort = 0;5960/*61* Where do we find the keystores?62*/63static String pathToStores = "../../../../javax/net/ssl/etc";64static String keyStoreFile = "keystore";65static String trustStoreFile = "truststore";66static String passwd = "passphrase";676869public SetClientMode() {70// trivial constructor71}7273public static void main(String[] args) throws Exception {74String keyFilename =75System.getProperty("test.src", "./") + "/" + pathToStores +76"/" + keyStoreFile;77String trustFilename =78System.getProperty("test.src", "./") + "/" + pathToStores +79"/" + trustStoreFile;8081System.setProperty("javax.net.ssl.keyStore", keyFilename);82System.setProperty("javax.net.ssl.keyStorePassword", passwd);83System.setProperty("javax.net.ssl.trustStore", trustFilename);84System.setProperty("javax.net.ssl.trustStorePassword", passwd);8586new SetClientMode().run();87}8889public void run() throws Exception {90for (int i = 0; i < algorithms.length; i++) {91testCombo( algorithms[i] );92}93}9495public void testCombo(String algorithm) throws Exception {96Exception modeException = null ;9798// Create a server socket99SSLServerSocketFactory ssf =100(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();101SSLServerSocket serverSocket =102(SSLServerSocket)ssf.createServerSocket(serverPort);103serverPort = serverSocket.getLocalPort();104105// Create a client socket106SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();107SSLSocket clientSocket = (SSLSocket)sf.createSocket(108InetAddress.getLocalHost(),109serverPort );110111// Create a client which will use the SSLSocket to talk to the server112SocketClient client = new SocketClient(clientSocket);113114// Start the client and then accept any connection115client.start();116117SSLSocket connectedSocket = (SSLSocket)serverSocket.accept();118119// force handshaking to complete120connectedSocket.getSession();121122try {123// Now try invoking setClientMode() on one124// or the other of our two sockets. We expect125// to see an IllegalArgumentException because126// handshaking has begun.127clientSocket.setUseClientMode(false);128129modeException = new Exception("no IllegalArgumentException");130} catch (IllegalArgumentException iae) {131System.out.println("succeeded, we can't set the client mode");132} catch (Exception e) {133modeException = e;134} finally {135// Shut down.136connectedSocket.close();137serverSocket.close();138139if (modeException != null) {140throw modeException;141}142}143144return;145}146147// A thread-based client which does nothing except148// start handshaking on the socket it's given.149class SocketClient extends Thread {150SSLSocket clientsideSocket;151Exception clientException = null;152boolean done = false;153154public SocketClient( SSLSocket s ) {155clientsideSocket = s;156}157158public void run() {159try {160clientsideSocket.startHandshake();161162// If we were to invoke setUseClientMode()163// here, the expected exception will happen.164//clientsideSocket.getSession();165//clientsideSocket.setUseClientMode( false );166} catch ( Exception e ) {167e.printStackTrace();168clientException = e;169} finally {170done = true;171try {172clientsideSocket.close();173} catch ( IOException e ) {174// eat it175}176}177return;178}179180boolean isDone() {181return done;182}183184Exception getException() {185return clientException;186}187}188}189190191