Path: blob/master/test/jdk/sun/security/ssl/GenSSLConfigs/main.java
41152 views
/*1* @test2* @build TestThread Traffic Handler ServerHandler ServerThread ClientThread3* @run main/othervm/timeout=140 -Djsse.enableCBCProtection=false main4* @summary Make sure that different configurations of SSL sockets work5* @key randomness6*/78/*9* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.10* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.11*12* This code is free software; you can redistribute it and/or modify it13* under the terms of the GNU General Public License version 2 only, as14* published by the Free Software Foundation.15*16* This code is distributed in the hope that it will be useful, but WITHOUT17* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or18* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License19* version 2 for more details (a copy is included in the LICENSE file that20* accompanied this code).21*22* You should have received a copy of the GNU General Public License version23* 2 along with this work; if not, write to the Free Software Foundation,24* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.25*26* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA27* or visit www.oracle.com if you need additional information or have any28* questions.29*/3031import java.io.*;32import java.security.SecureRandom;33import java.security.KeyStore;34import java.util.Date;35import java.util.Vector;36import java.util.ArrayList;3738import javax.net.ssl.*;3940public class main41{42// NOTE: "prng" doesn't need to be a SecureRandom4344private static final SecureRandom prng45= new SecureRandom ();46private static SSLContext sslContext;4748private static void usage() {49System.err.println (50"usage: tests.ssl.main default|random|cipher_suite [nthreads]");51}5253/**54* Runs a test ... there are a variety of configurations, and the way55* they're invoked is subject to change. This program can support56* single and multiple process tests, but by default it's set up for57* single process testing.58*59* <P> The first commandline argument identifies a test configuration.60* Currently identified configurations include "default", "random".61*62* <P> The second commandline argument identifies the number of63* client threads to use.64*/65public static void main (String argv [])66{67String config;68int NTHREADS;6970initContext();71String supported [] = sslContext.getSocketFactory()72.getSupportedCipherSuites();7374// Strip out any Kerberos Suites for now.75ArrayList list = new ArrayList(supported.length);76for (int i = 0; i < supported.length; i++) {77if (!supported[i].startsWith("TLS_KRB5")) {78list.add(supported[i]);79}80}81supported = (String [])list.toArray(new String [0]);8283if (argv.length == 2) {84config = argv [0];85NTHREADS = Integer.parseInt (argv [1]);86} else if (argv.length == 1) {87config = argv [0];88NTHREADS = 15;89} else {90/* temporaraly changed to make it run under jtreg with91* default configuration, when no input parameters are92* given93*/94//usage();95//return;96config = "default";97NTHREADS = supported.length;98}99100// More options ... port #. different clnt/svr configs,101// cipher suites, etc.102103ServerThread server = new ServerThread (0, NTHREADS, sslContext);104Vector clients = new Vector (NTHREADS);105106if (!(config.equals("default") || config.equals("random")))107supported = new String[] {config};108109System.out.println("Supported cipher suites are:");110for(int i=0; i < supported.length; i++) {111System.out.println(supported[i]);112}113114setConfig (server, config, supported);115116// if (OS != Win95)117server.setUseMT (true);118119server.start ();120server.waitTillReady ();121122//123// iterate over all cipher suites124//125int next = 0;126int passes = 0;127128if (usesRandom (config))129next = nextUnsignedRandom ();130131for (int i = 0; i < NTHREADS; i++, next++) {132ClientThread client = new ClientThread (server.getServerPort(), sslContext);133String cipher [] = new String [1];134135setConfig (client, config, supported);136next = next % supported.length;137cipher [0] = supported [next];138client.setBasicCipherSuites (cipher);139140//141// Win95 has been observed to choke if you throw many142// connections at it. So we make it easy to unthread143// everything; it can be handy outside Win95 too.144//145client.start ();146if (!server.getUseMT ()) {147waitForClient (client);148if (client.passed ())149passes++;150} else151clients.addElement (client);152}153154while (!clients.isEmpty ()) {155ClientThread client;156157client = (ClientThread) clients.elementAt (0);158clients.removeElement (client);159waitForClient (client);160if (client.passed ())161passes++;162}163164System.out.println ("SUMMARY: threads = " + NTHREADS165+ ", passes = " + passes);166}167168169//170// Rather than replicating code, a helper function!171//172private static void waitForClient (Thread client)173{174while (true)175try {176client.join ();177178// System.out.println ("Joined: " + client.getName ());179break;180} catch (InterruptedException e) {181continue;182}183}184185private static void initContext()186{187try {188String testRoot = System.getProperty("test.src", ".");189System.setProperty("javax.net.ssl.trustStore", testRoot190+ "/../../../../javax/net/ssl/etc/truststore");191192KeyStore ks = KeyStore.getInstance("JKS");193ks.load(new FileInputStream(testRoot194+ "/../../../../javax/net/ssl/etc/truststore"),195"passphrase".toCharArray());196KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");197kmf.init(ks, "passphrase".toCharArray());198TrustManagerFactory tmf =199TrustManagerFactory.getInstance("SunX509");200tmf.init(ks);201sslContext = SSLContext.getInstance("SSL");202sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);203} catch (Throwable t) {204// oh well; ignore it, the tester presumably intends this205System.out.println("Failed to read keystore/truststore file... Continuing");206t.printStackTrace();207}208}209210private static int nextUnsignedRandom ()211{212int retval = prng.nextInt ();213214if (retval < 0)215return -retval;216else217return retval;218}219220221//222// Randomness in testing can be good and bad ... covers more223// territory, but not reproducibly.224//225private static boolean usesRandom (String config)226{227return config.equalsIgnoreCase ("random");228}229230231private static void setConfig (232TestThread test,233String config,234String supported []235)236{237test.setBasicCipherSuites (supported);238test.setOutput (System.out);239test.setVerbosity (3);240241if (test instanceof ClientThread) {242test.setListenHandshake (true);243test.setIterations (20);244}245246// XXX role reversals !!!247248//249// We can establish a reasonable degree of variability250// on the test data and configs ... expecting that the251// diagnostics will identify any problems that exist.252// Client and server must agree on these things.253//254// Unless we do this, only the SSL nonces and ephemeral255// keys will be unpredictable in a given test run. Those256// affect only the utmost innards of SSL, details which257// are not visible to applications.258//259if (usesRandom (config)) {260int rand = nextUnsignedRandom ();261262if (test instanceof ClientThread)263test.setIterations (rand % 35);264265if ((rand & 0x080) == 0)266test.setInitiateHandshake (true);267// if ((rand & 0x040) == 0)268// test.setDoRenegotiate (true);269270test.setPRNG (new SecureRandom ());271}272}273}274275276