0N/A/*
2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
5361N/A//
5361N/A// SunJSSE does not support dynamic system properties, no way to re-use
5361N/A// system properties in samevm/agentvm mode.
5361N/A//
5361N/A
0N/A/*
0N/A * @test
0N/A * @bug 1234567
0N/A * @summary Use this template to help speed your client/server tests.
5361N/A * @run main/othervm SSLSocketTemplate
0N/A * @author Brad Wetmore
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport javax.net.ssl.*;
0N/A
0N/Apublic class SSLSocketTemplate {
0N/A
0N/A /*
0N/A * =============================================================
0N/A * Set the various variables needed for the tests, then
0N/A * specify what tests to run on each side.
0N/A */
0N/A
0N/A /*
0N/A * Should we run the client or server in a separate thread?
0N/A * Both sides can throw exceptions, but do you have a preference
0N/A * as to which side should be the main thread.
0N/A */
0N/A static boolean separateServerThread = false;
0N/A
0N/A /*
0N/A * Where do we find the keystores?
0N/A */
0N/A static String pathToStores = "../etc";
0N/A static String keyStoreFile = "keystore";
0N/A static String trustStoreFile = "truststore";
0N/A static String passwd = "passphrase";
0N/A
0N/A /*
0N/A * Is the server ready to serve?
0N/A */
0N/A volatile static boolean serverReady = false;
0N/A
0N/A /*
0N/A * Turn on SSL debugging?
0N/A */
0N/A static boolean debug = false;
0N/A
0N/A /*
0N/A * If the client or server is doing some kind of object creation
0N/A * that the other side depends on, and that thread prematurely
0N/A * exits, you may experience a hang. The test harness will
0N/A * terminate all hung threads after its timeout has expired,
0N/A * currently 3 minutes by default, but you might try to be
0N/A * smart about it....
0N/A */
0N/A
0N/A /*
0N/A * Define the server side of the test.
0N/A *
0N/A * If the server prematurely exits, serverReady will be set to true
0N/A * to avoid infinite hangs.
0N/A */
0N/A void doServerSide() throws Exception {
0N/A SSLServerSocketFactory sslssf =
0N/A (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
0N/A SSLServerSocket sslServerSocket =
0N/A (SSLServerSocket) sslssf.createServerSocket(serverPort);
0N/A
0N/A serverPort = sslServerSocket.getLocalPort();
0N/A
0N/A /*
0N/A * Signal Client, we're ready for his connect.
0N/A */
0N/A serverReady = true;
0N/A
0N/A SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
0N/A InputStream sslIS = sslSocket.getInputStream();
0N/A OutputStream sslOS = sslSocket.getOutputStream();
0N/A
0N/A sslIS.read();
0N/A sslOS.write(85);
0N/A sslOS.flush();
0N/A
0N/A sslSocket.close();
0N/A }
0N/A
0N/A /*
0N/A * Define the client side of the test.
0N/A *
0N/A * If the server prematurely exits, serverReady will be set to true
0N/A * to avoid infinite hangs.
0N/A */
0N/A void doClientSide() throws Exception {
0N/A
0N/A /*
0N/A * Wait for server to get started.
0N/A */
0N/A while (!serverReady) {
0N/A Thread.sleep(50);
0N/A }
0N/A
0N/A SSLSocketFactory sslsf =
0N/A (SSLSocketFactory) SSLSocketFactory.getDefault();
0N/A SSLSocket sslSocket = (SSLSocket)
0N/A sslsf.createSocket("localhost", serverPort);
0N/A
0N/A InputStream sslIS = sslSocket.getInputStream();
0N/A OutputStream sslOS = sslSocket.getOutputStream();
0N/A
0N/A sslOS.write(280);
0N/A sslOS.flush();
0N/A sslIS.read();
0N/A
0N/A sslSocket.close();
0N/A }
0N/A
0N/A /*
0N/A * =============================================================
0N/A * The remainder is just support stuff
0N/A */
0N/A
0N/A // use any free port by default
0N/A volatile int serverPort = 0;
0N/A
0N/A volatile Exception serverException = null;
0N/A volatile Exception clientException = null;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A String keyFilename =
0N/A System.getProperty("test.src", ".") + "/" + pathToStores +
0N/A "/" + keyStoreFile;
0N/A String trustFilename =
0N/A System.getProperty("test.src", ".") + "/" + pathToStores +
0N/A "/" + trustStoreFile;
0N/A
0N/A System.setProperty("javax.net.ssl.keyStore", keyFilename);
0N/A System.setProperty("javax.net.ssl.keyStorePassword", passwd);
0N/A System.setProperty("javax.net.ssl.trustStore", trustFilename);
0N/A System.setProperty("javax.net.ssl.trustStorePassword", passwd);
0N/A
0N/A if (debug)
0N/A System.setProperty("javax.net.debug", "all");
0N/A
0N/A /*
0N/A * Start the tests.
0N/A */
0N/A new SSLSocketTemplate();
0N/A }
0N/A
0N/A Thread clientThread = null;
0N/A Thread serverThread = null;
0N/A
0N/A /*
0N/A * Primary constructor, used to drive remainder of the test.
0N/A *
0N/A * Fork off the other side, then do your work.
0N/A */
0N/A SSLSocketTemplate() throws Exception {
0N/A try {
0N/A if (separateServerThread) {
0N/A startServer(true);
0N/A startClient(false);
0N/A } else {
0N/A startClient(true);
0N/A startServer(false);
0N/A }
0N/A } catch (Exception e) {
0N/A // swallow for now. Show later
0N/A }
0N/A
0N/A /*
0N/A * Wait for other side to close down.
0N/A */
0N/A if (separateServerThread) {
0N/A serverThread.join();
0N/A } else {
0N/A clientThread.join();
0N/A }
0N/A
0N/A /*
0N/A * When we get here, the test is pretty much over.
0N/A * Which side threw the error?
0N/A */
0N/A Exception local;
0N/A Exception remote;
0N/A String whichRemote;
0N/A
0N/A if (separateServerThread) {
0N/A remote = serverException;
0N/A local = clientException;
0N/A whichRemote = "server";
0N/A } else {
0N/A remote = clientException;
0N/A local = serverException;
0N/A whichRemote = "client";
0N/A }
0N/A
0N/A /*
0N/A * If both failed, return the curthread's exception, but also
0N/A * print the remote side Exception
0N/A */
0N/A if ((local != null) && (remote != null)) {
0N/A System.out.println(whichRemote + " also threw:");
0N/A remote.printStackTrace();
0N/A System.out.println();
0N/A throw local;
0N/A }
0N/A
0N/A if (remote != null) {
0N/A throw remote;
0N/A }
0N/A
0N/A if (local != null) {
0N/A throw local;
0N/A }
0N/A }
0N/A
0N/A void startServer(boolean newThread) throws Exception {
0N/A if (newThread) {
0N/A serverThread = new Thread() {
0N/A public void run() {
0N/A try {
0N/A doServerSide();
0N/A } catch (Exception e) {
0N/A /*
0N/A * Our server thread just died.
0N/A *
0N/A * Release the client, if not active already...
0N/A */
0N/A System.err.println("Server died...");
0N/A serverReady = true;
0N/A serverException = e;
0N/A }
0N/A }
0N/A };
0N/A serverThread.start();
0N/A } else {
0N/A try {
0N/A doServerSide();
0N/A } catch (Exception e) {
0N/A serverException = e;
0N/A } finally {
0N/A serverReady = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A void startClient(boolean newThread) throws Exception {
0N/A if (newThread) {
0N/A clientThread = new Thread() {
0N/A public void run() {
0N/A try {
0N/A doClientSide();
0N/A } catch (Exception e) {
0N/A /*
0N/A * Our client thread just died.
0N/A */
0N/A System.err.println("Client died...");
0N/A clientException = e;
0N/A }
0N/A }
0N/A };
0N/A clientThread.start();
0N/A } else {
0N/A try {
0N/A doClientSide();
0N/A } catch (Exception e) {
0N/A clientException = e;
0N/A }
0N/A }
0N/A }
0N/A}