Path: blob/master/test/jdk/javax/rmi/ssl/SSLSocketParametersTest.java
41149 views
/*1* Copyright (c) 2004, 2007, 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*/2223import java.io.IOException;24import java.io.File;25import java.io.Serializable;26import java.net.ServerSocket;27import java.net.Socket;28import java.rmi.Remote;29import java.rmi.RemoteException;30import java.rmi.server.RMIClientSocketFactory;31import java.rmi.server.RMIServerSocketFactory;32import java.rmi.server.UnicastRemoteObject;33import javax.net.ssl.SSLContext;34import javax.rmi.ssl.SslRMIClientSocketFactory;35import javax.rmi.ssl.SslRMIServerSocketFactory;3637public class SSLSocketParametersTest implements Serializable {3839public interface Hello extends Remote {40public String sayHello() throws RemoteException;41}4243public class HelloImpl extends UnicastRemoteObject implements Hello {4445public HelloImpl(int port,46RMIClientSocketFactory csf,47RMIServerSocketFactory ssf)48throws RemoteException {49super(port, csf, ssf);50}5152public String sayHello() {53return "Hello World!";54}5556public Remote runServer() throws IOException {57System.out.println("Inside HelloImpl::runServer");58// Get a remote stub for this RMI object59//60Remote stub = toStub(this);61System.out.println("Stub = " + stub);62return stub;63}64}6566public class HelloClient {6768public void runClient(Remote stub) throws IOException {69System.out.println("Inside HelloClient::runClient");70// "obj" is the identifier that we'll use to refer71// to the remote object that implements the "Hello"72// interface73Hello obj = (Hello) stub;74String message = obj.sayHello();75System.out.println(message);76}77}7879public class ClientFactory extends SslRMIClientSocketFactory {8081public ClientFactory() {82super();83}8485public Socket createSocket(String host, int port) throws IOException {86System.out.println("ClientFactory::Calling createSocket(" +87host + "," + port + ")");88return super.createSocket(host, port);89}90}9192public class ServerFactory extends SslRMIServerSocketFactory {9394public ServerFactory() {95super();96}9798public ServerFactory(String[] ciphers,99String[] protocols,100boolean need) {101super(ciphers, protocols, need);102}103104public ServerFactory(SSLContext context,105String[] ciphers,106String[] protocols,107boolean need) {108super(context, ciphers, protocols, need);109}110111public ServerSocket createServerSocket(int port) throws IOException {112System.out.println("ServerFactory::Calling createServerSocket(" +113port + ")");114return super.createServerSocket(port);115}116}117118public void runTest(String[] args) {119120int test = Integer.parseInt(args[0]);121122String msg1 = "Running SSLSocketParametersTest [" + test + "]";123String msg2 = "SSLSocketParametersTest [" + test + "] PASSED!";124String msg3 = "SSLSocketParametersTest [" + test + "] FAILED!";125126switch (test) {127case 1: /* default constructor - default config */128System.out.println(msg1);129try {130HelloImpl server = new HelloImpl(1310,132new ClientFactory(),133new ServerFactory());134Remote stub = server.runServer();135HelloClient client = new HelloClient();136client.runClient(stub);137System.out.println(msg2);138} catch (Exception e) {139System.out.println(msg3 + " Exception: " + e.toString());140e.printStackTrace(System.out);141System.exit(1);142}143break;144case 2: /* non-default constructor - default config */145System.out.println(msg1);146try {147HelloImpl server = new HelloImpl(1480,149new ClientFactory(),150new ServerFactory(null,151null,152false));153Remote stub = server.runServer();154HelloClient client = new HelloClient();155client.runClient(stub);156System.out.println(msg2);157} catch (Exception e) {158System.out.println(msg3 + " Exception: " + e.toString());159e.printStackTrace(System.out);160System.exit(1);161}162break;163case 3: /* needClientAuth=true */164System.out.println(msg1);165try {166HelloImpl server = new HelloImpl(1670,168new ClientFactory(),169new ServerFactory(null,170null,171null,172true));173Remote stub = server.runServer();174HelloClient client = new HelloClient();175client.runClient(stub);176System.out.println(msg2);177} catch (Exception e) {178System.out.println(msg3 + " Exception: " + e.toString());179e.printStackTrace(System.out);180System.exit(1);181}182break;183case 4: /* server side dummy_ciphersuite */184System.out.println(msg1);185try {186HelloImpl server = new HelloImpl(1870,188new ClientFactory(),189new ServerFactory(SSLContext.getDefault(),190new String[] {"dummy_ciphersuite"},191null,192false));193Remote stub = server.runServer();194HelloClient client = new HelloClient();195client.runClient(stub);196System.out.println(msg3);197System.exit(1);198} catch (Exception e) {199System.out.println(msg2 + " Exception: " + e.toString());200System.exit(0);201}202break;203case 5: /* server side dummy_protocol */204System.out.println(msg1);205try {206HelloImpl server = new HelloImpl(2070,208new ClientFactory(),209new ServerFactory(null,210new String[] {"dummy_protocol"},211false));212Remote stub = server.runServer();213HelloClient client = new HelloClient();214client.runClient(stub);215System.out.println(msg3);216System.exit(1);217} catch (Exception e) {218System.out.println(msg2 + " Exception: " + e.toString());219System.exit(0);220}221break;222case 6: /* client side dummy_ciphersuite */223System.out.println(msg1);224try {225System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",226"dummy_ciphersuite");227HelloImpl server = new HelloImpl(2280,229new ClientFactory(),230new ServerFactory());231Remote stub = server.runServer();232HelloClient client = new HelloClient();233client.runClient(stub);234System.out.println(msg3);235System.exit(1);236} catch (Exception e) {237System.out.println(msg2 + " Exception: " + e.toString());238System.exit(0);239}240break;241case 7: /* client side dummy_protocol */242System.out.println(msg1);243try {244System.setProperty("javax.rmi.ssl.client.enabledProtocols",245"dummy_protocol");246HelloImpl server = new HelloImpl(2470,248new ClientFactory(),249new ServerFactory());250Remote stub = server.runServer();251HelloClient client = new HelloClient();252client.runClient(stub);253System.out.println(msg3);254System.exit(1);255} catch (Exception e) {256System.out.println(msg2 + " Exception: " + e.toString());257System.exit(0);258}259break;260default:261throw new IllegalArgumentException("invalid test number");262}263}264265public static void main(String[] args) {266// Set keystore properties (server-side)267//268final String keystore = System.getProperty("test.src") +269File.separator + "keystore";270System.out.println("KeyStore = " + keystore);271System.setProperty("javax.net.ssl.keyStore", keystore);272System.setProperty("javax.net.ssl.keyStorePassword", "password");273274// Set truststore properties (client-side)275//276final String truststore = System.getProperty("test.src") +277File.separator + "truststore";278System.out.println("TrustStore = " + truststore);279System.setProperty("javax.net.ssl.trustStore", truststore);280System.setProperty("javax.net.ssl.trustStorePassword", "trustword");281282// Run test283//284SSLSocketParametersTest test = new SSLSocketParametersTest();285test.runTest(args);286System.exit(0);287}288}289290291